2015-10-15 12:08:53 +02:00
|
|
|
require_relative 'helper'
|
|
|
|
|
|
|
|
module Register
|
2017-01-03 21:37:25 +01:00
|
|
|
class TestAssignStatement < MiniTest::Test
|
|
|
|
include Statements
|
2015-10-15 12:08:53 +02:00
|
|
|
|
2017-01-03 21:37:25 +01:00
|
|
|
def test_assign_op
|
|
|
|
Parfait.object_space.get_main.add_local(:r , :Integer)
|
2016-12-17 12:12:49 +01:00
|
|
|
|
2017-01-03 21:37:25 +01:00
|
|
|
@input = s(:statements, s(:assignment, s(:name, :r), s(:operator_value, :+, s(:int, 10), s(:int, 1))))
|
2016-12-10 17:06:57 +01:00
|
|
|
|
2017-01-04 20:38:38 +01:00
|
|
|
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, SlotToReg, RegToSlot ,
|
|
|
|
LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
|
|
|
assert_nil msg = check_nil , msg
|
2017-01-03 21:37:25 +01:00
|
|
|
end
|
2015-10-22 10:02:57 +02:00
|
|
|
|
2017-01-03 21:37:25 +01:00
|
|
|
def test_assign_local
|
|
|
|
Parfait.object_space.get_main.add_local(:r , :Integer)
|
|
|
|
@input =s(:statements, s(:assignment, s(:name, :r), s(:int, 5)))
|
2015-10-22 10:02:57 +02:00
|
|
|
|
2017-01-04 20:38:38 +01:00
|
|
|
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
|
|
|
|
RegToSlot, Label, FunctionReturn]
|
|
|
|
assert_nil msg = check_nil , msg
|
2017-01-03 21:37:25 +01:00
|
|
|
end
|
2016-12-28 18:20:16 +01:00
|
|
|
|
2017-01-03 21:37:25 +01:00
|
|
|
def test_assign_local_assign
|
|
|
|
Parfait.object_space.get_main.add_local(:r , :Integer)
|
2015-10-22 10:02:57 +02:00
|
|
|
|
2017-01-03 21:37:25 +01:00
|
|
|
@input = s(:statements, s(:assignment, s(:name, :r), s(:int, 5)))
|
2016-12-28 18:20:16 +01:00
|
|
|
|
2017-01-04 20:38:38 +01:00
|
|
|
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
|
|
|
|
RegToSlot, Label, FunctionReturn]
|
|
|
|
assert_nil msg = check_nil , msg
|
2017-01-03 21:37:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_call
|
|
|
|
Parfait.object_space.get_main.add_local(:r , :Integer)
|
|
|
|
@input = s(:statements, s(:assignment, s(:name, :r), s(:call, s(:name, :main), s(:arguments))))
|
2017-01-04 20:38:38 +01:00
|
|
|
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
|
|
|
|
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RegisterTransfer ,
|
|
|
|
FunctionCall, Label, RegisterTransfer, SlotToReg, SlotToReg, SlotToReg ,
|
|
|
|
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
|
|
|
assert_nil msg = check_nil , msg
|
2017-01-03 21:37:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_named_list_get
|
|
|
|
Parfait.object_space.get_main.add_local(:r , :Integer)
|
|
|
|
@input = s(:statements, s(:assignment, s(:name, :r), s(:int, 5)), s(:return, s(:name, :r)))
|
2017-01-04 20:38:38 +01:00
|
|
|
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg ,
|
|
|
|
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
|
|
|
was = check_return
|
2017-01-03 21:37:25 +01:00
|
|
|
get = was.next(5)
|
|
|
|
assert_equal SlotToReg , get.class
|
|
|
|
assert_equal 1 + 1, get.index , "Get to named_list index must be offset, not #{get.index}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_local_int
|
|
|
|
Parfait.object_space.get_main.add_local(:r , :Integer)
|
|
|
|
@input = s(:statements, s(:assignment, s(:name, :r), s(:int, 5)) )
|
2017-01-04 20:38:38 +01:00
|
|
|
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
|
|
|
|
RegToSlot, Label, FunctionReturn]
|
|
|
|
was = check_return
|
2017-01-03 21:37:25 +01:00
|
|
|
set = was.next(3)
|
|
|
|
assert_equal RegToSlot , set.class
|
|
|
|
assert_equal 1 + 1, set.index , "Set to named_list index must be offset, not #{set.index}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_misassign_local
|
|
|
|
Parfait.object_space.get_main.add_local(:r , :Integer)
|
|
|
|
@input = s(:statements, s(:assignment, s(:name, :r), s(:string, "5")) )
|
|
|
|
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
|
|
|
assert_raises {check }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_arg
|
|
|
|
Parfait.object_space.get_main.add_argument(:blar , :Integer)
|
|
|
|
@input = s(:statements, s(:assignment, s(:name, :blar), s(:int, 5)))
|
2017-01-04 20:38:38 +01:00
|
|
|
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
|
|
|
|
RegToSlot, Label, FunctionReturn]
|
|
|
|
was = check_return
|
2017-01-03 21:37:25 +01:00
|
|
|
set = was.next(3)
|
|
|
|
assert_equal RegToSlot , set.class
|
|
|
|
assert_equal 1 + 1, set.index , "Set to args index must be offset, not #{set.index}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_misassign_arg
|
|
|
|
Parfait.object_space.get_main.add_argument(:blar , :Integer)
|
|
|
|
@input = s(:statements, s(:assignment, s(:name, :blar), s(:string, "5")))
|
|
|
|
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
|
|
|
|
assert_raises {check }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_arg_get
|
|
|
|
# have to define bar externally, just because redefining main. Otherwise that would be automatic
|
|
|
|
Parfait.object_space.get_main.add_argument(:balr , :Integer)
|
|
|
|
@input = s(:statements, s(:return, s(:name, :balr)))
|
2017-01-04 20:38:38 +01:00
|
|
|
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
|
|
|
|
RegToSlot, Label, FunctionReturn]
|
|
|
|
was = check_return
|
2017-01-03 21:37:25 +01:00
|
|
|
get = was.next(2)
|
|
|
|
assert_equal SlotToReg , get.class
|
|
|
|
assert_equal 1 + 1, get.index , "Get to args index must be offset, not #{get.index}"
|
|
|
|
end
|
2015-10-22 10:02:57 +02:00
|
|
|
end
|
2015-10-15 12:08:53 +02:00
|
|
|
end
|