X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/520c130fcc066582173d69b4c96797f87ba24a9b..17fe7546f6ef4df0bc8b0394e39d929c93d5c1f4:/Output.cpp diff --git a/Output.cpp b/Output.cpp index 82156a0..a3765da 100644 --- a/Output.cpp +++ b/Output.cpp @@ -1,4 +1,4 @@ -/* Cycript - Remove Execution Server and Disassembler +/* Cycript - Inlining/Optimizing JavaScript Compiler * Copyright (C) 2009 Jay Freeman (saurik) */ @@ -59,7 +59,7 @@ _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) { } _finline CYFlags CYLeft(CYFlags flags) { - return flags & ~CYNoDangle; + return flags & ~(CYNoDangle | CYNoInteger); } _finline CYFlags CYRight(CYFlags flags) { @@ -85,14 +85,22 @@ CYOutput &CYOutput::operator <<(char rhs) { 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_) @@ -116,6 +124,7 @@ CYOutput &CYOutput::operator <<(char rhs) { } else none: mode_ = NoMode; + right_ = true; out_ << rhs; done: return *this; @@ -141,6 +150,7 @@ CYOutput &CYOutput::operator <<(const char *rhs) { else mode_ = NoMode; + right_ = true; out_ << rhs; return *this; } @@ -213,10 +223,22 @@ void CYCall::Output(CYOutput &out, CYFlags flags) const { out << ')'; } -void CYCatch::Output(CYOutput &out) const { +namespace cy { +namespace Syntax { + +void Catch::Output(CYOutput &out) const { out << ' ' << "catch" << ' ' << '(' << *name_ << ')' << ' ' << code_; } +} } + +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 { if (CYExpression *expression = expressions_) if (CYExpression *next = expression->next_) { @@ -261,7 +283,7 @@ void CYClause::Output(CYOutput &out) const { } const char *CYDeclaration::ForEachIn() const { - return identifier_->Value(); + return identifier_->Word(); } void CYDeclaration::ForIn(CYOutput &out, CYFlags flags) const { @@ -271,6 +293,7 @@ void CYDeclaration::ForIn(CYOutput &out, CYFlags flags) const { 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)); @@ -393,7 +416,8 @@ void CYForEachInComprehension::Output(CYOutput &out) const { 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)); } @@ -430,6 +454,10 @@ void CYFunctionParameter::Output(CYOutput &out) const { 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) { @@ -506,10 +534,13 @@ void CYNull::Output(CYOutput &out, CYFlags flags) const { } void CYNumber::Output(CYOutput &out, CYFlags flags) const { - char value[32]; - // XXX: I want this to print 1e3 rather than 1000 - 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 { @@ -623,8 +654,6 @@ static const char *Reserved_[] = { "let", "yield", - "each", - NULL }; @@ -651,17 +680,22 @@ void CYThis::Output(CYOutput &out, CYFlags flags) const { CYWord::Output(out); } -void CYThrow::Output(CYOutput &out, CYFlags flags) const { +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 { +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); @@ -685,15 +719,21 @@ void CYWith::Output(CYOutput &out, CYFlags flags) const { 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_; +}