basic if statement tests

This commit is contained in:
Torsten Ruger 2015-10-15 09:47:11 +03:00
parent 3d83f203ca
commit f8efdd910c
4 changed files with 101 additions and 3 deletions

View File

@ -2,15 +2,15 @@ module Phisol
Compiler.class_eval do
def on_operator_value statement
puts "operator #{statement.inspect}"
#puts "operator #{statement.inspect}"
operator , left_e , right_e = *statement
# left and right must be expressions. Expressions return a register when compiled
left_reg = process(left_e)
right_reg = process(right_e)
raise "Not register #{left_reg}" unless left_reg.is_a?(Register::RegisterValue)
raise "Not register #{right_reg}" unless right_reg.is_a?(Register::RegisterValue)
puts "left #{left_reg}"
puts "right #{right_reg}"
#puts "left #{left_reg}"
#puts "right #{right_reg}"
@method.source.add_code Register::OperatorInstruction.new(statement,operator,left_reg,right_reg)
return left_reg # though this has wrong value attached
end

View File

@ -0,0 +1,23 @@
require_relative '../../helper'
module Statements
def check
machine = Virtual.machine.boot
machine.parse_and_compile @string_input
produced = Virtual.machine.space.get_main.source
assert @expect , "No output given"
assert_equal @expect.length , produced.blocks.length , "Block length"
produced.blocks.each_with_index do |b,i|
codes = @expect[i]
assert codes , "No codes for block #{i}"
assert_equal b.codes.length , codes.length , "Code length for block #{i+1}"
b.codes.each_with_index do |c , ii |
assert_equal codes[ii] , c.class , "Block #{i+1} , code #{ii+1}"
end
end
end
end

View File

@ -0,0 +1 @@
require_relative "test_if"

View File

@ -0,0 +1,74 @@
require_relative 'helper'
class TestIfStatement < MiniTest::Test
include Statements
def test_if_basic
@string_input = <<HERE
class Object
int main()
if( 10 < 12)
return 3
else
return 4
end
end
end
HERE
@expect = [[Virtual::MethodEnter,Register::LoadConstant,Register::LoadConstant,
Register::OperatorInstruction,Register::IsZeroBranch] ,
[Register::LoadConstant,Register::AlwaysBranch] ,[Register::LoadConstant] ,[] ,
[Virtual::MethodReturn]]
check
end
def test_if_small
@string_input = <<HERE
class Object
int main()
if( 10 < 12)
return 3
end
end
end
HERE
@expect = [[Virtual::MethodEnter,Register::LoadConstant,Register::LoadConstant,
Register::OperatorInstruction,Register::IsZeroBranch] ,
[Register::AlwaysBranch] ,[Register::LoadConstant] ,[] ,
[Virtual::MethodReturn]]
check
end
def ttest_return
@string_input = <<HERE
class Object
int main()
return 5
end
end
HERE
@expect = [[Virtual::MethodEnter,Register::LoadConstant] , [Virtual::MethodReturn]]
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 = [ [Virtual::MethodEnter,Register::GetSlot,Register::SetSlot,Register::LoadConstant,
Register::SetSlot,Register::LoadConstant,Register::SetSlot,Virtual::MethodCall,
Register::GetSlot] ,[Virtual::MethodReturn] ]
check
end
end