mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 03:21:20 -05:00
nodes: introduced a notion of levels that streamlines parenthesizations
This commit is contained in:
223
lib/nodes.js
223
lib/nodes.js
@@ -1,5 +1,5 @@
|
||||
(function() {
|
||||
var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, Literal, NO, NUMBER, ObjectLiteral, Op, Param, Parens, Push, Return, SIMPLENUM, Scope, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, _ref, compact, del, ends, extend, flatten, last, merge, starts, utility;
|
||||
var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, LVL_ACCESS, LVL_ASSIGN, LVL_COND, LVL_LIST, LVL_OP, LVL_PAREN, LVL_TOP, Literal, NO, NUMBER, ObjectLiteral, Op, Param, Parens, Push, Return, SIMPLENUM, Scope, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, _ref, compact, del, ends, extend, flatten, last, merge, starts, utility;
|
||||
var __extends = function(child, parent) {
|
||||
function ctor() { this.constructor = child; }
|
||||
ctor.prototype = parent.prototype;
|
||||
@@ -30,10 +30,13 @@
|
||||
};
|
||||
return Base;
|
||||
})();
|
||||
Base.prototype.compile = function(o) {
|
||||
Base.prototype.compile = function(o, lvl) {
|
||||
var top;
|
||||
o = o ? merge(o) : {};
|
||||
top = this.topSensitive() ? o.top : del(o, 'top');
|
||||
o = o ? extend({}, o) : {};
|
||||
if (lvl) {
|
||||
o.level = lvl;
|
||||
}
|
||||
top = this.topSensitive ? o.top : del(o, 'top');
|
||||
this.tab = o.indent;
|
||||
return top || o.asStatement || this instanceof Comment || this.isPureStatement() || !this.isStatement(o) ? this.compileNode(o) : this.compileClosure(o);
|
||||
};
|
||||
@@ -42,34 +45,22 @@
|
||||
throw SyntaxError('cannot include a pure statement in an expression.');
|
||||
}
|
||||
o.sharedScope = o.scope;
|
||||
return Closure.wrap(this).compile(o);
|
||||
return Closure.wrap(this).compileNode(o);
|
||||
};
|
||||
Base.prototype.compileReference = function(o, options) {
|
||||
var _len, compiled, i, node, pair, reference;
|
||||
pair = (function() {
|
||||
if (!this.isComplex()) {
|
||||
return [this, this];
|
||||
} else {
|
||||
reference = new Literal(o.scope.freeVariable('ref'));
|
||||
compiled = new Assign(reference, this);
|
||||
return [compiled, reference];
|
||||
}
|
||||
}).call(this);
|
||||
if ((options != null) ? options.precompile : undefined) {
|
||||
for (i = 0, _len = pair.length; i < _len; i++) {
|
||||
node = pair[i];
|
||||
(pair[i] = node.compile(o));
|
||||
}
|
||||
Base.prototype.cache = function(o, lvl) {
|
||||
var ref, sub;
|
||||
if (!this.isComplex()) {
|
||||
ref = lvl ? this.compile(o, lvl) : this;
|
||||
return [ref, ref];
|
||||
} else {
|
||||
ref = new Literal(o.scope.freeVariable('ref'));
|
||||
sub = new Assign(ref, this);
|
||||
return lvl ? [sub.compile(o, lvl), ref.value] : [sub, ref];
|
||||
}
|
||||
return pair;
|
||||
};
|
||||
Base.prototype.compileBare = function(o) {
|
||||
this.parenthetical = true;
|
||||
return this.compile(o);
|
||||
};
|
||||
Base.prototype.compileLoopReference = function(o, name) {
|
||||
var src, tmp;
|
||||
src = tmp = this.compile(o);
|
||||
src = tmp = this.compile(o, LVL_ASSIGN);
|
||||
if (!(NUMBER.test(src) || IDENTIFIER.test(src) && o.scope.check(src, {
|
||||
immediate: true
|
||||
}))) {
|
||||
@@ -164,7 +155,7 @@
|
||||
Base.prototype.isPureStatement = NO;
|
||||
Base.prototype.isComplex = YES;
|
||||
Base.prototype.isChainable = NO;
|
||||
Base.prototype.topSensitive = NO;
|
||||
Base.prototype.topSensitive = false;
|
||||
Base.prototype.unfoldSoak = NO;
|
||||
Base.prototype.assigns = NO;
|
||||
return Base;
|
||||
@@ -207,9 +198,9 @@
|
||||
}
|
||||
return this;
|
||||
};
|
||||
Expressions.prototype.compile = function(o) {
|
||||
Expressions.prototype.compile = function(o, lvl) {
|
||||
o || (o = {});
|
||||
return o.scope ? Expressions.__super__.compile.call(this, o) : this.compileRoot(o);
|
||||
return o.scope ? Expressions.__super__.compile.call(this, o, lvl) : this.compileRoot(o);
|
||||
};
|
||||
Expressions.prototype.compileNode = function(o) {
|
||||
var _i, _len, _ref2, _result, node;
|
||||
@@ -227,6 +218,7 @@
|
||||
var code;
|
||||
o.indent = this.tab = o.bare ? '' : TAB;
|
||||
o.scope = new Scope(null, this, null);
|
||||
o.level = LVL_TOP;
|
||||
code = this.compileWithDeclarations(o);
|
||||
code = code.replace(TRAILING_WHITESPACE, '');
|
||||
return o.bare ? code : "(function() {\n" + code + "\n}).call(this);\n";
|
||||
@@ -303,10 +295,10 @@
|
||||
Return.prototype.isStatement = YES;
|
||||
Return.prototype.isPureStatement = YES;
|
||||
Return.prototype.makeReturn = THIS;
|
||||
Return.prototype.compile = function(o) {
|
||||
Return.prototype.compile = function(o, lvl) {
|
||||
var _ref2, expr;
|
||||
expr = ((_ref2 = this.expression) != null) ? _ref2.makeReturn() : undefined;
|
||||
return expr && !(expr instanceof Return) ? expr.compile(o) : Return.__super__.compile.call(this, o);
|
||||
expr = (_ref2 = this.expression) != null ? _ref2.makeReturn() : undefined;
|
||||
return expr && !(expr instanceof Return) ? expr.compile(o, lvl) : Return.__super__.compile.call(this, o, lvl);
|
||||
};
|
||||
Return.prototype.compileNode = function(o) {
|
||||
var expr;
|
||||
@@ -315,7 +307,7 @@
|
||||
if (this.expression.isStatement(o)) {
|
||||
o.asStatement = true;
|
||||
}
|
||||
expr = ' ' + this.expression.compileBare(o);
|
||||
expr = ' ' + this.expression.compile(o, LVL_PAREN);
|
||||
}
|
||||
return this.tab + 'return' + expr + ';';
|
||||
};
|
||||
@@ -381,7 +373,7 @@
|
||||
Value.prototype.cacheReference = function(o) {
|
||||
var base, bref, name, nref;
|
||||
name = last(this.properties);
|
||||
if (this.properties.length < 2 && !this.base.isComplex() && !((name != null) ? name.isComplex() : undefined)) {
|
||||
if (this.properties.length < 2 && !this.base.isComplex() && !(name != null ? name.isComplex() : undefined)) {
|
||||
return [this, this];
|
||||
}
|
||||
base = new Value(this.base, this.properties.slice(0, -1));
|
||||
@@ -399,9 +391,9 @@
|
||||
}
|
||||
return [base.push(name), new Value(bref || base.base, [nref || name])];
|
||||
};
|
||||
Value.prototype.compile = function(o) {
|
||||
Value.prototype.compile = function(o, lvl) {
|
||||
this.base.tags.front = this.tags.front;
|
||||
return !o.top || this.properties.length ? Value.__super__.compile.call(this, o) : this.base.compile(o);
|
||||
return !o.top || this.properties.length ? Value.__super__.compile.call(this, o, lvl) : this.base.compile(o, lvl);
|
||||
};
|
||||
Value.prototype.compileNode = function(o) {
|
||||
var _i, _len, code, ifn, prop, props;
|
||||
@@ -409,16 +401,13 @@
|
||||
return ifn.compile(o);
|
||||
}
|
||||
props = this.properties;
|
||||
if (this.parenthetical && !props.length) {
|
||||
this.base.parenthetical = true;
|
||||
}
|
||||
code = this.base.compile(o);
|
||||
code = this.base.compile(o, props.length && LVL_ACCESS);
|
||||
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
|
||||
code = "(" + code + ")";
|
||||
}
|
||||
for (_i = 0, _len = props.length; _i < _len; _i++) {
|
||||
prop = props[_i];
|
||||
(code += prop.compile(o));
|
||||
(code += prop.compileNode(o));
|
||||
}
|
||||
return code;
|
||||
};
|
||||
@@ -441,8 +430,7 @@
|
||||
snd.base = ref;
|
||||
}
|
||||
return new If(new Existence(fst), snd, {
|
||||
soak: true,
|
||||
operation: this.tags.operation
|
||||
soak: true
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -493,9 +481,6 @@
|
||||
this.isNew = true;
|
||||
return this;
|
||||
};
|
||||
Call.prototype.prefix = function() {
|
||||
return this.isNew ? 'new ' : '';
|
||||
};
|
||||
Call.prototype.superReference = function(o) {
|
||||
var method, name;
|
||||
method = o.scope.method;
|
||||
@@ -524,8 +509,7 @@
|
||||
rite.isNew = this.isNew;
|
||||
left = new Literal("typeof " + (left.compile(o)) + " === \"function\"");
|
||||
return new If(left, new Value(rite), {
|
||||
soak: true,
|
||||
operation: this.tags.operation
|
||||
soak: true
|
||||
});
|
||||
}
|
||||
call = this;
|
||||
@@ -563,7 +547,7 @@
|
||||
if (ifn = this.unfoldSoak(o)) {
|
||||
return ifn.compile(o);
|
||||
}
|
||||
((_ref2 = this.variable) != null) ? _ref2.tags.front = this.tags.front : undefined;
|
||||
(_ref2 = this.variable) != null ? _ref2.tags.front = this.tags.front : undefined;
|
||||
_ref3 = this.args;
|
||||
for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
|
||||
arg = _ref3[_i];
|
||||
@@ -576,11 +560,11 @@
|
||||
_result = [];
|
||||
for (_j = 0, _len2 = _ref4.length; _j < _len2; _j++) {
|
||||
arg = _ref4[_j];
|
||||
_result.push(arg.compileBare(o));
|
||||
_result.push(arg.compile(o, LVL_LIST));
|
||||
}
|
||||
return _result;
|
||||
}).call(this).join(', ');
|
||||
return this.isSuper ? this.compileSuper(args, o) : "" + (this.prefix()) + (this.variable.compile(o)) + "(" + args + ")";
|
||||
return this.isSuper ? this.compileSuper(args, o) : (this.isNew ? 'new ' : '') + this.variable.compile(o, LVL_ACCESS) + ("(" + args + ")");
|
||||
};
|
||||
Call.prototype.compileSuper = function(args, o) {
|
||||
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
|
||||
@@ -595,17 +579,17 @@
|
||||
base = Value.wrap(this.variable);
|
||||
if ((name = base.properties.pop()) && base.isComplex()) {
|
||||
ref = o.scope.freeVariable('this');
|
||||
fun = "(" + ref + " = " + (base.compile(o)) + ")" + (name.compile(o));
|
||||
fun = "(" + ref + " = " + (base.compile(o, LVL_ASSIGN)) + ")" + (name.compileNode(o));
|
||||
} else {
|
||||
fun = ref = base.compile(o);
|
||||
fun = ref = base.compile(o, LVL_ACCESS);
|
||||
if (name) {
|
||||
fun += name.compile(o);
|
||||
fun += name.compileNode(o);
|
||||
}
|
||||
}
|
||||
return "" + fun + ".apply(" + ref + ", " + splatargs + ")";
|
||||
}
|
||||
idt = this.idt(1);
|
||||
return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + this.tab + "})(" + (this.variable.compile(o)) + ", " + splatargs + ", function() {})";
|
||||
return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + this.tab + "})(" + (this.variable.compile(o, LVL_LIST)) + ", " + splatargs + ", function() {})";
|
||||
};
|
||||
return Call;
|
||||
})();
|
||||
@@ -631,7 +615,7 @@
|
||||
function Accessor(_arg, tag) {
|
||||
this.name = _arg;
|
||||
Accessor.__super__.constructor.call(this);
|
||||
this.prototype = tag === 'prototype' ? '.prototype' : '';
|
||||
this.proto = tag === 'prototype' ? '.prototype' : '';
|
||||
this.soakNode = tag === 'soak';
|
||||
return this;
|
||||
};
|
||||
@@ -642,7 +626,7 @@
|
||||
Accessor.prototype.compileNode = function(o) {
|
||||
var name;
|
||||
name = this.name.compile(o);
|
||||
return this.prototype + (IS_STRING.test(name) ? "[" + name + "]" : "." + name);
|
||||
return this.proto + (IS_STRING.test(name) ? "[" + name + "]" : "." + name);
|
||||
};
|
||||
Accessor.prototype.isComplex = NO;
|
||||
return Accessor;
|
||||
@@ -659,7 +643,7 @@
|
||||
__extends(Index, Base);
|
||||
Index.prototype.children = ['index'];
|
||||
Index.prototype.compileNode = function(o) {
|
||||
return (this.proto ? '.prototype' : '') + ("[" + (this.index.compileBare(o)) + "]");
|
||||
return (this.proto ? '.prototype' : '') + ("[" + (this.index.compile(o, LVL_PAREN)) + "]");
|
||||
};
|
||||
Index.prototype.isComplex = function() {
|
||||
return this.index.isComplex();
|
||||
@@ -756,8 +740,8 @@
|
||||
_ref3 = this.objects;
|
||||
for (i = 0, _len2 = _ref3.length; i < _len2; i++) {
|
||||
obj = _ref3[i];
|
||||
code = obj.compileBare(o);
|
||||
objects.push(obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', ');
|
||||
code = obj.compile(o, LVL_LIST);
|
||||
objects.push((obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', '));
|
||||
}
|
||||
objects = objects.join('');
|
||||
return 0 < objects.indexOf('\n') ? "[\n" + o.indent + objects + "\n" + this.tab + "]" : "[" + objects + "]";
|
||||
@@ -818,7 +802,7 @@
|
||||
pvar = prop.variable, func = prop.value;
|
||||
if (pvar && pvar.base.value === 'constructor') {
|
||||
if (!(func instanceof Code)) {
|
||||
_ref3 = func.compileReference(o), func = _ref3[0], ref = _ref3[1];
|
||||
_ref3 = func.cache(o), func = _ref3[0], ref = _ref3[1];
|
||||
if (func !== ref) {
|
||||
props.push(func);
|
||||
}
|
||||
@@ -888,7 +872,7 @@
|
||||
Assign.prototype.METHOD_DEF = /^(?:(\S+)\.prototype\.)?([$A-Za-z_][$\w]*)$/;
|
||||
Assign.prototype.CONDITIONAL = ['||=', '&&=', '?='];
|
||||
Assign.prototype.children = ['variable', 'value'];
|
||||
Assign.prototype.topSensitive = YES;
|
||||
Assign.prototype.topSensitive = true;
|
||||
Assign.prototype.compileNode = function(o) {
|
||||
var _ref2, ifn, isValue, match, name, stmt, top, val;
|
||||
if (isValue = this.variable instanceof Value) {
|
||||
@@ -899,18 +883,18 @@
|
||||
delete o.top;
|
||||
return ifn.compile(o);
|
||||
}
|
||||
if ((_ref2 = this.context, __indexOf.call(this.CONDITIONAL, _ref2) >= 0)) {
|
||||
if (_ref2 = this.context, __indexOf.call(this.CONDITIONAL, _ref2) >= 0) {
|
||||
return this.compileConditional(o);
|
||||
}
|
||||
}
|
||||
top = del(o, 'top');
|
||||
stmt = del(o, 'asStatement');
|
||||
name = this.variable.compile(o);
|
||||
name = this.variable.compile(o, LVL_ASSIGN);
|
||||
if (this.value instanceof Code && (match = this.METHOD_DEF.exec(name))) {
|
||||
this.value.name = match[2];
|
||||
this.value.klass = match[1];
|
||||
}
|
||||
val = this.value.compileBare(o);
|
||||
val = this.value.compile(o, LVL_ASSIGN);
|
||||
if (this.context === 'object') {
|
||||
return "" + name + ": " + val;
|
||||
}
|
||||
@@ -921,7 +905,7 @@
|
||||
if (stmt) {
|
||||
return "" + this.tab + val + ";";
|
||||
}
|
||||
return top || this.parenthetical ? val : "(" + val + ")";
|
||||
return top || o.level <= LVL_ASSIGN ? val : "(" + val + ")";
|
||||
};
|
||||
Assign.prototype.compilePatternMatch = function(o) {
|
||||
var _len, _ref2, _ref3, accessClass, assigns, code, i, idx, isObject, obj, objects, olength, otop, ref, splat, top, val, valVar, value;
|
||||
@@ -929,9 +913,7 @@
|
||||
otop = merge(o, {
|
||||
top: true
|
||||
});
|
||||
if ((value = this.value).isStatement(o)) {
|
||||
value = Closure.wrap(value);
|
||||
}
|
||||
value = this.value;
|
||||
objects = this.variable.base.objects;
|
||||
if (!(olength = objects.length)) {
|
||||
return value.compile(o);
|
||||
@@ -983,7 +965,7 @@
|
||||
assigns.push(valVar);
|
||||
}
|
||||
code = assigns.join(', ');
|
||||
return top || this.parenthetical ? code : "(" + code + ")";
|
||||
return top || o.level <= LVL_PAREN ? code : "(" + code + ")";
|
||||
};
|
||||
Assign.prototype.compileConditional = function(o) {
|
||||
var _ref2, left, rite;
|
||||
@@ -1101,7 +1083,7 @@
|
||||
__extends(Param, Base);
|
||||
Param.prototype.children = ['name'];
|
||||
Param.prototype.compileNode = function(o) {
|
||||
return this.value.compile(o);
|
||||
return this.value.compile(o, LVL_LIST);
|
||||
};
|
||||
Param.prototype.toString = function() {
|
||||
var name;
|
||||
@@ -1131,7 +1113,7 @@
|
||||
return this.name.assigns(name);
|
||||
};
|
||||
Splat.prototype.compileNode = function(o) {
|
||||
return (this.index != null) ? this.compileParam(o) : this.name.compile(o);
|
||||
return this.index != null ? this.compileParam(o) : this.name.compile(o);
|
||||
};
|
||||
Splat.prototype.compileParam = function(o) {
|
||||
var _len, _ref2, assign, end, idx, len, name, pos, trailing, variadic;
|
||||
@@ -1140,7 +1122,7 @@
|
||||
end = '';
|
||||
if (this.trailings.length) {
|
||||
len = o.scope.freeVariable('len');
|
||||
o.scope.assign(len, "arguments.length");
|
||||
o.scope.assign(len, 'arguments.length');
|
||||
variadic = o.scope.freeVariable('result');
|
||||
o.scope.assign(variadic, len + ' >= ' + this.arglength);
|
||||
end = this.trailings.length ? ", " + len + " - " + this.trailings.length : undefined;
|
||||
@@ -1169,7 +1151,7 @@
|
||||
end = -1;
|
||||
for (i = 0, _len = list.length; i < _len; i++) {
|
||||
arg = list[i];
|
||||
code = arg.compile(o);
|
||||
code = arg.compile(o, LVL_LIST);
|
||||
prev = args[end];
|
||||
if (!(arg instanceof Splat)) {
|
||||
if (prev && starts(prev, '[') && ends(prev, ']')) {
|
||||
@@ -1192,15 +1174,15 @@
|
||||
While = (function() {
|
||||
function While(condition, opts) {
|
||||
While.__super__.constructor.call(this);
|
||||
this.condition = ((opts != null) ? opts.invert : undefined) ? condition.invert() : condition;
|
||||
this.guard = (opts != null) ? opts.guard : undefined;
|
||||
this.condition = (opts != null ? opts.invert : undefined) ? condition.invert() : condition;
|
||||
this.guard = opts != null ? opts.guard : undefined;
|
||||
return this;
|
||||
};
|
||||
return While;
|
||||
})();
|
||||
__extends(While, Base);
|
||||
While.prototype.children = ['condition', 'guard', 'body'];
|
||||
While.prototype.topSensitive = YES;
|
||||
While.prototype.topSensitive = true;
|
||||
While.prototype.isStatement = YES;
|
||||
While.prototype.addBody = function(body) {
|
||||
this.body = body;
|
||||
@@ -1214,7 +1196,7 @@
|
||||
var cond, post, pre, rvar, set, top;
|
||||
top = del(o, 'top') && !this.returns;
|
||||
o.indent = this.idt(1);
|
||||
cond = this.condition.compileBare(o);
|
||||
cond = this.condition.compile(o, LVL_PAREN);
|
||||
o.top = true;
|
||||
set = '';
|
||||
if (!top) {
|
||||
@@ -1255,10 +1237,8 @@
|
||||
}
|
||||
Op.__super__.constructor.call(this);
|
||||
this.operator = this.CONVERSIONS[op] || op;
|
||||
(this.first = first).tags.operation = true;
|
||||
if (second) {
|
||||
(this.second = second).tags.operation = true;
|
||||
}
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
this.flip = !!flip;
|
||||
return this;
|
||||
};
|
||||
@@ -1286,7 +1266,7 @@
|
||||
};
|
||||
Op.prototype.isChainable = function() {
|
||||
var _ref2;
|
||||
return (_ref2 = this.operator, __indexOf.call(this.CHAINABLE, _ref2) >= 0);
|
||||
return _ref2 = this.operator, __indexOf.call(this.CHAINABLE, _ref2) >= 0;
|
||||
};
|
||||
Op.prototype.invert = function() {
|
||||
var op;
|
||||
@@ -1313,12 +1293,12 @@
|
||||
return this.compileExistence(o);
|
||||
}
|
||||
this.first.tags.front = this.tags.front;
|
||||
return "" + (this.first.compile(o)) + " " + this.operator + " " + (this.second.compile(o));
|
||||
return "" + (this.first.compile(o, LVL_OP)) + " " + this.operator + " " + (this.second.compile(o, LVL_OP));
|
||||
};
|
||||
Op.prototype.compileChain = function(o) {
|
||||
var _ref2, shared;
|
||||
_ref2 = this.first.unwrap().second.compileReference(o), this.first.second = _ref2[0], shared = _ref2[1];
|
||||
return "" + (this.first.compile(o)) + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o));
|
||||
_ref2 = this.first.unwrap().second.cache(o), this.first.second = _ref2[0], shared = _ref2[1];
|
||||
return "" + (this.first.compile(o, LVL_OP)) + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LVL_OP));
|
||||
};
|
||||
Op.prototype.compileExistence = function(o) {
|
||||
var fst, ref;
|
||||
@@ -1329,13 +1309,12 @@
|
||||
fst = this.first;
|
||||
ref = fst.compile(o);
|
||||
}
|
||||
this.second.tags.operation = false;
|
||||
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compileBare(o)));
|
||||
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o, LVL_ASSIGN)));
|
||||
};
|
||||
Op.prototype.compileUnary = function(o) {
|
||||
var _ref2, parts, space;
|
||||
space = (_ref2 = this.operator, __indexOf.call(this.PREFIX_OPERATORS, _ref2) >= 0) ? ' ' : '';
|
||||
parts = [this.operator, space, this.first.compile(o)];
|
||||
parts = [this.operator, space, this.first.compile(o, LVL_OP)];
|
||||
return (this.flip ? parts.reverse() : parts).join('');
|
||||
};
|
||||
return Op;
|
||||
@@ -1361,9 +1340,7 @@
|
||||
};
|
||||
In.prototype.compileOrTest = function(o) {
|
||||
var _len, _ref2, _ref3, _ref4, _result, cmp, cnj, i, item, ref, sub, tests;
|
||||
_ref2 = this.object.compileReference(o, {
|
||||
precompile: true
|
||||
}), sub = _ref2[0], ref = _ref2[1];
|
||||
_ref2 = this.object.cache(o, LVL_OP), sub = _ref2[0], ref = _ref2[1];
|
||||
_ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1];
|
||||
tests = (function() {
|
||||
_ref4 = this.array.base.objects;
|
||||
@@ -1375,17 +1352,17 @@
|
||||
return _result;
|
||||
}).call(this);
|
||||
tests = tests.join(cnj);
|
||||
return this.parenthetical ? tests : "(" + tests + ")";
|
||||
return o.level < LVL_OP ? tests : "(" + tests + ")";
|
||||
};
|
||||
In.prototype.compileLoopTest = function(o) {
|
||||
var _ref2, code, ref, sub;
|
||||
_ref2 = this.object.compileReference(merge(o, {
|
||||
top: true
|
||||
}), {
|
||||
precompile: true
|
||||
}), sub = _ref2[0], ref = _ref2[1];
|
||||
_ref2 = this.object.cache(o, LVL_LIST), sub = _ref2[0], ref = _ref2[1];
|
||||
code = utility('indexOf') + (".call(" + (this.array.compile(o)) + ", " + ref + ") ") + (this.negated ? '< 0' : '>= 0');
|
||||
return sub === ref ? code : "(" + sub + ", " + code + ")";
|
||||
if (sub === ref) {
|
||||
return code;
|
||||
}
|
||||
code = sub + ', ' + code;
|
||||
return o.level < LVL_LIST ? code : "(" + code + ")";
|
||||
};
|
||||
In.prototype.toString = function(idt) {
|
||||
return In.__super__.toString.call(this, idt, this.constructor.name + (this.negated ? '!' : ''));
|
||||
@@ -1461,7 +1438,7 @@
|
||||
var code;
|
||||
code = this.expression.compile(o);
|
||||
code = IDENTIFIER.test(code) && !o.scope.check(code) ? "typeof " + code + " !== \"undefined\" && " + code + " !== null" : "" + code + " != null";
|
||||
return this.parenthetical ? code : "(" + code + ")";
|
||||
return o.level <= LVL_COND ? code : "(" + code + ")";
|
||||
};
|
||||
return Existence;
|
||||
})();
|
||||
@@ -1476,7 +1453,7 @@
|
||||
})();
|
||||
__extends(Parens, Base);
|
||||
Parens.prototype.children = ['expression'];
|
||||
Parens.prototype.topSensitive = YES;
|
||||
Parens.prototype.topSensitive = true;
|
||||
Parens.prototype.isStatement = function(o) {
|
||||
return this.expression.isStatement(o);
|
||||
};
|
||||
@@ -1494,9 +1471,12 @@
|
||||
expr.tags.front = this.tags.front;
|
||||
return expr.compile(o);
|
||||
}
|
||||
code = expr.compileBare(o);
|
||||
if (this.parenthetical || expr.isStatement(o)) {
|
||||
return top ? this.tab + code + ';' : code;
|
||||
code = expr.compile(o, LVL_PAREN);
|
||||
if (o.level < LVL_OP && (expr instanceof Op || expr instanceof Call)) {
|
||||
return code;
|
||||
}
|
||||
if (expr.isStatement(o)) {
|
||||
return (top ? this.tab + code + ';' : code);
|
||||
}
|
||||
return "(" + code + ")";
|
||||
};
|
||||
@@ -1525,7 +1505,7 @@
|
||||
})();
|
||||
__extends(For, Base);
|
||||
For.prototype.children = ['body', 'source', 'guard', 'step', 'from', 'to'];
|
||||
For.prototype.topSensitive = YES;
|
||||
For.prototype.topSensitive = true;
|
||||
For.prototype.isStatement = YES;
|
||||
For.prototype.makeReturn = function() {
|
||||
this.returns = true;
|
||||
@@ -1544,8 +1524,8 @@
|
||||
var _ref2, _ref3, _ref4, _ref5, _ref6, body, cond, defPart, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, resultRet, rvar, scope, sourcePart, step, svar, tail, top, tvar, varPart, vars;
|
||||
scope = o.scope;
|
||||
top = del(o, 'top') && !this.returns;
|
||||
name = !this.pattern && (((_ref2 = this.name) != null) ? _ref2.compile(o) : undefined);
|
||||
index = ((_ref3 = this.index) != null) ? _ref3.compile(o) : undefined;
|
||||
name = !this.pattern && ((_ref2 = this.name) != null ? _ref2.compile(o) : undefined);
|
||||
index = (_ref3 = this.index) != null ? _ref3.compile(o) : undefined;
|
||||
ivar = !index ? scope.freeVariable('i') : index;
|
||||
varPart = '';
|
||||
body = Expressions.wrap([this.body]);
|
||||
@@ -1662,7 +1642,7 @@
|
||||
idt1 = this.idt(1);
|
||||
idt2 = o.indent = this.idt(2);
|
||||
o.top = true;
|
||||
code = "" + this.tab + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {";
|
||||
code = "" + this.tab + "switch (" + (((_ref2 = this.subject) != null ? _ref2.compile(o) : undefined) || true) + ") {";
|
||||
for (_i = 0, _len = this.cases.length; _i < _len; _i++) {
|
||||
_ref3 = this.cases[_i], conditions = _ref3[0], block = _ref3[1];
|
||||
_ref4 = flatten([conditions]);
|
||||
@@ -1701,14 +1681,14 @@
|
||||
})();
|
||||
__extends(If, Base);
|
||||
If.prototype.children = ['condition', 'body', 'elseBody'];
|
||||
If.prototype.topSensitive = YES;
|
||||
If.prototype.topSensitive = true;
|
||||
If.prototype.bodyNode = function() {
|
||||
var _ref2;
|
||||
return ((_ref2 = this.body) != null) ? _ref2.unwrap() : undefined;
|
||||
return (_ref2 = this.body) != null ? _ref2.unwrap() : undefined;
|
||||
};
|
||||
If.prototype.elseBodyNode = function() {
|
||||
var _ref2;
|
||||
return ((_ref2 = this.elseBody) != null) ? _ref2.unwrap() : undefined;
|
||||
return (_ref2 = this.elseBody) != null ? _ref2.unwrap() : undefined;
|
||||
};
|
||||
If.prototype.addElse = function(elseBody) {
|
||||
if (this.isChain) {
|
||||
@@ -1721,7 +1701,7 @@
|
||||
};
|
||||
If.prototype.isStatement = function(o) {
|
||||
var _ref2;
|
||||
return this.statement || (this.statement = ((o != null) ? o.top : undefined) || this.bodyNode().isStatement(o) || (((_ref2 = this.elseBodyNode()) != null) ? _ref2.isStatement(o) : undefined));
|
||||
return this.statement || (this.statement = (o != null ? o.top : undefined) || this.bodyNode().isStatement(o) || ((_ref2 = this.elseBodyNode()) != null ? _ref2.isStatement(o) : undefined));
|
||||
};
|
||||
If.prototype.compileNode = function(o) {
|
||||
return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o);
|
||||
@@ -1745,7 +1725,7 @@
|
||||
condO = merge(o);
|
||||
o.indent = this.idt(1);
|
||||
o.top = true;
|
||||
ifPart = "if (" + (this.condition.compileBare(condO)) + ") {\n" + (this.body.compile(o)) + "\n" + this.tab + "}";
|
||||
ifPart = "if (" + (this.condition.compile(condO, LVL_PAREN)) + ") {\n" + (this.body.compile(o)) + "\n" + this.tab + "}";
|
||||
if (!child) {
|
||||
ifPart = this.tab + ifPart;
|
||||
}
|
||||
@@ -1759,9 +1739,8 @@
|
||||
};
|
||||
If.prototype.compileExpression = function(o) {
|
||||
var _ref2, code;
|
||||
this.condition.tags.operation = true;
|
||||
code = this.condition.compile(o) + ' ? ' + this.bodyNode().compileBare(o) + ' : ' + (((_ref2 = this.elseBodyNode()) != null) ? _ref2.compileBare(o) : undefined);
|
||||
return this.tags.operation ? "(" + code + ")" : code;
|
||||
code = this.condition.compile(o, LVL_COND) + ' ? ' + this.bodyNode().compile(o, LVL_ASSIGN) + ' : ' + ((_ref2 = this.elseBodyNode()) != null ? _ref2.compile(o, LVL_ASSIGN) : undefined);
|
||||
return o.level >= LVL_COND ? "(" + code + ")" : code;
|
||||
};
|
||||
If.prototype.unfoldSoak = function() {
|
||||
return this.soakNode && this;
|
||||
@@ -1773,9 +1752,6 @@
|
||||
}
|
||||
parent[name] = ifn.body;
|
||||
ifn.body = new Value(parent);
|
||||
if (parent.tags.operation) {
|
||||
ifn.tags.operation = true;
|
||||
}
|
||||
return ifn;
|
||||
};
|
||||
return If;
|
||||
@@ -1822,6 +1798,13 @@
|
||||
hasProp: 'Object.prototype.hasOwnProperty',
|
||||
slice: 'Array.prototype.slice'
|
||||
};
|
||||
LVL_TOP = 0;
|
||||
LVL_PAREN = 1;
|
||||
LVL_LIST = 2;
|
||||
LVL_ASSIGN = 3;
|
||||
LVL_COND = 4;
|
||||
LVL_OP = 5;
|
||||
LVL_ACCESS = 6;
|
||||
TAB = ' ';
|
||||
TRAILING_WHITESPACE = /[ \t]+$/gm;
|
||||
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
|
||||
|
||||
Reference in New Issue
Block a user