nodes: object literals are now parenthesized based on @tags.front (which indicates if the node leads an expression statement), fixing #542

This commit is contained in:
satyr
2010-10-12 11:25:54 +09:00
parent ed79715841
commit ac841ca4e9
3 changed files with 34 additions and 34 deletions

View File

@@ -37,7 +37,6 @@
return code;
};
Base.prototype.compileClosure = function(o) {
this.tab = o.indent;
o.sharedScope = o.scope;
return Closure.wrap(this).compile(o);
};
@@ -232,6 +231,7 @@
Expressions.prototype.compileExpression = function(node, o) {
var compiledNode;
this.tab = o.indent;
node.tags.front = true;
compiledNode = node.compile(merge(o, {
top: true
}));
@@ -364,8 +364,8 @@
Value.prototype.isStatement = function(o) {
return this.base.isStatement(o) && !this.properties.length;
};
Value.prototype.isNumber = function() {
return this.base instanceof Literal && NUMBER.test(this.base.value);
Value.prototype.isSimpleNumber = function() {
return this.base instanceof Literal && SIMPLENUM.test(this.base.value);
};
Value.prototype.cacheReference = function(o) {
var base, bref, name, nref;
@@ -389,6 +389,7 @@
return [base.push(name), new Value(bref || base.base, [nref || name])];
};
Value.prototype.compile = function(o) {
this.base.tags.front = this.tags.front;
return !o.top || this.properties.length ? Value.__super__.compile.call(this, o) : this.base.compile(o);
};
Value.prototype.compileNode = function(o) {
@@ -401,7 +402,7 @@
this.base.parenthetical = true;
}
code = this.base.compile(o);
if (props[0] instanceof Accessor && this.isNumber() || o.top && this.base instanceof ObjectLiteral) {
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
code = ("(" + code + ")");
}
for (_i = 0, _len = props.length; _i < _len; _i++) {
@@ -540,16 +541,17 @@
return node;
};
Call.prototype.compileNode = function(o) {
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, _result, arg, args, left, node, rite, val;
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, _ref5, _result, arg, args, left, node, rite, val;
if (node = this.unfoldSoak(o)) {
return node.compile(o);
}
(((_ref2 = this.variable) != null) ? (_ref2.tags.front = this.tags.front) : undefined);
if (this.exist) {
if (val = this.variable) {
if (!(val instanceof Value)) {
val = new Value(val);
}
_ref2 = val.cacheReference(o), left = _ref2[0], rite = _ref2[1];
_ref3 = val.cacheReference(o), left = _ref3[0], rite = _ref3[1];
rite = new Call(rite, this.args);
} else {
left = new Literal(this.superReference(o));
@@ -560,16 +562,16 @@
rite = rite.compile(o);
return ("(" + left + " ? undefined : " + rite + ")");
}
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; _i++) {
arg = _ref3[_i];
for (_i = 0, _len = (_ref4 = this.args).length; _i < _len; _i++) {
arg = _ref4[_i];
if (arg instanceof Splat) {
return this.compileSplat(o);
}
}
args = (function() {
_result = [];
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; _j++) {
arg = _ref4[_j];
for (_j = 0, _len2 = (_ref5 = this.args).length; _j < _len2; _j++) {
arg = _ref5[_j];
_result.push((arg.parenthetical = true) && arg.compile(o));
}
return _result;
@@ -803,7 +805,6 @@
})();
__extends(ObjectLiteral, Base);
ObjectLiteral.prototype.children = ['properties'];
ObjectLiteral.prototype.topSensitive = YES;
ObjectLiteral.prototype.compileNode = function(o) {
var _i, _len, _ref2, _result, i, indent, join, lastNoncom, nonComments, obj, prop, props, top;
top = del(o, 'top');
@@ -838,7 +839,7 @@
}).call(this);
props = props.join('');
obj = ("{" + (props ? '\n' + props + '\n' + this.idt() : '') + "}");
return top ? ("(" + obj + ")") : obj;
return this.tags.front ? ("(" + obj + ")") : obj;
};
ObjectLiteral.prototype.assigns = function(name) {
var _i, _len, _ref2, prop;
@@ -1207,9 +1208,8 @@
if (this.bound) {
return ("" + (utility('bind')) + "(" + func + ", " + (this.context) + ")");
}
return top ? ("(" + func + ")") : func;
return this.tags.front ? ("(" + func + ")") : func;
};
Code.prototype.topSensitive = YES;
Code.prototype.traverseChildren = function(crossScope, func) {
return crossScope ? Code.__super__.traverseChildren.call(this, crossScope, func) : undefined;
};
@@ -1371,9 +1371,7 @@
exports.Op = (function() {
Op = (function() {
function Op(op, first, second, flip) {
if (first instanceof Value && first.base instanceof ObjectLiteral) {
first = new Parens(first);
} else if (op === 'new') {
if (op === 'new') {
if (first instanceof Call) {
return first.newInstance();
}
@@ -1430,6 +1428,9 @@
return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
};
Op.prototype.compileNode = function(o) {
if (this.second) {
this.first.tags.front = this.tags.front;
}
if (this.isChainable() && this.first.unwrap() instanceof Op && this.first.unwrap().isChainable()) {
return this.compileChain(o);
}
@@ -1960,7 +1961,7 @@
TRAILING_WHITESPACE = /[ \t]+$/gm;
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i;
SIMPLENUM = /^-?\d+$/;
SIMPLENUM = /^[+-]?\d+$/;
IS_STRING = /^['"]/;
utility = function(name) {
var ref;