X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/51b2dc6b0c969f9297c8464cb91e93d65819e0ae..20c32d79db6c7cb83737ef0142fd75d7ec35c81c:/Syntax.hpp diff --git a/Syntax.hpp b/Syntax.hpp index a83659d..4b25b30 100644 --- a/Syntax.hpp +++ b/Syntax.hpp @@ -1,5 +1,5 @@ -/* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2015 Jay Freeman (saurik) +/* Cycript - The Truly Universal Scripting Language + * Copyright (C) 2009-2016 Jay Freeman (saurik) */ /* GNU Affero General Public License, Version 3 {{{ */ @@ -28,13 +28,30 @@ #include #include #include -#include -#include #include "List.hpp" #include "Location.hpp" #include "Options.hpp" #include "Pooling.hpp" +#include "String.hpp" + +double CYCastDouble(const char *value, size_t size); +double CYCastDouble(const char *value); +double CYCastDouble(CYUTF8String value); + +void CYNumerify(std::ostringstream &str, double value); + +enum CYStringifyMode { + CYStringifyModeLegacy, + CYStringifyModeCycript, + CYStringifyModeNative, +}; + +void CYStringify(std::ostringstream &str, const char *data, size_t size, CYStringifyMode mode); + +// XXX: this really should not be here ... :/ +void *CYPoolFile(CYPool &pool, const char *path, size_t *psize); +CYUTF8String CYPoolFileUTF8String(CYPool &pool, const char *path); struct CYContext; @@ -110,6 +127,8 @@ struct CYOutput { struct CYExpression; struct CYAssignment; +struct CYIdentifier; +struct CYNumber; struct CYPropertyName { virtual bool Computed() const { @@ -120,6 +139,14 @@ struct CYPropertyName { return false; } + virtual CYIdentifier *Identifier() { + return NULL; + } + + virtual CYNumber *Number(CYContext &context) { + return NULL; + } + virtual CYExpression *PropertyName(CYContext &context) = 0; virtual void PropertyName(CYOutput &out) const = 0; }; @@ -140,6 +167,7 @@ enum CYFlags { CYNoRightHand = (1 << 5), CYNoDangle = (1 << 6), CYNoInteger = (1 << 7), + CYNoColon = (1 << 8), CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass), }; @@ -201,6 +229,13 @@ struct CYStatement : typedef CYList CYStatements; +struct CYForInitializer : + CYStatement +{ + virtual CYForInitializer *Replace(CYContext &context) = 0; + virtual void Output(CYOutput &out, CYFlags flags) const = 0; +}; + struct CYWord : CYThing, CYPropertyName @@ -212,10 +247,6 @@ struct CYWord : { } - void Set(const char *value) { - word_ = value; - } - virtual bool Constructor() const { return strcmp(word_, "constructor") == 0; } @@ -232,24 +263,37 @@ _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) { return lhs << rhs.Word(); } +enum CYIdentifierKind { + CYIdentifierArgument, + CYIdentifierCatch, + CYIdentifierGlobal, + CYIdentifierLexical, + CYIdentifierMagic, + CYIdentifierOther, + CYIdentifierVariable, +}; + struct CYIdentifier : CYNext, CYWord { - CYIdentifier *replace_; + CYLocation location_; size_t offset_; size_t usage_; CYIdentifier(const char *word) : CYWord(word), - replace_(NULL), offset_(0), usage_(0) { } + CYIdentifier *Identifier() override { + return this; + } + virtual const char *Word() const; - CYIdentifier *Replace(CYContext &context); + CYIdentifier *Replace(CYContext &context, CYIdentifierKind); }; struct CYLabel : @@ -286,38 +330,43 @@ struct CYIdentifierValueLess : } }; -enum CYIdentifierFlags { - CYIdentifierArgument, - CYIdentifierVariable, - CYIdentifierOther, - CYIdentifierMagic, - CYIdentifierCatch, -}; - -typedef std::set CYCStringSet; -typedef std::set CYIdentifierValueSet; -typedef std::map CYIdentifierAddressFlagsMap; - -struct CYIdentifierUsage { +struct CYIdentifierFlags : + CYNext +{ CYIdentifier *identifier_; - size_t usage_; -}; + CYIdentifierKind kind_; + unsigned count_; + unsigned offset_; -typedef std::vector CYIdentifierUsageVector; + CYIdentifierFlags(CYIdentifier *identifier, CYIdentifierKind kind, CYIdentifierFlags *next = NULL) : + CYNext(next), + identifier_(identifier), + kind_(kind), + count_(0), + offset_(0) + { + } +}; struct CYScope { bool transparent_; CYScope *parent_; + bool damaged_; + CYIdentifierFlags *shadow_; - CYIdentifierAddressFlagsMap internal_; - CYIdentifierValueSet identifiers_; + CYIdentifierFlags *internal_; 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); + CYIdentifierFlags *Lookup(CYContext &context, const char *word); + CYIdentifierFlags *Lookup(CYContext &context, CYIdentifier *identifier); + + CYIdentifierFlags *Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind); + void Merge(CYContext &context, const CYIdentifierFlags *flags); + void Close(CYContext &context, CYStatement *&statements); + void Close(CYContext &context); + void Damage(); }; struct CYScript : @@ -344,12 +393,12 @@ struct CYContext { CYThisScope *this_; CYIdentifier *super_; - CYIdentifierUsageVector rename_; - CYNonLocal *nonlocal_; CYNonLocal *nextlocal_; unsigned unique_; + std::vector replace_; + CYContext(CYOptions &options) : options_(options), scope_(NULL), @@ -444,18 +493,13 @@ struct CYBlock : virtual CYStatement *Return(); }; -struct CYForInitializer { - virtual CYExpression *Replace(CYContext &context) = 0; - virtual void Output(CYOutput &out, CYFlags flags) const = 0; -}; +struct CYTarget; +struct CYVar; struct CYForInInitializer { - virtual void ForIn(CYOutput &out, CYFlags flags) const = 0; - virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0; - - virtual CYExpression *Replace(CYContext &context) = 0; - virtual CYAssignment *Assignment(CYContext &context) = 0; + virtual CYStatement *Initialize(CYContext &context, CYExpression *value) = 0; + virtual CYTarget *Replace(CYContext &context) = 0; virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; @@ -465,8 +509,6 @@ struct CYNumber; struct CYString; struct CYExpression : - CYForInitializer, - CYForInInitializer, CYThing { virtual int Precedence() const = 0; @@ -475,17 +517,17 @@ struct CYExpression : return true; } - virtual void ForIn(CYOutput &out, CYFlags flags) const; - virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); + virtual bool Eval() const { + return false; + } - virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); + virtual CYTarget *AddArgument(CYContext &context, CYExpression *value); virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const = 0; void Output(CYOutput &out, int precedence, CYFlags flags) const; virtual CYExpression *Replace(CYContext &context) = 0; - virtual CYAssignment *Assignment(CYContext &context); virtual CYExpression *Primitive(CYContext &context) { return NULL; @@ -506,6 +548,24 @@ struct CYExpression : } }; +struct CYTarget : + CYExpression, + CYForInInitializer +{ + virtual bool RightHand() const { + return false; + } + + virtual bool IsNew() const { + return false; + } + + virtual CYStatement *Initialize(CYContext &context, CYExpression *value); + + virtual CYTarget *Replace(CYContext &context) = 0; + using CYExpression::Output; +}; + #define CYAlphabetic(value) \ virtual bool Alphabetic() const { \ return value; \ @@ -517,11 +577,6 @@ struct CYExpression : return Precedence_; \ } -#define CYRightHand(value) \ - virtual bool RightHand() const { \ - return value; \ - } - struct CYCompound : CYExpression { @@ -545,7 +600,7 @@ struct CYCompound : }; struct CYParenthetical : - CYExpression + CYTarget { CYExpression *expression_; @@ -556,21 +611,21 @@ struct CYParenthetical : CYPrecedence(0) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); void Output(CYOutput &out, CYFlags flags) const; }; -struct CYDeclaration; +struct CYBinding; struct CYFunctionParameter : CYNext, CYThing { - CYForInInitializer *initialiser_; + CYBinding *binding_; - CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) : + CYFunctionParameter(CYBinding *binding, CYFunctionParameter *next = NULL) : CYNext(next), - initialiser_(initialiser) + binding_(binding) { } @@ -587,11 +642,6 @@ struct CYComprehension : { } - CYComprehension *Modify(CYComprehension *next) { - next_ = next; - return this; - } - virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; CYFunctionParameter *Parameters(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; @@ -601,13 +651,13 @@ struct CYComprehension : struct CYForInComprehension : CYComprehension { - CYDeclaration *declaration_; - CYExpression *set_; + CYBinding *binding_; + CYExpression *iterable_; - CYForInComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) : + CYForInComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) : CYComprehension(next), - declaration_(declaration), - set_(set) + binding_(binding), + iterable_(iterable) { } @@ -619,13 +669,13 @@ struct CYForInComprehension : struct CYForOfComprehension : CYComprehension { - CYDeclaration *declaration_; - CYExpression *set_; + CYBinding *binding_; + CYExpression *iterable_; - CYForOfComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) : + CYForOfComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) : CYComprehension(next), - declaration_(declaration), - set_(set) + binding_(binding), + iterable_(iterable) { } @@ -651,7 +701,7 @@ struct CYIfComprehension : }; struct CYArrayComprehension : - CYExpression + CYTarget { CYExpression *expression_; CYComprehension *comprehensions_; @@ -664,15 +714,16 @@ struct CYArrayComprehension : CYPrecedence(0) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYLiteral : - CYExpression + CYTarget { + CYLocation location_; + CYPrecedence(0) - CYRightHand(false) virtual CYExpression *Primitive(CYContext &context) { return this; @@ -682,14 +733,13 @@ struct CYLiteral : struct CYTrivial : CYLiteral { - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); }; struct CYMagic : - CYExpression + CYTarget { CYPrecedence(0) - CYRightHand(false) }; struct CYRange { @@ -751,6 +801,7 @@ struct CYString : return value_; } + virtual CYIdentifier *Identifier() const; virtual const char *Word() const; virtual CYNumber *Number(CYContext &context); @@ -782,7 +833,7 @@ struct CYSpan : }; struct CYTemplate : - CYExpression + CYTarget { CYString *string_; CYSpan *spans_; @@ -794,9 +845,10 @@ struct CYTemplate : } CYPrecedence(0) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + virtual CYString *String(CYContext &context); + + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -873,13 +925,19 @@ struct CYNull : struct CYThis : CYMagic { - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYBoolean : CYTrivial { + CYPrecedence(4) + + virtual bool RightHand() const { + return true; + } + virtual bool Value() const = 0; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -907,7 +965,7 @@ struct CYTrue : }; struct CYVariable : - CYExpression + CYTarget { CYIdentifier *name_; @@ -922,14 +980,33 @@ struct CYVariable : } CYPrecedence(0) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + virtual bool Eval() const { + return strcmp(name_->Word(), "eval") == 0; + } + + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; virtual CYFunctionParameter *Parameter() const; }; +struct CYSymbol : + CYTarget +{ + const char *name_; + + CYSymbol(const char *name) : + name_(name) + { + } + + CYPrecedence(0) + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYPrefix : CYExpression { @@ -993,17 +1070,17 @@ struct CYPostfix : struct CYAssignment : CYExpression { - CYExpression *lhs_; + CYTarget *lhs_; CYExpression *rhs_; - CYAssignment(CYExpression *lhs, CYExpression *rhs) : + CYAssignment(CYTarget *lhs, CYExpression *rhs) : lhs_(lhs), rhs_(rhs) { } - void SetLeft(CYExpression *lhs) { - lhs_ = lhs; + void SetRight(CYExpression *rhs) { + rhs_ = rhs; } virtual const char *Operator() const = 0; @@ -1043,11 +1120,11 @@ struct CYClause : CYThing, CYNext { - CYExpression *case_; + CYExpression *value_; CYStatement *code_; - CYClause(CYExpression *_case, CYStatement *code) : - case_(_case), + CYClause(CYExpression *value, CYStatement *code) : + value_(value), code_(code) { } @@ -1057,21 +1134,26 @@ struct CYClause : }; struct CYElement : + CYNext, CYThing { + CYElement(CYElement *next) : + CYNext(next) + { + } + virtual bool Elision() const = 0; virtual void Replace(CYContext &context) = 0; }; struct CYElementValue : - CYNext, CYElement { CYExpression *value_; - CYElementValue(CYExpression *value, CYElement *next) : - CYNext(next), + CYElementValue(CYExpression *value, CYElement *next = NULL) : + CYElement(next), value_(value) { } @@ -1089,7 +1171,8 @@ struct CYElementSpread : { CYExpression *value_; - CYElementSpread(CYExpression *value) : + CYElementSpread(CYExpression *value, CYElement *next = NULL) : + CYElement(next), value_(value) { } @@ -1112,103 +1195,117 @@ struct CYArray : { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYDeclaration : - CYForInInitializer -{ +struct CYBinding { CYIdentifier *identifier_; - CYExpression *initialiser_; + CYExpression *initializer_; - CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) : + CYBinding(CYIdentifier *identifier, CYExpression *initializer = NULL) : identifier_(identifier), - initialiser_(initialiser) + initializer_(initializer) { } - virtual void ForIn(CYOutput &out, CYFlags flags) const; - virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); + CYTarget *Target(CYContext &context); - virtual CYExpression *Replace(CYContext &context); + virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; - virtual CYAssignment *Assignment(CYContext &context); - CYVariable *Variable(CYContext &context); +struct CYForLexical : + CYForInInitializer +{ + bool constant_; + CYBinding *binding_; + + CYForLexical(bool constant, CYBinding *binding) : + constant_(constant), + binding_(binding) + { + } + + virtual CYStatement *Initialize(CYContext &context, CYExpression *value); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYDeclarations : - CYNext, - CYThing +struct CYForVariable : + CYForInInitializer { - CYDeclaration *declaration_; + CYBinding *binding_; - CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) : - CYNext(next), - declaration_(declaration) + CYForVariable(CYBinding *binding) : + binding_(binding) { } - void Replace(CYContext &context); - - CYExpression *Expression(CYContext &context); - CYArgument *Argument(CYContext &context); - CYFunctionParameter *Parameter(CYContext &context); + virtual CYStatement *Initialize(CYContext &context, CYExpression *value); - virtual void Output(CYOutput &out) const; + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYForDeclarations : - CYForInitializer +struct CYBindings : + CYNext, + CYThing { - CYDeclarations *declarations_; + CYBinding *binding_; - CYForDeclarations(CYDeclarations *declarations) : - declarations_(declarations) + CYBindings(CYBinding *binding, CYBindings *next = NULL) : + CYNext(next), + binding_(binding) { } - virtual CYExpression *Replace(CYContext &context); + CYExpression *Replace(CYContext &context, CYIdentifierKind kind); + + CYArgument *Argument(CYContext &context); + CYFunctionParameter *Parameter(CYContext &context); + + virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYVar : - CYStatement + CYForInitializer { - CYDeclarations *declarations_; + CYBindings *bindings_; - CYVar(CYDeclarations *declarations) : - declarations_(declarations) + CYVar(CYBindings *bindings) : + bindings_(bindings) { } CYCompact(None) - virtual CYStatement *Replace(CYContext &context); + virtual CYForInitializer *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYLet : - CYStatement +struct CYLexical : + CYForInitializer { - CYDeclarations *declarations_; + bool constant_; + CYBindings *bindings_; - CYLet(CYDeclarations *declarations) : - declarations_(declarations) + CYLexical(bool constant, CYBindings *bindings) : + constant_(constant), + bindings_(bindings) { } CYCompact(None) - virtual CYStatement *Replace(CYContext &context); + virtual CYForInitializer *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYBuilder { - CYList declarations_; + CYList bindings_; CYList statements_; operator bool() const { @@ -1228,6 +1325,8 @@ struct CYProperty : { } + virtual bool Update() const; + CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update); void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect); @@ -1256,13 +1355,13 @@ struct CYPropertyValue : struct CYFor : CYStatement { - CYForInitializer *initialiser_; + CYForInitializer *initializer_; CYExpression *test_; CYExpression *increment_; CYStatement *code_; - CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) : - initialiser_(initialiser), + CYFor(CYForInitializer *initializer, CYExpression *test, CYExpression *increment, CYStatement *code) : + initializer_(initializer), test_(test), increment_(increment), code_(code) @@ -1278,13 +1377,33 @@ struct CYFor : struct CYForIn : CYStatement { - CYForInInitializer *initialiser_; - CYExpression *set_; + CYForInInitializer *initializer_; + CYExpression *iterable_; CYStatement *code_; - CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) : - initialiser_(initialiser), - set_(set), + CYForIn(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) : + initializer_(initializer), + iterable_(iterable), + code_(code) + { + } + + CYCompact(Long) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYForInitialized : + CYStatement +{ + CYBinding *binding_; + CYExpression *iterable_; + CYStatement *code_; + + CYForInitialized(CYBinding *binding, CYExpression *iterable, CYStatement *code) : + binding_(binding), + iterable_(iterable), code_(code) { } @@ -1298,13 +1417,13 @@ struct CYForIn : struct CYForOf : CYStatement { - CYForInInitializer *initialiser_; - CYExpression *set_; + CYForInInitializer *initializer_; + CYExpression *iterable_; CYStatement *code_; - CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) : - initialiser_(initialiser), - set_(set), + CYForOf(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) : + initializer_(initializer), + iterable_(iterable), code_(code) { } @@ -1325,12 +1444,14 @@ struct CYObject : { } - virtual CYExpression *Replace(CYContext &context); + CYTarget *Replace(CYContext &context, CYTarget *seed); + + virtual CYTarget *Replace(CYContext &context); void Output(CYOutput &out, CYFlags flags) const; }; struct CYMember : - CYExpression + CYTarget { CYExpression *object_; CYExpression *property_; @@ -1355,9 +1476,22 @@ struct CYDirectMember : } CYPrecedence(1) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYAttemptMember : + CYMember +{ + CYAttemptMember(CYExpression *object, CYExpression *property) : + CYMember(object, property) + { + } + + CYPrecedence(1) + + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1370,9 +1504,36 @@ struct CYIndirectMember : } CYPrecedence(1) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYResolveMember : + CYMember +{ + CYResolveMember(CYExpression *object, CYExpression *property) : + CYMember(object, property) + { + } + + CYPrecedence(1) + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYSubscriptMember : + CYMember +{ + CYSubscriptMember(CYExpression *object, CYExpression *property) : + CYMember(object, property) + { + } + + CYPrecedence(1) + + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1380,7 +1541,7 @@ namespace cy { namespace Syntax { struct New : - CYExpression + CYTarget { CYExpression *constructor_; CYArgument *arguments_; @@ -1395,58 +1556,109 @@ struct New : return arguments_ == NULL ? 2 : 1; } - CYRightHand(false) + virtual bool IsNew() const { + return true; + } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; - virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); + virtual CYTarget *AddArgument(CYContext &context, CYExpression *value); }; } } -struct CYCall : - CYExpression +struct CYApply : + CYTarget { - CYExpression *function_; CYArgument *arguments_; - CYCall(CYExpression *function, CYArgument *arguments = NULL) : - function_(function), + CYApply(CYArgument *arguments = NULL) : arguments_(arguments) { } CYPrecedence(1) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *AddArgument(CYContext &context, CYExpression *value); +}; + +struct CYCall : + CYApply +{ + CYExpression *function_; + + CYCall(CYExpression *function, CYArgument *arguments = NULL) : + CYApply(arguments), + function_(function) + { + } + virtual void Output(CYOutput &out, CYFlags flags) const; + virtual CYTarget *Replace(CYContext &context); +}; + +struct CYEval : + CYApply +{ + CYEval(CYArgument *arguments) : + CYApply(arguments) + { + } - virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); + virtual void Output(CYOutput &out, CYFlags flags) const; + virtual CYTarget *Replace(CYContext &context); }; struct CYRubyProc; +struct CYBraced : + CYTarget +{ + CYTarget *lhs_; + + CYBraced(CYTarget *lhs = NULL) : + lhs_(lhs) + { + } + + CYPrecedence(1) + + void SetLeft(CYTarget *lhs) { + lhs_ = lhs; + } +}; + struct CYRubyBlock : - CYExpression + CYBraced { - CYExpression *call_; CYRubyProc *proc_; - CYRubyBlock(CYExpression *call, CYRubyProc *proc) : - call_(call), + CYRubyBlock(CYTarget *lhs, CYRubyProc *proc) : + CYBraced(lhs), proc_(proc) { } - CYPrecedence(1) - CYRightHand(false) - - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; - virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); + virtual CYTarget *AddArgument(CYContext &context, CYExpression *value); +}; + +struct CYExtend : + CYBraced +{ + CYObject object_; + + CYExtend(CYTarget *lhs, CYProperty *properties = NULL) : + CYBraced(lhs), + object_(properties) + { + } + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYIf : @@ -1531,7 +1743,7 @@ struct CYFunction { struct CYFunctionExpression : CYFunction, - CYExpression + CYTarget { CYIdentifier *name_; @@ -1542,9 +1754,8 @@ struct CYFunctionExpression : } CYPrecedence(0) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + CYTarget *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1558,15 +1769,14 @@ struct CYFatArrow : } CYPrecedence(0) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + CYExpression *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYRubyProc : CYFunction, - CYExpression + CYTarget { CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) : CYFunction(parameters, code) @@ -1574,9 +1784,8 @@ struct CYRubyProc : } CYPrecedence(0) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + CYTarget *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1594,7 +1803,7 @@ struct CYFunctionStatement : CYCompact(None) - virtual CYStatement *Replace(CYContext &context); + CYStatement *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1648,6 +1857,8 @@ struct CYPropertyMethod : { } + bool Update() const override; + virtual CYFunctionExpression *Constructor(); virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect); @@ -1673,7 +1884,7 @@ struct CYClassTail : }; struct CYClassExpression : - CYExpression + CYTarget { CYIdentifier *name_; CYClassTail *tail_; @@ -1685,9 +1896,8 @@ struct CYClassExpression : } CYPrecedence(0) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + CYTarget *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1705,12 +1915,12 @@ struct CYClassStatement : CYCompact(Long) - virtual CYStatement *Replace(CYContext &context); + CYStatement *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYSuperCall : - CYExpression + CYTarget { CYArgument *arguments_; @@ -1720,14 +1930,13 @@ struct CYSuperCall : } CYPrecedence(2) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + CYTarget *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYSuperAccess : - CYExpression + CYTarget { CYExpression *property_; @@ -1737,14 +1946,13 @@ struct CYSuperAccess : } CYPrecedence(1) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + CYTarget *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYExpress : - CYStatement + CYForInitializer { CYExpression *expression_; @@ -1757,7 +1965,7 @@ struct CYExpress : CYCompact(None) - virtual CYStatement *Replace(CYContext &context); + CYForInitializer *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; virtual CYStatement *Return(); @@ -1775,7 +1983,7 @@ struct CYContinue : CYCompact(Short) - virtual CYStatement *Replace(CYContext &context); + CYStatement *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1791,7 +1999,7 @@ struct CYBreak : CYCompact(Short) - virtual CYStatement *Replace(CYContext &context); + CYStatement *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1807,7 +2015,7 @@ struct CYReturn : CYCompact(None) - virtual CYStatement *Replace(CYContext &context); + CYStatement *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1823,7 +2031,7 @@ struct CYYieldGenerator : CYPrecedence(0) - virtual CYExpression *Replace(CYContext &context); + CYExpression *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1844,11 +2052,11 @@ struct CYYieldValue : }; struct CYEmpty : - CYStatement + CYForInitializer { CYCompact(Short) - virtual CYStatement *Replace(CYContext &context); + virtual CYForInitializer *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1869,7 +2077,7 @@ struct CYFinally : struct CYTypeSpecifier : CYThing { - virtual CYExpression *Replace(CYContext &context) = 0; + virtual CYTarget *Replace(CYContext &context) = 0; }; struct CYTypeError : @@ -1878,92 +2086,149 @@ struct CYTypeError : CYTypeError() { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; -struct CYTypeVoid : +enum CYTypeSigning { + CYTypeNeutral, + CYTypeSigned, + CYTypeUnsigned, +}; + +struct CYTypeCharacter : CYTypeSpecifier { - CYTypeVoid() { + CYTypeSigning signing_; + + CYTypeCharacter(CYTypeSigning signing) : + signing_(signing) + { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; -struct CYTypeVariable : +struct CYTypeInt128 : CYTypeSpecifier { - CYIdentifier *name_; + CYTypeSigning signing_; - CYTypeVariable(CYIdentifier *name) : - name_(name) + CYTypeInt128(CYTypeSigning signing) : + signing_(signing) { } - CYTypeVariable(const char *name) : - name_(new($pool) CYIdentifier(name)) + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeIntegral : + CYTypeSpecifier +{ + CYTypeSigning signing_; + int length_; + + CYTypeIntegral(CYTypeSigning signing, int length = 1) : + signing_(signing), + length_(length) { } - virtual CYExpression *Replace(CYContext &context); + CYTypeIntegral *Long() { + if (length_ != 1 && length_ != 2) + return NULL; + ++length_; + return this; + } + + CYTypeIntegral *Short() { + if (length_ != 1) + return NULL; + --length_; + return this; + } + + CYTypeIntegral *Signed() { + if (signing_ != CYTypeNeutral) + return NULL; + signing_ = CYTypeSigned; + return this; + } + + CYTypeIntegral *Unsigned() { + if (signing_ != CYTypeNeutral) + return NULL; + signing_ = CYTypeUnsigned; + return this; + } + + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; -struct CYTypeUnsigned : +struct CYTypeFloating : CYTypeSpecifier { - CYTypeSpecifier *specifier_; + int length_; - CYTypeUnsigned(CYTypeSpecifier *specifier) : - specifier_(specifier) + CYTypeFloating(int length) : + length_(length) { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; -struct CYTypeSigned : +struct CYTypeVoid : CYTypeSpecifier { - CYTypeSpecifier *specifier_; - - CYTypeSigned(CYTypeSpecifier *specifier) : - specifier_(specifier) - { + CYTypeVoid() { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; -struct CYTypeLong : +enum CYTypeReferenceKind { + CYTypeReferenceStruct, + CYTypeReferenceEnum, +}; + +struct CYTypeReference : CYTypeSpecifier { - CYTypeSpecifier *specifier_; + CYTypeReferenceKind kind_; + CYIdentifier *name_; - CYTypeLong(CYTypeSpecifier *specifier) : - specifier_(specifier) + CYTypeReference(CYTypeReferenceKind kind, CYIdentifier *name) : + kind_(kind), + name_(name) { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; -struct CYTypeShort : +struct CYTypeVariable : CYTypeSpecifier { - CYTypeSpecifier *specifier_; + CYIdentifier *name_; + + CYTypeVariable(CYIdentifier *name) : + name_(name) + { + } - CYTypeShort(CYTypeSpecifier *specifier) : - specifier_(specifier) + CYTypeVariable(const char *name) : + name_(new($pool) CYIdentifier(name)) { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; @@ -1979,11 +2244,11 @@ struct CYTypeModifier : virtual int Precedence() const = 0; - virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0; - CYExpression *Replace(CYContext &context, CYExpression *type); + virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0; + CYTarget *Replace(CYContext &context, CYTarget *type); - virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0; - void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const; + virtual void Output(CYOutput &out, CYPropertyName *name) const = 0; + void Output(CYOutput &out, int precedence, CYPropertyName *name, bool space) const; virtual CYTypeFunctionWith *Function() { return NULL; } }; @@ -2001,8 +2266,8 @@ struct CYTypeArrayOf : CYPrecedence(1) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); - virtual void Output(CYOutput &out, CYIdentifier *identifier) const; + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); + void Output(CYOutput &out, CYPropertyName *name) const override; }; struct CYTypeConstant : @@ -2015,8 +2280,8 @@ struct CYTypeConstant : CYPrecedence(0) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); - virtual void Output(CYOutput &out, CYIdentifier *identifier) const; + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); + void Output(CYOutput &out, CYPropertyName *name) const override; }; struct CYTypePointerTo : @@ -2029,8 +2294,8 @@ struct CYTypePointerTo : CYPrecedence(0) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); - virtual void Output(CYOutput &out, CYIdentifier *identifier) const; + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); + void Output(CYOutput &out, CYPropertyName *name) const override; }; struct CYTypeVolatile : @@ -2043,58 +2308,71 @@ struct CYTypeVolatile : CYPrecedence(0) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); - virtual void Output(CYOutput &out, CYIdentifier *identifier) const; + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); + void Output(CYOutput &out, CYPropertyName *name) const override; }; -struct CYTypedIdentifier : - CYNext, +struct CYType : 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), + CYType(CYTypeSpecifier *specifier = NULL, CYTypeModifier *modifier = NULL) : specifier_(specifier), modifier_(modifier) { } - inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) { + inline CYType *Modify(CYTypeModifier *modifier) { CYSetLast(modifier_) = modifier; return this; } - virtual CYExpression *Replace(CYContext &context); + void Output(CYOutput &out, CYPropertyName *name) const; + + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; CYTypeFunctionWith *Function(); }; +struct CYTypedLocation : + CYType +{ + CYLocation location_; + + CYTypedLocation(const CYLocation &location) : + location_(location) + { + } +}; + +struct CYTypedName : + CYTypedLocation +{ + CYPropertyName *name_; + + CYTypedName(const CYLocation &location, CYPropertyName *name = NULL) : + CYTypedLocation(location), + name_(name) + { + } +}; + struct CYEncodedType : - CYExpression + CYTarget { - CYTypedIdentifier *typed_; + CYType *typed_; - CYEncodedType(CYTypedIdentifier *typed) : + CYEncodedType(CYType *typed) : typed_(typed) { } CYPrecedence(1) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2102,11 +2380,13 @@ struct CYTypedParameter : CYNext, CYThing { - CYTypedIdentifier *typed_; + CYType *type_; + CYIdentifier *name_; - CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) : + CYTypedParameter(CYType *type, CYIdentifier *name, CYTypedParameter *next = NULL) : CYNext(next), - typed_(typed) + type_(type), + name_(name) { } @@ -2117,14 +2397,25 @@ struct CYTypedParameter : virtual void Output(CYOutput &out) const; }; +struct CYTypedFormal { + bool variadic_; + CYTypedParameter *parameters_; + + CYTypedFormal(bool variadic) : + variadic_(variadic), + parameters_(NULL) + { + } +}; + struct CYLambda : - CYExpression + CYTarget { - CYTypedIdentifier *typed_; + CYType *typed_; CYTypedParameter *parameters_; CYStatement *code_; - CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) : + CYLambda(CYType *typed, CYTypedParameter *parameters, CYStatement *code) : typed_(typed), parameters_(parameters), code_(code) @@ -2133,7 +2424,7 @@ struct CYLambda : CYPrecedence(1) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2169,15 +2460,70 @@ struct CYImport : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYExternal : +struct CYImportSpecifier : + CYNext +{ + CYWord *name_; + CYIdentifier *binding_; + + CYImportSpecifier(CYWord *name, CYIdentifier *binding) : + name_(name), + binding_(binding) + { + } + + CYStatement *Replace(CYContext &context, CYIdentifier *module); +}; + +struct CYImportDeclaration : CYStatement +{ + CYImportSpecifier *specifiers_; + CYString *module_; + + CYImportDeclaration(CYImportSpecifier *specifiers, CYString *module) : + specifiers_(specifiers), + module_(module) + { + } + + CYCompact(None) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYExternalExpression : + CYTarget { CYString *abi_; - CYTypedIdentifier *typed_; + CYType *type_; + CYPropertyName *name_; - CYExternal(CYString *abi, CYTypedIdentifier *typed) : + CYExternalExpression(CYString *abi, CYType *type, CYPropertyName *name) : abi_(abi), - typed_(typed) + type_(type), + name_(name) + { + } + + CYPrecedence(0) + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYExternalDefinition : + CYStatement +{ + CYString *abi_; + CYType *type_; + CYIdentifier *name_; + + CYExternalDefinition(CYString *abi, CYType *type, CYIdentifier *name) : + abi_(abi), + type_(type), + name_(name) { } @@ -2187,13 +2533,31 @@ struct CYExternal : virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYTypeExpression : + CYTarget +{ + CYType *typed_; + + CYTypeExpression(CYType *typed) : + typed_(typed) + { + } + + CYPrecedence(0) + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYTypeDefinition : CYStatement { - CYTypedIdentifier *typed_; + CYType *type_; + CYIdentifier *name_; - CYTypeDefinition(CYTypedIdentifier *typed) : - typed_(typed) + CYTypeDefinition(CYType *type, CYIdentifier *name) : + type_(type), + name_(name) { } @@ -2216,29 +2580,125 @@ struct CYTypeBlockWith : CYPrecedence(0) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); - virtual void Output(CYOutput &out, CYIdentifier *identifier) const; + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); + void Output(CYOutput &out, CYPropertyName *name) const override; }; struct CYTypeFunctionWith : CYTypeModifier { + bool variadic_; CYTypedParameter *parameters_; - CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) : + CYTypeFunctionWith(bool variadic, CYTypedParameter *parameters, CYTypeModifier *next = NULL) : CYTypeModifier(next), + variadic_(variadic), parameters_(parameters) { } CYPrecedence(1) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); - virtual void Output(CYOutput &out, CYIdentifier *identifier) const; + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); + void Output(CYOutput &out, CYPropertyName *name) const override; virtual CYTypeFunctionWith *Function() { return this; } }; +struct CYTypeStructField : + CYNext +{ + CYType *type_; + CYPropertyName *name_; + + CYTypeStructField(CYType *type, CYPropertyName *name, CYTypeStructField *next = NULL) : + CYNext(next), + type_(type), + name_(name) + { + } +}; + +struct CYStructTail : + CYThing +{ + CYTypeStructField *fields_; + + CYStructTail(CYTypeStructField *fields) : + fields_(fields) + { + } + + CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeStruct : + CYTypeSpecifier +{ + CYIdentifier *name_; + CYStructTail *tail_; + + CYTypeStruct(CYIdentifier *name, CYStructTail *tail) : + name_(name), + tail_(tail) + { + } + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYStructDefinition : + CYStatement +{ + CYIdentifier *name_; + CYStructTail *tail_; + + CYStructDefinition(CYIdentifier *name, CYStructTail *tail) : + name_(name), + tail_(tail) + { + } + + CYCompact(None) + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYEnumConstant : + CYNext +{ + CYIdentifier *name_; + CYNumber *value_; + + CYEnumConstant(CYIdentifier *name, CYNumber *value, CYEnumConstant *next = NULL) : + CYNext(next), + name_(name), + value_(value) + { + } +}; + +struct CYTypeEnum : + CYTypeSpecifier +{ + CYIdentifier *name_; + CYTypeSpecifier *specifier_; + CYEnumConstant *constants_; + + CYTypeEnum(CYIdentifier *name, CYTypeSpecifier *specifier, CYEnumConstant *constants) : + name_(name), + specifier_(specifier), + constants_(constants) + { + } + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + namespace cy { namespace Syntax { @@ -2383,20 +2843,20 @@ struct CYAddressOf : }; struct CYIndirect : - CYPrefix + CYTarget { + CYExpression *rhs_; + CYIndirect(CYExpression *rhs) : - CYPrefix(rhs) + rhs_(rhs) { } - virtual const char *Operator() const { - return "*"; - } - - CYAlphabetic(false) + // XXX: this should be checked + CYPrecedence(2) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; }; #define CYReplace \ @@ -2453,7 +2913,7 @@ struct CYIndirect : struct CY ## name ## Assign : \ CYAssignment \ { args \ - CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \ + CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \ CYAssignment(lhs, rhs) \ { \ } \