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 @@
require_relative '../helper'

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

View File

@ -0,0 +1,10 @@
# Builtin Testing
At the Module level (word/object/integer) mostly testing that
- functions exist
- they compile
- basic length tests (no "contents")
Minimal tests for risc compilation, and again length only (should be array too)
Functionality is tested by interpreter over in interpreter dir

View File

@ -0,0 +1,17 @@
require_relative "../helper"
module Mom
module Builtin
class BootTest < MiniTest::Test
include Preloader
def get_compiler(clazz , name)
compiler = RubyX::RubyXCompiler.new(RubyX.default_test_options)
coll = compiler.ruby_to_mom( get_preload("Space.main;#{clazz}.#{name}") )
@method = coll.method_compilers.last_compiler
@method
end
end
end
end

View File

@ -0,0 +1,36 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestIntComp1Risc < BootTest
def setup
@method = get_compiler("Integer",:lt)
end
def test_slot_length
assert_equal :< , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 26 , @method.to_risc.risc_instructions.length
end
end
class TestIntComp2Risc < BootTest
def setup
@method = get_compiler("Integer",:gt)
end
def test_slot_length
assert_equal :> , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 26 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,21 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestIntDiv10Risc < BootTest
def setup
@method = get_compiler("Integer",:div10)
end
def test_slot_length
assert_equal :div10 , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 70 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,22 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestIntDiv4Risc < BootTest
def setup
@method = get_compiler("Integer",:div4)
end
def test_slot_length
assert_equal :div4 , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
assert_equal :div4 , @method.callable.name
end
def test_risc_length
assert_equal 41 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,22 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestObjectExitRisc < BootTest
def setup
super
@method = get_compiler("Object",:exit)
end
def test_slot_length
assert_equal :exit , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 40 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,22 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestWordGetRisc < BootTest
def setup
super
@method = get_compiler("Word",:get)
end
def test_slot_length
assert_equal :get_internal_byte , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 41 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,22 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestWordGetRisc < BootTest
def setup
super
@method = get_compiler("Object",:get)
end
def test_slot_length
assert_equal :get_internal_word , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 18 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,23 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestObjectInitRisc < BootTest
def setup
compiler = RubyX::RubyXCompiler.new(RubyX.default_test_options)
coll = compiler.ruby_to_slot( get_preload("Space.main") )
@method = SlotCollection.create_init_compiler
end
def test_slot_length
assert_equal :__init__ , @method.callable.name
assert_equal 2 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 19 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,21 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestObjectMissingRisc < BootTest
def setup
@method = get_compiler("Object",:missing)
end
def test_slot_length
assert_equal :method_missing , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 15 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,36 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestIntOpPl < BootTest
def setup
@method = get_compiler("Integer",:and)
end
def test_slot_length
assert_equal :& , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 42 , @method.to_risc.risc_instructions.length
end
end
class TestIntOpMM < BootTest
def setup
@method = get_compiler("Integer",:or)
end
def test_slot_length
assert_equal :| , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 42 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,21 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestWordPutRisc < BootTest
def setup
@method = get_compiler("Word",:put)
end
def test_slot_length
assert_equal :putstring , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 44 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,22 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestWordSetRisc < BootTest
def setup
super
@method = get_compiler("Word",:set)
end
def test_slot_length
assert_equal :set_internal_byte , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 20 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,21 @@
require_relative "helper"
module SlotMachine
module Builtin
class TestWordSetRisc < BootTest
def setup
@method = get_compiler("Word",:set)
end
def test_slot_length
assert_equal :set_internal_byte , @method.callable.name
assert_equal 7 , @method.slot_instructions.length
end
def test_compile
assert_equal Risc::MethodCompiler , @method.to_risc.class
end
def test_risc_length
assert_equal 20 , @method.to_risc.risc_instructions.length
end
end
end
end

View File

@ -0,0 +1,44 @@
require_relative "helper"
module SlotMachine
class TestBlockCompiler1 < MiniTest::Test
include ScopeHelper
def setup
code = as_main_block("return 5" , "a = 1")
@risc = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_risc(code)
end
def test_collection
assert_equal Risc::RiscCollection, @risc.class
end
def test_main_compiler
assert_equal :main , @risc.method_compilers.callable.name
end
def test_main_block_compiler
main_block = @risc.method_compilers.find_compiler_name(:main_block)
assert_equal :main_block , main_block.callable.name
assert_equal :main , main_block.in_method.name
end
end
class TestBlockCompiler2 < MiniTest::Test
include ScopeHelper
def setup
code = as_main_block("return arg" , "arg = 1")
@risc = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_risc(code)
end
def test_collection
assert_equal Risc::RiscCollection, @risc.class
end
def test_main_compiler
assert_equal :main , @risc.method_compilers.callable.name
end
def test_main_block_compiler
main_block = @risc.method_compilers.find_compiler_name(:main_block)
assert_equal :main_block , main_block.callable.name
assert_equal :main , main_block.in_method.name
end
end
end

View File

@ -0,0 +1,54 @@
require_relative "helper"
module Vool
class TestBlockArg < MiniTest::Test
include SlotMachineCompile
def setup
@ret = compile_slot( as_main("self.main {|elem| elem = 5 } "))
end
def test_is_compiler
assert_equal SlotMachine::SlotCollection , @ret.class
end
def test_has_method
assert_equal Parfait::CallableMethod , @ret.method_compilers.get_method.class
end
def test_method_has_block
assert @ret.method_compilers.get_method.blocks , "No block created"
end
end
class TestBlockLocal < MiniTest::Test
include SlotMachineCompile
def setup
@ret = compile_slot( as_main("self.main {|elem| local = 5 } "))
@block = @ret.method_compilers.get_method.blocks
end
def test_block_arg_type
assert_equal Parfait::Type, @block.arguments_type.class
end
def test_block_arg_type_name
assert_equal 1, @block.arguments_type.variable_index(:elem)
end
def test_block_local_type
assert_equal Parfait::Type, @block.frame_type.class
end
def test_block_frame_type_name
assert_equal 1, @block.frame_type.variable_index(:local)
end
end
class TestBlockMethodArg < MiniTest::Test
include SlotMachineCompile
def setup
end
def test_method_arg_compiles
ret = compile_slot( as_main("self.main {|elem| arg = 5 } "))
assert ret
end
def test_method_local_compiles
ret = compile_slot( as_main("local = 5 ; self.main {|elem| local = 10 } "))
assert ret
end
end
end

View File

@ -0,0 +1,30 @@
require_relative "helper"
module SlotMachine
class FakeCallableCompiler < CallableCompiler
def source_name
"luke"
end
end
class TestCallableCompiler < MiniTest::Test
def setup
@compiler = FakeCallableCompiler.new(Risc::FakeCallable.new)
end
def test_ok
assert @compiler
end
def test_current
assert @compiler.current
end
def test_current_label
assert_equal Label , @compiler.current.class
assert_equal @compiler.source_name , @compiler.current.name
end
def test_slot
assert @compiler.slot_instructions
end
def test_const
assert_equal Array , @compiler.constants.class
end
end
end

View File

@ -0,0 +1,24 @@
require_relative "helper"
module Vool
class TestClassStatementSlotMachine < MiniTest::Test
include SlotMachineCompile
def setup
@ret = compile_slot( as_main("return 1"))
end
def test_return_class
assert_equal SlotMachine::SlotCollection , @ret.class
end
def test_has_compilers
assert_equal SlotMachine::MethodCompiler , @ret.method_compilers.class
end
def test_constant
assert @ret.method_compilers.add_constant( Parfait::Integer.new(5) )
end
end
end

View File

@ -0,0 +1,20 @@
require_relative "helper"
module SlotMachine
class TestInstruction < MiniTest::Test
def test_instantiates
assert Instruction.new("Hi")
end
def test_string_source
assert_equal "Hi" ,Instruction.new("Hi").source
end
def test_nil_next
assert_nil Instruction.new("Hi").next
end
def test_raise
assert_raises {Instruction.new(5)}
end
end
end

View File

@ -0,0 +1,76 @@
require_relative "helper"
module SlotMachine
class TestMethodCompiler < MiniTest::Test
include ScopeHelper
def setup
end
def in_test_vool(str)
vool = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_vool(in_Test(str))
vool.to_parfait
vool.to_slot(nil)
vool
end
def create_method(body = "@ivar = 5;return")
in_test_vool("def meth; #{body};end")
test = Parfait.object_space.get_class_by_name(:Test)
test.get_instance_method(:meth)
end
def test_method_has_source
method = create_method
assert_equal Vool::Statements , method.source.class
end
def test_method_has_no_locals
method = create_method
assert_equal 1 , method.frame_type.instance_length
end
def test_method_has_no_args
method = create_method
assert_equal 1 , method.args_type.instance_length
end
def test_creates_method_in_class
method = create_method
assert method , "No method created"
assert_equal Parfait::VoolMethod , method.class
end
def test_creates_method_statement_in_class
clazz = in_test_vool("def meth; @ivar = 5 ;return;end")
assert_equal Vool::Statements , clazz.body.class
assert_equal Vool::MethodExpression , clazz.body.first.class
end
def test_callable_method_instance_type
in_test_vool("def meth; @ivar = 5; @ibar = 4;return;end")
test = Parfait.object_space.get_class_by_name(:Test)
method = test.instance_type.get_method(:meth)
assert_equal 1, method.self_type.variable_index(:ivar)
assert_equal 2, method.self_type.variable_index(:ibar)
end
def test_callable_method_has_one_local
in_test_vool("def meth; local = 5 ; a = 6;return;end")
test = Parfait.object_space.get_class_by_name(:Test)
method = test.get_instance_method(:meth)
assert_equal 3 , method.frame_type.instance_length
assert_equal 1 , method.frame_type.variable_index(:local)
assert_equal 2 , method.frame_type.variable_index(:a)
end
def constant_setup(input)
mom = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_slot(in_Test(input))
assert_equal SlotMachine::SlotCollection , mom.class
compiler = mom.method_compilers
assert_equal SlotMachine::MethodCompiler , compiler.class
compiler
end
def test_return_label
compiler = constant_setup("def meth; return 'Hi';end")
assert_equal "return_label", compiler.return_label.name
end
end
end

View File

@ -0,0 +1,49 @@
require_relative "helper"
module SlotMachine
class TestSlotCollection < MiniTest::Test
include SlotMachineCompile
def setup
@comp = compile_slot( "class Test ; def main(); return 'Hi'; end; end;")
end
def test_class
assert_equal SlotCollection , @comp.class
end
def test_compilers
assert_equal 3 , @comp.compilers.num_compilers
end
def test_init_compilers
assert_equal SlotCollection , @comp.init_compilers.class
end
def test_compilers_bare
assert_equal 2 , SlotCollection.new.compilers.num_compilers
end
def test_append_class
assert_equal SlotCollection, (@comp.append @comp).class
end
end
class TestSlotCollectionToRisc < MiniTest::Test
include SlotMachineCompile
def setup
@comp = compile_slot( "class Space ; def main(); return 'Hi'; end; end;")
@collection = @comp.to_risc()
end
def compiler
@collection.method_compilers
end
def test_has_to_risc
assert_equal Risc::RiscCollection, @collection.class
end
def test_has_risc_compiler
assert_equal Risc::MethodCompiler, compiler.class
assert_equal 3, @collection.method_compilers.num_compilers
end
def test_has_risc_instructions
assert_equal Risc::Label, compiler.risc_instructions.class
assert_equal 13, compiler.risc_instructions.length
end
end
end