finish assignment tests
and related code fixes
This commit is contained in:
parent
f359e78982
commit
6d4347c7ba
@ -4,23 +4,23 @@ module Typed
|
||||
def on_Assignment( statement )
|
||||
# name , value = *statement
|
||||
reset_regs # statements reset registers, ie have all at their disposal
|
||||
name_s = no_space statement.name
|
||||
value = process(statement.value)
|
||||
raise "Not register #{v}" unless value.is_a?(Register::RegisterValue)
|
||||
code = get_code( statement , name_s , value)
|
||||
raise "must define variable #{name} before using it in #{@method.inspect}" unless code
|
||||
code = get_code( statement , value)
|
||||
raise "must define variable #{statement.name.name} before using it in #{@method.inspect}" unless code
|
||||
add_code code
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_code( statement , name_s , value)
|
||||
if( index = @method.has_arg(name_s.name))
|
||||
def get_code( statement , value)
|
||||
name = no_space(statement.name).name
|
||||
if( index = @method.has_arg(name))
|
||||
# TODO, check type @method.arguments[index].type
|
||||
return Register.set_slot(statement , value , :message , Parfait::Message.get_indexed(index) )
|
||||
end
|
||||
# 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
|
||||
# TODO, check type @method.locals[index].type
|
||||
frame = use_reg(:Frame)
|
||||
|
@ -8,7 +8,7 @@ class TestAssignStatement < MiniTest::Test
|
||||
Register.machine.boot
|
||||
end
|
||||
|
||||
def _test_assign_op
|
||||
def test_assign_op
|
||||
@input = s(:statements, s(:field_def, :Integer, s(:name, :n),
|
||||
s(:operator_value, :+, s(:int, 10), s(:int, 1))))
|
||||
|
||||
@ -17,7 +17,7 @@ class TestAssignStatement < MiniTest::Test
|
||||
check
|
||||
end
|
||||
|
||||
def _test_assign_local
|
||||
def test_assign_local
|
||||
@input = s(:statements, s(:field_def, :Integer, s(:name, :runner)),
|
||||
s(:assignment, s(:name, :runner), s(:int, 5)))
|
||||
|
||||
@ -25,41 +25,25 @@ class TestAssignStatement < MiniTest::Test
|
||||
check
|
||||
end
|
||||
|
||||
def _test_assign_local_assign
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
int runner = 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
def test_assign_local_assign
|
||||
@input = s(:statements, s(:field_def, :Integer, s(:name, :runner), s(:int, 5)))
|
||||
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def _test_assign_call
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
int r = main()
|
||||
end
|
||||
end
|
||||
HERE
|
||||
def test_assign_call
|
||||
@input = s(:statements, s(:field_def, :Integer, s(:name, :r),
|
||||
s(:call, s(:name, :main), s(:arguments))))
|
||||
@expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant ,
|
||||
SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
|
||||
GetSlot, GetSlot, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
check
|
||||
end
|
||||
|
||||
def _test_frame_get
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
int r = 5
|
||||
return r
|
||||
end
|
||||
end
|
||||
HERE
|
||||
def test_frame_get
|
||||
@input = s(:statements, s(:field_def, :Integer, s(:name, :r), s(:int, 5)),
|
||||
s(:return, s(:name, :r)))
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, GetSlot, GetSlot, SetSlot ,
|
||||
Label, FunctionReturn]
|
||||
was = check
|
||||
@ -68,14 +52,8 @@ HERE
|
||||
assert_equal 4, get.index , "Get to frame index must be offset, not #{get.index}"
|
||||
end
|
||||
|
||||
def _test_assign_int
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main()
|
||||
int r = 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
def test_assign_int
|
||||
@input = s(:statements, s(:field_def, :Integer, s(:name, :r), s(:int, 5)) )
|
||||
@expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
was = check
|
||||
set = was.next(3)
|
||||
@ -83,15 +61,9 @@ HERE
|
||||
assert_equal 4, set.index , "Set to frame index must be offset, not #{set.index}"
|
||||
end
|
||||
|
||||
def _test_assign_arg
|
||||
def test_assign_arg
|
||||
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :blar)
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main(int blar)
|
||||
blar = 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:assignment, s(:name, :blar), s(:int, 5)))
|
||||
@expect = [Label, LoadConstant, SetSlot, Label, FunctionReturn]
|
||||
was = check
|
||||
set = was.next(2)
|
||||
@ -99,16 +71,10 @@ HERE
|
||||
assert_equal 10, set.index , "Set to args index must be offset, not #{set.index}"
|
||||
end
|
||||
|
||||
def _test_arg_get
|
||||
def test_arg_get
|
||||
# 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)
|
||||
@input = <<HERE
|
||||
class Space
|
||||
int main(int balr)
|
||||
return balr
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@input = s(:statements, s(:return, s(:name, :balr)))
|
||||
@expect = [Label, GetSlot, SetSlot, Label, FunctionReturn]
|
||||
was = check
|
||||
get = was.next(1)
|
||||
|
Loading…
Reference in New Issue
Block a user