removed extra parens around simple values

This commit is contained in:
satyr 2010-10-22 08:50:36 +09:00
parent f43ee4075d
commit 341de42692
9 changed files with 73 additions and 53 deletions

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

@ -23,7 +23,7 @@
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;
}

View File

@ -221,7 +221,7 @@
return process.exit(0);
};
version = function() {
puts("CoffeeScript version " + (CoffeeScript.VERSION));
puts("CoffeeScript version " + CoffeeScript.VERSION);
return process.exit(0);
};
}).call(this);

View File

@ -605,7 +605,7 @@
}
}
if (name === 'Root') {
alt[1] = "return " + (alt[1]);
alt[1] = "return " + alt[1];
}
return alt;
})());

View File

@ -224,10 +224,10 @@
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;
};
@ -277,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() {
@ -316,7 +316,7 @@
}
expr = ' ' + this.expression.compileBare(o);
}
return "" + (this.tab) + "return" + expr + ";";
return "" + this.tab + "return" + expr + ";";
};
return Return;
})();
@ -354,6 +354,16 @@
Value.prototype.isComplex = function() {
return this.base.isComplex() || this.hasProperties();
};
Value.prototype.isAtomic = function() {
var _i, _len, _ref2, node;
for (_i = 0, _len = (_ref2 = this.properties.concat(this.base)).length; _i < _len; _i++) {
node = _ref2[_i];
if (node.soakNode || node instanceof Call) {
return false;
}
}
return true;
};
Value.prototype.assigns = function(name) {
return !this.properties.length && this.base.assigns(name);
};
@ -496,7 +506,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;
@ -592,7 +602,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)) + ", " + 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)) + ", " + splatargs + ", function() {})";
};
return Call;
})();
@ -696,9 +706,9 @@
}
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 + ")";
return "" + vars + "; " + compare + "; " + incr;
@ -709,7 +719,7 @@
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 + "--"));
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;
@ -732,11 +742,11 @@
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;
@ -850,10 +860,10 @@
for (i = 0, _len2 = (_ref3 = this.objects).length; i < _len2; i++) {
obj = _ref3[i];
code = obj.compileBare(o);
objects.push(obj instanceof Comment ? "\n" + code + "\n" + (o.indent) : i === this.objects.length - 1 ? code : code + ', ');
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;
@ -1013,7 +1023,7 @@
}
val = name + (" " + (this.context || '=') + " ") + val;
if (stmt) {
return "" + (this.tab) + val + ";";
return "" + this.tab + val + ";";
}
return top || this.parenthetical ? val : "(" + val + ")";
};
@ -1176,12 +1186,12 @@
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) + "}";
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 "" + (utility('bind')) + "(" + func + ", " + this.context + ")";
}
return this.tags.front ? "(" + func + ")" : func;
};
@ -1247,7 +1257,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) {
@ -1259,7 +1269,7 @@
o.scope.assign(trailing.compile(o), "arguments[" + variadic + " ? " + len + " - " + pos + " : " + (this.index + idx) + "]");
}
}
return "" + name + " = " + (utility('slice')) + ".call(arguments, " + (this.index) + end + ")";
return "" + name + " = " + (utility('slice')) + ".call(arguments, " + this.index + end + ")";
};
Splat.prototype.compileValue = function(o, name, index, trailings) {
var trail;
@ -1322,12 +1332,12 @@
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)]);
}
@ -1338,7 +1348,7 @@
} else {
post = '';
}
return "" + pre + " {\n" + (this.body.compile(o)) + "\n" + (this.tab) + "}" + post;
return "" + pre + " {\n" + (this.body.compile(o)) + "\n" + this.tab + "}" + post;
};
return While;
})();
@ -1416,12 +1426,12 @@
return this.compileExistence(o);
}
this.first.tags.front = this.tags.front;
return "" + (this.first.compile(o)) + " " + (this.operator) + " " + (this.second.compile(o));
return "" + (this.first.compile(o)) + " " + this.operator + " " + (this.second.compile(o));
};
Op.prototype.compileChain = function(o) {
var _ref2, shared;
_ref2 = this.first.unwrap().second.compileReference(o), this.first.second = _ref2[0], shared = _ref2[1];
return "" + (this.first.compile(o)) + " && " + (shared.compile(o)) + " " + (this.operator) + " " + (this.second.compile(o));
return "" + (this.first.compile(o)) + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o));
};
Op.prototype.compileExistence = function(o) {
var fst, ref;
@ -1524,9 +1534,9 @@
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) {}' : '';
finallyPart = (this.ensure || '') && ' finally {\n' + this.ensure.compile(merge(o)) + ("\n" + (this.tab) + "}");
return "" + (this.tab) + "try {\n" + attemptPart + "\n" + (this.tab) + "}" + catchPart + finallyPart;
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;
};
return Try;
})();
@ -1544,7 +1554,7 @@
Throw.prototype.isStatement = YES;
Throw.prototype.makeReturn = THIS;
Throw.prototype.compileNode = function(o) {
return "" + (this.tab) + "throw " + (this.expression.compile(o)) + ";";
return "" + this.tab + "throw " + (this.expression.compile(o)) + ";";
};
return Throw;
})();
@ -1589,13 +1599,15 @@
return this.expression.makeReturn();
};
Parens.prototype.compileNode = function(o) {
var code, top;
var code, expr, top;
top = del(o, 'top');
code = this.expression.compileBare(o);
if (top && this.expression.isPureStatement(o)) {
return code;
expr = this.expression;
if (expr instanceof Value && expr.isAtomic()) {
expr.tags.front = this.tags.front;
return expr.compile(o);
}
if (this.parenthetical || this.isStatement(o)) {
code = expr.compileBare(o);
if (this.parenthetical || expr.isStatement(o)) {
return top ? this.tab + code + ';' : code;
}
return "(" + code + ")";
@ -1706,7 +1718,7 @@
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);
@ -1746,7 +1758,7 @@
}
if (forPart && name === ivar) {
unstepPart = this.step ? "" + name + " -= " + (this.step.compile(o)) + ";" : "" + name + "--;";
unstepPart = ("\n" + (this.tab)) + unstepPart;
unstepPart = ("\n" + this.tab) + unstepPart;
}
}
if (this.object) {
@ -1760,7 +1772,7 @@
top: true
}));
vars = this.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;
})();
@ -1794,7 +1806,7 @@
idt1 = this.idt(1);
idt2 = o.indent = this.idt(2);
o.top = true;
code = "" + (this.tab) + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {";
code = "" + this.tab + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {";
for (_i = 0, _len = (_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++) {
@ -1812,7 +1824,7 @@
if (this.otherwise) {
code += "\n" + idt1 + "default:\n" + (this.otherwise.compile(o));
}
code += "\n" + (this.tab) + "}";
code += "\n" + this.tab + "}";
return code;
};
return Switch;
@ -1876,7 +1888,7 @@
condO = merge(o);
o.indent = this.idt(1);
o.top = true;
ifPart = "if (" + (this.condition.compileBare(condO)) + ") {\n" + (this.body.compile(o)) + "\n" + (this.tab) + "}";
ifPart = "if (" + (this.condition.compileBare(condO)) + ") {\n" + (this.body.compile(o)) + "\n" + this.tab + "}";
if (!child) {
ifPart = this.tab + ifPart;
}
@ -1886,7 +1898,7 @@
return ifPart + ' else ' + (this.isChain ? this.elseBodyNode().compile(merge(o, {
indent: this.tab,
chainChild: true
})) : "{\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}");
})) : "{\n" + (this.elseBody.compile(o)) + "\n" + this.tab + "}");
};
If.prototype.compileExpression = function(o) {
var _ref2, code;

View File

@ -42,7 +42,7 @@
var _i, _len, _ref, letPart, lines, rule, spaces;
lines = ['Available options:'];
if (this.banner) {
lines.unshift("" + (this.banner) + "\n");
lines.unshift("" + this.banner + "\n");
}
for (_i = 0, _len = (_ref = this.rules).length; _i < _len; _i++) {
rule = _ref[_i];

View File

@ -298,7 +298,7 @@
levels[open] -= 1;
}
if (levels[open] < 0) {
throw Error("too many " + (token[1]) + " on line " + (token[2] + 1));
throw Error("too many " + token[1] + " on line " + (token[2] + 1));
}
}
return 1;

View File

@ -137,7 +137,7 @@
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
v = _ref2[_i];
if (v.type.assigned) {
_result.push("" + (v.name) + " = " + (v.type.value));
_result.push("" + v.name + " = " + v.type.value);
}
}
return _result;

View File

@ -335,6 +335,11 @@ exports.Value = class Value extends Base
isComplex: ->
@base.isComplex() or @hasProperties()
isAtomic: ->
for node in @properties.concat @base
return no if node.soakNode or node instanceof Call
yes
assigns: (name) ->
not @properties.length and @base.assigns name
@ -1374,9 +1379,12 @@ exports.Parens = class Parens extends Base
compileNode: (o) ->
top = del o, 'top'
code = @expression.compileBare o
return code if top and @expression.isPureStatement o
if @parenthetical or @isStatement o
expr = @expression
if expr instanceof Value and expr.isAtomic()
expr.tags.front = @tags.front
return expr.compile o
code = expr.compileBare o
if @parenthetical or expr.isStatement o
return if top then @tab + code + ';' else code
"(#{code})"