fix the if syntax and branches

This commit is contained in:
Torsten Ruger 2015-10-19 16:22:24 +03:00
parent 1fd937927c
commit 99cff3aa32
5 changed files with 44 additions and 53 deletions

View File

@ -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

View File

@ -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

View File

@ -25,7 +25,7 @@ HERE
class Object
int main()
int n = 10
if_minus(8 - n )
if_zero(8 - n )
"10".putstring()
end
end

View File

@ -8,7 +8,7 @@ class TestIfStatement < MiniTest::Test
@string_input = <<HERE
class Object
int main()
if_plus( 10 < 12)
if_plus( 10 - 12)
return 3
else
return 4
@ -17,18 +17,36 @@ class Object
end
HERE
@expect = [[SaveReturn,LoadConstant,LoadConstant,
OperatorInstruction,IsZero] ,
OperatorInstruction,IsPlus] ,
[LoadConstant,Branch] ,[LoadConstant] ,[] ,
[RegisterTransfer,GetSlot,FunctionReturn]]
check
end
def test_if_small
def test_if_small_minus
@string_input = <<HERE
class Object
int main()
if_minus( 10 < 12)
if_minus( 10 - 12)
return 3
end
end
end
HERE
@expect = [[SaveReturn,LoadConstant,LoadConstant,
OperatorInstruction,IsMinus] ,
[Branch] ,[LoadConstant] ,[] ,
[RegisterTransfer,GetSlot,FunctionReturn]]
check
end
def test_if_small_zero
@string_input = <<HERE
class Object
int main()
if_zero( 10 - 12)
return 3
end
end
@ -40,24 +58,5 @@ HERE
[RegisterTransfer,GetSlot,FunctionReturn]]
check
end
def ttest_call_function
@string_input = <<HERE
class Object
int itest(int n)
return 4
end
int main()
itest(20)
end
end
HERE
@expect = [ [SaveReturn,GetSlot,SetSlot,LoadConstant,
SetSlot,LoadConstant,SetSlot,RegisterTransfer,FunctionCall,
GetSlot] ,[RegisterTransfer,GetSlot,FunctionReturn] ]
check
end
end
end

View File

@ -29,7 +29,7 @@ class Integer < Object
div = self / 10
int rest
rest = self - div
if_nonzero( rest )
if_notzero( rest )
rest = self.digit( rest )
str = str + rest
else
@ -58,7 +58,7 @@ HERE
# Phisol::Compiler.compile( statements , Virtual.machine.space.get_main )
@interpreter = Interpreter::Interpreter.new
@interpreter.start Virtual.machine.init
#show_ticks # get output of what is
# show_ticks # get output of what is
["Branch","LoadConstant","GetSlot","SetSlot","RegisterTransfer",
"FunctionCall","SaveReturn","GetSlot","LoadConstant","SetSlot",
"LoadConstant","SetSlot","RegisterTransfer","FunctionCall","SaveReturn",
@ -67,23 +67,14 @@ HERE
"SetSlot","RegisterTransfer","FunctionCall","SaveReturn","GetSlot",
"LoadConstant","OperatorInstruction","GetSlot","SetSlot","GetSlot",
"GetSlot","GetSlot","OperatorInstruction","GetSlot","SetSlot",
"GetSlot","GetSlot","IsZero","GetSlot","GetSlot",
"GetSlot","SetSlot","LoadConstant","SetSlot","GetSlot",
"GetSlot","GetSlot","IsNotzero","GetSlot","GetSlot",
"SetSlot","LoadConstant","SetSlot","GetSlot","GetSlot",
"SetSlot","RegisterTransfer","FunctionCall","SaveReturn","GetSlot",
"LoadConstant","OperatorInstruction","GetSlot","SetSlot","GetSlot",
"GetSlot","GetSlot","OperatorInstruction","GetSlot","SetSlot",
"GetSlot","GetSlot","IsZero","GetSlot","GetSlot",
"GetSlot","SetSlot","LoadConstant","SetSlot","GetSlot",
"SetSlot","RegisterTransfer","FunctionCall","SaveReturn","GetSlot",
"LoadConstant","OperatorInstruction","GetSlot","SetSlot","GetSlot",
"GetSlot","GetSlot","OperatorInstruction","GetSlot","SetSlot",
"GetSlot","GetSlot","IsZero","GetSlot","GetSlot",
"GetSlot","SetSlot","LoadConstant","SetSlot","GetSlot",
"SetSlot","RegisterTransfer","FunctionCall","SaveReturn","GetSlot",
"LoadConstant","OperatorInstruction","GetSlot","SetSlot","GetSlot",
"GetSlot","GetSlot","OperatorInstruction","GetSlot","SetSlot",
"GetSlot","GetSlot","IsZero","GetSlot","GetSlot",
"GetSlot"].each_with_index do |name , index|
"LoadConstant","OperatorInstruction","IsZero","LoadConstant","GetSlot",
"LoadConstant","OperatorInstruction","IsZero","LoadConstant","GetSlot",
"LoadConstant","OperatorInstruction","IsZero","LoadConstant","GetSlot",
"LoadConstant","OperatorInstruction","IsZero","LoadConstant","GetSlot",
"LoadConstant","OperatorInstruction","IsZero","LoadConstant","NilClass"].each_with_index do |name , index|
got = ticks(1)
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
end