From: Jay Freeman (saurik) Date: Tue, 5 Jun 2012 09:15:05 +0000 (-0700) Subject: Use CYForDeclarations as indirection to finish CYLet. X-Git-Tag: v0.9.456~33 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/15b88a33189cbd3da91900194d8e5c81252f8a4d?ds=inline Use CYForDeclarations as indirection to finish CYLet. --- diff --git a/Cycript.yy.in b/Cycript.yy.in index 9c05469..56b023a 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -396,7 +396,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type LabelledStatement %type LeftHandSideExpression %type LeftHandSideExpressionNoBF -//%type LetStatement +%type LetStatement %type Literal %type LiteralNoRE %type LiteralRE @@ -1379,7 +1379,7 @@ ForStatement ForStatementInitialiser : ExpressionNoInOpt { $$ = $1; } - | LexSetRegExp "var" VariableDeclarationListNoIn { $$ = $3; } + | LexSetRegExp "var" VariableDeclarationListNoIn { $$ = CYNew CYForDeclarations($3); } ; /* }}} */ /* 12.6.4 The for-in Statement {{{ */ @@ -1914,15 +1914,15 @@ ForInStatement : "for" "each" "(" ForInStatementInitialiser "in" Expression ")" Statement { $$ = CYNew CYForEachIn($4, $6, $8); } ; /* }}} */ -/* JavaScript 1.7: let Statements {{{ *//* +/* JavaScript 1.7: let Statements {{{ */ LetStatement - : "let" "(" VariableDeclarationList ")" Block_ { $$ = CYNew CYLet($3, $5); } + : "let" "(" VariableDeclarationList ")" Statement { $$ = CYNew CYLet($3, $5); } ; Statement_ : LetStatement ; -*//* }}} */ +/* }}} */ /* JavaScript FTW: Function Statements {{{ */ Statement diff --git a/Output.cpp b/Output.cpp index 122f6c7..cd04e2e 100644 --- a/Output.cpp +++ b/Output.cpp @@ -250,9 +250,9 @@ void CYDeclaration::Output(CYOutput &out, CYFlags flags) const { } } -void CYDeclarations::For(CYOutput &out) const { +void CYForDeclarations::Output(CYOutput &out, CYFlags flags) const { out << "var"; - Output(out, CYNoIn); + Output(out, CYRight(flags)); } void CYDeclarations::Output(CYOutput &out) const { @@ -314,10 +314,6 @@ void CYExpression::ClassName(CYOutput &out, bool object) const { Output(out, CYAssign::Precedence_, CYNoFlags); } -void CYExpression::For(CYOutput &out) const { - Output(out, CYNoIn); -} - void CYExpression::ForIn(CYOutput &out, CYFlags flags) const { Output(out, flags | CYNoRightHand); } @@ -340,7 +336,7 @@ void CYFinally::Output(CYOutput &out) const { void CYFor::Output(CYOutput &out, CYFlags flags) const { out << "for" << ' ' << '('; if (initialiser_ != NULL) - initialiser_->For(out); + initialiser_->Output(out, CYNoIn); out.Terminate(); if (test_ != NULL) out << ' '; @@ -468,7 +464,8 @@ void CYLabel::Output(CYOutput &out, CYFlags flags) const { } void CYLet::Output(CYOutput &out, CYFlags flags) const { - out << "let" << ' ' << '(' << *declarations_ << ')' << ' ' << code_; + out << "let" << ' ' << '(' << *declarations_ << ')'; + code_->Single(out, CYRight(flags)); } namespace cy { diff --git a/Parser.hpp b/Parser.hpp index d711d1d..db4b1e6 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -530,8 +530,8 @@ 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 { @@ -561,7 +561,6 @@ struct CYExpression : return true; } - virtual void For(CYOutput &out) const; virtual void ForIn(CYOutput &out, CYFlags flags) const; virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); @@ -1171,15 +1170,16 @@ struct CYDeclaration : 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_; @@ -1189,15 +1189,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 { @@ -1216,11 +1232,11 @@ struct CYLet : CYStatement { CYDeclarations *declarations_; - CYBlock code_; + CYStatement *code_; - CYLet(CYDeclarations *declarations, CYStatement *statements) : + CYLet(CYDeclarations *declarations, CYStatement *code) : declarations_(declarations), - code_(statements) + code_(code) { } diff --git a/Replace.cpp b/Replace.cpp index c9f57b8..2caa728 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -196,10 +196,13 @@ CYStatement *CYContinue::Replace(CYContext &context) { } CYAssignment *CYDeclaration::Assignment(CYContext &context) { - context.Replace(identifier_); - CYExpression *variable(Replace(context)); - context.scope_->Declare(context, identifier_, CYIdentifierVariable); - return initialiser_ == NULL ? NULL : $ CYAssign(variable, initialiser_); + if (initialiser_ == NULL) + return NULL; + return $ CYAssign(Variable(context), initialiser_); +} + +CYVariable *CYDeclaration::Variable(CYContext &context) { + return $V(identifier_); } CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) { @@ -207,15 +210,30 @@ CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) { } CYExpression *CYDeclaration::Replace(CYContext &context) { - return $V(identifier_); + context.Replace(identifier_); + context.scope_->Declare(context, identifier_, CYIdentifierVariable); + return Variable(context); +} + +void CYDeclarations::Replace(CYContext &context) { $T() + declaration_->Replace(context); + next_->Replace(context); } CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL) return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context)); } -CYCompound *CYDeclarations::Replace(CYContext &context) { - CYCompound *compound(next_ == NULL ? $ CYCompound() : next_->Replace(context)); +CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL) + return $ CYFunctionParameter(declaration_->identifier_, next_->Parameter(context)); +} + +CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL) + return $ CYArgument(declaration_->initialiser_ ?: $U, next_->Argument(context)); +} + +CYCompound *CYDeclarations::Compound(CYContext &context) { $T(NULL) + CYCompound *compound(next_->Compound(context) ?: $ CYCompound()); if (CYAssignment *assignment = declaration_->Assignment(context)) compound->AddPrev(assignment); return compound; @@ -294,20 +312,26 @@ CYStatement *CYFor::Replace(CYContext &context) { return this; } +CYCompound *CYForDeclarations::Replace(CYContext &context) { + declarations_->Replace(context); + return declarations_->Compound(context); +} + +// XXX: this still feels highly suboptimal CYStatement *CYForIn::Replace(CYContext &context) { - CYAssignment *assignment(initialiser_->Assignment(context)); + CYForInInitialiser *initialiser(initialiser_); context.Replace(initialiser_); context.Replace(set_); context.Replace(code_); - if (assignment == NULL) - return this; + if (CYAssignment *assignment = initialiser->Assignment(context)) + return $ CYBlock($$->* + $E(assignment)->* + this + ); - return $ CYBlock($$->* - $E(assignment)->* - this - ); + return this; } CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const { @@ -448,7 +472,8 @@ CYStatement *CYLabel::Replace(CYContext &context) { } CYStatement *CYLet::Replace(CYContext &context) { - return $ CYWith($ CYObject(declarations_->Property(context)), &code_); + declarations_->Replace(context); + return $ CYWith($ CYObject(declarations_->Property(context)), code_); } void CYMember::Replace_(CYContext &context) { @@ -804,7 +829,8 @@ CYStatement *Try::Replace(CYContext &context) { } } CYStatement *CYVar::Replace(CYContext &context) { - return $E(declarations_->Replace(context)); + declarations_->Replace(context); + return $E(declarations_->Compound(context)); } CYExpression *CYVariable::Replace(CYContext &context) {