]> git.saurik.com Git - cycript.git/commitdiff
Reboot variable renaming for lexical name scoping.
authorJay Freeman (saurik) <saurik@saurik.com>
Sat, 12 Dec 2015 13:56:41 +0000 (05:56 -0800)
committerJay Freeman (saurik) <saurik@saurik.com>
Sat, 12 Dec 2015 13:56:41 +0000 (05:56 -0800)
Execute.cpp
ObjectiveC/Replace.cpp
ObjectiveC/Syntax.hpp
Output.cpp
Parser.ypp.in
Replace.cpp
Scanner.lpp.in
Syntax.hpp

index 2882dcb4cb8fd0fd95897b3b729c796c4b1ba208..68a0d3908d6e51208eb5c8b1e1b4b72cfcb7236e 100644 (file)
@@ -362,6 +362,13 @@ _visible void CYGarbageCollect(JSContextRef context) {
     (JSSynchronousGarbageCollectForDebugging$ ?: &JSGarbageCollect)(context);
 }
 
+static JSValueRef Cycript_compile_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+    CYPool pool;
+    std::stringstream value(CYPoolCString(pool, context, arguments[0]));
+    CYUTF8String code(CYPoolCode(pool, value));
+    return CYCastJSValue(context, CYJSString(code));
+} CYCatch(NULL) }
+
 static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     CYGarbageCollect(context);
     return CYJSUndefined(context);
@@ -1897,6 +1904,7 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) {
 
     JSObjectRef cycript(JSObjectMake(context, NULL, NULL));
     CYSetProperty(context, global, CYJSString("Cycript"), cycript);
+    CYSetProperty(context, cycript, CYJSString("compile"), &Cycript_compile_callAsFunction);
     CYSetProperty(context, cycript, CYJSString("gc"), &Cycript_gc_callAsFunction);
 
     JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new));
index fb7f1f8c50823ff2791b750476b88c9632f367dd..2bd5c92e1f6724913066d372b2370c3a8777bd06 100644 (file)
@@ -80,7 +80,7 @@ CYStatement *CYImplementationField::Replace(CYContext &context) const { $T(NULL)
     );
 }
 
-CYExpression *CYInstanceLiteral::Replace(CYContext &context) {
+CYTarget *CYInstanceLiteral::Replace(CYContext &context) {
     return $N1($V("Instance"), number_);
 }
 
@@ -128,11 +128,11 @@ CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const {
     return MessageType(context, type_, next_);
 }
 
-CYExpression *CYBox::Replace(CYContext &context) {
+CYTarget *CYBox::Replace(CYContext &context) {
     return $C1($M($V("Instance"), $S("box")), value_);
 }
 
-CYExpression *CYObjCBlock::Replace(CYContext &context) {
+CYTarget *CYObjCBlock::Replace(CYContext &context) {
     return $C1($ CYEncodedType(($ CYTypedIdentifier(*typed_))->Modify($ CYTypeBlockWith(parameters_))), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_));
 }
 
@@ -145,7 +145,7 @@ CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL)
     );
 }
 
-CYExpression *CYSelector::Replace(CYContext &context) {
+CYTarget *CYSelector::Replace(CYContext &context) {
     return $C1($V("sel_registerName"), name_->Replace(context));
 }
 
@@ -160,7 +160,7 @@ CYString *CYSelectorPart::Replace(CYContext &context) {
     return $S($pool.strdup(str.str().c_str()));
 }
 
-CYExpression *CYSendDirect::Replace(CYContext &context) {
+CYTarget *CYSendDirect::Replace(CYContext &context) {
     std::ostringstream name;
     CYArgument **argument(&arguments_);
     CYSelectorPart *selector(NULL), *current(NULL);
@@ -185,6 +185,6 @@ CYExpression *CYSendDirect::Replace(CYContext &context) {
     return $C2($V("objc_msgSend"), self_, selector->Replace(context), arguments_);
 }
 
-CYExpression *CYSendSuper::Replace(CYContext &context) {
+CYTarget *CYSendSuper::Replace(CYContext &context) {
     return $ CYSendDirect($V("$cyr"), arguments_);
 }
index 5f6c726d9286b7d9b2b1cfce1829cc73910fec7d..aa55a42bc16be489116f41c0abd58f7c58ad6797 100644 (file)
@@ -25,7 +25,7 @@
 #include "../Syntax.hpp"
 
 struct CYInstanceLiteral :
-    CYExpression
+    CYTarget
 {
     CYNumber *number_;
 
@@ -36,12 +36,12 @@ struct CYInstanceLiteral :
 
     CYPrecedence(1)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYObjCBlock :
-    CYExpression
+    CYTarget
 {
     CYTypedIdentifier *typed_;
     CYTypedParameter *parameters_;
@@ -56,12 +56,12 @@ struct CYObjCBlock :
 
     CYPrecedence(1)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYBox :
-    CYExpression
+    CYTarget
 {
     CYExpression *value_;
 
@@ -72,7 +72,7 @@ struct CYBox :
 
     CYPrecedence(1)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -106,7 +106,7 @@ struct CYSelector :
 
     CYPrecedence(1)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -224,7 +224,7 @@ struct CYCategory :
 };
 
 struct CYSend :
-    CYExpression
+    CYTarget
 {
     CYArgument *arguments_;
 
@@ -249,7 +249,7 @@ struct CYSendDirect :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -261,7 +261,7 @@ struct CYSendSuper :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
index e43d038e8d64388a025a9d6b7d18e2b67d25a11d..5244b5bd8cf4e14277343b4857b051f80121fecf 100644 (file)
@@ -268,11 +268,6 @@ void CYDebugger::Output(CYOutput &out, CYFlags flags) const {
     out << "debugger" << ';';
 }
 
-void CYDeclaration::ForIn(CYOutput &out, CYFlags flags) const {
-    out << "var" << ' ';
-    Output(out, CYRight(flags));
-}
-
 void CYDeclaration::Output(CYOutput &out, CYFlags flags) const {
     out << *identifier_;
     //out.out_ << ':' << identifier_->usage_ << '#' << identifier_->offset_;
@@ -282,11 +277,6 @@ void CYDeclaration::Output(CYOutput &out, CYFlags flags) const {
     }
 }
 
-void CYForDeclarations::Output(CYOutput &out, CYFlags flags) const {
-    out << "var" << ' ';
-    declarations_->Output(out, CYRight(flags));
-}
-
 void CYDeclarations::Output(CYOutput &out) const {
     Output(out, CYNoFlags);
 }
@@ -353,15 +343,15 @@ void CYEmpty::Output(CYOutput &out, CYFlags flags) const {
     out.Terminate();
 }
 
+void CYEval::Output(CYOutput &out, CYFlags flags) const {
+    _assert(false);
+}
+
 void CYExpress::Output(CYOutput &out, CYFlags flags) const {
     expression_->Output(out, flags | CYNoBFC);
     out << ';';
 }
 
-void CYExpression::ForIn(CYOutput &out, CYFlags flags) const {
-    Output(out, flags | CYNoRightHand);
-}
-
 void CYExpression::Output(CYOutput &out) const {
     Output(out, CYNoFlags);
 }
@@ -406,33 +396,47 @@ void CYFor::Output(CYOutput &out, CYFlags flags) const {
     code_->Single(out, CYRight(flags), CYCompactShort);
 }
 
-void CYForOf::Output(CYOutput &out, CYFlags flags) const {
-    out << "for" << ' ' << "each" << ' ' << '(';
-    initialiser_->ForIn(out, CYNoIn);
-    out << ' ' << "in" << ' ' << *set_ << ')';
-    code_->Single(out, CYRight(flags), CYCompactShort);
+void CYForDeclarations::Output(CYOutput &out, CYFlags flags) const {
+    out << "var" << ' ';
+    declarations_->Output(out, CYRight(flags));
 }
 
-void CYForOfComprehension::Output(CYOutput &out) const {
-    out << "for" << ' ' << "each" << ' ' << '(';
-    declaration_->Output(out, CYNoIn);
-    out << ' ' << "in" << ' ' << *set_ << ')' << next_;
+void CYForLexical::Output(CYOutput &out, CYFlags flags) const {
+    out << (constant_ ? "const" : "let") << ' ';
+    declaration_->Output(out, CYRight(flags));
 }
 
 void CYForIn::Output(CYOutput &out, CYFlags flags) const {
     out << "for" << ' ' << '(';
-    if (initialiser_ != NULL)
-        initialiser_->ForIn(out, CYNoIn);
+    initialiser_->Output(out, CYNoIn | CYNoRightHand);
     out << ' ' << "in" << ' ' << *set_ << ')';
     code_->Single(out, CYRight(flags), CYCompactShort);
 }
 
 void CYForInComprehension::Output(CYOutput &out) const {
     out << "for" << ' ' << '(';
-    declaration_->Output(out, CYNoIn);
+    declaration_->Output(out, CYNoIn | CYNoRightHand);
     out << ' ' << "in" << ' ' << *set_ << ')';
 }
 
+void CYForOf::Output(CYOutput &out, CYFlags flags) const {
+    out << "for" << ' ' << '(';
+    initialiser_->Output(out, CYNoRightHand);
+    out << ' ' << "of" << ' ' << *set_ << ')';
+    code_->Single(out, CYRight(flags), CYCompactShort);
+}
+
+void CYForOfComprehension::Output(CYOutput &out) const {
+    out << "for" << ' ' << '(';
+    declaration_->Output(out, CYNoRightHand);
+    out << ' ' << "of" << ' ' << *set_ << ')' << next_;
+}
+
+void CYForVariable::Output(CYOutput &out, CYFlags flags) const {
+    out << "var" << ' ';
+    declaration_->Output(out, CYRight(flags));
+}
+
 void CYFunction::Output(CYOutput &out) const {
     out << '(' << parameters_ << ')' << ' ';
     out << '{' << '\n';
@@ -467,7 +471,7 @@ void CYFunctionParameter::Output(CYOutput &out) const {
 }
 
 const char *CYIdentifier::Word() const {
-    return replace_ == NULL || replace_ == this ? CYWord::Word() : replace_->Word();
+    return next_ == NULL || next_ == this ? CYWord::Word() : next_->Word();
 }
 
 void CYIf::Output(CYOutput &out, CYFlags flags) const {
@@ -513,6 +517,11 @@ void CYImport::Output(CYOutput &out, CYFlags flags) const {
     out << "@import";
 }
 
+void CYIndirect::Output(CYOutput &out, CYFlags flags) const {
+    out << "*";
+    rhs_->Output(out, Precedence(), CYRight(flags));
+}
+
 void CYIndirectMember::Output(CYOutput &out, CYFlags flags) const {
     object_->Output(out, Precedence(), CYLeft(flags));
     if (const char *word = property_->Word())
index 853c881aa41b71a151d3ab52582c87baa0b39e20..ae852908f9380744e19080b0ec7777b01c7587f2 100644 (file)
@@ -74,6 +74,7 @@
 %union { CYSpan *span_; }
 %union { CYStatement *statement_; }
 %union { CYString *string_; }
+%union { CYTarget *target_; }
 %union { CYThis *this_; }
 %union { CYTrue *true_; }
 %union { CYWord *word_; }
@@ -345,6 +346,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %token _char_ "char"
 %token _constructor_ "constructor"
 %token _double_ "double"
+%token _eval_ "eval"
 %token _final_ "final"
 %token _float_ "float"
 %token _from_ "from"
@@ -409,7 +411,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <argument_> ArgumentList
 %type <argument_> ArgumentListOpt
 %type <argument_> Arguments
-%type <expression_> ArrayComprehension
+%type <target_> ArrayComprehension
 %type <literal_> ArrayLiteral
 %type <expression_> ArrowFunction
 %type <functionParameter_> ArrowParameters
@@ -429,18 +431,18 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <statement_> BreakStatement
 %type <statement_> BreakableStatement
 %type <expression_> CallExpression_
-%type <expression_> CallExpression
+%type <target_> CallExpression
 %type <clause_> CaseBlock
 %type <clause_> CaseClause
 %type <clause_> CaseClausesOpt
 %type <catch_> Catch
 %type <identifier_> CatchParameter
 %type <statement_> ClassDeclaration
-%type <expression_> ClassExpression
+%type <target_> ClassExpression
 %type <classTail_> ClassHeritage
 %type <classTail_> ClassHeritageOpt
 %type <classTail_> ClassTail
-%type <expression_> Comprehension
+%type <target_> Comprehension
 %type <comprehension_> ComprehensionFor
 %type <comprehension_> ComprehensionIf
 %type <comprehension_> ComprehensionTail
@@ -464,7 +466,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <statement_> ExpressionStatement
 %type <finally_> Finally
 %type <declaration_> ForBinding
-%type <declaration_> ForDeclaration
+%type <forin_> ForDeclaration
 %type <forin_> ForInStatementInitializer
 %type <for_> ForStatementInitializer
 %type <declaration_> FormalParameter
@@ -473,11 +475,11 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <functionParameter_> FormalParameters
 %type <statement_> FunctionBody
 %type <statement_> FunctionDeclaration
-%type <expression_> FunctionExpression
+%type <target_> FunctionExpression
 %type <statement_> FunctionStatementList
 %type <statement_> GeneratorBody
 %type <statement_> GeneratorDeclaration
-%type <expression_> GeneratorExpression
+%type <target_> GeneratorExpression
 %type <method_> GeneratorMethod
 %type <statement_> HoistableDeclaration
 %type <identifier_> Identifier
@@ -491,7 +493,8 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <identifier_> LabelIdentifier
 %type <statement_> LabelledItem
 %type <statement_> LabelledStatement
-%type <expression_> LeftHandSideExpression
+%type <target_> LeftHandSideExpression
+%type <bool_> LetOrConst
 %type <declaration_> LexicalBinding
 %type <statement_> LexicalDeclaration
 %type <literal_> Literal
@@ -499,15 +502,15 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <expression_> LogicalANDExpression
 %type <expression_> LogicalORExpression
 %type <access_> MemberAccess
-%type <expression_> MemberExpression
+%type <target_> MemberExpression
 %type <method_> MethodDefinition
 %type <module_> ModulePath
 %type <expression_> MultiplicativeExpression
-%type <expression_> NewExpression
+%type <target_> NewExpression
 %type <null_> NullLiteral
 %type <literal_> ObjectLiteral
 %type <expression_> PostfixExpression
-%type <expression_> PrimaryExpression
+%type <target_> PrimaryExpression
 %type <propertyName_> PropertyName
 %type <property_> PropertyDefinition
 %type <property_> PropertyDefinitionList_
@@ -533,10 +536,10 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <statement_> StatementListOpt
 %type <statement_> StatementListItem
 %type <functionParameter_> StrictFormalParameters
-%type <expression_> SuperCall
-%type <expression_> SuperProperty
+%type <target_> SuperCall
+%type <target_> SuperProperty
 %type <statement_> SwitchStatement
-%type <expression_> TemplateLiteral
+%type <target_> TemplateLiteral
 %type <span_> TemplateSpans
 %type <statement_> ThrowStatement
 %type <statement_> TryStatement
@@ -580,7 +583,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <protocol_> ClassProtocols
 %type <protocol_> ClassProtocolsOpt
 %type <statement_> ImplementationStatement
-%type <expression_> MessageExpression
+%type <target_> MessageExpression
 %type <messageParameter_> MessageParameter
 %type <messageParameter_> MessageParameters
 %type <messageParameter_> MessageParameterList
@@ -823,6 +826,7 @@ IdentifierType
     | "constructor" { $$ = CYNew CYIdentifier("constructor"); }
     | "double" { $$ = CYNew CYIdentifier("double"); }
     | "each" { $$ = CYNew CYIdentifier("each"); }
+    | "eval" { $$ = CYNew CYIdentifier("eval"); }
     | "final" { $$ = CYNew CYIdentifier("final"); }
     | "float" { $$ = CYNew CYIdentifier("float"); }
     | "from" { $$ = CYNew CYIdentifier("from"); }
@@ -1020,12 +1024,12 @@ NewExpression
     ;
 
 CallExpression_
-    : MemberExpression
-    | CallExpression
+    : MemberExpression { $$ = $1; }
+    | CallExpression { $$ = $1; }
     ;
 
 CallExpression
-    : CallExpression_ Arguments { $$ = CYNew CYCall($1, $2); }
+    : CallExpression_ Arguments { if (!$1->Eval()) $$ = CYNew CYCall($1, $2); else $$ = CYNew CYEval($2); }
     | SuperCall { $$ = $1; }
     | CallExpression { driver.context_ = $1; } MemberAccess { $3->SetLeft($1); $$ = $3; }
     ;
@@ -1272,12 +1276,12 @@ StatementListItem
 /* }}} */
 /* 13.3 Let and Const Declarations {{{ */
 LexicalDeclaration
-    : LetOrConst BindingList Terminator { $$ = CYNew CYLet($2); }
+    : LetOrConst BindingList Terminator { $$ = CYNew CYLet($1, $2); }
     ;
 
 LetOrConst
-    : "let"
-    | "const"
+    : "let" { $$ = false; }
+    | "const" { $$ = true; }
     ;
 
 BindingList_
@@ -1385,7 +1389,7 @@ IterationStatement
     | "while" "(" Expression ")" Statement { $$ = CYNew CYWhile($3, $5); }
     | "for" "(" LexPushInOn ForStatementInitializer ";" LexPopIn ExpressionOpt ";" ExpressionOpt ")" Statement { $$ = CYNew CYFor($4, $7, $9, $11); }
     | "for" "(" LexPushInOn ForInStatementInitializer "!in" LexPopIn Expression ")" Statement { $$ = CYNew CYForIn($4, $7, $9); }
-    | "for" "(" LexPushInOn ForInStatementInitializer "of" LexPopIn Expression ")" Statement { $$ = CYNew CYForOf($4, $7, $9); }
+    | "for" "(" LexPushInOn ForInStatementInitializer "of" LexPopIn AssignmentExpression ")" Statement { $$ = CYNew CYForOf($4, $7, $9); }
     ;
 
 ForStatementInitializer
@@ -1396,12 +1400,12 @@ ForStatementInitializer
 
 ForInStatementInitializer
     : LeftHandSideExpression { $$ = $1; }
-    | LexSetRegExp Var_ ForBinding { $$ = $3; }
+    | LexSetRegExp Var_ ForBinding { $$ = CYNew CYForVariable($3); }
     | LexSetRegExp ForDeclaration { $$ = $2; }
     ;
 
 ForDeclaration
-    : LetOrConst ForBinding { $$ = $2; }
+    : LetOrConst ForBinding { $$ = CYNew CYForLexical($1, $2); }
     ;
 
 ForBinding
index ac673adefd204c04966a66d0a51f0f93b01cce0e..d1bc1e9c9d537b1f9fe56f42bf9a93f9dbeb22e7 100644 (file)
@@ -20,6 +20,7 @@
 /* }}} */
 
 #include <iomanip>
+#include <map>
 
 #include "Replace.hpp"
 #include "Syntax.hpp"
@@ -77,6 +78,14 @@ CYExpression *CYAddressOf::Replace(CYContext &context) {
     return $C0($M(rhs_, $S("$cya")));
 }
 
+CYTarget *CYApply::AddArgument(CYContext &context, CYExpression *value) {
+    CYArgument **argument(&arguments_);
+    while (*argument != NULL)
+        argument = &(*argument)->next_;
+    *argument = $ CYArgument(value);
+    return this;
+}
+
 CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
     context.Replace(value_);
     next_ = next_->Replace(context);
@@ -91,13 +100,13 @@ CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
     return this;
 }
 
-CYExpression *CYArray::Replace(CYContext &context) {
+CYTarget *CYArray::Replace(CYContext &context) {
     if (elements_ != NULL)
         elements_->Replace(context);
     return this;
 }
 
-CYExpression *CYArrayComprehension::Replace(CYContext &context) {
+CYTarget *CYArrayComprehension::Replace(CYContext &context) {
     CYVariable *cyv($V("$cyv"));
 
     return $C0($F(NULL, $P1($L($I("$cyv")), comprehensions_->Parameters(context)), $$->*
@@ -119,7 +128,10 @@ CYStatement *CYBlock::Return() {
 }
 
 CYStatement *CYBlock::Replace(CYContext &context) {
+    CYScope scope(true, context);
     context.ReplaceAll(code_);
+    scope.Close(context);
+
     if (code_ == NULL)
         return $ CYEmpty();
     return this;
@@ -129,15 +141,7 @@ CYStatement *CYBreak::Replace(CYContext &context) {
     return this;
 }
 
-CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
-    CYArgument **argument(&arguments_);
-    while (*argument != NULL)
-        argument = &(*argument)->next_;
-    *argument = $ CYArgument(value);
-    return this;
-}
-
-CYExpression *CYCall::Replace(CYContext &context) {
+CYTarget *CYCall::Replace(CYContext &context) {
     context.Replace(function_);
     arguments_->Replace(context);
     return this;
@@ -149,16 +153,15 @@ namespace Syntax {
 void Catch::Replace(CYContext &context) { $T()
     CYScope scope(true, context);
 
-    context.Replace(name_);
-    context.scope_->Declare(context, name_, CYIdentifierCatch);
+    name_ = name_->Replace(context, CYIdentifierCatch);
 
     context.ReplaceAll(code_);
-    scope.Close(context, code_);
+    scope.Close(context);
 }
 
 } }
 
-CYExpression *CYClassExpression::Replace(CYContext &context) {
+CYTarget *CYClassExpression::Replace(CYContext &context) {
     CYBuilder builder;
 
     CYIdentifier *super(context.Unique());
@@ -258,8 +261,8 @@ void CYContext::NonLocal(CYStatement *&statements) {
     CYContext &context(*this);
 
     if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
-        CYIdentifier *cye($I("$cye")->Replace(context));
-        CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
+        CYIdentifier *cye($I("$cye")->Replace(context, CYIdentifierGlobal));
+        CYIdentifier *unique(nextlocal_->identifier_->Replace(context, CYIdentifierGlobal));
 
         CYStatement *declare(
             $ CYVar($L1($L(unique, $ CYObject()))));
@@ -270,7 +273,6 @@ void CYContext::NonLocal(CYStatement *&statements) {
                     $ CYReturn($M($V(cye), $S("$cyv"))))->*
                 $ cy::Syntax::Throw($V(cye))));
 
-        // XXX: I don't understand any of this
         context.Replace(declare);
         rescue->Replace(context);
 
@@ -292,32 +294,31 @@ CYStatement *CYDebugger::Replace(CYContext &context) {
     return this;
 }
 
-CYAssignment *CYDeclaration::Assignment(CYContext &context) {
+CYTarget *CYDeclaration::Target(CYContext &context) {
+    return $V(identifier_);
+}
+
+CYAssignment *CYDeclaration::Replace(CYContext &context, CYIdentifierKind kind) {
+    identifier_ = identifier_->Replace(context, kind);
+
     if (initialiser_ == NULL)
         return NULL;
 
-    CYAssignment *value($ CYAssign(Variable(context), initialiser_));
+    CYAssignment *value($ CYAssign(Target(context), initialiser_));
     initialiser_ = NULL;
     return value;
 }
 
-CYVariable *CYDeclaration::Variable(CYContext &context) {
-    return $V(identifier_);
-}
+CYExpression *CYDeclarations::Replace(CYContext &context, CYIdentifierKind kind) { $T(NULL)
+    CYAssignment *assignment(declaration_->Replace(context, kind));
+    CYExpression *compound(next_->Replace(context, kind));
 
-CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
-    return $ CYVar($L1($ CYDeclaration(identifier_, value)));
-}
-
-CYExpression *CYDeclaration::Replace(CYContext &context) {
-    context.Replace(identifier_);
-    context.scope_->Declare(context, identifier_, CYIdentifierVariable);
-    return Variable(context);
-}
-
-void CYDeclarations::Replace(CYContext &context) { $T()
-    declaration_->Replace(context);
-    next_->Replace(context);
+    if (assignment != NULL)
+        if (compound == NULL)
+            compound = assignment;
+        else
+            compound = $ CYCompound(assignment, compound);
+    return compound;
 }
 
 CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
@@ -328,17 +329,7 @@ CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
     return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
 }
 
-CYExpression *CYDeclarations::Expression(CYContext &context) { $T(NULL)
-    CYExpression *compound(next_->Expression(context));
-    if (CYAssignment *assignment = declaration_->Assignment(context))
-        if (compound == NULL)
-            compound = assignment;
-        else
-            compound = $ CYCompound(assignment, compound);
-    return compound;
-}
-
-CYExpression *CYDirectMember::Replace(CYContext &context) {
+CYTarget *CYDirectMember::Replace(CYContext &context) {
     context.Replace(object_);
     context.Replace(property_);
     return this;
@@ -364,10 +355,17 @@ CYStatement *CYEmpty::Replace(CYContext &context) {
     return NULL;
 }
 
-CYExpression *CYEncodedType::Replace(CYContext &context) {
+CYTarget *CYEncodedType::Replace(CYContext &context) {
     return typed_->Replace(context);
 }
 
+CYTarget *CYEval::Replace(CYContext &context) {
+    context.scope_->Damage();
+    if (arguments_ != NULL)
+        arguments_->value_ = $C1($M($V("Cycript"), $S("compile")), arguments_->value_);
+    return $C($V("eval"), arguments_);
+}
+
 CYStatement *CYExpress::Return() {
     return $ CYReturn(expression_);
 }
@@ -377,18 +375,10 @@ CYStatement *CYExpress::Replace(CYContext &context) {
     return this;
 }
 
-CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
+CYTarget *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
     return $C1(this, value);
 }
 
-CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
-    return $E($ CYAssign(this, value));
-}
-
-CYAssignment *CYExpression::Assignment(CYContext &context) {
-    return NULL;
-}
-
 CYFunctionParameter *CYExpression::Parameter() const {
     return NULL;
 }
@@ -412,33 +402,54 @@ CYExpression *CYFatArrow::Replace(CYContext &context) {
 }
 
 void CYFinally::Replace(CYContext &context) { $T()
+    CYScope scope(true, context);
     context.ReplaceAll(code_);
+    scope.Close(context);
 }
 
 CYStatement *CYFor::Replace(CYContext &context) {
+    CYScope outer(true, context);
     context.Replace(initialiser_);
+
     context.Replace(test_);
+
+    {
+        CYScope inner(true, context);
+        context.ReplaceAll(code_);
+        inner.Close(context);
+    }
+
     context.Replace(increment_);
-    context.ReplaceAll(code_);
+
+    outer.Close(context);
     return this;
 }
 
 CYExpression *CYForDeclarations::Replace(CYContext &context) {
-    declarations_->Replace(context);
-    return declarations_->Expression(context);
+    return declarations_->Replace(context, CYIdentifierVariable);
 }
 
-// XXX: this still feels highly suboptimal
-CYStatement *CYForIn::Replace(CYContext &context) {
-    if (CYAssignment *assignment = initialiser_->Assignment(context))
-        return $ CYBlock($$->*
-            $E(assignment)->*
-            this
-        );
+CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) {
+    if (value == NULL) {
+        if (declaration_->initialiser_ == NULL)
+            return NULL;
+        value = declaration_->initialiser_;
+    }
+
+    return $ CYLet(constant_, $L1($ CYDeclaration(declaration_->identifier_, value)));
+}
+
+CYTarget *CYForLexical::Replace(CYContext &context) {
+    _assert(declaration_->Replace(context, CYIdentifierLexical) == NULL);
+    return declaration_->Target(context);
+}
 
+CYStatement *CYForIn::Replace(CYContext &context) {
+    CYScope scope(true, context);
     context.Replace(initialiser_);
     context.Replace(set_);
     context.ReplaceAll(code_);
+    scope.Close(context);
     return this;
 }
 
@@ -447,22 +458,17 @@ CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
 }
 
 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
-    return $ CYForIn(declaration_->Variable(context), set_, CYComprehension::Replace(context, statement));
+    return $ CYForIn(declaration_->Target(context), set_, CYComprehension::Replace(context, statement));
 }
 
 CYStatement *CYForOf::Replace(CYContext &context) {
-    if (CYAssignment *assignment = initialiser_->Assignment(context))
-        return $ CYBlock($$->*
-            $E(assignment)->*
-            this
-        );
-
-    CYIdentifier *cys(context.Unique()), *cyt(context.Unique());
+    CYIdentifier *item(context.Unique()), *list(context.Unique());
 
     return $ CYBlock($$
-        ->* $ CYLet($L2($L(cys, set_), $L(cyt)))
-        ->* $ CYForIn($V(cyt), $V(cys), $ CYBlock($$
-            ->* initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))
+        ->* initialiser_->Initialize(context, NULL)
+        ->* $ CYLet(false, $L2($L(list, set_), $L(item)))
+        ->* $ CYForIn($V(item), $V(list), $ CYBlock($$
+            ->* initialiser_->Initialize(context, $M($V(list), $V(item)))
             ->* code_
     )));
 }
@@ -476,13 +482,32 @@ CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *stat
 
     return $E($C0($F(NULL, $P1($L($I("$cys"))), $$->*
         $E($ CYAssign($V(cys), set_))->*
-        $ CYForIn(declaration_->Variable(context), $V(cys), $ CYBlock($$->*
-            $E($ CYAssign(declaration_->Variable(context), $M($V(cys), declaration_->Variable(context))))->*
+        $ CYForIn(declaration_->Target(context), $V(cys), $ CYBlock($$->*
+            $E($ CYAssign(declaration_->Target(context), $M($V(cys), declaration_->Target(context))))->*
             CYComprehension::Replace(context, statement)
         ))
     )));
 }
 
+CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
+    if (value == NULL) {
+        if (declaration_->initialiser_ == NULL)
+            return NULL;
+        value = declaration_->initialiser_;
+    }
+
+    return $ CYVar($L1($ CYDeclaration(declaration_->identifier_, value)));
+}
+
+CYTarget *CYForVariable::Replace(CYContext &context) {
+    _assert(declaration_->Replace(context, CYIdentifierVariable) == NULL);
+    return declaration_->Target(context);
+}
+
+// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
+#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
+//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
+
 void CYFunction::Replace(CYContext &context) {
     CYThisScope *_this(context.this_);
     context.this_ = &this_;
@@ -533,41 +558,42 @@ void CYFunction::Replace(CYContext &context) {
     scope.Close(context, code_);
 }
 
-CYExpression *CYFunctionExpression::Replace(CYContext &context) {
+CYTarget *CYFunctionExpression::Replace(CYContext &context) {
     CYScope scope(false, context);
     if (name_ != NULL)
-        context.scope_->Declare(context, name_, CYIdentifierOther);
+        name_ = name_->Replace(context, CYIdentifierOther);
+
     CYFunction::Replace(context);
-    scope.Close(context, code_);
+    scope.Close(context);
     return this;
 }
 
 void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
-    CYAssignment *assignment(initialiser_->Assignment(context));
-    context.Replace(initialiser_);
+    CYAssignment *assignment(initialiser_->Replace(context, CYIdentifierArgument));
 
     next_->Replace(context, statements);
 
     if (assignment != NULL)
         statements = $$->*
-            // XXX: this cast is quite incorrect
-            $ CYIf($ CYIdentical($ CYTypeOf(dynamic_cast<CYExpression *>(initialiser_)), $S("undefined")), $$->*
+            $ CYIf($ CYIdentical($ CYTypeOf(initialiser_->Target(context)), $S("undefined")), $$->*
                 $E(assignment)
             )->*
             statements;
 }
 
 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
-    context.scope_->Declare(context, name_, CYIdentifierOther);
+    name_ = name_->Replace(context, CYIdentifierOther);
     CYFunction::Replace(context);
     return this;
 }
 
-CYIdentifier *CYIdentifier::Replace(CYContext &context) {
-    if (replace_ != NULL && replace_ != this)
-        return replace_->Replace(context);
-    replace_ = context.scope_->Lookup(context, this);
-    return replace_;
+CYIdentifier *CYIdentifier::Replace(CYContext &context, CYIdentifierKind kind) {
+    if (next_ == this)
+        return this;
+    if (next_ != NULL)
+        return next_->Replace(context, kind);
+    next_ = context.scope_->Declare(context, this, kind)->identifier_;
+    return next_;
 }
 
 CYStatement *CYIf::Return() {
@@ -595,11 +621,11 @@ CYStatement *CYImport::Replace(CYContext &context) {
     return $ CYVar($L1($L($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
 }
 
-CYExpression *CYIndirect::Replace(CYContext &context) {
+CYTarget *CYIndirect::Replace(CYContext &context) {
     return $M(rhs_, $S("$cyi"));
 }
 
-CYExpression *CYIndirectMember::Replace(CYContext &context) {
+CYTarget *CYIndirectMember::Replace(CYContext &context) {
     return $M($ CYIndirect(object_), property_);
 }
 
@@ -614,13 +640,12 @@ CYStatement *CYLabel::Replace(CYContext &context) {
     return this;
 }
 
-CYExpression *CYLambda::Replace(CYContext &context) {
+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) {
-    declarations_->Replace(context);
-    if (CYExpression *expression = declarations_->Expression(context))
+    if (CYExpression *expression = declarations_->Replace(context, CYIdentifierLexical))
         return $E(expression);
     return $ CYEmpty();
 }
@@ -652,12 +677,12 @@ CYExpression *CYMultiply::Replace(CYContext &context) {
 namespace cy {
 namespace Syntax {
 
-CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
+CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
     CYSetLast(arguments_) = $ CYArgument(value);
     return this;
 }
 
-CYExpression *New::Replace(CYContext &context) {
+CYTarget *New::Replace(CYContext &context) {
     context.Replace(constructor_);
     arguments_->Replace(context);
     return this;
@@ -686,7 +711,7 @@ CYExpression *CYNumber::PropertyName(CYContext &context) {
     return String(context);
 }
 
-CYExpression *CYObject::Replace(CYContext &context) {
+CYTarget *CYObject::Replace(CYContext &context) {
     CYBuilder builder;
     if (properties_ != NULL)
         properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), false);
@@ -703,8 +728,10 @@ CYExpression *CYObject::Replace(CYContext &context) {
     return this;
 }
 
-CYExpression *CYParenthetical::Replace(CYContext &context) {
-    return expression_;
+CYTarget *CYParenthetical::Replace(CYContext &context) {
+    // XXX: return expression_;
+    context.Replace(expression_);
+    return this;
 }
 
 CYExpression *CYPostfix::Replace(CYContext &context) {
@@ -779,26 +806,9 @@ void CYPropertyValue::Replace(CYContext &context) {
     context.Replace(value_);
 }
 
-// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
-#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
-//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
-
-namespace {
-    struct IdentifierUsageLess :
-        std::binary_function<CYIdentifier *, CYIdentifier *, bool>
-    {
-        _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
-            if (lhs->usage_ != rhs->usage_)
-                return lhs->usage_ > rhs->usage_;
-            return lhs < rhs;
-        }
-    };
-
-    typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
-}
-
 void CYScript::Replace(CYContext &context) {
-    CYScope scope(true, context);
+    CYScope scope(false, context);
+    context.scope_->Damage();
 
     context.nextlocal_ = $ CYNonLocal();
     context.ReplaceAll(code_);
@@ -806,32 +816,18 @@ void CYScript::Replace(CYContext &context) {
 
     scope.Close(context, code_);
 
-    size_t offset(0);
-
-    CYCStringSet external;
-    for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
-        external.insert((*i)->Word());
-
-    IdentifierUsages usages;
-
-    if (offset < context.rename_.size())
-        CYForEach (i, context.rename_[offset].identifier_)
-            usages.insert(i);
-
-    // XXX: totalling the probable occurrences and sorting by them would improve the result
-    for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
-        //std::cout << *i << ":" << (*i)->offset_ << std::endl;
+    unsigned offset(0);
 
+    for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
         const char *name;
-
         if (context.options_.verbose_)
-            name = $pool.strcat("$", $pool.itoa(offset), NULL);
+            name = $pool.strcat("$", $pool.itoa(offset++), NULL);
         else {
             char id[8];
             id[7] = '\0';
 
           id:
-            unsigned position(7), local(offset + 1);
+            unsigned position(7), local(offset++ + 1);
 
             do {
                 unsigned index(local % (sizeof(MappingSet) - 1));
@@ -839,17 +835,16 @@ void CYScript::Replace(CYContext &context) {
                 id[--position] = MappingSet[index];
             } while (local != 0);
 
-            if (external.find(id + position) != external.end()) {
-                ++offset;
+            if (scope.Lookup(context, id + position) != NULL)
                 goto id;
-            }
+            // XXX: at some point, this could become a keyword
 
             name = $pool.strmemdup(id + position, 7 - position);
-            // XXX: at some point, this could become a keyword
         }
 
-        CYForEach (identifier, i->identifier_)
-            identifier->Set(name);
+        CYIdentifier *identifier(*i);
+        _assert(identifier->next_ == identifier);
+        identifier->next_ = $I(name);
     }
 }
 
@@ -865,15 +860,15 @@ CYStatement *CYReturn::Replace(CYContext &context) {
     return this;
 }
 
-CYExpression *CYRubyBlock::Replace(CYContext &context) {
+CYTarget *CYRubyBlock::Replace(CYContext &context) {
     return call_->AddArgument(context, proc_->Replace(context));
 }
 
-CYExpression *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
+CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
     return Replace(context)->AddArgument(context, value);
 }
 
-CYExpression *CYRubyProc::Replace(CYContext &context) {
+CYTarget *CYRubyProc::Replace(CYContext &context) {
     CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
     function = CYNonLocalize(context, function);
     function->implicit_ = true;
@@ -882,116 +877,164 @@ CYExpression *CYRubyProc::Replace(CYContext &context) {
 
 CYScope::CYScope(bool transparent, CYContext &context) :
     transparent_(transparent),
-    parent_(context.scope_)
+    parent_(context.scope_),
+    damaged_(false),
+    shadow_(NULL),
+    internal_(NULL)
 {
+    _assert(!transparent_ || parent_ != NULL);
     context.scope_ = this;
 }
 
-void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
-    if (!transparent_ || flags == CYIdentifierArgument || flags == CYIdentifierCatch)
-        internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
-    else if (parent_ != NULL)
-        parent_->Declare(context, identifier, flags);
+void CYScope::Damage() {
+    damaged_ = true;
+    if (parent_ != NULL)
+        parent_->Damage();
 }
 
-CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
-    std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
-    return *insert.first;
+CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
+    CYForEach (i, internal_)
+        if (strcmp(i->identifier_->Word(), word) == 0)
+            return i;
+    return NULL;
 }
 
-void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
-    std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
-    if (!insert.second) {
-        if ((*insert.first)->offset_ < identifier->offset_)
-            (*insert.first)->offset_ = identifier->offset_;
-        identifier->replace_ = *insert.first;
-        (*insert.first)->usage_ += identifier->usage_ + 1;
-    }
+CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
+    return Lookup(context, identifier->Word());
 }
 
-namespace {
-    struct IdentifierOffset {
-        size_t offset_;
-        CYIdentifierFlags flags_;
-        size_t usage_;
-        CYIdentifier *identifier_;
+CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
+    _assert(identifier->next_ == NULL || identifier->next_ == identifier);
 
-        IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
-            offset_(identifier->offset_),
-            flags_(flags),
-            usage_(identifier->usage_),
-            identifier_(identifier)
-        {
-        }
-    };
+    CYIdentifierFlags *existing(Lookup(context, identifier));
+    if (existing == NULL)
+        internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
+    ++internal_->count_;
+    if (existing == NULL)
+        return internal_;
 
-    struct IdentifierOffsetLess :
-        std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
-    {
-        _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
-            if (lhs.offset_ != rhs.offset_)
-                return lhs.offset_ < rhs.offset_;
-            if (lhs.flags_ != rhs.flags_)
-                return lhs.flags_ < rhs.flags_;
-            /*if (lhs.usage_ != rhs.usage_)
-                return lhs.usage_ < rhs.usage_;*/
-            return lhs.identifier_ < rhs.identifier_;
-        }
-    };
+    switch (kind) {
+        case CYIdentifierArgument:
+        case CYIdentifierCatch:
+        case CYIdentifierMagic:
+            _assert(false);
+        default:
+            break;
+    }
+
+    if (existing->kind_ == CYIdentifierGlobal)
+        existing->kind_ = kind;
+    else if (kind == CYIdentifierGlobal)
+        ;
+    else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
+        _assert(false); // XXX: throw new SyntaxError()
 
-    typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
+    return existing;
+}
+
+void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
+    _assert(flags->identifier_->next_ == flags->identifier_);
+    CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
+    flags->identifier_->next_ = existing->identifier_;
+
+    existing->count_ += flags->count_;
+    if (existing->offset_ < flags->offset_)
+        existing->offset_ = flags->offset_;
 }
 
 void CYScope::Close(CYContext &context, CYStatement *&statements) {
+    Close(context);
+
+    CYList<CYDeclarations> declarations;
+
+    CYForEach (i, internal_)
+        if (i->kind_ == CYIdentifierVariable)
+            declarations ->* $ CYDeclarations($ CYDeclaration(i->identifier_));
+
+    if (declarations) {
+        CYVar *var($ CYVar(declarations));
+        var->SetNext(statements);
+        statements = var;
+    }
+}
+
+void CYScope::Close(CYContext &context) {
     context.scope_ = parent_;
 
-    if (parent_ == NULL)
-        return;
+    CYForEach (i, internal_) {
+        _assert(i->identifier_->next_ == i->identifier_);
+    switch (i->kind_) {
+        case CYIdentifierArgument: {
+            _assert(!transparent_);
+        } break;
+
+        case CYIdentifierLexical: {
+            if (!damaged_) {
+                CYIdentifier *replace(context.Unique());
+                replace->next_ = replace;
+                i->identifier_->next_ = replace;
+                i->identifier_ = replace;
+            }
+
+            if (!transparent_)
+                i->kind_ = CYIdentifierVariable;
+            else
+                parent_->Declare(context, i->identifier_, CYIdentifierVariable);
+        } break;
 
-    CYDeclarations *last(NULL), *curr(NULL);
+        case CYIdentifierVariable: {
+            if (transparent_) {
+                parent_->Declare(context, i->identifier_, i->kind_);
+                i->kind_ = CYIdentifierGlobal;
+            }
+        } break;
+    default:; } }
 
-    IdentifierOffsets offsets;
+    if (damaged_)
+        return;
 
-    for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
-        if (i->second != CYIdentifierMagic)
-            offsets.insert(IdentifierOffset(i->first, i->second));
+    typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
+    CYIdentifierOffsetMap offsets;
 
-    size_t offset(0);
+    CYForEach (i, internal_) {
+        _assert(i->identifier_->next_ == i->identifier_);
+    switch (i->kind_) {
+        case CYIdentifierArgument:
+        case CYIdentifierVariable:
+            offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
+        break;
+    default:; } }
 
-    for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
-        if (i->flags_ == CYIdentifierVariable) {
-            CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
-            if (last == NULL)
-                last = next;
-            if (curr != NULL)
-                curr->SetNext(next);
-            curr = next;
-        }
+    unsigned offset(0);
 
-        if (offset < i->offset_)
-            offset = i->offset_;
-        if (context.rename_.size() <= offset)
-            context.rename_.resize(offset + 1);
+    for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
+        if (offset < i->first)
+            offset = i->first;
+        CYIdentifier *identifier(i->second);
 
-        CYIdentifierUsage &rename(context.rename_[offset++]);
-        i->identifier_->SetNext(rename.identifier_);
-        rename.identifier_ = i->identifier_;
-        rename.usage_ += i->identifier_->usage_ + 1;
-    }
+        if (offset >= context.replace_.size())
+            context.replace_.resize(offset + 1, NULL);
+        CYIdentifier *&replace(context.replace_[offset++]);
 
-    if (last != NULL) {
-        CYVar *var($ CYVar(last));
-        var->SetNext(statements);
-        statements = var;
+        if (replace == NULL)
+            replace = identifier;
+        else {
+            _assert(replace->next_ == replace);
+            identifier->next_ = replace;
+        }
     }
 
-    for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
-        if (internal_.find(*i) == internal_.end()) {
-            //std::cout << *i << '=' << offset << std::endl;
-            if ((*i)->offset_ < offset)
-                (*i)->offset_ = offset;
-            parent_->Merge(context, *i);
-        }
+    if (parent_ == NULL)
+        return;
+
+    CYForEach (i, internal_) {
+    switch (i->kind_) {
+        case CYIdentifierGlobal: {
+            if (i->offset_ < offset)
+                i->offset_ = offset;
+            parent_->Merge(context, i);
+        } break;
+    default:; } }
 }
 
 CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
@@ -1024,11 +1067,11 @@ CYString *CYString::String(CYContext &context) {
     return this;
 }
 
-CYExpression *CYSuperAccess::Replace(CYContext &context) {
+CYTarget *CYSuperAccess::Replace(CYContext &context) {
     return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
 }
 
-CYExpression *CYSuperCall::Replace(CYContext &context) {
+CYTarget *CYSuperCall::Replace(CYContext &context) {
     return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
 }
 
@@ -1038,11 +1081,17 @@ CYStatement *CYSwitch::Replace(CYContext &context) {
     return this;
 }
 
-CYExpression *CYTemplate::Replace(CYContext &context) {
+CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
+    if (value == NULL)
+        return NULL;
+    return $E($ CYAssign(this, value));
+}
+
+CYTarget *CYTemplate::Replace(CYContext &context) {
     return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
 }
 
-CYExpression *CYThis::Replace(CYContext &context) {
+CYTarget *CYThis::Replace(CYContext &context) {
     if (context.this_ != NULL)
         return $V(context.this_->Identifier(context));
     return this;
@@ -1058,7 +1107,7 @@ CYStatement *Throw::Replace(CYContext &context) {
 
 } }
 
-CYExpression *CYTrivial::Replace(CYContext &context) {
+CYTarget *CYTrivial::Replace(CYContext &context) {
     return this;
 }
 
@@ -1074,7 +1123,10 @@ namespace cy {
 namespace Syntax {
 
 CYStatement *Try::Replace(CYContext &context) {
+    CYScope scope(true, context);
     context.ReplaceAll(code_);
+    scope.Close(context);
+
     catch_->Replace(context);
     finally_->Replace(context);
     return this;
@@ -1082,15 +1134,15 @@ CYStatement *Try::Replace(CYContext &context) {
 
 } }
 
-CYExpression *CYTypeArrayOf::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
     return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
 }
 
-CYExpression *CYTypeBlockWith::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
     return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
 }
 
-CYExpression *CYTypeConstant::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
     return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
 }
 
@@ -1098,52 +1150,52 @@ CYStatement *CYTypeDefinition::Replace(CYContext &context) {
     return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context)));
 }
 
-CYExpression *CYTypeError::Replace(CYContext &context) {
+CYTarget *CYTypeError::Replace(CYContext &context) {
     _assert(false);
     return NULL;
 }
 
-CYExpression *CYTypeModifier::Replace(CYContext &context, CYExpression *type) { $T(type)
+CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
     return Replace_(context, type);
 }
 
-CYExpression *CYTypeFunctionWith::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
     return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
 }
 
-CYExpression *CYTypeLong::Replace(CYContext &context) {
+CYTarget *CYTypeLong::Replace(CYContext &context) {
     return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
 }
 
-CYExpression *CYTypePointerTo::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
     return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
 }
 
-CYExpression *CYTypeShort::Replace(CYContext &context) {
+CYTarget *CYTypeShort::Replace(CYContext &context) {
     return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
 }
 
-CYExpression *CYTypeSigned::Replace(CYContext &context) {
+CYTarget *CYTypeSigned::Replace(CYContext &context) {
     return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
 }
 
-CYExpression *CYTypeUnsigned::Replace(CYContext &context) {
+CYTarget *CYTypeUnsigned::Replace(CYContext &context) {
     return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
 }
 
-CYExpression *CYTypeVariable::Replace(CYContext &context) {
+CYTarget *CYTypeVariable::Replace(CYContext &context) {
     return $V(name_);
 }
 
-CYExpression *CYTypeVoid::Replace(CYContext &context) {
+CYTarget *CYTypeVoid::Replace(CYContext &context) {
     return $N1($V("Type"), $ CYString("v"));
 }
 
-CYExpression *CYTypeVolatile::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
     return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
 }
 
-CYExpression *CYTypedIdentifier::Replace(CYContext &context) {
+CYTarget *CYTypedIdentifier::Replace(CYContext &context) {
     return modifier_->Replace(context, specifier_->Replace(context));
 }
 
@@ -1173,14 +1225,13 @@ CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *
 }
 
 CYStatement *CYVar::Replace(CYContext &context) {
-    declarations_->Replace(context);
-    if (CYExpression *expression = declarations_->Expression(context))
+    if (CYExpression *expression = declarations_->Replace(context, CYIdentifierVariable))
         return $E(expression);
     return $ CYEmpty();
 }
 
-CYExpression *CYVariable::Replace(CYContext &context) {
-    context.Replace(name_);
+CYTarget *CYVariable::Replace(CYContext &context) {
+    name_ = name_->Replace(context, CYIdentifierGlobal);
     return this;
 }
 
index 139901104f1ed2dd029149b2312b5d20cee2402b..72011986e4b26010b69b1e62f74cba2b6d4ce192 100644 (file)
@@ -64,6 +64,7 @@ typedef cy::parser::token tk;
 
 #define I(type, Type, value, highlight) do { \
     yylval->semantic_.type ## _ = A CY ## Type; \
+    yylval->semantic_.type ## _->location_ = *yylloc; \
     F(value, highlight); \
 } while (false)
 
@@ -460,6 +461,7 @@ XMLName {XMLNameStart}{XMLNamePart}*
 "enum"            L /*FFF*/ F(tk::_enum_, hi::Meta);
 "export"          L /*FFK*/ F(tk::_export_, hi::Meta);
 "extends"         L /*FFK*/ F(tk::_extends_, hi::Meta);
+"eval"            L /*III*/ F(tk::_eval_, hi::Special);
 "false"           L /*LLL*/ F(tk::_false_, hi::Constant);
 "final"           L /*FII*/ F(tk::_final_, hi::Meta);
 "finally"         L /*KKK*/ F(tk::_finally_, hi::Control);
index a83659d7e6bd53ab15cbb60dee0d31fef46ace72..786b9f72969c116d35b8b7e830500927e4feefd8 100644 (file)
@@ -28,8 +28,6 @@
 #include <streambuf>
 #include <string>
 #include <vector>
-#include <map>
-#include <set>
 
 #include "List.hpp"
 #include "Location.hpp"
@@ -212,10 +210,6 @@ struct CYWord :
     {
     }
 
-    void Set(const char *value) {
-        word_ = value;
-    }
-
     virtual bool Constructor() const {
         return strcmp(word_, "constructor") == 0;
     }
@@ -232,24 +226,33 @@ _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
     return lhs << rhs.Word();
 }
 
+enum CYIdentifierKind {
+    CYIdentifierArgument,
+    CYIdentifierCatch,
+    CYIdentifierGlobal,
+    CYIdentifierLexical,
+    CYIdentifierMagic,
+    CYIdentifierOther,
+    CYIdentifierVariable,
+};
+
 struct CYIdentifier :
     CYNext<CYIdentifier>,
     CYWord
 {
-    CYIdentifier *replace_;
+    CYLocation location_;
     size_t offset_;
     size_t usage_;
 
     CYIdentifier(const char *word) :
         CYWord(word),
-        replace_(NULL),
         offset_(0),
         usage_(0)
     {
     }
 
     virtual const char *Word() const;
-    CYIdentifier *Replace(CYContext &context);
+    CYIdentifier *Replace(CYContext &context, CYIdentifierKind);
 };
 
 struct CYLabel :
@@ -286,38 +289,43 @@ struct CYIdentifierValueLess :
     }
 };
 
-enum CYIdentifierFlags {
-    CYIdentifierArgument,
-    CYIdentifierVariable,
-    CYIdentifierOther,
-    CYIdentifierMagic,
-    CYIdentifierCatch,
-};
-
-typedef std::set<const char *, CYCStringLess> CYCStringSet;
-typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
-typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
-
-struct CYIdentifierUsage {
+struct CYIdentifierFlags :
+    CYNext<CYIdentifierFlags>
+{
     CYIdentifier *identifier_;
-    size_t usage_;
-};
+    CYIdentifierKind kind_;
+    unsigned count_;
+    unsigned offset_;
 
-typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
+    CYIdentifierFlags(CYIdentifier *identifier, CYIdentifierKind kind, CYIdentifierFlags *next = NULL) :
+        CYNext<CYIdentifierFlags>(next),
+        identifier_(identifier),
+        kind_(kind),
+        count_(0),
+        offset_(0)
+    {
+    }
+};
 
 struct CYScope {
     bool transparent_;
     CYScope *parent_;
+    bool damaged_;
+    CYIdentifierFlags *shadow_;
 
-    CYIdentifierAddressFlagsMap internal_;
-    CYIdentifierValueSet identifiers_;
+    CYIdentifierFlags *internal_;
 
     CYScope(bool transparent, CYContext &context);
 
-    void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
-    virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
-    void Merge(CYContext &context, CYIdentifier *identifier);
+    CYIdentifierFlags *Lookup(CYContext &context, const char *word);
+    CYIdentifierFlags *Lookup(CYContext &context, CYIdentifier *identifier);
+
+    CYIdentifierFlags *Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind);
+    void Merge(CYContext &context, const CYIdentifierFlags *flags);
+
     void Close(CYContext &context, CYStatement *&statements);
+    void Close(CYContext &context);
+    void Damage();
 };
 
 struct CYScript :
@@ -344,12 +352,12 @@ struct CYContext {
     CYThisScope *this_;
     CYIdentifier *super_;
 
-    CYIdentifierUsageVector rename_;
-
     CYNonLocal *nonlocal_;
     CYNonLocal *nextlocal_;
     unsigned unique_;
 
+    std::vector<CYIdentifier *> replace_;
+
     CYContext(CYOptions &options) :
         options_(options),
         scope_(NULL),
@@ -449,13 +457,13 @@ struct CYForInitializer {
     virtual void Output(CYOutput &out, CYFlags flags) const = 0;
 };
 
-struct CYForInInitializer {
-    virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
-    virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
+struct CYTarget;
+struct CYVar;
 
-    virtual CYExpression *Replace(CYContext &context) = 0;
-    virtual CYAssignment *Assignment(CYContext &context) = 0;
+struct CYForInInitializer {
+    virtual CYStatement *Initialize(CYContext &context, CYExpression *value) = 0;
 
+    virtual CYTarget *Replace(CYContext &context) = 0;
     virtual void Output(CYOutput &out, CYFlags flags) const = 0;
 };
 
@@ -466,7 +474,6 @@ struct CYString;
 
 struct CYExpression :
     CYForInitializer,
-    CYForInInitializer,
     CYThing
 {
     virtual int Precedence() const = 0;
@@ -475,17 +482,17 @@ struct CYExpression :
         return true;
     }
 
-    virtual void ForIn(CYOutput &out, CYFlags flags) const;
-    virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
+    virtual bool Eval() const {
+        return false;
+    }
 
-    virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
+    virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
 
     virtual void Output(CYOutput &out) const;
     virtual void Output(CYOutput &out, CYFlags flags) const = 0;
     void Output(CYOutput &out, int precedence, CYFlags flags) const;
 
     virtual CYExpression *Replace(CYContext &context) = 0;
-    virtual CYAssignment *Assignment(CYContext &context);
 
     virtual CYExpression *Primitive(CYContext &context) {
         return NULL;
@@ -506,6 +513,20 @@ struct CYExpression :
     }
 };
 
+struct CYTarget :
+    CYExpression,
+    CYForInInitializer
+{
+    virtual bool RightHand() const {
+        return false;
+    }
+
+    virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
+
+    virtual CYTarget *Replace(CYContext &context) = 0;
+    using CYExpression::Output;
+};
+
 #define CYAlphabetic(value) \
     virtual bool Alphabetic() const { \
         return value; \
@@ -517,11 +538,6 @@ struct CYExpression :
         return Precedence_; \
     }
 
-#define CYRightHand(value) \
-    virtual bool RightHand() const { \
-        return value; \
-    }
-
 struct CYCompound :
     CYExpression
 {
@@ -545,7 +561,7 @@ struct CYCompound :
 };
 
 struct CYParenthetical :
-    CYExpression
+    CYTarget
 {
     CYExpression *expression_;
 
@@ -556,7 +572,7 @@ struct CYParenthetical :
 
     CYPrecedence(0)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -566,9 +582,9 @@ struct CYFunctionParameter :
     CYNext<CYFunctionParameter>,
     CYThing
 {
-    CYForInInitializer *initialiser_;
+    CYDeclaration *initialiser_;
 
-    CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) :
+    CYFunctionParameter(CYDeclaration *initialiser, CYFunctionParameter *next = NULL) :
         CYNext<CYFunctionParameter>(next),
         initialiser_(initialiser)
     {
@@ -651,7 +667,7 @@ struct CYIfComprehension :
 };
 
 struct CYArrayComprehension :
-    CYExpression
+    CYTarget
 {
     CYExpression *expression_;
     CYComprehension *comprehensions_;
@@ -664,15 +680,16 @@ struct CYArrayComprehension :
 
     CYPrecedence(0)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYLiteral :
-    CYExpression
+    CYTarget
 {
+    CYLocation location_;
+
     CYPrecedence(0)
-    CYRightHand(false)
 
     virtual CYExpression *Primitive(CYContext &context) {
         return this;
@@ -682,14 +699,13 @@ struct CYLiteral :
 struct CYTrivial :
     CYLiteral
 {
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
 };
 
 struct CYMagic :
-    CYExpression
+    CYTarget
 {
     CYPrecedence(0)
-    CYRightHand(false)
 };
 
 struct CYRange {
@@ -782,7 +798,7 @@ struct CYSpan :
 };
 
 struct CYTemplate :
-    CYExpression
+    CYTarget
 {
     CYString *string_;
     CYSpan *spans_;
@@ -794,9 +810,8 @@ struct CYTemplate :
     }
 
     CYPrecedence(0)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -873,7 +888,7 @@ struct CYNull :
 struct CYThis :
     CYMagic
 {
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -907,7 +922,7 @@ struct CYTrue :
 };
 
 struct CYVariable :
-    CYExpression
+    CYTarget
 {
     CYIdentifier *name_;
 
@@ -922,9 +937,12 @@ struct CYVariable :
     }
 
     CYPrecedence(0)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual bool Eval() const {
+        return strcmp(name_->Word(), "eval") == 0;
+    }
+
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 
     virtual CYFunctionParameter *Parameter() const;
@@ -993,16 +1011,16 @@ struct CYPostfix :
 struct CYAssignment :
     CYExpression
 {
-    CYExpression *lhs_;
+    CYTarget *lhs_;
     CYExpression *rhs_;
 
-    CYAssignment(CYExpression *lhs, CYExpression *rhs) :
+    CYAssignment(CYTarget *lhs, CYExpression *rhs) :
         lhs_(lhs),
         rhs_(rhs)
     {
     }
 
-    void SetLeft(CYExpression *lhs) {
+    void SetLeft(CYTarget *lhs) {
         lhs_ = lhs;
     }
 
@@ -1112,13 +1130,11 @@ struct CYArray :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
-struct CYDeclaration :
-    CYForInInitializer
-{
+struct CYDeclaration {
     CYIdentifier *identifier_;
     CYExpression *initialiser_;
 
@@ -1128,14 +1144,43 @@ struct CYDeclaration :
     {
     }
 
-    virtual void ForIn(CYOutput &out, CYFlags flags) const;
-    virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
+    CYTarget *Target(CYContext &context);
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
+    virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
+struct CYForLexical :
+    CYForInInitializer
+{
+    bool constant_;
+    CYDeclaration *declaration_;
+
+    CYForLexical(bool constant, CYDeclaration *declaration) :
+        constant_(constant),
+        declaration_(declaration)
+    {
+    }
+
+    virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
+
+    virtual CYTarget *Replace(CYContext &context);
+    virtual void Output(CYOutput &out, CYFlags flags) const;
+};
 
-    virtual CYAssignment *Assignment(CYContext &context);
-    CYVariable *Variable(CYContext &context);
+struct CYForVariable :
+    CYForInInitializer
+{
+    CYDeclaration *declaration_;
 
+    CYForVariable(CYDeclaration *declaration) :
+        declaration_(declaration)
+    {
+    }
+
+    virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
+
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1151,9 +1196,8 @@ struct CYDeclarations :
     {
     }
 
-    void Replace(CYContext &context);
+    CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
 
-    CYExpression *Expression(CYContext &context);
     CYArgument *Argument(CYContext &context);
     CYFunctionParameter *Parameter(CYContext &context);
 
@@ -1194,9 +1238,11 @@ struct CYVar :
 struct CYLet :
     CYStatement
 {
+    bool constant_;
     CYDeclarations *declarations_;
 
-    CYLet(CYDeclarations *declarations) :
+    CYLet(bool constant, CYDeclarations *declarations) :
+        constant_(constant),
         declarations_(declarations)
     {
     }
@@ -1325,12 +1371,12 @@ struct CYObject :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYMember :
-    CYExpression
+    CYTarget
 {
     CYExpression *object_;
     CYExpression *property_;
@@ -1355,9 +1401,8 @@ struct CYDirectMember :
     }
 
     CYPrecedence(1)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1370,9 +1415,8 @@ struct CYIndirectMember :
     }
 
     CYPrecedence(1)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1380,7 +1424,7 @@ namespace cy {
 namespace Syntax {
 
 struct New :
-    CYExpression
+    CYTarget
 {
     CYExpression *constructor_;
     CYArgument *arguments_;
@@ -1395,41 +1439,61 @@ struct New :
         return arguments_ == NULL ? 2 : 1;
     }
 
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 
-    virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
+    virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
 };
 
 } }
 
-struct CYCall :
-    CYExpression
+struct CYApply :
+    CYTarget
 {
-    CYExpression *function_;
     CYArgument *arguments_;
 
-    CYCall(CYExpression *function, CYArgument *arguments = NULL) :
-        function_(function),
+    CYApply(CYArgument *arguments = NULL) :
         arguments_(arguments)
     {
     }
 
     CYPrecedence(1)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
+};
+
+struct CYCall :
+    CYApply
+{
+    CYExpression *function_;
+
+    CYCall(CYExpression *function, CYArgument *arguments = NULL) :
+        CYApply(arguments),
+        function_(function)
+    {
+    }
+
     virtual void Output(CYOutput &out, CYFlags flags) const;
+    virtual CYTarget *Replace(CYContext &context);
+};
+
+struct CYEval :
+    CYApply
+{
+    CYEval(CYArgument *arguments) :
+        CYApply(arguments)
+    {
+    }
 
-    virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
+    virtual void Output(CYOutput &out, CYFlags flags) const;
+    virtual CYTarget *Replace(CYContext &context);
 };
 
 struct CYRubyProc;
 
 struct CYRubyBlock :
-    CYExpression
+    CYTarget
 {
     CYExpression *call_;
     CYRubyProc *proc_;
@@ -1441,12 +1505,11 @@ struct CYRubyBlock :
     }
 
     CYPrecedence(1)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 
-    virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
+    virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
 };
 
 struct CYIf :
@@ -1531,7 +1594,7 @@ struct CYFunction {
 
 struct CYFunctionExpression :
     CYFunction,
-    CYExpression
+    CYTarget
 {
     CYIdentifier *name_;
 
@@ -1542,9 +1605,8 @@ struct CYFunctionExpression :
     }
 
     CYPrecedence(0)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    CYTarget *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1558,15 +1620,14 @@ struct CYFatArrow :
     }
 
     CYPrecedence(0)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    CYExpression *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYRubyProc :
     CYFunction,
-    CYExpression
+    CYTarget
 {
     CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
         CYFunction(parameters, code)
@@ -1574,9 +1635,8 @@ struct CYRubyProc :
     }
 
     CYPrecedence(0)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    CYTarget *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1594,7 +1654,7 @@ struct CYFunctionStatement :
 
     CYCompact(None)
 
-    virtual CYStatement *Replace(CYContext &context);
+    CYStatement *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1673,7 +1733,7 @@ struct CYClassTail :
 };
 
 struct CYClassExpression :
-    CYExpression
+    CYTarget
 {
     CYIdentifier *name_;
     CYClassTail *tail_;
@@ -1685,9 +1745,8 @@ struct CYClassExpression :
     }
 
     CYPrecedence(0)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    CYTarget *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1705,12 +1764,12 @@ struct CYClassStatement :
 
     CYCompact(Long)
 
-    virtual CYStatement *Replace(CYContext &context);
+    CYStatement *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYSuperCall :
-    CYExpression
+    CYTarget
 {
     CYArgument *arguments_;
 
@@ -1720,14 +1779,13 @@ struct CYSuperCall :
     }
 
     CYPrecedence(2)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    CYTarget *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYSuperAccess :
-    CYExpression
+    CYTarget
 {
     CYExpression *property_;
 
@@ -1737,9 +1795,8 @@ struct CYSuperAccess :
     }
 
     CYPrecedence(1)
-    CYRightHand(false)
 
-    virtual CYExpression *Replace(CYContext &context);
+    CYTarget *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1757,7 +1814,7 @@ struct CYExpress :
 
     CYCompact(None)
 
-    virtual CYStatement *Replace(CYContext &context);
+    CYStatement *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 
     virtual CYStatement *Return();
@@ -1775,7 +1832,7 @@ struct CYContinue :
 
     CYCompact(Short)
 
-    virtual CYStatement *Replace(CYContext &context);
+    CYStatement *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1791,7 +1848,7 @@ struct CYBreak :
 
     CYCompact(Short)
 
-    virtual CYStatement *Replace(CYContext &context);
+    CYStatement *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1807,7 +1864,7 @@ struct CYReturn :
 
     CYCompact(None)
 
-    virtual CYStatement *Replace(CYContext &context);
+    CYStatement *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1823,7 +1880,7 @@ struct CYYieldGenerator :
 
     CYPrecedence(0)
 
-    virtual CYExpression *Replace(CYContext &context);
+    CYExpression *Replace(CYContext &context) override;
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1869,7 +1926,7 @@ struct CYFinally :
 struct CYTypeSpecifier :
     CYThing
 {
-    virtual CYExpression *Replace(CYContext &context) = 0;
+    virtual CYTarget *Replace(CYContext &context) = 0;
 };
 
 struct CYTypeError :
@@ -1878,7 +1935,7 @@ struct CYTypeError :
     CYTypeError() {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
 };
 
@@ -1888,7 +1945,7 @@ struct CYTypeVoid :
     CYTypeVoid() {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
 };
 
@@ -1907,7 +1964,7 @@ struct CYTypeVariable :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
 };
 
@@ -1921,7 +1978,7 @@ struct CYTypeUnsigned :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
 };
 
@@ -1935,7 +1992,7 @@ struct CYTypeSigned :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
 };
 
@@ -1949,7 +2006,7 @@ struct CYTypeLong :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
 };
 
@@ -1963,7 +2020,7 @@ struct CYTypeShort :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
 };
 
@@ -1979,8 +2036,8 @@ struct CYTypeModifier :
 
     virtual int Precedence() const = 0;
 
-    virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
-    CYExpression *Replace(CYContext &context, CYExpression *type);
+    virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
+    CYTarget *Replace(CYContext &context, CYTarget *type);
 
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
     void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
@@ -2001,7 +2058,7 @@ struct CYTypeArrayOf :
 
     CYPrecedence(1)
 
-    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 };
 
@@ -2015,7 +2072,7 @@ struct CYTypeConstant :
 
     CYPrecedence(0)
 
-    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 };
 
@@ -2029,7 +2086,7 @@ struct CYTypePointerTo :
 
     CYPrecedence(0)
 
-    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 };
 
@@ -2043,7 +2100,7 @@ struct CYTypeVolatile :
 
     CYPrecedence(0)
 
-    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 };
 
@@ -2076,14 +2133,14 @@ struct CYTypedIdentifier :
         return this;
     }
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
 
     CYTypeFunctionWith *Function();
 };
 
 struct CYEncodedType :
-    CYExpression
+    CYTarget
 {
     CYTypedIdentifier *typed_;
 
@@ -2094,7 +2151,7 @@ struct CYEncodedType :
 
     CYPrecedence(1)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -2118,7 +2175,7 @@ struct CYTypedParameter :
 };
 
 struct CYLambda :
-    CYExpression
+    CYTarget
 {
     CYTypedIdentifier *typed_;
     CYTypedParameter *parameters_;
@@ -2133,7 +2190,7 @@ struct CYLambda :
 
     CYPrecedence(1)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -2216,7 +2273,7 @@ struct CYTypeBlockWith :
 
     CYPrecedence(0)
 
-    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 };
 
@@ -2233,7 +2290,7 @@ struct CYTypeFunctionWith :
 
     CYPrecedence(1)
 
-    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 
     virtual CYTypeFunctionWith *Function() { return this; }
@@ -2383,20 +2440,20 @@ struct CYAddressOf :
 };
 
 struct CYIndirect :
-    CYPrefix
+    CYTarget
 {
+    CYExpression *rhs_;
+
     CYIndirect(CYExpression *rhs) :
-        CYPrefix(rhs)
+        rhs_(rhs)
     {
     }
 
-    virtual const char *Operator() const {
-        return "*";
-    }
-
-    CYAlphabetic(false)
+    // XXX: this should be checked
+    CYPrecedence(2)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYTarget *Replace(CYContext &context);
+    virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
 #define CYReplace \
@@ -2453,7 +2510,7 @@ struct CYIndirect :
     struct CY ## name ## Assign : \
         CYAssignment \
     { args \
-        CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
+        CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
             CYAssignment(lhs, rhs) \
         { \
         } \