From 38d1381c02a8e701e726ec3ec275dc6f07e84db0 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 11 Feb 2010 20:11:11 -0500 Subject: [PATCH] self-compiling pattern matching correctly --- lib/coffee_script/nodes.js | 17 +++++++++++++---- src/nodes.coffee | 8 +++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/coffee_script/nodes.js b/lib/coffee_script/nodes.js index f5fa630d..f2e9041d 100644 --- a/lib/coffee_script/nodes.js +++ b/lib/coffee_script/nodes.js @@ -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 diff --git a/src/nodes.coffee b/src/nodes.coffee index c33177c6..19f667b6 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -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 + ')' }