X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/ec18682d83264996954ab6473ea3d342ac8affc5..77dd5db92b788d747c11ee9c78d79df5b88e7a1d:/ObjectiveC/Output.cpp diff --git a/ObjectiveC/Output.cpp b/ObjectiveC/Output.cpp index 9d0caa0..6927938 100644 --- a/ObjectiveC/Output.cpp +++ b/ObjectiveC/Output.cpp @@ -1,5 +1,5 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2015 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -19,51 +19,39 @@ **/ /* }}} */ +#include + #include "Replace.hpp" -#include "ObjectiveC/Syntax.hpp" -#include +#include "ObjectiveC/Syntax.hpp" 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 << ';'; -} + out << "@implementation" << ' ' << *name_ << ' ' << '(' << ')' << '\n'; + ++out.indent_; -void CYClassStatement::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,"; - name_->ClassName(out, false); - out << ",0);"; - out << "$cym=object_getClass($cyc);"; - if (fields_ != NULL) - fields_->Output(out); - if (messages_ != NULL) - messages_->Output(out, false); - if (protocols_ != NULL) { - out << '<'; - out << *protocols_; - out << '>'; + CYForEach (message, messages_) { + message->Output(out); + out << '\n'; } - out << "objc_registerClassPair($cyc);"; - out << "return $cyc;"; - out << "}("; - if (super_ != NULL) - super_->Output(out, CYAssign::Precedence_, CYNoFlags); - else - out << "null"; - out << "))"; + + --out.indent_; + out << "@end"; } -void CYClassField::Output(CYOutput &out) const { +void CYImplementation::Output(CYOutput &out, CYFlags flags) const { + out << "@implementation" << ' ' << *name_ << '\n'; + ++out.indent_; + + // XXX: implement + + --out.indent_; + out << "@end"; +} + +void CYImplementationField::Output(CYOutput &out) const { + type_->Output(out, name_); + out.Terminate(); + out << '\n'; } void CYInstanceLiteral::Output(CYOutput &out, CYFlags flags) const { @@ -71,14 +59,19 @@ void CYInstanceLiteral::Output(CYOutput &out, CYFlags flags) const { number_->Output(out, CYRight(flags)); } -void CYMessage::Output(CYOutput &out, bool replace) const { +void CYMessage::Output(CYOutput &out) const { out << (instance_ ? '-' : '+'); CYForEach (parameter, parameters_) - if (parameter->tag_ != NULL) { - out << ' ' << *parameter->tag_; - if (parameter->type_ != NULL) - out << ':' << *parameter->type_->identifier_; + if (parameter->name_ != NULL) { + out << ' ' << *parameter->name_; + // XXX: this is off somehow + if (parameter->identifier_ != NULL) { + out << ':'; + if (parameter->type_ != NULL) + out << '(' << *parameter->type_ << ')'; + out << *parameter->identifier_; + } } out << code_; @@ -89,12 +82,70 @@ void CYBox::Output(CYOutput &out, CYFlags flags) const { value_->Output(out, Precedence(), CYRight(flags)); } +void CYObjCArray::Output(CYOutput &out, CYFlags flags) const { + out << '@' << '[' << elements_ << ']'; +} + +void CYObjCDictionary::Output(CYOutput &out, CYFlags flags) const { + unsigned count(0); + CYForEach (pair, pairs_) + ++count; + bool large(count > 8); + + out << '@' << '{'; + if (large) { + out << '\n'; + ++out.indent_; + } + + bool comma(false); + CYForEach (pair, pairs_) { + if (!comma) + comma = true; + else { + out << ','; + if (large) + out << '\n'; + else + out << ' '; + } + + if (large) + out << '\t'; + + pair->key_->Output(out, CYAssign::Precedence_, CYNoFlags); + out << ':' << ' '; + pair->value_->Output(out, CYAssign::Precedence_, CYNoFlags); + } + + if (large && out.pretty_) + out << ','; + + if (large) { + out << '\n'; + --out.indent_; + } + + out << '\t' << '}'; +} + void CYObjCBlock::Output(CYOutput &out, CYFlags flags) const { - // XXX: this is seriously wrong - out << "^("; - out << ")"; - out << "{"; - out << "}"; + out << '^' << ' ' << *typed_ << ' ' << '('; + + bool comma(false); + CYForEach (parameter, parameters_) { + if (comma) + out << ',' << ' '; + else + comma = true; + parameter->type_->Output(out, parameter->name_); + } + + out << ')' << ' ' << '{' << '\n'; + ++out.indent_; + out << code_; + --out.indent_; + out << '\t' << '}'; } void CYProtocol::Output(CYOutput &out) const { @@ -104,7 +155,7 @@ void CYProtocol::Output(CYOutput &out) const { } void CYSelector::Output(CYOutput &out, CYFlags flags) const { - out << "@selector" << '(' << name_ << ')'; + out << "@selector" << '(' << parts_ << ')'; } void CYSelectorPart::Output(CYOutput &out) const {