Skip to content

Commit

Permalink
closes #86; implemented named destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
satyr committed Aug 20, 2011
1 parent 5bc43f4 commit cf02f1b
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 106 deletions.
2 changes: 1 addition & 1 deletion extras/coco.js

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,13 @@ List = (function(_super){
List.displayName = 'List';
var prototype = __extends(List, _super).prototype;
prototype.children = ['items'];
prototype.show = function(){
return this.name;
};
prototype.named = function(name){
this.name = name;
return this;
};
prototype.isEmpty = function(){
return !this.items.length;
};
Expand Down Expand Up @@ -1686,12 +1693,15 @@ exports.Assign = Assign = (function(_super){
return If(test, left).addElse(put).compileNode(o);
};
prototype.compileDestructuring = function(o, left){
var items, len, ret, rite, rref, cache, list, code;
var items, len, ret, rite, that, cache, rref, list, code;
items = left.items;
len = items.length;
ret = o.level && !this['void'];
rite = this.right.compile(o, len === 1 ? LEVEL_CALL : LEVEL_LIST);
if ((ret || len > 1) && (!ID.test(rite) || left.assigns(rite))) {
if (that = left.name) {
cache = that + " = " + rite;
o.scope.declare(rite = that);
} else if ((ret || len > 1) && (!ID.test(rite) || left.assigns(rite))) {
cache = (rref = o.scope.temporary('ref')) + " = " + rite;
rite = rref;
}
Expand Down Expand Up @@ -2112,7 +2122,7 @@ exports.Fun = Fun = (function(_super){
if (vr.isEmpty()) {
vr = Var(scope.temporary('arg'));
} else if (!(vr instanceof Var)) {
v = Var(vr.varName() || scope.temporary('arg'));
v = Var((_ref = vr.name, delete vr.name, _ref) || vr.varName() || scope.temporary('arg'));
assigns.push(Assign(vr, df ? Op(p.op, v, p.second) : v));
vr = v;
} else if (df) {
Expand Down
4 changes: 4 additions & 0 deletions lib/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ bnf = {
return L(Arr($2));
}), o('{ Properties OptComma }', function(){
return L(Obj($2));
}), o('[ ArgList OptComma ] LABEL', function(){
return L(Arr($2)).named($5);
}), o('{ Properties OptComma } LABEL', function(){
return L(Obj($2)).named($5);
})
],
Key: [o('KeyBase'), o('Parenthetical')],
Expand Down
198 changes: 101 additions & 97 deletions lib/parser.js

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions src/ast.co
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,13 @@ class exports.Call extends Node
#### List
# An abstract node for a list of comma-separated items.
class List extends Node
children : [\items]
isEmpty : -> not @items.length
assigns : -> return true if node.assigns it for node of @items
children: [\items]

show : -> @name
named : (@name) -> this

isEmpty : -> not @items.length
assigns : -> return true if node.assigns it for node of @items

@compile = (o, items) ->
switch items.length
Expand Down Expand Up @@ -1011,7 +1015,10 @@ class exports.Assign extends Node
compileDestructuring: (o, left) ->
{items} = left; len = items.length; ret = o.level and not @void
rite = @right.compile o, if len is 1 then LEVEL_CALL else LEVEL_LIST
if (ret or len > 1) and (not ID.test rite or left.assigns rite)
if left.name
cache = "#that = #rite"
o.scope.declare rite = that
else if (ret or len > 1) and (not ID.test rite or left.assigns rite)
cache = "#{ rref = o.scope.temporary \ref } = #rite"
rite = rref
list = @"rend#{ left..displayName }" o, items, rite
Expand Down Expand Up @@ -1279,7 +1286,7 @@ class exports.Fun extends Node
if vr.isEmpty()
vr = Var scope.temporary \arg
else if vr not instanceof Var
v = Var vr.varName() || scope.temporary \arg
v = Var delete vr.name || vr.varName() || scope.temporary \arg
assigns.push Assign vr, if df then Op p.op, v, p.second else v
vr = v
else if df
Expand Down
3 changes: 3 additions & 0 deletions src/grammar.co
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ bnf =
List:
o '[ ArgList OptComma ]' -> L Arr $2
o '{ Properties OptComma }' -> L Obj $2
# Can be labeled to perform named destructuring.
o '[ ArgList OptComma ] LABEL' -> L Arr $2 .named $5
o '{ Properties OptComma } LABEL' -> L Obj $2 .named $5

# **Key** represents a property name, before `:` or after `.`.
Key:
Expand Down
10 changes: 10 additions & 0 deletions test/assignment.co
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,13 @@ o = d: 0, e: 1
o{d, e} &&*= d: 2, e: 3
eq 0 o.d
eq 3 o.e


### Named Destructuring
[b, c]:a = [0 1]
eq b, a.0
eq c, a.1

let {p, q}:o = {2 3}
eq p, o.p
eq q, o.q

0 comments on commit cf02f1b

Please sign in to comment.