From 99cff3aa326d5e3a2748f118a59738a3b0ce897d Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 19 Oct 2015 16:22:24 +0300 Subject: [PATCH] fix the if syntax and branches --- lib/interpreter/interpreter.rb | 18 +++++++----- lib/phisol/compiler/if_statement.rb | 5 ++-- test/compiler/fragments/test_if.rb | 2 +- test/compiler/statements/test_if.rb | 45 ++++++++++++++--------------- test/interpreter/test_puti.rb | 27 ++++++----------- 5 files changed, 44 insertions(+), 53 deletions(-) diff --git a/lib/interpreter/interpreter.rb b/lib/interpreter/interpreter.rb index 4b2ce01f..602bc6ad 100644 --- a/lib/interpreter/interpreter.rb +++ b/lib/interpreter/interpreter.rb @@ -103,14 +103,16 @@ module Interpreter end def execute_IsZero - #puts @instruction.inspect - if( @flags[:zero] ) - target = @instruction.block - set_block target - return false - else - return true - end + @flags[:zero] ? execute_Branch : true + end + def execute_IsNotzero + @flags[:zero] ? true : execute_Branch + end + def execute_IsPlus + @flags[:plus] ? execute_Branch : true + end + def execute_IsMinus + @flags[:minus] ? execute_Branch : true end def execute_LoadConstant diff --git a/lib/phisol/compiler/if_statement.rb b/lib/phisol/compiler/if_statement.rb index f3c49431..f5e1dd49 100644 --- a/lib/phisol/compiler/if_statement.rb +++ b/lib/phisol/compiler/if_statement.rb @@ -13,9 +13,8 @@ module Phisol false_block = @method.source.new_block "if_false" # directly next in order, ie if we don't jump we land here is = process(condition) - # TODO should/will use different branches for different conditions. - # just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? BranchCondition - @method.source.add_code Register::IsZero.new( condition , true_block ) + branch_class = Object.const_get "Register::Is#{branch_type.capitalize}" + @method.source.add_code branch_class.new( condition , true_block ) # compile the true block (as we think of it first, even it is second in sequential order) @method.source.current true_block diff --git a/test/compiler/fragments/test_if.rb b/test/compiler/fragments/test_if.rb index e20d0faa..4dcbf0bf 100644 --- a/test/compiler/fragments/test_if.rb +++ b/test/compiler/fragments/test_if.rb @@ -25,7 +25,7 @@ HERE class Object int main() int n = 10 - if_minus(8 - n ) + if_zero(8 - n ) "10".putstring() end end diff --git a/test/compiler/statements/test_if.rb b/test/compiler/statements/test_if.rb index 1f3e7bd9..990d2480 100644 --- a/test/compiler/statements/test_if.rb +++ b/test/compiler/statements/test_if.rb @@ -8,7 +8,7 @@ class TestIfStatement < MiniTest::Test @string_input = <