+/* Cycript - Inlining/Optimizing JavaScript Compiler
+ * Copyright (C) 2009 Jay Freeman (saurik)
+*/
+
+/* Modified BSD License {{{ */
+/*
+ * Redistribution and use in source and binary
+ * forms, with or without modification, are permitted
+ * provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the
+ * above copyright notice, this list of conditions
+ * and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the
+ * above copyright notice, this list of conditions
+ * and the following disclaimer in the documentation
+ * and/or other materials provided with the
+ * distribution.
+ * 3. The name of the author may not be used to endorse
+ * or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
+ * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+/* }}} */
+
+#include "cycript.hpp"
#include "Parser.hpp"
-#include <iostream>
-#include <iomanip>
-
-#include <objc/runtime.h>
#include <sstream>
_finline CYFlags operator ~(CYFlags rhs) {
}
_finline CYFlags CYLeft(CYFlags flags) {
- return flags & ~CYNoDangle;
+ return flags & ~(CYNoDangle | CYNoInteger);
}
_finline CYFlags CYRight(CYFlags flags) {
return CYLeft(CYRight(flags));
}
-#define CYPA 16
-
void CYOutput::Terminate() {
out_ << ';';
mode_ = NoMode;
for (unsigned i(0); i != indent_; ++i)
out_ << " ";
else goto done;
- else goto work;
-
+ else if (rhs == '\r') {
+ if (right_) {
+ out_ << '\n';
+ right_ = false;
+ } goto done;
+ } else goto work;
+
+ right_ = true;
mode_ = NoMode;
goto done;
work:
- if (mode_ == Terminated && rhs != '}')
+ if (mode_ == Terminated && rhs != '}') {
+ right_ = true;
out_ << ';';
+ }
if (rhs == ';') {
if (pretty_)
mode_ = Terminated;
goto done;
}
+ } else if (rhs == '+') {
+ if (mode_ == NoPlus)
+ out_ << ' ';
+ mode_ = NoPlus;
} else if (rhs == '-') {
if (mode_ == NoHyphen)
out_ << ' ';
} else none:
mode_ = NoMode;
+ right_ = true;
out_ << rhs;
done:
return *this;
if (mode_ == Terminated)
out_ << ';';
else if (
+ mode_ == NoPlus && *rhs == '+' ||
mode_ == NoHyphen && *rhs == '-' ||
mode_ == NoLetter && WordEndRange_[*rhs]
)
else
mode_ = NoMode;
+ right_ = true;
out_ << 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_;
}
void CYArrayComprehension::Output(CYOutput &out, CYFlags flags) const {
- // XXX: I don't necc. need the ()s
- out << "(function($cyv";
- for (CYComprehension *comprehension(comprehensions_); comprehension != NULL; comprehension = comprehension->next_)
- if (const char *name = comprehension->Name())
- out << ',' << name;
- out << "){";
- out << "$cyv=[];";
- comprehensions_->Output(out);
- out << "$cyv.push(";
- expression_->Output(out, CYPA, CYNoFlags);
- out << ");";
- for (CYComprehension *comprehension(comprehensions_); comprehension != NULL; comprehension = comprehension->next_)
- comprehension->End_(out);
- out << "return $cyv;";
- out << "}())";
+ out << '[' << *expression_ << ' ' << *comprehensions_ << ']';
}
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 {
out << ')';
}
-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 << ';';
-}
+namespace cy {
+namespace Syntax {
-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 Catch::Output(CYOutput &out) const {
+ out << ' ' << "catch" << ' ' << '(' << *name_ << ')' << ' ' << code_;
}
-void CYClassExpression::Output(CYOutput &out, CYFlags flags) const {
- CYClass::Output(out, flags);
-}
+} }
-void CYClassStatement::Output(CYOutput &out, CYFlags flags) const {
- CYClass::Output(out, flags);
+void CYComment::Output(CYOutput &out, CYFlags flags) const {
+ out << '\r';
+ out.out_ << value_;
+ out.right_ = true;
+ out << '\r';
}
void CYCompound::Output(CYOutput &out, CYFlags flags) const {
expression->Output(out, flags);
}
-void CYComprehension::Output(CYOutput &out) const {
- Begin_(out);
- out << next_;
-}
-
void CYCondition::Output(CYOutput &out, CYFlags flags) const {
test_->Output(out, Precedence() - 1, CYLeft(flags));
out << ' ' << '?' << ' ';
else
out << "default";
out << ':' << '\n';
- if (code_ != NULL)
- code_->Multiple(out, CYNoFlags);
+ if (statements_ != NULL)
+ statements_->Multiple(out);
out << next_;
}
const char *CYDeclaration::ForEachIn() const {
- return identifier_->Value();
+ return identifier_->Word();
}
void CYDeclaration::ForIn(CYOutput &out, CYFlags flags) const {
Output(out, CYRight(flags));
}
-void CYDeclaration::ForEachIn(CYOutput &out) const {
- out << *identifier_;
-}
-
void CYDeclaration::Output(CYOutput &out, CYFlags flags) const {
out << *identifier_;
+ //out.out_ << ':' << identifier_->usage_ << '#' << identifier_->offset_;
if (initialiser_ != NULL) {
out << ' ' << '=' << ' ';
initialiser_->Output(out, CYPA, CYRight(flags));
}
void CYDirectMember::Output(CYOutput &out, CYFlags flags) const {
- object_->Output(out, Precedence(), CYLeft(flags));
+ object_->Output(out, Precedence(), CYLeft(flags) | CYNoInteger);
if (const char *word = property_->Word())
out << '.' << word;
else
Output(out, CYNoIn);
}
-void CYExpression::ForEachIn(CYOutput &out) const {
- Output(out, CYPA, CYNoRightHand);
-}
-
void CYExpression::ForIn(CYOutput &out, CYFlags flags) const {
Output(out, flags | CYNoRightHand);
}
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 {
}
void CYForEachIn::Output(CYOutput &out, CYFlags flags) const {
- out << "with({$cys:0,$cyt:0}){";
-
- out << "$cys=";
- set_->Output(out, CYPA, CYNoFlags);
- out << ';';
-
- out << "for($cyt in $cys){";
-
- initialiser_->ForEachIn(out);
- out << "=$cys[$cyt];";
-
- code_->Multiple(out);
-
- out << '}';
-
- out << '}';
-}
-
-void CYForEachInComprehension::Begin_(CYOutput &out) const {
- out << "(function($cys){";
- out << "$cys=";
- set_->Output(out, CYPA, CYNoFlags);
- out << ';';
-
- out << "for(" << *name_ << " in $cys){";
- out << *name_ << "=$cys[" << *name_ << "];";
+ out << "for" << ' ' << "each" << ' ' << '(';
+ initialiser_->ForIn(out, CYNoIn);
+ out << "in" << *set_ << ')';
+ code_->Single(out, CYRight(flags));
}
-void CYForEachInComprehension::End_(CYOutput &out) const {
- out << "}}());";
+void CYForEachInComprehension::Output(CYOutput &out) const {
+ out << "for" << ' ' << "each" << ' ' << '(' << *name_ << ' ' << "in" << ' ' << *set_ << ')' << next_;
}
void CYForIn::Output(CYOutput &out, CYFlags flags) const {
out << "for" << ' ' << '(';
- initialiser_->ForIn(out, CYNoIn);
+ if (initialiser_ != NULL)
+ initialiser_->ForIn(out, CYNoIn);
out << "in" << *set_ << ')';
code_->Single(out, CYRight(flags));
}
-void CYForInComprehension::Begin_(CYOutput &out) const {
- out << "for" << ' ' << '(' << *name_ << "in" << *set_ << ')';
+void CYForInComprehension::Output(CYOutput &out) const {
+ out << "for" << ' ' << '(' << *name_ << ' ' << "in" << ' ' << *set_ << ')';
}
void CYFunction::Output(CYOutput &out, CYFlags flags) const {
if (name_ != NULL)
out << ' ' << *name_;
out << '(' << parameters_ << ')';
- OutputBody(out, body_);
+ out << ' ' << code_;
if (protect)
out << ')';
}
out << ',' << ' ' << *next_;
}
+const char *CYIdentifier::Word() const {
+ return replace_ == NULL || replace_ == this ? CYWord::Word() : replace_->Word();
+}
+
void CYIf::Output(CYOutput &out, CYFlags flags) const {
bool protect(false);
if (false_ == NULL && (flags & CYNoDangle) != 0) {
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);
}
out << '}';
}
-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 CYIfComprehension::Output(CYOutput &out) const {
+ out << "if" << ' ' << '(' << *test_ << ')' << next_;
}
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));
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 {
}
void CYNumber::Output(CYOutput &out, CYFlags flags) const {
- char value[32];
- sprintf(value, "%.17g", Value());
- out << value;
+ std::ostringstream str;
+ CYNumerify(str, Value());
+ std::string value(str.str());
+ out << value.c_str();
+ // XXX: this should probably also handle hex conversions and exponents
+ if ((flags & CYNoInteger) != 0 && value.find('.') == std::string::npos)
+ out << '.';
}
void CYNumber::PropertyName(CYOutput &out) 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 << ')';
}
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 {
}
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 {
}
}
-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 {
- unsigned quot(0), apos(0);
- for (const char *value(value_), *end(value_ + size_); value != end; ++value)
- if (*value == '"')
- ++quot;
- else if (*value == '\'')
- ++apos;
-
- bool single(quot > apos);
-
std::ostringstream str;
-
- str << (single ? '\'' : '"');
- for (const char *value(value_), *end(value_ + size_); value != end; ++value)
- switch (*value) {
- case '\\': str << "\\\\"; break;
- case '\b': str << "\\b"; break;
- case '\f': str << "\\f"; break;
- case '\n': str << "\\n"; break;
- case '\r': str << "\\r"; break;
- case '\t': str << "\\t"; break;
- case '\v': str << "\\v"; break;
-
- case '"':
- if (!single)
- str << "\\\"";
- else goto simple;
- break;
-
- case '\'':
- if (single)
- str << "\\'";
- else goto simple;
- break;
-
- default:
- if (*value < 0x20 || *value >= 0x7f)
- str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
- else simple:
- str << *value;
- }
- str << (single ? '\'' : '"');
-
+ CYStringify(str, value_, size_);
out << str.str().c_str();
}
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",
+
+ NULL
+};
+
const char *CYString::Word() const {
if (size_ == 0 || !WordStartRange_[value_[0]])
return NULL;
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;
CYWord::Output(out);
}
-void CYThrow::Output(CYOutput &out, CYFlags flags) const {
- out << "throw" << value_ << ';';
+namespace cy {
+namespace Syntax {
+
+void Throw::Output(CYOutput &out, CYFlags flags) const {
+ 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_;
+void Try::Output(CYOutput &out, CYFlags flags) const {
+ out << "try" << ' ' << code_ << catch_ << finally_;
}
+} }
+
void CYVar::Output(CYOutput &out, CYFlags flags) const {
out << "var";
declarations_->Output(out, flags);
void CYWord::ClassName(CYOutput &out, bool object) const {
if (object)
out << "objc_getClass(";
- out << '"' << Value() << '"';
+ out << '"' << Word() << '"';
if (object)
out << ')';
}
void CYWord::Output(CYOutput &out) const {
- out << Value();
+ out << Word();
+ if (out.options_.verbose_)
+ out.out_ << '@' << this;
}
void CYWord::PropertyName(CYOutput &out) const {
Output(out);
}
+
+const char *CYWord::Word() const {
+ return word_;
+}