X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/62f398e4aed5b34ee2bd8edff9fb3afa80d75e25..b0385401f63fe31f3cc1459ed159747a445852d6:/Parser.hpp
diff --git a/Parser.hpp b/Parser.hpp
index 59e657f..8bd7cdc 100644
--- a/Parser.hpp
+++ b/Parser.hpp
@@ -1,21 +1,21 @@
/* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2013 Jay Freeman (saurik)
+ * Copyright (C) 2009-2015 Jay Freeman (saurik)
*/
-/* GNU General Public License, Version 3 {{{ */
+/* GNU Affero General Public License, Version 3 {{{ */
/*
- * 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
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program 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 General Public License
- * along with Cycript. If not, see .
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
**/
/* }}} */
@@ -33,6 +33,7 @@
#include
#include "List.hpp"
+#include "Location.hpp"
#include "Pooling.hpp"
#include "Options.hpp"
@@ -145,13 +146,15 @@ _finline CYFlags CYCenter(CYFlags flags) {
}
struct CYStatement :
- CYNext
+ CYNext,
+ CYThing
{
virtual ~CYStatement() {
}
void Single(CYOutput &out, CYFlags flags) const;
void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
+ virtual void Output(CYOutput &out) const;
virtual CYStatement *Replace(CYContext &context) = 0;
@@ -312,33 +315,26 @@ typedef std::vector CYIdentifierUsageVector;
struct CYScope {
bool transparent_;
-
- CYContext &context_;
- CYStatement *&statements_;
-
CYScope *parent_;
CYIdentifierAddressFlagsMap internal_;
CYIdentifierValueSet identifiers_;
- CYScope(bool transparent, CYContext &context, CYStatement *&statements);
- virtual ~CYScope();
-
- void Close();
+ CYScope(bool transparent, CYContext &context);
void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
void Merge(CYContext &context, CYIdentifier *identifier);
- void Scope(CYContext &context, CYStatement *&statements);
+ void Close(CYContext &context, CYStatement *&statements);
};
struct CYProgram :
CYThing
{
- CYStatement *statements_;
+ CYStatement *code_;
- CYProgram(CYStatement *statements) :
- statements_(statements)
+ CYProgram(CYStatement *code) :
+ code_(code)
{
}
@@ -374,14 +370,18 @@ struct CYContext {
virtual ~CYContext() {
}
- template
- void ReplaceAll(Type_ *&values) {
- Type_ **last(&values);
- CYForEach (next, values) {
- Replace(*last = next);
- if (*last != NULL)
- last = &(*last)->next_;
- }
+ void ReplaceAll(CYStatement *&statement) {
+ if (statement == NULL)
+ return;
+ CYStatement *next(statement->next_);
+
+ Replace(statement);
+ ReplaceAll(next);
+
+ if (statement == NULL)
+ statement = next;
+ else
+ statement->SetNext(next);
}
template
@@ -435,52 +435,20 @@ struct CYThisScope :
};
struct CYBlock :
- CYStatement,
- CYThing
+ CYStatement
{
- CYStatement *statements_;
+ CYStatement *code_;
- CYBlock(CYStatement *statements) :
- statements_(statements)
+ CYBlock(CYStatement *code) :
+ code_(code)
{
}
- operator CYStatement *() const {
- return statements_;
- }
-
- void AddPrev(CYStatement *statement) {
- CYSetLast(statement) = statements_;
- statements_ = statement;
- }
-
virtual CYStatement *Replace(CYContext &context);
- virtual void Output(CYOutput &out) const;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
-class CYStream :
- public std::istream
-{
- private:
- class CYBuffer :
- public std::streambuf
- {
- public:
- CYBuffer(const char *start, const char *end) {
- setg(const_cast(start), const_cast(start), const_cast(end));
- }
- } buffer_;
-
- public:
- CYStream(const char *start, const char *end) :
- std::istream(&buffer_),
- buffer_(start, end)
- {
- }
-};
-
struct CYForInitialiser {
virtual ~CYForInitialiser() {
}
@@ -502,11 +470,12 @@ struct CYForInInitialiser {
virtual void Output(CYOutput &out, CYFlags flags) const = 0;
};
+struct CYFunctionParameter;
+
struct CYNumber;
struct CYString;
struct CYExpression :
- CYNext,
CYForInitialiser,
CYForInInitialiser,
CYClassName,
@@ -534,9 +503,11 @@ struct CYExpression :
virtual CYAssignment *Assignment(CYContext &context);
virtual CYExpression *Primitive(CYContext &context) {
- return this;
+ return NULL;
}
+ virtual CYFunctionParameter *Parameter() const;
+
virtual CYNumber *Number(CYContext &context) {
return NULL;
}
@@ -569,16 +540,15 @@ struct CYExpression :
struct CYCompound :
CYExpression
{
- CYExpression *expressions_;
+ CYExpression *expression_;
+ CYExpression *next_;
- CYCompound(CYExpression *expressions = NULL) :
- expressions_(expressions)
+ CYCompound(CYExpression *expression, CYExpression *next) :
+ expression_(expression),
+ next_(next)
{
- }
-
- void AddPrev(CYExpression *expression) {
- CYSetLast(expression) = expressions_;
- expressions_ = expression;
+ _assert(expression_ != NULL);
+ _assert(next != NULL);
}
CYPrecedence(17)
@@ -586,7 +556,23 @@ struct CYCompound :
virtual CYExpression *Replace(CYContext &context);
void Output(CYOutput &out, CYFlags flags) const;
- virtual CYExpression *Primitive(CYContext &context);
+ virtual CYFunctionParameter *Parameter() const;
+};
+
+struct CYParenthetical :
+ CYExpression
+{
+ CYExpression *expression_;
+
+ CYParenthetical(CYExpression *expression) :
+ expression_(expression)
+ {
+ }
+
+ CYPrecedence(0)
+
+ virtual CYExpression *Replace(CYContext &context);
+ void Output(CYOutput &out, CYFlags flags) const;
};
struct CYDeclaration;
@@ -603,7 +589,7 @@ struct CYFunctionParameter :
{
}
- void Replace(CYContext &context, CYBlock &code);
+ void Replace(CYContext &context, CYStatement *&statements);
void Output(CYOutput &out) const;
};
@@ -716,6 +702,10 @@ struct CYLiteral :
{
CYPrecedence(0)
CYRightHand(false)
+
+ virtual CYExpression *Primitive(CYContext &context) {
+ return this;
+ }
};
struct CYTrivial :
@@ -928,6 +918,8 @@ struct CYVariable :
virtual CYExpression *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
+
+ virtual CYFunctionParameter *Parameter() const;
};
struct CYPrefix :
@@ -1053,11 +1045,11 @@ struct CYClause :
CYNext
{
CYExpression *case_;
- CYStatement *statements_;
+ CYStatement *code_;
- CYClause(CYExpression *_case, CYStatement *statements) :
+ CYClause(CYExpression *_case, CYStatement *code) :
case_(_case),
- statements_(statements)
+ code_(code)
{
}
@@ -1150,7 +1142,7 @@ struct CYDeclarations :
void Replace(CYContext &context);
- CYCompound *Compound(CYContext &context);
+ CYExpression *Expression(CYContext &context);
CYProperty *Property(CYContext &context);
CYArgument *Argument(CYContext &context);
CYFunctionParameter *Parameter(CYContext &context);
@@ -1169,7 +1161,7 @@ struct CYForDeclarations :
{
}
- virtual CYCompound *Replace(CYContext &context);
+ virtual CYExpression *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
@@ -1445,15 +1437,15 @@ struct CYWhile :
struct CYFunction {
CYIdentifier *name_;
CYFunctionParameter *parameters_;
- CYBlock code_;
+ CYStatement *code_;
CYNonLocal *nonlocal_;
CYThisScope this_;
- CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
+ CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
name_(name),
parameters_(parameters),
- code_(statements),
+ code_(code),
nonlocal_(NULL)
{
}
@@ -1471,8 +1463,8 @@ struct CYFunctionExpression :
CYFunction,
CYExpression
{
- CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
- CYFunction(name, parameters, statements)
+ CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
+ CYFunction(name, parameters, code)
{
}
@@ -1488,8 +1480,8 @@ struct CYFatArrow :
CYFunction,
CYExpression
{
- CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
- CYFunction(NULL, parameters, statements)
+ CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
+ CYFunction(NULL, parameters, code)
{
}
@@ -1504,8 +1496,8 @@ struct CYFatArrow :
struct CYRubyProc :
CYFunctionExpression
{
- CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
- CYFunctionExpression(NULL, parameters, statements)
+ CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
+ CYFunctionExpression(NULL, parameters, code)
{
}
@@ -1518,8 +1510,8 @@ struct CYFunctionStatement :
CYFunction,
CYStatement
{
- CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
- CYFunction(name, parameters, statements)
+ CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
+ CYFunction(name, parameters, code)
{
}
@@ -1535,7 +1527,7 @@ struct CYExpress :
CYExpress(CYExpression *expression) :
expression_(expression)
{
- if (expression == NULL)
+ if (expression_ == NULL)
throw;
}
@@ -1595,10 +1587,10 @@ struct CYEmpty :
struct CYFinally :
CYThing
{
- CYBlock code_;
+ CYStatement *code_;
- CYFinally(CYStatement *statements) :
- code_(statements)
+ CYFinally(CYStatement *code) :
+ code_(code)
{
}
@@ -1606,6 +1598,109 @@ struct CYFinally :
virtual void Output(CYOutput &out) const;
};
+struct CYTypeSpecifier :
+ CYThing
+{
+ virtual CYExpression *Replace(CYContext &context) = 0;
+};
+
+struct CYTypeError :
+ CYTypeSpecifier
+{
+ CYTypeError() {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeVoid :
+ CYTypeSpecifier
+{
+ CYTypeVoid() {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeVariable :
+ CYTypeSpecifier
+{
+ CYIdentifier *name_;
+
+ CYTypeVariable(CYIdentifier *name) :
+ name_(name)
+ {
+ }
+
+ CYTypeVariable(const char *name) :
+ name_(new($pool) CYIdentifier(name))
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeUnsigned :
+ CYTypeSpecifier
+{
+ CYTypeSpecifier *specifier_;
+
+ CYTypeUnsigned(CYTypeSpecifier *specifier) :
+ specifier_(specifier)
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeSigned :
+ CYTypeSpecifier
+{
+ CYTypeSpecifier *specifier_;
+
+ CYTypeSigned(CYTypeSpecifier *specifier) :
+ specifier_(specifier)
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeLong :
+ CYTypeSpecifier
+{
+ CYTypeSpecifier *specifier_;
+
+ CYTypeLong(CYTypeSpecifier *specifier) :
+ specifier_(specifier)
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeShort :
+ CYTypeSpecifier
+{
+ CYTypeSpecifier *specifier_;
+
+ CYTypeShort(CYTypeSpecifier *specifier) :
+ specifier_(specifier)
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeFunctionWith;
+
struct CYTypeModifier :
CYNext
{
@@ -1621,6 +1716,8 @@ struct CYTypeModifier :
virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
+
+ virtual CYTypeFunctionWith *Function() { return NULL; }
};
struct CYTypeArrayOf :
@@ -1686,20 +1783,22 @@ struct CYTypedIdentifier :
CYNext,
CYThing
{
+ CYLocation location_;
CYIdentifier *identifier_;
- CYExpression *type_;
+ CYTypeSpecifier *specifier_;
CYTypeModifier *modifier_;
- CYTypedIdentifier(CYIdentifier *identifier = NULL) :
+ CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
+ location_(location),
identifier_(identifier),
- type_(NULL),
+ specifier_(NULL),
modifier_(NULL)
{
}
- CYTypedIdentifier(CYExpression *type, CYTypeModifier *modifier = NULL) :
+ CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
identifier_(NULL),
- type_(type),
+ specifier_(specifier),
modifier_(modifier)
{
}
@@ -1711,6 +1810,8 @@ struct CYTypedIdentifier :
virtual CYExpression *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
+
+ CYTypeFunctionWith *Function();
};
struct CYEncodedType :
@@ -1753,12 +1854,12 @@ struct CYLambda :
{
CYTypedIdentifier *typed_;
CYTypedParameter *parameters_;
- CYStatement *statements_;
+ CYStatement *code_;
- CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
+ CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
typed_(typed),
parameters_(parameters),
- statements_(statements)
+ code_(code)
{
}
@@ -1768,6 +1869,52 @@ struct CYLambda :
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYModule :
+ CYNext,
+ CYThing
+{
+ CYWord *part_;
+
+ CYModule(CYWord *part, CYModule *next = NULL) :
+ CYNext(next),
+ part_(part)
+ {
+ }
+
+ CYString *Replace(CYContext &context, const char *separator) const;
+ void Output(CYOutput &out) const;
+};
+
+struct CYImport :
+ CYStatement
+{
+ CYModule *module_;
+
+ CYImport(CYModule *module) :
+ module_(module)
+ {
+ }
+
+ virtual CYStatement *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
+struct CYExternal :
+ CYStatement
+{
+ CYString *abi_;
+ CYTypedIdentifier *typed_;
+
+ CYExternal(CYString *abi, CYTypedIdentifier *typed) :
+ abi_(abi),
+ typed_(typed)
+ {
+ }
+
+ virtual CYStatement *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYTypeDefinition :
CYStatement
{
@@ -1814,6 +1961,8 @@ struct CYTypeFunctionWith :
virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+
+ virtual CYTypeFunctionWith *Function() { return this; }
};
namespace cy {
@@ -1823,11 +1972,11 @@ struct Catch :
CYThing
{
CYIdentifier *name_;
- CYBlock code_;
+ CYStatement *code_;
- Catch(CYIdentifier *name, CYStatement *statements) :
+ Catch(CYIdentifier *name, CYStatement *code) :
name_(name),
- code_(statements)
+ code_(code)
{
}
@@ -1838,12 +1987,12 @@ struct Catch :
struct Try :
CYStatement
{
- CYBlock code_;
+ CYStatement *code_;
Catch *catch_;
CYFinally *finally_;
- Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
- code_(statements),
+ Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
+ code_(code),
catch_(_catch),
finally_(finally)
{