X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/7085e1abd73d7405804870e2404d38c4d22ea73e..0559abf83c4cc9bbd896b69dfaae1e8422521479:/Syntax.hpp diff --git a/Syntax.hpp b/Syntax.hpp index 786b9f7..5599f7e 100644 --- a/Syntax.hpp +++ b/Syntax.hpp @@ -33,6 +33,18 @@ #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); +void CYStringify(std::ostringstream &str, const char *data, size_t size, bool c = false); + +// 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; @@ -199,6 +211,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 @@ -452,11 +471,6 @@ 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; @@ -473,7 +487,6 @@ struct CYNumber; struct CYString; struct CYExpression : - CYForInitializer, CYThing { virtual int Precedence() const = 0; @@ -576,17 +589,17 @@ struct CYParenthetical : void Output(CYOutput &out, CYFlags flags) const; }; -struct CYDeclaration; +struct CYBinding; struct CYFunctionParameter : CYNext, CYThing { - CYDeclaration *initialiser_; + CYBinding *binding_; - CYFunctionParameter(CYDeclaration *initialiser, CYFunctionParameter *next = NULL) : + CYFunctionParameter(CYBinding *binding, CYFunctionParameter *next = NULL) : CYNext(next), - initialiser_(initialiser) + binding_(binding) { } @@ -603,11 +616,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; @@ -617,13 +625,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) { } @@ -635,13 +643,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) { } @@ -895,6 +903,12 @@ struct CYThis : struct CYBoolean : CYTrivial { + CYPrecedence(4) + + virtual bool RightHand() const { + return true; + } + virtual bool Value() const = 0; virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1020,8 +1034,8 @@ struct CYAssignment : { } - void SetLeft(CYTarget *lhs) { - lhs_ = lhs; + void SetRight(CYExpression *rhs) { + rhs_ = rhs; } virtual const char *Operator() const = 0; @@ -1061,11 +1075,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) { } @@ -1088,7 +1102,7 @@ struct CYElementValue : { CYExpression *value_; - CYElementValue(CYExpression *value, CYElement *next) : + CYElementValue(CYExpression *value, CYElement *next = NULL) : CYNext(next), value_(value) { @@ -1134,13 +1148,13 @@ struct CYArray : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYDeclaration { +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) { } @@ -1154,11 +1168,11 @@ struct CYForLexical : CYForInInitializer { bool constant_; - CYDeclaration *declaration_; + CYBinding *binding_; - CYForLexical(bool constant, CYDeclaration *declaration) : + CYForLexical(bool constant, CYBinding *binding) : constant_(constant), - declaration_(declaration) + binding_(binding) { } @@ -1171,10 +1185,10 @@ struct CYForLexical : struct CYForVariable : CYForInInitializer { - CYDeclaration *declaration_; + CYBinding *binding_; - CYForVariable(CYDeclaration *declaration) : - declaration_(declaration) + CYForVariable(CYBinding *binding) : + binding_(binding) { } @@ -1184,15 +1198,15 @@ struct CYForVariable : 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) { } @@ -1205,56 +1219,42 @@ struct CYDeclarations : 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 + 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 { bool constant_; - CYDeclarations *declarations_; + CYBindings *bindings_; - CYLet(bool constant, CYDeclarations *declarations) : + CYLexical(bool constant, CYBindings *bindings) : constant_(constant), - declarations_(declarations) + 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 { @@ -1274,6 +1274,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); @@ -1302,13 +1304,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) @@ -1324,13 +1326,33 @@ struct CYFor : struct CYForIn : CYStatement { - CYForInInitializer *initialiser_; - CYExpression *set_; + CYForInInitializer *initializer_; + CYExpression *iterable_; + CYStatement *code_; + + 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_; - CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) : - initialiser_(initialiser), - set_(set), + CYForInitialized(CYBinding *binding, CYExpression *iterable, CYStatement *code) : + binding_(binding), + iterable_(iterable), code_(code) { } @@ -1344,13 +1366,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) { } @@ -1708,6 +1730,8 @@ struct CYPropertyMethod : { } + bool Update() const override; + virtual CYFunctionExpression *Constructor(); virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect); @@ -1801,7 +1825,7 @@ struct CYSuperAccess : }; struct CYExpress : - CYStatement + CYForInitializer { CYExpression *expression_; @@ -1814,7 +1838,7 @@ struct CYExpress : CYCompact(None) - CYStatement *Replace(CYContext &context) override; + CYForInitializer *Replace(CYContext &context) override; virtual void Output(CYOutput &out, CYFlags flags) const; virtual CYStatement *Return(); @@ -1901,11 +1925,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; }; @@ -1939,70 +1963,87 @@ struct CYTypeError : 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 CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; -struct CYTypeVariable : +struct CYTypeIntegral : CYTypeSpecifier { - CYIdentifier *name_; + CYTypeSigning signing_; + int length_; - CYTypeVariable(CYIdentifier *name) : - name_(name) + CYTypeIntegral(CYTypeSigning signing, int length = 1) : + signing_(signing), + length_(length) { } - CYTypeVariable(const char *name) : - name_(new($pool) CYIdentifier(name)) - { + CYTypeIntegral *Long() { + if (length_ != 1 && length_ != 2) + return NULL; + ++length_; + return this; } - virtual CYTarget *Replace(CYContext &context); - virtual void Output(CYOutput &out) const; -}; + CYTypeIntegral *Short() { + if (length_ != 1) + return NULL; + --length_; + return this; + } -struct CYTypeUnsigned : - CYTypeSpecifier -{ - CYTypeSpecifier *specifier_; + CYTypeIntegral *Signed() { + if (signing_ != CYTypeNeutral) + return NULL; + signing_ = CYTypeSigned; + return this; + } - CYTypeUnsigned(CYTypeSpecifier *specifier) : - specifier_(specifier) - { + CYTypeIntegral *Unsigned() { + if (signing_ != CYTypeNeutral) + return NULL; + signing_ = CYTypeUnsigned; + return this; } 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 CYTarget *Replace(CYContext &context); virtual void Output(CYOutput &out) const; }; -struct CYTypeLong : +struct CYTypeReference : CYTypeSpecifier { - CYTypeSpecifier *specifier_; + CYIdentifier *name_; - CYTypeLong(CYTypeSpecifier *specifier) : - specifier_(specifier) + CYTypeReference(CYIdentifier *name) : + name_(name) { } @@ -2010,13 +2051,18 @@ struct CYTypeLong : 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)) { } @@ -2226,6 +2272,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 { @@ -2244,6 +2323,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 { @@ -2296,6 +2391,66 @@ struct CYTypeFunctionWith : 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 {