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