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

Issue #609. Slightly faster implementation of ||= and &&=

This commit is contained in:
Jeremy Ashkenas 2010-08-14 18:02:07 -04:00
parent f496cc229b
commit 975b82f09b
7 changed files with 30 additions and 30 deletions

View file

@ -20,7 +20,7 @@
exports.VERSION = '0.9.1'; exports.VERSION = '0.9.1';
lexer = new Lexer(); lexer = new Lexer();
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) {

View file

@ -220,7 +220,7 @@
var o; var o;
optionParser = new optparse.OptionParser(SWITCHES, BANNER); optionParser = new optparse.OptionParser(SWITCHES, BANNER);
o = (options = optionParser.parse(process.argv.slice(2, process.argv.length))); o = (options = optionParser.parse(process.argv.slice(2, process.argv.length)));
options.compile = options.compile || (!!o.output); options.compile || (options.compile = (!!o.output));
options.run = !(o.compile || o.print || o.lint); options.run = !(o.compile || o.print || o.lint);
options.print = !!(o.print || (o.eval || o.stdio && o.compile)); options.print = !!(o.print || (o.eval || o.stdio && o.compile));
return (sources = options.arguments); return (sources = options.arguments);

View file

@ -324,7 +324,7 @@
if (value && value.match(CODE)) { if (value && value.match(CODE)) {
this.tagParameters(); this.tagParameters();
} }
value = value || this.chunk.substr(0, 1); value || (value = this.chunk.substr(0, 1));
this.i += value.length; this.i += value.length;
spaced = this.prev() && this.prev().spaced; spaced = this.prev() && this.prev().spaced;
tag = value; tag = value;
@ -435,7 +435,7 @@
}; };
Lexer.prototype.balancedString = function(str, delimited, options) { Lexer.prototype.balancedString = function(str, delimited, options) {
var _d, _e, _f, _g, close, i, levels, open, pair, slash; var _d, _e, _f, _g, close, i, levels, open, pair, slash;
options = options || {}; options || (options = {});
slash = delimited[0][0] === '/'; slash = delimited[0][0] === '/';
levels = []; levels = [];
i = 0; i = 0;
@ -478,7 +478,7 @@
}; };
Lexer.prototype.interpolateString = function(str, options) { Lexer.prototype.interpolateString = function(str, options) {
var _d, _e, _f, _g, _h, _i, escaped, expr, i, idx, inner, interpolated, lexer, nested, pi, quote, tag, tok, token, tokens, value; var _d, _e, _f, _g, _h, _i, escaped, expr, i, idx, inner, interpolated, lexer, nested, pi, quote, tag, tok, token, tokens, value;
options = options || {}; options || (options = {});
if (str.length < 3 || !starts(str, '"')) { if (str.length < 3 || !starts(str, '"')) {
return this.token('STRING', str); return this.token('STRING', str);
} else { } else {

View file

@ -48,7 +48,7 @@
}; };
BaseNode.prototype.compileReference = function(o, options) { BaseNode.prototype.compileReference = function(o, options) {
var compiled, pair, reference; var compiled, pair, reference;
options = options || {}; options || (options = {});
pair = (function() { pair = (function() {
if (!((this instanceof CallNode || this.contains(function(n) { if (!((this instanceof CallNode || this.contains(function(n) {
return n instanceof CallNode; return n instanceof CallNode;
@ -105,7 +105,7 @@
}; };
BaseNode.prototype.toString = function(idt, override) { BaseNode.prototype.toString = function(idt, override) {
var _b, _c, _d, _e, child, children; var _b, _c, _d, _e, child, children;
idt = idt || ''; idt || (idt = '');
children = (function() { children = (function() {
_b = []; _d = this.collectChildren(); _b = []; _d = this.collectChildren();
for (_c = 0, _e = _d.length; _c < _e; _c++) { for (_c = 0, _e = _d.length; _c < _e; _c++) {
@ -207,7 +207,7 @@
return this; return this;
}; };
Expressions.prototype.compile = function(o) { Expressions.prototype.compile = function(o) {
o = o || {}; o || (o = {});
return o.scope ? Expressions.__superClass__.compile.call(this, o) : this.compileRoot(o); return o.scope ? Expressions.__superClass__.compile.call(this, o) : this.compileRoot(o);
}; };
Expressions.prototype.compileNode = function(o) { Expressions.prototype.compileNode = function(o) {
@ -318,7 +318,7 @@
ValueNode = function(_b, _c) { ValueNode = function(_b, _c) {
this.properties = _c; this.properties = _c;
this.base = _b; this.base = _b;
this.properties = this.properties || []; this.properties || (this.properties = []);
return this; return this;
}; };
__extends(ValueNode, BaseNode); __extends(ValueNode, BaseNode);
@ -382,7 +382,7 @@
only = del(o, 'onlyFirst'); only = del(o, 'onlyFirst');
op = del(o, 'operation'); op = del(o, 'operation');
props = only ? this.properties.slice(0, this.properties.length - 1) : this.properties; props = only ? this.properties.slice(0, this.properties.length - 1) : this.properties;
o.chainRoot = o.chainRoot || this; o.chainRoot || (o.chainRoot = this);
if (this.parenthetical && !props.length) { if (this.parenthetical && !props.length) {
this.base.parenthetical = true; this.base.parenthetical = true;
} }
@ -445,7 +445,7 @@
this.isNew = false; this.isNew = false;
this.isSuper = variable === 'super'; this.isSuper = variable === 'super';
this.variable = this.isSuper ? null : variable; this.variable = this.isSuper ? null : variable;
this.args = this.args || []; this.args || (this.args = []);
this.compileSplatArguments = function(o) { this.compileSplatArguments = function(o) {
return SplatNode.compileSplattedArray.call(this, this.args, o); return SplatNode.compileSplattedArray.call(this, this.args, o);
}; };
@ -552,7 +552,7 @@
AccessorNode.prototype.compileNode = function(o) { AccessorNode.prototype.compileNode = function(o) {
var name, namePart; var name, namePart;
name = this.name.compile(o); name = this.name.compile(o);
o.chainRoot.wrapped = o.chainRoot.wrapped || this.soakNode; o.chainRoot.wrapped || (o.chainRoot.wrapped = this.soakNode);
namePart = name.match(IS_STRING) ? ("[" + (name) + "]") : ("." + (name)); namePart = name.match(IS_STRING) ? ("[" + (name) + "]") : ("." + (name));
return this.prototype + namePart; return this.prototype + namePart;
}; };
@ -568,7 +568,7 @@
IndexNode.prototype.children = ['index']; IndexNode.prototype.children = ['index'];
IndexNode.prototype.compileNode = function(o) { IndexNode.prototype.compileNode = function(o) {
var idx, prefix; var idx, prefix;
o.chainRoot.wrapped = o.chainRoot.wrapped || this.soakNode; o.chainRoot.wrapped || (o.chainRoot.wrapped = this.soakNode);
idx = this.index.compile(o); idx = this.index.compile(o);
prefix = this.proto ? '.prototype' : ''; prefix = this.proto ? '.prototype' : '';
return "" + (prefix) + "[" + (idx) + "]"; return "" + (prefix) + "[" + (idx) + "]";
@ -637,7 +637,7 @@
to = _b[1]; to = _b[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) + "--")));
}; };
RangeNode.prototype.compileArray = function(o) { RangeNode.prototype.compileArray = function(o) {
@ -745,7 +745,7 @@
exports.ArrayNode = (function() { exports.ArrayNode = (function() {
ArrayNode = function(_b) { ArrayNode = function(_b) {
this.objects = _b; this.objects = _b;
this.objects = this.objects || []; this.objects || (this.objects = []);
this.compileSplatLiteral = function(o) { this.compileSplatLiteral = function(o) {
return SplatNode.compileSplattedArray.call(this, this.objects, o); return SplatNode.compileSplattedArray.call(this, this.objects, o);
}; };
@ -782,7 +782,7 @@
this.properties = _d; this.properties = _d;
this.parent = _c; this.parent = _c;
this.variable = _b; this.variable = _b;
this.properties = this.properties || []; this.properties || (this.properties = []);
this.returns = false; this.returns = false;
return this; return this;
}; };
@ -832,8 +832,8 @@
} }
if (func instanceof CodeNode && func.bound) { if (func instanceof CodeNode && func.bound) {
func.bound = false; func.bound = false;
constScope = constScope || new Scope(o.scope, constructor.body, constructor); constScope || (constScope = new Scope(o.scope, constructor.body, constructor));
me = me || constScope.freeVariable(); me || (me = constScope.freeVariable());
pname = pvar.compile(o); pname = pvar.compile(o);
if (constructor.body.empty()) { if (constructor.body.empty()) {
constructor.body.push(new ReturnNode(literal('this'))); constructor.body.push(new ReturnNode(literal('this')));
@ -982,8 +982,8 @@
CodeNode = function(_b, _c, tag) { CodeNode = function(_b, _c, tag) {
this.body = _c; this.body = _c;
this.params = _b; this.params = _b;
this.params = this.params || []; this.params || (this.params = []);
this.body = this.body || new Expressions(); this.body || (this.body = new Expressions());
this.bound = tag === 'boundfunc'; this.bound = tag === 'boundfunc';
return this; return this;
}; };
@ -1064,7 +1064,7 @@
}; };
CodeNode.prototype.toString = function(idt) { CodeNode.prototype.toString = function(idt) {
var _b, _c, _d, _e, child, children; var _b, _c, _d, _e, child, children;
idt = idt || ''; idt || (idt = '');
children = (function() { children = (function() {
_b = []; _d = this.collectChildren(); _b = []; _d = this.collectChildren();
for (_c = 0, _e = _d.length; _c < _e; _c++) { for (_c = 0, _e = _d.length; _c < _e; _c++) {
@ -1314,7 +1314,7 @@
if (this.operator === '?=') { if (this.operator === '?=') {
return ("" + (first) + " = " + (ExistenceNode.compileTest(o, literal(firstVar))[0]) + " ? " + (firstVar) + " : " + (second)); return ("" + (first) + " = " + (ExistenceNode.compileTest(o, literal(firstVar))[0]) + " ? " + (firstVar) + " : " + (second));
} }
return "" + (first) + " = " + (firstVar) + " " + (this.operator.substr(0, 2)) + " " + (second); return "" + (first) + " " + (this.operator.substr(0, 2)) + " (" + (firstVar) + " = " + (second) + ")";
}; };
OpNode.prototype.compileExistence = function(o) { OpNode.prototype.compileExistence = function(o) {
var _b, ref, test; var _b, ref, test;
@ -1495,7 +1495,7 @@
this.index = _d; this.index = _d;
this.name = _c; this.name = _c;
this.body = _b; this.body = _b;
this.index = this.index || null; this.index || (this.index = null);
this.source = source.source; this.source = source.source;
this.guard = source.guard; this.guard = source.guard;
this.step = source.step; this.step = source.step;
@ -1635,7 +1635,7 @@
this.tags = _d; this.tags = _d;
this.body = _c; this.body = _c;
this.condition = _b; this.condition = _b;
this.tags = this.tags || {}; this.tags || (this.tags = {});
if (this.tags.invert) { if (this.tags.invert) {
this.condition = new OpNode('!', new ParentheticalNode(this.condition)); this.condition = new OpNode('!', new ParentheticalNode(this.condition));
} }
@ -1700,7 +1700,7 @@
return this; return this;
}; };
IfNode.prototype.isStatement = function(o) { IfNode.prototype.isStatement = function(o) {
return this.statement = this.statement || (!!((o && o.top) || this.tags.statement || this.bodyNode().isStatement(o) || (this.elseBody && this.elseBodyNode().isStatement(o)))); return this.statement || (this.statement = (!!((o && o.top) || this.tags.statement || this.bodyNode().isStatement(o) || (this.elseBody && this.elseBodyNode().isStatement(o)))));
}; };
IfNode.prototype.compileCondition = function(o) { IfNode.prototype.compileCondition = function(o) {
var _b, _c, _d, _e, cond, conditions; var _b, _c, _d, _e, cond, conditions;
@ -1722,8 +1722,8 @@
}; };
IfNode.prototype.makeReturn = function() { IfNode.prototype.makeReturn = function() {
if (this.isStatement()) { if (this.isStatement()) {
this.body = this.body && this.ensureExpressions(this.body.makeReturn()); this.body && (this.body = this.ensureExpressions(this.body.makeReturn()));
this.elseBody = this.elseBody && this.ensureExpressions(this.elseBody.makeReturn()); this.elseBody && (this.elseBody = this.ensureExpressions(this.elseBody.makeReturn()));
return this; return this;
} else { } else {
return new ReturnNode(this); return new ReturnNode(this);

View file

@ -83,7 +83,7 @@
var match; var match;
match = longFlag.match(OPTIONAL); match = longFlag.match(OPTIONAL);
longFlag = longFlag.match(LONG_FLAG)[1]; longFlag = longFlag.match(LONG_FLAG)[1];
options = options || {}; options || (options = {});
return { return {
name: longFlag.substr(2), name: longFlag.substr(2),
shortFlag: shortFlag, shortFlag: shortFlag,

View file

@ -264,7 +264,7 @@
_f = pair; _f = pair;
open = _f[0]; open = _f[0];
close = _f[1]; close = _f[1];
levels[open] = levels[open] || 0; levels[open] || (levels[open] = 0);
if (token[0] === open) { if (token[0] === open) {
if (levels[open] === 0) { if (levels[open] === 0) {
openLine[open] = token[2]; openLine[open] = token[2];

View file

@ -1121,7 +1121,7 @@ exports.OpNode = class OpNode extends BaseNode
second = "(#{second})" if @second instanceof OpNode second = "(#{second})" if @second instanceof OpNode
o.scope.find(first) if first.match(IDENTIFIER) o.scope.find(first) if first.match(IDENTIFIER)
return "#{first} = #{ ExistenceNode.compileTest(o, literal(firstVar))[0] } ? #{firstVar} : #{second}" if @operator is '?=' return "#{first} = #{ ExistenceNode.compileTest(o, literal(firstVar))[0] } ? #{firstVar} : #{second}" if @operator is '?='
"#{first} = #{firstVar} #{ @operator.substr(0, 2) } #{second}" "#{first} #{ @operator.substr(0, 2) } (#{firstVar} = #{second})"
# If this is an existence operator, we delegate to `ExistenceNode.compileTest` # If this is an existence operator, we delegate to `ExistenceNode.compileTest`
# to give us the safe references for the variables. # to give us the safe references for the variables.