From bfd79faeaedfb0028661b7ad25a496def9d1f454 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sat, 12 Dec 2015 06:13:05 -0800 Subject: [PATCH] Extract ForInitializer as a subclass of Statement. --- Output.cpp | 5 ----- Parser.ypp.in | 31 +++++++++++++++++++++++-------- Replace.cpp | 12 ++++-------- Syntax.hpp | 43 +++++++++++++++---------------------------- 4 files changed, 42 insertions(+), 49 deletions(-) diff --git a/Output.cpp b/Output.cpp index 5244b5b..4506db6 100644 --- a/Output.cpp +++ b/Output.cpp @@ -396,11 +396,6 @@ void CYFor::Output(CYOutput &out, CYFlags flags) const { code_->Single(out, CYRight(flags), CYCompactShort); } -void CYForDeclarations::Output(CYOutput &out, CYFlags flags) const { - out << "var" << ' '; - declarations_->Output(out, CYRight(flags)); -} - void CYForLexical::Output(CYOutput &out, CYFlags flags) const { out << (constant_ ? "const" : "let") << ' '; declaration_->Output(out, CYRight(flags)); diff --git a/Parser.ypp.in b/Parser.ypp.in index ae85290..9797492 100644 --- a/Parser.ypp.in +++ b/Parser.ypp.in @@ -459,10 +459,11 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY %type ElementList %type ElementListOpt %type ElseStatementOpt -%type EmptyStatement +%type EmptyStatement %type EqualityExpression %type Expression %type ExpressionOpt +%type ExpressionStatement_ %type ExpressionStatement %type Finally %type ForBinding @@ -496,6 +497,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY %type LeftHandSideExpression %type LetOrConst %type LexicalBinding +%type LexicalDeclaration_ %type LexicalDeclaration %type Literal %type LiteralPropertyName @@ -548,6 +550,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY %type VariableDeclaration %type VariableDeclarationList_ %type VariableDeclarationList +%type VariableStatement_ %type VariableStatement %type WithStatement %type Word @@ -1275,8 +1278,12 @@ StatementListItem ; /* }}} */ /* 13.3 Let and Const Declarations {{{ */ +LexicalDeclaration_ + : LetOrConst BindingList { $$ = CYNew CYLet($1, $2); } + ; + LexicalDeclaration - : LetOrConst BindingList Terminator { $$ = CYNew CYLet($1, $2); } + : LexicalDeclaration_ Terminator { $$ = $1; } ; LetOrConst @@ -1299,8 +1306,12 @@ LexicalBinding ; /* }}} */ /* 13.3.2 Variable Statement {{{ */ +VariableStatement_ + : Var_ VariableDeclarationList { $$ = CYNew CYVar($2); } + ; + VariableStatement - : Var_ VariableDeclarationList Terminator { $$ = CYNew CYVar($2); } + : VariableStatement_ Terminator { $$ = $1; } ; VariableDeclarationList_ @@ -1369,8 +1380,11 @@ EmptyStatement ; /* }}} */ /* 13.5 Expression Statement {{{ */ +ExpressionStatement_ + : Expression { $$ = CYNew CYExpress($1); } + ExpressionStatement - : Expression Terminator { $$ = CYNew CYExpress($1); } + : ExpressionStatement_ Terminator { $$ = $1; } ; /* }}} */ /* 13.6 The if Statement {{{ */ @@ -1387,15 +1401,16 @@ IfStatement IterationStatement : "do" Statement "while" "(" Expression ")" TerminatorOpt { $$ = CYNew CYDoWhile($5, $2); } | "while" "(" Expression ")" Statement { $$ = CYNew CYWhile($3, $5); } - | "for" "(" LexPushInOn ForStatementInitializer ";" LexPopIn ExpressionOpt ";" ExpressionOpt ")" Statement { $$ = CYNew CYFor($4, $7, $9, $11); } + | "for" "(" LexPushInOn ForStatementInitializer LexPopIn ExpressionOpt ";" ExpressionOpt ")" Statement { $$ = CYNew CYFor($4, $6, $8, $10); } | "for" "(" LexPushInOn ForInStatementInitializer "!in" LexPopIn Expression ")" Statement { $$ = CYNew CYForIn($4, $7, $9); } | "for" "(" LexPushInOn ForInStatementInitializer "of" LexPopIn AssignmentExpression ")" Statement { $$ = CYNew CYForOf($4, $7, $9); } ; ForStatementInitializer - : ExpressionOpt { $$ = $1; } - | LexSetRegExp Var_ VariableDeclarationList { $$ = CYNew CYForDeclarations($3); } - | LexSetRegExp LexicalDeclaration { CYNOT(@$); } + : LexSetRegExp EmptyStatement { $$ = $2; } + | ExpressionStatement_ ";" { $$ = $1; } + | LexSetRegExp VariableStatement_ ";" { $$ = $2; } + | LexSetRegExp LexicalDeclaration_ ";" { $$ = $2; } ; ForInStatementInitializer diff --git a/Replace.cpp b/Replace.cpp index d1bc1e9..cab4cdd 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -351,7 +351,7 @@ void CYElementValue::Replace(CYContext &context) { next_->Replace(context); } -CYStatement *CYEmpty::Replace(CYContext &context) { +CYForInitializer *CYEmpty::Replace(CYContext &context) { return NULL; } @@ -370,7 +370,7 @@ CYStatement *CYExpress::Return() { return $ CYReturn(expression_); } -CYStatement *CYExpress::Replace(CYContext &context) { +CYForInitializer *CYExpress::Replace(CYContext &context) { context.Replace(expression_); return this; } @@ -425,10 +425,6 @@ CYStatement *CYFor::Replace(CYContext &context) { return this; } -CYExpression *CYForDeclarations::Replace(CYContext &context) { - return declarations_->Replace(context, CYIdentifierVariable); -} - CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) { if (value == NULL) { if (declaration_->initialiser_ == NULL) @@ -644,7 +640,7 @@ CYTarget *CYLambda::Replace(CYContext &context) { return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context))); } -CYStatement *CYLet::Replace(CYContext &context) { +CYForInitializer *CYLet::Replace(CYContext &context) { if (CYExpression *expression = declarations_->Replace(context, CYIdentifierLexical)) return $E(expression); return $ CYEmpty(); @@ -1224,7 +1220,7 @@ CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression * return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context))); } -CYStatement *CYVar::Replace(CYContext &context) { +CYForInitializer *CYVar::Replace(CYContext &context) { if (CYExpression *expression = declarations_->Replace(context, CYIdentifierVariable)) return $E(expression); return $ CYEmpty(); diff --git a/Syntax.hpp b/Syntax.hpp index 786b9f7..db2b816 100644 --- a/Syntax.hpp +++ b/Syntax.hpp @@ -199,6 +199,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 +459,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 +475,6 @@ struct CYNumber; struct CYString; struct CYExpression : - CYForInitializer, CYThing { virtual int Precedence() const = 0; @@ -1205,22 +1206,8 @@ 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_; @@ -1231,12 +1218,12 @@ struct CYVar : CYCompact(None) - virtual CYStatement *Replace(CYContext &context); + virtual CYForInitializer *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYLet : - CYStatement + CYForInitializer { bool constant_; CYDeclarations *declarations_; @@ -1249,7 +1236,7 @@ struct CYLet : CYCompact(None) - virtual CYStatement *Replace(CYContext &context); + virtual CYForInitializer *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -1801,7 +1788,7 @@ struct CYSuperAccess : }; struct CYExpress : - CYStatement + CYForInitializer { CYExpression *expression_; @@ -1814,7 +1801,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 +1888,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; }; -- 2.45.2