X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/20052ff75c224699725da0f8e096053009c62741..2e43a0b0c288f70981ab2715c8135ac7b9cd0f1b:/Syntax.hpp diff --git a/Syntax.hpp b/Syntax.hpp index 3d248d6..ae0a68b 100644 --- a/Syntax.hpp +++ b/Syntax.hpp @@ -28,13 +28,18 @@ #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); + +void CYNumerify(std::ostringstream &str, double value); +void CYStringify(std::ostringstream &str, const char *data, size_t size, bool c = false); struct CYContext; @@ -78,15 +83,15 @@ struct CYOutput { _assert(out_.sputc(value) != EOF); recent_ = indent_; if (value == '\n') - position_.lines(1); + position_.Lines(1); else - position_.columns(1); + position_.Columns(1); } _finline void operator ()(const char *data, std::streamsize size) { _assert(out_.sputn(data, size) == size); recent_ = indent_; - position_.columns(size); + position_.Columns(size); } _finline void operator ()(const char *data) { @@ -108,13 +113,22 @@ struct CYOutput { } }; +struct CYExpression; +struct CYAssignment; + struct CYPropertyName { + virtual bool Computed() const { + return false; + } + + virtual bool Constructor() const { + return false; + } + + virtual CYExpression *PropertyName(CYContext &context) = 0; virtual void PropertyName(CYOutput &out) const = 0; }; -struct CYExpression; -struct CYAssignment; - enum CYNeeded { CYNever = -1, CYSometimes = 0, @@ -125,12 +139,13 @@ enum CYFlags { CYNoFlags = 0, CYNoBrace = (1 << 0), CYNoFunction = (1 << 1), - CYNoIn = (1 << 2), - CYNoCall = (1 << 3), - CYNoRightHand = (1 << 4), - CYNoDangle = (1 << 5), - CYNoInteger = (1 << 6), - CYNoBF = (CYNoBrace | CYNoFunction), + CYNoClass = (1 << 2), + CYNoIn = (1 << 3), + CYNoCall = (1 << 4), + CYNoRightHand = (1 << 5), + CYNoDangle = (1 << 6), + CYNoInteger = (1 << 7), + CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass), }; _finline CYFlags operator ~(CYFlags rhs) { @@ -154,7 +169,7 @@ _finline CYFlags CYLeft(CYFlags flags) { } _finline CYFlags CYRight(CYFlags flags) { - return flags & ~CYNoBF; + return flags & ~CYNoBFC; } _finline CYFlags CYCenter(CYFlags flags) { @@ -189,44 +204,18 @@ struct CYStatement : virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; -struct CYStatements { - CYStatement *first_; - CYStatement *last_; - - CYStatements() : - first_(NULL), - last_(NULL) - { - } - - operator CYStatement *() const { - return first_; - } - - CYStatements &operator ->*(CYStatement *next) { - if (next != NULL) - if (first_ == NULL) { - first_ = next; - last_ = next; - } else for (;; last_ = last_->next_) - if (last_->next_ == NULL) { - last_->next_ = next; - last_ = next; - break; - } - return *this; - } -}; +typedef CYList CYStatements; -struct CYClassName { - virtual CYExpression *ClassName(CYContext &context, bool object) = 0; - virtual void ClassName(CYOutput &out, bool object) const = 0; +struct CYForInitializer : + CYStatement +{ + virtual CYForInitializer *Replace(CYContext &context) = 0; + virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; struct CYWord : CYThing, - CYPropertyName, - CYClassName + CYPropertyName { const char *word_; @@ -235,15 +224,14 @@ struct CYWord : { } - void Set(const char *value) { - word_ = value; + virtual bool Constructor() const { + return strcmp(word_, "constructor") == 0; } virtual const char *Word() const; virtual void Output(CYOutput &out) const; - virtual CYExpression *ClassName(CYContext &context, bool object); - virtual void ClassName(CYOutput &out, bool object) const; + virtual CYExpression *PropertyName(CYContext &context); virtual void PropertyName(CYOutput &out) const; }; @@ -252,24 +240,33 @@ _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) { } virtual const char *Word() const; - CYIdentifier *Replace(CYContext &context); + CYIdentifier *Replace(CYContext &context, CYIdentifierKind); }; struct CYLabel : @@ -306,38 +303,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 : @@ -362,17 +364,19 @@ struct CYContext { CYScope *scope_; CYThisScope *this_; - - CYIdentifierUsageVector rename_; + CYIdentifier *super_; CYNonLocal *nonlocal_; CYNonLocal *nextlocal_; unsigned unique_; + std::vector replace_; + CYContext(CYOptions &options) : options_(options), scope_(NULL), this_(NULL), + super_(NULL), nonlocal_(NULL), nextlocal_(NULL), unique_(0) @@ -462,18 +466,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; }; @@ -483,9 +482,6 @@ struct CYNumber; struct CYString; struct CYExpression : - CYForInitializer, - CYForInInitializer, - CYClassName, CYThing { virtual int Precedence() const = 0; @@ -494,20 +490,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 *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 NULL; @@ -528,6 +521,20 @@ struct CYExpression : } }; +struct CYTarget : + CYExpression, + CYForInInitializer +{ + virtual bool RightHand() 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; \ @@ -539,11 +546,6 @@ struct CYExpression : return Precedence_; \ } -#define CYRightHand(value) \ - virtual bool RightHand() const { \ - return value; \ - } - struct CYCompound : CYExpression { @@ -567,7 +569,7 @@ struct CYCompound : }; struct CYParenthetical : - CYExpression + CYTarget { CYExpression *expression_; @@ -578,21 +580,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) { } @@ -609,13 +611,6 @@ struct CYComprehension : { } - CYComprehension *Modify(CYComprehension *next) { - next_ = next; - return this; - } - - virtual const char *Name() const = 0; - virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; CYFunctionParameter *Parameters(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; @@ -625,20 +620,16 @@ struct CYComprehension : struct CYForInComprehension : CYComprehension { - CYIdentifier *name_; - CYExpression *set_; + CYBinding *binding_; + CYExpression *iterable_; - CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) : + CYForInComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) : CYComprehension(next), - name_(name), - set_(set) + binding_(binding), + iterable_(iterable) { } - virtual const char *Name() const { - return name_->Word(); - } - virtual CYFunctionParameter *Parameter(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; virtual void Output(CYOutput &out) const; @@ -647,20 +638,16 @@ struct CYForInComprehension : struct CYForOfComprehension : CYComprehension { - CYIdentifier *name_; - CYExpression *set_; + CYBinding *binding_; + CYExpression *iterable_; - CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) : + CYForOfComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) : CYComprehension(next), - name_(name), - set_(set) + binding_(binding), + iterable_(iterable) { } - virtual const char *Name() const { - return name_->Word(); - } - virtual CYFunctionParameter *Parameter(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; virtual void Output(CYOutput &out) const; @@ -677,17 +664,13 @@ struct CYIfComprehension : { } - virtual const char *Name() const { - return NULL; - } - virtual CYFunctionParameter *Parameter(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; virtual void Output(CYOutput &out) const; }; struct CYArrayComprehension : - CYExpression + CYTarget { CYExpression *expression_; CYComprehension *comprehensions_; @@ -700,15 +683,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; @@ -718,14 +702,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 { @@ -794,6 +777,8 @@ struct CYString : CYString *Concat(CYContext &out, CYString *rhs) const; virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *PropertyName(CYContext &context); virtual void PropertyName(CYOutput &out) const; }; @@ -816,7 +801,7 @@ struct CYSpan : }; struct CYTemplate : - CYExpression + CYTarget { CYString *string_; CYSpan *spans_; @@ -828,9 +813,8 @@ struct CYTemplate : } CYPrecedence(0) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -853,6 +837,26 @@ struct CYNumber : virtual CYString *String(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *PropertyName(CYContext &context); + virtual void PropertyName(CYOutput &out) const; +}; + +struct CYComputed : + CYPropertyName +{ + CYExpression *expression_; + + CYComputed(CYExpression *expression) : + expression_(expression) + { + } + + virtual bool Computed() const { + return true; + } + + virtual CYExpression *PropertyName(CYContext &context); virtual void PropertyName(CYOutput &out) const; }; @@ -887,13 +891,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; }; @@ -921,7 +931,7 @@ struct CYTrue : }; struct CYVariable : - CYExpression + CYTarget { CYIdentifier *name_; @@ -936,9 +946,12 @@ 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; @@ -1007,17 +1020,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; @@ -1057,11 +1070,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) { } @@ -1084,7 +1097,7 @@ struct CYElementValue : { CYExpression *value_; - CYElementValue(CYExpression *value, CYElement *next) : + CYElementValue(CYExpression *value, CYElement *next = NULL) : CYNext(next), value_(value) { @@ -1126,67 +1139,74 @@ struct CYArray : { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYProperty : - CYNext, - CYThing -{ - CYPropertyName *name_; - CYExpression *value_; +struct CYBinding { + CYIdentifier *identifier_; + CYExpression *initializer_; - CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) : - CYNext(next), - name_(name), - value_(value) + CYBinding(CYIdentifier *identifier, CYExpression *initializer = NULL) : + identifier_(identifier), + initializer_(initializer) { } - void Replace(CYContext &context); - virtual void Output(CYOutput &out) const; + CYTarget *Target(CYContext &context); + + virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind); + virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYDeclaration : +struct CYForLexical : CYForInInitializer { - CYIdentifier *identifier_; - CYExpression *initialiser_; + bool constant_; + CYBinding *binding_; - CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) : - identifier_(identifier), - initialiser_(initialiser) + CYForLexical(bool constant, CYBinding *binding) : + constant_(constant), + binding_(binding) { } - virtual void ForIn(CYOutput &out, CYFlags flags) const; - virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); + virtual CYStatement *Initialize(CYContext &context, CYExpression *value); - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; - virtual CYAssignment *Assignment(CYContext &context); - CYVariable *Variable(CYContext &context); +struct CYForVariable : + CYForInInitializer +{ + CYBinding *binding_; + CYForVariable(CYBinding *binding) : + 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, +struct CYBindings : + CYNext, CYThing { - CYDeclaration *declaration_; + CYBinding *binding_; - CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) : - CYNext(next), - declaration_(declaration) + CYBindings(CYBinding *binding, CYBindings *next = NULL) : + CYNext(next), + binding_(binding) { } - void Replace(CYContext &context); + CYExpression *Replace(CYContext &context, CYIdentifierKind kind); - CYExpression *Expression(CYContext &context); - CYProperty *Property(CYContext &context); CYArgument *Argument(CYContext &context); CYFunctionParameter *Parameter(CYContext &context); @@ -1194,64 +1214,98 @@ struct CYDeclarations : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYForDeclarations : +struct CYVar : CYForInitializer { - CYDeclarations *declarations_; + CYBindings *bindings_; - CYForDeclarations(CYDeclarations *declarations) : - declarations_(declarations) + CYVar(CYBindings *bindings) : + bindings_(bindings) { } - virtual CYExpression *Replace(CYContext &context); + CYCompact(None) + + virtual CYForInitializer *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYVar : - CYStatement +struct CYLexical : + CYForInitializer { - CYDeclarations *declarations_; + bool constant_; + CYBindings *bindings_; - CYVar(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 CYLetStatement : - CYStatement +struct CYBuilder { + CYList bindings_; + CYList statements_; + + operator bool() const { + return statements_ != NULL; + } +}; + +struct CYProperty : + CYNext, + CYThing { - CYDeclarations *declarations_; - CYStatement *code_; + CYPropertyName *name_; - CYLetStatement(CYDeclarations *declarations, CYStatement *code) : - declarations_(declarations), - code_(code) + CYProperty(CYPropertyName *name, CYProperty *next = NULL) : + CYNext(next), + name_(name) { } - CYCompact(Long) + virtual bool Update() const; - virtual CYStatement *Replace(CYContext &context); - virtual void Output(CYOutput &out, CYFlags flags) const; + CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update); + void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect); + + virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0; + + virtual void Replace(CYContext &context) = 0; + virtual void Output(CYOutput &out) const; +}; + +struct CYPropertyValue : + CYProperty +{ + CYExpression *value_; + + CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) : + CYProperty(name, next), + value_(value) + { + } + + virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect); + virtual void Replace(CYContext &context); + virtual void Output(CYOutput &out) const; }; 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) @@ -1267,13 +1321,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) { } @@ -1287,13 +1361,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) { } @@ -1314,12 +1388,12 @@ struct CYObject : { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); void Output(CYOutput &out, CYFlags flags) const; }; struct CYMember : - CYExpression + CYTarget { CYExpression *object_; CYExpression *property_; @@ -1344,9 +1418,8 @@ struct CYDirectMember : } CYPrecedence(1) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1359,9 +1432,8 @@ struct CYIndirectMember : } CYPrecedence(1) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1369,12 +1441,12 @@ namespace cy { namespace Syntax { struct New : - CYExpression + CYTarget { CYExpression *constructor_; CYArgument *arguments_; - New(CYExpression *constructor, CYArgument *arguments) : + New(CYExpression *constructor, CYArgument *arguments = NULL) : constructor_(constructor), arguments_(arguments) { @@ -1384,41 +1456,61 @@ struct New : return arguments_ == NULL ? 2 : 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 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 CYRubyBlock : - CYExpression + CYTarget { CYExpression *call_; CYRubyProc *proc_; @@ -1430,10 +1522,11 @@ struct CYRubyBlock : } CYPrecedence(1) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYTarget *AddArgument(CYContext &context, CYExpression *value); }; struct CYIf : @@ -1494,95 +1587,240 @@ struct CYWhile : virtual void Output(CYOutput &out, CYFlags flags) const; }; -// XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass) struct CYFunction { - CYIdentifier *name_; CYFunctionParameter *parameters_; CYStatement *code_; CYNonLocal *nonlocal_; bool implicit_; CYThisScope this_; + CYIdentifier *super_; - CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) : - name_(name), + CYFunction(CYFunctionParameter *parameters, CYStatement *code) : parameters_(parameters), code_(code), nonlocal_(NULL), - implicit_(false) + implicit_(false), + super_(NULL) { } - void Inject(CYContext &context); - virtual void Replace_(CYContext &context, bool outer); - virtual void Output(CYOutput &out, CYFlags flags) const; + void Replace(CYContext &context); + void Output(CYOutput &out) const; }; -// XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression struct CYFunctionExpression : CYFunction, - CYExpression + CYTarget { + CYIdentifier *name_; + CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) : - CYFunction(name, parameters, code) + CYFunction(parameters, code), + name_(name) { } CYPrecedence(0) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + CYTarget *Replace(CYContext &context) override; 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) + CYFunction(parameters, code) { } CYPrecedence(0) - CYRightHand(false) - virtual CYExpression *Replace(CYContext &context); + CYExpression *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; -// XXX: this should derive from CYAnonymousFunctionExpression struct CYRubyProc : - CYFunctionExpression + CYFunction, + CYTarget { CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) : - CYFunctionExpression(NULL, parameters, code) + CYFunction(parameters, code) { } - virtual CYExpression *Replace(CYContext &context); + CYPrecedence(0) + + CYTarget *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; -// XXX: this should derive from CYNamedFunction struct CYFunctionStatement : CYFunction, CYStatement { + CYIdentifier *name_; + CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) : - CYFunction(name, parameters, code) + CYFunction(parameters, code), + name_(name) { } CYCompact(None) - virtual CYStatement *Replace(CYContext &context); + CYStatement *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYExpress : +struct CYPropertyMethod; + +struct CYMethod : + CYFunction, + CYProperty +{ + CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) : + CYFunction(parameters, code), + CYProperty(name, next) + { + } + + virtual CYFunctionExpression *Constructor(); + + using CYProperty::Replace; + virtual void Replace(CYContext &context); +}; + +struct CYPropertyGetter : + CYMethod +{ + CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) : + CYMethod(name, NULL, code, next) + { + } + + virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect); + virtual void Output(CYOutput &out) const; +}; + +struct CYPropertySetter : + CYMethod +{ + CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) : + CYMethod(name, parameters, code, next) + { + } + + virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect); + virtual void Output(CYOutput &out) const; +}; + +struct CYPropertyMethod : + CYMethod +{ + CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) : + CYMethod(name, parameters, code, next) + { + } + + bool Update() const override; + + virtual CYFunctionExpression *Constructor(); + + virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect); + virtual void Output(CYOutput &out) const; +}; + +struct CYClassTail : + CYThing +{ + CYExpression *extends_; + + CYFunctionExpression *constructor_; + CYList instance_; + CYList static_; + + CYClassTail(CYExpression *extends) : + extends_(extends), + constructor_(NULL) + { + } + + void Output(CYOutput &out) const; +}; + +struct CYClassExpression : + CYTarget +{ + CYIdentifier *name_; + CYClassTail *tail_; + + CYClassExpression(CYIdentifier *name, CYClassTail *tail) : + name_(name), + tail_(tail) + { + } + + CYPrecedence(0) + + CYTarget *Replace(CYContext &context) override; + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYClassStatement : CYStatement +{ + CYIdentifier *name_; + CYClassTail *tail_; + + CYClassStatement(CYIdentifier *name, CYClassTail *tail) : + name_(name), + tail_(tail) + { + } + + CYCompact(Long) + + CYStatement *Replace(CYContext &context) override; + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYSuperCall : + CYTarget +{ + CYArgument *arguments_; + + CYSuperCall(CYArgument *arguments) : + arguments_(arguments) + { + } + + CYPrecedence(2) + + CYTarget *Replace(CYContext &context) override; + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYSuperAccess : + CYTarget +{ + CYExpression *property_; + + CYSuperAccess(CYExpression *property) : + property_(property) + { + } + + CYPrecedence(1) + + CYTarget *Replace(CYContext &context) override; + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYExpress : + CYForInitializer { CYExpression *expression_; @@ -1595,7 +1833,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(); @@ -1613,7 +1851,7 @@ struct CYContinue : CYCompact(Short) - virtual CYStatement *Replace(CYContext &context); + CYStatement *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1629,7 +1867,7 @@ struct CYBreak : CYCompact(Short) - virtual CYStatement *Replace(CYContext &context); + CYStatement *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1645,7 +1883,7 @@ struct CYReturn : CYCompact(None) - virtual CYStatement *Replace(CYContext &context); + CYStatement *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1661,7 +1899,7 @@ struct CYYieldGenerator : CYPrecedence(0) - virtual CYExpression *Replace(CYContext &context); + CYExpression *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1682,11 +1920,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; }; @@ -1707,7 +1945,7 @@ struct CYFinally : struct CYTypeSpecifier : CYThing { - virtual CYExpression *Replace(CYContext &context) = 0; + virtual CYTarget *Replace(CYContext &context) = 0; }; struct CYTypeError : @@ -1716,7 +1954,7 @@ struct CYTypeError : CYTypeError() { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; @@ -1726,7 +1964,21 @@ struct CYTypeVoid : CYTypeVoid() { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeReference : + CYTypeSpecifier +{ + CYIdentifier *name_; + + CYTypeReference(CYIdentifier *name) : + name_(name) + { + } + + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; @@ -1745,7 +1997,7 @@ struct CYTypeVariable : { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; @@ -1759,7 +2011,7 @@ struct CYTypeUnsigned : { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; @@ -1773,7 +2025,7 @@ struct CYTypeSigned : { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; @@ -1787,7 +2039,7 @@ struct CYTypeLong : { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; @@ -1801,7 +2053,7 @@ struct CYTypeShort : { } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; @@ -1817,8 +2069,8 @@ 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; @@ -1839,7 +2091,7 @@ struct CYTypeArrayOf : CYPrecedence(1) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); virtual void Output(CYOutput &out, CYIdentifier *identifier) const; }; @@ -1853,7 +2105,7 @@ struct CYTypeConstant : CYPrecedence(0) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); virtual void Output(CYOutput &out, CYIdentifier *identifier) const; }; @@ -1867,7 +2119,7 @@ struct CYTypePointerTo : CYPrecedence(0) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); virtual void Output(CYOutput &out, CYIdentifier *identifier) const; }; @@ -1881,7 +2133,7 @@ struct CYTypeVolatile : CYPrecedence(0) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); virtual void Output(CYOutput &out, CYIdentifier *identifier) const; }; @@ -1914,14 +2166,14 @@ struct CYTypedIdentifier : return this; } - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; CYTypeFunctionWith *Function(); }; struct CYEncodedType : - CYExpression + CYTarget { CYTypedIdentifier *typed_; @@ -1932,7 +2184,7 @@ struct CYEncodedType : CYPrecedence(1) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1956,7 +2208,7 @@ struct CYTypedParameter : }; struct CYLambda : - CYExpression + CYTarget { CYTypedIdentifier *typed_; CYTypedParameter *parameters_; @@ -1971,7 +2223,7 @@ struct CYLambda : CYPrecedence(1) - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -2007,6 +2259,39 @@ struct CYImport : virtual void Output(CYOutput &out, CYFlags flags) const; }; +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 CYExternal : CYStatement { @@ -2025,6 +2310,22 @@ struct CYExternal : virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYTypeExpression : + CYTarget +{ + CYTypedIdentifier *typed_; + + CYTypeExpression(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + CYPrecedence(0) + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYTypeDefinition : CYStatement { @@ -2054,7 +2355,7 @@ struct CYTypeBlockWith : CYPrecedence(0) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); virtual void Output(CYOutput &out, CYIdentifier *identifier) const; }; @@ -2071,12 +2372,72 @@ struct CYTypeFunctionWith : CYPrecedence(1) - virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual CYTarget *Replace_(CYContext &context, CYTarget *type); virtual void Output(CYOutput &out, CYIdentifier *identifier) const; virtual CYTypeFunctionWith *Function() { return this; } }; +struct CYTypeStructField : + CYNext +{ + CYTypedIdentifier *typed_; + + CYTypeStructField(CYTypedIdentifier *typed, CYTypeStructField *next = NULL) : + CYNext(next), + typed_(typed) + { + } +}; + +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; +}; + namespace cy { namespace Syntax { @@ -2221,20 +2582,20 @@ struct CYAddressOf : }; struct CYIndirect : - CYPrefix + CYTarget { + CYExpression *rhs_; + CYIndirect(CYExpression *rhs) : - CYPrefix(rhs) + rhs_(rhs) { } - virtual const char *Operator() const { - return "*"; - } + // XXX: this should be checked + CYPrecedence(2) - CYAlphabetic(false) - - virtual CYExpression *Replace(CYContext &context); + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; }; #define CYReplace \ @@ -2291,7 +2652,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) \ { \ } \