Change Mom to SlotMachine

rather large commit, but essentially a simple rename
Rationale in docs and blogs
This commit is contained in:
2019-10-03 20:55:41 +03:00
parent fd8a3e9cc5
commit c43436f35a
170 changed files with 481 additions and 480 deletions

View File

@ -0,0 +1,45 @@
require_relative '../helper'
module SlotMachine
class InstructionMock < Instruction
def initialize
super("mocking")
end
end
# Most SlotMachineInstructionTests test the risc instructions of the mom instruction
# quite carefully, ie every instruction, every register.
#
# This is done with the assert methods in risc_assert
#
# Most tests go through instructions from top to bottom.
# For working code, one can get a list of those instructions by using the all_str as message
# Most tests will test for length and give the all_str as message to see where it went wrong
# like: assert_equal 8 , all.length , all_str
class SlotMachineInstructionTest < MiniTest::Test
include Output
def setup
Parfait.boot!(Parfait.default_test_options)
@instruction = instruction
@compiler = Risc::MethodCompiler.new(Risc::FakeCallable.new , Label.new("source","start"))
@instruction.to_risc(@compiler)
@risc = @compiler.risc_instructions
end
def risc(at)
return @risc if at == 0
@risc.next( at )
end
def all
ret = []
@risc.each {|i| ret << i}
ret
end
def all_str
class_list(all.collect{|i|i.class})
end
end
end

View File

@ -0,0 +1,29 @@
require_relative "helper"
module SlotMachine
class TestArgumentTransfer < SlotMachineInstructionTest
def instruction
receiver = SlotDefinition.new(:message , [:receiver])
arg = SlotLoad.new("test", [:message, :caller] , [:message,:type] )
ArgumentTransfer.new("" , receiver ,[arg])
end
def test_len
assert_equal 6 , all.length , all_str
end
def test_1_slot
assert_slot_to_reg risc(1) ,:r0 , 2 , :r2
end
def test_2_slot
assert_slot_to_reg risc(2) ,:r0 , 1 , :r3
end
def test_3_reg
assert_reg_to_slot risc(3) , :r2 , :r3 , 2
end
def test_4_slot
assert_slot_to_reg risc(4) ,:r0 , 0 , :r2
end
def test_5_reg
assert_reg_to_slot risc(5) , :r2 , :r0 , 6
end
end
end

View File

@ -0,0 +1,35 @@
require_relative "helper"
module SlotMachine
class TesBlockYield < SlotMachineInstructionTest
def instruction
BlockYield.new("source",1)
end
def test_len
assert_equal 8 , all.length , all_str
end
def test_1_slot
assert_slot_to_reg risc(1) ,:r0 , 1 , :r1
end
def test_2_load
assert_load risc(2) , Risc::Label , :r2
assert_label risc(2).constant , "continue_"
end
def test_3_reg
assert_reg_to_slot risc(3) , :r2 , :r1 , 4
end
def test_4_slot
assert_slot_to_reg risc(4) ,:r0 , 9 , :r3
end
def test_5_slot
assert_slot_to_reg risc(5) ,:r0 , 1 , :r0
end
def test_6_jump
assert_equal Risc::DynamicJump , risc(6).class
assert_equal :r3 , risc(6).register.symbol
end
def test_7_label
assert_label risc(7) , "continue_"
end
end
end

View File

@ -0,0 +1,37 @@
require_relative "helper"
module SlotMachine
class TestDynamicCall < SlotMachineInstructionTest
def instruction
DynamicCall.new(nil,nil)
end
def test_len
assert_equal 9 , all.length , all_str
end
def test_1_load
assert_load risc(1) , Risc::Label , :r1
assert_label risc(1).constant , "continue_"
end
def test_2_slot
assert_slot_to_reg risc(2) ,:r0 , 1 , :r2
end
def test_3_reg
assert_reg_to_slot risc(3) , :r1 , :r2 , 4
end
def test_4_slot
assert_slot_to_reg risc(4) ,:r0 , 1 , :r0
end
def test_5_load
assert_load risc(5) , Parfait::CacheEntry , :r3
end
def test_6_slot
assert_slot_to_reg risc(6) ,:r3 , 2 , :r3
end
def test_7_jump
assert_equal Risc::DynamicJump , risc(7).class
end
def test_8_label
assert_label risc(8) , "continue_"
end
end
end

View File

@ -0,0 +1,15 @@
require_relative "helper"
module SlotMachine
class TestJump < SlotMachineInstructionTest
def instruction
Jump.new( Label.new("ok" , "target"))
end
def test_len
assert_equal 2 , all.length , all_str
end
def test_1_slot
assert_branch risc(1) , "target"
end
end
end

View File

@ -0,0 +1,46 @@
require_relative "helper"
module SlotMachine
class TestMessageSetupInt < SlotMachineInstructionTest
def instruction
MessageSetup.new( 1 )
end
def test_len
assert_equal 4 , all.length , all_str
end
def test_1_slot
assert_slot_to_reg risc(1) ,:r0 , 9 , :r1
end
def test_2_slot
assert_slot_to_reg risc(2) ,:r0 , 1 , :r2
end
def test_3_reg
assert_reg_to_slot risc(3) , :r1 , :r2 , 7
end
end
class TestMessageSetupCache < SlotMachineInstructionTest
include Parfait::MethodHelper
def instruction
method = make_method
cache_entry = Parfait::CacheEntry.new(method.frame_type, method)
MessageSetup.new( cache_entry )
end
def test_len
assert_equal 5 , all.length , all_str
end
def test_1_load
assert_load risc(1) , Parfait::CacheEntry , :r1
end
def test_2_slot
assert_slot_to_reg risc(2) ,:r1 , 2 , :r2
end
def test_3_slot
assert_slot_to_reg risc(3) ,:r0 , 1 , :r3
end
def test_4_reg
assert_reg_to_slot risc(4) , :r2 , :r3 , 7
end
end
end

View File

@ -0,0 +1,25 @@
require_relative "helper"
module SlotMachine
class TestNotSameCheck < SlotMachineInstructionTest
def instruction
target = SlotDefinition.new(:message , :caller)
NotSameCheck.new(target , target , Label.new("ok" , "target"))
end
def test_len
assert_equal 5 , all.length , all_str
end
def test_1_slot
assert_slot_to_reg risc(1) ,:r0 , 6 , :r2
end
def test_2_slot
assert_slot_to_reg risc(2) ,:r0 , 6 , :r4
end
def test_3_op
assert_operator risc(3) , :-, :r2 , :r4
end
def test_4_zero
assert_zero risc(4) , "target"
end
end
end

View File

@ -0,0 +1,72 @@
require_relative "helper"
module SlotMachine
class TestResolveMethod < SlotMachineInstructionTest
include Parfait::MethodHelper
def instruction
method = make_method
cache_entry = Parfait::CacheEntry.new(method.frame_type, method)
ResolveMethod.new( "method" , :name , cache_entry )
end
def test_len
assert_equal 19 , all.length , all_str
end
def test_1_load_name
assert_load risc(1) , Symbol , :r1
assert_equal :name , risc(1).constant
end
def test_2_load_cache
assert_load risc(2) , Parfait::CacheEntry , :r2
end
def test_3_get_cache_type
assert_slot_to_reg risc(3) ,:r2 , 1 , :r3
end
def test_4_get_type_methods
assert_slot_to_reg risc(4) ,:r3 , 4 , :r4
end
def test_5_start_label
assert_label risc(5) , "while_start_"
end
def test_6_load_nil
assert_load risc(6) , Parfait::NilClass , :r5
end
def test_7_check_nil
assert_operator risc(7) , :- , :r5 , :r4
end
def test_8_nil_branch
assert_zero risc(8) , "exit_label_"
end
def test_9_get_method_name
assert_slot_to_reg risc(9) ,:r4 , 6 , :r6
end
# Syscall, Label, RegToSlot,] #20
def test_10_check_name
assert_operator risc(10) , :- , :r6 , :r1
end
def test_11_nil_branch
assert_zero risc(11) , "ok_label_"
end
def test_12_get_next_method
assert_slot_to_reg risc(12) ,:r4 , 2 , :r4
end
def test_13_continue_while
assert_branch risc(13) , "while_start_"
end
def test_14_goto_exit
assert_label risc(14) , "exit_label_"
end
def test_15_move_name
assert_transfer( risc(15) , :r1 , :r1)
end
def test_16_die
assert_syscall risc(16) , :died
end
def test_17_label
assert_label risc(17) , "ok_label_"
end
def test_18_load_method
assert_reg_to_slot risc(18) , :r4 , :r2 , 2
end
end
end

View File

@ -0,0 +1,15 @@
require_relative "helper"
module SlotMachine
class TestReturnJump < SlotMachineInstructionTest
def instruction
ReturnJump.new("source",Label.new("ok" , "return"))
end
def test_len
assert_equal 2 , all.length , all_str
end
def test_2_branch
assert_branch risc(1) ,"return"
end
end
end

View File

@ -0,0 +1,34 @@
require_relative "helper"
module SlotMachine
class TestReturnSequence < SlotMachineInstructionTest
def instruction
ReturnSequence.new("source")
end
def test_len
assert_equal 8 , all.length , all_str
end
def test_1_load_return_value
assert_slot_to_reg risc(1) ,:r0 , 5 , :r1
end
def test_2_load_caller
assert_slot_to_reg risc(2) ,:r0 , 6 , :r2
end
def test_3_store_return_in_caller
assert_reg_to_slot risc(3) , :r1 , :r2 , 5
end
def test_4_load_return_address
assert_slot_to_reg risc(4) ,:r0 , 4 , :r3
end
def test_5_get_int_for_address
assert_slot_to_reg risc(5) ,:r3 , 2 , :r3
end
def test_6_swap_messages
assert_slot_to_reg risc(6) ,:r0 , 6 , :r0
end
def test_7_do_return
assert_equal Risc::FunctionReturn , risc(7).class
assert_equal :r3 , risc(7).register.symbol
end
end
end

View File

@ -0,0 +1,34 @@
require_relative "helper"
module SlotMachine
class TestSimpleCall < SlotMachineInstructionTest
include Parfait::MethodHelper
def instruction
SimpleCall.new( make_method )
end
def test_len
assert_equal 7 , all.length , all_str
end
def test_1_load_return_label
assert_load risc(1) , Risc::Label , :r1
assert_label risc(1).constant , "continue_"
end
def test_2_load_next_message
assert_slot_to_reg risc(2) ,:r0 , 1 , :r2
end
def test_3_store_return_address
assert_reg_to_slot risc(3) , :r1 , :r2 , 4
end
def test_4_swap_messages
assert_slot_to_reg risc(4) ,:r0 , 1 , :r0
end
def test_5_call
assert_equal Risc::FunctionCall , risc(5).class
assert_equal :meth , risc(5).method.name
end
def test_6_label
assert_label risc(6) , "continue_"
end
end
end

View File

@ -0,0 +1,23 @@
require_relative "helper"
module SlotMachine
class TestSlotDefinitionBasics < MiniTest::Test
def slot(slot = :caller)
SlotDefinition.new(:message , slot)
end
def test_create_ok1
assert_equal :message , slot.known_object
end
def test_create_ok2
assert_equal Array , slot.slots.class
assert_equal :caller , slot.slots.first
end
def test_to_s
assert_equal "[message, caller]" , slot.to_s
end
def test_create_fail_none
assert_raises {slot(nil)}
end
end
end

View File

@ -0,0 +1,53 @@
require_relative "helper"
module SlotMachine
class TestSlotDefinitionConstant < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
@compiler = Risc::FakeCompiler.new
@definition = SlotDefinition.new(StringConstant.new("hi") , [])
@register = @definition.to_register(@compiler , InstructionMock.new)
@instruction = @compiler.instructions.first
end
def test_def_class
assert_equal Risc::LoadConstant , @instruction.class
end
def test_def_register
assert_equal :r1 , @instruction.register.symbol
end
def test_def_const
assert_equal "hi" , @instruction.constant.to_string
end
def test_to_s
assert_equal "[StringConstant]" , @definition.to_s
end
end
class TestSlotDefinitionConstantType < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
@compiler = Risc::FakeCompiler.new
@definition = SlotDefinition.new(StringConstant.new("hi") , [:type])
@register = @definition.to_register(@compiler , InstructionMock.new)
@instruction = @compiler.instructions.first
end
def test_def_class
assert_equal Risc::LoadConstant , @instruction.class
end
def test_def_register
assert_equal :r1 , @instruction.register.symbol
end
def test_def_const
assert_equal "hi" , @instruction.constant.to_string
end
def test_to_s
assert_equal "[StringConstant, type]" , @definition.to_s
end
def test_def_register2
assert_equal :r1 , @compiler.instructions[1].register.symbol
end
def test_def_next_index
assert_equal 0 , @compiler.instructions[1].index
end
end
end

View File

@ -0,0 +1,28 @@
require_relative "helper"
module SlotMachine
class TestSlotDefinitionKnown1 < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
@compiler = Risc::FakeCompiler.new
@definition = SlotDefinition.new(:message , :caller)
@register = @definition.to_register(@compiler , "fake source")
@instruction = @compiler.instructions.first
end
def test_def_class
assert_equal Risc::SlotToReg , @instruction.class
end
def test_def_next_class
assert_equal NilClass , @instruction.next.class
end
def test_def_array #from message r0
assert_equal :r0 , @instruction.array.symbol
end
def test_def_register # to next free register r1
assert_equal :r1 , @register.symbol
end
def test_def_index # at caller index 6
assert_equal 6 , @instruction.index
end
end
end

View File

@ -0,0 +1,27 @@
require_relative "helper"
module SlotMachine
class TestSlotDefinitionKnown2 < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
@compiler = Risc::FakeCompiler.new
@definition = SlotDefinition.new(:message , [:caller , :type])
@register = @definition.to_register(@compiler , InstructionMock.new)
end
def test_def_next_class
assert_equal Risc::SlotToReg , @compiler.instructions[1].class
end
def test_def_next_next_class
assert_equal NilClass , @compiler.instructions[2].class
end
def test_def_next_index
assert_equal 0 , @compiler.instructions[1].index
end
def test_def_next_register
assert_equal :r1 , @compiler.instructions[1].register.symbol
end
def test_def_next_array
assert_equal :r1 , @compiler.instructions[1].array.symbol
end
end
end

View File

@ -0,0 +1,21 @@
require_relative "helper"
module SlotMachine
class TestSlotLoadBasics < MiniTest::Test
def test_ins_ok
assert SlotLoad.new("test", [:message, :caller] , [:receiver,:type] )
end
def test_ins_fail1
assert_raises {SlotLoad.new( "test",[:message, :caller] , nil )}
end
def test_fail_on_right
@load = SlotLoad.new( "test",[:message, :caller] , [:receiver,:type] )
assert_raises {@load.to_risc(Risc::FakeCompiler.new)}
end
def test_fail_on_left_long
@load = SlotLoad.new("test", [:message, :caller , :type] , [:receiver,:type] )
assert_raises {@load.to_risc(Risc::FakeCompiler.new)}
end
end
end

View File

@ -0,0 +1,39 @@
require_relative "helper"
module SlotMachine
class TestSlotLoad1 < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
load = SlotLoad.new("test", [:message, :caller] , [:message,:type] )
@compiler = Risc::FakeCompiler.new
load.to_risc(@compiler)
@instructions = @compiler.instructions
end
def test_ins_class
assert_equal Risc::SlotToReg , @instructions[0].class
end
def test_ins_next_class
assert_equal Risc::RegToSlot , @instructions[1].class
end
def test_ins_arr
assert_equal :r0 , @instructions[0].array.symbol
end
def test_ins_reg
assert_equal :r1 , @instructions[0].register.symbol
end
def test_ins_index
assert_equal 0 , @instructions[0].index
end
def test_ins_next_reg
assert_equal :r1 , @instructions[1].register.symbol
end
def test_ins_next_arr
assert_equal :r0 , @instructions[1].array.symbol
end
def test_ins_next_index
assert_equal 6 , @instructions[1].index
end
end
end

View File

@ -0,0 +1,54 @@
require_relative "helper"
module SlotMachine
class TestSlotLoad2 < MiniTest::Test
def setup
Parfait.boot!(Parfait.default_test_options)
@compiler = Risc::FakeCompiler.new
load = SlotLoad.new( "test",[:message, :caller, :type] , [:message, :caller , :type] )
load.to_risc(@compiler)
@instructions = @compiler.instructions
end
def test_ins_next_classes
assert_equal Risc::SlotToReg , @instructions[0].class
assert_equal Risc::SlotToReg , @instructions[1].class
assert_equal Risc::SlotToReg , @instructions[2].class
end
def test_ins_next_next_class
assert_equal Risc::RegToSlot , @instructions[3].class
assert_equal NilClass , @instructions[4].class
end
def test_ins_next_reg
assert_equal :r1 , @instructions[1].register.symbol
end
def test_ins_next_arr
assert_equal :r1 , @instructions[1].array.symbol
end
def test_ins_next_index
assert_equal 0 , @instructions[1].index
end
def test_ins_next_2_reg
assert_equal :r1 , @instructions[2].register.symbol
end
def test_ins_next_2_arr
assert_equal :r0 , @instructions[2].array.symbol
end
def test_ins_next_2_index
assert_equal 6 , @instructions[2].index
end
def test_ins_next_3_reg
assert_equal :r1 , @instructions[3].register.symbol
end
def test_ins_next_3_arr
assert_equal :r1 , @instructions[3].array.symbol
end
def test_ins_next_3_index
assert_equal 0 , @instructions[3].index
end
end
end

View File

@ -0,0 +1,51 @@
require_relative "helper"
module SlotMachine
class TestSlotLoad3 < MiniTest::Test
include Parfait::MethodHelper
def setup
Parfait.boot!(Parfait.default_test_options)
method = make_method
@compiler = Risc::FakeCompiler.new
@cache_entry = Parfait::CacheEntry.new(method.frame_type, method)
load = SlotLoad.new("test", [@cache_entry , :cached_type] , [:message, :type] )
load.to_risc(@compiler)
@instructions = @compiler.instructions
end
def test_ins_next_class
assert_equal Risc::SlotToReg , @instructions[0].class
assert_equal Risc::LoadConstant, @instructions[1].class
end
def test_ins_next_class
assert_equal Risc::RegToSlot , @instructions[2].class
assert_equal NilClass , @instructions[3].class
end
def test_ins_load
assert_equal :r1 , @instructions[1].register.symbol
assert_equal Parfait::CacheEntry , @instructions[1].constant.class
end
def test_ins_next_reg
assert_equal :r1 , @instructions[0].register.symbol
end
def test_ins_next_arr
assert_equal :r0 , @instructions[0].array.symbol
end
def test_ins_next_index
assert_equal 0 , @instructions[0].index
end
def test_ins_next_2_reg
assert_equal :r1 , @instructions[2].register.symbol
end
def test_ins_next_2_arr
assert_equal :r1 , @instructions[2].array.symbol
end
def test_ins_next_2_index
assert_equal 1 , @instructions[2].index
end
end
end

View File

@ -0,0 +1,42 @@
require_relative "helper"
module SlotMachine
class TestSameCheck < SlotMachineInstructionTest
def instruction
target = SlotDefinition.new(:message , :caller)
TruthCheck.new(target , Label.new("ok" , "target"))
end
def test_len
assert_equal 8 , all.length , all_str
end
def test_1_slot
assert_slot_to_reg risc(1) ,:r0 , 6 , :r2
end
def test_2_load
assert_load risc(2) , Parfait::FalseClass , :r3
end
def test_3_op
assert_equal Risc::OperatorInstruction , risc(3).class
assert_equal :r3 , risc(3).left.symbol
assert_equal :r2 , risc(3).right.symbol
assert_equal :- , risc(3).operator
end
def test_4_zero
assert_equal Risc::IsZero , risc(4).class
assert_label risc(4).label , "target"
end
def test_5_load
assert_load risc(5) , Parfait::NilClass , :r3
end
def test_6_op
assert_equal Risc::OperatorInstruction , risc(6).class
assert_equal :r3 , risc(6).left.symbol
assert_equal :r2 , risc(6).right.symbol
assert_equal :- , risc(6).operator
end
def test_7_zero
assert_equal Risc::IsZero , risc(7).class
assert_label risc(7).label , "target"
end
end
end