From 326656245a59a650365be8bdd2c85135920b4172 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 29 Mar 2010 21:49:20 -0400 Subject: [PATCH] using the new static properties of class definitions in the CoffeeScript compiler -- it's hardly used. --- lib/lexer.js | 6 ++-- lib/nodes.js | 90 ++++++++++++++++++++++++------------------------ src/lexer.coffee | 7 ++-- src/nodes.coffee | 61 ++++++++++++++++---------------- 4 files changed, 83 insertions(+), 81 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index da12f7c6..e135a6c7 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -589,10 +589,12 @@ prev = this.prev(2); return this.value() && this.value().match && this.value().match(NO_NEWLINE) && prev && (prev[0] !== '.') && !this.value().match(CODE); }; + // Lexer Properties + // ---------------- + // There are no exensions to the core lexer by default. + Lexer.extensions = []; return Lexer; }).call(this); - // There are no exensions to the core lexer by default. - Lexer.extensions = []; // Constants // --------- // Keywords that CoffeeScript shares in common with JavaScript. diff --git a/lib/nodes.js b/lib/nodes.js index 5a62b892..5fc7066c 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1144,35 +1144,35 @@ return "Array.prototype.slice.call(" + name + ", " + index + ")"; } }; + // Utility function that converts arbitrary number of elements, mixed with + // splats, to a proper array + SplatNode.compile_mixed_array = function compile_mixed_array(list, o) { + var _a, _b, _c, arg, args, code, i, prev; + args = []; + i = 0; + _b = list; + for (_a = 0, _c = _b.length; _a < _c; _a++) { + arg = _b[_a]; + code = arg.compile(o); + if (!(arg instanceof SplatNode)) { + prev = args[i - 1]; + if (i === 1 && prev.substr(0, 1) === '[' && prev.substr(prev.length - 1, 1) === ']') { + args[i - 1] = "" + (prev.substr(0, prev.length - 1)) + ", " + code + "]"; + continue; + } else if (i > 1 && prev.substr(0, 9) === '.concat([' && prev.substr(prev.length - 2, 2) === '])') { + args[i - 1] = "" + (prev.substr(0, prev.length - 2)) + ", " + code + "])"; + continue; + } else { + code = "[" + code + "]"; + } + } + args.push(i === 0 ? code : ".concat(" + code + ")"); + i += 1; + } + return args.join(''); + }; return SplatNode; }).call(this); - // Utility function that converts arbitrary number of elements, mixed with - // splats, to a proper array - SplatNode.compile_mixed_array = function compile_mixed_array(list, o) { - var _a, _b, _c, arg, args, code, i, prev; - args = []; - i = 0; - _b = list; - for (_a = 0, _c = _b.length; _a < _c; _a++) { - arg = _b[_a]; - code = arg.compile(o); - if (!(arg instanceof SplatNode)) { - prev = args[i - 1]; - if (i === 1 && prev.substr(0, 1) === '[' && prev.substr(prev.length - 1, 1) === ']') { - args[i - 1] = "" + (prev.substr(0, prev.length - 1)) + ", " + code + "]"; - continue; - } else if (i > 1 && prev.substr(0, 9) === '.concat([' && prev.substr(prev.length - 2, 2) === '])') { - args[i - 1] = "" + (prev.substr(0, prev.length - 2)) + ", " + code + "])"; - continue; - } else { - code = "[" + code + "]"; - } - } - args.push(i === 0 ? code : ".concat(" + code + ")"); - i += 1; - } - return args.join(''); - }; //### WhileNode // A while loop, the only sort of low-level loop exposed by CoffeeScript. From // it, all other loops can be manufactured. Useful in cases where you need more @@ -1399,26 +1399,26 @@ ExistenceNode.prototype.compile_node = function compile_node(o) { return ExistenceNode.compile_test(o, this.expression); }; + // The meat of the **ExistenceNode** is in this static `compile_test` method + // because other nodes like to check the existence of their variables as well. + // Be careful not to double-evaluate anything. + ExistenceNode.compile_test = function compile_test(o, variable) { + var _a, _b, _c, first, second; + _a = [variable, variable]; + first = _a[0]; + second = _a[1]; + if (variable instanceof CallNode || (variable instanceof ValueNode && variable.has_properties())) { + _b = variable.compile_reference(o); + first = _b[0]; + second = _b[1]; + } + _c = [first.compile(o), second.compile(o)]; + first = _c[0]; + second = _c[1]; + return "(typeof " + first + " !== \"undefined\" && " + second + " !== null)"; + }; return ExistenceNode; }).call(this); - // The meat of the **ExistenceNode** is in this static `compile_test` method - // because other nodes like to check the existence of their variables as well. - // Be careful not to double-evaluate anything. - ExistenceNode.compile_test = function compile_test(o, variable) { - var _a, _b, _c, first, second; - _a = [variable, variable]; - first = _a[0]; - second = _a[1]; - if (variable instanceof CallNode || (variable instanceof ValueNode && variable.has_properties())) { - _b = variable.compile_reference(o); - first = _b[0]; - second = _b[1]; - } - _c = [first.compile(o), second.compile(o)]; - first = _c[0]; - second = _c[1]; - return "(typeof " + first + " !== \"undefined\" && " + second + " !== null)"; - }; //### ParentheticalNode // An extra set of parentheses, specified explicitly in the source. At one time // we tried to clean up the results by detecting and removing redundant diff --git a/src/lexer.coffee b/src/lexer.coffee index 57bc855c..3df8023e 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -423,8 +423,11 @@ exports.Lexer: class Lexer @value() and @value().match and @value().match(NO_NEWLINE) and prev and (prev[0] isnt '.') and not @value().match(CODE) -# There are no exensions to the core lexer by default. -Lexer.extensions: [] + # Lexer Properties + # ---------------- + + # There are no exensions to the core lexer by default. + @extensions: [] # Constants # --------- diff --git a/src/nodes.coffee b/src/nodes.coffee index eb25b1dc..841da0b6 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -400,7 +400,6 @@ exports.CallNode: class CallNode extends BaseNode meth: "($temp = ${ @variable.source })${ @variable.last }" "${@prefix()}${meth}.apply($obj, ${ @compile_splat_arguments(o) })" - #### CurryNode # Binds a context object and a list of arguments to a function, @@ -426,8 +425,6 @@ exports.CurryNode: class CurryNode extends CallNode curry: new CodeNode([literal('func'), literal('obj'), literal('args')], Expressions.wrap([curried])) (new ParentheticalNode(new CallNode(curry, [@meth, @context, literal(@arguments(o))]))).compile o - - #### ExtendsNode # Node to extend an object's prototype with an ancestor object. @@ -846,26 +843,26 @@ exports.SplatNode: class SplatNode extends BaseNode if trailings? then "Array.prototype.slice.call($name, $index, ${name}.length - $trailings)" \ else "Array.prototype.slice.call($name, $index)" -# Utility function that converts arbitrary number of elements, mixed with -# splats, to a proper array -SplatNode.compile_mixed_array: (list, o) -> - args: [] - i: 0 - for arg in list - code: arg.compile o - if not (arg instanceof SplatNode) - prev: args[i - 1] - if i is 1 and prev.substr(0, 1) is '[' and prev.substr(prev.length - 1, 1) is ']' - args[i - 1]: "${prev.substr(0, prev.length - 1)}, $code]" - continue - else if i > 1 and prev.substr(0, 9) is '.concat([' and prev.substr(prev.length - 2, 2) is '])' - args[i - 1]: "${prev.substr(0, prev.length - 2)}, $code])" - continue - else - code: "[$code]" - args.push(if i is 0 then code else ".concat($code)") - i: + 1 - args.join('') + # Utility function that converts arbitrary number of elements, mixed with + # splats, to a proper array + @compile_mixed_array: (list, o) -> + args: [] + i: 0 + for arg in list + code: arg.compile o + if not (arg instanceof SplatNode) + prev: args[i - 1] + if i is 1 and prev.substr(0, 1) is '[' and prev.substr(prev.length - 1, 1) is ']' + args[i - 1]: "${prev.substr(0, prev.length - 1)}, $code]" + continue + else if i > 1 and prev.substr(0, 9) is '.concat([' and prev.substr(prev.length - 2, 2) is '])' + args[i - 1]: "${prev.substr(0, prev.length - 2)}, $code])" + continue + else + code: "[$code]" + args.push(if i is 0 then code else ".concat($code)") + i: + 1 + args.join('') #### WhileNode @@ -1052,15 +1049,15 @@ exports.ExistenceNode: class ExistenceNode extends BaseNode compile_node: (o) -> ExistenceNode.compile_test(o, @expression) -# The meat of the **ExistenceNode** is in this static `compile_test` method -# because other nodes like to check the existence of their variables as well. -# Be careful not to double-evaluate anything. -ExistenceNode.compile_test: (o, variable) -> - [first, second]: [variable, variable] - if variable instanceof CallNode or (variable instanceof ValueNode and variable.has_properties()) - [first, second]: variable.compile_reference(o) - [first, second]: [first.compile(o), second.compile(o)] - "(typeof $first !== \"undefined\" && $second !== null)" + # The meat of the **ExistenceNode** is in this static `compile_test` method + # because other nodes like to check the existence of their variables as well. + # Be careful not to double-evaluate anything. + @compile_test: (o, variable) -> + [first, second]: [variable, variable] + if variable instanceof CallNode or (variable instanceof ValueNode and variable.has_properties()) + [first, second]: variable.compile_reference(o) + [first, second]: [first.compile(o), second.compile(o)] + "(typeof $first !== \"undefined\" && $second !== null)" #### ParentheticalNode