]> git.saurik.com Git - cycript.git/blobdiff - Output.cpp
Fixed packaging on Linux.
[cycript.git] / Output.cpp
index 1e4d437045d77f3804fff807cfce8b1bd22be462..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) {
@@ -34,8 +31,6 @@ _finline CYFlags CYCenter(CYFlags flags) {
     return CYLeft(CYRight(flags));
 }
 
-#define CYPA 16
-
 void CYOutput::Terminate() {
     out_ << ';';
     mode_ = NoMode;
@@ -111,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_;
@@ -168,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 {
@@ -194,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 {
@@ -288,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_;
 }
 
@@ -405,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 {
@@ -481,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 << ')';
 }
@@ -517,10 +457,9 @@ void CYIf::Output(CYOutput &out, CYFlags flags) const {
     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);
     }
@@ -533,18 +472,12 @@ 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 {
@@ -561,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 {
@@ -619,7 +531,7 @@ void CYObject::Output(CYOutput &out, CYFlags flags) const {
         out << '(';
     out << '{' << '\n';
     ++out.indent_;
-    out << property_;
+    out << properties_;
     --out.indent_;
     out << '\t' << '}';
     if (protect)
@@ -639,6 +551,11 @@ 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);
@@ -661,40 +578,6 @@ void CYReturn::Output(CYOutput &out, CYFlags flags) const {
     out << ';';
 }
 
-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 << ')';
-}
-
 void CYStatement::Multiple(CYOutput &out, CYFlags flags) const {
     bool first(true);
     for (const CYStatement *next(this); next != NULL; next = next->next_) {
@@ -707,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 {
@@ -836,12 +708,7 @@ void CYThrow::Output(CYOutput &out, CYFlags flags) const {
 }
 
 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 {