From 83e6955dcef0ce7446c0162626300a796e86b9e0 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 1 Nov 2010 22:01:52 -0400 Subject: [PATCH] Optimizing default arguments and existential assignment. --- lib/browser.js | 2 +- lib/coffee-script.js | 2 +- lib/grammar.js | 2 +- lib/lexer.js | 4 ++-- lib/nodes.js | 17 +++++++++++------ src/nodes.coffee | 14 +++++++++++--- 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/browser.js b/lib/browser.js index 84a6a76e..4cfc8250 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -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))(); }; diff --git a/lib/coffee-script.js b/lib/coffee-script.js index f84f3cf3..c2c30f8f 100755 --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -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) { diff --git a/lib/grammar.js b/lib/grammar.js index d4d0154f..60f82a63 100644 --- a/lib/grammar.js +++ b/lib/grammar.js @@ -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); }) ], diff --git a/lib/lexer.js b/lib/lexer.js index 99872e1d..7304abea 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -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; diff --git a/lib/nodes.js b/lib/nodes.js index 8a83f2cb..cb0e87e3 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -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; diff --git a/src/nodes.coffee b/src/nodes.coffee index 318cdf17..a939faa5 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -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) ->