From 34de4ee642fa0c3ef897a71a205d645b2cb71603 Mon Sep 17 00:00:00 2001 From: satyr Date: Mon, 15 Oct 2012 15:38:25 +0900 Subject: [PATCH] allowed `let this = x` (#172) --- lib/ast.js | 11 +++++++---- src/ast.co | 8 +++++--- test/function.co | 3 +++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/ast.js b/lib/ast.js index e6e3fd6bc..5a1f6921f 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -996,19 +996,22 @@ exports.Call = Call = (function(superclass){ return node.back = (args[index] = fun).body, node; }; Call['let'] = function(args, body){ - var params, res$, i, len$, a; + var params, res$, i, len$, a, that, gotThis; res$ = []; for (i = 0, len$ = args.length; i < len$; ++i) { a = args[i]; - if (a.op === '=' && !a.logic) { - args[i] = a.right; + if (that = a.op === '=' && !a.logic && a.right) { + args[i] = that; + if (i === 0 && (gotThis = a.left.value === 'this')) { + continue; + } res$.push(a.left); } else { res$.push(Var(a.varName() || a.carp('invalid "let" argument'))); } } params = res$; - args.unshift(Literal('this')); + gotThis || args.unshift(Literal('this')); return this.block(Fun(params, body), args, '.call'); }; return Call; diff --git a/src/ast.co b/src/ast.co index 337a12960..885f07c37 100644 --- a/src/ast.co +++ b/src/ast.co @@ -608,10 +608,12 @@ class exports.Call extends Node @let = (args, body) -> params = for a, i of args - if a.op is \= and not a.logic - then args[i] = a.right; a.left + if a.op is \= and not a.logic and a.right + args[i] = that + continue if i is 0 and gotThis = a.left.value is \this + a.left else Var a.varName! || a.carp 'invalid "let" argument' - args.unshift Literal \this + gotThis or args.unshift Literal \this @block Fun(params, body), args, \.call #### List diff --git a/test/function.co b/test/function.co index a6672cb7f..1a1cd4a63 100644 --- a/test/function.co +++ b/test/function.co @@ -445,6 +445,9 @@ eq ''' ok let [it] = [ok] it is ok +let this = eq + this eq, this + ### `with` with o = {}