From 0ceca0778c45d768893fa9662ef5bcc4425d6e5b Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 13 Jan 2010 19:56:35 -0500 Subject: [PATCH] adding when clauses with multiple values --- lib/coffee_script/grammar.y | 10 ++++++++-- lib/coffee_script/nodes.rb | 10 ++++++++-- test/fixtures/execution/test_switch.coffee | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/coffee_script/grammar.y b/lib/coffee_script/grammar.y index e922ecf0..aab2339d 100644 --- a/lib/coffee_script/grammar.y +++ b/lib/coffee_script/grammar.y @@ -314,6 +314,12 @@ rule | ArgList OUTDENT { result = val[0] } ; + # Just simple, comma-separated, required arguments (no fancy syntax). + SimpleArgs: + Expression { result = val[0] } + | SimpleArgs "," Expression { result = ([val[0]] << val[2]).flatten } + ; + # Try/catch/finally exception handling blocks. Try: TRY Block Catch { result = TryNode.new(val[1], val[2][0], val[2][1]) } @@ -383,8 +389,8 @@ rule # An individual when. When: - LEADING_WHEN Expression Block { result = IfNode.new(val[1], val[2], nil, {:statement => true}) } - | LEADING_WHEN Expression Block + LEADING_WHEN SimpleArgs Block { result = IfNode.new(val[1], val[2], nil, {:statement => true}) } + | LEADING_WHEN SimpleArgs Block Terminator { result = IfNode.new(val[1], val[2], nil, {:statement => true}) } | Comment Terminator When { result = val[2].add_comment(val[0]) } ; diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 2a8605cd..64b854e3 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -821,6 +821,7 @@ module CoffeeScript @body = body && body.unwrap @else_body = else_body && else_body.unwrap @tags = tags + @multiple = true if @condition.is_a?(Array) @condition = OpNode.new("!", ParentheticalNode.new(@condition)) if @tags[:invert] end @@ -842,7 +843,8 @@ module CoffeeScript # Rewrite a chain of IfNodes with their switch condition for equality. def rewrite_condition(expression) - @condition = OpNode.new("is", expression, @condition) + @condition = @multiple ? @condition.map {|c| OpNode.new("is", expression, c) } : + OpNode.new("is", expression, @condition) @else_body.rewrite_condition(expression) if chain? self end @@ -864,6 +866,10 @@ module CoffeeScript @is_statement ||= !!(@comment || @tags[:statement] || @body.statement? || (@else_body && @else_body.statement?)) end + def compile_condition(o) + [@condition].flatten.map {|c| c.compile(o) }.join(' || ') + end + def compile_node(o) write(statement? ? compile_statement(o) : compile_ternary(o)) end @@ -879,7 +885,7 @@ module CoffeeScript if_dent = child ? '' : idt com_dent = child ? idt : '' prefix = @comment ? @comment.compile(cond_o) + "\n#{com_dent}" : '' - if_part = "#{prefix}#{if_dent}if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{idt}}" + if_part = "#{prefix}#{if_dent}if (#{compile_condition(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{idt}}" return if_part unless @else_body else_part = chain? ? " else #{@else_body.compile(o.merge(:indent => idt, :chain_child => true))}" : diff --git a/test/fixtures/execution/test_switch.coffee b/test/fixtures/execution/test_switch.coffee index 9bdbf0f0..35ff3c7a 100644 --- a/test/fixtures/execution/test_switch.coffee +++ b/test/fixtures/execution/test_switch.coffee @@ -15,3 +15,17 @@ result: switch num else false print(result) + + +func: num => + switch num + when 2, 4, 6 + true + when 1, 3, 5 + false + else false + +print(func(2)) +print(func(6)) +print(!func(3)) +print(!func(8))