/* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2010 Jay Freeman (saurik)
+ * Copyright (C) 2009-2013 Jay Freeman (saurik)
*/
-/* GNU Lesser General Public License, Version 3 {{{ */
+/* GNU General Public License, Version 3 {{{ */
/*
- * Cycript is free software: you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your
- * option) any later version.
+ * Cycript is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation, either version 3 of the License,
+ * or (at your option) any later version.
*
- * Cycript is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
- * License for more details.
+ * Cycript is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
*
- * You should have received a copy of the GNU Lesser General Public License
+ * You should have received a copy of the GNU General Public License
* along with Cycript. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include <iostream>
+#include <stack>
#include <string>
#include <vector>
#include <map>
#include "Pooling.hpp"
#include "Options.hpp"
-class CYContext;
+struct CYContext;
struct CYThing {
virtual ~CYThing() {
typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
-// XXX: strategy pattern, maybe subclass
-enum CYScopeType {
- CYScopeCatch,
- CYScopeFunction,
- CYScopeProgram,
-};
-
struct CYScope {
- CYScopeType type_;
+ bool transparent_;
CYContext &context_;
CYStatement *&statements_;
CYIdentifierAddressFlagsMap internal_;
CYIdentifierValueSet identifiers_;
- CYScope(CYScopeType type, CYContext &context, CYStatement *&statements);
+ CYScope(bool transparent, CYContext &context, CYStatement *&statements);
virtual ~CYScope();
void Close();
};
struct CYNonLocal;
+struct CYThisScope;
struct CYContext {
CYOptions &options_;
CYScope *scope_;
+ CYThisScope *this_;
+
CYIdentifierUsageVector rename_;
CYNonLocal *nonlocal_;
CYContext(CYOptions &options) :
options_(options),
scope_(NULL),
+ this_(NULL),
nonlocal_(NULL),
nextlocal_(NULL),
unique_(0)
}
};
+struct CYThisScope :
+ CYNext<CYThisScope>
+{
+ CYIdentifier *identifier_;
+
+ CYThisScope() :
+ identifier_(NULL)
+ {
+ }
+
+ CYIdentifier *Identifier(CYContext &context) {
+ if (next_ != NULL)
+ return next_->Identifier(context);
+ if (identifier_ == NULL)
+ identifier_ = context.Unique();
+ return identifier_;
+ }
+};
+
struct CYBlock :
CYStatement,
CYThing
}
void AddPrev(CYStatement *statement) {
- CYSetLast(statement, statements_);
+ CYSetLast(statement) = statements_;
statements_ = statement;
}
CYNewLine
};
+class CYStream :
+ public std::istream
+{
+ private:
+ class CYBuffer :
+ public std::streambuf
+ {
+ public:
+ CYBuffer(const char *start, const char *end) {
+ setg(const_cast<char *>(start), const_cast<char *>(start), const_cast<char *>(end));
+ }
+ } buffer_;
+
+ public:
+ CYStream(const char *start, const char *end) :
+ std::istream(&buffer_),
+ buffer_(start, end)
+ {
+ }
+};
+
class CYDriver {
public:
- CYState state_;
void *scanner_;
- const char *data_;
- size_t size_;
- FILE *file_;
+ CYState state_;
+ std::stack<bool> in_;
+
+ struct {
+ bool AtImplementation;
+ bool Function;
+ bool OpenBrace;
+ } no_;
+
+ std::istream &data_;
bool strict_;
+ bool commented_;
enum Condition {
RegExpCondition,
void ScannerDestroy();
public:
- CYDriver(const std::string &filename = "");
+ CYDriver(std::istream &data, const std::string &filename = "");
~CYDriver();
Condition GetCondition();
virtual CYExpression *Replace(CYContext &context) = 0;
virtual CYAssignment *Assignment(CYContext &context) = 0;
+
+ virtual void Output(CYOutput &out, CYFlags flags) const = 0;
};
struct CYNumber;
}
void AddPrev(CYExpression *expression) {
- CYSetLast(expression, expressions_);
+ CYSetLast(expression) = expressions_;
expressions_ = expression;
}
virtual CYExpression *Replace(CYContext &context);
void Output(CYOutput &out, CYFlags flags) const;
+
+ virtual CYExpression *Primitive(CYContext &context);
};
+struct CYDeclaration;
+
struct CYFunctionParameter :
CYNext<CYFunctionParameter>,
CYThing
{
- CYIdentifier *name_;
+ CYForInInitialiser *initialiser_;
- CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
+ CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
CYNext<CYFunctionParameter>(next),
- name_(name)
- {
- }
-
- virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
- virtual void Output(CYOutput &out) const;
-};
-
-struct CYOptionalFunctionParameter :
- CYFunctionParameter
-{
- CYExpression *initializer_;
-
- CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) :
- CYFunctionParameter(name, next),
- initializer_(initializer)
+ initialiser_(initialiser)
{
}
- virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
- virtual void Output(CYOutput &out) const;
+ void Replace(CYContext &context, CYBlock &code);
+ void Output(CYOutput &out) const;
};
struct CYComprehension :
CYNext<CYComprehension>,
CYThing
{
+ CYComprehension(CYComprehension *next = NULL) :
+ CYNext<CYComprehension>(next)
+ {
+ }
+
virtual const char *Name() const = 0;
virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
CYIdentifier *name_;
CYExpression *set_;
- CYForInComprehension(CYIdentifier *name, CYExpression *set) :
+ CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
+ CYComprehension(next),
name_(name),
set_(set)
{
virtual void Output(CYOutput &out) const;
};
-struct CYForEachInComprehension :
+struct CYForOfComprehension :
CYComprehension
{
CYIdentifier *name_;
CYExpression *set_;
- CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
+ CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
+ CYComprehension(next),
name_(name),
set_(set)
{
{
}
- void Replace(CYContext &context);
+ CYArgument *Replace(CYContext &context);
void Output(CYOutput &out) const;
};
virtual void Output(CYOutput &out, CYFlags flags) const;
};
-struct CYLet :
+struct CYLetStatement :
CYStatement
{
CYDeclarations *declarations_;
CYStatement *code_;
- CYLet(CYDeclarations *declarations, CYStatement *code) :
+ CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
declarations_(declarations),
code_(code)
{
virtual void Output(CYOutput &out, CYFlags flags) const;
};
-struct CYForEachIn :
+struct CYForOf :
CYStatement
{
CYForInInitialiser *initialiser_;
CYExpression *set_;
CYStatement *code_;
- CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
+ CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
initialiser_(initialiser),
set_(set),
code_(code)
void SetLeft(CYExpression *object) {
object_ = object;
}
-
- void Replace_(CYContext &context);
};
struct CYDirectMember :
CYIdentifier *name_;
CYFunctionParameter *parameters_;
CYBlock code_;
+
CYNonLocal *nonlocal_;
+ CYThisScope this_;
CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
name_(name),
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+// XXX: this should derive from CYAnonymousFunction
+struct CYFatArrow :
+ CYFunction,
+ CYExpression
+{
+ CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
+ CYFunction(NULL, parameters, statements)
+ {
+ }
+
+ CYPrecedence(0)
+ CYRightHand(false)
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
// XXX: this should derive from CYAnonymousFunctionExpression
struct CYRubyProc :
CYFunctionExpression
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYDebugger :
+ CYStatement
+{
+ CYDebugger()
+ {
+ }
+
+ virtual CYStatement *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYCondition :
CYExpression
{