From aa45456d7db7d35a9e98cae48ea8d05d03acb1cb Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 27 Dec 2009 21:50:02 -0800 Subject: [PATCH] part of the way to supporting multiline array comprehensions -- the grammar and parsing is there -- the code generation is tricky --- lib/coffee_script/grammar.y | 8 +++++--- lib/coffee_script/nodes.rb | 2 ++ test/fixtures/execution/test_array_comprehension.coffee | 8 ++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/coffee_script/grammar.y b/lib/coffee_script/grammar.y index a0946f65..5c574201 100644 --- a/lib/coffee_script/grammar.y +++ b/lib/coffee_script/grammar.y @@ -321,7 +321,9 @@ rule # Looks a little confusing, check nodes.rb for the arguments to ForNode. For: Expression FOR - ForVariables ForSource { result = ForNode.new(val[0], val[3][0], val[2][0], val[3][1], val[2][1]) } + ForVariables ForSource "." { result = ForNode.new(val[0], val[3][0], val[2][0], val[3][1], val[2][1]) } + | FOR ForVariables ForSource + Terminator Expressions "." { result = ForNode.new(val[4], val[2][0], val[1][0], val[2][1], val[1][1]) } ; # An array comprehension has variables for the current element and index. @@ -332,9 +334,9 @@ rule # The source of the array comprehension can optionally be filtered. ForSource: - IN PureExpression "." { result = [val[1]] } + IN PureExpression { result = [val[1]] } | IN PureExpression - IF Expression "." { result = [val[1], val[3]] } + IF Expression { result = [val[1], val[3]] } ; # Switch/When blocks. diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 7730ed74..188055e9 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -571,6 +571,8 @@ module CoffeeScript if o[:return] || o[:assign] return_result = "#{o[:assign].compile(o)} = #{return_result}" if o[:assign] return_result = "return #{return_result}" if o[:return] + o.delete(:assign) + o.delete(:return) if @filter body = CallNode.new(ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [@body]) body = IfNode.new(@filter, body, nil, :statement => true) diff --git a/test/fixtures/execution/test_array_comprehension.coffee b/test/fixtures/execution/test_array_comprehension.coffee index 973a595a..7df03142 100644 --- a/test/fixtures/execution/test_array_comprehension.coffee +++ b/test/fixtures/execution/test_array_comprehension.coffee @@ -1,4 +1,8 @@ nums: n * n for n in [1, 2, 3] if n % 2 isnt 0. -result: n * 2 for n in nums. +results: n * 2 for n in nums. -print(result.join(',') is '2,18') \ No newline at end of file +# next: for n in [1, 2, 3] if n % 2 isnt 0 +# print('hi') if false +# n * n * 2. + +print(results.join(',') is '2,18') \ No newline at end of file