1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Merge branch 'master' of git://github.com/matehat/coffee-script

This commit is contained in:
Jeremy Ashkenas 2010-03-22 23:20:05 -04:00
commit 94185e3f70
3 changed files with 37 additions and 12 deletions

View file

@ -934,12 +934,13 @@
// See the [ECMAScript Harmony Wiki](http://wiki.ecmascript.org/doku.php?id=harmony:destructuring)
// for details.
AssignNode.prototype.compile_pattern_match = function compile_pattern_match(o) {
var _a, _b, _c, access_class, assigns, code, i, idx, obj, val, val_var, value;
var _a, _b, _c, access_class, assigns, code, i, idx, obj, oindex, olength, splat, val, val_var, value;
val_var = o.scope.free_variable();
value = this.value.is_statement() ? ClosureNode.wrap(this.value) : this.value;
assigns = ['' + this.tab + val_var + " = " + (value.compile(o)) + ";"];
o.top = true;
o.as_statement = true;
splat = false;
_a = this.variable.base.objects;
for (i = 0, _b = _a.length; i < _b; i++) {
obj = _a[i];
@ -950,11 +951,12 @@
idx = _c[1];
}
access_class = this.variable.is_array() ? IndexNode : AccessorNode;
if (obj instanceof SplatNode) {
val = literal(obj.compile_value(o, val_var, this.variable.base.objects.indexOf(obj)));
if (obj instanceof SplatNode && !splat) {
val = literal(obj.compile_value(o, val_var, ((oindex = this.variable.base.objects.indexOf(obj))), ((olength = this.variable.base.objects.length)) - oindex - 1));
splat = true;
} else {
if (!(typeof idx === 'object')) {
idx = literal(idx);
if (typeof idx !== 'object') {
idx = literal(splat ? '' + (val_var) + ".length - " + (olength - idx) : idx);
}
val = new ValueNode(literal(val_var), [new access_class(idx)]);
}
@ -1123,8 +1125,12 @@
};
// A compiling a splat as a destructuring assignment means slicing arguments
// from the right-hand-side's corresponding array.
SplatNode.prototype.compile_value = function compile_value(o, name, index) {
return "Array.prototype.slice.call(" + name + ", " + index + ")";
SplatNode.prototype.compile_value = function compile_value(o, name, index, trailings) {
if ((typeof trailings !== "undefined" && trailings !== null)) {
return "Array.prototype.slice.call(" + name + ", " + index + ", " + (name) + ".length - " + trailings + ")";
} else {
return "Array.prototype.slice.call(" + name + ", " + index + ")";
}
};
return SplatNode;
}).call(this);

View file

@ -708,14 +708,18 @@ exports.AssignNode: class AssignNode extends BaseNode
assigns: ["$@tab$val_var = ${ value.compile(o) };"]
o.top: true
o.as_statement: true
splat: false
for obj, i in @variable.base.objects
idx: i
[obj, idx]: [obj.value, obj.variable.base] if @variable.is_object()
access_class: if @variable.is_array() then IndexNode else AccessorNode
if obj instanceof SplatNode
val: literal(obj.compile_value(o, val_var, @variable.base.objects.indexOf(obj)))
if obj instanceof SplatNode and not splat
val: literal(obj.compile_value(o, val_var,
(oindex: @variable.base.objects.indexOf(obj)),
(olength: @variable.base.objects.length) - oindex - 1))
splat: true
else
idx: literal(idx) unless typeof idx is 'object'
idx: literal(if splat then "${val_var}.length - ${olength - idx}" else idx) if typeof idx isnt 'object'
val: new ValueNode(literal(val_var), [new access_class(idx)])
assigns.push(new AssignNode(obj, val).compile(o))
code: assigns.join("\n")
@ -829,8 +833,9 @@ exports.SplatNode: class SplatNode extends BaseNode
# A compiling a splat as a destructuring assignment means slicing arguments
# from the right-hand-side's corresponding array.
compile_value: (o, name, index) ->
"Array.prototype.slice.call($name, $index)"
compile_value: (o, name, index, trailings) ->
if trailings? then "Array.prototype.slice.call($name, $index, ${name}.length - $trailings)" \
else "Array.prototype.slice.call($name, $index)"
# Utility function that converts arbitrary number of elements, mixed with
# splats, to a proper array

View file

@ -28,6 +28,20 @@ ok a is 1
ok b is 2
ok c is 3
[x,y...,z]: [1,2,3,4,5]
ok x is 1
ok y.length is 3
ok z is 5
[x, [y, mids..., last], z..., end]: [1, [10, 20, 30, 40], 2,3,4, 5]
ok x is 1
ok y is 10
ok mids.length is 2 and mids[1] is 30
ok last is 40
ok z.length is 3 and z[2] is 4
ok end is 5
obj: {x: 10, y: 20, z: 30}