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

self-compiling pattern matching correctly

This commit is contained in:
Jeremy Ashkenas 2010-02-11 20:11:11 -05:00
parent 98f15d001f
commit 38d1381c02
2 changed files with 18 additions and 7 deletions

View file

@ -779,7 +779,14 @@
idx = __b[1];
}
access_class = this.variable.is_array() ? IndexNode : AccessorNode;
obj instanceof SplatNode ? (val = new LiteralNode(obj.compile_value(o, val_var, this.variable.base.objects.indexOf(obj)))) : (val = new ValueNode(new LiteralNode(val_var), [new access_class(idx)]));
if (obj instanceof SplatNode) {
val = new LiteralNode(obj.compile_value(o, val_var, this.variable.base.objects.indexOf(obj)));
} else {
if (!(typeof idx === 'object')) {
idx = new LiteralNode(idx);
}
val = new ValueNode(new LiteralNode(val_var), [new access_class(idx)]);
}
assigns.push(new AssignNode(obj, val).compile(o));
}
return assigns.join("\n");
@ -853,11 +860,13 @@
return this.index ? this.compile_param(o) : this.name.compile(o);
},
compile_param: function compile_param(o) {
o.scope.find(this.name);
return this.name + ' = Array.prototype.slice.call(arguments, ' + this.index + ')';
var name;
name = this.name.compile(o);
o.scope.find(name);
return name + ' = Array.prototype.slice.call(arguments, ' + this.index + ')';
},
compile_value: function compile_value(o, name, index) {
return "Array.prototype.slice.call(" + this.name + ', ' + this.index + ')';
return "Array.prototype.slice.call(" + name + ', ' + index + ')';
}
}));
// A while loop, the only sort of low-level loop exposed by CoffeeScript. From

View file

@ -600,6 +600,7 @@ AssignNode: exports.AssignNode: inherit Node, {
if obj instanceof SplatNode
val: new LiteralNode(obj.compile_value(o, val_var, @variable.base.objects.indexOf(obj)))
else
idx: new LiteralNode(idx) unless typeof idx is 'object'
val: new ValueNode(new LiteralNode(val_var), [new access_class(idx)])
assigns.push(new AssignNode(obj, val).compile(o))
assigns.join("\n")
@ -664,11 +665,12 @@ SplatNode: exports.SplatNode: inherit Node, {
if @index then @compile_param(o) else @name.compile(o)
compile_param: (o) ->
o.scope.find @name
@name + ' = Array.prototype.slice.call(arguments, ' + @index + ')'
name: @name.compile(o)
o.scope.find name
name + ' = Array.prototype.slice.call(arguments, ' + @index + ')'
compile_value: (o, name, index) ->
"Array.prototype.slice.call(" + @name + ', ' + @index + ')'
"Array.prototype.slice.call(" + name + ', ' + index + ')'
}