X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/a86e34d01f829a8b4f58f6066d78148c0901c1dd..9674674739f8ab3c2b81879f36fdb7fcf294cb5e:/Parser.hpp diff --git a/Parser.hpp b/Parser.hpp index 008be05..fab18af 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1,47 +1,26 @@ -/* Cycript - Inlining/Optimizing JavaScript Compiler - * Copyright (C) 2009 Jay Freeman (saurik) +/* Cycript - Optimizing JavaScript Compiler/Runtime + * Copyright (C) 2009-2013 Jay Freeman (saurik) */ -/* Modified BSD License {{{ */ +/* GNU General Public License, Version 3 {{{ */ /* - * Redistribution and use in source and binary - * forms, with or without modification, are permitted - * provided that the following conditions are met: + * Cycript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. * - * 1. Redistributions of source code must retain the - * above copyright notice, this list of conditions - * and the following disclaimer. - * 2. Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions - * and the following disclaimer in the documentation - * and/or other materials provided with the - * distribution. - * 3. The name of the author may not be used to endorse - * or promote products derived from this software - * without specific prior written permission. + * Cycript is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + * You should have received a copy of the GNU General Public License + * along with Cycript. If not, see . +**/ /* }}} */ -#ifndef CYPARSER_HPP -#define CYPARSER_HPP - -// XXX: wtf is this here?! -#define CYPA 16 +#ifndef CYCRIPT_PARSER_HPP +#define CYCRIPT_PARSER_HPP #include @@ -50,32 +29,14 @@ #include #include +#include #include -#include "location.hh" +#include "List.hpp" #include "Pooling.hpp" #include "Options.hpp" -class CYContext; - -template -struct CYNext { - Type_ *next_; - - CYNext() : - next_(NULL) - { - } - - CYNext(Type_ *next) : - next_(next) - { - } - - void SetNext(Type_ *next) { - next_ = next; - } -}; +struct CYContext; struct CYThing { virtual ~CYThing() { @@ -135,6 +96,7 @@ struct CYPropertyName { }; struct CYExpression; +struct CYAssignment; enum CYNeeded { CYNever = -1, @@ -154,6 +116,34 @@ enum CYFlags { CYNoBF = (CYNoBrace | CYNoFunction), }; +_finline CYFlags operator ~(CYFlags rhs) { + return static_cast(~static_cast(rhs)); +} + +_finline CYFlags operator &(CYFlags lhs, CYFlags rhs) { + return static_cast(static_cast(lhs) & static_cast(rhs)); +} + +_finline CYFlags operator |(CYFlags lhs, CYFlags rhs) { + return static_cast(static_cast(lhs) | static_cast(rhs)); +} + +_finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) { + return lhs = lhs | rhs; +} + +_finline CYFlags CYLeft(CYFlags flags) { + return flags & ~(CYNoDangle | CYNoInteger); +} + +_finline CYFlags CYRight(CYFlags flags) { + return flags & ~CYNoBF; +} + +_finline CYFlags CYCenter(CYFlags flags) { + return CYLeft(CYRight(flags)); +} + struct CYStatement : CYNext { @@ -163,9 +153,6 @@ struct CYStatement : void Single(CYOutput &out, CYFlags flags) const; void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const; - CYStatement *ReplaceAll(CYContext &context); - virtual CYStatement *Collapse(CYContext &context); - virtual CYStatement *Replace(CYContext &context) = 0; private: @@ -243,10 +230,14 @@ struct CYIdentifier : CYWord { CYIdentifier *replace_; + size_t offset_; + size_t usage_; CYIdentifier(const char *word) : CYWord(word), - replace_(NULL) + replace_(NULL), + offset_(0), + usage_(0) { } @@ -305,40 +296,46 @@ enum CYIdentifierFlags { CYIdentifierVariable, CYIdentifierOther, CYIdentifierMagic, + CYIdentifierCatch, }; typedef std::set CYCStringSet; typedef std::set CYIdentifierValueSet; -typedef std::set CYIdentifierAddressSet; -typedef std::vector CYIdentifierAddressVector; typedef std::map CYIdentifierAddressFlagsMap; +struct CYIdentifierUsage { + CYIdentifier *identifier_; + size_t usage_; +}; + +typedef std::vector CYIdentifierUsageVector; + struct CYScope { + bool transparent_; + + CYContext &context_; + CYStatement *&statements_; + CYScope *parent_; - CYIdentifierAddressFlagsMap internal_; + CYIdentifierAddressFlagsMap internal_; CYIdentifierValueSet identifiers_; - size_t offset_; - CYScope() : - parent_(NULL), - offset_(0) - { - } + CYScope(bool transparent, CYContext &context, CYStatement *&statements); + virtual ~CYScope(); + + void Close(); void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags); virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier); - void Merge(CYContext &context, CYIdentifierAddressVector &external); + void Merge(CYContext &context, CYIdentifier *identifier); void Scope(CYContext &context, CYStatement *&statements); }; struct CYProgram : - CYScope, CYThing { CYStatement *statements_; - CYIdentifierAddressVector rename_; - CYCStringSet external_; CYProgram(CYStatement *statements) : statements_(statements) @@ -349,98 +346,126 @@ struct CYProgram : virtual void Output(CYOutput &out) const; }; -struct CYBlock : - CYStatement, - CYThing -{ - CYStatement *statements_; +struct CYNonLocal; +struct CYThisScope; + +struct CYContext { + CYOptions &options_; + CYScope *scope_; + CYThisScope *this_; + + CYIdentifierUsageVector rename_; - CYBlock(CYStatement *statements, CYScope *scope = NULL) : - statements_(statements), - scope_(scope) + CYNonLocal *nonlocal_; + CYNonLocal *nextlocal_; + unsigned unique_; + + CYContext(CYOptions &options) : + options_(options), + scope_(NULL), + this_(NULL), + nonlocal_(NULL), + nextlocal_(NULL), + unique_(0) { } - operator CYStatement *() const { - return statements_; + virtual ~CYContext() { } - void AddPrev(CYStatement *statement) { - CYStatement *last(statement); - while (last->next_ != NULL) - last = last->next_; - last->SetNext(statements_); - statements_ = statement; + template + void ReplaceAll(Type_ *&values) { + Type_ **last(&values); + CYForEach (next, values) { + Replace(*last = next); + if (*last != NULL) + last = &(*last)->next_; + } } - virtual CYStatement *Replace(CYContext &context); - - virtual void Output(CYOutput &out) const; - virtual void Output(CYOutput &out, CYFlags flags) const; -}; + template + void Replace(Type_ *&value) { + for (;;) if (value == NULL) + break; + else { + Type_ *replace(value->Replace(*this)); + if (replace != value) + value = replace; + else break; + } + } -enum CYState { - CYClear, - CYRestricted, - CYNewLine + void NonLocal(CYStatement *&statements); + CYIdentifier *Unique(); }; -class CYDriver { - public: - CYPool pool_; - - CYState state_; - void *scanner_; - - const char *data_; - size_t size_; - FILE *file_; +struct CYNonLocal { + CYIdentifier *identifier_; - bool strict_; + CYNonLocal() : + identifier_(NULL) + { + } - enum Condition { - RegExpCondition, - XMLContentCondition, - XMLTagCondition, - }; + CYIdentifier *Target(CYContext &context) { + if (identifier_ == NULL) + identifier_ = context.Unique(); + return identifier_; + } +}; - std::string filename_; +struct CYThisScope : + CYNext +{ + CYIdentifier *identifier_; - struct Error { - bool warning_; - cy::location location_; - std::string message_; - }; + CYThisScope() : + identifier_(NULL) + { + } - typedef std::vector Errors; + CYIdentifier *Identifier(CYContext &context) { + if (next_ != NULL) + return next_->Identifier(context); + if (identifier_ == NULL) + identifier_ = context.Unique(); + return identifier_; + } +}; - CYProgram *program_; - Errors errors_; +struct CYBlock : + CYStatement, + CYThing +{ + CYStatement *statements_; - private: - void ScannerInit(); - void ScannerDestroy(); + CYBlock(CYStatement *statements) : + statements_(statements) + { + } - public: - CYDriver(const std::string &filename); - ~CYDriver(); + operator CYStatement *() const { + return statements_; + } - Condition GetCondition(); - void SetCondition(Condition condition); + void AddPrev(CYStatement *statement) { + CYSetLast(statement) = statements_; + statements_ = statement; + } - void PushCondition(Condition condition); - void PopCondition(); + virtual CYStatement *Replace(CYContext &context); - void Warning(const cy::location &location, const char *message); + virtual void Output(CYOutput &out) const; + virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYForInitialiser { virtual ~CYForInitialiser() { } - virtual void For(CYOutput &out) const = 0; virtual CYExpression *Replace(CYContext &context) = 0; + virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; struct CYForInInitialiser { @@ -448,46 +473,46 @@ struct CYForInInitialiser { } virtual void ForIn(CYOutput &out, CYFlags flags) const = 0; - virtual const char *ForEachIn() const = 0; - virtual CYExpression *ForEachIn(CYContext &out) = 0; + virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0; + virtual CYExpression *Replace(CYContext &context) = 0; + virtual CYAssignment *Assignment(CYContext &context) = 0; + + virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; struct CYNumber; struct CYString; struct CYExpression : - CYNext, CYForInitialiser, CYForInInitialiser, CYClassName, CYThing { - virtual unsigned Precedence() const = 0; + virtual int Precedence() const = 0; virtual bool RightHand() const { return true; } - virtual void For(CYOutput &out) const; virtual void ForIn(CYOutput &out, CYFlags flags) const; + virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); - virtual const char *ForEachIn() const; - virtual CYExpression *ForEachIn(CYContext &out); + virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const = 0; - void Output(CYOutput &out, unsigned precedence, CYFlags flags) const; + void Output(CYOutput &out, int precedence, CYFlags flags) const; virtual CYExpression *ClassName(CYContext &context, bool object); virtual void ClassName(CYOutput &out, bool object) const; - CYExpression *ReplaceAll(CYContext &context); - virtual CYExpression *Replace(CYContext &context) = 0; + virtual CYAssignment *Assignment(CYContext &context); virtual CYExpression *Primitive(CYContext &context) { - return this; + return NULL; } virtual CYNumber *Number(CYContext &context) { @@ -509,8 +534,9 @@ struct CYExpression : } #define CYPrecedence(value) \ - virtual unsigned Precedence() const { \ - return value; \ + static const int Precedence_ = value; \ + virtual int Precedence() const { \ + return Precedence_; \ } #define CYRightHand(value) \ @@ -521,47 +547,58 @@ struct CYExpression : struct CYCompound : CYExpression { - CYExpression *expressions_; + CYExpression *expression_; + CYExpression *next_; - CYCompound(CYExpression *expressions = NULL) : - expressions_(expressions) + CYCompound(CYExpression *expression, CYExpression *next = NULL) : + expression_(expression), + next_(next) { - } - - void AddPrev(CYExpression *expression) { - CYExpression *last(expression); - while (last->next_ != NULL) - last = last->next_; - last->SetNext(expressions_); - expressions_ = expression; + if (expression_ == NULL) + throw; + _assert(expression_ != NULL); } CYPrecedence(17) virtual CYExpression *Replace(CYContext &context); void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *Primitive(CYContext &context); }; +struct CYDeclaration; + struct CYFunctionParameter : CYNext, CYThing { - CYIdentifier *name_; + CYForInInitialiser *initialiser_; - CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) : + CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) : CYNext(next), - name_(name) + initialiser_(initialiser) { } - void Replace(CYContext &context); - virtual void Output(CYOutput &out) const; + void Replace(CYContext &context, CYBlock &code); + void Output(CYOutput &out) const; }; struct CYComprehension : CYNext, CYThing { + CYComprehension(CYComprehension *next = NULL) : + CYNext(next) + { + } + + CYComprehension *Modify(CYComprehension *next) { + next_ = next; + return this; + } + virtual const char *Name() const = 0; virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; @@ -576,7 +613,8 @@ struct CYForInComprehension : CYIdentifier *name_; CYExpression *set_; - CYForInComprehension(CYIdentifier *name, CYExpression *set) : + CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) : + CYComprehension(next), name_(name), set_(set) { @@ -591,13 +629,14 @@ struct CYForInComprehension : virtual void Output(CYOutput &out) const; }; -struct CYForEachInComprehension : +struct CYForOfComprehension : CYComprehension { CYIdentifier *name_; CYExpression *set_; - CYForEachInComprehension(CYIdentifier *name, CYExpression *set) : + CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) : + CYComprehension(next), name_(name), set_(set) { @@ -617,7 +656,8 @@ struct CYIfComprehension : { CYExpression *test_; - CYIfComprehension(CYExpression *test) : + CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) : + CYComprehension(next), test_(test) { } @@ -654,6 +694,10 @@ struct CYLiteral : { CYPrecedence(0) CYRightHand(false) + + virtual CYExpression *Primitive(CYContext &context) { + return this; + } }; struct CYTrivial : @@ -856,6 +900,11 @@ struct CYVariable : { } + CYVariable(const char *name) : + name_(new($pool) CYIdentifier(name)) + { + } + CYPrecedence(0) CYRightHand(false) @@ -968,7 +1017,7 @@ struct CYArgument : { } - void Replace(CYContext &context); + CYArgument *Replace(CYContext &context); void Output(CYOutput &out) const; }; @@ -1059,20 +1108,19 @@ struct CYDeclaration : } virtual void ForIn(CYOutput &out, CYFlags flags) const; - - virtual const char *ForEachIn() const; - virtual CYExpression *ForEachIn(CYContext &out); + virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); virtual CYExpression *Replace(CYContext &context); + virtual CYAssignment *Assignment(CYContext &context); + CYVariable *Variable(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYDeclarations : CYNext, - CYThing, - CYForInitialiser + CYThing { CYDeclaration *declaration_; @@ -1082,15 +1130,31 @@ struct CYDeclarations : { } - virtual void For(CYOutput &out) const; + void Replace(CYContext &context); - virtual CYCompound *Replace(CYContext &context); + CYCompound *Compound(CYContext &context); CYProperty *Property(CYContext &context); + CYArgument *Argument(CYContext &context); + CYFunctionParameter *Parameter(CYContext &context); virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYForDeclarations : + CYForInitialiser +{ + CYDeclarations *declarations_; + + CYForDeclarations(CYDeclarations *declarations) : + declarations_(declarations) + { + } + + virtual CYCompound *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYVar : CYStatement { @@ -1105,15 +1169,15 @@ struct CYVar : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYLet : +struct CYLetStatement : CYStatement { CYDeclarations *declarations_; - CYBlock code_; + CYStatement *code_; - CYLet(CYDeclarations *declarations, CYStatement *statements) : + CYLetStatement(CYDeclarations *declarations, CYStatement *code) : declarations_(declarations), - code_(statements) + code_(code) { } @@ -1159,14 +1223,14 @@ struct CYForIn : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYForEachIn : +struct CYForOf : CYStatement { CYForInInitialiser *initialiser_; CYExpression *set_; CYStatement *code_; - CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : + CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : initialiser_(initialiser), set_(set), code_(code) @@ -1182,7 +1246,7 @@ struct CYObject : { CYProperty *properties_; - CYObject(CYProperty *properties) : + CYObject(CYProperty *properties = NULL) : properties_(properties) { } @@ -1206,8 +1270,6 @@ struct CYMember : void SetLeft(CYExpression *object) { object_ = object; } - - void Replace_(CYContext &context); }; struct CYDirectMember : @@ -1240,19 +1302,22 @@ struct CYIndirectMember : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYNew : +namespace cy { +namespace Syntax { + +struct New : CYExpression { CYExpression *constructor_; CYArgument *arguments_; - CYNew(CYExpression *constructor, CYArgument *arguments) : + New(CYExpression *constructor, CYArgument *arguments) : constructor_(constructor), arguments_(arguments) { } - virtual unsigned Precedence() const { + virtual int Precedence() const { return arguments_ == NULL ? 2 : 1; } @@ -1260,8 +1325,12 @@ struct CYNew : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); }; +} } + struct CYCall : CYExpression { @@ -1279,6 +1348,29 @@ struct CYCall : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); +}; + +struct CYRubyProc; + +struct CYRubyBlock : + CYExpression +{ + CYExpression *call_; + CYRubyProc *proc_; + + CYRubyBlock(CYExpression *call, CYRubyProc *proc) : + call_(call), + proc_(proc) + { + } + + CYPrecedence(1) + CYRightHand(false) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYIf : @@ -1331,17 +1423,20 @@ struct CYWhile : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYFunction : - CYScope -{ +// XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass) +struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; CYBlock code_; + CYNonLocal *nonlocal_; + CYThisScope this_; + CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : name_(name), parameters_(parameters), - code_(statements, this) + code_(statements), + nonlocal_(NULL) { } @@ -1353,6 +1448,7 @@ struct CYFunction : virtual void Output(CYOutput &out, CYFlags flags) const; }; +// XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression struct CYFunctionExpression : CYFunction, CYExpression @@ -1369,6 +1465,37 @@ struct CYFunctionExpression : virtual void Output(CYOutput &out, CYFlags flags) const; }; +// XXX: this should derive from CYAnonymousFunction +struct CYFatArrow : + CYFunction, + CYExpression +{ + CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) : + CYFunction(NULL, parameters, statements) + { + } + + CYPrecedence(0) + CYRightHand(false) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +// XXX: this should derive from CYAnonymousFunctionExpression +struct CYRubyProc : + CYFunctionExpression +{ + CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) : + CYFunctionExpression(NULL, parameters, statements) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +// XXX: this should derive from CYNamedFunction struct CYFunctionStatement : CYFunction, CYStatement @@ -1390,11 +1517,10 @@ struct CYExpress : CYExpress(CYExpression *expression) : expression_(expression) { - if (expression == NULL) + if (expression_ == NULL) throw; } - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1444,7 +1570,6 @@ struct CYReturn : struct CYEmpty : CYStatement { - virtual CYStatement *Collapse(CYContext &context); virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1463,6 +1588,317 @@ struct CYFinally : virtual void Output(CYOutput &out) const; }; +struct CYTypeSpecifier : + CYThing +{ + virtual CYExpression *Replace(CYContext &context) = 0; +}; + +struct CYTypeError : + CYTypeSpecifier +{ + CYTypeError() { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeVoid : + CYTypeSpecifier +{ + CYTypeVoid() { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeVariable : + CYTypeSpecifier +{ + CYIdentifier *name_; + + CYTypeVariable(CYIdentifier *name) : + name_(name) + { + } + + CYTypeVariable(const char *name) : + name_(new($pool) CYIdentifier(name)) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeUnsigned : + CYTypeSpecifier +{ + CYTypeSpecifier *specifier_; + + CYTypeUnsigned(CYTypeSpecifier *specifier) : + specifier_(specifier) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeSigned : + CYTypeSpecifier +{ + CYTypeSpecifier *specifier_; + + CYTypeSigned(CYTypeSpecifier *specifier) : + specifier_(specifier) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeLong : + CYTypeSpecifier +{ + CYTypeSpecifier *specifier_; + + CYTypeLong(CYTypeSpecifier *specifier) : + specifier_(specifier) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeShort : + CYTypeSpecifier +{ + CYTypeSpecifier *specifier_; + + CYTypeShort(CYTypeSpecifier *specifier) : + specifier_(specifier) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYTypeModifier : + CYNext +{ + CYTypeModifier(CYTypeModifier *next) : + CYNext(next) + { + } + + virtual int Precedence() const = 0; + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0; + CYExpression *Replace(CYContext &context, CYExpression *type); + + virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0; + void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const; +}; + +struct CYTypeArrayOf : + CYTypeModifier +{ + CYExpression *size_; + + CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + size_(size) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypeConstant : + CYTypeModifier +{ + CYTypeConstant(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypePointerTo : + CYTypeModifier +{ + CYTypePointerTo(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypeVolatile : + CYTypeModifier +{ + CYTypeVolatile(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypedIdentifier : + CYNext, + CYThing +{ + CYIdentifier *identifier_; + CYTypeSpecifier *specifier_; + CYTypeModifier *modifier_; + + CYTypedIdentifier(CYIdentifier *identifier = NULL) : + identifier_(identifier), + specifier_(NULL), + modifier_(NULL) + { + } + + CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) : + identifier_(NULL), + specifier_(specifier), + modifier_(modifier) + { + } + + inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) { + CYSetLast(modifier_) = modifier; + return this; + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + +struct CYEncodedType : + CYExpression +{ + CYTypedIdentifier *typed_; + + CYEncodedType(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypedParameter : + CYNext, + CYThing +{ + CYTypedIdentifier *typed_; + + CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) : + CYNext(next), + typed_(typed) + { + } + + CYArgument *Argument(CYContext &context); + CYFunctionParameter *Parameters(CYContext &context); + CYExpression *TypeSignature(CYContext &context, CYExpression *prefix); + + virtual void Output(CYOutput &out) const; +}; + +struct CYLambda : + CYExpression +{ + CYTypedIdentifier *typed_; + CYTypedParameter *parameters_; + CYStatement *statements_; + + CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) : + typed_(typed), + parameters_(parameters), + statements_(statements) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypeDefinition : + CYStatement +{ + CYTypedIdentifier *typed_; + + CYTypeDefinition(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYTypeBlockWith : + CYTypeModifier +{ + CYTypedParameter *parameters_; + + CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + parameters_(parameters) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + +struct CYTypeFunctionWith : + CYTypeModifier +{ + CYTypedParameter *parameters_; + + CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + parameters_(parameters) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace_(CYContext &context, CYExpression *type); + virtual void Output(CYOutput &out, CYIdentifier *identifier) const; +}; + namespace cy { namespace Syntax { @@ -1505,7 +1941,7 @@ struct Throw : { CYExpression *value_; - Throw(CYExpression *value) : + Throw(CYExpression *value = NULL) : value_(value) { } @@ -1548,6 +1984,17 @@ struct CYSwitch : virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYDebugger : + CYStatement +{ + CYDebugger() + { + } + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYCondition : CYExpression { @@ -1679,7 +2126,7 @@ CYPrefix_(false, "-", Negate) CYPrefix_(false, "~", BitwiseNot) CYPrefix_(false, "!", LogicalNot) -CYInfix_(false, 5, "*", Multiply) +CYInfix_(false, 5, "*", Multiply, CYReplace) CYInfix_(false, 5, "/", Divide) CYInfix_(false, 5, "%", Modulus) CYInfix_(false, 6, "+", Add, CYReplace) @@ -1716,4 +2163,4 @@ CYAssignment_("&=", BitwiseAnd) CYAssignment_("^=", BitwiseXOr) CYAssignment_("|=", BitwiseOr) -#endif/*CYPARSER_HPP*/ +#endif/*CYCRIPT_PARSER_HPP*/