making sure that the body of extends only gets defined once per file.

This commit is contained in:
Jeremy Ashkenas
2010-02-27 19:29:34 -05:00
parent 8f871a8218
commit d2cb1f321e
2 changed files with 162 additions and 276 deletions

View File

@@ -1,6 +1,12 @@
(function(){ (function(){
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, ValueNode, WhileNode, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, compact, del, flatten, inherit, merge, statement; var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, ValueNode, WhileNode, compact, del, flatten, inherit, merge, statement;
var __hasProp = Object.prototype.hasOwnProperty; var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
child.__superClass__ = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
};
(typeof process !== "undefined" && process !== null) ? process.mixin(require('scope')) : (this.exports = this); (typeof process !== "undefined" && process !== null) ? process.mixin(require('scope')) : (this.exports = this);
// Some helper functions // Some helper functions
// Tabs are two spaces for pretty printing. // Tabs are two spaces for pretty printing.
@@ -58,16 +64,12 @@
}; };
// Quickie inheritance convenience wrapper to reduce typing. // Quickie inheritance convenience wrapper to reduce typing.
inherit = function inherit(parent, props) { inherit = function inherit(parent, props) {
var _a, _b, klass, name, prop; var _a, klass, name, prop;
klass = del(props, 'constructor'); klass = del(props, 'constructor');
_a = function(){}; __extends(klass, parent);
_a.prototype = parent.prototype; _a = props;
klass.__superClass__ = parent.prototype; for (name in _a) { if (__hasProp.call(_a, name)) {
klass.prototype = new _a(); prop = _a[name];
klass.prototype.constructor = klass;
_b = props;
for (name in _b) { if (__hasProp.call(_b, name)) {
prop = _b[name];
((klass.prototype[name] = prop)); ((klass.prototype[name] = prop));
}} }}
return klass; return klass;
@@ -202,11 +204,7 @@
this.children = (this.expressions = compact(flatten(nodes || []))); this.children = (this.expressions = compact(flatten(nodes || [])));
return this; return this;
}; };
_a = function(){}; __extends(Expressions, BaseNode);
_a.prototype = BaseNode.prototype;
Expressions.__superClass__ = BaseNode.prototype;
Expressions.prototype = new _a();
Expressions.prototype.constructor = Expressions;
Expressions.prototype.type = 'Expressions'; Expressions.prototype.type = 'Expressions';
// Tack an expression on to the end of this expression list. // Tack an expression on to the end of this expression list.
Expressions.prototype.push = function push(node) { Expressions.prototype.push = function push(node) {
@@ -316,11 +314,7 @@
this.value = value; this.value = value;
return this; return this;
}; };
_b = function(){}; __extends(LiteralNode, BaseNode);
_b.prototype = BaseNode.prototype;
LiteralNode.__superClass__ = BaseNode.prototype;
LiteralNode.prototype = new _b();
LiteralNode.prototype.constructor = LiteralNode;
LiteralNode.prototype.type = 'Literal'; LiteralNode.prototype.type = 'Literal';
// Break and continue must be treated as statements -- they lose their meaning // Break and continue must be treated as statements -- they lose their meaning
// when wrapped in a closure. // when wrapped in a closure.
@@ -345,11 +339,7 @@
this.children = [(this.expression = expression)]; this.children = [(this.expression = expression)];
return this; return this;
}; };
_c = function(){}; __extends(ReturnNode, BaseNode);
_c.prototype = BaseNode.prototype;
ReturnNode.__superClass__ = BaseNode.prototype;
ReturnNode.prototype = new _c();
ReturnNode.prototype.constructor = ReturnNode;
ReturnNode.prototype.type = 'Return'; ReturnNode.prototype.type = 'Return';
ReturnNode.prototype.compile_node = function compile_node(o) { ReturnNode.prototype.compile_node = function compile_node(o) {
if (this.expression.is_statement()) { if (this.expression.is_statement()) {
@@ -368,11 +358,7 @@
this.children = flatten([(this.base = base), (this.properties = (properties || []))]); this.children = flatten([(this.base = base), (this.properties = (properties || []))]);
return this; return this;
}; };
_d = function(){}; __extends(ValueNode, BaseNode);
_d.prototype = BaseNode.prototype;
ValueNode.__superClass__ = BaseNode.prototype;
ValueNode.prototype = new _d();
ValueNode.prototype.constructor = ValueNode;
ValueNode.prototype.type = 'Value'; ValueNode.prototype.type = 'Value';
ValueNode.prototype.SOAK = " == undefined ? undefined : "; ValueNode.prototype.SOAK = " == undefined ? undefined : ";
ValueNode.prototype.push = function push(prop) { ValueNode.prototype.push = function push(prop) {
@@ -406,7 +392,7 @@
return this.base.is_statement && this.base.is_statement() && !this.has_properties(); return this.base.is_statement && this.base.is_statement() && !this.has_properties();
}; };
ValueNode.prototype.compile_node = function compile_node(o) { ValueNode.prototype.compile_node = function compile_node(o) {
var _d, _e, _f, baseline, complete, only, op, part, prop, props, soaked, temp; var _a, _b, _c, baseline, complete, only, op, part, prop, props, soaked, temp;
soaked = false; soaked = false;
only = del(o, 'only_first'); only = del(o, 'only_first');
op = del(o, 'operation'); op = del(o, 'operation');
@@ -416,9 +402,9 @@
baseline = '(' + baseline + ')'; baseline = '(' + baseline + ')';
} }
complete = (this.last = baseline); complete = (this.last = baseline);
_d = props; _a = props;
for (_e = 0, _f = _d.length; _e < _f; _e++) { for (_b = 0, _c = _a.length; _b < _c; _b++) {
prop = _d[_e]; prop = _a[_b];
this.source = baseline; this.source = baseline;
if (prop.soak_node) { if (prop.soak_node) {
soaked = true; soaked = true;
@@ -447,11 +433,7 @@
this; this;
return this; return this;
}; };
_e = function(){}; __extends(CommentNode, BaseNode);
_e.prototype = BaseNode.prototype;
CommentNode.__superClass__ = BaseNode.prototype;
CommentNode.prototype = new _e();
CommentNode.prototype.constructor = CommentNode;
CommentNode.prototype.type = 'Comment'; CommentNode.prototype.type = 'Comment';
CommentNode.prototype.compile_node = function compile_node(o) { CommentNode.prototype.compile_node = function compile_node(o) {
return this.idt() + '//' + this.lines.join('\n' + this.idt() + '//'); return this.idt() + '//' + this.lines.join('\n' + this.idt() + '//');
@@ -467,11 +449,7 @@
this.prefix = ''; this.prefix = '';
return this; return this;
}; };
_f = function(){}; __extends(CallNode, BaseNode);
_f.prototype = BaseNode.prototype;
CallNode.__superClass__ = BaseNode.prototype;
CallNode.prototype = new _f();
CallNode.prototype.constructor = CallNode;
CallNode.prototype.type = 'Call'; CallNode.prototype.type = 'Call';
CallNode.prototype.new_instance = function new_instance() { CallNode.prototype.new_instance = function new_instance() {
this.prefix = 'new '; this.prefix = 'new ';
@@ -484,17 +462,17 @@
}; };
// Compile a vanilla function call. // Compile a vanilla function call.
CallNode.prototype.compile_node = function compile_node(o) { CallNode.prototype.compile_node = function compile_node(o) {
var _f, _g, _h, _i, arg, args; var _a, _b, _c, _d, arg, args;
if (this.args[this.args.length - 1] instanceof SplatNode) { if (this.args[this.args.length - 1] instanceof SplatNode) {
return this.compile_splat(o); return this.compile_splat(o);
} }
args = (function() { args = (function() {
_f = []; _g = this.args; _a = []; _b = this.args;
for (_h = 0, _i = _g.length; _h < _i; _h++) { for (_c = 0, _d = _b.length; _c < _d; _c++) {
arg = _g[_h]; arg = _b[_c];
_f.push(arg.compile(o)); _a.push(arg.compile(o));
} }
return _f; return _a;
}).call(this).join(', '); }).call(this).join(', ');
if (this.variable === 'super') { if (this.variable === 'super') {
return this.compile_super(args, o); return this.compile_super(args, o);
@@ -511,7 +489,7 @@
}; };
// Compile a function call being passed variable arguments. // Compile a function call being passed variable arguments.
CallNode.prototype.compile_splat = function compile_splat(o) { CallNode.prototype.compile_splat = function compile_splat(o) {
var _f, _g, _h, arg, args, code, i, meth, obj, temp; var _a, _b, _c, arg, args, code, i, meth, obj, temp;
meth = this.variable.compile(o); meth = this.variable.compile(o);
obj = this.variable.source || 'this'; obj = this.variable.source || 'this';
if (obj.match(/\(/)) { if (obj.match(/\(/)) {
@@ -520,16 +498,16 @@
meth = '(' + temp + ' = ' + this.variable.source + ')' + this.variable.last; meth = '(' + temp + ' = ' + this.variable.source + ')' + this.variable.last;
} }
args = (function() { args = (function() {
_f = []; _g = this.args; _a = []; _b = this.args;
for (i = 0, _h = _g.length; i < _h; i++) { for (i = 0, _c = _b.length; i < _c; i++) {
arg = _g[i]; arg = _b[i];
_f.push((function() { _a.push((function() {
code = arg.compile(o); code = arg.compile(o);
code = arg instanceof SplatNode ? code : '[' + code + ']'; code = arg instanceof SplatNode ? code : '[' + code + ']';
return i === 0 ? code : '.concat(' + code + ')'; return i === 0 ? code : '.concat(' + code + ')';
}).call(this)); }).call(this));
} }
return _f; return _a;
}).call(this); }).call(this);
return this.prefix + meth + '.apply(' + obj + ', ' + args.join('') + ')'; return this.prefix + meth + '.apply(' + obj + ', ' + args.join('') + ')';
}; };
@@ -542,34 +520,19 @@
this.children = [(this.child = child), (this.parent = parent)]; this.children = [(this.child = child), (this.parent = parent)];
return this; return this;
}; };
_g = function(){}; __extends(ExtendsNode, BaseNode);
_g.prototype = BaseNode.prototype;
ExtendsNode.__superClass__ = BaseNode.prototype;
ExtendsNode.prototype = new _g();
ExtendsNode.prototype.constructor = ExtendsNode;
ExtendsNode.prototype.type = 'Extends'; ExtendsNode.prototype.type = 'Extends';
ExtendsNode.prototype.code = "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 }";
// Hooking one constructor into another's prototype chain. // Hooking one constructor into another's prototype chain.
ExtendsNode.prototype.compile_node = function compile_node(o) { ExtendsNode.prototype.compile_node = function compile_node(o) {
var child, child_var, construct, parent, parent_var, prefix; var call, ref;
construct = o.scope.free_variable(); o.scope.assign('__extends', this.code, true);
child = this.child.compile(o); ref = new ValueNode(new LiteralNode('__extends'));
parent = this.parent.compile(o); call = new CallNode(ref, [this.child, this.parent]);
prefix = ''; return call.compile(o);
if (!(this.child instanceof ValueNode) || this.child.has_properties() || !(this.child.unwrap() instanceof LiteralNode)) {
child_var = o.scope.free_variable();
prefix += this.idt() + child_var + ' = ' + child + ';\n';
child = child_var;
}
if (!(this.parent instanceof ValueNode) || this.parent.has_properties() || !(this.parent.unwrap() instanceof LiteralNode)) {
parent_var = o.scope.free_variable();
prefix += this.idt() + parent_var + ' = ' + parent + ';\n';
parent = parent_var;
}
return prefix + this.idt() + construct + ' = function(){};\n' + this.idt() + construct + '.prototype = ' + parent + ".prototype;\n" + this.idt() + child + '.__superClass__ = ' + parent + ".prototype;\n" + this.idt() + child + '.prototype = new ' + construct + "();\n" + this.idt() + child + '.prototype.constructor = ' + child + ';';
}; };
return ExtendsNode; return ExtendsNode;
}).call(this); }).call(this);
statement(ExtendsNode);
// A dotted accessor into a part of a value, or the :: shorthand for // A dotted accessor into a part of a value, or the :: shorthand for
// an accessor into the object's prototype. // an accessor into the object's prototype.
exports.AccessorNode = (function() { exports.AccessorNode = (function() {
@@ -580,11 +543,7 @@
this; this;
return this; return this;
}; };
_h = function(){}; __extends(AccessorNode, BaseNode);
_h.prototype = BaseNode.prototype;
AccessorNode.__superClass__ = BaseNode.prototype;
AccessorNode.prototype = new _h();
AccessorNode.prototype.constructor = AccessorNode;
AccessorNode.prototype.type = 'Accessor'; AccessorNode.prototype.type = 'Accessor';
AccessorNode.prototype.compile_node = function compile_node(o) { AccessorNode.prototype.compile_node = function compile_node(o) {
return '.' + (this.prototype ? 'prototype.' : '') + this.name.compile(o); return '.' + (this.prototype ? 'prototype.' : '') + this.name.compile(o);
@@ -598,11 +557,7 @@
this.soak_node = tag === 'soak'; this.soak_node = tag === 'soak';
return this; return this;
}; };
_i = function(){}; __extends(IndexNode, BaseNode);
_i.prototype = BaseNode.prototype;
IndexNode.__superClass__ = BaseNode.prototype;
IndexNode.prototype = new _i();
IndexNode.prototype.constructor = IndexNode;
IndexNode.prototype.type = 'Index'; IndexNode.prototype.type = 'Index';
IndexNode.prototype.compile_node = function compile_node(o) { IndexNode.prototype.compile_node = function compile_node(o) {
return '[' + this.index.compile(o) + ']'; return '[' + this.index.compile(o) + ']';
@@ -617,11 +572,7 @@
this.exclusive = !!exclusive; this.exclusive = !!exclusive;
return this; return this;
}; };
_j = function(){}; __extends(RangeNode, BaseNode);
_j.prototype = BaseNode.prototype;
RangeNode.__superClass__ = BaseNode.prototype;
RangeNode.prototype = new _j();
RangeNode.prototype.constructor = RangeNode;
RangeNode.prototype.type = 'Range'; RangeNode.prototype.type = 'Range';
RangeNode.prototype.compile_variables = function compile_variables(o) { RangeNode.prototype.compile_variables = function compile_variables(o) {
this.indent = o.indent; this.indent = o.indent;
@@ -668,11 +619,7 @@
this; this;
return this; return this;
}; };
_k = function(){}; __extends(SliceNode, BaseNode);
_k.prototype = BaseNode.prototype;
SliceNode.__superClass__ = BaseNode.prototype;
SliceNode.prototype = new _k();
SliceNode.prototype.constructor = SliceNode;
SliceNode.prototype.type = 'Slice'; SliceNode.prototype.type = 'Slice';
SliceNode.prototype.compile_node = function compile_node(o) { SliceNode.prototype.compile_node = function compile_node(o) {
var from, plus_part, to; var from, plus_part, to;
@@ -689,34 +636,30 @@
this.children = (this.objects = (this.properties = props || [])); this.children = (this.objects = (this.properties = props || []));
return this; return this;
}; };
_l = function(){}; __extends(ObjectNode, BaseNode);
_l.prototype = BaseNode.prototype;
ObjectNode.__superClass__ = BaseNode.prototype;
ObjectNode.prototype = new _l();
ObjectNode.prototype.constructor = ObjectNode;
ObjectNode.prototype.type = 'Object'; ObjectNode.prototype.type = 'Object';
// All the mucking about with commas is to make sure that CommentNodes and // All the mucking about with commas is to make sure that CommentNodes and
// AssignNodes get interleaved correctly, with no trailing commas or // AssignNodes get interleaved correctly, with no trailing commas or
// commas affixed to comments. TODO: Extract this and add it to ArrayNode. // commas affixed to comments. TODO: Extract this and add it to ArrayNode.
ObjectNode.prototype.compile_node = function compile_node(o) { ObjectNode.prototype.compile_node = function compile_node(o) {
var _l, _m, _n, _o, _p, _q, _r, i, indent, inner, join, last_noncom, non_comments, prop, props; var _a, _b, _c, _d, _e, _f, _g, i, indent, inner, join, last_noncom, non_comments, prop, props;
o.indent = this.idt(1); o.indent = this.idt(1);
non_comments = (function() { non_comments = (function() {
_l = []; _m = this.properties; _a = []; _b = this.properties;
for (_n = 0, _o = _m.length; _n < _o; _n++) { for (_c = 0, _d = _b.length; _c < _d; _c++) {
prop = _m[_n]; prop = _b[_c];
if (!(prop instanceof CommentNode)) { if (!(prop instanceof CommentNode)) {
_l.push(prop); _a.push(prop);
} }
} }
return _l; return _a;
}).call(this); }).call(this);
last_noncom = non_comments[non_comments.length - 1]; last_noncom = non_comments[non_comments.length - 1];
props = (function() { props = (function() {
_p = []; _q = this.properties; _e = []; _f = this.properties;
for (i = 0, _r = _q.length; i < _r; i++) { for (i = 0, _g = _f.length; i < _g; i++) {
prop = _q[i]; prop = _f[i];
_p.push((function() { _e.push((function() {
join = ",\n"; join = ",\n";
if ((prop === last_noncom) || (prop instanceof CommentNode)) { if ((prop === last_noncom) || (prop instanceof CommentNode)) {
join = "\n"; join = "\n";
@@ -728,7 +671,7 @@
return indent + prop.compile(o) + join; return indent + prop.compile(o) + join;
}).call(this)); }).call(this));
} }
return _p; return _e;
}).call(this); }).call(this);
props = props.join(''); props = props.join('');
inner = props ? '\n' + props + '\n' + this.idt() : ''; inner = props ? '\n' + props + '\n' + this.idt() : '';
@@ -742,22 +685,18 @@
this.children = compact(flatten([(this.variable = variable), (this.parent = parent), (this.properties = props || [])])); this.children = compact(flatten([(this.variable = variable), (this.parent = parent), (this.properties = props || [])]));
return this; return this;
}; };
_m = function(){}; __extends(ClassNode, BaseNode);
_m.prototype = BaseNode.prototype;
ClassNode.__superClass__ = BaseNode.prototype;
ClassNode.prototype = new _m();
ClassNode.prototype.constructor = ClassNode;
ClassNode.prototype.type = 'Class'; ClassNode.prototype.type = 'Class';
ClassNode.prototype.compile_node = function compile_node(o) { ClassNode.prototype.compile_node = function compile_node(o) {
var _m, _n, _o, construct, extension, func, prop, props, ret, returns, val; var _a, _b, _c, construct, extension, func, prop, props, ret, returns, val;
extension = this.parent && new ExtendsNode(this.variable, this.parent); extension = this.parent && new ExtendsNode(this.variable, this.parent);
constructor = null; constructor = null;
props = new Expressions(); props = new Expressions();
o.top = true; o.top = true;
ret = del(o, 'returns'); ret = del(o, 'returns');
_m = this.properties; _a = this.properties;
for (_n = 0, _o = _m.length; _n < _o; _n++) { for (_b = 0, _c = _a.length; _b < _c; _b++) {
prop = _m[_n]; prop = _a[_b];
if (prop.variable && prop.variable.base.value === 'constructor') { if (prop.variable && prop.variable.base.value === 'constructor') {
func = prop.value; func = prop.value;
func.body.push(new ReturnNode(new LiteralNode('this'))); func.body.push(new ReturnNode(new LiteralNode('this')));
@@ -775,7 +714,7 @@
} }
construct = this.idt() + constructor.compile(o) + ';\n'; construct = this.idt() + constructor.compile(o) + ';\n';
props = props.empty() ? '' : props.compile(o) + '\n'; props = props.empty() ? '' : props.compile(o) + '\n';
extension = extension ? extension.compile(o) + '\n' : ''; extension = extension ? this.idt() + extension.compile(o) + ';\n' : '';
returns = ret ? '\n' + this.idt() + 'return ' + this.variable.compile(o) + ';' : ''; returns = ret ? '\n' + this.idt() + 'return ' + this.variable.compile(o) + ';' : '';
return construct + extension + props + returns; return construct + extension + props + returns;
}; };
@@ -788,20 +727,16 @@
this.children = (this.objects = objects || []); this.children = (this.objects = objects || []);
return this; return this;
}; };
_n = function(){}; __extends(ArrayNode, BaseNode);
_n.prototype = BaseNode.prototype;
ArrayNode.__superClass__ = BaseNode.prototype;
ArrayNode.prototype = new _n();
ArrayNode.prototype.constructor = ArrayNode;
ArrayNode.prototype.type = 'Array'; ArrayNode.prototype.type = 'Array';
ArrayNode.prototype.compile_node = function compile_node(o) { ArrayNode.prototype.compile_node = function compile_node(o) {
var _n, _o, _p, code, ending, i, obj, objects; var _a, _b, _c, code, ending, i, obj, objects;
o.indent = this.idt(1); o.indent = this.idt(1);
objects = (function() { objects = (function() {
_n = []; _o = this.objects; _a = []; _b = this.objects;
for (i = 0, _p = _o.length; i < _p; i++) { for (i = 0, _c = _b.length; i < _c; i++) {
obj = _o[i]; obj = _b[i];
_n.push((function() { _a.push((function() {
code = obj.compile(o); code = obj.compile(o);
if (obj instanceof CommentNode) { if (obj instanceof CommentNode) {
return '\n' + code + '\n' + o.indent; return '\n' + code + '\n' + o.indent;
@@ -812,7 +747,7 @@
} }
}).call(this)); }).call(this));
} }
return _n; return _a;
}).call(this); }).call(this);
objects = objects.join(''); objects = objects.join('');
ending = objects.indexOf('\n') >= 0 ? "\n" + this.idt() + ']' : ']'; ending = objects.indexOf('\n') >= 0 ? "\n" + this.idt() + ']' : ']';
@@ -850,11 +785,7 @@
this.context = context; this.context = context;
return this; return this;
}; };
_o = function(){}; __extends(AssignNode, BaseNode);
_o.prototype = BaseNode.prototype;
AssignNode.__superClass__ = BaseNode.prototype;
AssignNode.prototype = new _o();
AssignNode.prototype.constructor = AssignNode;
AssignNode.prototype.type = 'Assign'; AssignNode.prototype.type = 'Assign';
AssignNode.prototype.PROTO_ASSIGN = /^(\S+)\.prototype/; AssignNode.prototype.PROTO_ASSIGN = /^(\S+)\.prototype/;
AssignNode.prototype.LEADING_DOT = /^\.(prototype\.)?/; AssignNode.prototype.LEADING_DOT = /^\.(prototype\.)?/;
@@ -911,20 +842,20 @@
// object literals to a value. Peeks at their properties to assign inner names. // object literals to a value. Peeks at their properties to assign inner names.
// See: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring // See: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring
AssignNode.prototype.compile_pattern_match = function compile_pattern_match(o) { AssignNode.prototype.compile_pattern_match = function compile_pattern_match(o) {
var _o, _p, _q, access_class, assigns, code, i, idx, obj, val, val_var, value; var _a, _b, _c, access_class, assigns, code, i, idx, obj, val, val_var, value;
val_var = o.scope.free_variable(); val_var = o.scope.free_variable();
value = this.value.is_statement() ? ClosureNode.wrap(this.value) : this.value; value = this.value.is_statement() ? ClosureNode.wrap(this.value) : this.value;
assigns = [this.idt() + val_var + ' = ' + value.compile(o) + ';']; assigns = [this.idt() + val_var + ' = ' + value.compile(o) + ';'];
o.top = true; o.top = true;
o.as_statement = true; o.as_statement = true;
_o = this.variable.base.objects; _a = this.variable.base.objects;
for (i = 0, _p = _o.length; i < _p; i++) { for (i = 0, _b = _a.length; i < _b; i++) {
obj = _o[i]; obj = _a[i];
idx = i; idx = i;
if (this.variable.is_object()) { if (this.variable.is_object()) {
_q = [obj.value, obj.variable.base]; _c = [obj.value, obj.variable.base];
obj = _q[0]; obj = _c[0];
idx = _q[1]; idx = _c[1];
} }
access_class = this.variable.is_array() ? IndexNode : AccessorNode; access_class = this.variable.is_array() ? IndexNode : AccessorNode;
if (obj instanceof SplatNode) { if (obj instanceof SplatNode) {
@@ -966,14 +897,10 @@
this.bound = tag === 'boundfunc'; this.bound = tag === 'boundfunc';
return this; return this;
}; };
_p = function(){}; __extends(CodeNode, BaseNode);
_p.prototype = BaseNode.prototype;
CodeNode.__superClass__ = BaseNode.prototype;
CodeNode.prototype = new _p();
CodeNode.prototype.constructor = CodeNode;
CodeNode.prototype.type = 'Code'; CodeNode.prototype.type = 'Code';
CodeNode.prototype.compile_node = function compile_node(o) { CodeNode.prototype.compile_node = function compile_node(o) {
var _p, _q, _r, _s, _t, _u, _v, code, func, inner, name_part, param, params, shared_scope, splat, top; var _a, _b, _c, _d, _e, _f, _g, code, func, inner, name_part, param, params, shared_scope, splat, top;
shared_scope = del(o, 'shared_scope'); shared_scope = del(o, 'shared_scope');
top = del(o, 'top'); top = del(o, 'top');
o.scope = shared_scope || new Scope(o.scope, this.body, this); o.scope = shared_scope || new Scope(o.scope, this.body, this);
@@ -988,16 +915,16 @@
this.body.unshift(splat); this.body.unshift(splat);
} }
params = (function() { params = (function() {
_p = []; _q = this.params; _a = []; _b = this.params;
for (_r = 0, _s = _q.length; _r < _s; _r++) { for (_c = 0, _d = _b.length; _c < _d; _c++) {
param = _q[_r]; param = _b[_c];
_p.push(param.compile(o)); _a.push(param.compile(o));
} }
return _p; return _a;
}).call(this); }).call(this);
_t = params; _e = params;
for (_u = 0, _v = _t.length; _u < _v; _u++) { for (_f = 0, _g = _e.length; _f < _g; _f++) {
param = _t[_u]; param = _e[_f];
(o.scope.parameter(param)); (o.scope.parameter(param));
} }
code = this.body.expressions.length ? '\n' + this.body.compile_with_declarations(o) + '\n' : ''; code = this.body.expressions.length ? '\n' + this.body.compile_with_declarations(o) + '\n' : '';
@@ -1019,25 +946,25 @@
return flatten([this.params, this.body.expressions]); return flatten([this.params, this.body.expressions]);
}; };
CodeNode.prototype.traverse = function traverse(block) { CodeNode.prototype.traverse = function traverse(block) {
var _p, _q, _r, _s, child; var _a, _b, _c, _d, child;
block(this); block(this);
_p = []; _q = this.real_children(); _a = []; _b = this.real_children();
for (_r = 0, _s = _q.length; _r < _s; _r++) { for (_c = 0, _d = _b.length; _c < _d; _c++) {
child = _q[_r]; child = _b[_c];
_p.push(block(child)); _a.push(block(child));
} }
return _p; return _a;
}; };
CodeNode.prototype.toString = function toString(idt) { CodeNode.prototype.toString = function toString(idt) {
var _p, _q, _r, _s, child; var _a, _b, _c, _d, child;
idt = idt || ''; idt = idt || '';
return '\n' + idt + this.type + (function() { return '\n' + idt + this.type + (function() {
_p = []; _q = this.real_children(); _a = []; _b = this.real_children();
for (_r = 0, _s = _q.length; _r < _s; _r++) { for (_c = 0, _d = _b.length; _c < _d; _c++) {
child = _q[_r]; child = _b[_c];
_p.push(child.toString(idt + TAB)); _a.push(child.toString(idt + TAB));
} }
return _p; return _a;
}).call(this).join(''); }).call(this).join('');
}; };
return CodeNode; return CodeNode;
@@ -1052,15 +979,11 @@
this.children = [(this.name = name)]; this.children = [(this.name = name)];
return this; return this;
}; };
_q = function(){}; __extends(SplatNode, BaseNode);
_q.prototype = BaseNode.prototype;
SplatNode.__superClass__ = BaseNode.prototype;
SplatNode.prototype = new _q();
SplatNode.prototype.constructor = SplatNode;
SplatNode.prototype.type = 'Splat'; SplatNode.prototype.type = 'Splat';
SplatNode.prototype.compile_node = function compile_node(o) { SplatNode.prototype.compile_node = function compile_node(o) {
var _q; var _a;
return (typeof (_q = this.index) !== "undefined" && _q !== null) ? this.compile_param(o) : this.name.compile(o); return (typeof (_a = this.index) !== "undefined" && _a !== null) ? this.compile_param(o) : this.name.compile(o);
}; };
SplatNode.prototype.compile_param = function compile_param(o) { SplatNode.prototype.compile_param = function compile_param(o) {
var name; var name;
@@ -1081,11 +1004,7 @@
this.filter = opts && opts.filter; this.filter = opts && opts.filter;
return this; return this;
}; };
_r = function(){}; __extends(WhileNode, BaseNode);
_r.prototype = BaseNode.prototype;
WhileNode.__superClass__ = BaseNode.prototype;
WhileNode.prototype = new _r();
WhileNode.prototype.constructor = WhileNode;
WhileNode.prototype.type = 'While'; WhileNode.prototype.type = 'While';
WhileNode.prototype.add_body = function add_body(body) { WhileNode.prototype.add_body = function add_body(body) {
this.children.push((this.body = body)); this.children.push((this.body = body));
@@ -1132,11 +1051,7 @@
this.flip = !!flip; this.flip = !!flip;
return this; return this;
}; };
_s = function(){}; __extends(OpNode, BaseNode);
_s.prototype = BaseNode.prototype;
OpNode.__superClass__ = BaseNode.prototype;
OpNode.prototype = new _s();
OpNode.prototype.constructor = OpNode;
OpNode.prototype.type = 'Op'; OpNode.prototype.type = 'Op';
OpNode.prototype.CONVERSIONS = { OpNode.prototype.CONVERSIONS = {
'==': '===', '==': '===',
@@ -1175,20 +1090,20 @@
// Mimic Python's chained comparisons. See: // Mimic Python's chained comparisons. See:
// http://docs.python.org/reference/expressions.html#notin // http://docs.python.org/reference/expressions.html#notin
OpNode.prototype.compile_chain = function compile_chain(o) { OpNode.prototype.compile_chain = function compile_chain(o) {
var _s, shared; var _a, shared;
shared = this.first.unwrap().second; shared = this.first.unwrap().second;
if (shared instanceof CallNode) { if (shared instanceof CallNode) {
_s = shared.compile_reference(o); _a = shared.compile_reference(o);
this.first.second = _s[0]; this.first.second = _a[0];
shared = _s[1]; shared = _a[1];
} }
return '(' + this.first.compile(o) + ') && (' + shared.compile(o) + ' ' + this.operator + ' ' + this.second.compile(o) + ')'; return '(' + this.first.compile(o) + ') && (' + shared.compile(o) + ' ' + this.operator + ' ' + this.second.compile(o) + ')';
}; };
OpNode.prototype.compile_assignment = function compile_assignment(o) { OpNode.prototype.compile_assignment = function compile_assignment(o) {
var _s, first, second; var _a, first, second;
_s = [this.first.compile(o), this.second.compile(o)]; _a = [this.first.compile(o), this.second.compile(o)];
first = _s[0]; first = _a[0];
second = _s[1]; second = _a[1];
if (first.match(IDENTIFIER)) { if (first.match(IDENTIFIER)) {
o.scope.find(first); o.scope.find(first);
} }
@@ -1198,10 +1113,10 @@
return first + ' = ' + first + ' ' + this.operator.substr(0, 2) + ' ' + second; return first + ' = ' + first + ' ' + this.operator.substr(0, 2) + ' ' + second;
}; };
OpNode.prototype.compile_existence = function compile_existence(o) { OpNode.prototype.compile_existence = function compile_existence(o) {
var _s, first, second; var _a, first, second;
_s = [this.first.compile(o), this.second.compile(o)]; _a = [this.first.compile(o), this.second.compile(o)];
first = _s[0]; first = _a[0];
second = _s[1]; second = _a[1];
return ExistenceNode.compile_test(o, this.first) + ' ? ' + first + ' : ' + second; return ExistenceNode.compile_test(o, this.first) + ' ? ' + first + ' : ' + second;
}; };
OpNode.prototype.compile_unary = function compile_unary(o) { OpNode.prototype.compile_unary = function compile_unary(o) {
@@ -1223,11 +1138,7 @@
this; this;
return this; return this;
}; };
_t = function(){}; __extends(TryNode, BaseNode);
_t.prototype = BaseNode.prototype;
TryNode.__superClass__ = BaseNode.prototype;
TryNode.prototype = new _t();
TryNode.prototype.constructor = TryNode;
TryNode.prototype.type = 'Try'; TryNode.prototype.type = 'Try';
TryNode.prototype.compile_node = function compile_node(o) { TryNode.prototype.compile_node = function compile_node(o) {
var catch_part, error_part, finally_part; var catch_part, error_part, finally_part;
@@ -1249,11 +1160,7 @@
this.children = [(this.expression = expression)]; this.children = [(this.expression = expression)];
return this; return this;
}; };
_u = function(){}; __extends(ThrowNode, BaseNode);
_u.prototype = BaseNode.prototype;
ThrowNode.__superClass__ = BaseNode.prototype;
ThrowNode.prototype = new _u();
ThrowNode.prototype.constructor = ThrowNode;
ThrowNode.prototype.type = 'Throw'; ThrowNode.prototype.type = 'Throw';
ThrowNode.prototype.compile_node = function compile_node(o) { ThrowNode.prototype.compile_node = function compile_node(o) {
return this.idt() + 'throw ' + this.expression.compile(o) + ';'; return this.idt() + 'throw ' + this.expression.compile(o) + ';';
@@ -1267,11 +1174,7 @@
this.children = [(this.expression = expression)]; this.children = [(this.expression = expression)];
return this; return this;
}; };
_v = function(){}; __extends(ExistenceNode, BaseNode);
_v.prototype = BaseNode.prototype;
ExistenceNode.__superClass__ = BaseNode.prototype;
ExistenceNode.prototype = new _v();
ExistenceNode.prototype.constructor = ExistenceNode;
ExistenceNode.prototype.type = 'Existence'; ExistenceNode.prototype.type = 'Existence';
ExistenceNode.prototype.compile_node = function compile_node(o) { ExistenceNode.prototype.compile_node = function compile_node(o) {
return ExistenceNode.compile_test(o, this.expression); return ExistenceNode.compile_test(o, this.expression);
@@ -1279,14 +1182,14 @@
return ExistenceNode; return ExistenceNode;
}).call(this); }).call(this);
ExistenceNode.compile_test = function compile_test(o, variable) { ExistenceNode.compile_test = function compile_test(o, variable) {
var _w, _x, first, second; var _a, _b, first, second;
_w = [variable, variable]; _a = [variable, variable];
first = _w[0]; first = _a[0];
second = _w[1]; second = _a[1];
if (variable instanceof CallNode || (variable instanceof ValueNode && variable.has_properties())) { if (variable instanceof CallNode || (variable instanceof ValueNode && variable.has_properties())) {
_x = variable.compile_reference(o); _b = variable.compile_reference(o);
first = _x[0]; first = _b[0];
second = _x[1]; second = _b[1];
} }
return '(typeof ' + first.compile(o) + ' !== "undefined" && ' + second.compile(o) + ' !== null)'; return '(typeof ' + first.compile(o) + ' !== "undefined" && ' + second.compile(o) + ' !== null)';
}; };
@@ -1296,11 +1199,7 @@
this.children = [(this.expression = expression)]; this.children = [(this.expression = expression)];
return this; return this;
}; };
_w = function(){}; __extends(ParentheticalNode, BaseNode);
_w.prototype = BaseNode.prototype;
ParentheticalNode.__superClass__ = BaseNode.prototype;
ParentheticalNode.prototype = new _w();
ParentheticalNode.prototype.constructor = ParentheticalNode;
ParentheticalNode.prototype.type = 'Paren'; ParentheticalNode.prototype.type = 'Paren';
ParentheticalNode.prototype.is_statement = function is_statement() { ParentheticalNode.prototype.is_statement = function is_statement() {
return this.expression.is_statement(); return this.expression.is_statement();
@@ -1325,7 +1224,7 @@
// the current index of the loop as a second parameter. // the current index of the loop as a second parameter.
exports.ForNode = (function() { exports.ForNode = (function() {
ForNode = function ForNode(body, source, name, index) { ForNode = function ForNode(body, source, name, index) {
var _x; var _a;
this.body = body; this.body = body;
this.name = name; this.name = name;
this.index = index || null; this.index = index || null;
@@ -1334,18 +1233,14 @@
this.step = source.step; this.step = source.step;
this.object = !!source.object; this.object = !!source.object;
if (this.object) { if (this.object) {
_x = [this.index, this.name]; _a = [this.index, this.name];
this.name = _x[0]; this.name = _a[0];
this.index = _x[1]; this.index = _a[1];
} }
this.children = compact([this.body, this.source, this.filter]); this.children = compact([this.body, this.source, this.filter]);
return this; return this;
}; };
_x = function(){}; __extends(ForNode, BaseNode);
_x.prototype = BaseNode.prototype;
ForNode.__superClass__ = BaseNode.prototype;
ForNode.prototype = new _x();
ForNode.prototype.constructor = ForNode;
ForNode.prototype.type = 'For'; ForNode.prototype.type = 'For';
ForNode.prototype.top_sensitive = function top_sensitive() { ForNode.prototype.top_sensitive = function top_sensitive() {
return true; return true;
@@ -1445,11 +1340,7 @@
} }
return this; return this;
}; };
_y = function(){}; __extends(IfNode, BaseNode);
_y.prototype = BaseNode.prototype;
IfNode.__superClass__ = BaseNode.prototype;
IfNode.prototype = new _y();
IfNode.prototype.constructor = IfNode;
IfNode.prototype.type = 'If'; IfNode.prototype.type = 'If';
IfNode.prototype.push = function push(else_body) { IfNode.prototype.push = function push(else_body) {
var eb; var eb;
@@ -1468,7 +1359,7 @@
}; };
// Rewrite a chain of IfNodes with their switch condition for equality. // Rewrite a chain of IfNodes with their switch condition for equality.
IfNode.prototype.rewrite_switch = function rewrite_switch(o) { IfNode.prototype.rewrite_switch = function rewrite_switch(o) {
var _aa, _y, _z, assigner, cond, i, variable; var _a, _b, _c, assigner, cond, i, variable;
assigner = this.switcher; assigner = this.switcher;
if (!(this.switcher.unwrap() instanceof LiteralNode)) { if (!(this.switcher.unwrap() instanceof LiteralNode)) {
variable = new LiteralNode(o.scope.free_variable()); variable = new LiteralNode(o.scope.free_variable());
@@ -1477,12 +1368,12 @@
} }
this.condition = (function() { this.condition = (function() {
if (this.multiple) { if (this.multiple) {
_y = []; _z = this.condition; _a = []; _b = this.condition;
for (i = 0, _aa = _z.length; i < _aa; i++) { for (i = 0, _c = _b.length; i < _c; i++) {
cond = _z[i]; cond = _b[i];
_y.push(new OpNode('is', (i === 0 ? assigner : this.switcher), cond)); _a.push(new OpNode('is', (i === 0 ? assigner : this.switcher), cond));
} }
return _y; return _a;
} else { } else {
return new OpNode('is', assigner, this.condition); return new OpNode('is', assigner, this.condition);
} }
@@ -1514,14 +1405,14 @@
return this.statement = this.statement || !!(this.comment || this.tags.statement || this.body.is_statement() || (this.else_body && this.else_body.is_statement())); return this.statement = this.statement || !!(this.comment || this.tags.statement || this.body.is_statement() || (this.else_body && this.else_body.is_statement()));
}; };
IfNode.prototype.compile_condition = function compile_condition(o) { IfNode.prototype.compile_condition = function compile_condition(o) {
var _aa, _ab, _y, _z, cond; var _a, _b, _c, _d, cond;
return (function() { return (function() {
_y = []; _z = flatten([this.condition]); _a = []; _b = flatten([this.condition]);
for (_aa = 0, _ab = _z.length; _aa < _ab; _aa++) { for (_c = 0, _d = _b.length; _c < _d; _c++) {
cond = _z[_aa]; cond = _b[_c];
_y.push(cond.compile(o)); _a.push(cond.compile(o));
} }
return _y; return _a;
}).call(this).join(' || '); }).call(this).join(' || ');
}; };
IfNode.prototype.compile_node = function compile_node(o) { IfNode.prototype.compile_node = function compile_node(o) {

View File

@@ -371,30 +371,25 @@ exports.CallNode: class CallNode extends BaseNode
exports.ExtendsNode: class ExtendsNode extends BaseNode exports.ExtendsNode: class ExtendsNode extends BaseNode
type: 'Extends' type: 'Extends'
code: '''
function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
child.__superClass__ = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
}
'''
constructor: (child, parent) -> constructor: (child, parent) ->
@children: [@child: child, @parent: parent] @children: [@child: child, @parent: parent]
# Hooking one constructor into another's prototype chain. # Hooking one constructor into another's prototype chain.
compile_node: (o) -> compile_node: (o) ->
construct: o.scope.free_variable() o.scope.assign('__extends', @code, true)
child: @child.compile(o) ref: new ValueNode new LiteralNode '__extends'
parent: @parent.compile(o) call: new CallNode ref, [@child, @parent]
prefix: '' call.compile(o)
if not (@child instanceof ValueNode) or @child.has_properties() or not (@child.unwrap() instanceof LiteralNode)
child_var: o.scope.free_variable()
prefix += @idt() + child_var + ' = ' + child + ';\n'
child: child_var
if not (@parent instanceof ValueNode) or @parent.has_properties() or not (@parent.unwrap() instanceof LiteralNode)
parent_var: o.scope.free_variable()
prefix += @idt() + parent_var + ' = ' + parent + ';\n'
parent: parent_var
prefix + @idt() + construct + ' = function(){};\n' + @idt() +
construct + '.prototype = ' + parent + ".prototype;\n" + @idt() +
child + '.__superClass__ = ' + parent + ".prototype;\n" + @idt() +
child + '.prototype = new ' + construct + "();\n" + @idt() +
child + '.prototype.constructor = ' + child + ';'
statement ExtendsNode
# A dotted accessor into a part of a value, or the :: shorthand for # A dotted accessor into a part of a value, or the :: shorthand for
@@ -532,7 +527,7 @@ exports.ClassNode: class ClassNode extends BaseNode
construct: @idt() + constructor.compile(o) + ';\n' construct: @idt() + constructor.compile(o) + ';\n'
props: if props.empty() then '' else props.compile(o) + '\n' props: if props.empty() then '' else props.compile(o) + '\n'
extension: if extension then extension.compile(o) + '\n' else '' extension: if extension then @idt() + extension.compile(o) + ';\n' else ''
returns: if ret then '\n' + @idt() + 'return ' + @variable.compile(o) + ';' else '' returns: if ret then '\n' + @idt() + 'return ' + @variable.compile(o) + ';' else ''
construct + extension + props + returns construct + extension + props + returns