Making the LEVEL constants a bit more readable.

This commit is contained in:
Jeremy Ashkenas
2010-10-23 10:34:22 -04:00
parent b60afdb619
commit 6058910b49
2 changed files with 120 additions and 117 deletions

View File

@@ -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, LVL_ACCESS, 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, multident, starts, utility;
var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, LEVEL, 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, multident, starts, utility;
var __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
@@ -38,7 +38,7 @@
}
node = this.unfoldSoak(o) || this;
node.tab = o.indent;
return o.level === LVL_TOP || node.isPureStatement() || !node.isStatement(o) ? node.compileNode(o) : node.compileClosure(o);
return o.level === LEVEL.TOP || node.isPureStatement() || !node.isStatement(o) ? node.compileNode(o) : node.compileClosure(o);
};
Base.prototype.compileClosure = function(o) {
if (this.containsPureStatement()) {
@@ -60,7 +60,7 @@
};
Base.prototype.compileLoopReference = function(o, name) {
var src, tmp;
src = tmp = this.compile(o, LVL_LIST);
src = tmp = this.compile(o, LEVEL.LIST);
if (!(NUMBER.test(src) || IDENTIFIER.test(src) && o.scope.check(src, {
immediate: true
}))) {
@@ -218,7 +218,7 @@
var code;
o.indent = this.tab = o.bare ? '' : TAB;
o.scope = new Scope(null, this, null);
o.level = LVL_TOP;
o.level = LEVEL.TOP;
code = this.compileWithDeclarations(o);
code = code.replace(TRAILING_WHITESPACE, '');
return o.bare ? code : "(function() {\n" + code + "\n}).call(this);\n";
@@ -242,7 +242,7 @@
}
node = node.unfoldSoak(o) || node;
node.tags.front = true;
o.level = LVL_TOP;
o.level = LEVEL.TOP;
code = node.compile(o);
return node.isStatement(o) ? code : this.tab + code + ';';
};
@@ -303,7 +303,7 @@
return expr && !(expr instanceof Return) ? expr.compile(o, lvl) : Return.__super__.compile.call(this, o, lvl);
};
Return.prototype.compileNode = function(o) {
o.level = LVL_PAREN;
o.level = LEVEL.PAREN;
return this.tab + ("return" + (this.expression ? ' ' + this.expression.compile(o) : '') + ";");
};
return Return;
@@ -390,7 +390,7 @@
var _i, _len, code, prop, props;
this.base.tags.front = this.tags.front;
props = this.properties;
code = this.base.compile(o, props.length ? LVL_ACCESS : null);
code = this.base.compile(o, props.length ? LEVEL.ACCESS : null);
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
code = "(" + code + ")";
}
@@ -548,11 +548,11 @@
_result = [];
for (_j = 0, _len2 = _ref4.length; _j < _len2; _j++) {
arg = _ref4[_j];
_result.push(arg.compile(o, LVL_LIST));
_result.push(arg.compile(o, LEVEL.LIST));
}
return _result;
}).call(this)).join(', ');
return this.isSuper ? this.compileSuper(args, o) : (this.isNew ? 'new ' : '') + this.variable.compile(o, LVL_ACCESS) + ("(" + args + ")");
return this.isSuper ? this.compileSuper(args, o) : (this.isNew ? 'new ' : '') + this.variable.compile(o, LEVEL.ACCESS) + ("(" + args + ")");
};
Call.prototype.compileSuper = function(args, o) {
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
@@ -567,9 +567,9 @@
base = Value.wrap(this.variable);
if ((name = base.properties.pop()) && base.isComplex()) {
ref = o.scope.freeVariable('this');
fun = "(" + ref + " = " + (base.compile(o, LVL_LIST)) + ")" + (name.compile(o));
fun = "(" + ref + " = " + (base.compile(o, LEVEL.LIST)) + ")" + (name.compile(o));
} else {
fun = ref = base.compile(o, LVL_ACCESS);
fun = ref = base.compile(o, LEVEL.ACCESS);
if (name) {
fun += name.compile(o);
}
@@ -577,7 +577,7 @@
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, LVL_LIST)) + ", " + 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, LEVEL.LIST)) + ", " + splatargs + ", function() {})";
};
return Call;
})();
@@ -631,7 +631,7 @@
__extends(Index, Base);
Index.prototype.children = ['index'];
Index.prototype.compile = function(o) {
return (this.proto ? '.prototype' : '') + ("[" + (this.index.compile(o, LVL_PAREN)) + "]");
return (this.proto ? '.prototype' : '') + ("[" + (this.index.compile(o, LEVEL.PAREN)) + "]");
};
Index.prototype.isComplex = function() {
return this.index.isComplex();
@@ -727,7 +727,7 @@
_ref3 = this.objects;
for (i = 0, _len2 = _ref3.length; i < _len2; i++) {
obj = _ref3[i];
code = obj.compile(o, LVL_LIST);
code = obj.compile(o, LEVEL.LIST);
objects.push((obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', '));
}
objects = objects.join('');
@@ -879,12 +879,12 @@
return this.compileConditional(o);
}
}
name = this.variable.compile(o, LVL_LIST);
name = this.variable.compile(o, LEVEL.LIST);
if (this.value instanceof Code && (match = this.METHOD_DEF.exec(name))) {
this.value.name = match[2];
this.value.klass = match[1];
}
val = this.value.compile(o, LVL_LIST);
val = this.value.compile(o, LEVEL.LIST);
if (this.context === 'object') {
return "" + name + ": " + val;
}
@@ -892,11 +892,11 @@
o.scope.find(name);
}
val = name + (" " + (this.context || '=') + " ") + val;
return o.level <= LVL_LIST ? val : "(" + val + ")";
return o.level <= LEVEL.LIST ? val : "(" + val + ")";
};
Assign.prototype.compilePatternMatch = function(o) {
var _len, _ref2, _ref3, _ref4, _ref5, accessClass, assigns, code, i, idx, isObject, obj, objects, olength, ref, splat, top, val, valVar, value;
top = o.level === LVL_TOP;
top = o.level === LEVEL.TOP;
value = this.value;
objects = this.variable.base.objects;
if (!(olength = objects.length)) {
@@ -913,7 +913,7 @@
(value = Value.wrap(value)).properties.push(new accessClass(idx));
return new Assign(obj, value).compile(o);
}
valVar = value.compile(o, LVL_LIST);
valVar = value.compile(o, LEVEL.LIST);
assigns = [];
splat = false;
if (!IDENTIFIER.test(valVar) || this.variable.assigns(valVar)) {
@@ -943,13 +943,13 @@
}
val = new Value(new Literal(valVar), [new accessClass(idx)]);
}
assigns.push(new Assign(obj, val).compile(o, LVL_LIST));
assigns.push(new Assign(obj, val).compile(o, LEVEL.LIST));
}
if (!top) {
assigns.push(valVar);
}
code = assigns.join(', ');
return o.level < LVL_LIST ? code : "(" + code + ")";
return o.level < LEVEL.LIST ? code : "(" + code + ")";
};
Assign.prototype.compileConditional = function(o) {
var _ref2, left, rite;
@@ -1067,7 +1067,7 @@
__extends(Param, Base);
Param.prototype.children = ['name'];
Param.prototype.compile = function(o) {
return this.value.compile(o, LVL_LIST);
return this.value.compile(o, LEVEL.LIST);
};
Param.prototype.toString = function() {
var name;
@@ -1135,7 +1135,7 @@
end = -1;
for (i = 0, _len = list.length; i < _len; i++) {
arg = list[i];
code = arg.compile(o, LVL_LIST);
code = arg.compile(o, LEVEL.LIST);
prev = args[end];
if (!(arg instanceof Splat)) {
if (prev && starts(prev, '[') && ends(prev, ']')) {
@@ -1180,7 +1180,7 @@
o.indent = this.idt(1);
set = '';
body = this.body;
if (o.level > LVL_TOP || this.returns) {
if (o.level > LEVEL.TOP || this.returns) {
rvar = o.scope.freeVariable('result');
set = "" + this.tab + rvar + " = [];\n";
if (body) {
@@ -1190,7 +1190,7 @@
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
code = set + this.tab + ("while (" + (this.condition.compile(o, LVL_PAREN)) + ") {\n" + (body.compile(o, LVL_TOP)) + "\n" + this.tab + "}");
code = set + this.tab + ("while (" + (this.condition.compile(o, LEVEL.PAREN)) + ") {\n" + (body.compile(o, LEVEL.TOP)) + "\n" + this.tab + "}");
if (this.returns) {
o.indent = this.tab;
code += '\n' + new Return(new Literal(rvar)).compile(o);
@@ -1271,12 +1271,12 @@
return this.compileExistence(o);
}
this.first.tags.front = this.tags.front;
return "" + (this.first.compile(o, LVL_OP)) + " " + this.operator + " " + (this.second.compile(o, LVL_OP));
return "" + (this.first.compile(o, LEVEL.OP)) + " " + this.operator + " " + (this.second.compile(o, LEVEL.OP));
};
Op.prototype.compileChain = function(o) {
var _ref2, shared;
_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));
return "" + (this.first.compile(o, LEVEL.OP)) + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LEVEL.OP));
};
Op.prototype.compileExistence = function(o) {
var fst, ref;
@@ -1287,12 +1287,12 @@
fst = this.first;
ref = fst.compile(o);
}
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o, LVL_LIST)));
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o, LEVEL.LIST)));
};
Op.prototype.compileUnary = function(o) {
var _ref2, _ref3, parts, space;
space = (_ref2 = this.operator, __indexOf.call(this.PREFIX_OPERATORS, _ref2) >= 0) || this.first instanceof Op && this.first.operator === this.operator && ((_ref3 = this.operator) === '+' || _ref3 === '-') ? ' ' : '';
parts = [this.operator, space, this.first.compile(o, LVL_OP)];
parts = [this.operator, space, this.first.compile(o, LEVEL.OP)];
return (this.flip ? parts.reverse() : parts).join('');
};
return Op;
@@ -1318,7 +1318,7 @@
};
In.prototype.compileOrTest = function(o) {
var _len, _ref2, _ref3, _ref4, _result, cmp, cnj, i, item, ref, sub, tests;
_ref2 = this.object.cache(o, LVL_OP), sub = _ref2[0], ref = _ref2[1];
_ref2 = this.object.cache(o, LEVEL.OP), sub = _ref2[0], ref = _ref2[1];
_ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1];
tests = (function() {
_ref4 = this.array.base.objects;
@@ -1330,17 +1330,17 @@
return _result;
}).call(this);
tests = tests.join(cnj);
return o.level < LVL_OP ? tests : "(" + tests + ")";
return o.level < LEVEL.OP ? tests : "(" + tests + ")";
};
In.prototype.compileLoopTest = function(o) {
var _ref2, code, ref, sub;
_ref2 = this.object.cache(o, LVL_LIST), sub = _ref2[0], ref = _ref2[1];
_ref2 = this.object.cache(o, LEVEL.LIST), sub = _ref2[0], ref = _ref2[1];
code = utility('indexOf') + (".call(" + (this.array.compile(o)) + ", " + ref + ") ") + (this.negated ? '< 0' : '>= 0');
if (sub === ref) {
return code;
}
code = sub + ', ' + code;
return o.level < LVL_LIST ? code : "(" + code + ")";
return o.level < LEVEL.LIST ? code : "(" + code + ")";
};
In.prototype.toString = function(idt) {
return In.__super__.toString.call(this, idt, this.constructor.name + (this.negated ? '!' : ''));
@@ -1375,8 +1375,8 @@
var catchPart, errorPart;
o.indent = this.idt(1);
errorPart = this.error ? " (" + (this.error.compile(o)) + ") " : ' ';
catchPart = this.recovery ? " catch" + errorPart + "{\n" + (this.recovery.compile(o, LVL_TOP)) + "\n" + this.tab + "}" : !(this.ensure || this.recovery) ? ' catch (_e) {}' : undefined;
return ("" + this.tab + "try {\n" + (this.attempt.compile(o, LVL_TOP)) + "\n" + this.tab + "}" + (catchPart || '')) + (this.ensure ? " finally {\n" + (this.ensure.compile(o, LVL_TOP)) + "\n" + this.tab + "}" : '');
catchPart = this.recovery ? " catch" + errorPart + "{\n" + (this.recovery.compile(o, LEVEL.TOP)) + "\n" + this.tab + "}" : !(this.ensure || this.recovery) ? ' catch (_e) {}' : undefined;
return ("" + this.tab + "try {\n" + (this.attempt.compile(o, LEVEL.TOP)) + "\n" + this.tab + "}" + (catchPart || '')) + (this.ensure ? " finally {\n" + (this.ensure.compile(o, LEVEL.TOP)) + "\n" + this.tab + "}" : '');
};
return Try;
})();
@@ -1413,7 +1413,7 @@
var code;
code = this.expression.compile(o);
code = IDENTIFIER.test(code) && !o.scope.check(code) ? "typeof " + code + " !== \"undefined\" && " + code + " !== null" : "" + code + " != null";
return o.level <= LVL_COND ? code : "(" + code + ")";
return o.level <= LEVEL.COND ? code : "(" + code + ")";
};
return Existence;
})();
@@ -1444,8 +1444,8 @@
expr.tags.front = this.tags.front;
return expr.compile(o);
}
bare = o.level < LVL_OP && (expr instanceof Op || expr instanceof Call);
code = expr.compile(o, LVL_PAREN);
bare = o.level < LEVEL.OP && (expr instanceof Op || expr instanceof Call);
code = expr.compile(o, LEVEL.PAREN);
return bare ? code : "(" + code + ")";
};
return Parens;
@@ -1517,9 +1517,9 @@
if (name || this.object && !this.raw) {
_ref6 = this.source.compileLoopReference(o, 'ref'), sourcePart = _ref6[0], svar = _ref6[1];
} else {
sourcePart = svar = this.source.compile(o, LVL_PAREN);
sourcePart = svar = this.source.compile(o, LEVEL.PAREN);
}
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(o, LVL_TOP) : name ? "" + name + " = " + svar + "[" + ivar + "]" : undefined;
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(o, LEVEL.TOP) : name ? "" + name + " = " + svar + "[" + ivar + "]" : undefined;
if (!this.object) {
if (0 > pvar && (pvar | 0) === +pvar) {
vars = "" + ivar + " = " + svar + ".length - 1";
@@ -1552,7 +1552,7 @@
}
})();
}
if (o.level > LVL_TOP || this.returns) {
if (o.level > LEVEL.TOP || this.returns) {
rvar = scope.freeVariable('result');
defPart += this.tab + rvar + ' = [];\n';
retPart = this.compileReturnValue(rvar, o);
@@ -1565,7 +1565,7 @@
varPart = idt + namePart + ';\n';
}
o.indent = idt;
return defPart + ("" + this.tab + "for (" + forPart + ") {\n" + (guardPart || '') + varPart + (body.compile(o, LVL_TOP)) + "\n" + this.tab + "}") + retPart;
return defPart + ("" + this.tab + "for (" + forPart + ") {\n" + (guardPart || '') + varPart + (body.compile(o, LEVEL.TOP)) + "\n" + this.tab + "}") + retPart;
};
return For;
})();
@@ -1599,7 +1599,7 @@
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, _ref5, block, code, cond, conditions, expr, i, idt1, idt2;
idt1 = this.idt(1);
idt2 = o.indent = this.idt(2);
code = this.tab + ("switch (" + (((_ref2 = this.subject) != null ? _ref2.compile(o, LVL_PAREN) : undefined) || true) + ") {\n");
code = this.tab + ("switch (" + (((_ref2 = this.subject) != null ? _ref2.compile(o, LEVEL.PAREN) : undefined) || true) + ") {\n");
for (i = 0, _len = this.cases.length; i < _len; i++) {
_ref3 = this.cases[i], conditions = _ref3[0], block = _ref3[1];
_ref4 = flatten([conditions]);
@@ -1608,9 +1608,9 @@
if (!this.subject) {
cond = cond.invert().invert();
}
code += idt1 + ("case " + (cond.compile(o, LVL_PAREN)) + ":\n");
code += idt1 + ("case " + (cond.compile(o, LEVEL.PAREN)) + ":\n");
}
code += block.compile(o, LVL_TOP) + '\n';
code += block.compile(o, LEVEL.TOP) + '\n';
if (i === this.cases.length - 1 && !this.otherwise) {
break;
}
@@ -1626,7 +1626,7 @@
}
}
if (this.otherwise) {
code += idt1 + ("default:\n" + (this.otherwise.compile(o, LVL_TOP)) + "\n");
code += idt1 + ("default:\n" + (this.otherwise.compile(o, LEVEL.TOP)) + "\n");
}
return code + this.tab + '}';
};
@@ -1666,7 +1666,7 @@
};
If.prototype.isStatement = function(o) {
var _ref2;
return (o != null ? o.level : undefined) === LVL_TOP || this.bodyNode().isStatement(o) || ((_ref2 = this.elseBodyNode()) != null ? _ref2.isStatement(o) : undefined);
return (o != null ? o.level : undefined) === LEVEL.TOP || 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);
@@ -1686,7 +1686,7 @@
If.prototype.compileStatement = function(o) {
var body, child, cond, ifPart;
child = del(o, 'chainChild');
cond = this.condition.compile(o, LVL_PAREN);
cond = this.condition.compile(o, LEVEL.PAREN);
o.indent = this.idt(1);
body = this.ensureExpressions(this.body).compile(o);
ifPart = "if (" + cond + ") {\n" + body + "\n" + this.tab + "}";
@@ -1699,12 +1699,12 @@
return ifPart + ' else ' + (this.isChain ? this.elseBodyNode().compile(merge(o, {
indent: this.tab,
chainChild: true
})) : "{\n" + (this.elseBody.compile(o, LVL_TOP)) + "\n" + this.tab + "}");
})) : "{\n" + (this.elseBody.compile(o, LEVEL.TOP)) + "\n" + this.tab + "}");
};
If.prototype.compileExpression = function(o) {
var _ref2, code;
code = this.condition.compile(o, LVL_COND) + ' ? ' + this.bodyNode().compile(o, LVL_LIST) + ' : ' + ((_ref2 = this.elseBodyNode()) != null ? _ref2.compile(o, LVL_LIST) : undefined);
return o.level >= LVL_COND ? "(" + code + ")" : code;
code = this.condition.compile(o, LEVEL.COND) + ' ? ' + this.bodyNode().compile(o, LEVEL.LIST) + ' : ' + ((_ref2 = this.elseBodyNode()) != null ? _ref2.compile(o, LEVEL.LIST) : undefined);
return o.level >= LEVEL.COND ? "(" + code + ")" : code;
};
If.prototype.unfoldSoak = function() {
return this.soakNode && this;
@@ -1762,12 +1762,14 @@
hasProp: 'Object.prototype.hasOwnProperty',
slice: 'Array.prototype.slice'
};
LVL_TOP = 0;
LVL_PAREN = 1;
LVL_LIST = 2;
LVL_COND = 3;
LVL_OP = 4;
LVL_ACCESS = 5;
LEVEL = {
TOP: 0,
PAREN: 1,
LIST: 2,
COND: 3,
OP: 4,
ACCESS: 5
};
TAB = ' ';
TRAILING_WHITESPACE = /[ \t]+$/gm;
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;