X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/ad3b38bb9a44094065b05115c4b2518f47054a3f..b900e1a43acf792e7eaccff2418919d02382cd3c:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index b133af2..93d44cb 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,29 +1,28 @@ /* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2010 Jay Freeman (saurik) + * Copyright (C) 2009-2015 Jay Freeman (saurik) */ -/* GNU Lesser 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 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 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. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Cycript. If not, see . + * 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 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 . **/ /* }}} */ #ifndef CYCRIPT_PARSER_HPP #define CYCRIPT_PARSER_HPP -#include - +#include #include #include #include @@ -32,26 +31,25 @@ #include #include -#include "location.hh" - #include "List.hpp" +#include "Location.hpp" #include "Pooling.hpp" #include "Options.hpp" -class CYContext; +struct CYContext; struct CYThing { - virtual ~CYThing() { - } - virtual void Output(struct CYOutput &out) const = 0; }; struct CYOutput { - std::ostream &out_; + std::streambuf &out_; + CYPosition position_; + CYOptions &options_; bool pretty_; unsigned indent_; + unsigned recent_; bool right_; enum { @@ -62,11 +60,12 @@ struct CYOutput { Terminated } mode_; - CYOutput(std::ostream &out, CYOptions &options) : + CYOutput(std::streambuf &out, CYOptions &options) : out_(out), options_(options), pretty_(false), indent_(0), + recent_(0), right_(false), mode_(NoMode) { @@ -75,6 +74,25 @@ struct CYOutput { void Check(char value); void Terminate(); + _finline void operator ()(char value) { + _assert(out_.sputc(value) != EOF); + recent_ = indent_; + if (value == '\n') + position_.lines(1); + else + position_.columns(1); + } + + _finline void operator ()(const char *data, std::streamsize size) { + _assert(out_.sputn(data, size) == size); + recent_ = indent_; + position_.columns(size); + } + + _finline void operator ()(const char *data) { + return operator ()(data, strlen(data)); + } + CYOutput &operator <<(char rhs); CYOutput &operator <<(const char *rhs); @@ -92,12 +110,10 @@ struct CYOutput { struct CYPropertyName { virtual void PropertyName(CYOutput &out) const = 0; - - virtual ~CYPropertyName() { - } }; struct CYExpression; +struct CYAssignment; enum CYNeeded { CYNever = -1, @@ -145,18 +161,30 @@ _finline CYFlags CYCenter(CYFlags flags) { return CYLeft(CYRight(flags)); } -struct CYStatement : - CYNext -{ - virtual ~CYStatement() { +enum CYCompactType { + CYCompactNone, + CYCompactLong, + CYCompactShort, +}; + +#define CYCompact(type) \ + virtual CYCompactType Compact() const { \ + return CYCompact ## type; \ } - void Single(CYOutput &out, CYFlags flags) const; +struct CYStatement : + CYNext, + CYThing +{ + void Single(CYOutput &out, CYFlags flags, CYCompactType request) const; void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const; + virtual void Output(CYOutput &out) const; - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context) = 0; + virtual CYCompactType Compact() const = 0; + virtual CYStatement *Return(); + private: virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; @@ -191,9 +219,6 @@ struct CYStatements { }; struct CYClassName { - virtual ~CYClassName() { - } - virtual CYExpression *ClassName(CYContext &context, bool object) = 0; virtual void ClassName(CYOutput &out, bool object) const = 0; }; @@ -247,20 +272,6 @@ struct CYIdentifier : CYIdentifier *Replace(CYContext &context); }; -struct CYComment : - CYStatement -{ - const char *value_; - - CYComment(const char *value) : - value_(value) - { - } - - virtual CYStatement *Replace(CYContext &context); - virtual void Output(CYOutput &out, CYFlags flags) const; -}; - struct CYLabel : CYStatement { @@ -273,6 +284,8 @@ struct CYLabel : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -312,42 +325,28 @@ struct CYIdentifierUsage { typedef std::vector CYIdentifierUsageVector; -// XXX: strategy pattern, maybe subclass -enum CYScopeType { - CYScopeCatch, - CYScopeFunction, - CYScopeProgram, -}; - struct CYScope { - CYScopeType type_; - - CYContext &context_; - CYStatement *&statements_; - + bool transparent_; CYScope *parent_; CYIdentifierAddressFlagsMap internal_; CYIdentifierValueSet identifiers_; - CYScope(CYScopeType type, 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 : +struct CYScript : CYThing { - CYStatement *statements_; + CYStatement *code_; - CYProgram(CYStatement *statements) : - statements_(statements) + CYScript(CYStatement *code) : + code_(code) { } @@ -356,11 +355,14 @@ struct CYProgram : }; struct CYNonLocal; +struct CYThisScope; struct CYContext { CYOptions &options_; CYScope *scope_; + CYThisScope *this_; + CYIdentifierUsageVector rename_; CYNonLocal *nonlocal_; @@ -370,22 +372,25 @@ struct CYContext { CYContext(CYOptions &options) : options_(options), scope_(NULL), + this_(NULL), nonlocal_(NULL), nextlocal_(NULL), unique_(0) { } - virtual ~CYContext() { - } + void ReplaceAll(CYStatement *&statement) { + if (statement == NULL) + return; + CYStatement *next(statement->next_); - template - void ReplaceAll(Type_ *&values) { - Type_ **last(&values); - CYForEach (next, values) { - Replace(*last = next); - last = &(*last)->next_; - } + Replace(statement); + ReplaceAll(next); + + if (statement == NULL) + statement = next; + else + statement->SetNext(next); } template @@ -419,167 +424,97 @@ struct CYNonLocal { } }; -struct CYBlock : - CYStatement, - CYThing +struct CYThisScope : + CYNext { - CYStatement *statements_; + CYIdentifier *identifier_; - CYBlock(CYStatement *statements) : - statements_(statements) + CYThisScope() : + identifier_(NULL) { } - operator CYStatement *() const { - return statements_; - } - - void AddPrev(CYStatement *statement) { - CYSetLast(statement, statements_); - statements_ = statement; + CYIdentifier *Identifier(CYContext &context) { + if (next_ != NULL) + return next_->Identifier(context); + if (identifier_ == NULL) + identifier_ = context.Unique(); + return identifier_; } - - virtual CYStatement *Replace(CYContext &context); - - virtual void Output(CYOutput &out) const; - virtual void Output(CYOutput &out, CYFlags flags) const; -}; - -enum CYState { - CYClear, - CYRestricted, - CYNewLine }; -class CYDriver { - public: - CYState state_; - void *scanner_; - - const char *data_; - size_t size_; - FILE *file_; - - bool strict_; - - enum Condition { - RegExpCondition, - XMLContentCondition, - XMLTagCondition, - }; - - std::string filename_; - - struct Error { - bool warning_; - cy::location location_; - std::string message_; - }; - - typedef std::vector Errors; - - CYProgram *program_; - Errors errors_; - - bool auto_; - - struct Context { - CYExpression *context_; - - Context(CYExpression *context) : - context_(context) - { - } - - typedef std::vector Words; - Words words_; - }; - - typedef std::vector Contexts; - Contexts contexts_; - - CYExpression *context_; - - enum Mode { - AutoNone, - AutoPrimary, - AutoDirect, - AutoIndirect, - AutoMessage - } mode_; +struct CYBlock : + CYStatement +{ + CYStatement *code_; - private: - void ScannerInit(); - void ScannerDestroy(); + CYBlock(CYStatement *code) : + code_(code) + { + } - public: - CYDriver(const std::string &filename = ""); - ~CYDriver(); + CYCompact(Short) - Condition GetCondition(); - void SetCondition(Condition condition); + virtual CYStatement *Replace(CYContext &context); - void PushCondition(Condition condition); - void PopCondition(); + virtual void Output(CYOutput &out, CYFlags flags) const; - void Warning(const cy::location &location, const char *message); + virtual CYStatement *Return(); }; -struct CYForInitialiser { - virtual ~CYForInitialiser() { - } - - virtual void For(CYOutput &out) const = 0; +struct CYForInitializer { virtual CYExpression *Replace(CYContext &context) = 0; + virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; -struct CYForInInitialiser { - virtual ~CYForInInitialiser() { - } - +struct CYForInInitializer { virtual void ForIn(CYOutput &out, CYFlags flags) const = 0; - virtual const char *ForEachIn() const = 0; virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0; + virtual CYExpression *Replace(CYContext &context) = 0; + virtual CYAssignment *Assignment(CYContext &context) = 0; + + virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; +struct CYFunctionParameter; + struct CYNumber; struct CYString; struct CYExpression : - CYNext, - CYForInitialiser, - CYForInInitialiser, + CYForInitializer, + CYForInInitializer, CYClassName, CYThing { - virtual unsigned Precedence() const = 0; + virtual int Precedence() const = 0; virtual bool RightHand() const { return true; } - virtual void For(CYOutput &out) const; virtual void ForIn(CYOutput &out, CYFlags flags) const; - - virtual const char *ForEachIn() const; virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const = 0; - void Output(CYOutput &out, unsigned precedence, CYFlags flags) const; + void Output(CYOutput &out, int precedence, CYFlags flags) const; virtual CYExpression *ClassName(CYContext &context, bool object); virtual void ClassName(CYOutput &out, bool object) const; virtual CYExpression *Replace(CYContext &context) = 0; + 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; } @@ -599,8 +534,8 @@ struct CYExpression : } #define CYPrecedence(value) \ - static const unsigned Precedence_ = value; \ - virtual unsigned Precedence() const { \ + static const int Precedence_ = value; \ + virtual int Precedence() const { \ return Precedence_; \ } @@ -612,59 +547,73 @@ 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) virtual CYExpression *Replace(CYContext &context); void Output(CYOutput &out, CYFlags flags) const; + + virtual CYFunctionParameter *Parameter() const; }; -struct CYFunctionParameter : - CYNext, - CYThing +struct CYParenthetical : + CYExpression { - CYIdentifier *name_; + CYExpression *expression_; - CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) : - CYNext(next), - name_(name) + CYParenthetical(CYExpression *expression) : + expression_(expression) { } - virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code); - virtual void Output(CYOutput &out) const; + CYPrecedence(0) + + virtual CYExpression *Replace(CYContext &context); + void Output(CYOutput &out, CYFlags flags) const; }; -struct CYOptionalFunctionParameter : - CYFunctionParameter +struct CYDeclaration; + +struct CYFunctionParameter : + CYNext, + CYThing { - CYExpression *initializer_; + CYForInInitializer *initialiser_; - CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) : - CYFunctionParameter(name, next), - initializer_(initializer) + CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) : + CYNext(next), + initialiser_(initialiser) { } - virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code); - virtual void Output(CYOutput &out) const; + void Replace(CYContext &context, CYStatement *&statements); + void Output(CYOutput &out) const; }; struct CYComprehension : CYNext, CYThing { + CYComprehension(CYComprehension *next = NULL) : + CYNext(next) + { + } + + CYComprehension *Modify(CYComprehension *next) { + next_ = next; + return this; + } + virtual const char *Name() const = 0; virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; @@ -679,7 +628,8 @@ struct CYForInComprehension : CYIdentifier *name_; CYExpression *set_; - CYForInComprehension(CYIdentifier *name, CYExpression *set) : + CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) : + CYComprehension(next), name_(name), set_(set) { @@ -694,13 +644,14 @@ struct CYForInComprehension : 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) { @@ -720,7 +671,8 @@ struct CYIfComprehension : { CYExpression *test_; - CYIfComprehension(CYExpression *test) : + CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) : + CYComprehension(next), test_(test) { } @@ -757,6 +709,10 @@ struct CYLiteral : { CYPrecedence(0) CYRightHand(false) + + virtual CYExpression *Primitive(CYContext &context) { + return this; + } }; struct CYTrivial : @@ -841,6 +797,43 @@ struct CYString : virtual void PropertyName(CYOutput &out) const; }; +struct CYElement; + +struct CYSpan : + CYNext +{ + CYExpression *expression_; + CYString *string_; + + CYSpan(CYExpression *expression, CYString *string, CYSpan *next) : + CYNext(next), + expression_(expression), + string_(string) + { + } + + CYElement *Replace(CYContext &context); +}; + +struct CYTemplate : + CYExpression +{ + CYString *string_; + CYSpan *spans_; + + CYTemplate(CYString *string, CYSpan *spans) : + string_(string), + spans_(spans) + { + } + + CYPrecedence(0) + CYRightHand(false) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYNumber : CYTrivial, CYPropertyName @@ -881,14 +874,8 @@ struct CYRegEx : }; struct CYNull : - CYWord, CYTrivial { - CYNull() : - CYWord("null") - { - } - virtual CYNumber *Number(CYContext &context); virtual CYString *String(CYContext &context); @@ -896,14 +883,8 @@ struct CYNull : }; struct CYThis : - CYWord, CYMagic { - CYThis() : - CYWord("this") - { - } - virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -916,14 +897,8 @@ struct CYBoolean : }; struct CYFalse : - CYWord, CYBoolean { - CYFalse() : - CYWord("false") - { - } - virtual bool Value() const { return false; } @@ -933,14 +908,8 @@ struct CYFalse : }; struct CYTrue : - CYWord, CYBoolean { - CYTrue() : - CYWord("true") - { - } - virtual bool Value() const { return true; } @@ -969,6 +938,8 @@ struct CYVariable : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYFunctionParameter *Parameter() const; }; struct CYPrefix : @@ -1076,29 +1047,20 @@ struct CYArgument : { } - void Replace(CYContext &context); + CYArgument *Replace(CYContext &context); void Output(CYOutput &out) const; }; -struct CYBlank : - public CYWord -{ - CYBlank() : - CYWord("") - { - } -}; - struct CYClause : CYThing, CYNext { CYExpression *case_; - CYStatement *statements_; + CYStatement *code_; - CYClause(CYExpression *_case, CYStatement *statements) : + CYClause(CYExpression *_case, CYStatement *code) : case_(_case), - statements_(statements) + code_(code) { } @@ -1155,7 +1117,7 @@ struct CYProperty : }; struct CYDeclaration : - CYForInInitialiser + CYForInInitializer { CYIdentifier *identifier_; CYExpression *initialiser_; @@ -1167,20 +1129,19 @@ struct CYDeclaration : } virtual void ForIn(CYOutput &out, CYFlags flags) const; - - virtual const char *ForEachIn() const; virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); virtual CYExpression *Replace(CYContext &context); + virtual CYAssignment *Assignment(CYContext &context); + CYVariable *Variable(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYDeclarations : CYNext, - CYThing, - CYForInitialiser + CYThing { CYDeclaration *declaration_; @@ -1190,15 +1151,31 @@ struct CYDeclarations : { } - virtual void For(CYOutput &out) const; + void Replace(CYContext &context); - virtual CYCompound *Replace(CYContext &context); + CYExpression *Expression(CYContext &context); CYProperty *Property(CYContext &context); + CYArgument *Argument(CYContext &context); + CYFunctionParameter *Parameter(CYContext &context); virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYForDeclarations : + CYForInitializer +{ + CYDeclarations *declarations_; + + CYForDeclarations(CYDeclarations *declarations) : + declarations_(declarations) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYVar : CYStatement { @@ -1209,22 +1186,26 @@ struct CYVar : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYLet : +struct CYLetStatement : CYStatement { CYDeclarations *declarations_; - CYBlock code_; + CYStatement *code_; - CYLet(CYDeclarations *declarations, CYStatement *statements) : + CYLetStatement(CYDeclarations *declarations, CYStatement *code) : declarations_(declarations), - code_(statements) + code_(code) { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1232,12 +1213,12 @@ struct CYLet : struct CYFor : CYStatement { - CYForInitialiser *initialiser_; + CYForInitializer *initialiser_; CYExpression *test_; CYExpression *increment_; CYStatement *code_; - CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) : + CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) : initialiser_(initialiser), test_(test), increment_(increment), @@ -1245,6 +1226,8 @@ struct CYFor : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1252,35 +1235,39 @@ struct CYFor : struct CYForIn : CYStatement { - CYForInInitialiser *initialiser_; + CYForInInitializer *initialiser_; CYExpression *set_; CYStatement *code_; - CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : + CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) : initialiser_(initialiser), set_(set), code_(code) { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYForEachIn : +struct CYForOf : CYStatement { - CYForInInitialiser *initialiser_; + CYForInInitializer *initialiser_; CYExpression *set_; CYStatement *code_; - CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : + CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) : initialiser_(initialiser), set_(set), code_(code) { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1314,8 +1301,6 @@ struct CYMember : void SetLeft(CYExpression *object) { object_ = object; } - - void Replace_(CYContext &context); }; struct CYDirectMember : @@ -1363,7 +1348,7 @@ struct New : { } - virtual unsigned Precedence() const { + virtual int Precedence() const { return arguments_ == NULL ? 2 : 1; } @@ -1433,8 +1418,12 @@ struct CYIf : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYStatement *Return(); }; struct CYDoWhile : @@ -1449,6 +1438,8 @@ struct CYDoWhile : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1465,6 +1456,8 @@ struct CYWhile : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1473,20 +1466,21 @@ struct CYWhile : struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; - CYBlock code_; + CYStatement *code_; + CYNonLocal *nonlocal_; + bool implicit_; + CYThisScope this_; - CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : + CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) : name_(name), parameters_(parameters), - code_(statements), - nonlocal_(NULL) + code_(code), + nonlocal_(NULL), + implicit_(false) { } - virtual ~CYFunction() { - } - void Inject(CYContext &context); virtual void Replace_(CYContext &context, bool outer); virtual void Output(CYOutput &out, CYFlags flags) const; @@ -1497,8 +1491,25 @@ 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) + { + } + + CYPrecedence(0) + CYRightHand(false) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +// XXX: this should derive from CYAnonymousFunction +struct CYFatArrow : + CYFunction, + CYExpression +{ + CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) : + CYFunction(NULL, parameters, code) { } @@ -1513,8 +1524,8 @@ struct CYFunctionExpression : struct CYRubyProc : CYFunctionExpression { - CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) : - CYFunctionExpression(NULL, parameters, statements) + CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) : + CYFunctionExpression(NULL, parameters, code) { } @@ -1527,11 +1538,13 @@ 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) { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1544,13 +1557,16 @@ struct CYExpress : CYExpress(CYExpression *expression) : expression_(expression) { - if (expression == NULL) + if (expression_ == NULL) throw; } - virtual CYStatement *Collapse(CYContext &context); + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYStatement *Return(); }; struct CYContinue : @@ -1563,6 +1579,8 @@ struct CYContinue : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1577,6 +1595,8 @@ struct CYBreak : { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1591,6 +1611,8 @@ struct CYReturn : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1598,7 +1620,8 @@ struct CYReturn : struct CYEmpty : CYStatement { - virtual CYStatement *Collapse(CYContext &context); + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1606,10 +1629,10 @@ struct CYEmpty : struct CYFinally : CYThing { - CYBlock code_; + CYStatement *code_; - CYFinally(CYStatement *statements) : - code_(statements) + CYFinally(CYStatement *code) : + code_(code) { } @@ -1617,6 +1640,379 @@ 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 +{ + CYTypeModifier(CYTypeModifier *next) : + CYNext(next) + { + } + + virtual int Precedence() const = 0; + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0; + CYExpression *Replace(CYContext &context, CYExpression *type); + + 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 : + CYTypeModifier +{ + CYExpression *size_; + + CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + size_(size) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypeConstant : + CYTypeModifier +{ + CYTypeConstant(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypePointerTo : + CYTypeModifier +{ + CYTypePointerTo(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypeVolatile : + CYTypeModifier +{ + CYTypeVolatile(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypedIdentifier : + CYNext, + CYThing +{ + CYLocation location_; + CYIdentifier *identifier_; + CYTypeSpecifier *specifier_; + CYTypeModifier *modifier_; + + CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) : + location_(location), + identifier_(identifier), + specifier_(NULL), + modifier_(NULL) + { + } + + CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) : + identifier_(NULL), + specifier_(specifier), + modifier_(modifier) + { + } + + inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) { + CYSetLast(modifier_) = modifier; + return this; + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; + + CYTypeFunctionWith *Function(); +}; + +struct CYEncodedType : + CYExpression +{ + CYTypedIdentifier *typed_; + + CYEncodedType(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypedParameter : + CYNext, + CYThing +{ + CYTypedIdentifier *typed_; + + CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) : + CYNext(next), + typed_(typed) + { + } + + CYArgument *Argument(CYContext &context); + CYFunctionParameter *Parameters(CYContext &context); + CYExpression *TypeSignature(CYContext &context, CYExpression *prefix); + + virtual void Output(CYOutput &out) const; +}; + +struct CYLambda : + CYExpression +{ + CYTypedIdentifier *typed_; + CYTypedParameter *parameters_; + CYStatement *code_; + + CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) : + typed_(typed), + parameters_(parameters), + code_(code) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + 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) + { + } + + CYCompact(None) + + 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) + { + } + + CYCompact(None) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypeDefinition : + CYStatement +{ + CYTypedIdentifier *typed_; + + CYTypeDefinition(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + CYCompact(None) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypeBlockWith : + CYTypeModifier +{ + CYTypedParameter *parameters_; + + CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + parameters_(parameters) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypeFunctionWith : + CYTypeModifier +{ + CYTypedParameter *parameters_; + + CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + parameters_(parameters) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; + + virtual CYTypeFunctionWith *Function() { return this; } +}; + namespace cy { namespace Syntax { @@ -1624,11 +2020,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) { } @@ -1639,17 +2035,19 @@ 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) { } + CYCompact(Short) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1664,6 +2062,8 @@ struct Throw : { } + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1682,6 +2082,8 @@ struct CYWith : { } + CYCompact(Long) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1698,6 +2100,21 @@ struct CYSwitch : { } + CYCompact(Long) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYDebugger : + CYStatement +{ + CYDebugger() + { + } + + CYCompact(None) + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1833,7 +2250,7 @@ CYPrefix_(false, "-", Negate) CYPrefix_(false, "~", BitwiseNot) CYPrefix_(false, "!", LogicalNot) -CYInfix_(false, 5, "*", Multiply) +CYInfix_(false, 5, "*", Multiply, CYReplace) CYInfix_(false, 5, "/", Divide) CYInfix_(false, 5, "%", Modulus) CYInfix_(false, 6, "+", Add, CYReplace)