From 98f15d001f56e0218a4fe8dc7006a759e12f9ca3 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 11 Feb 2010 18:44:00 -0500 Subject: [PATCH] got pattern matching working --- lib/coffee_script/nodes.js | 7 ++++--- src/nodes.coffee | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/coffee_script/nodes.js b/lib/coffee_script/nodes.js index 892a6e8a..f5fa630d 100644 --- a/lib/coffee_script/nodes.js +++ b/lib/coffee_script/nodes.js @@ -764,7 +764,7 @@ // object literals to a value. Peeks at their properties to assign inner names. // See: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring compile_pattern_match: function compile_pattern_match(o) { - var __a, __b, access_class, assigns, i, obj, val, val_var; + var __a, __b, access_class, assigns, i, idx, obj, val, val_var; val_var = o.scope.free_variable(); assigns = [this.idt() + val_var + ' = ' + this.value.compile(o) + ';']; o.top = true; @@ -772,13 +772,14 @@ __a = this.variable.base.objects; for (i = 0; i < __a.length; i++) { obj = __a[i]; + idx = i; if (this.variable.is_object()) { __b = [obj.value, obj.variable.base]; obj = __b[0]; - i = __b[1]; + 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(new LiteralNode(i))])); + 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)])); assigns.push(new AssignNode(obj, val).compile(o)); } return assigns.join("\n"); diff --git a/src/nodes.coffee b/src/nodes.coffee index 6031f754..c33177c6 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -594,12 +594,13 @@ AssignNode: exports.AssignNode: inherit Node, { o.top: true o.as_statement: true for obj, i in @variable.base.objects - [obj, i]: [obj.value, obj.variable.base] if @variable.is_object() + 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: new LiteralNode(obj.compile_value(o, val_var, @variable.base.objects.indexOf(obj))) else - val: new ValueNode(new LiteralNode(val_var), [new access_class(new LiteralNode(i))]) + val: new ValueNode(new LiteralNode(val_var), [new access_class(idx)]) assigns.push(new AssignNode(obj, val).compile(o)) assigns.join("\n")