Skip to content

Commit

Permalink
allowed let this = x (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Oct 15, 2012
1 parent c167ed6 commit 34de4ee
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
11 changes: 7 additions & 4 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/ast.co
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/function.co
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ eq '''
ok let [it] = [ok]
it is ok

let this = eq
this eq, this


### `with`
with o = {}
Expand Down

0 comments on commit 34de4ee

Please sign in to comment.