removed extra parens from compilations with assignments or conditional operators

This commit is contained in:
satyr 2010-10-21 02:29:06 +09:00
parent e2a6f292a2
commit c1dc74fc8b
10 changed files with 141 additions and 150 deletions

View File

@ -6,7 +6,7 @@
return eval(CoffeeScript.compile(code, options));
};
CoffeeScript.run = function(code, options) {
((options != null) ? (options.bare = true) : undefined);
((options != null) ? options.bare = true : undefined);
return Function(CoffeeScript.compile(code, options))();
};
if (!(typeof window !== "undefined" && window !== null)) {
@ -30,9 +30,9 @@
(function() {
var script = _ref[_i];
_j = script;
return script.type === 'text/coffeescript' ? (script.src ? CoffeeScript.load(script.src) : setTimeout(function() {
return script.type === 'text/coffeescript' ? script.src ? CoffeeScript.load(script.src) : setTimeout(function() {
return CoffeeScript.run(script.innerHTML);
})) : undefined;
}) : undefined;
})();
script = _j;
}

View File

@ -61,7 +61,7 @@
task = _ref[name];
spaces = 20 - name.length;
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
desc = task.description ? ("# " + (task.description)) : '';
desc = task.description ? "# " + (task.description) : '';
puts("cake " + name + spaces + " " + desc);
}
return switches.length ? puts(oparse.help()) : undefined;

View File

@ -17,17 +17,17 @@
}
exports.VERSION = '0.9.4';
exports.helpers = require('./helpers');
exports.compile = (compile = function(code, options) {
exports.compile = compile = function(code, options) {
options || (options = {});
try {
return (parser.parse(lexer.tokenize(code))).compile(options);
} catch (err) {
if (options.fileName) {
err.message = ("In " + (options.fileName) + ", " + (err.message));
err.message = "In " + (options.fileName) + ", " + (err.message);
}
throw err;
}
});
};
exports.tokens = function(code, options) {
return lexer.tokenize(code, options);
};

View File

@ -44,7 +44,7 @@
if (opts.run) {
flags = sources.splice(1).concat(flags);
}
process.ARGV = (process.argv = flags);
process.ARGV = process.argv = flags;
return compileScripts();
};
compileScripts = function() {
@ -99,11 +99,11 @@
}
}
try {
t = (task = {
t = task = {
file: file,
input: input,
options: options
});
};
CoffeeScript.emit('compile', task);
if (o.tokens) {
return printTokens(CoffeeScript.tokens(t.input));
@ -114,7 +114,7 @@
} else {
t.output = CoffeeScript.compile(t.input, t.options);
CoffeeScript.emit('success', task);
return o.print ? print(t.output) : (o.compile ? writeJs(t.file, t.output, base) : (o.lint ? lint(t.output) : undefined));
return o.print ? print(t.output) : o.compile ? writeJs(t.file, t.output, base) : o.lint ? lint(t.output) : undefined;
}
} catch (err) {
CoffeeScript.emit('failure', err, task);
@ -133,7 +133,7 @@
code = '';
stdin = process.openStdin();
stdin.on('data', function(buffer) {
return buffer ? (code += buffer.toString()) : undefined;
return buffer ? code += buffer.toString() : undefined;
});
return stdin.on('end', function() {
return compileScript('stdio', code);
@ -167,7 +167,7 @@
js = ' ';
}
return fs.writeFile(jsPath, js, function(err) {
return err ? puts(err.message) : (opts.compile && opts.watch ? puts("Compiled " + source) : undefined);
return err ? puts(err.message) : opts.compile && opts.watch ? puts("Compiled " + source) : undefined;
});
};
return path.exists(dir, function(exists) {
@ -204,7 +204,7 @@
parseOptions = function() {
var o;
optionParser = new optparse.OptionParser(SWITCHES, BANNER);
o = (opts = optionParser.parse(process.argv.slice(2)));
o = opts = optionParser.parse(process.argv.slice(2));
o.compile || (o.compile = !!o.output);
o.run = !(o.compile || o.print || o.lint);
o.print = !!(o.print || (o.eval || o.stdio && o.compile));

View File

@ -9,7 +9,7 @@
if (!action) {
return [patternString, '$$ = $1;', options];
}
action = (match = unwrap.exec(action)) ? match[1] : ("(" + action + "())");
action = (match = unwrap.exec(action)) ? match[1] : "(" + action + "())";
action = action.replace(/\bnew /g, '$&yy.');
action = action.replace(/\bExpressions\.wrap\b/g, 'yy.$&');
return [patternString, ("$$ = " + action + ";"), options];
@ -586,7 +586,7 @@
}), o("SimpleAssignable COMPOUND_ASSIGN INDENT Expression OUTDENT", function() {
return new Assign($1, $4, $2);
}), o("Expression RELATION Expression", function() {
return $2.charAt(0) === '!' ? ($2 === '!in' ? new Op('!', new In($1, $3)) : new Op('!', new Parens(new Op($2.slice(1), $1, $3)))) : ($2 === 'in' ? new In($1, $3) : new Op($2, $1, $3));
return $2.charAt(0) === '!' ? $2 === '!in' ? new Op('!', new In($1, $3)) : new Op('!', new Parens(new Op($2.slice(1), $1, $3))) : $2 === 'in' ? new In($1, $3) : new Op($2, $1, $3);
})
]
};
@ -607,7 +607,7 @@
}
}
if (name === 'Root') {
alt[1] = ("return " + (alt[1]));
alt[1] = "return " + (alt[1]);
}
return alt;
})());

View File

@ -21,7 +21,7 @@
};
exports.count = function(string, letter) {
var num, pos;
num = (pos = 0);
num = pos = 0;
while (pos = 1 + string.indexOf(letter, pos)) {
num++;
}
@ -30,15 +30,15 @@
exports.merge = function(options, overrides) {
return extend(extend({}, options), overrides);
};
extend = (exports.extend = function(object, properties) {
extend = exports.extend = function(object, properties) {
var key, val;
for (key in properties) {
val = properties[key];
object[key] = val;
}
return object;
});
exports.flatten = (flatten = function(array) {
};
exports.flatten = flatten = function(array) {
var _i, _len, element, flattened;
flattened = [];
for (_i = 0, _len = array.length; _i < _len; _i++) {
@ -50,7 +50,7 @@
}
}
return flattened;
});
};
exports.del = function(obj, key) {
var val;
val = obj[key];

View File

@ -80,7 +80,7 @@
}
if (!forcedIdentifier) {
if (COFFEE_ALIASES.hasOwnProperty(id)) {
tag = (id = COFFEE_ALIASES[id]);
tag = id = COFFEE_ALIASES[id];
}
if (id === '!') {
tag = 'UNARY';
@ -266,7 +266,7 @@
diff = size - this.indent + this.outdebt;
this.token('INDENT', diff);
this.indents.push(diff);
this.outdebt = (this.indebt = 0);
this.outdebt = this.indebt = 0;
} else {
this.indebt = 0;
this.outdentToken(this.indent - size, noNewlines);
@ -564,11 +564,11 @@
};
Lexer.prototype.tag = function(index, tag) {
var tok;
return (tok = last(this.tokens, index)) && ((tag != null) ? (tok[0] = tag) : tok[0]);
return (tok = last(this.tokens, index)) && ((tag != null) ? tok[0] = tag : tok[0]);
};
Lexer.prototype.value = function(index, val) {
var tok;
return (tok = last(this.tokens, index)) && ((val != null) ? (tok[1] = val) : tok[1]);
return (tok = last(this.tokens, index)) && ((val != null) ? tok[1] = val : tok[1]);
};
Lexer.prototype.unfinished = function() {
var prev, value;

View File

@ -64,6 +64,10 @@
}
return pair;
};
Base.prototype.compileBare = function(o) {
this.parenthetical = true;
return this.compile(o);
};
Base.prototype.idt = function(tabs) {
return (this.tab || '') + Array((tabs || 0) + 1).join(TAB);
};
@ -210,20 +214,20 @@
};
Expressions.prototype.compileRoot = function(o) {
var code;
o.indent = (this.tab = o.bare ? '' : TAB);
o.indent = this.tab = o.bare ? '' : TAB;
o.scope = new Scope(null, this, null);
code = this.compileWithDeclarations(o);
code = code.replace(TRAILING_WHITESPACE, '');
return o.bare ? code : ("(function() {\n" + code + "\n}).call(this);\n");
return o.bare ? code : "(function() {\n" + code + "\n}).call(this);\n";
};
Expressions.prototype.compileWithDeclarations = function(o) {
var code;
code = this.compileNode(o);
if (o.scope.hasAssignments(this)) {
code = ("" + (this.tab) + "var " + (o.scope.compiledAssignments().replace(/\n/g, '$&' + this.tab)) + ";\n" + code);
code = "" + (this.tab) + "var " + (o.scope.compiledAssignments().replace(/\n/g, '$&' + this.tab)) + ";\n" + code;
}
if (!o.globals && o.scope.hasDeclarations(this)) {
code = ("" + (this.tab) + "var " + (o.scope.compiledDeclarations()) + ";\n" + code);
code = "" + (this.tab) + "var " + (o.scope.compiledDeclarations()) + ";\n" + code;
}
return code;
};
@ -234,7 +238,7 @@
compiledNode = node.compile(merge(o, {
top: true
}));
return node.isStatement(o) ? compiledNode : ("" + (this.idt()) + compiledNode + ";");
return node.isStatement(o) ? compiledNode : "" + (this.idt()) + compiledNode + ";";
};
return Expressions;
})();
@ -273,7 +277,7 @@
var end, idt, val;
idt = this.isStatement(o) ? this.idt() : '';
end = this.isStatement(o) ? ';' : '';
val = this.isReserved() ? ("\"" + (this.value) + "\"") : this.value;
val = this.isReserved() ? "\"" + (this.value) + "\"" : this.value;
return idt + val + end;
};
Literal.prototype.toString = function() {
@ -401,7 +405,7 @@
}
code = this.base.compile(o);
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
code = ("(" + code + ")");
code = "(" + code + ")";
}
for (_i = 0, _len = props.length; _i < _len; _i++) {
prop = props[_i];
@ -489,7 +493,7 @@
if (!name) {
throw SyntaxError('cannot call super on an anonymous function.');
}
return method.klass ? ("" + (method.klass) + ".__super__." + name) : ("" + name + ".__super__.constructor");
return method.klass ? "" + (method.klass) + ".__super__." + name : "" + name + ".__super__.constructor";
};
Call.prototype.unfoldSoak = function(o) {
var _i, _len, _ref2, _ref3, call, ifn, left, list, rite, val;
@ -545,7 +549,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);
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; _i++) {
arg = _ref3[_i];
if (arg instanceof Splat) {
@ -556,11 +560,11 @@
_result = [];
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; _j++) {
arg = _ref4[_j];
_result.push((arg.parenthetical = true) && arg.compile(o));
_result.push(arg.compileBare(o));
}
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.prefix()) + (this.variable.compile(o)) + "(" + args + ")";
};
Call.prototype.compileSuper = function(args, o) {
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
@ -577,9 +581,9 @@
}
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)) + ")" + (name.compile(o));
} else {
fun = (ref = base.compile(o));
fun = ref = base.compile(o);
if (name) {
fun += name.compile(o);
}
@ -624,7 +628,7 @@
Accessor.prototype.compileNode = function(o) {
var name;
name = this.name.compile(o);
return this.prototype + (IS_STRING.test(name) ? ("[" + name + "]") : ("." + name));
return this.prototype + (IS_STRING.test(name) ? "[" + name + "]" : "." + name);
};
Accessor.prototype.isComplex = NO;
return Accessor;
@ -691,11 +695,11 @@
}
idx = del(o, 'index');
step = del(o, 'step');
vars = ("" + idx + " = " + (this.from)) + (this.to !== this.toVar ? (", " + (this.to)) : '');
intro = ("(" + (this.fromVar) + " <= " + (this.toVar) + " ? " + idx);
compare = ("" + intro + " <" + (this.equals) + " " + (this.toVar) + " : " + idx + " >" + (this.equals) + " " + (this.toVar) + ")");
vars = ("" + idx + " = " + (this.from)) + (this.to !== this.toVar ? ", " + (this.to) : '');
intro = "(" + (this.fromVar) + " <= " + (this.toVar) + " ? " + idx;
compare = "" + intro + " <" + (this.equals) + " " + (this.toVar) + " : " + idx + " >" + (this.equals) + " " + (this.toVar) + ")";
stepPart = step ? step.compile(o) : '1';
incr = step ? ("" + idx + " += " + stepPart) : ("" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")");
incr = step ? "" + idx + " += " + stepPart : "" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")";
return "" + vars + "; " + compare + "; " + incr;
};
Range.prototype.compileSimple = function(o) {
@ -703,8 +707,8 @@
_ref2 = [+this.fromNum, +this.toNum], from = _ref2[0], to = _ref2[1];
idx = del(o, 'index');
step = del(o, 'step');
step && (step = ("" + idx + " += " + (step.compile(o))));
return from <= to ? ("" + idx + " = " + from + "; " + idx + " <" + (this.equals) + " " + to + "; " + (step || ("" + idx + "++"))) : ("" + idx + " = " + from + "; " + idx + " >" + (this.equals) + " " + to + "; " + (step || ("" + idx + "--")));
step && (step = "" + idx + " += " + (step.compile(o)));
return from <= to ? "" + idx + " = " + from + "; " + idx + " <" + (this.equals) + " " + to + "; " + (step || ("" + idx + "++")) : "" + idx + " = " + from + "; " + idx + " >" + (this.equals) + " " + to + "; " + (step || ("" + idx + "--"));
};
Range.prototype.compileArray = function(o) {
var _i, _ref2, _ref3, _result, body, clause, i, idt, post, pre, range, result, vars;
@ -722,16 +726,16 @@
idt = this.idt(1);
i = o.scope.freeVariable('i');
result = o.scope.freeVariable('result');
pre = ("\n" + idt + result + " = [];");
pre = "\n" + idt + result + " = [];";
if (this.fromNum && this.toNum) {
o.index = i;
body = this.compileSimple(o);
} else {
vars = ("" + i + " = " + (this.from)) + (this.to !== this.toVar ? (", " + (this.to)) : '');
clause = ("" + (this.fromVar) + " <= " + (this.toVar) + " ?");
body = ("var " + vars + "; " + clause + " " + i + " <" + (this.equals) + " " + (this.toVar) + " : " + i + " >" + (this.equals) + " " + (this.toVar) + "; " + clause + " " + i + " += 1 : " + i + " -= 1");
vars = ("" + i + " = " + (this.from)) + (this.to !== this.toVar ? ", " + (this.to) : '');
clause = "" + (this.fromVar) + " <= " + (this.toVar) + " ?";
body = "var " + vars + "; " + clause + " " + i + " <" + (this.equals) + " " + (this.toVar) + " : " + i + " >" + (this.equals) + " " + (this.toVar) + "; " + clause + " " + i + " += 1 : " + i + " -= 1";
}
post = ("{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + (o.indent));
post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + (o.indent);
return "(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).call(this)";
};
return Range;
@ -763,7 +767,7 @@
ObjectLiteral = (function() {
function ObjectLiteral(props) {
ObjectLiteral.__super__.constructor.call(this);
this.objects = (this.properties = props || []);
this.objects = this.properties = props || [];
return this;
};
return ObjectLiteral;
@ -790,7 +794,7 @@
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; i++) {
prop = _ref2[i];
_result.push((function() {
join = i === this.properties.length - 1 ? '' : (prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n');
join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
indent = prop instanceof Comment ? '' : this.idt(1);
if (prop instanceof Value && prop.tags["this"]) {
prop = new Assign(prop.properties[0].name, prop, 'object');
@ -803,8 +807,8 @@
return _result;
}).call(this);
props = props.join('');
obj = ("{" + (props ? '\n' + props + '\n' + this.idt() : '') + "}");
return this.tags.front ? ("(" + obj + ")") : obj;
obj = "{" + (props ? '\n' + props + '\n' + this.idt() : '') + "}";
return this.tags.front ? "(" + obj + ")" : obj;
};
ObjectLiteral.prototype.assigns = function(name) {
var _i, _len, _ref2, prop;
@ -845,10 +849,10 @@
for (i = 0, _len2 = (_ref3 = this.objects).length; i < _len2; i++) {
obj = _ref3[i];
code = obj.compile(o);
objects.push(obj instanceof Comment ? ("\n" + code + "\n" + (o.indent)) : (i === this.objects.length - 1 ? code : code + ', '));
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 + "]");
return 0 < objects.indexOf('\n') ? "[\n" + (o.indent) + objects + "\n" + (this.tab) + "]" : "[" + objects + "]";
};
ArrayLiteral.prototype.assigns = function(name) {
var _i, _len, _ref2, obj;
@ -999,7 +1003,7 @@
this.value.name = match[2];
this.value.klass = match[1];
}
val = this.value.compile(o);
val = this.value.compileBare(o);
if (this.context === 'object') {
return ("" + name + ": " + val);
}
@ -1010,7 +1014,7 @@
if (stmt) {
return ("" + (this.tab) + val + ";");
}
return top || this.parenthetical ? val : ("(" + val + ")");
return top || this.parenthetical ? 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;
@ -1026,7 +1030,7 @@
if (obj instanceof Assign) {
_ref2 = obj, idx = _ref2.variable.base, obj = _ref2.value;
} else {
idx = isObject ? (obj.tags["this"] ? obj.properties[0].name : obj) : new Literal(0);
idx = isObject ? obj.tags["this"] ? obj.properties[0].name : obj : new Literal(0);
}
if (!(value instanceof Value)) {
value = new Value(value);
@ -1065,7 +1069,7 @@
splat = true;
} else {
if (typeof idx !== 'object') {
idx = new Literal(splat ? ("" + valVar + ".length - " + (olength - idx)) : idx);
idx = new Literal(splat ? "" + valVar + ".length - " + (olength - idx) : idx);
}
val = new Value(new Literal(valVar), [new accessClass(idx)]);
}
@ -1075,7 +1079,7 @@
assigns.push(valVar);
}
code = assigns.join(', ');
return top || this.parenthetical ? code : ("(" + code + ")");
return top || this.parenthetical ? code : "(" + code + ")";
};
Assign.prototype.compileSplice = function(o) {
var from, name, plus, range, ref, to, val;
@ -1083,7 +1087,7 @@
name = this.variable.compile(o);
plus = range.exclusive ? '' : ' + 1';
from = range.from ? range.from.compile(o) : '0';
to = range.to ? range.to.compile(o) + ' - ' + from + plus : ("" + name + ".length");
to = range.to ? range.to.compile(o) + ' - ' + from + plus : "" + name + ".length";
ref = o.scope.freeVariable('ref');
val = this.value.compile(o);
return "([].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + ref + " = " + val + ")), " + ref + ")";
@ -1173,15 +1177,15 @@
if (this.className) {
o.indent = this.idt(2);
}
code = this.body.expressions.length ? ("\n" + (this.body.compileWithDeclarations(o)) + "\n") : '';
open = this.className ? ("(function() {\n" + comm + (this.idt(1)) + "function " + (this.className) + "(") : "function(";
close = this.className ? ("" + (code && this.idt(1)) + "};\n" + (this.idt(1)) + "return " + (this.className) + ";\n" + (this.tab) + "})()") : ("" + (code && this.tab) + "}");
func = ("" + open + (params.join(', ')) + ") {" + code + close);
code = this.body.expressions.length ? "\n" + (this.body.compileWithDeclarations(o)) + "\n" : '';
open = this.className ? "(function() {\n" + comm + (this.idt(1)) + "function " + (this.className) + "(" : "function(";
close = this.className ? "" + (code && this.idt(1)) + "};\n" + (this.idt(1)) + "return " + (this.className) + ";\n" + (this.tab) + "})()" : "" + (code && this.tab) + "}";
func = "" + open + (params.join(', ')) + ") {" + code + close;
o.scope.endLevel();
if (this.bound) {
return ("" + (utility('bind')) + "(" + func + ", " + (this.context) + ")");
}
return this.tags.front ? ("(" + func + ")") : func;
return this.tags.front ? "(" + func + ")" : func;
};
Code.prototype.traverseChildren = function(crossScope, func) {
return crossScope ? Code.__super__.traverseChildren.call(this, crossScope, func) : undefined;
@ -1245,7 +1249,7 @@
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;
end = this.trailings.length ? ", " + len + " - " + (this.trailings.length) : undefined;
for (idx = 0, _len = (_ref2 = this.trailings).length; idx < _len; idx++) {
trailing = _ref2[idx];
if (trailing.attach) {
@ -1261,7 +1265,7 @@
};
Splat.prototype.compileValue = function(o, name, index, trailings) {
var trail;
trail = trailings ? (", " + name + ".length - " + trailings) : '';
trail = trailings ? ", " + name + ".length - " + trailings : '';
return "" + (utility('slice')) + ".call(" + name + ", " + index + trail + ")";
};
Splat.compileSplattedArray = function(list, o) {
@ -1274,16 +1278,16 @@
prev = args[end];
if (!(arg instanceof Splat)) {
if (prev && starts(prev, '[') && ends(prev, ']')) {
args[end] = ("" + (prev.slice(0, -1)) + ", " + code + "]");
args[end] = "" + (prev.slice(0, -1)) + ", " + code + "]";
continue;
}
if (prev && starts(prev, '.concat([') && ends(prev, '])')) {
args[end] = ("" + (prev.slice(0, -2)) + ", " + code + "])");
args[end] = "" + (prev.slice(0, -2)) + ", " + code + "])";
continue;
}
code = ("[" + code + "]");
code = "[" + code + "]";
}
args[++end] = i === 0 ? code : (".concat(" + code + ")");
args[++end] = i === 0 ? code : ".concat(" + code + ")";
}
return args.join('');
};
@ -1315,18 +1319,17 @@
var cond, post, pre, rvar, set, top;
top = del(o, 'top') && !this.returns;
o.indent = this.idt(1);
this.condition.parenthetical = true;
cond = this.condition.compile(o);
cond = this.condition.compileBare(o);
o.top = true;
set = '';
if (!top) {
rvar = o.scope.freeVariable('result');
set = ("" + (this.tab) + rvar + " = [];\n");
set = "" + (this.tab) + rvar + " = [];\n";
if (this.body) {
this.body = Push.wrap(rvar, this.body);
}
}
pre = ("" + set + (this.tab) + "while (" + cond + ")");
pre = "" + set + (this.tab) + "while (" + cond + ")";
if (this.guard) {
this.body = Expressions.wrap([new If(this.guard, this.body)]);
}
@ -1428,7 +1431,8 @@
fst = this.first;
ref = fst.compile(o);
}
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o)));
this.second.tags.operation = false;
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compileBare(o)));
};
Op.prototype.compileUnary = function(o) {
var _ref2, parts, space;
@ -1478,9 +1482,9 @@
}), {
precompile: true
}), obj1 = _ref2[0], obj2 = _ref2[1];
prefix = obj1 !== obj2 ? ("" + obj1 + ", ") : '';
code = ("" + prefix + (utility('indexOf')) + ".call(" + (this.array.compile(o)) + ", " + obj2 + ") >= 0");
return this.parenthetical ? code : ("(" + code + ")");
prefix = obj1 !== obj2 ? "" + obj1 + ", " : '';
code = "" + prefix + (utility('indexOf')) + ".call(" + (this.array.compile(o)) + ", " + obj2 + ") >= 0";
return this.parenthetical ? code : "(" + code + ")";
};
return In;
})();
@ -1513,8 +1517,8 @@
o.indent = this.idt(1);
o.top = true;
attemptPart = this.attempt.compile(o);
errorPart = this.error ? (" (" + (this.error.compile(o)) + ") ") : ' ';
catchPart = this.recovery ? (" catch" + errorPart + "{\n" + (this.recovery.compile(o)) + "\n" + (this.tab) + "}") : (!(this.ensure || this.recovery) ? ' catch (_e) {}' : '');
errorPart = this.error ? " (" + (this.error.compile(o)) + ") " : ' ';
catchPart = this.recovery ? " catch" + errorPart + "{\n" + (this.recovery.compile(o)) + "\n" + (this.tab) + "}" : !(this.ensure || this.recovery) ? ' catch (_e) {}' : '';
finallyPart = (this.ensure || '') && ' finally {\n' + this.ensure.compile(merge(o)) + ("\n" + (this.tab) + "}");
return "" + (this.tab) + "try {\n" + attemptPart + "\n" + (this.tab) + "}" + catchPart + finallyPart;
};
@ -1552,8 +1556,8 @@
Existence.prototype.compileNode = function(o) {
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 + ")");
code = IDENTIFIER.test(code) && !o.scope.check(code) ? "typeof " + code + " !== \"undefined\" && " + code + " !== null" : "" + code + " != null";
return this.parenthetical ? code : "(" + code + ")";
};
return Existence;
})();
@ -1581,8 +1585,7 @@
Parens.prototype.compileNode = function(o) {
var code, top;
top = del(o, 'top');
this.expression.parenthetical = true;
code = this.expression.compile(o);
code = this.expression.compileBare(o);
if (top && this.expression.isPureStatement(o)) {
return code;
}
@ -1675,26 +1678,26 @@
step: this.step
}));
} else {
svar = (sourcePart = this.source.compile(o));
svar = sourcePart = this.source.compile(o);
if ((name || !this.raw) && !(IDENTIFIER.test(svar) && scope.check(svar, {
immediate: true
}))) {
sourcePart = ("" + (ref = scope.freeVariable('ref')) + " = " + svar);
sourcePart = "" + (ref = scope.freeVariable('ref')) + " = " + svar;
if (!this.object) {
sourcePart = ("(" + sourcePart + ")");
sourcePart = "(" + sourcePart + ")";
}
svar = ref;
}
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(merge(o, {
top: true
})) : (name ? ("" + name + " = " + svar + "[" + ivar + "]") : undefined);
})) : name ? "" + name + " = " + svar + "[" + ivar + "]" : undefined;
if (!this.object) {
lvar = scope.freeVariable('len');
stepPart = this.step ? ("" + ivar + " += " + (this.step.compile(o))) : ("" + ivar + "++");
forPart = ("" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length; " + ivar + " < " + lvar + "; " + stepPart);
stepPart = this.step ? "" + ivar + " += " + (this.step.compile(o)) : "" + ivar + "++";
forPart = "" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length; " + ivar + " < " + lvar + "; " + stepPart;
}
}
resultPart = rvar ? ("" + (this.tab) + rvar + " = [];\n") : '';
resultPart = rvar ? "" + (this.tab) + rvar + " = [];\n" : '';
returnResult = this.compileReturnValue(rvar, o);
if (!topLevel) {
body = Push.wrap(rvar, body);
@ -1730,24 +1733,24 @@
}
} else {
if (namePart) {
varPart = ("" + idt1 + namePart + ";\n");
varPart = "" + idt1 + namePart + ";\n";
}
if (forPart && name === ivar) {
unstepPart = this.step ? ("" + name + " -= " + (this.step.compile(o)) + ";") : ("" + name + "--;");
unstepPart = this.step ? "" + name + " -= " + (this.step.compile(o)) + ";" : "" + name + "--;";
unstepPart = ("\n" + (this.tab)) + unstepPart;
}
}
if (this.object) {
forPart = ("" + ivar + " in " + sourcePart);
forPart = "" + ivar + " in " + sourcePart;
if (!this.raw) {
guardPart = ("\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;");
guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;";
}
}
body = body.compile(merge(o, {
indent: idt1,
top: true
}));
vars = range ? name : ("" + name + ", " + ivar);
vars = range ? name : "" + name + ", " + ivar;
return "" + resultPart + (this.tab) + "for (" + forPart + ") {" + guardPart + "\n" + varPart + body + "\n" + (this.tab) + "}" + unstepPart + returnResult;
};
return For;
@ -1780,9 +1783,9 @@
Switch.prototype.compileNode = function(o) {
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, _ref5, block, code, condition, conditions, idt1, idt2;
idt1 = this.idt(1);
idt2 = (o.indent = this.idt(2));
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 = (_ref3 = this.cases).length; _i < _len; _i++) {
_ref4 = _ref3[_i], conditions = _ref4[0], block = _ref4[1];
for (_j = 0, _len2 = (_ref5 = flatten([conditions])).length; _j < _len2; _j++) {
@ -1790,17 +1793,17 @@
if (!this.subject) {
condition = condition.invert().invert();
}
code += ("\n" + idt1 + "case " + (condition.compile(o)) + ":");
code += "\n" + idt1 + "case " + (condition.compile(o)) + ":";
}
code += ("\n" + (block.compile(o)));
code += "\n" + (block.compile(o));
if (!(last(block.expressions) instanceof Return)) {
code += ("\n" + idt2 + "break;");
code += "\n" + idt2 + "break;";
}
}
if (this.otherwise) {
code += ("\n" + idt1 + "default:\n" + (this.otherwise.compile(o)));
code += "\n" + idt1 + "default:\n" + (this.otherwise.compile(o));
}
code += ("\n" + (this.tab) + "}");
code += "\n" + (this.tab) + "}";
return code;
};
return Switch;
@ -1819,7 +1822,7 @@
return If;
})();
__extends(If, Base);
If.prototype.children = ['condition', 'body', 'elseBody', 'assigner'];
If.prototype.children = ['condition', 'body', 'elseBody'];
If.prototype.topSensitive = YES;
If.prototype.bodyNode = function() {
var _ref2;
@ -1842,10 +1845,6 @@
var _ref2;
return this.statement || (this.statement = ((o != null) ? o.top : undefined) || this.bodyNode().isStatement(o) || (((_ref2 = this.elseBodyNode()) != null) ? _ref2.isStatement(o) : undefined));
};
If.prototype.compileCondition = function(o) {
this.condition.parenthetical = true;
return this.condition.compile(o);
};
If.prototype.compileNode = function(o) {
return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o);
};
@ -1868,28 +1867,22 @@
condO = merge(o);
o.indent = this.idt(1);
o.top = true;
ifPart = ("if (" + (this.compileCondition(condO)) + ") {\n" + (this.body.compile(o)) + "\n" + (this.tab) + "}");
ifPart = "if (" + (this.condition.compileBare(condO)) + ") {\n" + (this.body.compile(o)) + "\n" + (this.tab) + "}";
if (!child) {
ifPart = this.tab + ifPart;
}
if (!this.elseBody) {
return ifPart;
}
return ifPart + (this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, {
return ifPart + ' else ' + (this.isChain ? this.elseBodyNode().compile(merge(o, {
indent: this.tab,
chainChild: true
})) : (" else {\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}"));
})) : "{\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}");
};
If.prototype.compileExpression = function(o) {
var code, elsePart, ifPart;
this.bodyNode().tags.operation = (this.condition.tags.operation = true);
if (this.elseBody) {
this.elseBodyNode().tags.operation = true;
}
ifPart = this.condition.compile(o) + ' ? ' + this.bodyNode().compile(o);
elsePart = this.elseBody ? this.elseBodyNode().compile(o) : 'undefined';
code = ("" + ifPart + " : " + elsePart);
return this.tags.operation || this.soakNode ? ("(" + code + ")") : code;
var _ref2, code;
code = this.condition.compile(o) + ' ? ' + this.bodyNode().compileBare(o) + ' : ' + (((_ref2 = this.elseBodyNode()) != null) ? _ref2.compileBare(o) : undefined);
return this.tags.operation || this.soakNode ? "(" + code + ")" : code;
};
If.prototype.unfoldSoak = function() {
return this.soakNode && this;
@ -1955,7 +1948,7 @@
IS_STRING = /^['"]/;
utility = function(name) {
var ref;
ref = ("__" + name);
ref = "__" + name;
Scope.root.assign(ref, UTILITIES[name]);
return ref;
};

View File

@ -154,7 +154,7 @@
};
return this.scanTokens(function(token, i, tokens) {
var _ref, idx, tag, tok;
if (_ref = (tag = token[0]), __indexOf.call(EXPRESSION_START, _ref) >= 0) {
if (_ref = tag = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0) {
stack.push(tag === 'INDENT' && this.tag(i - 1) === '{' ? '{' : tag);
return 1;
}
@ -243,7 +243,7 @@
if (starter === 'THEN') {
indent.fromThen = true;
}
indent.generated = (outdent.generated = true);
indent.generated = outdent.generated = true;
tokens.splice(i + 1, 0, indent);
condition = function(token, i) {
var _ref3;
@ -274,7 +274,7 @@
}
original = token;
this.detectEnd(i + 1, condition, function(token, i) {
return token[0] !== 'INDENT' ? (original[0] = 'POST_' + original[0]) : undefined;
return token[0] !== 'INDENT' ? original[0] = 'POST_' + original[0] : undefined;
});
return 1;
});
@ -326,7 +326,7 @@
}
return this.scanTokens(function(token, i, tokens) {
var _ref, inv, match, mtag, oppos, tag, val;
if (_ref = (tag = token[0]), __indexOf.call(EXPRESSION_START, _ref) >= 0) {
if (_ref = tag = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0) {
stack.push(token);
return 1;
}

View File

@ -68,6 +68,11 @@ exports.Base = class Base
(pair[i] = node.compile o) for node, i in pair if options?.precompile
pair
# Compile unparenthesized.
compileBare: (o) ->
@parenthetical = on
@compile o
# Convenience method to grab the current indentation level, plus tabbing in.
idt: (tabs) ->
(@tab or '') + Array((tabs or 0) + 1).join TAB
@ -494,7 +499,7 @@ exports.Call = class Call extends Base
@variable?.tags.front = @tags.front
for arg in @args when arg instanceof Splat
return @compileSplat o
args = ((arg.parenthetical = on) and arg.compile o for arg in @args).join ', '
args = (arg.compileBare o for arg in @args).join ', '
if @isSuper
@compileSuper args, o
else
@ -864,7 +869,7 @@ exports.Assign = class Assign extends Base
if @value instanceof Code and match = @METHOD_DEF.exec name
@value.name = match[2]
@value.klass = match[1]
val = @value.compile o
val = @value.compileBare o
return "#{name}: #{val}" if @context is 'object'
o.scope.find name unless isValue and (@variable.hasProperties() or @variable.namespaced)
val = name + " #{ @context or '=' } " + val
@ -1131,8 +1136,7 @@ exports.While = class While extends Base
compileNode: (o) ->
top = del(o, 'top') and not @returns
o.indent = @idt 1
@condition.parenthetical = yes
cond = @condition.compile(o)
cond = @condition.compileBare o
o.top = true
set = ''
unless top
@ -1232,7 +1236,8 @@ exports.Op = class Op extends Base
else
fst = @first
ref = fst.compile o
new Existence(fst).compile(o) + " ? #{ref} : #{ @second.compile o }"
@second.tags.operation = no
new Existence(fst).compile(o) + " ? #{ref} : #{ @second.compileBare o }"
# Compile a unary **Op**.
compileUnary: (o) ->
@ -1360,8 +1365,7 @@ exports.Parens = class Parens extends Base
compileNode: (o) ->
top = del o, 'top'
@expression.parenthetical = true
code = @expression.compile o
code = @expression.compileBare o
return code if top and @expression.isPureStatement o
if @parenthetical or @isStatement o
return if top then @tab + code + ';' else code
@ -1517,7 +1521,7 @@ exports.Switch = class Switch extends Base
# because ternaries are already proper expressions, and don't need conversion.
exports.If = class If extends Base
children: ['condition', 'body', 'elseBody', 'assigner']
children: ['condition', 'body', 'elseBody']
topSensitive: YES
@ -1545,10 +1549,6 @@ exports.If = class If extends Base
isStatement: (o) ->
@statement or= o?.top or @bodyNode().isStatement(o) or @elseBodyNode()?.isStatement(o)
compileCondition: (o) ->
@condition.parenthetical = yes
@condition.compile o
compileNode: (o) ->
if @isStatement o then @compileStatement o else @compileExpression o
@ -1571,21 +1571,19 @@ exports.If = class If extends Base
condO = merge o
o.indent = @idt 1
o.top = true
ifPart = "if (#{ @compileCondition condO }) {\n#{ @body.compile o }\n#{@tab}}"
ifPart = "if (#{ @condition.compileBare condO }) {\n#{ @body.compile o }\n#{@tab}}"
ifPart = @tab + ifPart unless child
return ifPart unless @elseBody
ifPart + if @isChain
' else ' + @elseBodyNode().compile merge o, indent: @tab, chainChild: true
ifPart + ' else ' + if @isChain
@elseBodyNode().compile merge o, indent: @tab, chainChild: true
else
" else {\n#{ @elseBody.compile(o) }\n#{@tab}}"
"{\n#{ @elseBody.compile o }\n#{@tab}}"
# Compile the If as a conditional operator.
compileExpression: (o) ->
@bodyNode().tags.operation = @condition.tags.operation = yes
@elseBodyNode().tags.operation = yes if @elseBody
ifPart = @condition.compile(o) + ' ? ' + @bodyNode().compile(o)
elsePart = if @elseBody then @elseBodyNode().compile(o) else 'undefined'
code = "#{ifPart} : #{elsePart}"
code = @condition.compile(o) + ' ? ' +
@bodyNode().compileBare(o) + ' : ' +
@elseBodyNode()?.compileBare o
if @tags.operation or @soakNode then "(#{code})" else code
unfoldSoak: -> @soakNode and this