mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 03:21:20 -05:00
nodes: refactored to reduce parens
This commit is contained in:
109
lib/nodes.js
109
lib/nodes.js
@@ -41,7 +41,7 @@
|
||||
Base.prototype.compileClosure = function(o) {
|
||||
o.sharedScope = o.scope;
|
||||
if (this.containsPureStatement()) {
|
||||
throw new Error('cannot include a pure statement in an expression.');
|
||||
throw SyntaxError('cannot include a pure statement in an expression.');
|
||||
}
|
||||
return Closure.wrap(this).compile(o);
|
||||
};
|
||||
@@ -109,11 +109,10 @@
|
||||
return '\n' + idt + klass + children;
|
||||
};
|
||||
Base.prototype.eachChild = function(func) {
|
||||
var _i, _j, _len, _len2, _ref2, _ref3, _result, attr, child;
|
||||
var _i, _j, _len, _len2, _ref2, _ref3, attr, child;
|
||||
if (!this.children) {
|
||||
return;
|
||||
}
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = this.children).length; _i < _len; _i++) {
|
||||
attr = _ref2[_i];
|
||||
if (this[attr]) {
|
||||
@@ -125,7 +124,7 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
return _result;
|
||||
return this;
|
||||
};
|
||||
Base.prototype.collectChildren = function() {
|
||||
var nodes;
|
||||
@@ -292,14 +291,14 @@
|
||||
return Return;
|
||||
})();
|
||||
__extends(Return, Base);
|
||||
Return.prototype.children = ['expression'];
|
||||
Return.prototype.isStatement = YES;
|
||||
Return.prototype.isPureStatement = YES;
|
||||
Return.prototype.children = ['expression'];
|
||||
Return.prototype.makeReturn = THIS;
|
||||
Return.prototype.compile = function(o) {
|
||||
var _ref2, expr;
|
||||
expr = (((_ref2 = this.expression) != null) ? _ref2.makeReturn() : undefined);
|
||||
if (expr && (!(expr instanceof Return))) {
|
||||
if (expr && !(expr instanceof Return)) {
|
||||
return expr.compile(o);
|
||||
}
|
||||
return Return.__super__.compile.call(this, o);
|
||||
@@ -319,11 +318,10 @@
|
||||
})();
|
||||
exports.Value = (function() {
|
||||
Value = (function() {
|
||||
function Value(_arg, _arg2, tag) {
|
||||
this.properties = _arg2;
|
||||
function Value(_arg, props, tag) {
|
||||
this.base = _arg;
|
||||
Value.__super__.constructor.call(this);
|
||||
this.properties || (this.properties = []);
|
||||
this.properties = props || [];
|
||||
if (tag) {
|
||||
this.tags[tag] = true;
|
||||
}
|
||||
@@ -485,11 +483,11 @@
|
||||
var method, name;
|
||||
method = o.scope.method;
|
||||
if (!method) {
|
||||
throw Error("cannot call super outside of a function.");
|
||||
throw SyntaxError('cannot call super outside of a function.');
|
||||
}
|
||||
name = method.name;
|
||||
if (!name) {
|
||||
throw Error("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");
|
||||
};
|
||||
@@ -606,9 +604,7 @@
|
||||
__extends(Extends, Base);
|
||||
Extends.prototype.children = ['child', 'parent'];
|
||||
Extends.prototype.compileNode = function(o) {
|
||||
var ref;
|
||||
ref = new Value(new Literal(utility('extends')));
|
||||
return (new Call(ref, [this.child, this.parent])).compile(o);
|
||||
return new Call(new Value(new Literal(utility('extends'))), [this.child, this.parent]).compile(o);
|
||||
};
|
||||
return Extends;
|
||||
})();
|
||||
@@ -626,10 +622,9 @@
|
||||
__extends(Accessor, Base);
|
||||
Accessor.prototype.children = ['name'];
|
||||
Accessor.prototype.compileNode = function(o) {
|
||||
var name, namePart;
|
||||
var name;
|
||||
name = this.name.compile(o);
|
||||
namePart = name.match(IS_STRING) ? ("[" + name + "]") : ("." + name);
|
||||
return this.prototype + namePart;
|
||||
return this.prototype + (IS_STRING.test(name) ? ("[" + name + "]") : ("." + name));
|
||||
};
|
||||
Accessor.prototype.isComplex = NO;
|
||||
return Accessor;
|
||||
@@ -646,10 +641,7 @@
|
||||
__extends(Index, Base);
|
||||
Index.prototype.children = ['index'];
|
||||
Index.prototype.compileNode = function(o) {
|
||||
var idx, prefix;
|
||||
idx = this.index.compile(o);
|
||||
prefix = this.proto ? '.prototype' : '';
|
||||
return "" + prefix + "[" + idx + "]";
|
||||
return "" + (this.proto ? '.prototype' : '') + "[" + (this.index.compile(o)) + "]";
|
||||
};
|
||||
Index.prototype.isComplex = function() {
|
||||
return this.index.isComplex();
|
||||
@@ -828,10 +820,9 @@
|
||||
})();
|
||||
exports.ArrayLiteral = (function() {
|
||||
ArrayLiteral = (function() {
|
||||
function ArrayLiteral(_arg) {
|
||||
this.objects = _arg;
|
||||
function ArrayLiteral(objs) {
|
||||
ArrayLiteral.__super__.constructor.call(this);
|
||||
this.objects || (this.objects = []);
|
||||
this.objects = objs || [];
|
||||
return this;
|
||||
};
|
||||
return ArrayLiteral;
|
||||
@@ -873,12 +864,11 @@
|
||||
})();
|
||||
exports.Class = (function() {
|
||||
Class = (function() {
|
||||
function Class(variable, _arg, _arg2) {
|
||||
this.properties = _arg2;
|
||||
function Class(variable, _arg, props) {
|
||||
this.parent = _arg;
|
||||
Class.__super__.constructor.call(this);
|
||||
this.variable = variable === '__temp__' ? new Literal(variable) : variable;
|
||||
this.properties || (this.properties = []);
|
||||
this.properties = props || [];
|
||||
this.returns = false;
|
||||
return this;
|
||||
};
|
||||
@@ -922,7 +912,7 @@
|
||||
func = new Code([], new Expressions([apply]));
|
||||
}
|
||||
if (func.bound) {
|
||||
throw new Error("cannot define a constructor as a bound function.");
|
||||
throw SyntaxError('cannot define a constructor as a bound function.');
|
||||
}
|
||||
func.name = className;
|
||||
func.body.push(new Return(new Literal('this')));
|
||||
@@ -955,7 +945,7 @@
|
||||
}
|
||||
props.push(prop);
|
||||
}
|
||||
constructor.className = className.match(/[\w\d\$_]+$/);
|
||||
constructor.className = className.match(/[$\w]+$/);
|
||||
if (me) {
|
||||
constructor.body.unshift(new Literal("" + me + " = this"));
|
||||
}
|
||||
@@ -1067,7 +1057,7 @@
|
||||
}
|
||||
}
|
||||
if (!(obj instanceof Value || obj instanceof Splat)) {
|
||||
throw new Error('pattern matching must use only identifiers on the left-hand side.');
|
||||
throw SyntaxError('pattern matching must use only identifiers on the left-hand side.');
|
||||
}
|
||||
accessClass = isObject && IDENTIFIER.test(idx.value) ? Accessor : Index;
|
||||
if (!splat && obj instanceof Splat) {
|
||||
@@ -1177,7 +1167,7 @@
|
||||
}
|
||||
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
|
||||
param = params[_i];
|
||||
(o.scope.parameter(param));
|
||||
o.scope.parameter(param);
|
||||
}
|
||||
comm = this.comment ? this.comment.compile(o) + '\n' : '';
|
||||
if (this.className) {
|
||||
@@ -1311,6 +1301,7 @@
|
||||
})();
|
||||
__extends(While, Base);
|
||||
While.prototype.children = ['condition', 'guard', 'body'];
|
||||
While.prototype.topSensitive = YES;
|
||||
While.prototype.isStatement = YES;
|
||||
While.prototype.addBody = function(body) {
|
||||
this.body = body;
|
||||
@@ -1320,7 +1311,6 @@
|
||||
this.returns = true;
|
||||
return this;
|
||||
};
|
||||
While.prototype.topSensitive = YES;
|
||||
While.prototype.compileNode = function(o) {
|
||||
var cond, post, pre, rvar, set, top;
|
||||
top = del(o, 'top') && !this.returns;
|
||||
@@ -1398,9 +1388,9 @@
|
||||
return (_ref2 = this.operator, __indexOf.call(this.CHAINABLE, _ref2) >= 0);
|
||||
};
|
||||
Op.prototype.invert = function() {
|
||||
var _ref2;
|
||||
if (((_ref2 = this.operator) === '===' || _ref2 === '!==')) {
|
||||
this.operator = this.INVERSIONS[this.operator];
|
||||
var op;
|
||||
if (op = this.INVERSIONS[this.operator]) {
|
||||
this.operator = op;
|
||||
return this;
|
||||
} else return this.second ? new Parens(this).invert() : Op.__super__.invert.call(this);
|
||||
};
|
||||
@@ -1425,11 +1415,9 @@
|
||||
return "" + (this.first.compile(o)) + " " + (this.operator) + " " + (this.second.compile(o));
|
||||
};
|
||||
Op.prototype.compileChain = function(o) {
|
||||
var _ref2, _ref3, first, second, shared;
|
||||
shared = this.first.unwrap().second;
|
||||
_ref2 = shared.compileReference(o), this.first.second = _ref2[0], shared = _ref2[1];
|
||||
_ref3 = [this.first.compile(o), this.second.compile(o), shared.compile(o)], first = _ref3[0], second = _ref3[1], shared = _ref3[2];
|
||||
return "(" + first + ") && (" + shared + " " + (this.operator) + " " + second + ")";
|
||||
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));
|
||||
};
|
||||
Op.prototype.compileExistence = function(o) {
|
||||
var fst, ref;
|
||||
@@ -1580,13 +1568,13 @@
|
||||
})();
|
||||
__extends(Parens, Base);
|
||||
Parens.prototype.children = ['expression'];
|
||||
Parens.prototype.topSensitive = YES;
|
||||
Parens.prototype.isStatement = function(o) {
|
||||
return this.expression.isStatement(o);
|
||||
};
|
||||
Parens.prototype.isComplex = function() {
|
||||
return this.expression.isComplex();
|
||||
};
|
||||
Parens.prototype.topSensitive = YES;
|
||||
Parens.prototype.makeReturn = function() {
|
||||
return this.expression.makeReturn();
|
||||
};
|
||||
@@ -1619,10 +1607,10 @@
|
||||
if (this.object) {
|
||||
_ref2 = [this.index, this.name], this.name = _ref2[0], this.index = _ref2[1];
|
||||
}
|
||||
this.pattern = this.name instanceof Value;
|
||||
if (this.index instanceof Value) {
|
||||
throw new Error('index cannot be a pattern matching expression');
|
||||
throw SyntaxError('index cannot be a pattern matching expression');
|
||||
}
|
||||
this.pattern = this.name instanceof Value;
|
||||
this.returns = false;
|
||||
return this;
|
||||
};
|
||||
@@ -1630,8 +1618,8 @@
|
||||
})();
|
||||
__extends(For, Base);
|
||||
For.prototype.children = ['body', 'source', 'guard'];
|
||||
For.prototype.isStatement = YES;
|
||||
For.prototype.topSensitive = YES;
|
||||
For.prototype.isStatement = YES;
|
||||
For.prototype.makeReturn = function() {
|
||||
this.returns = true;
|
||||
return this;
|
||||
@@ -1771,8 +1759,6 @@
|
||||
this.cases = _arg2;
|
||||
this.subject = _arg;
|
||||
Switch.__super__.constructor.call(this);
|
||||
this.tags.subjectless = !this.subject;
|
||||
this.subject || (this.subject = new Literal('true'));
|
||||
return this;
|
||||
};
|
||||
return Switch;
|
||||
@@ -1792,28 +1778,27 @@
|
||||
return this;
|
||||
};
|
||||
Switch.prototype.compileNode = function(o) {
|
||||
var _i, _j, _len, _len2, _ref2, _ref3, block, code, condition, conditions, exprs, idt, pair;
|
||||
idt = (o.indent = this.idt(2));
|
||||
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));
|
||||
o.top = true;
|
||||
code = ("" + (this.tab) + "switch (" + (this.subject.compile(o)) + ") {");
|
||||
for (_i = 0, _len = (_ref2 = this.cases).length; _i < _len; _i++) {
|
||||
pair = _ref2[_i];
|
||||
conditions = pair[0], block = pair[1];
|
||||
exprs = block.expressions;
|
||||
for (_j = 0, _len2 = (_ref3 = flatten([conditions])).length; _j < _len2; _j++) {
|
||||
condition = _ref3[_j];
|
||||
if (this.tags.subjectless) {
|
||||
condition = new Op('!!', new Parens(condition));
|
||||
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++) {
|
||||
condition = _ref5[_j];
|
||||
if (!this.subject) {
|
||||
condition = condition.invert().invert();
|
||||
}
|
||||
code += ("\n" + (this.idt(1)) + "case " + (condition.compile(o)) + ":");
|
||||
code += ("\n" + idt1 + "case " + (condition.compile(o)) + ":");
|
||||
}
|
||||
code += ("\n" + (block.compile(o)));
|
||||
if (!(last(exprs) instanceof Return)) {
|
||||
code += ("\n" + idt + "break;");
|
||||
if (!(last(block.expressions) instanceof Return)) {
|
||||
code += ("\n" + idt2 + "break;");
|
||||
}
|
||||
}
|
||||
if (this.otherwise) {
|
||||
code += ("\n" + (this.idt(1)) + "default:\n" + (this.otherwise.compile(o)));
|
||||
code += ("\n" + idt1 + "default:\n" + (this.otherwise.compile(o)));
|
||||
}
|
||||
code += ("\n" + (this.tab) + "}");
|
||||
return code;
|
||||
@@ -1965,7 +1950,7 @@
|
||||
TAB = ' ';
|
||||
TRAILING_WHITESPACE = /[ \t]+$/gm;
|
||||
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
|
||||
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i;
|
||||
NUMBER = /^0x[\da-f]+$|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i;
|
||||
SIMPLENUM = /^[+-]?\d+$/;
|
||||
IS_STRING = /^['"]/;
|
||||
utility = function(name) {
|
||||
|
||||
Reference in New Issue
Block a user