1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

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)); return eval(CoffeeScript.compile(code, options));
}; };
CoffeeScript.run = function(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))(); return Function(CoffeeScript.compile(code, options))();
}; };
if (!(typeof window !== "undefined" && window !== null)) { if (!(typeof window !== "undefined" && window !== null)) {
@ -30,9 +30,9 @@
(function() { (function() {
var script = _ref[_i]; var script = _ref[_i];
_j = script; _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); return CoffeeScript.run(script.innerHTML);
})) : undefined; }) : undefined;
})(); })();
script = _j; script = _j;
} }

View file

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

View file

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

View file

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

View file

@ -9,7 +9,7 @@
if (!action) { if (!action) {
return [patternString, '$$ = $1;', options]; 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(/\bnew /g, '$&yy.');
action = action.replace(/\bExpressions\.wrap\b/g, 'yy.$&'); action = action.replace(/\bExpressions\.wrap\b/g, 'yy.$&');
return [patternString, ("$$ = " + action + ";"), options]; return [patternString, ("$$ = " + action + ";"), options];
@ -586,7 +586,7 @@
}), o("SimpleAssignable COMPOUND_ASSIGN INDENT Expression OUTDENT", function() { }), o("SimpleAssignable COMPOUND_ASSIGN INDENT Expression OUTDENT", function() {
return new Assign($1, $4, $2); return new Assign($1, $4, $2);
}), o("Expression RELATION Expression", function() { }), 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') { if (name === 'Root') {
alt[1] = ("return " + (alt[1])); alt[1] = "return " + (alt[1]);
} }
return alt; return alt;
})()); })());

View file

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

View file

@ -80,7 +80,7 @@
} }
if (!forcedIdentifier) { if (!forcedIdentifier) {
if (COFFEE_ALIASES.hasOwnProperty(id)) { if (COFFEE_ALIASES.hasOwnProperty(id)) {
tag = (id = COFFEE_ALIASES[id]); tag = id = COFFEE_ALIASES[id];
} }
if (id === '!') { if (id === '!') {
tag = 'UNARY'; tag = 'UNARY';
@ -266,7 +266,7 @@
diff = size - this.indent + this.outdebt; diff = size - this.indent + this.outdebt;
this.token('INDENT', diff); this.token('INDENT', diff);
this.indents.push(diff); this.indents.push(diff);
this.outdebt = (this.indebt = 0); this.outdebt = this.indebt = 0;
} else { } else {
this.indebt = 0; this.indebt = 0;
this.outdentToken(this.indent - size, noNewlines); this.outdentToken(this.indent - size, noNewlines);
@ -564,11 +564,11 @@
}; };
Lexer.prototype.tag = function(index, tag) { Lexer.prototype.tag = function(index, tag) {
var tok; 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) { Lexer.prototype.value = function(index, val) {
var tok; 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() { Lexer.prototype.unfinished = function() {
var prev, value; var prev, value;

View file

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

View file

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