From c466537a26e782d76cab30a075a567792ad2a5a6 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 9 Feb 2010 21:01:25 -0500 Subject: [PATCH] Self-compiler: array literals --- lib/coffee_script/nodes.js | 33 ++++++++++++++++++++++++++++++++- src/nodes.coffee | 21 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/coffee_script/nodes.js b/lib/coffee_script/nodes.js index 3e2435ef..0ef4d16a 100644 --- a/lib/coffee_script/nodes.js +++ b/lib/coffee_script/nodes.js @@ -1,5 +1,5 @@ (function(){ - var AccessorNode, AssignNode, CallNode, CommentNode, Expressions, ExtendsNode, IndexNode, LiteralNode, Node, ObjectNode, RangeNode, ReturnNode, SliceNode, TAB, TRAILING_WHITESPACE, ThisNode, ValueNode, any, compact, del, dup, flatten, inherit, merge, statement; + var AccessorNode, ArrayNode, AssignNode, CallNode, CommentNode, Expressions, ExtendsNode, IndexNode, LiteralNode, Node, ObjectNode, RangeNode, ReturnNode, SliceNode, TAB, TRAILING_WHITESPACE, ThisNode, ValueNode, any, compact, del, dup, flatten, inherit, merge, statement; var __hasProp = Object.prototype.hasOwnProperty; process.mixin(require('./scope')); // The abstract base class for all CoffeeScript nodes. @@ -786,6 +786,37 @@ return '{\n' + props.join('') + '\n' + this.idt() + '}'; } })); + // An array literal. + ArrayNode = (exports.ArrayNode = inherit(Node, { + constructor: function constructor(objects) { + this.children = (this.objects = objects || []); + return this; + }, + compile_node: function compile_node(o) { + var __a, __b, code, ending, i, obj, objects; + o.indent = this.idt(1); + objects = (function() { + __a = []; __b = this.objects; + for (i = 0; i < __b.length; i++) { + obj = __b[i]; + __a.push((function() { + code = obj.compile(o); + if (obj instanceof CommentNode) { + return '\n' + code + '\n' + o.indent; + } else if (i === this.objects.length - 1) { + return code; + } else { + return code + ', '; + } + }).call(this)); + } + return __a; + }).call(this); + objects = objects.join(''); + ending = objects.indexOf('\n') >= 0 ? "\n" + this.idt() + ']' : ']'; + return '[' + objects + ending; + } + })); // Setting the value of a local variable, or the value of an object property. AssignNode = (exports.AssignNode = inherit(Node, { // Keep the identifier regex in sync with the Lexer. diff --git a/src/nodes.coffee b/src/nodes.coffee index 0f12954b..f3f12634 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -538,7 +538,28 @@ ObjectNode: exports.ObjectNode: inherit Node, { } +# An array literal. +ArrayNode: exports.ArrayNode: inherit Node, { + constructor: (objects) -> + @children: @objects: objects or [] + this + + compile_node: (o) -> + o.indent: @idt(1) + objects: for obj, i in @objects + code: obj.compile(o) + if obj instanceof CommentNode + '\n' + code + '\n' + o.indent + else if i is @objects.length - 1 + code + else + code + ', ' + objects: objects.join('') + ending: if objects.indexOf('\n') >= 0 then "\n" + @idt() + ']' else ']' + '[' + objects + ending + +}