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

determine @children dynamically based on attribute names, instead of manual bookkeeping

This commit is contained in:
gfxmonk 2010-05-10 20:58:01 +10:00
parent ee4e34bf6d
commit eb91f9922d
2 changed files with 257 additions and 140 deletions

View file

@ -1,15 +1,15 @@
(function(){
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _a, compact, del, flatten, helpers, literal, merge, statement, utility;
var __extends = function(child, parent) {
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _a, children, compact, del, flatten, helpers, literal, merge, statement, utility;
var __slice = Array.prototype.slice, __bind = function(func, obj, args) {
return function() {
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
};
}, __extends = function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
child.__superClass__ = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
}, __slice = Array.prototype.slice, __bind = function(func, obj, args) {
return function() {
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
};
};
// `nodes.coffee` contains all of the node classes for the syntax tree. Most
// nodes are created as the result of actions in the [grammar](grammar.html),
@ -46,6 +46,12 @@
return klass.prototype.is_pure_statement;
}
};
children = function children(klass) {
var child_attrs;
child_attrs = __slice.call(arguments, 1, arguments.length - 0);
klass.prototype._children_attributes = child_attrs;
return klass.prototype._children_attributes;
};
//### BaseNode
// The **BaseNode** is the abstract base class for all nodes in the syntax tree.
// Each subclass implements the `compile_node` method, which performs the
@ -119,18 +125,15 @@
// and returning true when the block finds a match. `contains` does not cross
// scope boundaries.
BaseNode.prototype.contains = function contains(block) {
var _b, _c, _d, node;
_c = this.children;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
node = _c[_b];
if (block(node)) {
return true;
}
if (node.contains && node.contains(block)) {
return true;
}
}
return false;
var contains;
contains = false;
this._traverse_children(false, __bind(function(node, attr, index) {
if (block(node)) {
contains = true;
return false;
}
}, this));
return contains;
};
// Is this node of a certain type, or does it contain the type?
BaseNode.prototype.contains_type = function contains_type(type) {
@ -147,18 +150,7 @@
};
// Perform an in-order traversal of the AST. Crosses scope boundaries.
BaseNode.prototype.traverse = function traverse(block) {
var _b, _c, _d, _e, node;
_b = []; _d = this.children;
for (_c = 0, _e = _d.length; _c < _e; _c++) {
node = _d[_c];
_b.push((function() {
block(node);
if (node.traverse) {
return node.traverse(block);
}
})());
}
return _b;
return this._traverse_children(true, block);
};
// `toString` representation of the node, for inspecting the parse tree.
// This is what `coffee --nodes` prints out.
@ -166,7 +158,7 @@
var _b, _c, _d, _e, child;
idt = idt || '';
return '\n' + idt + this.constructor.name + (function() {
_b = []; _d = this.children;
_b = []; _d = this.children();
for (_c = 0, _e = _d.length; _c < _e; _c++) {
child = _d[_c];
_b.push(child.toString(idt + TAB));
@ -174,12 +166,55 @@
return _b;
}).call(this).join('');
};
BaseNode.prototype.children = function children() {
var _children;
_children = [];
this._each_child(function(child) {
return _children.push(child);
});
return _children;
};
BaseNode.prototype._each_child = function _each_child(func) {
var _b, _c, _d, _e, _f, _g, attr, child, child_collection, i, result;
_c = this._children_attributes;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
attr = _c[_b];
child_collection = this[attr];
if (!((typeof child_collection !== "undefined" && child_collection !== null))) {
continue;
}
if (child_collection instanceof Array) {
i = 0;
_f = child_collection;
for (_e = 0, _g = _f.length; _e < _g; _e++) {
child = _f[_e];
result = func(child, attr, i);
if (result === false) {
return null;
}
i += 1;
}
} else {
func(child_collection, attr);
}
}
};
BaseNode.prototype._traverse_children = function _traverse_children(cross_scope, func) {
if (!(this._children_attributes)) {
return null;
}
return this._each_child(function(child, attr, i) {
func.apply(this, arguments);
if (child instanceof BaseNode) {
return child._traverse_children(cross_scope, func);
}
});
};
// Default implementations of the common node identification methods. Nodes
// will override these with custom logic, if needed.
BaseNode.prototype.unwrap = function unwrap() {
return this;
};
BaseNode.prototype.children = [];
BaseNode.prototype.is_statement = function is_statement() {
return false;
};
@ -197,7 +232,7 @@
// `if`, `switch`, or `try`, and so on...
exports.Expressions = (function() {
Expressions = function Expressions(nodes) {
this.children = (this.expressions = compact(flatten(nodes || [])));
this.expressions = compact(flatten(nodes || []));
return this;
};
__extends(Expressions, BaseNode);
@ -226,7 +261,7 @@
};
// Make a copy of this node.
Expressions.prototype.copy = function copy() {
return new Expressions(this.children.slice());
return new Expressions(this.Expressions.slice());
};
// An Expressions node does not return its entire body, rather it
// ensures that the final expression is returned.
@ -317,6 +352,7 @@
}
return new Expressions(nodes);
};
children(Expressions, 'expressions');
statement(Expressions);
//### LiteralNode
// Literals are static values that can be passed through directly into
@ -350,7 +386,7 @@
// make sense.
exports.ReturnNode = (function() {
ReturnNode = function ReturnNode(expression) {
this.children = [(this.expression = expression)];
this.expression = expression;
return this;
};
__extends(ReturnNode, BaseNode);
@ -372,12 +408,14 @@
return ReturnNode;
})();
statement(ReturnNode, true);
children(ReturnNode, 'expression');
//### ValueNode
// A value, variable or literal or parenthesized, indexed or dotted into,
// or vanilla.
exports.ValueNode = (function() {
ValueNode = function ValueNode(base, properties) {
this.children = flatten([(this.base = base), (this.properties = (properties || []))]);
this.base = base;
this.properties = (properties || []);
return this;
};
__extends(ValueNode, BaseNode);
@ -386,7 +424,6 @@
// Add a property access to the list.
ValueNode.prototype.push = function push(prop) {
this.properties.push(prop);
this.children.push(prop);
return this;
};
ValueNode.prototype.has_properties = function has_properties() {
@ -464,6 +501,7 @@
};
return ValueNode;
})();
children(ValueNode, 'base', 'properties');
//### CommentNode
// CoffeeScript passes through comments as JavaScript comments at the
// same position.
@ -491,7 +529,7 @@
this.is_new = false;
this.is_super = variable === 'super';
this.variable = this.is_super ? null : variable;
this.children = compact(flatten([this.variable, (this.args = (args || []))]));
this.args = (args || []);
this.compile_splat_arguments = __bind(SplatNode.compile_mixed_array, this, [this.args]);
return this;
};
@ -558,13 +596,16 @@
};
return CallNode;
})();
children(CallNode, 'variable', 'args');
//### CurryNode
// Binds a context object and a list of arguments to a function,
// returning the bound function. After ECMAScript 5, Prototype.js, and
// Underscore's `bind` functions.
exports.CurryNode = (function() {
CurryNode = function CurryNode(meth, args) {
this.children = flatten([(this.meth = meth), (this.context = args[0]), (this.args = (args.slice(1) || []))]);
this.meth = meth;
this.context = args[0];
this.args = (args.slice(1) || []);
this.compile_splat_arguments = __bind(SplatNode.compile_mixed_array, this, [this.args]);
return this;
};
@ -588,13 +629,15 @@
};
return CurryNode;
}).apply(this, arguments);
children(CurryNode, 'meth', 'context', 'args');
//### ExtendsNode
// Node to extend an object's prototype with an ancestor object.
// After `goog.inherits` from the
// [Closure Library](http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.html).
exports.ExtendsNode = (function() {
ExtendsNode = function ExtendsNode(child, parent) {
this.children = [(this.child = child), (this.parent = parent)];
this.child = child;
this.parent = parent;
return this;
};
__extends(ExtendsNode, BaseNode);
@ -606,12 +649,13 @@
};
return ExtendsNode;
})();
children(ExtendsNode, 'child', 'parent');
//### AccessorNode
// A `.` accessor into a property of a value, or the `::` shorthand for
// an accessor into the object's prototype.
exports.AccessorNode = (function() {
AccessorNode = function AccessorNode(name, tag) {
this.children = [(this.name = name)];
this.name = name;
this.prototype = tag === 'prototype';
this.soak_node = tag === 'soak';
this;
@ -625,11 +669,12 @@
};
return AccessorNode;
})();
children(AccessorNode, 'name');
//### IndexNode
// A `[ ... ]` indexed accessor into an array or object.
exports.IndexNode = (function() {
IndexNode = function IndexNode(index, tag) {
this.children = [(this.index = index)];
this.index = index;
this.soak_node = tag === 'soak';
return this;
};
@ -641,13 +686,15 @@
};
return IndexNode;
})();
children(IndexNode, 'index');
//### RangeNode
// A range literal. Ranges can be used to extract portions (slices) of arrays,
// to specify a range for comprehensions, or as a value, to be expanded into the
// corresponding array of integers at runtime.
exports.RangeNode = (function() {
RangeNode = function RangeNode(from, to, exclusive) {
this.children = [(this.from = from), (this.to = to)];
this.from = from;
this.to = to;
this.exclusive = !!exclusive;
return this;
};
@ -697,13 +744,14 @@
};
return RangeNode;
})();
children(RangeNode, 'from', 'to');
//### SliceNode
// An array slice literal. Unlike JavaScript's `Array#slice`, the second parameter
// specifies the index of the end of the slice, just as the first parameter
// is the index of the beginning.
exports.SliceNode = (function() {
SliceNode = function SliceNode(range) {
this.children = [(this.range = range)];
this.range = range;
this;
return this;
};
@ -717,11 +765,12 @@
};
return SliceNode;
})();
children(SliceNode, 'range');
//### ObjectNode
// An object literal, nothing fancy.
exports.ObjectNode = (function() {
ObjectNode = function ObjectNode(props) {
this.children = (this.objects = (this.properties = props || []));
this.objects = (this.properties = props || []);
return this;
};
__extends(ObjectNode, BaseNode);
@ -767,11 +816,12 @@
};
return ObjectNode;
})();
children(ObjectNode, 'properties');
//### ArrayNode
// An array literal.
exports.ArrayNode = (function() {
ArrayNode = function ArrayNode(objects) {
this.children = (this.objects = objects || []);
this.objects = objects || [];
this.compile_splat_literal = __bind(SplatNode.compile_mixed_array, this, [this.objects]);
return this;
};
@ -803,11 +853,14 @@
};
return ArrayNode;
})();
children(ArrayNode, 'objects');
//### ClassNode
// The CoffeeScript class definition.
exports.ClassNode = (function() {
ClassNode = function ClassNode(variable, parent, props) {
this.children = compact(flatten([(this.variable = variable), (this.parent = parent), (this.properties = props || [])]));
this.variable = variable;
this.parent = parent;
this.properties = props || [];
this.returns = false;
return this;
};
@ -862,12 +915,14 @@
return ClassNode;
})();
statement(ClassNode);
children(ClassNode, 'variable', 'parent', 'properties');
//### AssignNode
// The **AssignNode** is used to assign a local variable to value, or to set the
// property of an object -- including within object literals.
exports.AssignNode = (function() {
AssignNode = function AssignNode(variable, value, context) {
this.children = [(this.variable = variable), (this.value = value)];
this.variable = variable;
this.value = value;
this.context = context;
return this;
};
@ -994,6 +1049,7 @@
};
return AssignNode;
})();
children(AssignNode, 'variable', 'value');
//### CodeNode
// A function definition. This is the only node that creates a new Scope.
// When for the purposes of walking the contents of a function body, the CodeNode
@ -1068,27 +1124,18 @@
CodeNode.prototype.top_sensitive = function top_sensitive() {
return true;
};
// When traversing (for printing or inspecting), return the real children of
// the function -- the parameters and body of expressions.
CodeNode.prototype.real_children = function real_children() {
return flatten([this.params, this.body.expressions]);
};
// Custom `traverse` implementation that uses the `real_children`.
CodeNode.prototype.traverse = function traverse(block) {
var _b, _c, _d, _e, child;
block(this);
_b = []; _d = this.real_children();
for (_c = 0, _e = _d.length; _c < _e; _c++) {
child = _d[_c];
_b.push(child.traverse(block));
// Short-circuit _traverse_children method to prevent it from crossing scope boundaries
// unless cross_scope is true
CodeNode.prototype._traverse_children = function _traverse_children(cross_scope, func) {
if (cross_scope) {
return CodeNode.__superClass__._traverse_children.call(this, cross_scope, func);
}
return _b;
};
CodeNode.prototype.toString = function toString(idt) {
var _b, _c, _d, _e, child, children;
var _b, _c, _d, _e, child;
idt = idt || '';
children = (function() {
_b = []; _d = this.real_children();
_b = []; _d = this.children();
for (_c = 0, _e = _d.length; _c < _e; _c++) {
child = _d[_c];
_b.push(child.toString(idt + TAB));
@ -1099,6 +1146,7 @@
};
return CodeNode;
})();
children(CodeNode, 'params', 'body');
//### SplatNode
// A splat, either as a parameter to a function, an argument to a call,
// or as part of a destructuring assignment.
@ -1107,7 +1155,7 @@
if (!(name.compile)) {
name = literal(name);
}
this.children = [(this.name = name)];
this.name = name;
return this;
};
__extends(SplatNode, BaseNode);
@ -1170,6 +1218,7 @@
};
return SplatNode;
}).call(this);
children(SplatNode, 'name');
//### WhileNode
// A while loop, the only sort of low-level loop exposed by CoffeeScript. From
// it, all other loops can be manufactured. Useful in cases where you need more
@ -1179,13 +1228,13 @@
if (opts && opts.invert) {
condition = new OpNode('!', condition);
}
this.children = [(this.condition = condition)];
this.condition = condition;
this.guard = opts && opts.guard;
return this;
};
__extends(WhileNode, BaseNode);
WhileNode.prototype.add_body = function add_body(body) {
this.children.push((this.body = body));
this.body = body;
return this;
};
WhileNode.prototype.make_return = function make_return() {
@ -1227,13 +1276,15 @@
return WhileNode;
})();
statement(WhileNode);
children(WhileNode, 'condition', 'guard', 'body');
//### OpNode
// Simple Arithmetic and logical operations. Performs some conversion from
// CoffeeScript operations into their JavaScript equivalents.
exports.OpNode = (function() {
OpNode = function OpNode(operator, first, second, flip) {
this.constructor.name += ' ' + operator;
this.children = compact([(this.first = first), (this.second = second)]);
this.first = first;
this.second = second;
this.operator = this.CONVERSIONS[operator] || operator;
this.flip = !!flip;
return this;
@ -1329,11 +1380,14 @@
};
return OpNode;
})();
children(OpNode, 'first', 'second');
//### TryNode
// A classic *try/catch/finally* block.
exports.TryNode = (function() {
TryNode = function TryNode(attempt, error, recovery, ensure) {
this.children = compact([(this.attempt = attempt), (this.recovery = recovery), (this.ensure = ensure)]);
this.attempt = attempt;
this.recovery = recovery;
this.ensure = ensure;
this.error = error;
this;
return this;
@ -1363,11 +1417,12 @@
return TryNode;
})();
statement(TryNode);
children(TryNode, 'attempt', 'recovery', 'ensure');
//### ThrowNode
// Simple node to throw an exception.
exports.ThrowNode = (function() {
ThrowNode = function ThrowNode(expression) {
this.children = [(this.expression = expression)];
this.expression = expression;
return this;
};
__extends(ThrowNode, BaseNode);
@ -1381,13 +1436,14 @@
return ThrowNode;
})();
statement(ThrowNode);
children(ThrowNode, 'expression');
//### ExistenceNode
// Checks a variable for existence -- not *null* and not *undefined*. This is
// similar to `.nil?` in Ruby, and avoids having to consult a JavaScript truth
// table.
exports.ExistenceNode = (function() {
ExistenceNode = function ExistenceNode(expression) {
this.children = [(this.expression = expression)];
this.expression = expression;
return this;
};
__extends(ExistenceNode, BaseNode);
@ -1414,6 +1470,7 @@
};
return ExistenceNode;
}).call(this);
children(ExistenceNode, 'expression');
//### ParentheticalNode
// An extra set of parentheses, specified explicitly in the source. At one time
// we tried to clean up the results by detecting and removing redundant
@ -1421,7 +1478,7 @@
// Parentheses are a good way to force any statement to become an expression.
exports.ParentheticalNode = (function() {
ParentheticalNode = function ParentheticalNode(expression) {
this.children = [(this.expression = expression)];
this.expression = expression;
return this;
};
__extends(ParentheticalNode, BaseNode);
@ -1449,6 +1506,7 @@
};
return ParentheticalNode;
})();
children(ParentheticalNode, 'expression');
//### ForNode
// CoffeeScript's replacement for the *for* loop is our array and object
// comprehensions, that compile into *for* loops here. They also act as an
@ -1475,7 +1533,6 @@
if (this.index instanceof ValueNode) {
throw new Error('index cannot be a pattern matching expression');
}
this.children = compact([this.body, this.source, this.guard]);
this.returns = false;
return this;
};
@ -1566,6 +1623,7 @@
return ForNode;
})();
statement(ForNode);
children(ForNode, 'body', 'source', 'guard');
//### IfNode
// *If/else* statements. Our *switch/when* will be compiled into this. Acts as an
// expression by pushing down requested returns to the last line of each clause.
@ -1576,7 +1634,6 @@
this.condition = condition;
this.body = body;
this.else_body = null;
this.populate_children();
this.tags = tags || {};
if (this.condition instanceof Array) {
this.multiple = true;
@ -1588,10 +1645,6 @@
return this;
};
__extends(IfNode, BaseNode);
IfNode.prototype.populate_children = function populate_children() {
this.children = compact(flatten([this.condition, this.body, this.else_body]));
return this.children;
};
IfNode.prototype.body_node = function body_node() {
return this.body == undefined ? undefined : this.body.unwrap();
};
@ -1611,12 +1664,11 @@
// Rewrite a chain of **IfNodes** with their switch condition for equality.
// Ensure that the switch expression isn't evaluated more than once.
IfNode.prototype.rewrite_switch = function rewrite_switch(o) {
var _b, _c, _d, assigner, cond, i, variable;
assigner = this.switch_subject;
var _b, _c, _d, cond, i, variable;
this.assigner = this.switch_subject;
if (!((this.switch_subject.unwrap() instanceof LiteralNode))) {
variable = literal(o.scope.free_variable());
assigner = new AssignNode(variable, this.switch_subject);
this.children.push(assigner);
this.assigner = new AssignNode(variable, this.switch_subject);
this.switch_subject = variable;
}
this.condition = (function() {
@ -1624,11 +1676,11 @@
_b = []; _c = this.condition;
for (i = 0, _d = _c.length; i < _d; i++) {
cond = _c[i];
_b.push(new OpNode('==', (i === 0 ? assigner : this.switch_subject), cond));
_b.push(new OpNode('==', (i === 0 ? this.assigner : this.switch_subject), cond));
}
return _b;
} else {
return new OpNode('==', assigner, this.condition);
return new OpNode('==', this.assigner, this.condition);
}
}).call(this);
if (this.is_chain) {
@ -1645,7 +1697,6 @@
} else {
this.is_chain = else_body instanceof IfNode;
this.else_body = this.ensure_expressions(else_body);
this.populate_children();
}
return this;
};
@ -1717,6 +1768,7 @@
};
return IfNode;
})();
children(IfNode, 'condition', 'body', 'else_body', 'assigner');
// Faux-Nodes
// ----------
//### PushNode

View file

@ -24,6 +24,9 @@ statement: (klass, only) ->
klass::is_statement: -> true
(klass::is_pure_statement: -> true) if only
children: (klass, child_attrs...) ->
klass::_children_attributes: child_attrs
#### BaseNode
# The **BaseNode** is the abstract base class for all nodes in the syntax tree.
@ -90,10 +93,12 @@ exports.BaseNode: class BaseNode
# and returning true when the block finds a match. `contains` does not cross
# scope boundaries.
contains: (block) ->
for node in @children
return true if block(node)
return true if node.contains and node.contains block
false
contains: false
@_traverse_children false, (node, attr, index) =>
if block(node)
contains: true
return false
return contains
# Is this node of a certain type, or does it contain the type?
contains_type: (type) ->
@ -105,21 +110,41 @@ exports.BaseNode: class BaseNode
@is_pure_statement() or @contains (n) -> n.is_pure_statement()
# Perform an in-order traversal of the AST. Crosses scope boundaries.
traverse: (block) ->
for node in @children
block node
node.traverse block if node.traverse
traverse: (block) -> @_traverse_children true, block
# `toString` representation of the node, for inspecting the parse tree.
# This is what `coffee --nodes` prints out.
toString: (idt) ->
idt: or ''
'\n' + idt + @constructor.name + (child.toString(idt + TAB) for child in @children).join('')
'\n' + idt + @constructor.name + (child.toString(idt + TAB) for child in @children()).join('')
children: ->
_children: []
@_each_child (child) -> _children.push(child)
_children
_each_child: (func) ->
for attr in @_children_attributes
child_collection: this[attr]
continue unless child_collection?
if child_collection instanceof Array
i: 0
for child in child_collection
result: func(child, attr, i)
return if result is false
i += 1
else
func(child_collection, attr)
_traverse_children: (cross_scope, func) ->
return unless @_children_attributes
@_each_child (child, attr, i) ->
func.apply(this, arguments)
child._traverse_children(cross_scope, func) if child instanceof BaseNode
# Default implementations of the common node identification methods. Nodes
# will override these with custom logic, if needed.
unwrap: -> this
children: []
is_statement: -> false
is_pure_statement: -> false
top_sensitive: -> false
@ -132,7 +157,7 @@ exports.BaseNode: class BaseNode
exports.Expressions: class Expressions extends BaseNode
constructor: (nodes) ->
@children: @expressions: compact flatten nodes or []
@expressions: compact flatten nodes or []
# Tack an expression on to the end of this expression list.
push: (node) ->
@ -155,7 +180,7 @@ exports.Expressions: class Expressions extends BaseNode
# Make a copy of this node.
copy: ->
new Expressions @children.slice()
new Expressions @Expressions.slice()
# An Expressions node does not return its entire body, rather it
# ensures that the final expression is returned.
@ -206,6 +231,7 @@ Expressions.wrap: (nodes) ->
return nodes[0] if nodes.length is 1 and nodes[0] instanceof Expressions
new Expressions(nodes)
children Expressions, 'expressions'
statement Expressions
#### LiteralNode
@ -239,7 +265,7 @@ exports.LiteralNode: class LiteralNode extends BaseNode
exports.ReturnNode: class ReturnNode extends BaseNode
constructor: (expression) ->
@children: [@expression: expression]
@expression: expression
top_sensitive: ->
true
@ -252,6 +278,7 @@ exports.ReturnNode: class ReturnNode extends BaseNode
"${@tab}return ${@expression.compile(o)};"
statement ReturnNode, true
children ReturnNode, 'expression'
#### ValueNode
@ -263,12 +290,12 @@ exports.ValueNode: class ValueNode extends BaseNode
# A **ValueNode** has a base and a list of property accesses.
constructor: (base, properties) ->
@children: flatten [@base: base, @properties: (properties or [])]
@base: base
@properties: (properties or [])
# Add a property access to the list.
push: (prop) ->
@properties.push(prop)
@children.push(prop)
this
has_properties: ->
@ -327,6 +354,8 @@ exports.ValueNode: class ValueNode extends BaseNode
if op and soaked then "($complete)" else complete
children ValueNode, 'base', 'properties'
#### CommentNode
# CoffeeScript passes through comments as JavaScript comments at the
@ -355,7 +384,7 @@ exports.CallNode: class CallNode extends BaseNode
@is_new: false
@is_super: variable is 'super'
@variable: if @is_super then null else variable
@children: compact flatten [@variable, @args: (args or [])]
@args: (args or [])
@compile_splat_arguments: SplatNode.compile_mixed_array <- @, @args
# Tag this invocation as creating a new instance.
@ -398,6 +427,8 @@ exports.CallNode: class CallNode extends BaseNode
meth: "($temp = ${ @variable.source })${ @variable.last }"
"${@prefix()}${meth}.apply($obj, ${ @compile_splat_arguments(o) })"
children CallNode, 'variable', 'args'
#### CurryNode
# Binds a context object and a list of arguments to a function,
@ -406,7 +437,9 @@ exports.CallNode: class CallNode extends BaseNode
exports.CurryNode: class CurryNode extends CallNode
constructor: (meth, args) ->
@children: flatten [@meth: meth, @context: args[0], @args: (args.slice(1) or [])]
@meth: meth
@context: args[0]
@args: (args.slice(1) or [])
@compile_splat_arguments: SplatNode.compile_mixed_array <- @, @args
arguments: (o) ->
@ -419,6 +452,7 @@ exports.CurryNode: class CurryNode extends CallNode
ref: new ValueNode literal utility 'bind'
(new CallNode(ref, [@meth, @context, literal(@arguments(o))])).compile o
children CurryNode, 'meth', 'context', 'args'
#### ExtendsNode
@ -428,13 +462,16 @@ exports.CurryNode: class CurryNode extends CallNode
exports.ExtendsNode: class ExtendsNode extends BaseNode
constructor: (child, parent) ->
@children: [@child: child, @parent: parent]
@child: child
@parent: parent
# Hooks one constructor into another's prototype chain.
compile_node: (o) ->
ref: new ValueNode literal utility 'extends'
(new CallNode ref, [@child, @parent]).compile o
children ExtendsNode, 'child', 'parent'
#### AccessorNode
# A `.` accessor into a property of a value, or the `::` shorthand for
@ -442,7 +479,7 @@ exports.ExtendsNode: class ExtendsNode extends BaseNode
exports.AccessorNode: class AccessorNode extends BaseNode
constructor: (name, tag) ->
@children: [@name: name]
@name: name
@prototype:tag is 'prototype'
@soak_node: tag is 'soak'
this
@ -451,19 +488,23 @@ exports.AccessorNode: class AccessorNode extends BaseNode
proto_part: if @prototype then 'prototype.' else ''
".$proto_part${@name.compile(o)}"
children AccessorNode, 'name'
#### IndexNode
# A `[ ... ]` indexed accessor into an array or object.
exports.IndexNode: class IndexNode extends BaseNode
constructor: (index, tag) ->
@children: [@index: index]
@index: index
@soak_node: tag is 'soak'
compile_node: (o) ->
idx: @index.compile o
"[$idx]"
children IndexNode, 'index'
#### RangeNode
# A range literal. Ranges can be used to extract portions (slices) of arrays,
@ -472,7 +513,8 @@ exports.IndexNode: class IndexNode extends BaseNode
exports.RangeNode: class RangeNode extends BaseNode
constructor: (from, to, exclusive) ->
@children: [@from: from, @to: to]
@from: from
@to: to
@exclusive: !!exclusive
# Compiles the range's source variables -- where it starts and where it ends.
@ -505,6 +547,9 @@ exports.RangeNode: class RangeNode extends BaseNode
arr: Expressions.wrap([new ForNode(body, {source: (new ValueNode(this))}, literal(name))])
(new ParentheticalNode(new CallNode(new CodeNode([], arr.make_return())))).compile(o)
children RangeNode, 'from', 'to'
#### SliceNode
# An array slice literal. Unlike JavaScript's `Array#slice`, the second parameter
@ -513,7 +558,7 @@ exports.RangeNode: class RangeNode extends BaseNode
exports.SliceNode: class SliceNode extends BaseNode
constructor: (range) ->
@children: [@range: range]
@range: range
this
compile_node: (o) ->
@ -522,13 +567,15 @@ exports.SliceNode: class SliceNode extends BaseNode
plus_part: if @range.exclusive then '' else ' + 1'
".slice($from, $to$plus_part)"
children SliceNode, 'range'
#### ObjectNode
# An object literal, nothing fancy.
exports.ObjectNode: class ObjectNode extends BaseNode
constructor: (props) ->
@children: @objects: @properties: props or []
@objects: @properties: props or []
# All the mucking about with commas is to make sure that CommentNodes and
# AssignNodes get interleaved correctly, with no trailing commas or
@ -548,13 +595,15 @@ exports.ObjectNode: class ObjectNode extends BaseNode
inner: if props then '\n' + props + '\n' + @idt() else ''
"{$inner}"
children ObjectNode, 'properties'
#### ArrayNode
# An array literal.
exports.ArrayNode: class ArrayNode extends BaseNode
constructor: (objects) ->
@children: @objects: objects or []
@objects: objects or []
@compile_splat_literal: SplatNode.compile_mixed_array <- @, @objects
compile_node: (o) ->
@ -576,6 +625,8 @@ exports.ArrayNode: class ArrayNode extends BaseNode
else
"[$objects]"
children ArrayNode, 'objects'
#### ClassNode
# The CoffeeScript class definition.
@ -584,7 +635,9 @@ exports.ClassNode: class ClassNode extends BaseNode
# Initialize a **ClassNode** with its name, an optional superclass, and a
# list of prototype property assignments.
constructor: (variable, parent, props) ->
@children: compact flatten [@variable: variable, @parent: parent, @properties: props or []]
@variable: variable
@parent: parent
@properties: props or []
@returns: false
make_return: ->
@ -628,6 +681,7 @@ exports.ClassNode: class ClassNode extends BaseNode
"$construct$extension$props$returns"
statement ClassNode
children ClassNode, 'variable', 'parent', 'properties'
#### AssignNode
@ -640,7 +694,8 @@ exports.AssignNode: class AssignNode extends BaseNode
LEADING_DOT: /^\.(prototype\.)?/
constructor: (variable, value, context) ->
@children: [@variable: variable, @value: value]
@variable: variable
@value: value
@context: context
top_sensitive: ->
@ -727,6 +782,8 @@ exports.AssignNode: class AssignNode extends BaseNode
val: @value.compile(o)
"${name}.splice.apply($name, [$from, $to].concat($val))"
children AssignNode, 'variable', 'value'
#### CodeNode
# A function definition. This is the only node that creates a new Scope.
@ -781,21 +838,17 @@ exports.CodeNode: class CodeNode extends BaseNode
top_sensitive: ->
true
# When traversing (for printing or inspecting), return the real children of
# the function -- the parameters and body of expressions.
real_children: ->
flatten [@params, @body.expressions]
# Custom `traverse` implementation that uses the `real_children`.
traverse: (block) ->
block this
child.traverse block for child in @real_children()
# Short-circuit _traverse_children method to prevent it from crossing scope boundaries
# unless cross_scope is true
_traverse_children: (cross_scope, func) -> super(cross_scope, func) if cross_scope
toString: (idt) ->
idt: or ''
children: (child.toString(idt + TAB) for child in @real_children()).join('')
children: (child.toString(idt + TAB) for child in @children()).join('')
"\n$idt$children"
children CodeNode, 'params', 'body'
#### SplatNode
# A splat, either as a parameter to a function, an argument to a call,
@ -804,7 +857,7 @@ exports.SplatNode: class SplatNode extends BaseNode
constructor: (name) ->
name: literal(name) unless name.compile
@children: [@name: name]
@name: name
compile_node: (o) ->
if @index? then @compile_param(o) else @name.compile(o)
@ -847,6 +900,8 @@ exports.SplatNode: class SplatNode extends BaseNode
i: + 1
args.join('')
children SplatNode, 'name'
#### WhileNode
# A while loop, the only sort of low-level loop exposed by CoffeeScript. From
@ -856,11 +911,11 @@ exports.WhileNode: class WhileNode extends BaseNode
constructor: (condition, opts) ->
condition: new OpNode('!', condition) if opts and opts.invert
@children:[@condition: condition]
@condition: condition
@guard: opts and opts.guard
add_body: (body) ->
@children.push @body: body
@body: body
this
make_return: ->
@ -893,6 +948,7 @@ exports.WhileNode: class WhileNode extends BaseNode
"$pre {\n${ @body.compile(o) }\n$@tab}\n$post"
statement WhileNode
children WhileNode, 'condition', 'guard', 'body'
#### OpNode
@ -918,7 +974,8 @@ exports.OpNode: class OpNode extends BaseNode
constructor: (operator, first, second, flip) ->
@constructor.name: + ' ' + operator
@children: compact [@first: first, @second: second]
@first: first
@second: second
@operator: @CONVERSIONS[operator] or operator
@flip: !!flip
@ -970,13 +1027,17 @@ exports.OpNode: class OpNode extends BaseNode
parts: parts.reverse() if @flip
parts.join('')
children OpNode, 'first', 'second'
#### TryNode
# A classic *try/catch/finally* block.
exports.TryNode: class TryNode extends BaseNode
constructor: (attempt, error, recovery, ensure) ->
@children: compact [@attempt: attempt, @recovery: recovery, @ensure: ensure]
@attempt: attempt
@recovery: recovery
@ensure: ensure
@error: error
this
@ -997,6 +1058,7 @@ exports.TryNode: class TryNode extends BaseNode
"${@tab}try {\n$attempt_part\n$@tab}$catch_part$finally_part"
statement TryNode
children TryNode, 'attempt', 'recovery', 'ensure'
#### ThrowNode
@ -1004,7 +1066,7 @@ statement TryNode
exports.ThrowNode: class ThrowNode extends BaseNode
constructor: (expression) ->
@children: [@expression: expression]
@expression: expression
# A **ThrowNode** is already a return, of sorts...
make_return: ->
@ -1014,6 +1076,7 @@ exports.ThrowNode: class ThrowNode extends BaseNode
"${@tab}throw ${@expression.compile(o)};"
statement ThrowNode
children ThrowNode, 'expression'
#### ExistenceNode
@ -1023,7 +1086,7 @@ statement ThrowNode
exports.ExistenceNode: class ExistenceNode extends BaseNode
constructor: (expression) ->
@children: [@expression: expression]
@expression: expression
compile_node: (o) ->
ExistenceNode.compile_test(o, @expression)
@ -1038,6 +1101,8 @@ exports.ExistenceNode: class ExistenceNode extends BaseNode
[first, second]: [first.compile(o), second.compile(o)]
"(typeof $first !== \"undefined\" && $second !== null)"
children ExistenceNode, 'expression'
#### ParentheticalNode
# An extra set of parentheses, specified explicitly in the source. At one time
@ -1048,7 +1113,7 @@ exports.ExistenceNode: class ExistenceNode extends BaseNode
exports.ParentheticalNode: class ParentheticalNode extends BaseNode
constructor: (expression) ->
@children: [@expression: expression]
@expression: expression
is_statement: ->
@expression.is_statement()
@ -1063,6 +1128,8 @@ exports.ParentheticalNode: class ParentheticalNode extends BaseNode
code: code.substr(o, l-1) if code.substr(l-1, 1) is ';'
if @expression instanceof AssignNode then code else "($code)"
children ParentheticalNode, 'expression'
#### ForNode
# CoffeeScript's replacement for the *for* loop is our array and object
@ -1085,7 +1152,6 @@ exports.ForNode: class ForNode extends BaseNode
[@name, @index]: [@index, @name] if @object
@pattern: @name instanceof ValueNode
throw new Error('index cannot be a pattern matching expression') if @index instanceof ValueNode
@children: compact [@body, @source, @guard]
@returns: false
top_sensitive: ->
@ -1146,6 +1212,7 @@ exports.ForNode: class ForNode extends BaseNode
"$set_result${source_part}for ($for_part) {\n$var_part$body\n$@tab$close$return_result"
statement ForNode
children ForNode, 'body', 'source', 'guard'
#### IfNode
@ -1160,15 +1227,11 @@ exports.IfNode: class IfNode extends BaseNode
@condition: condition
@body: body
@else_body: null
@populate_children()
@tags: tags or {}
@multiple: true if @condition instanceof Array
@condition: new OpNode('!', new ParentheticalNode(@condition)) if @tags.invert
@is_chain: false
populate_children: ->
@children: compact flatten [@condition, @body, @else_body]
body_node: -> @body?.unwrap()
else_body_node: -> @else_body?.unwrap()
@ -1185,17 +1248,16 @@ exports.IfNode: class IfNode extends BaseNode
# Rewrite a chain of **IfNodes** with their switch condition for equality.
# Ensure that the switch expression isn't evaluated more than once.
rewrite_switch: (o) ->
assigner: @switch_subject
@assigner: @switch_subject
unless (@switch_subject.unwrap() instanceof LiteralNode)
variable: literal(o.scope.free_variable())
assigner: new AssignNode(variable, @switch_subject)
@children.push(assigner)
@assigner: new AssignNode(variable, @switch_subject)
@switch_subject: variable
@condition: if @multiple
for cond, i in @condition
new OpNode('==', (if i is 0 then assigner else @switch_subject), cond)
new OpNode('==', (if i is 0 then @assigner else @switch_subject), cond)
else
new OpNode('==', assigner, @condition)
new OpNode('==', @assigner, @condition)
@else_body_node().switches_over(@switch_subject) if @is_chain
# prevent this rewrite from happening again
@switch_subject: undefined
@ -1208,7 +1270,6 @@ exports.IfNode: class IfNode extends BaseNode
else
@is_chain: else_body instanceof IfNode
@else_body: @ensure_expressions else_body
@populate_children()
this
# The **IfNode** only compiles into a statement if either of its bodies needs
@ -1257,6 +1318,10 @@ exports.IfNode: class IfNode extends BaseNode
else_part: if @else_body then @else_body_node().compile(o) else 'null'
"$if_part : $else_part"
children IfNode, 'condition', 'body', 'else_body', 'assigner'
# Faux-Nodes
# ----------