adding when clauses with multiple values

This commit is contained in:
Jeremy Ashkenas
2010-01-13 19:56:35 -05:00
parent abd9ab5c71
commit 0ceca0778c
3 changed files with 30 additions and 4 deletions

View File

@@ -314,6 +314,12 @@ rule
| ArgList OUTDENT { result = val[0] } | 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/catch/finally exception handling blocks.
Try: Try:
TRY Block Catch { result = TryNode.new(val[1], val[2][0], val[2][1]) } TRY Block Catch { result = TryNode.new(val[1], val[2][0], val[2][1]) }
@@ -383,8 +389,8 @@ rule
# An individual when. # An individual when.
When: When:
LEADING_WHEN Expression Block { result = IfNode.new(val[1], val[2], nil, {:statement => true}) } LEADING_WHEN SimpleArgs Block { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
| LEADING_WHEN Expression Block | LEADING_WHEN SimpleArgs Block
Terminator { result = IfNode.new(val[1], val[2], nil, {:statement => true}) } Terminator { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
| Comment Terminator When { result = val[2].add_comment(val[0]) } | Comment Terminator When { result = val[2].add_comment(val[0]) }
; ;

View File

@@ -821,6 +821,7 @@ module CoffeeScript
@body = body && body.unwrap @body = body && body.unwrap
@else_body = else_body && else_body.unwrap @else_body = else_body && else_body.unwrap
@tags = tags @tags = tags
@multiple = true if @condition.is_a?(Array)
@condition = OpNode.new("!", ParentheticalNode.new(@condition)) if @tags[:invert] @condition = OpNode.new("!", ParentheticalNode.new(@condition)) if @tags[:invert]
end end
@@ -842,7 +843,8 @@ module CoffeeScript
# Rewrite a chain of IfNodes with their switch condition for equality. # Rewrite a chain of IfNodes with their switch condition for equality.
def rewrite_condition(expression) 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? @else_body.rewrite_condition(expression) if chain?
self self
end end
@@ -864,6 +866,10 @@ module CoffeeScript
@is_statement ||= !!(@comment || @tags[:statement] || @body.statement? || (@else_body && @else_body.statement?)) @is_statement ||= !!(@comment || @tags[:statement] || @body.statement? || (@else_body && @else_body.statement?))
end end
def compile_condition(o)
[@condition].flatten.map {|c| c.compile(o) }.join(' || ')
end
def compile_node(o) def compile_node(o)
write(statement? ? compile_statement(o) : compile_ternary(o)) write(statement? ? compile_statement(o) : compile_ternary(o))
end end
@@ -879,7 +885,7 @@ module CoffeeScript
if_dent = child ? '' : idt if_dent = child ? '' : idt
com_dent = child ? idt : '' com_dent = child ? idt : ''
prefix = @comment ? @comment.compile(cond_o) + "\n#{com_dent}" : '' 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 return if_part unless @else_body
else_part = chain? ? else_part = chain? ?
" else #{@else_body.compile(o.merge(:indent => idt, :chain_child => true))}" : " else #{@else_body.compile(o.merge(:indent => idt, :chain_child => true))}" :

View File

@@ -15,3 +15,17 @@ result: switch num
else false else false
print(result) 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))