2010-01-30 23:17:36 -05:00
|
|
|
(function(){
|
2010-05-31 19:38:45 -04:00
|
|
|
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, include, index_of, literal, merge, starts, utility;
|
2010-05-19 15:50:29 -04:00
|
|
|
var __extends = function(child, parent) {
|
2010-05-14 23:40:04 -04:00
|
|
|
var ctor = function(){ };
|
|
|
|
ctor.prototype = parent.prototype;
|
|
|
|
child.__superClass__ = parent.prototype;
|
|
|
|
child.prototype = new ctor();
|
|
|
|
child.prototype.constructor = child;
|
2010-05-19 15:50:29 -04:00
|
|
|
}, __slice = Array.prototype.slice, __bind = function(func, obj, args) {
|
2010-05-14 23:40:04 -04:00
|
|
|
return function() {
|
|
|
|
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
|
|
|
};
|
2010-03-30 19:27:38 -04:00
|
|
|
};
|
2010-03-07 16:41:06 -05:00
|
|
|
// `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),
|
|
|
|
// but some are created by other nodes as a method of code generation. To convert
|
|
|
|
// the syntax tree into a string of JavaScript code, call `compile()` on the root.
|
|
|
|
// Set up for both **Node.js** and the browser, by
|
2010-03-31 00:04:14 -04:00
|
|
|
// including the [Scope](scope.html) class and the [helper](helpers.html) functions.
|
2010-03-09 21:24:30 -05:00
|
|
|
if ((typeof process !== "undefined" && process !== null)) {
|
2010-03-15 23:39:46 -04:00
|
|
|
Scope = require('./scope').Scope;
|
|
|
|
helpers = require('./helpers').helpers;
|
2010-03-09 21:24:30 -05:00
|
|
|
} else {
|
|
|
|
this.exports = this;
|
2010-03-16 23:18:54 -04:00
|
|
|
helpers = this.helpers;
|
|
|
|
Scope = this.Scope;
|
2010-03-09 21:24:30 -05:00
|
|
|
}
|
2010-03-31 00:04:14 -04:00
|
|
|
// Import the helpers we plan to use.
|
2010-04-25 22:29:43 -04:00
|
|
|
_a = helpers;
|
|
|
|
compact = _a.compact;
|
|
|
|
flatten = _a.flatten;
|
|
|
|
merge = _a.merge;
|
|
|
|
del = _a.del;
|
2010-05-25 02:15:46 -04:00
|
|
|
include = _a.include;
|
2010-05-14 23:40:04 -04:00
|
|
|
index_of = _a.index_of;
|
2010-05-31 19:38:45 -04:00
|
|
|
starts = _a.starts;
|
2010-03-07 17:31:39 -05:00
|
|
|
//### BaseNode
|
2010-03-07 16:41:06 -05:00
|
|
|
// The **BaseNode** is the abstract base class for all nodes in the syntax tree.
|
|
|
|
// Each subclass implements the `compile_node` method, which performs the
|
|
|
|
// code generation for that node. To compile a node to JavaScript,
|
|
|
|
// call `compile` on it, which wraps `compile_node` in some generic extra smarts,
|
|
|
|
// to know when the generated code needs to be wrapped up in a closure.
|
|
|
|
// An options hash is passed and cloned throughout, containing information about
|
|
|
|
// the environment from higher in the tree (such as if a returned value is
|
|
|
|
// being requested by the surrounding function), information about the current
|
|
|
|
// scope, and indentation level.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.BaseNode = (function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
BaseNode = function() { };
|
2010-03-07 16:41:06 -05:00
|
|
|
// Common logic for determining whether to wrap this node in a closure before
|
|
|
|
// compiling it, or to compile directly. We need to wrap if this node is a
|
|
|
|
// *statement*, and it's not a *pure_statement*, and we're not at
|
2010-02-27 19:19:53 -05:00
|
|
|
// the top level of a block (which would be unnecessary), and we haven't
|
2010-03-07 16:41:06 -05:00
|
|
|
// already been asked to return the result (because statements know how to
|
|
|
|
// return results).
|
2010-03-07 19:11:03 -05:00
|
|
|
// If a Node is *top_sensitive*, that means that it needs to compile differently
|
|
|
|
// depending on whether it's being used as part of a larger expression, or is a
|
|
|
|
// top-level statement within the function body.
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.compile = function(o) {
|
2010-02-27 19:19:53 -05:00
|
|
|
var closure, top;
|
2010-03-14 17:58:32 -04:00
|
|
|
this.options = merge(o || {});
|
2010-03-07 16:41:06 -05:00
|
|
|
this.tab = o.indent;
|
2010-05-03 09:04:26 -04:00
|
|
|
if (!(this instanceof ValueNode || this instanceof CallNode)) {
|
2010-03-14 17:58:32 -04:00
|
|
|
del(this.options, 'operation');
|
2010-05-03 09:04:26 -04:00
|
|
|
if (!(this instanceof AccessorNode || this instanceof IndexNode)) {
|
|
|
|
del(this.options, 'chain_root');
|
|
|
|
}
|
2010-02-07 15:45:05 -05:00
|
|
|
}
|
2010-03-14 17:58:32 -04:00
|
|
|
top = this.top_sensitive() ? this.options.top : del(this.options, 'top');
|
2010-03-21 11:28:05 -04:00
|
|
|
closure = this.is_statement() && !this.is_pure_statement() && !top && !this.options.as_statement && !(this instanceof CommentNode) && !this.contains_pure_statement();
|
|
|
|
if (closure) {
|
|
|
|
return this.compile_closure(this.options);
|
|
|
|
} else {
|
|
|
|
return this.compile_node(this.options);
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 16:41:06 -05:00
|
|
|
// Statements converted into expressions via closure-wrapping share a scope
|
|
|
|
// object with their parent closure, to preserve the expected lexical scope.
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.compile_closure = function(o) {
|
2010-03-07 16:41:06 -05:00
|
|
|
this.tab = o.indent;
|
2010-02-27 19:19:53 -05:00
|
|
|
o.shared_scope = o.scope;
|
|
|
|
return ClosureNode.wrap(this).compile(o);
|
|
|
|
};
|
|
|
|
// If the code generation wishes to use the result of a complex expression
|
2010-03-07 16:41:06 -05:00
|
|
|
// in multiple places, ensure that the expression is only ever evaluated once,
|
|
|
|
// by assigning it to a temporary variable.
|
2010-05-31 19:38:45 -04:00
|
|
|
BaseNode.prototype.compile_reference = function(o, only_if_necessary) {
|
2010-02-27 19:19:53 -05:00
|
|
|
var compiled, reference;
|
2010-05-31 19:38:45 -04:00
|
|
|
if (only_if_necessary && !(this instanceof CallNode || this instanceof ValueNode && (!(this.base instanceof LiteralNode) || this.has_properties()))) {
|
|
|
|
return [this, this];
|
|
|
|
}
|
2010-02-28 00:22:06 -05:00
|
|
|
reference = literal(o.scope.free_variable());
|
2010-02-27 19:19:53 -05:00
|
|
|
compiled = new AssignNode(reference, this);
|
|
|
|
return [compiled, reference];
|
|
|
|
};
|
2010-03-07 16:41:06 -05:00
|
|
|
// Convenience method to grab the current indentation level, plus tabbing in.
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.idt = function(tabs) {
|
2010-03-07 17:31:39 -05:00
|
|
|
var idt, num;
|
2010-03-07 16:41:06 -05:00
|
|
|
idt = this.tab || '';
|
2010-03-07 17:31:39 -05:00
|
|
|
num = (tabs || 0) + 1;
|
|
|
|
while (num -= 1) {
|
2010-03-10 20:15:12 -05:00
|
|
|
idt += TAB;
|
2010-02-27 19:19:53 -05:00
|
|
|
}
|
|
|
|
return idt;
|
|
|
|
};
|
2010-03-21 11:28:05 -04:00
|
|
|
// Construct a node that returns the current node's result.
|
|
|
|
// Note that this is overridden for smarter behavior for
|
|
|
|
// many statement nodes (eg IfNode, ForNode)...
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.make_return = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
return new ReturnNode(this);
|
|
|
|
};
|
2010-02-27 19:19:53 -05:00
|
|
|
// Does this node, or any of its children, contain a node of a certain kind?
|
2010-03-07 17:31:39 -05:00
|
|
|
// Recursively traverses down the *children* of the nodes, yielding to a block
|
|
|
|
// and returning true when the block finds a match. `contains` does not cross
|
|
|
|
// scope boundaries.
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.contains = function(block) {
|
2010-05-10 06:58:01 -04:00
|
|
|
var contains;
|
|
|
|
contains = false;
|
2010-05-10 22:41:18 -04:00
|
|
|
this.traverse_children(false, function(node) {
|
|
|
|
if (block(node)) {
|
|
|
|
contains = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2010-05-10 06:58:01 -04:00
|
|
|
return contains;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-31 22:48:47 -04:00
|
|
|
// Is this node of a certain type, or does it contain the type?
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.contains_type = function(type) {
|
2010-03-31 22:48:47 -04:00
|
|
|
return this instanceof type || this.contains(function(n) {
|
|
|
|
return n instanceof type;
|
|
|
|
});
|
|
|
|
};
|
2010-03-21 11:28:05 -04:00
|
|
|
// Convenience for the most common use of contains. Does the node contain
|
|
|
|
// a pure statement?
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.contains_pure_statement = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
return this.is_pure_statement() || this.contains(function(n) {
|
|
|
|
return n.is_pure_statement();
|
|
|
|
});
|
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// Perform an in-order traversal of the AST. Crosses scope boundaries.
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.traverse = function(block) {
|
2010-05-10 22:41:18 -04:00
|
|
|
return this.traverse_children(true, block);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// `toString` representation of the node, for inspecting the parse tree.
|
|
|
|
// This is what `coffee --nodes` prints out.
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.toString = function(idt) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, _e, child;
|
2010-02-27 19:19:53 -05:00
|
|
|
idt = idt || '';
|
2010-05-31 10:36:50 -04:00
|
|
|
return '\n' + idt + this.type + (function() {
|
|
|
|
_b = []; _d = this.collect_children();
|
2010-04-25 22:29:43 -04:00
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
child = _d[_c];
|
|
|
|
_b.push(child.toString(idt + TAB));
|
2010-02-27 19:19:53 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _b;
|
2010-02-27 19:19:53 -05:00
|
|
|
}).call(this).join('');
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.each_child = function(func) {
|
2010-05-10 22:41:18 -04:00
|
|
|
var _b, _c, _d, _e, _f, _g, attr, child;
|
2010-05-31 10:36:50 -04:00
|
|
|
if (!(this.children)) {
|
2010-05-19 15:37:42 -04:00
|
|
|
return null;
|
|
|
|
}
|
2010-05-31 10:36:50 -04:00
|
|
|
_c = this.children;
|
2010-05-10 06:58:01 -04:00
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
attr = _c[_b];
|
2010-05-10 22:41:18 -04:00
|
|
|
if (this[attr]) {
|
|
|
|
_f = flatten([this[attr]]);
|
2010-05-10 06:58:01 -04:00
|
|
|
for (_e = 0, _g = _f.length; _e < _g; _e++) {
|
|
|
|
child = _f[_e];
|
2010-05-10 22:41:18 -04:00
|
|
|
if (func(child) === false) {
|
2010-05-10 06:58:01 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2010-05-31 10:36:50 -04:00
|
|
|
BaseNode.prototype.collect_children = function() {
|
|
|
|
var nodes;
|
|
|
|
nodes = [];
|
|
|
|
this.each_child(function(node) {
|
|
|
|
return nodes.push(node);
|
|
|
|
});
|
|
|
|
return nodes;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.traverse_children = function(cross_scope, func) {
|
2010-05-10 22:41:18 -04:00
|
|
|
return this.each_child(function(child) {
|
2010-05-10 06:58:01 -04:00
|
|
|
func.apply(this, arguments);
|
|
|
|
if (child instanceof BaseNode) {
|
2010-05-10 22:41:18 -04:00
|
|
|
return child.traverse_children(cross_scope, func);
|
2010-05-10 06:58:01 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2010-05-19 15:50:29 -04:00
|
|
|
// Default implementations of the common node properties and methods. Nodes
|
2010-03-07 17:31:39 -05:00
|
|
|
// will override these with custom logic, if needed.
|
2010-05-31 10:36:50 -04:00
|
|
|
BaseNode.prototype.type = 'BaseNode';
|
|
|
|
BaseNode.prototype.children = [];
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.unwrap = function() {
|
2010-02-27 19:19:53 -05:00
|
|
|
return this;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.is_statement = function() {
|
2010-02-27 19:19:53 -05:00
|
|
|
return false;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.is_pure_statement = function() {
|
2010-02-27 19:19:53 -05:00
|
|
|
return false;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.top_sensitive = function() {
|
2010-02-27 19:19:53 -05:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
return BaseNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 17:31:39 -05:00
|
|
|
//### Expressions
|
|
|
|
// The expressions body is the list of expressions that forms the body of an
|
|
|
|
// indented block of code -- the implementation of a function, a clause in an
|
|
|
|
// `if`, `switch`, or `try`, and so on...
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.Expressions = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions = function(nodes) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expressions = compact(flatten(nodes || []));
|
2010-02-08 21:10:48 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(Expressions, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
Expressions.prototype.type = 'Expressions';
|
|
|
|
Expressions.prototype.children = ['expressions'];
|
|
|
|
Expressions.prototype.is_statement = function() {
|
|
|
|
return true;
|
|
|
|
};
|
2010-02-08 20:20:11 -05:00
|
|
|
// Tack an expression on to the end of this expression list.
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.push = function(node) {
|
2010-02-08 20:20:11 -05:00
|
|
|
this.expressions.push(node);
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// Add an expression at the beginning of this expression list.
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.unshift = function(node) {
|
2010-02-08 20:20:11 -05:00
|
|
|
this.expressions.unshift(node);
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// If this Expressions consists of just a single node, unwrap it by pulling
|
|
|
|
// it back out.
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.unwrap = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
if (this.expressions.length === 1) {
|
|
|
|
return this.expressions[0];
|
|
|
|
} else {
|
|
|
|
return this;
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-02-08 20:20:11 -05:00
|
|
|
// Is this an empty block of code?
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.empty = function() {
|
2010-02-08 20:20:11 -05:00
|
|
|
return this.expressions.length === 0;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-21 11:28:05 -04:00
|
|
|
// An Expressions node does not return its entire body, rather it
|
|
|
|
// ensures that the final expression is returned.
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.make_return = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
var idx, last;
|
|
|
|
idx = this.expressions.length - 1;
|
|
|
|
last = this.expressions[idx];
|
|
|
|
if (last instanceof CommentNode) {
|
|
|
|
last = this.expressions[idx -= 1];
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
2010-03-21 11:28:05 -04:00
|
|
|
if (!last || last instanceof ReturnNode) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
if (!(last.contains_pure_statement())) {
|
|
|
|
this.expressions[idx] = last.make_return();
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// An **Expressions** is the only node that can serve as the root.
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.compile = function(o) {
|
2010-02-13 01:13:08 -05:00
|
|
|
o = o || {};
|
2010-03-21 07:17:58 -04:00
|
|
|
if (o.scope) {
|
|
|
|
return Expressions.__superClass__.compile.call(this, o);
|
|
|
|
} else {
|
|
|
|
return this.compile_root(o);
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.compile_node = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, _e, node;
|
2010-02-17 19:19:51 -05:00
|
|
|
return (function() {
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = []; _d = this.expressions;
|
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
node = _d[_c];
|
|
|
|
_b.push(this.compile_expression(node, merge(o)));
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _b;
|
2010-02-17 19:19:51 -05:00
|
|
|
}).call(this).join("\n");
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// If we happen to be the top-level **Expressions**, wrap everything in
|
|
|
|
// a safety closure, unless requested not to.
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.compile_root = function(o) {
|
2010-03-07 16:41:06 -05:00
|
|
|
var code;
|
|
|
|
o.indent = (this.tab = o.no_wrap ? '' : TAB);
|
2010-02-08 20:20:11 -05:00
|
|
|
o.scope = new Scope(null, this, null);
|
|
|
|
code = o.globals ? this.compile_node(o) : this.compile_with_declarations(o);
|
|
|
|
code = code.replace(TRAILING_WHITESPACE, '');
|
2010-03-21 07:17:58 -04:00
|
|
|
if (o.no_wrap) {
|
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(function(){\n" + code + "\n})();\n";
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// Compile the expressions body for the contents of a function, with
|
|
|
|
// declarations of all inner variables pushed up to the top.
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.compile_with_declarations = function(o) {
|
2010-03-15 23:46:14 -04:00
|
|
|
var code;
|
2010-02-08 20:20:11 -05:00
|
|
|
code = this.compile_node(o);
|
|
|
|
if (o.scope.has_assignments(this)) {
|
2010-04-04 02:59:44 -04:00
|
|
|
code = ("" + (this.tab) + "var " + (o.scope.compiled_assignments()) + ";\n" + code);
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
|
|
|
if (o.scope.has_declarations(this)) {
|
2010-04-04 02:59:44 -04:00
|
|
|
code = ("" + (this.tab) + "var " + (o.scope.compiled_declarations()) + ";\n" + code);
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
|
|
|
return code;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// Compiles a single expression within the expressions body. If we need to
|
|
|
|
// return the result, and it's an expression, simply return it. If it's a
|
|
|
|
// statement, ask the statement to do so.
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.compile_expression = function(node, o) {
|
2010-03-21 11:28:05 -04:00
|
|
|
var compiled_node;
|
2010-03-07 16:41:06 -05:00
|
|
|
this.tab = o.indent;
|
2010-03-21 07:17:58 -04:00
|
|
|
compiled_node = node.compile(merge(o, {
|
|
|
|
top: true
|
|
|
|
}));
|
2010-03-21 11:28:05 -04:00
|
|
|
if (node.is_statement()) {
|
2010-03-21 07:17:58 -04:00
|
|
|
return compiled_node;
|
2010-03-21 11:28:05 -04:00
|
|
|
} else {
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + (this.idt()) + compiled_node + ";";
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return Expressions;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 17:31:39 -05:00
|
|
|
// Wrap up the given nodes as an **Expressions**, unless it already happens
|
|
|
|
// to be one.
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.wrap = function(nodes) {
|
2010-02-08 21:10:48 -05:00
|
|
|
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
|
|
|
|
return nodes[0];
|
|
|
|
}
|
|
|
|
return new Expressions(nodes);
|
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
//### LiteralNode
|
2010-02-08 20:20:11 -05:00
|
|
|
// Literals are static values that can be passed through directly into
|
2010-03-07 17:31:39 -05:00
|
|
|
// JavaScript without translation, such as: strings, numbers,
|
|
|
|
// `true`, `false`, `null`...
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.LiteralNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
LiteralNode = function(value) {
|
2010-02-11 20:20:59 -05:00
|
|
|
this.value = value;
|
2010-02-08 22:22:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(LiteralNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
LiteralNode.prototype.type = 'LiteralNode';
|
2010-03-07 17:31:39 -05:00
|
|
|
// Break and continue must be treated as pure statements -- they lose their
|
|
|
|
// meaning when wrapped in a closure.
|
2010-05-14 23:40:04 -04:00
|
|
|
LiteralNode.prototype.is_statement = function() {
|
2010-02-08 21:10:48 -05:00
|
|
|
return this.value === 'break' || this.value === 'continue';
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 16:41:06 -05:00
|
|
|
LiteralNode.prototype.is_pure_statement = LiteralNode.prototype.is_statement;
|
2010-05-14 23:40:04 -04:00
|
|
|
LiteralNode.prototype.compile_node = function(o) {
|
2010-02-08 21:10:48 -05:00
|
|
|
var end, idt;
|
|
|
|
idt = this.is_statement() ? this.idt() : '';
|
|
|
|
end = this.is_statement() ? ';' : '';
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + idt + this.value + end;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
LiteralNode.prototype.toString = function(idt) {
|
2010-03-05 22:52:28 -05:00
|
|
|
return " \"" + this.value + "\"";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return LiteralNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 17:31:39 -05:00
|
|
|
//### ReturnNode
|
|
|
|
// A `return` is a *pure_statement* -- wrapping it in a closure wouldn't
|
|
|
|
// make sense.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ReturnNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ReturnNode = function(expression) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expression = expression;
|
2010-02-08 22:22:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ReturnNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
ReturnNode.prototype.type = 'ReturnNode';
|
|
|
|
ReturnNode.prototype.is_statement = function() {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
ReturnNode.prototype.is_pure_statement = function() {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
ReturnNode.prototype.children = ['expression'];
|
2010-05-14 23:40:04 -04:00
|
|
|
ReturnNode.prototype.top_sensitive = function() {
|
2010-03-25 18:51:24 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ReturnNode.prototype.compile_node = function(o) {
|
2010-03-25 18:51:24 -04:00
|
|
|
var expr;
|
|
|
|
expr = this.expression.make_return();
|
|
|
|
if (!(expr instanceof ReturnNode)) {
|
|
|
|
return expr.compile(o);
|
|
|
|
}
|
|
|
|
del(o, 'top');
|
2010-02-08 22:22:59 -05:00
|
|
|
if (this.expression.is_statement()) {
|
2010-03-21 11:28:05 -04:00
|
|
|
o.as_statement = true;
|
|
|
|
}
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + (this.tab) + "return " + (this.expression.compile(o)) + ";";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ReturnNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 17:31:39 -05:00
|
|
|
//### ValueNode
|
|
|
|
// A value, variable or literal or parenthesized, indexed or dotted into,
|
|
|
|
// or vanilla.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ValueNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode = function(base, properties) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.base = base;
|
|
|
|
this.properties = (properties || []);
|
2010-02-08 22:22:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ValueNode, BaseNode);
|
2010-02-27 19:19:53 -05:00
|
|
|
ValueNode.prototype.SOAK = " == undefined ? undefined : ";
|
2010-05-31 10:36:50 -04:00
|
|
|
ValueNode.prototype.type = 'ValueNode';
|
|
|
|
ValueNode.prototype.children = ['base', 'properties'];
|
2010-03-07 17:31:39 -05:00
|
|
|
// A **ValueNode** has a base and a list of property accesses.
|
|
|
|
// Add a property access to the list.
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.push = function(prop) {
|
2010-02-08 22:22:59 -05:00
|
|
|
this.properties.push(prop);
|
2010-02-08 23:42:03 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.has_properties = function() {
|
2010-02-19 07:51:52 -05:00
|
|
|
return !!this.properties.length;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// Some boolean checks for the benefit of other nodes.
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.is_array = function() {
|
2010-02-08 22:22:59 -05:00
|
|
|
return this.base instanceof ArrayNode && !this.has_properties();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.is_object = function() {
|
2010-02-08 22:22:59 -05:00
|
|
|
return this.base instanceof ObjectNode && !this.has_properties();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.is_splice = function() {
|
2010-02-08 22:22:59 -05:00
|
|
|
return this.has_properties() && this.properties[this.properties.length - 1] instanceof SliceNode;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.make_return = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
if (this.has_properties()) {
|
2010-03-21 07:17:58 -04:00
|
|
|
return ValueNode.__superClass__.make_return.call(this);
|
2010-03-21 11:28:05 -04:00
|
|
|
} else {
|
|
|
|
return this.base.make_return();
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// The value can be unwrapped as its inner node, if there are no attached
|
|
|
|
// properties.
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.unwrap = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
if (this.properties.length) {
|
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
return this.base;
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// Values are considered to be statements if their base is a statement.
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.is_statement = function() {
|
2010-02-08 22:22:59 -05:00
|
|
|
return this.base.is_statement && this.base.is_statement() && !this.has_properties();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-05 05:58:48 -04:00
|
|
|
// Works out if the value is the start of a chain.
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.is_start = function(o) {
|
2010-05-05 05:58:48 -04:00
|
|
|
var node;
|
|
|
|
if (this === o.chain_root && this.properties[0] instanceof AccessorNode) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
node = o.chain_root.base || o.chain_root.variable;
|
|
|
|
while (node instanceof CallNode) {
|
|
|
|
node = node.variable;
|
|
|
|
}
|
|
|
|
return node === this;
|
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// We compile a value to JavaScript by compiling and joining each property.
|
|
|
|
// Things get much more insteresting if the chain of properties has *soak*
|
|
|
|
// operators `?.` interspersed. Then we have to take care not to accidentally
|
|
|
|
// evaluate a anything twice when building the soak chain.
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.compile_node = function(o) {
|
2010-05-05 05:58:48 -04:00
|
|
|
var _b, _c, baseline, complete, i, only, op, part, prop, props, temp;
|
2010-03-14 17:58:32 -04:00
|
|
|
only = del(o, 'only_first');
|
|
|
|
op = del(o, 'operation');
|
2010-03-30 20:06:44 -04:00
|
|
|
props = only ? this.properties.slice(0, this.properties.length - 1) : this.properties;
|
2010-05-05 05:58:48 -04:00
|
|
|
o.chain_root = o.chain_root || this;
|
2010-02-08 22:22:59 -05:00
|
|
|
baseline = this.base.compile(o);
|
2010-02-15 19:13:08 -05:00
|
|
|
if (this.base instanceof ObjectNode && this.has_properties()) {
|
2010-04-04 02:59:44 -04:00
|
|
|
baseline = ("(" + baseline + ")");
|
2010-02-15 19:13:08 -05:00
|
|
|
}
|
2010-02-22 22:11:47 -05:00
|
|
|
complete = (this.last = baseline);
|
2010-05-05 05:58:48 -04:00
|
|
|
_b = props;
|
|
|
|
for (i = 0, _c = _b.length; i < _c; i++) {
|
|
|
|
prop = _b[i];
|
2010-02-22 22:11:47 -05:00
|
|
|
this.source = baseline;
|
2010-02-24 00:06:01 -05:00
|
|
|
if (prop.soak_node) {
|
2010-05-05 05:58:48 -04:00
|
|
|
if (this.base instanceof CallNode && i === 0) {
|
2010-02-08 22:22:59 -05:00
|
|
|
temp = o.scope.free_variable();
|
2010-05-05 05:58:48 -04:00
|
|
|
complete = ("(" + (baseline = temp) + " = (" + complete + "))");
|
2010-02-08 22:22:59 -05:00
|
|
|
}
|
2010-05-05 05:58:48 -04:00
|
|
|
if (i === 0 && this.is_start(o)) {
|
|
|
|
complete = ("typeof " + complete + " === \"undefined\" || " + baseline);
|
2010-02-08 22:22:59 -05:00
|
|
|
}
|
2010-05-05 05:58:48 -04:00
|
|
|
complete += this.SOAK + (baseline += prop.compile(o));
|
2010-02-08 22:22:59 -05:00
|
|
|
} else {
|
|
|
|
part = prop.compile(o);
|
|
|
|
baseline += part;
|
2010-02-22 22:11:47 -05:00
|
|
|
complete += part;
|
|
|
|
this.last = part;
|
2010-02-08 22:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
2010-05-03 09:04:26 -04:00
|
|
|
if (op && this.wrapped) {
|
2010-03-21 07:17:58 -04:00
|
|
|
return "(" + complete + ")";
|
|
|
|
} else {
|
|
|
|
return complete;
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ValueNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 17:31:39 -05:00
|
|
|
//### CommentNode
|
|
|
|
// CoffeeScript passes through comments as JavaScript comments at the
|
2010-02-08 22:22:59 -05:00
|
|
|
// same position.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.CommentNode = (function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
CommentNode = function(lines, kind) {
|
2010-02-08 22:22:59 -05:00
|
|
|
this.lines = lines;
|
2010-05-31 10:36:50 -04:00
|
|
|
this.kind = kind;
|
2010-02-08 22:22:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(CommentNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
CommentNode.prototype.type = 'CommentNode';
|
|
|
|
CommentNode.prototype.is_statement = function() {
|
|
|
|
return true;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
CommentNode.prototype.make_return = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
CommentNode.prototype.compile_node = function(o) {
|
2010-05-12 20:56:44 -04:00
|
|
|
var sep;
|
2010-05-31 10:36:50 -04:00
|
|
|
if (this.kind === 'herecomment') {
|
2010-05-12 21:49:46 -04:00
|
|
|
sep = '\n' + this.tab;
|
2010-05-12 20:56:44 -04:00
|
|
|
return "" + this.tab + "/*" + sep + (this.lines.join(sep)) + "\n" + this.tab + "*/";
|
|
|
|
} else {
|
|
|
|
return ("" + this.tab + "//") + this.lines.join(("\n" + this.tab + "//"));
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return CommentNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 17:31:39 -05:00
|
|
|
//### CallNode
|
|
|
|
// Node for a function invocation. Takes care of converting `super()` calls into
|
2010-02-08 22:55:56 -05:00
|
|
|
// calls against the prototype's function of the same name.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.CallNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
CallNode = function(variable, args) {
|
2010-03-21 07:17:58 -04:00
|
|
|
this.is_new = false;
|
2010-03-21 11:28:05 -04:00
|
|
|
this.is_super = variable === 'super';
|
|
|
|
this.variable = this.is_super ? null : variable;
|
2010-05-10 06:58:01 -04:00
|
|
|
this.args = (args || []);
|
2010-03-30 16:48:43 -04:00
|
|
|
this.compile_splat_arguments = __bind(SplatNode.compile_mixed_array, this, [this.args]);
|
2010-02-08 22:55:56 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(CallNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
CallNode.prototype.type = 'CallNode';
|
|
|
|
CallNode.prototype.children = ['variable', 'args'];
|
2010-03-07 17:31:39 -05:00
|
|
|
// Tag this invocation as creating a new instance.
|
2010-05-14 23:40:04 -04:00
|
|
|
CallNode.prototype.new_instance = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
this.is_new = true;
|
2010-02-08 22:55:56 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
CallNode.prototype.prefix = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
if (this.is_new) {
|
|
|
|
return 'new ';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
2010-04-24 15:57:15 -04:00
|
|
|
// Grab the reference to the superclass' implementation of the current method.
|
2010-05-14 23:40:04 -04:00
|
|
|
CallNode.prototype.super_reference = function(o) {
|
2010-04-24 15:57:15 -04:00
|
|
|
var meth, methname;
|
|
|
|
methname = o.scope.method.name;
|
2010-05-02 23:20:51 -04:00
|
|
|
meth = (function() {
|
|
|
|
if (o.scope.method.proto) {
|
|
|
|
return "" + (o.scope.method.proto) + ".__superClass__." + methname;
|
|
|
|
} else if (methname) {
|
|
|
|
return "" + (methname) + ".__superClass__.constructor";
|
|
|
|
} else {
|
|
|
|
throw new Error("cannot call super on an anonymous function.");
|
|
|
|
}
|
|
|
|
})();
|
2010-04-24 15:57:15 -04:00
|
|
|
return meth;
|
|
|
|
};
|
2010-02-08 22:55:56 -05:00
|
|
|
// Compile a vanilla function call.
|
2010-05-14 23:40:04 -04:00
|
|
|
CallNode.prototype.compile_node = function(o) {
|
2010-05-03 09:04:26 -04:00
|
|
|
var _b, _c, _d, _e, _f, _g, _h, arg, args, compilation;
|
|
|
|
if (!(o.chain_root)) {
|
|
|
|
o.chain_root = this;
|
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = this.args;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
arg = _c[_b];
|
2010-05-03 09:04:26 -04:00
|
|
|
arg instanceof SplatNode ? (compilation = this.compile_splat(o)) : null;
|
2010-02-08 22:55:56 -05:00
|
|
|
}
|
2010-05-03 09:04:26 -04:00
|
|
|
if (!(compilation)) {
|
|
|
|
args = (function() {
|
|
|
|
_e = []; _g = this.args;
|
|
|
|
for (_f = 0, _h = _g.length; _f < _h; _f++) {
|
|
|
|
arg = _g[_f];
|
|
|
|
_e.push(arg.compile(o));
|
|
|
|
}
|
|
|
|
return _e;
|
|
|
|
}).call(this).join(', ');
|
|
|
|
compilation = this.is_super ? this.compile_super(args, o) : ("" + (this.prefix()) + (this.variable.compile(o)) + "(" + args + ")");
|
|
|
|
}
|
|
|
|
if (o.operation && this.wrapped) {
|
|
|
|
return "(" + compilation + ")";
|
|
|
|
} else {
|
|
|
|
return compilation;
|
2010-02-08 22:55:56 -05:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// `super()` is converted into a call against the superclass's implementation
|
|
|
|
// of the current function.
|
2010-05-14 23:40:04 -04:00
|
|
|
CallNode.prototype.compile_super = function(args, o) {
|
2010-04-24 15:57:15 -04:00
|
|
|
return "" + (this.super_reference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// If you call a function with a splat, it's converted into a JavaScript
|
2010-03-10 21:53:33 -05:00
|
|
|
// `.apply()` call to allow an array of arguments to be passed.
|
2010-05-14 23:40:04 -04:00
|
|
|
CallNode.prototype.compile_splat = function(o) {
|
2010-03-17 12:25:04 -04:00
|
|
|
var meth, obj, temp;
|
2010-04-24 15:57:15 -04:00
|
|
|
meth = this.variable ? this.variable.compile(o) : this.super_reference(o);
|
|
|
|
obj = this.variable && this.variable.source || 'this';
|
2010-02-27 15:15:26 -05:00
|
|
|
if (obj.match(/\(/)) {
|
|
|
|
temp = o.scope.free_variable();
|
|
|
|
obj = temp;
|
2010-04-04 02:59:44 -04:00
|
|
|
meth = ("(" + temp + " = " + (this.variable.source) + ")" + (this.variable.last));
|
2010-02-27 15:15:26 -05:00
|
|
|
}
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + (this.prefix()) + (meth) + ".apply(" + obj + ", " + (this.compile_splat_arguments(o)) + ")";
|
2010-03-17 12:25:04 -04:00
|
|
|
};
|
2010-02-27 19:19:53 -05:00
|
|
|
return CallNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-17 03:11:10 -04:00
|
|
|
//### CurryNode
|
2010-03-23 00:18:50 -04:00
|
|
|
// 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.
|
2010-03-17 03:11:10 -04:00
|
|
|
exports.CurryNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
CurryNode = function(meth, args) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.meth = meth;
|
|
|
|
this.context = args[0];
|
|
|
|
this.args = (args.slice(1) || []);
|
2010-03-30 16:48:43 -04:00
|
|
|
this.compile_splat_arguments = __bind(SplatNode.compile_mixed_array, this, [this.args]);
|
2010-03-17 03:11:10 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(CurryNode, CallNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
CurryNode.prototype.type = 'CurryNode';
|
|
|
|
CurryNode.prototype.children = ['meth', 'context', 'args'];
|
2010-05-14 23:40:04 -04:00
|
|
|
CurryNode.prototype.arguments = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, arg;
|
|
|
|
_c = this.args;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
arg = _c[_b];
|
2010-03-17 12:52:59 -04:00
|
|
|
if (arg instanceof SplatNode) {
|
2010-03-18 00:19:32 -04:00
|
|
|
return this.compile_splat_arguments(o);
|
2010-03-17 12:52:59 -04:00
|
|
|
}
|
|
|
|
}
|
2010-03-18 00:19:32 -04:00
|
|
|
return (new ArrayNode(this.args)).compile(o);
|
2010-03-17 12:52:59 -04:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
CurryNode.prototype.compile_node = function(o) {
|
2010-03-30 09:02:51 -04:00
|
|
|
var ref;
|
2010-03-30 19:48:37 -04:00
|
|
|
utility('slice');
|
|
|
|
ref = new ValueNode(literal(utility('bind')));
|
2010-03-30 09:02:51 -04:00
|
|
|
return (new CallNode(ref, [this.meth, this.context, literal(this.arguments(o))])).compile(o);
|
2010-03-17 03:11:10 -04:00
|
|
|
};
|
|
|
|
return CurryNode;
|
2010-04-10 14:20:32 -04:00
|
|
|
}).apply(this, arguments);
|
2010-03-07 17:31:39 -05:00
|
|
|
//### ExtendsNode
|
2010-02-08 23:16:29 -05:00
|
|
|
// Node to extend an object's prototype with an ancestor object.
|
2010-03-07 17:31:39 -05:00
|
|
|
// After `goog.inherits` from the
|
|
|
|
// [Closure Library](http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.html).
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ExtendsNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ExtendsNode = function(child, parent) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.child = child;
|
|
|
|
this.parent = parent;
|
2010-02-08 23:16:29 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ExtendsNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
ExtendsNode.prototype.type = 'ExtendsNode';
|
|
|
|
ExtendsNode.prototype.children = ['child', 'parent'];
|
2010-03-07 17:31:39 -05:00
|
|
|
// Hooks one constructor into another's prototype chain.
|
2010-05-14 23:40:04 -04:00
|
|
|
ExtendsNode.prototype.compile_node = function(o) {
|
2010-03-30 09:02:51 -04:00
|
|
|
var ref;
|
2010-03-30 20:15:51 -04:00
|
|
|
ref = new ValueNode(literal(utility('extends')));
|
2010-03-30 09:02:51 -04:00
|
|
|
return (new CallNode(ref, [this.child, this.parent])).compile(o);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ExtendsNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 17:31:39 -05:00
|
|
|
//### AccessorNode
|
|
|
|
// A `.` accessor into a property of a value, or the `::` shorthand for
|
2010-02-08 23:42:03 -05:00
|
|
|
// an accessor into the object's prototype.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.AccessorNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
AccessorNode = function(name, tag) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.name = name;
|
2010-02-08 23:42:03 -05:00
|
|
|
this.prototype = tag === 'prototype';
|
2010-02-24 00:06:01 -05:00
|
|
|
this.soak_node = tag === 'soak';
|
2010-02-08 23:42:03 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(AccessorNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
AccessorNode.prototype.type = 'AccessorNode';
|
|
|
|
AccessorNode.prototype.children = ['name'];
|
2010-05-14 23:40:04 -04:00
|
|
|
AccessorNode.prototype.compile_node = function(o) {
|
2010-03-10 21:53:33 -05:00
|
|
|
var proto_part;
|
2010-05-03 09:04:26 -04:00
|
|
|
o.chain_root.wrapped = o.chain_root.wrapped || this.soak_node;
|
2010-03-10 21:53:33 -05:00
|
|
|
proto_part = this.prototype ? 'prototype.' : '';
|
|
|
|
return "." + proto_part + (this.name.compile(o));
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return AccessorNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 17:31:39 -05:00
|
|
|
//### IndexNode
|
2010-03-10 21:53:33 -05:00
|
|
|
// A `[ ... ]` indexed accessor into an array or object.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.IndexNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
IndexNode = function(index, tag) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.index = index;
|
2010-02-24 00:06:01 -05:00
|
|
|
this.soak_node = tag === 'soak';
|
2010-02-08 23:45:46 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(IndexNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
IndexNode.prototype.type = 'IndexNode';
|
|
|
|
IndexNode.prototype.children = ['index'];
|
2010-05-14 23:40:04 -04:00
|
|
|
IndexNode.prototype.compile_node = function(o) {
|
2010-03-06 00:23:54 -05:00
|
|
|
var idx;
|
2010-05-03 09:12:15 -04:00
|
|
|
o.chain_root.wrapped = o.chain_root.wrapped || this.soak_node;
|
2010-03-06 00:23:54 -05:00
|
|
|
idx = this.index.compile(o);
|
|
|
|
return "[" + idx + "]";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return IndexNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 17:31:39 -05:00
|
|
|
//### RangeNode
|
2010-02-09 19:30:28 -05:00
|
|
|
// A range literal. Ranges can be used to extract portions (slices) of arrays,
|
2010-03-07 17:31:39 -05:00
|
|
|
// to specify a range for comprehensions, or as a value, to be expanded into the
|
|
|
|
// corresponding array of integers at runtime.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.RangeNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
RangeNode = function(from, to, exclusive) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.from = from;
|
|
|
|
this.to = to;
|
2010-02-09 19:30:28 -05:00
|
|
|
this.exclusive = !!exclusive;
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(RangeNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
RangeNode.prototype.type = 'RangeNode';
|
|
|
|
RangeNode.prototype.children = ['from', 'to'];
|
2010-03-07 17:31:39 -05:00
|
|
|
// Compiles the range's source variables -- where it starts and where it ends.
|
2010-05-31 19:38:45 -04:00
|
|
|
// But only if they need to be cached to avoid double evaluation.
|
2010-05-14 23:40:04 -04:00
|
|
|
RangeNode.prototype.compile_variables = function(o) {
|
2010-05-31 19:38:45 -04:00
|
|
|
var _b, _c, parts, tail;
|
|
|
|
_b = this.from.compile_reference(o, true);
|
|
|
|
this.from = _b[0];
|
|
|
|
this.from_var = _b[1];
|
|
|
|
_c = this.to.compile_reference(o, true);
|
|
|
|
this.to = _c[0];
|
|
|
|
this.to_var = _c[1];
|
|
|
|
parts = [];
|
|
|
|
if (this.from !== this.from_var) {
|
|
|
|
parts.push(this.from.compile(o));
|
|
|
|
}
|
|
|
|
if (this.to !== this.to_var) {
|
|
|
|
parts.push(this.to.compile(o));
|
|
|
|
}
|
|
|
|
tail = ("\n" + o.indent);
|
|
|
|
if (parts.length) {
|
|
|
|
return "" + (parts.join('; ')) + ";" + tail;
|
|
|
|
} else {
|
|
|
|
return tail;
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 17:31:39 -05:00
|
|
|
// When compiled normally, the range returns the contents of the *for loop*
|
|
|
|
// needed to iterate over the values in the range. Used by comprehensions.
|
2010-05-14 23:40:04 -04:00
|
|
|
RangeNode.prototype.compile_node = function(o) {
|
2010-05-31 19:38:45 -04:00
|
|
|
var equals, idx, op, step, vars;
|
2010-02-09 19:30:28 -05:00
|
|
|
if (!(o.index)) {
|
|
|
|
return this.compile_array(o);
|
|
|
|
}
|
2010-03-14 17:58:32 -04:00
|
|
|
idx = del(o, 'index');
|
|
|
|
step = del(o, 'step');
|
2010-05-31 19:38:45 -04:00
|
|
|
vars = ("" + idx + " = " + (this.from_var.compile(o)));
|
2010-02-11 20:20:59 -05:00
|
|
|
step = step ? step.compile(o) : '1';
|
2010-02-09 19:30:28 -05:00
|
|
|
equals = this.exclusive ? '' : '=';
|
2010-05-31 19:38:45 -04:00
|
|
|
op = starts(step, '-') ? (">" + equals) : ("<" + equals);
|
|
|
|
return "" + vars + "; " + (idx) + " " + op + " " + (this.to_var.compile(o)) + "; " + idx + " += " + step;
|
|
|
|
};
|
|
|
|
// When used as a value, expand the range into the equivalent array.
|
2010-05-14 23:40:04 -04:00
|
|
|
RangeNode.prototype.compile_array = function(o) {
|
2010-05-31 19:38:45 -04:00
|
|
|
var body, clause, equals, from, idt, post, pre, to, vars;
|
|
|
|
idt = this.idt(1);
|
|
|
|
vars = this.compile_variables(merge(o, {
|
|
|
|
indent: idt
|
|
|
|
}));
|
|
|
|
equals = this.exclusive ? '' : '=';
|
|
|
|
from = this.from_var.compile(o);
|
|
|
|
to = this.to_var.compile(o);
|
|
|
|
clause = ("" + from + " <= " + to + " ?");
|
|
|
|
pre = ("\n" + (idt) + "a = [];" + (vars));
|
|
|
|
body = ("var i = " + from + "; (" + clause + " i <" + equals + " " + to + " : i >" + equals + " " + to + "); (" + clause + " i += 1 : i -= 1)");
|
|
|
|
post = ("a.push(i);\n" + (idt) + "return a;\n" + o.indent);
|
|
|
|
return "(function(){" + (pre) + "for (" + body + ") " + post + "}).call(this)";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return RangeNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 19:11:03 -05:00
|
|
|
//### 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
|
2010-02-09 19:30:28 -05:00
|
|
|
// is the index of the beginning.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.SliceNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
SliceNode = function(range) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.range = range;
|
2010-02-09 19:30:28 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(SliceNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
SliceNode.prototype.type = 'SliceNode';
|
|
|
|
SliceNode.prototype.children = ['range'];
|
2010-05-14 23:40:04 -04:00
|
|
|
SliceNode.prototype.compile_node = function(o) {
|
2010-03-21 12:11:44 -04:00
|
|
|
var from, plus_part, to;
|
|
|
|
from = this.range.from.compile(o);
|
|
|
|
to = this.range.to.compile(o);
|
|
|
|
plus_part = this.range.exclusive ? '' : ' + 1';
|
|
|
|
return ".slice(" + from + ", " + to + plus_part + ")";
|
|
|
|
};
|
2010-02-27 19:19:53 -05:00
|
|
|
return SliceNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 19:11:03 -05:00
|
|
|
//### ObjectNode
|
|
|
|
// An object literal, nothing fancy.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ObjectNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ObjectNode = function(props) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.objects = (this.properties = props || []);
|
2010-02-09 20:53:25 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ObjectNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
ObjectNode.prototype.type = 'ObjectNode';
|
|
|
|
ObjectNode.prototype.children = ['properties'];
|
2010-02-09 20:53:25 -05:00
|
|
|
// All the mucking about with commas is to make sure that CommentNodes and
|
|
|
|
// AssignNodes get interleaved correctly, with no trailing commas or
|
2010-03-07 19:11:03 -05:00
|
|
|
// commas affixed to comments.
|
2010-05-14 23:40:04 -04:00
|
|
|
ObjectNode.prototype.compile_node = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, _e, _f, _g, _h, i, indent, inner, join, last_noncom, non_comments, prop, props;
|
2010-02-09 20:53:25 -05:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
non_comments = (function() {
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = []; _d = this.properties;
|
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
prop = _d[_c];
|
|
|
|
!(prop instanceof CommentNode) ? _b.push(prop) : null;
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _b;
|
2010-02-09 20:53:25 -05:00
|
|
|
}).call(this);
|
|
|
|
last_noncom = non_comments[non_comments.length - 1];
|
|
|
|
props = (function() {
|
2010-04-25 22:29:43 -04:00
|
|
|
_f = []; _g = this.properties;
|
|
|
|
for (i = 0, _h = _g.length; i < _h; i++) {
|
|
|
|
prop = _g[i];
|
|
|
|
_f.push((function() {
|
2010-02-09 20:53:25 -05:00
|
|
|
join = ",\n";
|
2010-02-13 00:24:26 -05:00
|
|
|
if ((prop === last_noncom) || (prop instanceof CommentNode)) {
|
2010-02-09 20:53:25 -05:00
|
|
|
join = "\n";
|
|
|
|
}
|
2010-02-13 00:24:26 -05:00
|
|
|
if (i === this.properties.length - 1) {
|
2010-02-09 20:53:25 -05:00
|
|
|
join = '';
|
|
|
|
}
|
|
|
|
indent = prop instanceof CommentNode ? '' : this.idt(1);
|
2010-04-26 00:00:12 -04:00
|
|
|
if (!(prop instanceof AssignNode || prop instanceof CommentNode)) {
|
2010-04-25 23:54:47 -04:00
|
|
|
prop = new AssignNode(prop, prop, 'object');
|
|
|
|
}
|
2010-02-09 20:53:25 -05:00
|
|
|
return indent + prop.compile(o) + join;
|
|
|
|
}).call(this));
|
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _f;
|
2010-02-09 20:53:25 -05:00
|
|
|
}).call(this);
|
2010-02-11 23:57:31 -05:00
|
|
|
props = props.join('');
|
|
|
|
inner = props ? '\n' + props + '\n' + this.idt() : '';
|
2010-03-06 00:23:54 -05:00
|
|
|
return "{" + inner + "}";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ObjectNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 19:11:03 -05:00
|
|
|
//### ArrayNode
|
|
|
|
// An array literal.
|
|
|
|
exports.ArrayNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ArrayNode = function(objects) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.objects = objects || [];
|
2010-03-30 16:48:43 -04:00
|
|
|
this.compile_splat_literal = __bind(SplatNode.compile_mixed_array, this, [this.objects]);
|
2010-03-07 19:11:03 -05:00
|
|
|
return this;
|
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ArrayNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
ArrayNode.prototype.type = 'ArrayNode';
|
|
|
|
ArrayNode.prototype.children = ['objects'];
|
2010-05-14 23:40:04 -04:00
|
|
|
ArrayNode.prototype.compile_node = function(o) {
|
2010-04-26 23:46:35 -04:00
|
|
|
var _b, _c, code, i, obj, objects;
|
2010-03-07 19:11:03 -05:00
|
|
|
o.indent = this.idt(1);
|
2010-03-21 11:28:05 -04:00
|
|
|
objects = [];
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = this.objects;
|
|
|
|
for (i = 0, _c = _b.length; i < _c; i++) {
|
|
|
|
obj = _b[i];
|
2010-03-21 11:28:05 -04:00
|
|
|
code = obj.compile(o);
|
|
|
|
if (obj instanceof SplatNode) {
|
2010-03-30 20:06:44 -04:00
|
|
|
return this.compile_splat_literal(this.objects, o);
|
2010-03-21 11:28:05 -04:00
|
|
|
} else if (obj instanceof CommentNode) {
|
2010-04-04 02:59:44 -04:00
|
|
|
objects.push(("\n" + code + "\n" + o.indent));
|
2010-03-21 11:28:05 -04:00
|
|
|
} else if (i === this.objects.length - 1) {
|
|
|
|
objects.push(code);
|
|
|
|
} else {
|
2010-04-04 02:59:44 -04:00
|
|
|
objects.push(("" + code + ", "));
|
2010-03-07 19:11:03 -05:00
|
|
|
}
|
2010-03-21 11:28:05 -04:00
|
|
|
}
|
2010-03-07 19:11:03 -05:00
|
|
|
objects = objects.join('');
|
2010-05-14 23:40:04 -04:00
|
|
|
if (index_of(objects, '\n') >= 0) {
|
2010-04-26 23:46:35 -04:00
|
|
|
return "[\n" + (this.idt(1)) + objects + "\n" + this.tab + "]";
|
|
|
|
} else {
|
|
|
|
return "[" + objects + "]";
|
|
|
|
}
|
2010-03-07 19:11:03 -05:00
|
|
|
};
|
|
|
|
return ArrayNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 19:11:03 -05:00
|
|
|
//### ClassNode
|
|
|
|
// The CoffeeScript class definition.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ClassNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ClassNode = function(variable, parent, props) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.variable = variable;
|
|
|
|
this.parent = parent;
|
|
|
|
this.properties = props || [];
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = false;
|
2010-02-27 18:57:45 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ClassNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
ClassNode.prototype.type = 'ClassNode';
|
|
|
|
ClassNode.prototype.children = ['variable', 'parent', 'properties'];
|
|
|
|
ClassNode.prototype.is_statement = function() {
|
|
|
|
return true;
|
|
|
|
};
|
2010-03-07 19:11:03 -05:00
|
|
|
// Initialize a **ClassNode** with its name, an optional superclass, and a
|
|
|
|
// list of prototype property assignments.
|
2010-05-14 23:40:04 -04:00
|
|
|
ClassNode.prototype.make_return = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = true;
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-03-07 19:11:03 -05:00
|
|
|
// Instead of generating the JavaScript string directly, we build up the
|
|
|
|
// equivalent syntax tree and compile that, in pieces. You can see the
|
|
|
|
// constructor, property assignments, and inheritance getting built out below.
|
2010-05-14 23:40:04 -04:00
|
|
|
ClassNode.prototype.compile_node = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, _e, access, applied, construct, extension, func, prop, props, pvar, returns, val;
|
2010-02-27 18:57:45 -05:00
|
|
|
extension = this.parent && new ExtendsNode(this.variable, this.parent);
|
|
|
|
constructor = null;
|
|
|
|
props = new Expressions();
|
|
|
|
o.top = true;
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = this.properties;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
prop = _c[_b];
|
|
|
|
_e = [prop.variable, prop.value];
|
|
|
|
pvar = _e[0];
|
|
|
|
func = _e[1];
|
2010-04-03 14:53:26 -04:00
|
|
|
if (pvar && pvar.base.value === 'constructor' && func instanceof CodeNode) {
|
2010-05-25 02:15:46 -04:00
|
|
|
func.name = this.variable.compile(o);
|
2010-02-28 00:22:06 -05:00
|
|
|
func.body.push(new ReturnNode(literal('this')));
|
2010-05-25 02:15:46 -04:00
|
|
|
this.variable = new ValueNode(this.variable);
|
|
|
|
this.variable.namespaced = include(func.name, '.');
|
2010-02-27 18:57:45 -05:00
|
|
|
constructor = new AssignNode(this.variable, func);
|
|
|
|
} else {
|
2010-03-29 21:43:12 -04:00
|
|
|
if (pvar) {
|
|
|
|
access = prop.context === 'this' ? pvar.base.properties[0] : new AccessorNode(pvar, 'prototype');
|
|
|
|
val = new ValueNode(this.variable, [access]);
|
2010-04-03 14:53:26 -04:00
|
|
|
prop = new AssignNode(val, func);
|
2010-02-27 19:03:23 -05:00
|
|
|
}
|
2010-02-27 18:57:45 -05:00
|
|
|
props.push(prop);
|
|
|
|
}
|
|
|
|
}
|
2010-04-27 19:38:24 -04:00
|
|
|
if (!(constructor)) {
|
2010-02-28 00:13:17 -05:00
|
|
|
if (this.parent) {
|
2010-02-28 00:22:06 -05:00
|
|
|
applied = new ValueNode(this.parent, [new AccessorNode(literal('apply'))]);
|
|
|
|
constructor = new AssignNode(this.variable, new CodeNode([], new Expressions([new CallNode(applied, [literal('this'), literal('arguments')])])));
|
2010-02-28 00:13:17 -05:00
|
|
|
} else {
|
|
|
|
constructor = new AssignNode(this.variable, new CodeNode());
|
|
|
|
}
|
2010-02-27 18:57:45 -05:00
|
|
|
}
|
|
|
|
construct = this.idt() + constructor.compile(o) + ';\n';
|
|
|
|
props = props.empty() ? '' : props.compile(o) + '\n';
|
2010-02-27 19:29:34 -05:00
|
|
|
extension = extension ? this.idt() + extension.compile(o) + ';\n' : '';
|
2010-03-21 11:28:05 -04:00
|
|
|
returns = this.returns ? new ReturnNode(this.variable).compile(o) : '';
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + construct + extension + props + returns;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ClassNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 19:11:03 -05:00
|
|
|
//### AssignNode
|
|
|
|
// The **AssignNode** is used to assign a local variable to value, or to set the
|
|
|
|
// property of an object -- including within object literals.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.AssignNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
AssignNode = function(variable, value, context) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.variable = variable;
|
|
|
|
this.value = value;
|
2010-02-09 20:53:25 -05:00
|
|
|
this.context = context;
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(AssignNode, BaseNode);
|
2010-03-07 19:11:03 -05:00
|
|
|
// Matchers for detecting prototype assignments.
|
2010-02-27 19:19:53 -05:00
|
|
|
AssignNode.prototype.PROTO_ASSIGN = /^(\S+)\.prototype/;
|
|
|
|
AssignNode.prototype.LEADING_DOT = /^\.(prototype\.)?/;
|
2010-05-31 10:36:50 -04:00
|
|
|
AssignNode.prototype.type = 'AssignNode';
|
|
|
|
AssignNode.prototype.children = ['variable', 'value'];
|
2010-05-14 23:40:04 -04:00
|
|
|
AssignNode.prototype.top_sensitive = function() {
|
2010-02-09 20:53:25 -05:00
|
|
|
return true;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
AssignNode.prototype.is_value = function() {
|
2010-02-09 20:53:25 -05:00
|
|
|
return this.variable instanceof ValueNode;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
AssignNode.prototype.make_return = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
return new Expressions([this, new ReturnNode(this.variable)]);
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
AssignNode.prototype.is_statement = function() {
|
2010-02-09 20:53:25 -05:00
|
|
|
return this.is_value() && (this.variable.is_array() || this.variable.is_object());
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 19:11:03 -05:00
|
|
|
// Compile an assignment, delegating to `compile_pattern_match` or
|
|
|
|
// `compile_splice` if appropriate. Keep track of the name of the base object
|
|
|
|
// we've been assigned to, for correct internal references. If the variable
|
|
|
|
// has not been seen yet within the current scope, declare it.
|
2010-05-14 23:40:04 -04:00
|
|
|
AssignNode.prototype.compile_node = function(o) {
|
2010-02-09 20:53:25 -05:00
|
|
|
var last, match, name, proto, stmt, top, val;
|
2010-03-14 17:58:32 -04:00
|
|
|
top = del(o, 'top');
|
2010-02-09 20:53:25 -05:00
|
|
|
if (this.is_statement()) {
|
|
|
|
return this.compile_pattern_match(o);
|
|
|
|
}
|
|
|
|
if (this.is_value() && this.variable.is_splice()) {
|
|
|
|
return this.compile_splice(o);
|
|
|
|
}
|
2010-03-14 17:58:32 -04:00
|
|
|
stmt = del(o, 'as_statement');
|
2010-02-09 20:53:25 -05:00
|
|
|
name = this.variable.compile(o);
|
|
|
|
last = this.is_value() ? this.variable.last.replace(this.LEADING_DOT, '') : name;
|
|
|
|
match = name.match(this.PROTO_ASSIGN);
|
|
|
|
proto = match && match[1];
|
|
|
|
if (this.value instanceof CodeNode) {
|
2010-02-10 00:05:56 -05:00
|
|
|
if (last.match(IDENTIFIER)) {
|
2010-02-09 20:53:25 -05:00
|
|
|
this.value.name = last;
|
|
|
|
}
|
|
|
|
if (proto) {
|
|
|
|
this.value.proto = proto;
|
|
|
|
}
|
|
|
|
}
|
2010-03-06 00:23:54 -05:00
|
|
|
val = this.value.compile(o);
|
2010-02-09 20:53:25 -05:00
|
|
|
if (this.context === 'object') {
|
2010-04-04 02:59:44 -04:00
|
|
|
return ("" + name + ": " + val);
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-05-25 02:15:46 -04:00
|
|
|
if (!(this.is_value() && (this.variable.has_properties() || this.variable.namespaced))) {
|
2010-02-09 20:53:25 -05:00
|
|
|
o.scope.find(name);
|
|
|
|
}
|
2010-04-04 02:59:44 -04:00
|
|
|
val = ("" + name + " = " + val);
|
2010-02-09 20:53:25 -05:00
|
|
|
if (stmt) {
|
2010-04-04 02:59:44 -04:00
|
|
|
return ("" + this.tab + val + ";");
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-03-21 11:28:05 -04:00
|
|
|
if (top) {
|
|
|
|
return val;
|
|
|
|
} else {
|
|
|
|
return "(" + val + ")";
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 19:11:03 -05:00
|
|
|
// Brief implementation of recursive pattern matching, when assigning array or
|
2010-02-09 20:53:25 -05:00
|
|
|
// object literals to a value. Peeks at their properties to assign inner names.
|
2010-03-07 19:11:03 -05:00
|
|
|
// See the [ECMAScript Harmony Wiki](http://wiki.ecmascript.org/doku.php?id=harmony:destructuring)
|
|
|
|
// for details.
|
2010-05-14 23:40:04 -04:00
|
|
|
AssignNode.prototype.compile_pattern_match = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, access_class, assigns, code, i, idx, is_string, obj, oindex, olength, splat, val, val_var, value;
|
2010-02-09 20:53:25 -05:00
|
|
|
val_var = o.scope.free_variable();
|
2010-02-25 07:28:48 -05:00
|
|
|
value = this.value.is_statement() ? ClosureNode.wrap(this.value) : this.value;
|
2010-04-04 02:59:44 -04:00
|
|
|
assigns = [("" + this.tab + val_var + " = " + (value.compile(o)) + ";")];
|
2010-02-09 20:53:25 -05:00
|
|
|
o.top = true;
|
|
|
|
o.as_statement = true;
|
2010-03-22 02:02:04 -04:00
|
|
|
splat = false;
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = this.variable.base.objects;
|
|
|
|
for (i = 0, _c = _b.length; i < _c; i++) {
|
|
|
|
obj = _b[i];
|
2010-04-25 22:21:53 -04:00
|
|
|
// A regular array pattern-match.
|
2010-02-11 18:44:00 -05:00
|
|
|
idx = i;
|
2010-02-09 20:53:25 -05:00
|
|
|
if (this.variable.is_object()) {
|
2010-04-25 22:21:53 -04:00
|
|
|
if (obj instanceof AssignNode) {
|
|
|
|
// A regular object pattern-match.
|
2010-04-25 22:29:43 -04:00
|
|
|
_d = [obj.value, obj.variable.base];
|
|
|
|
obj = _d[0];
|
|
|
|
idx = _d[1];
|
2010-04-25 22:21:53 -04:00
|
|
|
} else {
|
|
|
|
// A shorthand `{a, b, c}: val` pattern-match.
|
|
|
|
idx = obj;
|
|
|
|
}
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-04-25 11:22:15 -04:00
|
|
|
if (!(obj instanceof ValueNode || obj instanceof SplatNode)) {
|
|
|
|
throw new Error('pattern matching must use only identifiers on the left-hand side.');
|
|
|
|
}
|
2010-04-25 11:07:09 -04:00
|
|
|
is_string = idx.value && idx.value.match(IS_STRING);
|
|
|
|
access_class = is_string || this.variable.is_array() ? IndexNode : AccessorNode;
|
2010-03-22 02:02:04 -04:00
|
|
|
if (obj instanceof SplatNode && !splat) {
|
2010-05-14 23:40:04 -04:00
|
|
|
val = literal(obj.compile_value(o, val_var, (oindex = index_of(this.variable.base.objects, obj)), (olength = this.variable.base.objects.length) - oindex - 1));
|
2010-03-22 02:02:04 -04:00
|
|
|
splat = true;
|
2010-02-11 20:11:11 -05:00
|
|
|
} else {
|
2010-03-22 02:02:04 -04:00
|
|
|
if (typeof idx !== 'object') {
|
2010-04-04 02:59:44 -04:00
|
|
|
idx = literal(splat ? ("" + (val_var) + ".length - " + (olength - idx)) : idx);
|
2010-03-22 02:02:04 -04:00
|
|
|
}
|
2010-02-28 00:22:06 -05:00
|
|
|
val = new ValueNode(literal(val_var), [new access_class(idx)]);
|
2010-02-11 20:11:11 -05:00
|
|
|
}
|
2010-02-09 20:53:25 -05:00
|
|
|
assigns.push(new AssignNode(obj, val).compile(o));
|
|
|
|
}
|
2010-02-25 00:43:02 -05:00
|
|
|
code = assigns.join("\n");
|
|
|
|
return code;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-07 19:11:03 -05:00
|
|
|
// Compile the assignment from an array splice literal, using JavaScript's
|
|
|
|
// `Array#splice` method.
|
2010-05-14 23:40:04 -04:00
|
|
|
AssignNode.prototype.compile_splice = function(o) {
|
2010-03-30 20:06:44 -04:00
|
|
|
var from, l, name, plus, range, to, val;
|
|
|
|
name = this.variable.compile(merge(o, {
|
|
|
|
only_first: true
|
2010-02-09 20:53:25 -05:00
|
|
|
}));
|
2010-03-30 20:06:44 -04:00
|
|
|
l = this.variable.properties.length;
|
|
|
|
range = this.variable.properties[l - 1].range;
|
|
|
|
plus = range.exclusive ? '' : ' + 1';
|
|
|
|
from = range.from.compile(o);
|
|
|
|
to = range.to.compile(o) + ' - ' + from + plus;
|
|
|
|
val = this.value.compile(o);
|
|
|
|
return "" + (name) + ".splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + val + "))";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return AssignNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 19:11:03 -05:00
|
|
|
//### 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
|
|
|
|
// has no *children* -- they're within the inner scope.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.CodeNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
CodeNode = function(params, body, tag) {
|
2010-02-27 18:57:45 -05:00
|
|
|
this.params = params || [];
|
|
|
|
this.body = body || new Expressions();
|
2010-02-09 21:31:07 -05:00
|
|
|
this.bound = tag === 'boundfunc';
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(CodeNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
CodeNode.prototype.type = 'CodeNode';
|
|
|
|
CodeNode.prototype.children = ['params', 'body'];
|
2010-03-07 19:11:03 -05:00
|
|
|
// Compilation creates a new scope unless explicitly asked to share with the
|
|
|
|
// outer scope. Handles splat parameters in the parameter list by peeking at
|
|
|
|
// the JavaScript `arguments` objects. If the function is bound with the `=>`
|
|
|
|
// arrow, generates a wrapper that saves the current value of `this` through
|
|
|
|
// a closure.
|
2010-05-14 23:40:04 -04:00
|
|
|
CodeNode.prototype.compile_node = function(o) {
|
|
|
|
var _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, code, func, i, param, params, ref, shared_scope, splat, top;
|
2010-03-14 17:58:32 -04:00
|
|
|
shared_scope = del(o, 'shared_scope');
|
|
|
|
top = del(o, 'top');
|
2010-02-09 21:31:07 -05:00
|
|
|
o.scope = shared_scope || new Scope(o.scope, this.body, this);
|
|
|
|
o.top = true;
|
|
|
|
o.indent = this.idt(this.bound ? 2 : 1);
|
2010-03-14 17:58:32 -04:00
|
|
|
del(o, 'no_wrap');
|
|
|
|
del(o, 'globals');
|
2010-03-17 14:41:09 -04:00
|
|
|
i = 0;
|
|
|
|
splat = undefined;
|
|
|
|
params = [];
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = this.params;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
param = _c[_b];
|
2010-03-17 14:41:09 -04:00
|
|
|
if (param instanceof SplatNode && !(typeof splat !== "undefined" && splat !== null)) {
|
|
|
|
splat = param;
|
|
|
|
splat.index = i;
|
|
|
|
splat.trailings = [];
|
2010-05-08 12:44:54 -04:00
|
|
|
splat.arglength = this.params.length;
|
|
|
|
this.body.unshift(splat);
|
2010-03-17 14:41:09 -04:00
|
|
|
} else if ((typeof splat !== "undefined" && splat !== null)) {
|
|
|
|
splat.trailings.push(param);
|
|
|
|
} else {
|
|
|
|
params.push(param);
|
|
|
|
}
|
|
|
|
i += 1;
|
2010-02-09 21:31:07 -05:00
|
|
|
}
|
2010-02-17 19:19:51 -05:00
|
|
|
params = (function() {
|
2010-04-25 22:29:43 -04:00
|
|
|
_e = []; _g = params;
|
|
|
|
for (_f = 0, _h = _g.length; _f < _h; _f++) {
|
|
|
|
param = _g[_f];
|
|
|
|
_e.push(param.compile(o));
|
2010-02-12 13:35:34 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _e;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-21 07:17:58 -04:00
|
|
|
this.body.make_return();
|
2010-04-25 22:29:43 -04:00
|
|
|
_j = params;
|
|
|
|
for (_i = 0, _k = _j.length; _i < _k; _i++) {
|
|
|
|
param = _j[_i];
|
2010-02-09 21:31:07 -05:00
|
|
|
(o.scope.parameter(param));
|
|
|
|
}
|
2010-04-04 02:59:44 -04:00
|
|
|
code = this.body.expressions.length ? ("\n" + (this.body.compile_with_declarations(o)) + "\n") : '';
|
2010-05-14 23:40:04 -04:00
|
|
|
func = ("function(" + (params.join(', ')) + ") {" + code + (this.idt(this.bound ? 1 : 0)) + "}");
|
2010-02-09 21:31:07 -05:00
|
|
|
if (top && !this.bound) {
|
2010-04-04 02:59:44 -04:00
|
|
|
func = ("(" + func + ")");
|
2010-02-09 21:31:07 -05:00
|
|
|
}
|
|
|
|
if (!(this.bound)) {
|
|
|
|
return func;
|
|
|
|
}
|
2010-03-30 19:48:37 -04:00
|
|
|
utility('slice');
|
|
|
|
ref = new ValueNode(literal(utility('bind')));
|
2010-03-30 09:02:51 -04:00
|
|
|
return (new CallNode(ref, [literal(func), literal('this')])).compile(o);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
CodeNode.prototype.top_sensitive = function() {
|
2010-02-09 21:31:07 -05:00
|
|
|
return true;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-10 22:41:18 -04:00
|
|
|
// Short-circuit traverse_children method to prevent it from crossing scope boundaries
|
2010-05-10 06:58:01 -04:00
|
|
|
// unless cross_scope is true
|
2010-05-14 23:40:04 -04:00
|
|
|
CodeNode.prototype.traverse_children = function(cross_scope, func) {
|
2010-05-10 06:58:01 -04:00
|
|
|
if (cross_scope) {
|
2010-05-10 22:41:18 -04:00
|
|
|
return CodeNode.__superClass__.traverse_children.call(this, cross_scope, func);
|
2010-02-27 01:22:21 -05:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
CodeNode.prototype.toString = function(idt) {
|
2010-05-19 15:50:29 -04:00
|
|
|
var _b, _c, _d, _e, child, children;
|
2010-02-13 09:51:52 -05:00
|
|
|
idt = idt || '';
|
2010-03-06 00:23:54 -05:00
|
|
|
children = (function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
_b = []; _d = this.collect_children();
|
2010-04-25 22:29:43 -04:00
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
child = _d[_c];
|
|
|
|
_b.push(child.toString(idt + TAB));
|
2010-02-16 18:38:03 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _b;
|
2010-02-17 19:19:51 -05:00
|
|
|
}).call(this).join('');
|
2010-03-06 00:23:54 -05:00
|
|
|
return "\n" + idt + children;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return CodeNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 19:11:03 -05:00
|
|
|
//### SplatNode
|
2010-02-09 21:44:34 -05:00
|
|
|
// A splat, either as a parameter to a function, an argument to a call,
|
2010-03-08 04:28:26 -05:00
|
|
|
// or as part of a destructuring assignment.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.SplatNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
SplatNode = function(name) {
|
2010-02-11 23:57:31 -05:00
|
|
|
if (!(name.compile)) {
|
2010-02-28 00:22:06 -05:00
|
|
|
name = literal(name);
|
2010-02-11 23:57:31 -05:00
|
|
|
}
|
2010-05-10 06:58:01 -04:00
|
|
|
this.name = name;
|
2010-02-09 21:44:34 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(SplatNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
SplatNode.prototype.type = 'SplatNode';
|
|
|
|
SplatNode.prototype.children = ['name'];
|
2010-05-14 23:40:04 -04:00
|
|
|
SplatNode.prototype.compile_node = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b;
|
|
|
|
if ((typeof (_b = this.index) !== "undefined" && _b !== null)) {
|
2010-03-21 07:17:58 -04:00
|
|
|
return this.compile_param(o);
|
|
|
|
} else {
|
|
|
|
return this.name.compile(o);
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 04:28:26 -05:00
|
|
|
// Compiling a parameter splat means recovering the parameters that succeed
|
|
|
|
// the splat in the parameter list, by slicing the arguments object.
|
2010-05-14 23:40:04 -04:00
|
|
|
SplatNode.prototype.compile_param = function(o) {
|
2010-05-08 12:44:54 -04:00
|
|
|
var _b, _c, idx, len, name, pos, trailing, variadic;
|
2010-02-11 20:11:11 -05:00
|
|
|
name = this.name.compile(o);
|
|
|
|
o.scope.find(name);
|
2010-05-08 12:20:14 -04:00
|
|
|
len = o.scope.free_variable();
|
|
|
|
o.scope.assign(len, "arguments.length");
|
2010-05-08 12:44:54 -04:00
|
|
|
variadic = o.scope.free_variable();
|
|
|
|
o.scope.assign(variadic, ("" + len + " >= " + this.arglength));
|
2010-05-08 12:15:47 -04:00
|
|
|
_b = this.trailings;
|
2010-05-08 12:44:54 -04:00
|
|
|
for (idx = 0, _c = _b.length; idx < _c; idx++) {
|
|
|
|
trailing = _b[idx];
|
|
|
|
pos = this.trailings.length - idx;
|
|
|
|
o.scope.assign(trailing.compile(o), ("arguments[" + variadic + " ? " + len + " - " + pos + " : " + (this.index + idx) + "]"));
|
2010-03-17 14:41:09 -04:00
|
|
|
}
|
2010-05-08 12:20:14 -04:00
|
|
|
return "" + name + " = " + (utility('slice')) + ".call(arguments, " + this.index + ", " + len + " - " + (this.trailings.length) + ")";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 04:28:26 -05:00
|
|
|
// A compiling a splat as a destructuring assignment means slicing arguments
|
|
|
|
// from the right-hand-side's corresponding array.
|
2010-05-14 23:40:04 -04:00
|
|
|
SplatNode.prototype.compile_value = function(o, name, index, trailings) {
|
2010-03-30 19:42:09 -04:00
|
|
|
var trail;
|
2010-04-04 02:59:44 -04:00
|
|
|
trail = trailings ? (", " + (name) + ".length - " + trailings) : '';
|
2010-03-30 19:48:37 -04:00
|
|
|
return "" + (utility('slice')) + ".call(" + name + ", " + index + trail + ")";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-29 21:49:20 -04:00
|
|
|
// Utility function that converts arbitrary number of elements, mixed with
|
|
|
|
// splats, to a proper array
|
2010-05-14 23:40:04 -04:00
|
|
|
SplatNode.compile_mixed_array = function(list, o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, arg, args, code, i, prev;
|
2010-03-29 21:49:20 -04:00
|
|
|
args = [];
|
|
|
|
i = 0;
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = list;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
arg = _c[_b];
|
2010-03-29 21:49:20 -04:00
|
|
|
code = arg.compile(o);
|
|
|
|
if (!(arg instanceof SplatNode)) {
|
|
|
|
prev = args[i - 1];
|
|
|
|
if (i === 1 && prev.substr(0, 1) === '[' && prev.substr(prev.length - 1, 1) === ']') {
|
2010-04-04 02:59:44 -04:00
|
|
|
args[i - 1] = ("" + (prev.substr(0, prev.length - 1)) + ", " + code + "]");
|
2010-03-29 21:49:20 -04:00
|
|
|
continue;
|
|
|
|
} else if (i > 1 && prev.substr(0, 9) === '.concat([' && prev.substr(prev.length - 2, 2) === '])') {
|
2010-04-04 02:59:44 -04:00
|
|
|
args[i - 1] = ("" + (prev.substr(0, prev.length - 2)) + ", " + code + "])");
|
2010-03-29 21:49:20 -04:00
|
|
|
continue;
|
|
|
|
} else {
|
2010-04-04 02:59:44 -04:00
|
|
|
code = ("[" + code + "]");
|
2010-03-29 21:49:20 -04:00
|
|
|
}
|
2010-03-18 09:27:13 -04:00
|
|
|
}
|
2010-04-04 02:59:44 -04:00
|
|
|
args.push(i === 0 ? code : (".concat(" + code + ")"));
|
2010-03-29 21:49:20 -04:00
|
|
|
i += 1;
|
2010-03-18 09:27:13 -04:00
|
|
|
}
|
2010-03-29 21:49:20 -04:00
|
|
|
return args.join('');
|
|
|
|
};
|
|
|
|
return SplatNode;
|
|
|
|
}).call(this);
|
2010-03-08 04:28:26 -05:00
|
|
|
//### WhileNode
|
2010-02-09 22:05:17 -05:00
|
|
|
// A while loop, the only sort of low-level loop exposed by CoffeeScript. From
|
2010-03-08 04:28:26 -05:00
|
|
|
// it, all other loops can be manufactured. Useful in cases where you need more
|
|
|
|
// flexibility or more speed than a comprehension can provide.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.WhileNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
WhileNode = function(condition, opts) {
|
2010-04-28 22:08:00 -04:00
|
|
|
if (opts && opts.invert) {
|
2010-05-15 01:18:05 -04:00
|
|
|
if (condition instanceof OpNode) {
|
|
|
|
condition = new ParentheticalNode(condition);
|
|
|
|
}
|
2010-04-28 22:08:00 -04:00
|
|
|
condition = new OpNode('!', condition);
|
|
|
|
}
|
2010-05-10 06:58:01 -04:00
|
|
|
this.condition = condition;
|
2010-04-28 21:46:47 -04:00
|
|
|
this.guard = opts && opts.guard;
|
2010-02-23 22:53:43 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(WhileNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
WhileNode.prototype.type = 'WhileNode';
|
|
|
|
WhileNode.prototype.children = ['condition', 'guard', 'body'];
|
|
|
|
WhileNode.prototype.is_statement = function() {
|
|
|
|
return true;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
WhileNode.prototype.add_body = function(body) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.body = body;
|
2010-02-09 22:05:17 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
WhileNode.prototype.make_return = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = true;
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
WhileNode.prototype.top_sensitive = function() {
|
2010-02-09 22:05:17 -05:00
|
|
|
return true;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 04:28:26 -05:00
|
|
|
// The main difference from a JavaScript *while* is that the CoffeeScript
|
|
|
|
// *while* can be used as a part of a larger expression -- while loops may
|
|
|
|
// return an array containing the computed result of each iteration.
|
2010-05-14 23:40:04 -04:00
|
|
|
WhileNode.prototype.compile_node = function(o) {
|
2010-03-21 07:17:58 -04:00
|
|
|
var cond, post, pre, rvar, set, top;
|
2010-03-21 11:28:05 -04:00
|
|
|
top = del(o, 'top') && !this.returns;
|
2010-02-09 22:05:17 -05:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
o.top = true;
|
|
|
|
cond = this.condition.compile(o);
|
|
|
|
set = '';
|
2010-04-27 19:38:24 -04:00
|
|
|
if (!(top)) {
|
2010-02-09 22:05:17 -05:00
|
|
|
rvar = o.scope.free_variable();
|
2010-04-04 02:59:44 -04:00
|
|
|
set = ("" + this.tab + rvar + " = [];\n");
|
2010-02-23 22:53:43 -05:00
|
|
|
if (this.body) {
|
|
|
|
this.body = PushNode.wrap(rvar, this.body);
|
|
|
|
}
|
2010-02-09 22:05:17 -05:00
|
|
|
}
|
2010-04-04 02:59:44 -04:00
|
|
|
pre = ("" + set + (this.tab) + "while (" + cond + ")");
|
2010-04-28 21:46:47 -04:00
|
|
|
if (this.guard) {
|
|
|
|
this.body = Expressions.wrap([new IfNode(this.guard, this.body)]);
|
2010-02-23 22:53:43 -05:00
|
|
|
}
|
2010-05-12 21:47:31 -04:00
|
|
|
this.returns ? (post = '\n' + new ReturnNode(literal(rvar)).compile(merge(o, {
|
2010-03-21 07:17:58 -04:00
|
|
|
indent: this.idt()
|
|
|
|
}))) : (post = '');
|
2010-05-12 21:47:31 -04:00
|
|
|
return "" + pre + " {\n" + (this.body.compile(o)) + "\n" + this.tab + "}" + post;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return WhileNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-08 04:28:26 -05:00
|
|
|
//### OpNode
|
2010-02-09 22:20:04 -05:00
|
|
|
// Simple Arithmetic and logical operations. Performs some conversion from
|
|
|
|
// CoffeeScript operations into their JavaScript equivalents.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.OpNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
OpNode = function(operator, first, second, flip) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.first = first;
|
|
|
|
this.second = second;
|
2010-02-27 19:19:53 -05:00
|
|
|
this.operator = this.CONVERSIONS[operator] || operator;
|
|
|
|
this.flip = !!flip;
|
|
|
|
return this;
|
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(OpNode, BaseNode);
|
2010-03-08 04:28:26 -05:00
|
|
|
// The map of conversions from CoffeeScript to JavaScript symbols.
|
2010-02-27 19:19:53 -05:00
|
|
|
OpNode.prototype.CONVERSIONS = {
|
2010-02-09 22:20:04 -05:00
|
|
|
'==': '===',
|
2010-03-21 21:46:53 -04:00
|
|
|
'!=': '!=='
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 04:28:26 -05:00
|
|
|
// The list of operators for which we perform
|
|
|
|
// [Python-style comparison chaining](http://docs.python.org/reference/expressions.html#notin).
|
2010-02-27 19:19:53 -05:00
|
|
|
OpNode.prototype.CHAINABLE = ['<', '>', '>=', '<=', '===', '!=='];
|
2010-03-08 04:28:26 -05:00
|
|
|
// Our assignment operators that have no JavaScript equivalent.
|
2010-02-27 19:19:53 -05:00
|
|
|
OpNode.prototype.ASSIGNMENT = ['||=', '&&=', '?='];
|
2010-03-08 04:28:26 -05:00
|
|
|
// Operators must come before their operands with a space.
|
2010-02-27 19:19:53 -05:00
|
|
|
OpNode.prototype.PREFIX_OPERATORS = ['typeof', 'delete'];
|
2010-05-31 10:36:50 -04:00
|
|
|
OpNode.prototype.type = 'OpNode';
|
|
|
|
OpNode.prototype.children = ['first', 'second'];
|
2010-05-14 23:40:04 -04:00
|
|
|
OpNode.prototype.is_unary = function() {
|
2010-02-09 22:20:04 -05:00
|
|
|
return !this.second;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
OpNode.prototype.is_chainable = function() {
|
|
|
|
return index_of(this.CHAINABLE, this.operator) >= 0;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
OpNode.prototype.compile_node = function(o) {
|
2010-02-20 20:00:07 -05:00
|
|
|
o.operation = true;
|
2010-02-10 00:05:56 -05:00
|
|
|
if (this.is_chainable() && this.first.unwrap() instanceof OpNode && this.first.unwrap().is_chainable()) {
|
2010-02-09 22:20:04 -05:00
|
|
|
return this.compile_chain(o);
|
|
|
|
}
|
2010-05-14 23:40:04 -04:00
|
|
|
if (index_of(this.ASSIGNMENT, this.operator) >= 0) {
|
2010-02-10 00:05:56 -05:00
|
|
|
return this.compile_assignment(o);
|
|
|
|
}
|
|
|
|
if (this.is_unary()) {
|
|
|
|
return this.compile_unary(o);
|
|
|
|
}
|
|
|
|
if (this.operator === '?') {
|
|
|
|
return this.compile_existence(o);
|
|
|
|
}
|
2010-03-06 00:23:54 -05:00
|
|
|
return [this.first.compile(o), this.operator, this.second.compile(o)].join(' ');
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 04:28:26 -05:00
|
|
|
// Mimic Python's chained comparisons when multiple comparison operators are
|
2010-03-08 05:19:48 -05:00
|
|
|
// used sequentially. For example:
|
|
|
|
// bin/coffee -e "puts 50 < 65 > 10"
|
|
|
|
// true
|
2010-05-14 23:40:04 -04:00
|
|
|
OpNode.prototype.compile_chain = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, first, second, shared;
|
2010-02-10 00:05:56 -05:00
|
|
|
shared = this.first.unwrap().second;
|
2010-03-31 22:48:47 -04:00
|
|
|
if (shared.contains_type(CallNode)) {
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = shared.compile_reference(o);
|
|
|
|
this.first.second = _b[0];
|
|
|
|
shared = _b[1];
|
2010-02-10 00:05:56 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = [this.first.compile(o), this.second.compile(o), shared.compile(o)];
|
|
|
|
first = _c[0];
|
|
|
|
second = _c[1];
|
|
|
|
shared = _c[2];
|
2010-03-06 00:23:54 -05:00
|
|
|
return "(" + first + ") && (" + shared + " " + this.operator + " " + second + ")";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 04:28:26 -05:00
|
|
|
// When compiling a conditional assignment, take care to ensure that the
|
|
|
|
// operands are only evaluated once, even though we have to reference them
|
|
|
|
// more than once.
|
2010-05-14 23:40:04 -04:00
|
|
|
OpNode.prototype.compile_assignment = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, first, second;
|
|
|
|
_b = [this.first.compile(o), this.second.compile(o)];
|
|
|
|
first = _b[0];
|
|
|
|
second = _b[1];
|
2010-02-12 14:15:14 -05:00
|
|
|
if (first.match(IDENTIFIER)) {
|
2010-02-10 00:05:56 -05:00
|
|
|
o.scope.find(first);
|
|
|
|
}
|
|
|
|
if (this.operator === '?=') {
|
2010-04-04 02:59:44 -04:00
|
|
|
return ("" + first + " = " + (ExistenceNode.compile_test(o, this.first)) + " ? " + first + " : " + second);
|
2010-02-10 00:05:56 -05:00
|
|
|
}
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + first + " = " + first + " " + (this.operator.substr(0, 2)) + " " + second;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 05:19:48 -05:00
|
|
|
// If this is an existence operator, we delegate to `ExistenceNode.compile_test`
|
|
|
|
// to give us the safe references for the variables.
|
2010-05-14 23:40:04 -04:00
|
|
|
OpNode.prototype.compile_existence = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, first, second, test;
|
|
|
|
_b = [this.first.compile(o), this.second.compile(o)];
|
|
|
|
first = _b[0];
|
|
|
|
second = _b[1];
|
2010-03-06 00:23:54 -05:00
|
|
|
test = ExistenceNode.compile_test(o, this.first);
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + test + " ? " + first + " : " + second;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 05:19:48 -05:00
|
|
|
// Compile a unary **OpNode**.
|
2010-05-14 23:40:04 -04:00
|
|
|
OpNode.prototype.compile_unary = function(o) {
|
2010-02-10 00:05:56 -05:00
|
|
|
var parts, space;
|
2010-05-14 23:40:04 -04:00
|
|
|
space = index_of(this.PREFIX_OPERATORS, this.operator) >= 0 ? ' ' : '';
|
2010-02-10 00:05:56 -05:00
|
|
|
parts = [this.operator, space, this.first.compile(o)];
|
|
|
|
if (this.flip) {
|
|
|
|
parts = parts.reverse();
|
|
|
|
}
|
|
|
|
return parts.join('');
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return OpNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-08 05:19:48 -05:00
|
|
|
//### TryNode
|
|
|
|
// A classic *try/catch/finally* block.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.TryNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
TryNode = function(attempt, error, recovery, ensure) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.attempt = attempt;
|
|
|
|
this.recovery = recovery;
|
|
|
|
this.ensure = ensure;
|
2010-02-10 18:33:03 -05:00
|
|
|
this.error = error;
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(TryNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
TryNode.prototype.type = 'TryNode';
|
|
|
|
TryNode.prototype.children = ['attempt', 'recovery', 'ensure'];
|
|
|
|
TryNode.prototype.is_statement = function() {
|
|
|
|
return true;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
TryNode.prototype.make_return = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
if (this.attempt) {
|
|
|
|
this.attempt = this.attempt.make_return();
|
|
|
|
}
|
|
|
|
if (this.recovery) {
|
|
|
|
this.recovery = this.recovery.make_return();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2010-03-08 05:19:48 -05:00
|
|
|
// Compilation is more or less as you would expect -- the *finally* clause
|
|
|
|
// is optional, the *catch* is not.
|
2010-05-14 23:40:04 -04:00
|
|
|
TryNode.prototype.compile_node = function(o) {
|
2010-03-06 00:23:54 -05:00
|
|
|
var attempt_part, catch_part, error_part, finally_part;
|
2010-02-10 18:33:03 -05:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
o.top = true;
|
2010-03-06 00:23:54 -05:00
|
|
|
attempt_part = this.attempt.compile(o);
|
2010-04-04 02:59:44 -04:00
|
|
|
error_part = this.error ? (" (" + (this.error.compile(o)) + ") ") : ' ';
|
|
|
|
catch_part = this.recovery ? (" catch" + error_part + "{\n" + (this.recovery.compile(o)) + "\n" + this.tab + "}") : '';
|
|
|
|
finally_part = (this.ensure || '') && ' finally {\n' + this.ensure.compile(merge(o)) + ("\n" + this.tab + "}");
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + (this.tab) + "try {\n" + attempt_part + "\n" + this.tab + "}" + catch_part + finally_part;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return TryNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-08 05:19:48 -05:00
|
|
|
//### ThrowNode
|
2010-03-08 05:42:57 -05:00
|
|
|
// Simple node to throw an exception.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ThrowNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ThrowNode = function(expression) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expression = expression;
|
2010-02-10 19:13:11 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ThrowNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
ThrowNode.prototype.type = 'ThrowNode';
|
|
|
|
ThrowNode.prototype.children = ['expression'];
|
|
|
|
ThrowNode.prototype.is_statement = function() {
|
|
|
|
return true;
|
|
|
|
};
|
2010-03-21 11:28:05 -04:00
|
|
|
// A **ThrowNode** is already a return, of sorts...
|
2010-05-14 23:40:04 -04:00
|
|
|
ThrowNode.prototype.make_return = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ThrowNode.prototype.compile_node = function(o) {
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + (this.tab) + "throw " + (this.expression.compile(o)) + ";";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ThrowNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-08 05:42:57 -05:00
|
|
|
//### 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.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ExistenceNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ExistenceNode = function(expression) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expression = expression;
|
2010-02-10 19:13:11 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ExistenceNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
ExistenceNode.prototype.type = 'ExistenceNode';
|
|
|
|
ExistenceNode.prototype.children = ['expression'];
|
2010-05-14 23:40:04 -04:00
|
|
|
ExistenceNode.prototype.compile_node = function(o) {
|
2010-02-10 19:13:11 -05:00
|
|
|
return ExistenceNode.compile_test(o, this.expression);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-29 21:49:20 -04:00
|
|
|
// The meat of the **ExistenceNode** is in this static `compile_test` method
|
|
|
|
// because other nodes like to check the existence of their variables as well.
|
|
|
|
// Be careful not to double-evaluate anything.
|
2010-05-14 23:40:04 -04:00
|
|
|
ExistenceNode.compile_test = function(o, variable) {
|
2010-05-31 19:38:45 -04:00
|
|
|
var _b, first, second;
|
|
|
|
_b = variable.compile_reference(o, true);
|
2010-04-25 22:29:43 -04:00
|
|
|
first = _b[0];
|
|
|
|
second = _b[1];
|
2010-05-31 19:38:45 -04:00
|
|
|
return "(typeof " + (first.compile(o)) + " !== \"undefined\" && " + (second.compile(o)) + " !== null)";
|
2010-03-29 21:49:20 -04:00
|
|
|
};
|
2010-02-27 19:19:53 -05:00
|
|
|
return ExistenceNode;
|
|
|
|
}).call(this);
|
2010-03-08 05:42:57 -05:00
|
|
|
//### 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
|
|
|
|
// parentheses, but no longer -- you can put in as many as you please.
|
|
|
|
// Parentheses are a good way to force any statement to become an expression.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ParentheticalNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ParentheticalNode = function(expression) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expression = expression;
|
2010-02-10 19:13:11 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ParentheticalNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
ParentheticalNode.prototype.type = 'ParentheticalNode';
|
|
|
|
ParentheticalNode.prototype.children = ['expression'];
|
2010-05-14 23:40:04 -04:00
|
|
|
ParentheticalNode.prototype.is_statement = function() {
|
2010-02-17 19:19:51 -05:00
|
|
|
return this.expression.is_statement();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ParentheticalNode.prototype.make_return = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
return this.expression.make_return();
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ParentheticalNode.prototype.compile_node = function(o) {
|
2010-02-10 19:13:11 -05:00
|
|
|
var code, l;
|
2010-02-17 19:19:51 -05:00
|
|
|
code = this.expression.compile(o);
|
|
|
|
if (this.is_statement()) {
|
|
|
|
return code;
|
|
|
|
}
|
2010-02-10 19:13:11 -05:00
|
|
|
l = code.length;
|
|
|
|
if (code.substr(l - 1, 1) === ';') {
|
|
|
|
code = code.substr(o, l - 1);
|
|
|
|
}
|
2010-03-23 00:18:50 -04:00
|
|
|
if (this.expression instanceof AssignNode) {
|
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(" + code + ")";
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ParentheticalNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-08 05:42:57 -05:00
|
|
|
//### 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
|
|
|
|
// expression, able to return the result of each filtered iteration.
|
|
|
|
// Unlike Python array comprehensions, they can be multi-line, and you can pass
|
|
|
|
// the current index of the loop as a second parameter. Unlike Ruby blocks,
|
|
|
|
// you can map and filter in a single pass.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ForNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ForNode = function(body, source, name, index) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b;
|
2010-02-10 20:19:59 -05:00
|
|
|
this.body = body;
|
|
|
|
this.name = name;
|
|
|
|
this.index = index || null;
|
|
|
|
this.source = source.source;
|
2010-04-28 21:46:47 -04:00
|
|
|
this.guard = source.guard;
|
2010-02-10 20:19:59 -05:00
|
|
|
this.step = source.step;
|
|
|
|
this.object = !!source.object;
|
|
|
|
if (this.object) {
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = [this.index, this.name];
|
|
|
|
this.name = _b[0];
|
|
|
|
this.index = _b[1];
|
2010-02-10 20:19:59 -05:00
|
|
|
}
|
2010-04-26 14:35:35 -04:00
|
|
|
this.pattern = this.name instanceof ValueNode;
|
2010-04-26 14:54:31 -04:00
|
|
|
if (this.index instanceof ValueNode) {
|
2010-04-26 14:35:35 -04:00
|
|
|
throw new Error('index cannot be a pattern matching expression');
|
|
|
|
}
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = false;
|
2010-02-10 20:19:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ForNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
ForNode.prototype.type = 'ForNode';
|
|
|
|
ForNode.prototype.children = ['body', 'source', 'guard'];
|
|
|
|
ForNode.prototype.is_statement = function() {
|
|
|
|
return true;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ForNode.prototype.top_sensitive = function() {
|
2010-02-10 20:19:59 -05:00
|
|
|
return true;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ForNode.prototype.make_return = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = true;
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ForNode.prototype.compile_return_value = function(val, o) {
|
2010-03-21 11:28:05 -04:00
|
|
|
if (this.returns) {
|
2010-05-12 21:47:31 -04:00
|
|
|
return '\n' + new ReturnNode(literal(val)).compile(o);
|
|
|
|
}
|
|
|
|
if (val) {
|
|
|
|
return '\n' + val;
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
2010-05-12 21:47:31 -04:00
|
|
|
return '';
|
2010-03-21 07:17:58 -04:00
|
|
|
};
|
2010-03-08 05:42:57 -05:00
|
|
|
// Welcome to the hairiest method in all of CoffeeScript. Handles the inner
|
|
|
|
// loop, filtering, stepping, and result saving for array, object, and range
|
|
|
|
// comprehensions. Some of the generated code can be shared in common, and
|
|
|
|
// some cannot.
|
2010-05-14 23:40:04 -04:00
|
|
|
ForNode.prototype.compile_node = function(o) {
|
2010-04-30 03:48:30 -04:00
|
|
|
var body, body_dent, close, for_part, index, ivar, lvar, name, range, return_result, rvar, scope, set_result, source, source_part, step_part, svar, top_level, var_part, vars;
|
2010-03-21 11:28:05 -04:00
|
|
|
top_level = del(o, 'top') && !this.returns;
|
2010-02-10 20:19:59 -05:00
|
|
|
range = this.source instanceof ValueNode && this.source.base instanceof RangeNode && !this.source.properties.length;
|
|
|
|
source = range ? this.source.base : this.source;
|
|
|
|
scope = o.scope;
|
|
|
|
name = this.name && this.name.compile(o);
|
|
|
|
index = this.index && this.index.compile(o);
|
2010-04-26 14:35:35 -04:00
|
|
|
if (name && !this.pattern) {
|
2010-03-10 22:32:00 -05:00
|
|
|
scope.find(name);
|
|
|
|
}
|
|
|
|
if (index) {
|
|
|
|
scope.find(index);
|
|
|
|
}
|
2010-02-10 20:19:59 -05:00
|
|
|
body_dent = this.idt(1);
|
|
|
|
if (!(top_level)) {
|
|
|
|
rvar = scope.free_variable();
|
|
|
|
}
|
|
|
|
ivar = range ? name : index || scope.free_variable();
|
|
|
|
var_part = '';
|
|
|
|
body = Expressions.wrap([this.body]);
|
|
|
|
if (range) {
|
|
|
|
source_part = source.compile_variables(o);
|
2010-03-14 17:58:32 -04:00
|
|
|
for_part = source.compile(merge(o, {
|
2010-02-10 20:19:59 -05:00
|
|
|
index: ivar,
|
|
|
|
step: this.step
|
2010-03-06 00:23:54 -05:00
|
|
|
}));
|
2010-02-10 20:19:59 -05:00
|
|
|
} else {
|
2010-03-27 16:04:47 -04:00
|
|
|
svar = scope.free_variable();
|
2010-04-04 02:59:44 -04:00
|
|
|
source_part = ("" + svar + " = " + (this.source.compile(o)) + ";\n" + this.tab);
|
2010-04-26 14:35:35 -04:00
|
|
|
if (this.pattern) {
|
2010-04-26 23:46:35 -04:00
|
|
|
var_part = new AssignNode(this.name, literal(("" + svar + "[" + ivar + "]"))).compile(merge(o, {
|
|
|
|
indent: this.idt(1),
|
|
|
|
top: true
|
|
|
|
})) + "\n";
|
2010-04-27 09:32:45 -04:00
|
|
|
} else {
|
|
|
|
if (name) {
|
|
|
|
var_part = ("" + body_dent + name + " = " + svar + "[" + ivar + "];\n");
|
|
|
|
}
|
2010-02-10 20:19:59 -05:00
|
|
|
}
|
2010-04-27 19:38:24 -04:00
|
|
|
if (!(this.object)) {
|
2010-02-25 23:39:14 -05:00
|
|
|
lvar = scope.free_variable();
|
2010-04-04 02:59:44 -04:00
|
|
|
step_part = this.step ? ("" + ivar + " += " + (this.step.compile(o))) : ("" + ivar + "++");
|
|
|
|
for_part = ("" + ivar + " = 0, " + lvar + " = " + (svar) + ".length; " + ivar + " < " + lvar + "; " + step_part);
|
2010-02-25 23:39:14 -05:00
|
|
|
}
|
2010-02-10 20:19:59 -05:00
|
|
|
}
|
|
|
|
set_result = rvar ? this.idt() + rvar + ' = []; ' : this.idt();
|
2010-03-21 07:17:58 -04:00
|
|
|
return_result = this.compile_return_value(rvar, o);
|
2010-03-16 01:27:31 -04:00
|
|
|
if (top_level && body.contains(function(n) {
|
2010-02-10 20:19:59 -05:00
|
|
|
return n instanceof CodeNode;
|
2010-03-16 01:27:31 -04:00
|
|
|
})) {
|
|
|
|
body = ClosureNode.wrap(body, true);
|
|
|
|
}
|
2010-02-10 20:19:59 -05:00
|
|
|
if (!(top_level)) {
|
|
|
|
body = PushNode.wrap(rvar, body);
|
|
|
|
}
|
2010-04-28 21:46:47 -04:00
|
|
|
this.guard ? (body = Expressions.wrap([new IfNode(this.guard, body)])) : null;
|
2010-04-04 02:59:44 -04:00
|
|
|
this.object ? (for_part = ("" + ivar + " in " + svar + ") { if (" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")")) : null;
|
2010-03-14 17:58:32 -04:00
|
|
|
body = body.compile(merge(o, {
|
2010-02-10 20:19:59 -05:00
|
|
|
indent: body_dent,
|
|
|
|
top: true
|
|
|
|
}));
|
2010-04-04 02:59:44 -04:00
|
|
|
vars = range ? name : ("" + name + ", " + ivar);
|
2010-05-12 21:47:31 -04:00
|
|
|
close = this.object ? '}}' : '}';
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + set_result + (source_part) + "for (" + for_part + ") {\n" + var_part + body + "\n" + this.tab + close + return_result;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ForNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-08 05:42:57 -05:00
|
|
|
//### 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.
|
|
|
|
// Single-expression **IfNodes** are compiled into ternary operators if possible,
|
|
|
|
// because ternaries are already proper expressions, and don't need conversion.
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.IfNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode = function(condition, body, tags) {
|
2010-02-10 21:40:10 -05:00
|
|
|
this.condition = condition;
|
2010-04-27 09:32:45 -04:00
|
|
|
this.body = body;
|
|
|
|
this.else_body = null;
|
2010-02-13 01:13:08 -05:00
|
|
|
this.tags = tags || {};
|
2010-02-10 21:40:10 -05:00
|
|
|
if (this.tags.invert) {
|
|
|
|
this.condition = new OpNode('!', new ParentheticalNode(this.condition));
|
|
|
|
}
|
2010-04-27 09:32:45 -04:00
|
|
|
this.is_chain = false;
|
2010-02-10 21:40:10 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(IfNode, BaseNode);
|
2010-05-31 10:36:50 -04:00
|
|
|
IfNode.prototype.type = 'IfNode';
|
|
|
|
IfNode.prototype.children = ['condition', 'switch_subject', 'body', 'else_body', 'assigner'];
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.body_node = function() {
|
2010-04-27 09:32:45 -04:00
|
|
|
return this.body == undefined ? undefined : this.body.unwrap();
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.else_body_node = function() {
|
2010-04-27 09:32:45 -04:00
|
|
|
return this.else_body == undefined ? undefined : this.else_body.unwrap();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.force_statement = function() {
|
2010-02-10 21:40:10 -05:00
|
|
|
this.tags.statement = true;
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 05:42:57 -05:00
|
|
|
// Tag a chain of **IfNodes** with their object(s) to switch on for equality
|
|
|
|
// tests. `rewrite_switch` will perform the actual change at compile time.
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.switches_over = function(expression) {
|
2010-04-27 09:32:45 -04:00
|
|
|
this.switch_subject = expression;
|
2010-02-17 21:23:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 05:42:57 -05:00
|
|
|
// Rewrite a chain of **IfNodes** with their switch condition for equality.
|
|
|
|
// Ensure that the switch expression isn't evaluated more than once.
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.rewrite_switch = function(o) {
|
2010-05-10 06:58:01 -04:00
|
|
|
var _b, _c, _d, cond, i, variable;
|
|
|
|
this.assigner = this.switch_subject;
|
2010-04-27 09:32:45 -04:00
|
|
|
if (!((this.switch_subject.unwrap() instanceof LiteralNode))) {
|
2010-02-28 00:22:06 -05:00
|
|
|
variable = literal(o.scope.free_variable());
|
2010-05-10 06:58:01 -04:00
|
|
|
this.assigner = new AssignNode(variable, this.switch_subject);
|
2010-04-27 09:32:45 -04:00
|
|
|
this.switch_subject = variable;
|
2010-02-17 21:23:59 -05:00
|
|
|
}
|
2010-02-10 21:40:10 -05:00
|
|
|
this.condition = (function() {
|
2010-05-10 22:50:11 -04:00
|
|
|
_b = []; _c = flatten([this.condition]);
|
|
|
|
for (i = 0, _d = _c.length; i < _d; i++) {
|
|
|
|
cond = _c[i];
|
|
|
|
_b.push((function() {
|
|
|
|
if (cond instanceof OpNode) {
|
|
|
|
cond = new ParentheticalNode(cond);
|
|
|
|
}
|
|
|
|
return new OpNode('==', (i === 0 ? this.assigner : this.switch_subject), cond);
|
|
|
|
}).call(this));
|
2010-02-10 21:40:10 -05:00
|
|
|
}
|
2010-05-10 22:50:11 -04:00
|
|
|
return _b;
|
2010-02-10 21:40:10 -05:00
|
|
|
}).call(this);
|
2010-04-27 09:32:45 -04:00
|
|
|
if (this.is_chain) {
|
|
|
|
this.else_body_node().switches_over(this.switch_subject);
|
2010-02-10 21:40:10 -05:00
|
|
|
}
|
2010-04-27 09:32:45 -04:00
|
|
|
// prevent this rewrite from happening again
|
|
|
|
this.switch_subject = undefined;
|
2010-02-10 21:40:10 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 05:42:57 -05:00
|
|
|
// Rewrite a chain of **IfNodes** to add a default case as the final *else*.
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.add_else = function(else_body, statement) {
|
2010-04-27 09:32:45 -04:00
|
|
|
if (this.is_chain) {
|
|
|
|
this.else_body_node().add_else(else_body, statement);
|
2010-02-22 19:22:09 -05:00
|
|
|
} else {
|
2010-04-27 09:32:45 -04:00
|
|
|
this.is_chain = else_body instanceof IfNode;
|
|
|
|
this.else_body = this.ensure_expressions(else_body);
|
2010-02-22 19:22:09 -05:00
|
|
|
}
|
2010-02-10 21:40:10 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 05:42:57 -05:00
|
|
|
// The **IfNode** only compiles into a statement if either of its bodies needs
|
|
|
|
// to be a statement. Otherwise a ternary is safe.
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.is_statement = function() {
|
2010-04-27 09:32:45 -04:00
|
|
|
return this.statement = this.statement || !!(this.comment || this.tags.statement || this.body_node().is_statement() || (this.else_body && this.else_body_node().is_statement()));
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.compile_condition = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, _e, cond;
|
2010-02-17 19:19:51 -05:00
|
|
|
return (function() {
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = []; _d = flatten([this.condition]);
|
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
cond = _d[_c];
|
|
|
|
_b.push(cond.compile(o));
|
2010-02-10 21:40:10 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _b;
|
2010-02-17 19:19:51 -05:00
|
|
|
}).call(this).join(' || ');
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.compile_node = function(o) {
|
2010-03-21 07:17:58 -04:00
|
|
|
if (this.is_statement()) {
|
|
|
|
return this.compile_statement(o);
|
|
|
|
} else {
|
|
|
|
return this.compile_ternary(o);
|
|
|
|
}
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.make_return = function() {
|
2010-04-27 09:32:45 -04:00
|
|
|
this.body = this.body && this.ensure_expressions(this.body.make_return());
|
|
|
|
this.else_body = this.else_body && this.ensure_expressions(this.else_body.make_return());
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.ensure_expressions = function(node) {
|
2010-04-27 09:32:45 -04:00
|
|
|
if (!(node instanceof Expressions)) {
|
|
|
|
node = new Expressions([node]);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
};
|
2010-03-08 05:42:57 -05:00
|
|
|
// Compile the **IfNode** as a regular *if-else* statement. Flattened chains
|
|
|
|
// force inner *else* bodies into statement form.
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.compile_statement = function(o) {
|
2010-02-10 21:40:10 -05:00
|
|
|
var body, child, com_dent, cond_o, else_part, if_dent, if_part, prefix;
|
2010-04-27 09:32:45 -04:00
|
|
|
if (this.switch_subject) {
|
2010-02-17 21:23:59 -05:00
|
|
|
this.rewrite_switch(o);
|
|
|
|
}
|
2010-03-14 17:58:32 -04:00
|
|
|
child = del(o, 'chain_child');
|
|
|
|
cond_o = merge(o);
|
2010-02-10 21:40:10 -05:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
o.top = true;
|
|
|
|
if_dent = child ? '' : this.idt();
|
|
|
|
com_dent = child ? this.idt() : '';
|
2010-04-04 02:59:44 -04:00
|
|
|
prefix = this.comment ? ("" + (this.comment.compile(cond_o)) + "\n" + com_dent) : '';
|
2010-04-27 09:32:45 -04:00
|
|
|
body = this.body.compile(o);
|
2010-04-04 02:59:44 -04:00
|
|
|
if_part = ("" + prefix + (if_dent) + "if (" + (this.compile_condition(cond_o)) + ") {\n" + body + "\n" + this.tab + "}");
|
2010-02-10 21:40:10 -05:00
|
|
|
if (!(this.else_body)) {
|
|
|
|
return if_part;
|
|
|
|
}
|
2010-04-27 09:32:45 -04:00
|
|
|
else_part = this.is_chain ? ' else ' + this.else_body_node().compile(merge(o, {
|
2010-02-10 21:40:10 -05:00
|
|
|
indent: this.idt(),
|
|
|
|
chain_child: true
|
2010-04-27 09:32:45 -04:00
|
|
|
})) : (" else {\n" + (this.else_body.compile(o)) + "\n" + this.tab + "}");
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + if_part + else_part;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-08 05:42:57 -05:00
|
|
|
// Compile the IfNode as a ternary operator.
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode.prototype.compile_ternary = function(o) {
|
2010-02-10 21:40:10 -05:00
|
|
|
var else_part, if_part;
|
2010-04-27 09:32:45 -04:00
|
|
|
if_part = this.condition.compile(o) + ' ? ' + this.body_node().compile(o);
|
|
|
|
else_part = this.else_body ? this.else_body_node().compile(o) : 'null';
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + if_part + " : " + else_part;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return IfNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-09 23:04:16 -05:00
|
|
|
// Faux-Nodes
|
|
|
|
// ----------
|
|
|
|
//### PushNode
|
|
|
|
// Faux-nodes are never created by the grammar, but are used during code
|
|
|
|
// generation to generate other combinations of nodes. The **PushNode** creates
|
|
|
|
// the tree for `array.push(value)`, which is helpful for recording the result
|
|
|
|
// arrays from comprehensions.
|
|
|
|
PushNode = (exports.PushNode = {
|
2010-05-14 23:40:04 -04:00
|
|
|
wrap: function(array, expressions) {
|
2010-03-09 23:04:16 -05:00
|
|
|
var expr;
|
|
|
|
expr = expressions.unwrap();
|
2010-03-21 11:28:05 -04:00
|
|
|
if (expr.is_pure_statement() || expr.contains_pure_statement()) {
|
2010-03-09 23:04:16 -05:00
|
|
|
return expressions;
|
|
|
|
}
|
|
|
|
return Expressions.wrap([new CallNode(new ValueNode(literal(array), [new AccessorNode(literal('push'))]), [expr])]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//### ClosureNode
|
|
|
|
// A faux-node used to wrap an expressions body in a closure.
|
|
|
|
ClosureNode = (exports.ClosureNode = {
|
2010-03-16 01:27:31 -04:00
|
|
|
// Wrap the expressions body, unless it contains a pure statement,
|
2010-04-10 14:40:05 -04:00
|
|
|
// in which case, no dice. If the body mentions `this` or `arguments`,
|
|
|
|
// then make sure that the closure wrapper preserves the original values.
|
2010-05-14 23:40:04 -04:00
|
|
|
wrap: function(expressions, statement) {
|
2010-04-10 14:40:05 -04:00
|
|
|
var args, call, func, mentions_args, mentions_this, meth;
|
2010-03-21 11:28:05 -04:00
|
|
|
if (expressions.contains_pure_statement()) {
|
2010-03-16 01:27:31 -04:00
|
|
|
return expressions;
|
|
|
|
}
|
2010-04-10 14:40:05 -04:00
|
|
|
func = new ParentheticalNode(new CodeNode([], Expressions.wrap([expressions])));
|
|
|
|
args = [];
|
2010-04-10 14:20:32 -04:00
|
|
|
mentions_args = expressions.contains(function(n) {
|
|
|
|
return (n instanceof LiteralNode) && (n.value === 'arguments');
|
|
|
|
});
|
2010-04-10 14:40:05 -04:00
|
|
|
mentions_this = expressions.contains(function(n) {
|
|
|
|
return (n instanceof LiteralNode) && (n.value === 'this');
|
|
|
|
});
|
|
|
|
if (mentions_args || mentions_this) {
|
|
|
|
meth = literal(mentions_args ? 'apply' : 'call');
|
|
|
|
args = [literal('this')];
|
|
|
|
if (mentions_args) {
|
|
|
|
args.push(literal('arguments'));
|
|
|
|
}
|
|
|
|
func = new ValueNode(func, [new AccessorNode(meth)]);
|
2010-04-10 14:20:32 -04:00
|
|
|
}
|
2010-04-10 14:40:05 -04:00
|
|
|
call = new CallNode(func, args);
|
2010-03-21 07:17:58 -04:00
|
|
|
if (statement) {
|
|
|
|
return Expressions.wrap([call]);
|
|
|
|
} else {
|
|
|
|
return call;
|
|
|
|
}
|
2010-03-09 23:04:16 -05:00
|
|
|
}
|
|
|
|
});
|
2010-03-30 19:42:09 -04:00
|
|
|
// Utility Functions
|
|
|
|
// -----------------
|
|
|
|
UTILITIES = {
|
2010-03-30 20:06:44 -04:00
|
|
|
// Correctly set up a prototype chain for inheritance, including a reference
|
|
|
|
// to the superclass for `super()` calls. See:
|
2010-03-31 00:04:14 -04:00
|
|
|
// [goog.inherits](http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html#line1206).
|
2010-05-14 21:50:17 -04:00
|
|
|
__extends: "function(child, parent) {\n var ctor = function(){ };\n ctor.prototype = parent.prototype;\n child.__superClass__ = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n }",
|
2010-03-30 20:06:44 -04:00
|
|
|
// Bind a function to a calling context, optionally including curried arguments.
|
2010-03-31 00:04:14 -04:00
|
|
|
// See [Underscore's implementation](http://jashkenas.github.com/coffee-script/documentation/docs/underscore.html#section-47).
|
2010-05-14 21:50:17 -04:00
|
|
|
__bind: "function(func, obj, args) {\n return function() {\n return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);\n };\n }",
|
2010-04-26 00:00:12 -04:00
|
|
|
// Shortcuts to speed up the lookup time for native functions.
|
2010-03-30 20:15:51 -04:00
|
|
|
__hasProp: 'Object.prototype.hasOwnProperty',
|
|
|
|
__slice: 'Array.prototype.slice'
|
2010-03-30 19:42:09 -04:00
|
|
|
};
|
2010-03-07 16:41:06 -05:00
|
|
|
// Constants
|
|
|
|
// ---------
|
|
|
|
// Tabs are two spaces for pretty printing.
|
|
|
|
TAB = ' ';
|
2010-03-08 05:42:57 -05:00
|
|
|
// Trim out all trailing whitespace, so that the generated code plays nice
|
|
|
|
// with Git.
|
2010-05-12 21:47:31 -04:00
|
|
|
TRAILING_WHITESPACE = /[ \t]+$/gm;
|
2010-03-08 05:42:57 -05:00
|
|
|
// Keep this identifier regex in sync with the Lexer.
|
2010-03-08 13:05:02 -05:00
|
|
|
IDENTIFIER = /^[a-zA-Z\$_](\w|\$)*$/;
|
2010-04-25 11:07:09 -04:00
|
|
|
// Is a literal value a string?
|
|
|
|
IS_STRING = /^['"]/;
|
2010-03-07 16:41:06 -05:00
|
|
|
// Utility Functions
|
|
|
|
// -----------------
|
2010-03-08 05:42:57 -05:00
|
|
|
// Handy helper for a generating LiteralNode.
|
2010-05-14 23:40:04 -04:00
|
|
|
literal = function(name) {
|
2010-03-07 16:41:06 -05:00
|
|
|
return new LiteralNode(name);
|
|
|
|
};
|
2010-03-30 19:48:37 -04:00
|
|
|
// Helper for ensuring that utility functions are assigned at the top level.
|
2010-05-14 23:40:04 -04:00
|
|
|
utility = function(name) {
|
2010-03-30 19:48:37 -04:00
|
|
|
var ref;
|
2010-04-04 02:59:44 -04:00
|
|
|
ref = ("__" + name);
|
2010-03-30 20:15:51 -04:00
|
|
|
Scope.root.assign(ref, UTILITIES[ref]);
|
2010-03-30 19:48:37 -04:00
|
|
|
return ref;
|
|
|
|
};
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|