]> git.saurik.com Git - cycript.git/blobdiff - Output.cpp
Ported back to the iPhone.
[cycript.git] / Output.cpp
index f648a575d35a55fea68369205210a189b20921d7..dec2df5bbce221f444521ab403896f6b2eba2ff5 100644 (file)
@@ -1,9 +1,6 @@
 #include "Parser.hpp"
 
-#include <iostream>
 #include <iomanip>
-
-#include <objc/runtime.h>
 #include <sstream>
 
 _finline CYFlags operator ~(CYFlags rhs) {
@@ -23,7 +20,7 @@ _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
 }
 
 _finline CYFlags CYLeft(CYFlags flags) {
-    return flags;
+    return flags & ~CYNoDangle;
 }
 
 _finline CYFlags CYRight(CYFlags flags) {
@@ -31,11 +28,9 @@ _finline CYFlags CYRight(CYFlags flags) {
 }
 
 _finline CYFlags CYCenter(CYFlags flags) {
-    return CYRight(flags);
+    return CYLeft(CYRight(flags));
 }
 
-#define CYPA 16
-
 void CYOutput::Terminate() {
     out_ << ';';
     mode_ = NoMode;
@@ -67,6 +62,10 @@ CYOutput &CYOutput::operator <<(char rhs) {
             mode_ = Terminated;
             goto done;
         }
+    } else if (rhs == '+') {
+        if (mode_ == NoPlus)
+            out_ << ' ';
+        mode_ = NoPlus;
     } else if (rhs == '-') {
         if (mode_ == NoHyphen)
             out_ << ' ';
@@ -92,6 +91,7 @@ CYOutput &CYOutput::operator <<(const char *rhs) {
     if (mode_ == Terminated)
         out_ << ';';
     else if (
+        mode_ == NoPlus && *rhs == '+' ||
         mode_ == NoHyphen && *rhs == '-' ||
         mode_ == NoLetter && WordEndRange_[*rhs]
     )
@@ -106,20 +106,6 @@ CYOutput &CYOutput::operator <<(const char *rhs) {
     return *this;
 }
 
-void OutputBody(CYOutput &out, CYStatement *body) {
-    out << ' ' << '{' << '\n';
-    ++out.indent_;
-    if (body != NULL)
-        body->Multiple(out);
-    --out.indent_;
-    out << '\t' << '}';
-}
-
-void CYAddressOf::Output(CYOutput &out, CYFlags flags) const {
-    rhs_->Output(out, 1, CYLeft(flags));
-    out << ".$cya()";
-}
-
 void CYArgument::Output(CYOutput &out) const {
     if (name_ != NULL) {
         out << *name_;
@@ -163,8 +149,22 @@ void CYAssignment::Output(CYOutput &out, CYFlags flags) const {
     rhs_->Output(out, Precedence(), CYRight(flags));
 }
 
+void CYBlock::Output(CYOutput &out) const {
+    out << '{' << '\n';
+    ++out.indent_;
+    if (statements_ != NULL)
+        statements_->Multiple(out);
+    --out.indent_;
+    out << '\t' << '}';
+}
+
 void CYBlock::Output(CYOutput &out, CYFlags flags) const {
-    statements_->Single(out, flags);
+    if (statements_ == NULL)
+        out.Terminate();
+    else if (statements_->next_ == NULL)
+        statements_->Single(out, flags);
+    else
+        Output(out);
 }
 
 void CYBoolean::Output(CYOutput &out, CYFlags flags) const {
@@ -189,55 +189,7 @@ void CYCall::Output(CYOutput &out, CYFlags flags) const {
 }
 
 void CYCatch::Output(CYOutput &out) const {
-    out << "catch" << ' ' << '(' << *name_ << ')' << ' ' << '{';
-    if (code_ != NULL)
-        code_->Multiple(out);
-    out << '}';
-}
-
-void CYCategory::Output(CYOutput &out, CYFlags flags) const {
-    out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
-    out << "$cyp=object_getClass($cys);";
-    out << "$cyc=$cys;";
-    if (messages_ != NULL)
-        messages_->Output(out, true);
-    out << "})(";
-    name_->ClassName(out, true);
-    out << ')';
-    out << ';';
-}
-
-void CYClass::Output(CYOutput &out, CYFlags flags) const {
-    // XXX: I don't necc. need the ()s
-    out << "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
-    out << "$cyp=object_getClass($cys);";
-    out << "$cyc=objc_allocateClassPair($cys,";
-    if (name_ != NULL)
-        name_->ClassName(out, false);
-    else
-        out << "$cyq(\"CY$\")";
-    out << ",0);";
-    out << "$cym=object_getClass($cyc);";
-    if (fields_ != NULL)
-        fields_->Output(out);
-    if (messages_ != NULL)
-        messages_->Output(out, false);
-    out << "objc_registerClassPair($cyc);";
-    out << "return $cyc;";
-    out << "}(";
-    if (super_ != NULL)
-        super_->Output(out, CYPA, CYNoFlags);
-    else
-        out << "null";
-    out << "))";
-}
-
-void CYClassExpression::Output(CYOutput &out, CYFlags flags) const {
-    CYClass::Output(out, flags);
-}
-
-void CYClassStatement::Output(CYOutput &out, CYFlags flags) const {
-    CYClass::Output(out, flags);
+    out << ' ' << "catch" << ' ' << '(' << *name_ << ')' << ' ' << code_;
 }
 
 void CYCompound::Output(CYOutput &out, CYFlags flags) const {
@@ -283,8 +235,8 @@ void CYClause::Output(CYOutput &out) const {
     else
         out << "default";
     out << ':' << '\n';
-    if (code_ != NULL)
-        code_->Multiple(out, CYNoFlags);
+    if (statements_ != NULL)
+        statements_->Multiple(out);
     out << next_;
 }
 
@@ -344,7 +296,7 @@ void CYDirectMember::Output(CYOutput &out, CYFlags flags) const {
 
 void CYDoWhile::Output(CYOutput &out, CYFlags flags) const {
     out << "do";
-    code_->Single(out, CYNoFlags);
+    code_->Single(out, CYCenter(flags));
     out << "while" << ' ' << '(' << *test_ << ')';
 }
 
@@ -400,15 +352,8 @@ void CYExpression::Output(CYOutput &out, unsigned precedence, CYFlags flags) con
         Output(out, flags);
 }
 
-void CYField::Output(CYOutput &out) const {
-    // XXX: implement!
-}
-
 void CYFinally::Output(CYOutput &out) const {
-    out << "finally" << ' ' << '{';
-    if (code_ != NULL)
-        code_->Multiple(out);
-    out << '}';
+    out << ' ' << "finally" << ' ' << code_;
 }
 
 void CYFor::Output(CYOutput &out, CYFlags flags) const {
@@ -420,7 +365,7 @@ void CYFor::Output(CYOutput &out, CYFlags flags) const {
     out.Terminate();
     out << increment_;
     out << ')';
-    code_->Single(out, CYNoFlags);
+    code_->Single(out, CYRight(flags));
 }
 
 void CYForEachIn::Output(CYOutput &out, CYFlags flags) const {
@@ -476,7 +421,7 @@ void CYFunction::Output(CYOutput &out, CYFlags flags) const {
     if (name_ != NULL)
         out << ' ' << *name_;
     out << '(' << parameters_ << ')';
-    OutputBody(out, body_);
+    out << ' ' << code_;
     if (protect)
         out << ')';
 }
@@ -505,14 +450,16 @@ void CYIf::Output(CYOutput &out, CYFlags flags) const {
     out << "if" << ' ' << '(' << *test_ << ')';
 
     CYFlags right(protect ? CYNoFlags : CYRight(flags));
+
     CYFlags jacks(CYNoDangle);
     if (false_ == NULL)
         jacks |= right;
+    else
+        jacks |= protect ? CYNoFlags : CYCenter(flags);
 
-    bool single(true_->Single(out, jacks));
+    true_->Single(out, jacks);
 
     if (false_ != NULL) {
-        out << (single ? '\t' : ' ');
         out << "else";
         false_->Single(out, right);
     }
@@ -525,23 +472,17 @@ void CYIfComprehension::Begin_(CYOutput &out) const {
     out << "if" << '(' << *test_ << ')';
 }
 
-void CYIndirect::Output(CYOutput &out, CYFlags flags) const {
-    rhs_->Output(out, 1, CYLeft(flags));
-    out << ".$cyi";
-}
-
 void CYIndirectMember::Output(CYOutput &out, CYFlags flags) const {
     object_->Output(out, Precedence(), CYLeft(flags));
-    out << ".$cyi";
     if (const char *word = property_->Word())
-        out << '.' << word;
+        out << "->" << word;
     else
-        out << '[' << *property_ << ']';
+        out << "->" << '[' << *property_ << ']';
 }
 
 void CYInfix::Output(CYOutput &out, CYFlags flags) const {
     const char *name(Operator());
-    bool protect((flags & CYNoIn) != 0 && strcmp(name, "in"));
+    bool protect((flags & CYNoIn) != 0 && strcmp(name, "in") == 0);
     if (protect)
         out << '(';
     CYFlags left(protect ? CYNoFlags : CYLeft(flags));
@@ -553,34 +494,13 @@ void CYInfix::Output(CYOutput &out, CYFlags flags) const {
         out << ')';
 }
 
-void CYLet::Output(CYOutput &out, CYFlags flags) const {
-    out << "let" << ' ' << '(' << *declarations_ << ')' << ' ' << '{';
-    if (statements_ != NULL)
-        statements_->Multiple(out);
-    out << '}';
+void CYLabel::Output(CYOutput &out, CYFlags flags) const {
+    out << *name_ << ':' << ' ';
+    statement_->Single(out, CYRight(flags));
 }
 
-void CYMessage::Output(CYOutput &out, bool replace) const {
-    if (next_ != NULL)
-        next_->Output(out, replace);
-    out << "$cyn=new Selector(\"";
-    for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
-        if (parameter->tag_ != NULL) {
-            out << *parameter->tag_;
-            if (parameter->name_ != NULL)
-                out << ':';
-        }
-    out << "\");";
-    out << "$cyt=$cyn.type($cy" << (instance_ ? 's' : 'p') << ");";
-    out << "class_" << (replace ? "replace" : "add") << "Method($cy" << (instance_ ? 'c' : 'm') << ",$cyn,";
-    out << "new Functor(function(self,_cmd";
-    for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
-        if (parameter->name_ != NULL)
-            out << ',' << *parameter->name_;
-    out << "){return function(){";
-    if (body_ != NULL)
-        body_->Multiple(out);
-    out << "}.call(self);},$cyt),$cyt);";
+void CYLet::Output(CYOutput &out, CYFlags flags) const {
+    out << "let" << ' ' << '(' << *declarations_ << ')' << ' ' << code_;
 }
 
 void CYNew::Output(CYOutput &out, CYFlags flags) const {
@@ -609,9 +529,11 @@ void CYObject::Output(CYOutput &out, CYFlags flags) const {
     bool protect((flags & CYNoBrace) != 0);
     if (protect)
         out << '(';
-    out << '{';
-    out << property_;
-    out << '}';
+    out << '{' << '\n';
+    ++out.indent_;
+    out << properties_;
+    --out.indent_;
+    out << '\t' << '}';
     if (protect)
         out << ')';
 }
@@ -629,12 +551,20 @@ void CYPrefix::Output(CYOutput &out, CYFlags flags) const {
     rhs_->Output(out, Precedence(), CYRight(flags));
 }
 
+void CYProgram::Output(CYOutput &out) const {
+    if (statements_ != NULL)
+        statements_->Multiple(out);
+}
+
 void CYProperty::Output(CYOutput &out) const {
+    out << '\t';
     name_->PropertyName(out);
     out << ':' << ' ';
     value_->Output(out, CYPA, CYNoFlags);
     if (next_ != NULL)
-        out << ',' << ' ' << *next_;
+        out << ',' << '\n' << *next_;
+    else
+        out << '\n';
 }
 
 void CYRegEx::Output(CYOutput &out, CYFlags flags) const {
@@ -642,41 +572,10 @@ void CYRegEx::Output(CYOutput &out, CYFlags flags) const {
 }
 
 void CYReturn::Output(CYOutput &out, CYFlags flags) const {
-    out << "return" << value_ << ';';
-}
-
-void CYSelector::Output(CYOutput &out, CYFlags flags) const {
-    out << "new Selector(\"";
-    if (name_ != NULL)
-        name_->Output(out);
-    out << "\")";
-}
-
-void CYSelectorPart::Output(CYOutput &out) const {
-    out << name_;
-    if (value_)
-        out << ':';
-    out << next_;
-}
-
-void CYSend::Output(CYOutput &out, CYFlags flags) const {
-    out << "objc_msgSend(";
-    self_->Output(out, CYPA, CYNoFlags);
-    out << ',';
-    std::ostringstream name;
-    for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
-        if (argument->name_ != NULL) {
-            name << *argument->name_;
-            if (argument->value_ != NULL)
-                name << ':';
-        }
-    out.out_ << reinterpret_cast<void *>(sel_registerName(name.str().c_str()));
-    for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
-        if (argument->value_ != NULL) {
-            out << ',';
-            argument->value_->Output(out, CYPA, CYNoFlags);
-        }
-    out << ')';
+    out << "return";
+    if (value_ != NULL)
+        out << ' ' << *value_;
+    out << ';';
 }
 
 void CYStatement::Multiple(CYOutput &out, CYFlags flags) const {
@@ -691,25 +590,14 @@ void CYStatement::Multiple(CYOutput &out, CYFlags flags) const {
     }
 }
 
-bool CYStatement::Single(CYOutput &out, CYFlags flags) const {
-    if (next_ != NULL) {
-        out << ' ' << '{' << '\n';
-        ++out.indent_;
-        Multiple(out);
-        --out.indent_;
-        out << '\t' << '}';
-        return false;
-    } else {
-        for (CYLabel *label(labels_); label != NULL; label = label->next_)
-            out << ' ' << *label->name_ << ':';
-        out << '\n';
-        ++out.indent_;
-        out << '\t';
-        Output(out, flags);
-        out << '\n';
-        --out.indent_;
-        return true;
-    }
+void CYStatement::Single(CYOutput &out, CYFlags flags) const {
+    _assert(next_ == NULL);
+    out << '\n';
+    ++out.indent_;
+    out << '\t';
+    Output(out, flags);
+    out << '\n';
+    --out.indent_;
 }
 
 void CYString::Output(CYOutput &out, CYFlags flags) const {
@@ -765,6 +653,30 @@ void CYString::PropertyName(CYOutput &out) const {
         out << *this;
 }
 
+static const char *Reserved_[] = {
+    "false", "null", "true",
+
+    "break", "case", "catch", "continue", "default",
+    "delete", "do", "else", "finally", "for", "function",
+    "if", "in", "instanceof", "new", "return", "switch",
+    "this", "throw", "try", "typeof", "var", "void",
+    "while", "with",
+
+    "debugger", "const",
+
+    "class", "enum", "export", "extends", "import", "super",
+
+    "abstract", "boolean", "byte", "char", "double", "final",
+    "float", "goto", "int", "long", "native", "short",
+    "synchronized", "throws", "transient", "volatile",
+
+    "let", "yield",
+
+    "each",
+
+    NULL
+};
+
 const char *CYString::Word() const {
     if (size_ == 0 || !WordStartRange_[value_[0]])
         return NULL;
@@ -772,9 +684,7 @@ const char *CYString::Word() const {
         if (!WordEndRange_[value_[i]])
             return NULL;
     const char *value(Value());
-    // XXX: we should probably include the full ECMAScript3+5 list.
-    static const char *reserveds[] = {"class", "const", "enum", "export", "extends", "import", "super", NULL};
-    for (const char **reserved(reserveds); *reserved != NULL; ++reserved)
+    for (const char **reserved(Reserved_); *reserved != NULL; ++reserved)
         if (strcmp(*reserved, value) == 0)
             return NULL;
     return value;
@@ -791,16 +701,14 @@ void CYThis::Output(CYOutput &out, CYFlags flags) const {
 }
 
 void CYThrow::Output(CYOutput &out, CYFlags flags) const {
-    out << "throw" << value_ << ';';
+    out << "throw";
+    if (value_ != NULL)
+        out << ' ' << *value_;
+    out << ';';
 }
 
 void CYTry::Output(CYOutput &out, CYFlags flags) const {
-    out << "try" << ' ' << '{';
-    if (code_ != NULL)
-        code_->Multiple(out);
-    out << '}';
-    out << catch_;
-    out << finally_;
+    out << "try" << ' ' << code_ << catch_ << finally_;
 }
 
 void CYVar::Output(CYOutput &out, CYFlags flags) const {