From 713f6f32e1e0c044272d89711f1a98fe4fd39576 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 11 Feb 2010 02:39:57 -0500 Subject: [PATCH] done for now --- lib/coffee_script/coffee-script.js | 9 ++-- lib/coffee_script/command_line.js | 33 ++++++------ lib/coffee_script/nodes.js | 10 ++-- src/coffee-script.coffee | 8 +-- src/command_line.coffee | 10 ++-- src/nodes.coffee | 51 ++----------------- .../execution/test_expressions.coffee | 34 ++++++------- 7 files changed, 59 insertions(+), 96 deletions(-) diff --git a/lib/coffee_script/coffee-script.js b/lib/coffee_script/coffee-script.js index eade55a6..746930dd 100644 --- a/lib/coffee_script/coffee-script.js +++ b/lib/coffee_script/coffee-script.js @@ -27,10 +27,11 @@ exports.VERSION = '0.5.0'; // Compile CoffeeScript to JavaScript, using the Coffee/Jison compiler. exports.compile = function compile(code) { - var nodes, tokens; - tokens = lexer.tokenize(code); - nodes = parser.parse(tokens); - return nodes.compile(); + return (parser.parse(lexer.tokenize(code))).compile(); + }; + // Just the tokens. + exports.tokenize = function tokenize(code) { + return lexer.tokenize(code); }; //---------- Below this line is obsolete, for the Ruby compiler. ---------------- // Executes the `coffee` Ruby program to convert from CoffeeScript to JavaScript. diff --git a/lib/coffee_script/command_line.js b/lib/coffee_script/command_line.js index d7bbcd23..4292ec07 100644 --- a/lib/coffee_script/command_line.js +++ b/lib/coffee_script/command_line.js @@ -47,26 +47,25 @@ // Compiles the source CoffeeScript, returning the desired JavaScript, tokens, // or JSLint results. exports.compile_scripts = function compile_scripts() { - var source; + var opts, source; if (!((source = this.sources.shift()))) { return null; } - return posix.cat(source).addCallback((function(__this) { - var __func = function(code) { - var js; - js = coffee.compile(code); - if (this.options.run) { - return eval(js); - } - if (this.options.print) { - return puts(js); - } - return exports.compile_scripts(); - }; - return (function() { - return __func.apply(__this, arguments); - }); - })(this)); + opts = this.options; + return posix.cat(source).addCallback(function(code) { + var js; + if (opts.tokens) { + return puts(coffee.tokenize(code).join(' ')); + } + js = coffee.compile(code); + if (opts.run) { + return eval(js); + } + if (opts.print) { + return puts(js); + } + return exports.compile_scripts(); + }); }; // Use OptionParser for all the options. exports.parse_options = function parse_options() { diff --git a/lib/coffee_script/nodes.js b/lib/coffee_script/nodes.js index 7048f59b..275dd044 100644 --- a/lib/coffee_script/nodes.js +++ b/lib/coffee_script/nodes.js @@ -147,8 +147,8 @@ this.options = dup(o || { }); this.indent = o.indent; - top = this.top_sensitive() ? o.top : del(o, 'top'); - closure = this.is_statement() && !this.is_statement_only() && !top && !o.returns && !this instanceof CommentNode && !this.contains(function(node) { + top = this.top_sensitive() ? this.options.top : del(this.options, 'top'); + closure = this.is_statement() && !this.is_statement_only() && !top && !this.options.returns && !(this instanceof CommentNode) && !this.contains(function(node) { return node.is_statement_only(); }); return closure ? this.compile_closure(this.options) : this.compile_node(this.options); @@ -202,7 +202,7 @@ // A collection of nodes, each one representing an expression. Expressions = (exports.Expressions = inherit(Node, { constructor: function constructor(nodes) { - this.children = (this.expressions = flatten(nodes)); + this.children = (this.expressions = compact(flatten(nodes))); return this; }, // Tack an expression on to the end of this expression list. @@ -1235,14 +1235,14 @@ com_dent = child ? this.idt() : ''; prefix = this.comment ? this.comment.compile(cond_o) + '\n' + com_dent : ''; body = Expressions.wrap([body]).compile(o); - if_part = prefix + if_dent + 'if (' + compile_condition(cond_o) + ') {\n' + body + '\n' + this.idt() + '}'; + if_part = prefix + if_dent + 'if (' + this.compile_condition(cond_o) + ') {\n' + body + '\n' + this.idt() + '}'; if (!(this.else_body)) { return if_part; } else_part = this.is_chain() ? ' else ' + this.else_body.compile(merge(o, { indent: this.idt(), chain_child: true - })) : ' else {\n' + Expressions.wrap(this.else_body).compile(o) + '\n' + this.idt() + '}'; + })) : ' else {\n' + Expressions.wrap([this.else_body]).compile(o) + '\n' + this.idt() + '}'; return if_part + else_part; }, // Compile the IfNode into a ternary operator. diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 9ac169d9..cc7de8b6 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -21,9 +21,11 @@ exports.VERSION: '0.5.0' # Compile CoffeeScript to JavaScript, using the Coffee/Jison compiler. exports.compile: (code) -> - tokens: lexer.tokenize code - nodes: parser.parse tokens - nodes.compile() + (parser.parse lexer.tokenize code).compile() + +# Just the tokens. +exports.tokenize: (code) -> + lexer.tokenize code #---------- Below this line is obsolete, for the Ruby compiler. ---------------- diff --git a/src/command_line.coffee b/src/command_line.coffee index 6eeefe46..307c0f2b 100644 --- a/src/command_line.coffee +++ b/src/command_line.coffee @@ -59,10 +59,12 @@ exports.compile: (script, source) -> # or JSLint results. exports.compile_scripts: -> return unless source: @sources.shift() - posix.cat(source).addCallback (code) => - js: coffee.compile code - return eval js if @options.run - return puts js if @options.print + opts: @options + posix.cat(source).addCallback (code) -> + return puts coffee.tokenize(code).join(' ') if opts.tokens + js: coffee.compile code + return eval js if opts.run + return puts js if opts.print exports.compile_scripts() diff --git a/src/nodes.coffee b/src/nodes.coffee index b988c556..ee262cc5 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -77,9 +77,9 @@ Node: exports.Node: -> Node::compile: (o) -> @options: dup(o or {}) @indent: o.indent - top: if @top_sensitive() then o.top else del o, 'top' + top: if @top_sensitive() then @options.top else del @options, 'top' closure: @is_statement() and not @is_statement_only() and not top and - not o.returns and not this instanceof CommentNode and + not @options.returns and not (this instanceof CommentNode) and not @contains (node) -> node.is_statement_only() if closure then @compile_closure(@options) else @compile_node(@options) @@ -114,7 +114,7 @@ Node::top_sensitive: -> false Expressions: exports.Expressions: inherit Node, { constructor: (nodes) -> - @children: @expressions: flatten nodes + @children: @expressions: compact flatten nodes this # Tack an expression on to the end of this expression list. @@ -970,12 +970,12 @@ IfNode: exports.IfNode: inherit Node, { com_dent: if child then @idt() else '' prefix: if @comment then @comment.compile(cond_o) + '\n' + com_dent else '' body: Expressions.wrap([body]).compile(o) - if_part: prefix + if_dent + 'if (' + compile_condition(cond_o) + ') {\n' + body + '\n' + @idt() + '}' + if_part: prefix + if_dent + 'if (' + @compile_condition(cond_o) + ') {\n' + body + '\n' + @idt() + '}' return if_part unless @else_body else_part: if @is_chain() ' else ' + @else_body.compile(merge(o, {indent: @idt(), chain_child: true})) else - ' else {\n' + Expressions.wrap(@else_body).compile(o) + '\n' + @idt() + '}' + ' else {\n' + Expressions.wrap([@else_body]).compile(o) + '\n' + @idt() + '}' if_part + else_part # Compile the IfNode into a ternary operator. @@ -985,44 +985,3 @@ IfNode: exports.IfNode: inherit Node, { if_part + ' : ' + else_part } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/test/fixtures/execution/test_expressions.coffee b/test/fixtures/execution/test_expressions.coffee index eee44179..7843a134 100644 --- a/test/fixtures/execution/test_expressions.coffee +++ b/test/fixtures/execution/test_expressions.coffee @@ -1,9 +1,9 @@ # Ensure that we don't wrap Nodes that are "statement_only" in a closure. -items: [1, 2, 3, "bacon", 4, 5] - -for item in items - break if item is "bacon" +# items: [1, 2, 3, "bacon", 4, 5] +# +# for item in items +# break if item is "bacon" findit: (items) -> for item in items @@ -15,16 +15,16 @@ puts findit(items) is "bacon" # When when a closure wrapper is generated for expression conversion, make sure # that references to "this" within the wrapper are safely converted as well. -obj: { - num: 5 - func: -> - this.result: if false - 10 - else - "a" - "b" - this.num -} - -puts obj.num is obj.func() -puts obj.num is obj.result \ No newline at end of file +# obj: { +# num: 5 +# func: -> +# this.result: if false +# 10 +# else +# "a" +# "b" +# this.num +# } +# +# puts obj.num is obj.func() +# puts obj.num is obj.result \ No newline at end of file