2018-04-06 13:21:38 +02:00
|
|
|
require_relative "../helper"
|
|
|
|
|
|
|
|
module Risc
|
2018-06-29 12:27:57 +02:00
|
|
|
class TestCodeBuilder < MiniTest::Test
|
2018-04-06 13:54:24 +02:00
|
|
|
|
|
|
|
def setup
|
2019-02-08 22:03:23 +01:00
|
|
|
Parfait.boot!(Parfait.default_test_options)
|
2018-07-01 13:12:42 +02:00
|
|
|
Risc.boot!
|
2019-10-03 19:55:41 +02:00
|
|
|
label = SlotMachine::Label.new( "source_name", "return_label")
|
2019-09-15 11:58:43 +02:00
|
|
|
@builder = Risc::MethodCompiler.new( FakeCallable.new ,label).builder("source")
|
2018-05-29 19:26:00 +02:00
|
|
|
@label = Risc.label("source","name")
|
2018-08-19 12:16:07 +02:00
|
|
|
@start = @builder.compiler.current
|
|
|
|
end
|
|
|
|
def built
|
|
|
|
@start.next
|
2018-04-06 13:54:24 +02:00
|
|
|
end
|
2018-04-06 23:35:54 +02:00
|
|
|
def test_has_build
|
|
|
|
assert @builder.respond_to?(:build)
|
|
|
|
end
|
|
|
|
def test_has_attribute
|
|
|
|
assert_nil @builder.built
|
|
|
|
end
|
2018-04-06 21:54:54 +02:00
|
|
|
def test_alloc_space
|
2018-08-14 18:39:46 +02:00
|
|
|
reg = @builder.space!
|
2018-06-29 10:39:07 +02:00
|
|
|
assert_equal RegisterValue , reg.class
|
2018-07-15 14:16:12 +02:00
|
|
|
assert_equal :Space , reg.type.class_name
|
2018-04-06 15:35:27 +02:00
|
|
|
end
|
2018-08-14 18:39:46 +02:00
|
|
|
def test_not_alloc_space
|
|
|
|
assert_raises {@builder.space}
|
|
|
|
end
|
2018-11-21 10:12:39 +01:00
|
|
|
def test_reset
|
2020-03-01 09:22:24 +01:00
|
|
|
assert_equal :integer , @builder.integer!.symbol
|
2018-11-21 10:12:39 +01:00
|
|
|
@builder.reset_names
|
2020-03-01 09:22:24 +01:00
|
|
|
assert_equal :integer , @builder.integer!.symbol # would raise if it existed
|
2018-11-21 10:12:39 +01:00
|
|
|
end
|
2018-04-06 21:54:54 +02:00
|
|
|
def test_next_message
|
2018-08-14 18:39:46 +02:00
|
|
|
reg = @builder.next_message!
|
2020-03-01 09:22:24 +01:00
|
|
|
assert_equal :next_message , reg.symbol
|
2018-07-15 14:16:12 +02:00
|
|
|
assert_equal :Message , reg.type.class_name
|
2018-04-06 15:35:27 +02:00
|
|
|
end
|
2018-04-06 21:54:54 +02:00
|
|
|
def test_message
|
|
|
|
reg = @builder.message
|
|
|
|
assert_equal :r0 , reg.symbol
|
2018-07-15 14:16:12 +02:00
|
|
|
assert_equal :Message , reg.type.class_name
|
2018-04-06 21:54:54 +02:00
|
|
|
end
|
2018-04-06 15:35:27 +02:00
|
|
|
def test_returns_built
|
2018-06-29 10:39:07 +02:00
|
|
|
r1 = RegisterValue.new(:r1 , :Space)
|
2018-08-19 12:16:07 +02:00
|
|
|
@builder.build{ space! << r1 }
|
2018-04-06 13:54:24 +02:00
|
|
|
assert_equal Transfer , built.class
|
|
|
|
end
|
2018-04-06 15:35:27 +02:00
|
|
|
def test_returns_two
|
2018-06-29 10:39:07 +02:00
|
|
|
r1 = RegisterValue.new(:r1 , :Space)
|
2018-08-19 12:16:07 +02:00
|
|
|
@builder.build{ space! << r1 ; space << r1}
|
2018-04-06 15:35:27 +02:00
|
|
|
assert_equal Transfer , built.next.class
|
|
|
|
end
|
|
|
|
def test_returns_slot
|
2020-03-01 09:22:24 +01:00
|
|
|
r2 = RegisterValue.new(:message , :Message).set_builder( @builder )
|
2018-09-01 10:28:53 +02:00
|
|
|
@builder.build{ r2 << factory![:next_object] }
|
2018-04-06 15:35:27 +02:00
|
|
|
assert_equal SlotToReg , built.class
|
2020-03-01 09:22:24 +01:00
|
|
|
assert_equal :factory , built.array.symbol
|
2018-04-06 13:54:24 +02:00
|
|
|
end
|
2020-03-01 09:22:24 +01:00
|
|
|
def pest_returns_slot_reverse
|
2018-08-14 19:08:58 +02:00
|
|
|
r2 = RegisterValue.new(:r2 , :Message).set_builder( @builder )
|
2018-09-01 10:28:53 +02:00
|
|
|
@builder.build{ r2 << factory![:next_object] }
|
2018-04-06 20:05:26 +02:00
|
|
|
assert_equal SlotToReg , built.class
|
2020-03-01 09:22:24 +01:00
|
|
|
assert_equal :factory , built.array.symbol
|
2018-04-06 20:05:26 +02:00
|
|
|
end
|
2018-04-06 19:55:21 +02:00
|
|
|
def test_reuses_names
|
2018-06-29 10:39:07 +02:00
|
|
|
r1 = RegisterValue.new(:r1 , :Space)
|
2018-08-19 12:16:07 +02:00
|
|
|
@builder.build{ space! << r1 ; space << r1}
|
2018-04-06 19:55:21 +02:00
|
|
|
assert_equal built.to.symbol , built.next.to.symbol
|
|
|
|
end
|
2018-04-06 21:54:54 +02:00
|
|
|
def test_uses_message_as_message
|
2018-06-29 10:39:07 +02:00
|
|
|
r1 = RegisterValue.new(:r1 , :Space)
|
2018-08-19 12:16:07 +02:00
|
|
|
@builder.build{ message[:receiver] << r1}
|
2018-04-06 21:54:54 +02:00
|
|
|
assert_equal RegToSlot , built.class
|
|
|
|
assert_equal :r0 , built.array.symbol
|
|
|
|
end
|
2018-04-07 23:39:35 +02:00
|
|
|
def test_label
|
|
|
|
label = @builder.exit_label
|
|
|
|
assert_equal Label , label.class
|
|
|
|
assert label.name.index("exit")
|
|
|
|
end
|
|
|
|
def test_two_label
|
|
|
|
label1 = @builder.exit_label
|
|
|
|
label2 = @builder.exit_label
|
|
|
|
assert_equal label1 , label2
|
|
|
|
end
|
2018-04-07 23:50:51 +02:00
|
|
|
def test_if_zero
|
|
|
|
ret = @builder.if_zero @label
|
|
|
|
assert_equal IsZero , ret.class
|
|
|
|
assert_equal @label , ret.label
|
|
|
|
end
|
|
|
|
def test_if_not_zero
|
|
|
|
ret = @builder.if_not_zero @label
|
|
|
|
assert_equal IsNotZero , ret.class
|
|
|
|
assert_equal @label , ret.label
|
|
|
|
end
|
|
|
|
def test_branch
|
|
|
|
ret = @builder.branch @label
|
|
|
|
assert_equal Branch , ret.class
|
|
|
|
assert_equal @label , ret.label
|
|
|
|
end
|
2018-04-08 00:01:24 +02:00
|
|
|
def test_minus
|
2018-08-14 18:39:46 +02:00
|
|
|
op = @builder.build {space! - callable_method!}
|
2018-04-08 00:01:24 +02:00
|
|
|
assert_equal OperatorInstruction , op.class
|
|
|
|
assert_equal :- , op.operator
|
2018-07-15 14:16:12 +02:00
|
|
|
assert_equal :Space , op.left.type.class_name
|
2018-04-08 00:01:24 +02:00
|
|
|
end
|
2018-04-06 13:21:38 +02:00
|
|
|
end
|
|
|
|
end
|