2015-10-15 12:08:53 +02:00
|
|
|
require_relative 'helper'
|
|
|
|
|
|
|
|
module Register
|
|
|
|
class TestAssignStatement < MiniTest::Test
|
|
|
|
include Statements
|
|
|
|
|
|
|
|
def setup
|
2015-10-22 17:16:29 +02:00
|
|
|
Register.machine.boot
|
2015-10-15 12:08:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_op
|
|
|
|
@string_input = <<HERE
|
|
|
|
class Object
|
|
|
|
int main()
|
|
|
|
int n = 10 + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2015-10-18 16:39:35 +02:00
|
|
|
@expect = [[SaveReturn,LoadConstant,LoadConstant,
|
2015-10-18 16:32:32 +02:00
|
|
|
OperatorInstruction,GetSlot,SetSlot],[RegisterTransfer,GetSlot,FunctionReturn]]
|
2015-10-15 12:08:53 +02:00
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_local
|
|
|
|
@string_input = <<HERE
|
|
|
|
class Object
|
|
|
|
int main()
|
|
|
|
int runner
|
|
|
|
runner = 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2015-10-18 16:39:35 +02:00
|
|
|
@expect = [[SaveReturn,LoadConstant,GetSlot,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]]
|
2015-10-15 12:08:53 +02:00
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_local_assign
|
|
|
|
@string_input = <<HERE
|
|
|
|
class Object
|
|
|
|
int main()
|
|
|
|
int runner = 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2015-10-18 16:39:35 +02:00
|
|
|
@expect = [[SaveReturn,LoadConstant, GetSlot,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]]
|
2015-10-15 12:08:53 +02:00
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_call
|
|
|
|
@string_input = <<HERE
|
|
|
|
class Object
|
|
|
|
int main()
|
|
|
|
int r = main()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2015-10-18 16:39:35 +02:00
|
|
|
@expect = [[SaveReturn,GetSlot,GetSlot,SetSlot, LoadConstant,SetSlot,
|
2015-10-18 18:27:46 +02:00
|
|
|
RegisterTransfer,FunctionCall,GetSlot,GetSlot,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]]
|
2015-10-15 12:08:53 +02:00
|
|
|
check
|
|
|
|
end
|
2015-10-22 10:02:57 +02:00
|
|
|
|
|
|
|
def test_frame_get
|
|
|
|
@string_input = <<HERE
|
|
|
|
class Object
|
|
|
|
int main()
|
|
|
|
int r = 5
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@expect = [[SaveReturn,LoadConstant,GetSlot,SetSlot,GetSlot,GetSlot] ,
|
|
|
|
[RegisterTransfer,GetSlot,FunctionReturn]]
|
|
|
|
was = check
|
|
|
|
get = was[0].codes[5]
|
|
|
|
assert_equal GetSlot , get.class
|
2015-10-22 10:48:03 +02:00
|
|
|
assert_equal 3, get.index , "Get to frame index must be offset, not #{get.index}"
|
2015-10-22 10:02:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_arg
|
2015-10-22 17:16:29 +02:00
|
|
|
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :bar)
|
2015-10-22 10:02:57 +02:00
|
|
|
@string_input = <<HERE
|
|
|
|
class Object
|
|
|
|
int main(int bar)
|
|
|
|
bar = 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@expect = [[SaveReturn,LoadConstant,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]]
|
|
|
|
was = check
|
|
|
|
set = was[0].codes[2]
|
|
|
|
assert_equal SetSlot , set.class
|
2015-10-22 10:48:03 +02:00
|
|
|
assert_equal 9, set.index , "Set to args index must be offset, not #{set.index}"
|
2015-10-22 10:02:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assign_int
|
|
|
|
@string_input = <<HERE
|
|
|
|
class Object
|
|
|
|
int main()
|
|
|
|
int r = 5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@expect = [[SaveReturn,LoadConstant,GetSlot,SetSlot] , [RegisterTransfer,GetSlot,FunctionReturn]]
|
|
|
|
was = check
|
|
|
|
set = was[0].codes[3]
|
|
|
|
assert_equal SetSlot , set.class
|
2015-10-22 10:48:03 +02:00
|
|
|
assert_equal 3, set.index , "Set to frame index must be offset, not #{set.index}"
|
2015-10-22 10:02:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_arg_get
|
|
|
|
# have to define bar externally, just because redefining main. Otherwise that would be automatic
|
2015-10-22 17:16:29 +02:00
|
|
|
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :bar)
|
2015-10-22 10:02:57 +02:00
|
|
|
@string_input = <<HERE
|
|
|
|
class Object
|
|
|
|
int main(int bar)
|
|
|
|
return bar
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@expect = [[SaveReturn,GetSlot] ,
|
|
|
|
[RegisterTransfer,GetSlot,FunctionReturn]]
|
|
|
|
was = check
|
|
|
|
get = was[0].codes[1]
|
|
|
|
assert_equal GetSlot , get.class
|
2015-10-22 10:48:03 +02:00
|
|
|
assert_equal 9, get.index , "Get to frame index must be offset, not #{get.index}"
|
2015-10-22 10:02:57 +02:00
|
|
|
end
|
2015-10-15 12:08:53 +02:00
|
|
|
end
|
|
|
|
end
|