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

Self-compiler: array literals

This commit is contained in:
Jeremy Ashkenas 2010-02-09 21:01:25 -05:00
parent 001c915c21
commit c466537a26
2 changed files with 53 additions and 1 deletions

View file

@ -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.

View file

@ -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
}