diff --git a/lib/vool/compiler.rb b/lib/vool/compiler.rb index 2989e095..948d949a 100644 --- a/lib/vool/compiler.rb +++ b/lib/vool/compiler.rb @@ -118,11 +118,10 @@ module Vool w end - def on_while_statement statement - branch_type , condition , statements = *statement - w = WhileStatement.new() - w.branch_type = branch_type - w.condition = process(condition) + def on_while statement + condition , statements = *statement + w = WhileStatement.new( process(condition) ) + simplify_condition(w) w.statements = process(statements) w end @@ -130,16 +129,18 @@ module Vool def on_if statement # puts "IF #{statement}" condition , if_true , if_false = *statement - w = IfStatement.new() - w.condition = process(condition) - if(w.condition.is_a?(ScopeStatement) and w.condition.single?) - w.condition = w.condition.first - end + w = IfStatement.new( process(condition) ) + simplify_condition(w) w.if_true = process(if_true) w.if_false = process(if_false) w end + def simplify_condition( cond ) + return unless cond.condition.is_a?(ScopeStatement) + cond.condition = cond.condition.first if cond.condition.single? + end + def on_operator_value statement operator , left_e , right_e = *statement w = OperatorStatement.new() diff --git a/lib/vool/while_statement.rb b/lib/vool/while_statement.rb index 21a18686..83970f0a 100644 --- a/lib/vool/while_statement.rb +++ b/lib/vool/while_statement.rb @@ -1,5 +1,9 @@ module Vool class WhileStatement < Statement - attr_accessor :branch_type , :condition , :statements + attr_accessor :condition , :statements + + def initialize( condition ) + @condition = condition + end end end diff --git a/test/vool/test_compiler.rb b/test/vool/test_compiler.rb index 2a6baf5d..573949c0 100644 --- a/test/vool/test_compiler.rb +++ b/test/vool/test_compiler.rb @@ -7,3 +7,4 @@ require_relative "test_if_statement" require_relative "test_method_statement" require_relative "test_send_statement" require_relative "test_variables" +require_relative "test_while_statement" diff --git a/test/vool/test_while_statement.rb b/test/vool/test_while_statement.rb new file mode 100644 index 00000000..e7263dec --- /dev/null +++ b/test/vool/test_while_statement.rb @@ -0,0 +1,39 @@ +require_relative 'helper' + +module Vool + class TestWhileStatement < MiniTest::Test + + def basic_while + "while(10 < 12) ; true ; end" + end + def test_while_basic + lst = Compiler.compile( basic_while ) + assert_equal WhileStatement , lst.class + end + def test_while_basic_cond + lst = Compiler.compile( basic_while ) + assert_equal SendStatement , lst.condition.class + end + def test_while_basic_branches + lst = Compiler.compile( basic_while ) + assert_equal TrueStatement , lst.statements.class + end + + def reverse_while + "true while(false)" + end + def test_while_reverse_branches + lst = Compiler.compile( reverse_while ) + assert_equal WhileStatement , lst.class + end + def test_while_reverse_cond + lst = Compiler.compile( reverse_while ) + assert_equal FalseStatement , lst.condition.class + end + def test_while_reverse_branches + lst = Compiler.compile( reverse_while ) + assert_equal TrueStatement , lst.statements.class + end + + end +end