From 550ee46a9be4b8e9a9e25c1d080f081cbdfa10b9 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 27 Oct 2009 22:33:05 +0000 Subject: [PATCH] Rewrote for-each-in in terms of let, and replaced let with with. --- Parser.hpp | 39 ++++++++++++++++++++------------------- Replace.cpp | 13 ++++++++++--- Replace.hpp | 15 +++++++++++++++ 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/Parser.hpp b/Parser.hpp index 2dc89ce..c094018 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -929,13 +929,31 @@ struct CYArray : virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYProperty : + CYNext, + CYThing +{ + CYPropertyName *name_; + CYExpression *value_; + + CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) : + CYNext(next), + name_(name), + value_(value) + { + } + + void Replace(CYContext &context); + virtual void Output(CYOutput &out) const; +}; + struct CYDeclaration : CYForInInitialiser { CYIdentifier *identifier_; CYExpression *initialiser_; - CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) : + CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) : identifier_(identifier), initialiser_(initialiser) { @@ -967,6 +985,7 @@ struct CYDeclarations : virtual void For(CYOutput &out) const; void Replace(CYContext &context); + CYProperty *Property(CYContext &context); virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const; @@ -1058,24 +1077,6 @@ struct CYForEachIn : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYProperty : - CYNext, - CYThing -{ - CYPropertyName *name_; - CYExpression *value_; - - CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) : - CYNext(next), - name_(name), - value_(value) - { - } - - void Replace(CYContext &context); - virtual void Output(CYOutput &out) const; -}; - struct CYObject : CYLiteral { diff --git a/Replace.cpp b/Replace.cpp index 5a2ed37..fe93557 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -171,6 +171,10 @@ void CYDeclaration::Replace(CYContext &context) { context.Replace(initialiser_); } +CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL) + return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context)); +} + void CYDeclarations::Replace(CYContext &context) { $T() declaration_->Replace(context); next_->Replace(context); @@ -259,13 +263,12 @@ CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *stat CYStatement *CYForEachIn::Replace(CYContext &context) { CYVariable *cys($V("$cys")), *cyt($V("$cyt")); - return $ CYWith($ CYObject($ CYProperty($S("$cys"), $D(0), $ CYProperty($S("$cyt"), $D(0)))), $ CYBlock($$->* - $E($ CYAssign(cys, set_))->* + return $ CYLet($L2($L($I("$cys"), set_), $L($I("$cyt"))), $$->* $ CYForIn(cyt, cys, $ CYBlock($$->* $E($ CYAssign(initialiser_->ForEachIn(context), $M(cys, cyt)))->* code_ )) - )); + ); } CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const { @@ -334,6 +337,10 @@ CYStatement *CYLabel::Replace(CYContext &context) { return NULL; } +CYStatement *CYLet::Replace(CYContext &context) { + return $ CYWith($ CYObject(declarations_->Property(context)), &code_); +} + void CYMember::Replace_(CYContext &context) { context.Replace(object_); context.Replace(property_); diff --git a/Replace.hpp b/Replace.hpp index 493ea30..5bffa07 100644 --- a/Replace.hpp +++ b/Replace.hpp @@ -59,6 +59,8 @@ ($ CYFunctionParameter(args)) #define $S(args...) \ ($ CYString(args)) +#define $U \ + $V("undefined") #define $V(name) \ ($ CYVariable($I(name))) @@ -127,4 +129,17 @@ #define $N5(func, args...) \ $N(func, $C5_(args)) +#define $L(args...) \ + $ CYDeclaration(args) +#define $L1(arg0) \ + $ CYDeclarations(arg0) +#define $L2(arg0, args...) \ + $ CYDeclarations(arg0, $L1(args)) +#define $L3(arg0, args...) \ + $ CYDeclarations(arg0, $L2(args)) +#define $L4(arg0, args...) \ + $ CYDeclarations(arg0, $L3(args)) +#define $L5(arg0, args...) \ + $ CYDeclarations(arg0, $L4(args)) + #endif/*REPLACE_HPP*/ -- 2.45.2