Optimizing default arguments and existential assignment.

This commit is contained in:
Jeremy Ashkenas 2010-11-01 22:01:52 -04:00
parent 749e056618
commit 83e6955dce
6 changed files with 27 additions and 14 deletions

View File

@ -6,7 +6,7 @@
return eval(CoffeeScript.compile(code, options));
};
CoffeeScript.run = function(code, options) {
options != null ? options : options = {};
options != null || (options = {});
options.bare = true;
return Function(CoffeeScript.compile(code, options))();
};

View File

@ -18,7 +18,7 @@
exports.VERSION = '0.9.4';
exports.helpers = require('./helpers');
exports.compile = compile = function(code, options) {
options != null ? options : options = {};
options != null || (options = {});
try {
return (parser.parse(lexer.tokenize(code))).compile(options);
} catch (err) {

View File

@ -128,7 +128,7 @@
return new Param($1);
}), o('ParamVar ...', function() {
return new Param($1, null, true);
}), o('ParamVar = Expression', function() {
}), o('ParamVar = Expression', function() {
return new Param($1, $3);
})
],

View File

@ -438,7 +438,7 @@
};
Lexer.prototype.balancedString = function(str, delimited, options) {
var _i, _len, close, i, levels, open, pair, slen;
options != null ? options : options = {};
options != null || (options = {});
levels = [];
i = 0;
slen = str.length;
@ -476,7 +476,7 @@
};
Lexer.prototype.interpolateString = function(str, options) {
var _len, _ref2, _this, expr, heredoc, i, inner, interpolated, letter, nested, pi, regex, tag, tokens, value;
options != null ? options : options = {};
options != null || (options = {});
heredoc = options.heredoc, regex = options.regex;
tokens = [];
pi = 0;

View File

@ -96,7 +96,7 @@
};
Base.prototype.toString = function(idt, override) {
var _i, _len, _ref2, _result, child, children, klass;
idt != null ? idt : idt = '';
idt != null || (idt = '');
children = ((function() {
_ref2 = this.collectChildren();
_result = [];
@ -209,7 +209,7 @@
return this;
};
Expressions.prototype.compile = function(o, level) {
o != null ? o : o = {};
o != null || (o = {});
return o.scope ? Expressions.__super__.compile.call(this, o, level) : this.compileRoot(o);
};
Expressions.prototype.compileNode = function(o) {
@ -1069,7 +1069,7 @@
} else {
ref = param;
if (param.value) {
exprs.push(new Assign(new Value(param.name), param.value, '?='));
exprs.push(new Op('||', new Literal("" + param.name.value + " != null"), new Assign(param.name, param.value)));
}
}
if (!splats) {
@ -1320,15 +1320,20 @@
return o.level < LEVEL_OP ? code : "(" + code + ")";
};
Op.prototype.compileExistence = function(o) {
var fst, ref;
if (this.first.isComplex()) {
var end, fst, ref;
if (this.first.isComplex() && o.level > LEVEL_TOP) {
ref = o.scope.freeVariable('ref');
fst = new Parens(new Assign(new Literal(ref), this.first));
} else {
fst = this.first;
ref = fst.compile(o);
}
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o, LEVEL_LIST)));
if (o.level === LEVEL_TOP) {
end = " || " + (this.second.compile(o, LEVEL_OP));
} else {
end = " ? " + ref + " : " + (this.second.compile(o, LEVEL_LIST));
}
return new Existence(fst).compile(o) + end;
};
Op.prototype.compileUnary = function(o) {
var op, parts;

View File

@ -882,7 +882,11 @@ exports.Code = class Code extends Base
if param.value then new Op '?', ref, param.value else ref
else
ref = param
exprs.push new Assign new Value(param.name), param.value, '?=' if param.value
if param.value
exprs.push new Op('||',
new Literal("#{param.name.value} != null"),
new Assign(param.name, param.value)
)
vars.push ref unless splats
scope.startLevel()
wasEmpty = @body.isEmpty()
@ -1084,13 +1088,17 @@ exports.Op = class Op extends Base
if o.level < LEVEL_OP then code else "(#{code})"
compileExistence: (o) ->
if @first.isComplex()
if @first.isComplex() and o.level > LEVEL_TOP
ref = o.scope.freeVariable 'ref'
fst = new Parens new Assign new Literal(ref), @first
else
fst = @first
ref = fst.compile o
new Existence(fst).compile(o) + " ? #{ref} : #{ @second.compile o, LEVEL_LIST }"
if o.level is LEVEL_TOP
end = " || #{@second.compile o, LEVEL_OP}"
else
end = " ? #{ref} : #{@second.compile o, LEVEL_LIST}"
new Existence(fst).compile(o) + end
# Compile a unary **Op**.
compileUnary: (o) ->