From c0bc320e4e9af4976a1f0e61e44d58d2e230ca5c Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Wed, 21 Oct 2009 08:10:36 +0000 Subject: [PATCH] Re-added final output to script execution, implemented unary affirmation operator (+), fixed pretty spacing for return/throw and object literals, and fleshed out the complete reserved word list for property banning. --- Console.cpp | 2 +- Cycript.y | 2 +- Output.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++-------- Parser.hpp | 2 ++ 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/Console.cpp b/Console.cpp index 2b61656..3a911cd 100644 --- a/Console.cpp +++ b/Console.cpp @@ -470,7 +470,7 @@ int main(int argc, char *argv[]) { if (compile) std::cout << code; else - Run(socket, code); + Run(socket, code, stdout); } } diff --git a/Cycript.y b/Cycript.y index 14acf8d..151f950 100644 --- a/Cycript.y +++ b/Cycript.y @@ -757,7 +757,7 @@ UnaryExpression_ | "\n++" UnaryExpression { $$ = new(driver.pool_) CYPreIncrement($2); } | "--" UnaryExpression { $$ = new(driver.pool_) CYPreDecrement($2); } | "\n--" UnaryExpression { $$ = new(driver.pool_) CYPreDecrement($2); } - | "+" UnaryExpression { $$ = $2; } + | "+" UnaryExpression { $$ = new(driver.pool_) CYAffirm($2); } | "-" UnaryExpression { $$ = new(driver.pool_) CYNegate($2); } | "~" UnaryExpression { $$ = new(driver.pool_) CYBitwiseNot($2); } | "!" UnaryExpression { $$ = new(driver.pool_) CYLogicalNot($2); } diff --git a/Output.cpp b/Output.cpp index f888524..7f77d65 100644 --- a/Output.cpp +++ b/Output.cpp @@ -67,6 +67,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 +96,7 @@ CYOutput &CYOutput::operator <<(const char *rhs) { if (mode_ == Terminated) out_ << ';'; else if ( + mode_ == NoPlus && *rhs == '+' || mode_ == NoHyphen && *rhs == '-' || mode_ == NoLetter && WordEndRange_[*rhs] ) @@ -612,9 +617,11 @@ void CYObject::Output(CYOutput &out, CYFlags flags) const { bool protect((flags & CYNoBrace) != 0); if (protect) out << '('; - out << '{'; + out << '{' << '\n'; + ++out.indent_; out << property_; - out << '}'; + --out.indent_; + out << '\t' << '}'; if (protect) out << ')'; } @@ -633,11 +640,14 @@ void CYPrefix::Output(CYOutput &out, CYFlags flags) const { } 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 { @@ -645,7 +655,10 @@ void CYRegEx::Output(CYOutput &out, CYFlags flags) const { } void CYReturn::Output(CYOutput &out, CYFlags flags) const { - out << "return" << value_ << ';'; + out << "return"; + if (value_ != NULL) + out << ' ' << *value_; + out << ';'; } void CYSelector::Output(CYOutput &out, CYFlags flags) const { @@ -768,6 +781,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; @@ -775,9 +812,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; @@ -794,7 +829,10 @@ 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 { diff --git a/Parser.hpp b/Parser.hpp index 33de680..a9a8812 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -78,6 +78,7 @@ struct CYOutput { enum { NoMode, NoLetter, + NoPlus, NoHyphen, Terminated } mode_; @@ -1510,6 +1511,7 @@ CYPrefix_(true, "void", Void) CYPrefix_(true, "typeof", TypeOf) CYPrefix_(false, "++", PreIncrement) CYPrefix_(false, "--", PreDecrement) +CYPrefix_(false, "+", Affirm) CYPrefix_(false, "-", Negate) CYPrefix_(false, "~", BitwiseNot) CYPrefix_(false, "!", LogicalNot) -- 2.45.2