finish assignment tests

and related code fixes
This commit is contained in:
Torsten Ruger 2016-12-10 18:48:18 +02:00
parent f359e78982
commit 6d4347c7ba
2 changed files with 23 additions and 57 deletions

View File

@ -4,23 +4,23 @@ module Typed
def on_Assignment( statement ) def on_Assignment( statement )
# name , value = *statement # name , value = *statement
reset_regs # statements reset registers, ie have all at their disposal reset_regs # statements reset registers, ie have all at their disposal
name_s = no_space statement.name
value = process(statement.value) value = process(statement.value)
raise "Not register #{v}" unless value.is_a?(Register::RegisterValue) raise "Not register #{v}" unless value.is_a?(Register::RegisterValue)
code = get_code( statement , name_s , value) code = get_code( statement , value)
raise "must define variable #{name} before using it in #{@method.inspect}" unless code raise "must define variable #{statement.name.name} before using it in #{@method.inspect}" unless code
add_code code add_code code
end end
private private
def get_code( statement , name_s , value) def get_code( statement , value)
if( index = @method.has_arg(name_s.name)) name = no_space(statement.name).name
if( index = @method.has_arg(name))
# TODO, check type @method.arguments[index].type # TODO, check type @method.arguments[index].type
return Register.set_slot(statement , value , :message , Parfait::Message.get_indexed(index) ) return Register.set_slot(statement , value , :message , Parfait::Message.get_indexed(index) )
end end
# or a local so it is in the frame # or a local so it is in the frame
index = @method.has_local( name_s.name ) index = @method.has_local( name )
return nil unless index return nil unless index
# TODO, check type @method.locals[index].type # TODO, check type @method.locals[index].type
frame = use_reg(:Frame) frame = use_reg(:Frame)

View File

@ -8,7 +8,7 @@ class TestAssignStatement < MiniTest::Test
Register.machine.boot Register.machine.boot
end end
def _test_assign_op def test_assign_op
@input = s(:statements, s(:field_def, :Integer, s(:name, :n), @input = s(:statements, s(:field_def, :Integer, s(:name, :n),
s(:operator_value, :+, s(:int, 10), s(:int, 1)))) s(:operator_value, :+, s(:int, 10), s(:int, 1))))
@ -17,7 +17,7 @@ class TestAssignStatement < MiniTest::Test
check check
end end
def _test_assign_local def test_assign_local
@input = s(:statements, s(:field_def, :Integer, s(:name, :runner)), @input = s(:statements, s(:field_def, :Integer, s(:name, :runner)),
s(:assignment, s(:name, :runner), s(:int, 5))) s(:assignment, s(:name, :runner), s(:int, 5)))
@ -25,41 +25,25 @@ class TestAssignStatement < MiniTest::Test
check check
end end
def _test_assign_local_assign def test_assign_local_assign
@input = <<HERE @input = s(:statements, s(:field_def, :Integer, s(:name, :runner), s(:int, 5)))
class Space
int main()
int runner = 5
end
end
HERE
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn] @expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
check check
end end
def _test_assign_call def test_assign_call
@input = <<HERE @input = s(:statements, s(:field_def, :Integer, s(:name, :r),
class Space s(:call, s(:name, :main), s(:arguments))))
int main()
int r = main()
end
end
HERE
@expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant , @expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant ,
SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer , SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
GetSlot, GetSlot, GetSlot, SetSlot, Label, FunctionReturn] GetSlot, GetSlot, GetSlot, SetSlot, Label, FunctionReturn]
check check
end end
def _test_frame_get def test_frame_get
@input = <<HERE @input = s(:statements, s(:field_def, :Integer, s(:name, :r), s(:int, 5)),
class Space s(:return, s(:name, :r)))
int main()
int r = 5
return r
end
end
HERE
@expect = [Label, LoadConstant, GetSlot, SetSlot, GetSlot, GetSlot, SetSlot , @expect = [Label, LoadConstant, GetSlot, SetSlot, GetSlot, GetSlot, SetSlot ,
Label, FunctionReturn] Label, FunctionReturn]
was = check was = check
@ -68,14 +52,8 @@ HERE
assert_equal 4, get.index , "Get to frame index must be offset, not #{get.index}" assert_equal 4, get.index , "Get to frame index must be offset, not #{get.index}"
end end
def _test_assign_int def test_assign_int
@input = <<HERE @input = s(:statements, s(:field_def, :Integer, s(:name, :r), s(:int, 5)) )
class Space
int main()
int r = 5
end
end
HERE
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn] @expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
was = check was = check
set = was.next(3) set = was.next(3)
@ -83,15 +61,9 @@ HERE
assert_equal 4, set.index , "Set to frame index must be offset, not #{set.index}" assert_equal 4, set.index , "Set to frame index must be offset, not #{set.index}"
end end
def _test_assign_arg def test_assign_arg
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :blar) Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :blar)
@input = <<HERE @input = s(:statements, s(:assignment, s(:name, :blar), s(:int, 5)))
class Space
int main(int blar)
blar = 5
end
end
HERE
@expect = [Label, LoadConstant, SetSlot, Label, FunctionReturn] @expect = [Label, LoadConstant, SetSlot, Label, FunctionReturn]
was = check was = check
set = was.next(2) set = was.next(2)
@ -99,16 +71,10 @@ HERE
assert_equal 10, set.index , "Set to args index must be offset, not #{set.index}" assert_equal 10, set.index , "Set to args index must be offset, not #{set.index}"
end end
def _test_arg_get def test_arg_get
# have to define bar externally, just because redefining main. Otherwise that would be automatic # have to define bar externally, just because redefining main. Otherwise that would be automatic
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :balr) Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :balr)
@input = <<HERE @input = s(:statements, s(:return, s(:name, :balr)))
class Space
int main(int balr)
return balr
end
end
HERE
@expect = [Label, GetSlot, SetSlot, Label, FunctionReturn] @expect = [Label, GetSlot, SetSlot, Label, FunctionReturn]
was = check was = check
get = was.next(1) get = was.next(1)