-/* Cycript - Remove Execution Server and Disassembler
+/* Cycript - Inlining/Optimizing JavaScript Compiler
* Copyright (C) 2009 Jay Freeman (saurik)
*/
*/
/* }}} */
+#include "cycript.hpp"
#include "Parser.hpp"
-#include <iomanip>
#include <sstream>
_finline CYFlags operator ~(CYFlags rhs) {
}
_finline CYFlags CYLeft(CYFlags flags) {
- return flags & ~CYNoDangle;
+ return flags & ~(CYNoDangle | CYNoInteger);
}
_finline CYFlags CYRight(CYFlags flags) {
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_)
} else none:
mode_ = NoMode;
+ right_ = true;
out_ << rhs;
done:
return *this;
else
mode_ = NoMode;
+ right_ = true;
out_ << rhs;
return *this;
}
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_) {
}
const char *CYDeclaration::ForEachIn() const {
- return identifier_->Value();
+ return identifier_->Word();
}
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));
}
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
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));
}
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) {
}
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 {
out << ')';
}
+void CYOptionalFunctionParameter::Output(CYOutput &out) const {
+ out << *name_ << '=';
+ initializer_->Output(out, CYPA, CYNoFlags);
+ if (next_ != NULL)
+ out << ',' << ' ' << *next_;
+}
+
void CYPostfix::Output(CYOutput &out, CYFlags flags) const {
lhs_->Output(out, Precedence(), CYLeft(flags));
out << Operator();
out << ';';
}
+void CYRubyBlock::Output(CYOutput &out, CYFlags flags) const {
+ call_->Output(out, CYLeft(flags));
+ out << ' ';
+ proc_->Output(out, CYRight(flags));
+}
+
+void CYRubyProc::Output(CYOutput &out, CYFlags flags) const {
+ // XXX: this is not outputting the parameters
+ out << code_;
+}
+
void CYStatement::Multiple(CYOutput &out, CYFlags flags) const {
bool first(true);
for (const CYStatement *next(this); next != NULL; next = next->next_) {
}
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();
}
"let", "yield",
- "each",
-
NULL
};
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);
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_;
+}