Skip to content

Commit

Permalink
Add ifFalse: shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed Feb 5, 2011
1 parent b54fa11 commit 3bbcd27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/prattle/ast/keyword_send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ def self.grammar(g)
end
end

def self.conditional(g, name, args)
def self.if_cond(g, name, args, if_true)
return false unless args[0].kind_of? AST::Block

done_lbl = g.new_label
else_lbl = g.new_label

g.gif else_lbl
if if_true
g.gif else_lbl
else
g.git else_lbl
end

args[0].body.each_with_index do |e,idx|
g.pop unless idx == 0
e.bytecode(g)
Expand All @@ -76,8 +81,11 @@ def self.conditional(g, name, args)
end

def self.send_method(g, name, args)
if name == "ifTrue:"
return if conditional g, name, args
case name
when "ifTrue:"
return if if_cond g, name, args, true
when "ifFalse:"
return if if_cond g, name, args, false
end

if name[0] == ?~
Expand Down
16 changes: 16 additions & 0 deletions test/ast/test_keyword_send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,20 @@ def test_shortcut_ifTrue

assert_equal nil, node.run
end

def test_shortcut_ifFalse
str = '(4 > 3) ifFalse: [ 8 ]'

parser = Prattle::Parser.new(str)
node = parser.parse :keyword_send

assert_equal nil, node.run

str = '(3 > 4) ifFalse: [ 8 ]'

parser = Prattle::Parser.new(str)
node = parser.parse :keyword_send

assert_equal 8, node.run
end
end

0 comments on commit 3bbcd27

Please sign in to comment.