SlotLanguage reborn in the Machine

Just added the compiler, that can parse Slot directly into SlotMachine code (no language layer in between)
Still unclear wheather the Maker is a thing, but since it was in the Language layer i did not remove it (yet)
Otherwise just the compiler and all the tests, moved from the slot_language.
This commit is contained in:
2020-02-19 02:19:14 +07:00
parent d751c53d1d
commit 8df2e4bf08
14 changed files with 490 additions and 1 deletions

View File

@ -0,0 +1,2 @@
start_label
a = b

View File

@ -0,0 +1,66 @@
require_relative "../helper"
module SlotMachine
class TestAssignment < MiniTest::Test
include SlotHelper
def compile_assign(str)
assign = compile(str)
assert_equal SlotLoad , assign.class
assign
end
def test_slot_load_rinst
assign = compile_assign("a = @b")
assert_equal "receiver.a" , assign.left.slots.to_s
assert_equal "receiver.b" , assign.right.slots.to_s
end
def test_slot_load_linst
assign = compile_assign("@a = b")
assert_equal "receiver.a" , assign.left.slots.to_s
assert_equal "b" , assign.right.slots.to_s
end
def test_slot_load_lrinst
assign = compile_assign("@a = @b")
assert_equal "receiver.a" , assign.left.slots.to_s
assert_equal "receiver.b" , assign.right.slots.to_s
end
def test_assign
assign = compile_assign("a = b")
assert_equal "receiver.a" , assign.left.slots.to_s
assert_equal "b" , assign.right.slots.to_s
end
end
class TestAssignment2 < MiniTest::Test
include SlotHelper
def test_slot_load_linst_trav
assert_equal SlotLoad , compile_class("@a = b.c")
end
def test_assign1
assign = compile("c = c.next")
assert_equal SlotLoad , assign.class
end
def test_shift
load = compile("a = b.c")
assert_equal SlotLoad , load.class
assert_equal "receiver.a" , load.left.slots.to_s
assert_equal "b.c" , load.right.slots.to_s
end
end
class TestAssignment3 < MiniTest::Test
include SlotHelper
def test_inst_ass
assign = compile("@a.b = c")
assert_equal SlotLoad , assign.class
assert_equal SlottedMessage , assign.left.class
assert_equal "receiver.a.b" , assign.left.slots.to_s
end
def test_local_ass
assign = compile("a.b = c")
assert_equal SlotLoad , assign.class
assert_equal SlottedMessage , assign.left.class
assert_equal "a.b" , assign.left.slots.to_s
assert_equal "c" , assign.right.slots.to_s
end
end
end

View File

@ -0,0 +1,83 @@
require_relative "../helper"
module SlotMachine
class TestEqualGoto < MiniTest::Test
include SlotHelper
def do_check(code)
check = compile(code)
assert_equal SameCheck , check.class
assert_equal Label , check.false_label.class
assert check.left.is_a?(Slotted)
assert check.right.is_a?(Slotted)
check
end
def test_equal_local
check = do_check("goto(exit_label) if(a == b)")
assert_equal "message.a" , check.left.to_s
assert_equal "message.b" , check.right.to_s
end
def test_equal_inst_left
check = do_check("goto(exit_label) if(@a == b)")
assert_equal "message.receiver.a" , check.left.to_s
assert_equal "message.b" , check.right.to_s
end
def test_equal_inst_right
check = do_check("goto(exit_label) if(a == @b)")
assert_equal "message.a" , check.left.to_s
assert_equal "message.receiver.b" , check.right.to_s
end
end
class TestEqualGotoFull < MiniTest::Test
include SlotHelper
def setup
@expr = compile("start_label;goto(start_label) if( b == c)")
end
def test_label
assert_equal SlotMachine::Label , @expr.class
assert_equal :start_label , @expr.name
end
def test_conditional
assert_equal SameCheck , @expr.last.class
assert_equal :start_label , @expr.last.false_label.name
end
def test_same_label
assert_equal @expr.object_id , @expr.next.false_label.object_id
end
def test_expression_left
assert_equal SlottedMessage , @expr.last.left.class
assert_equal "message.b" , @expr.last.left.to_s
end
def test_expression_right
assert_equal SlottedMessage , @expr.last.right.class
assert_equal "message.c" , @expr.last.right.to_s
end
end
class TestEqualGotoChain < MiniTest::Test
include SlotHelper
def setup
@expr = compile("goto(start_label) if( a.b == c)")
end
def test_eq
assert_equal SameCheck , @expr.class
end
def test_left
assert_equal SlottedMessage , @expr.left.class
assert_equal "message.a.b" , @expr.left.to_s
end
end
class TestEqualGotoChain2 < MiniTest::Test
include SlotHelper
def setup
@expr = compile("goto(start_label) if( a == @b.c)")
end
def test_eq
assert_equal SameCheck , @expr.class
end
def test_right
assert_equal SlottedMessage , @expr.right.class
assert_equal "message.receiver.b.c" , @expr.right.to_s
end
end
end

View File

@ -0,0 +1,36 @@
require_relative "../helper"
module SlotMachine
class TestGoto < MiniTest::Test
include SlotHelper
def test_goto_class
assert_equal Jump , compile_class("goto(exit_label)")
end
def test_goto_label
goto = compile("goto(exit_label)")
assert_equal Jump , goto.class
assert_equal :exit_label , goto.label.name
end
def test_label
label = compile("while_label")
assert_equal SlotMachine::Label , label.class
assert_equal :while_label , label.name
end
def test_2_label
labels = compile("exit_label;exit_label")
assert_equal :exit_label , labels.name
assert_equal :exit_label , labels.next.name
assert_equal labels.object_id , labels.next.object_id
end
def test_goto_with_label
gotos = compile("exit_label;goto(exit_label)")
assert_equal :exit_label , gotos.name
assert_equal :exit_label , gotos.next.label.name
assert_equal gotos.object_id , gotos.next.label.object_id
end
end
end

View File

@ -0,0 +1,17 @@
require_relative "../helper"
module SlotMachine
class TestSlotCompiler < MiniTest::Test
include SlotHelper
def test_init
assert SlotCompiler.new
end
def test_labels
assert SlotCompiler.new.labels.empty?
end
def test_basic_compile
assert_equal SlottedMessage , compile("a").class
end
end
end

View File

@ -0,0 +1,38 @@
require_relative "../helper"
module SlotMachine
class TestVariable < MiniTest::Test
include SlotHelper
def compile_var(str)
var = compile(str)
assert var.is_a?(Slotted) , "Was #{var.class}"
var
end
def test_local
assert_equal SlottedMessage , compile_var("a").class
end
def test_inst
var = compile_var("@a")
assert_equal SlottedMessage , var.class
end
def test_local_chain
chain = compile_var("a.b")
assert_equal Slot , chain.slots.class
assert_equal :b , chain.slots.next_slot.name
end
def test_local_chain2
chain = compile_var("a.b.c")
assert_equal Slot , chain.slots.next_slot.next_slot.class
assert_equal :c , chain.slots.next_slot.next_slot.name
end
def test_inst_chain
chain = compile_var("@a.b")
assert_equal SlottedMessage , chain.class
assert_equal Slot , chain.slots.class
assert_equal :receiver , chain.slots.name
assert_equal Slot , chain.slots.class
assert_equal :a , chain.slots.next_slot.name
assert_equal :b , chain.slots.next_slot.next_slot.name
end
end
end

View File

@ -6,4 +6,18 @@ module SlotMachine
super("mocking")
end
end
module SlotHelper
def compile(input)
SlotCompiler.compile(input)
end
def compile_class(input)
compile(input).class
end
end
module SlotToHelper
def setup
Parfait.boot!({})
@compiler = SlotMachine::SlotCollection.compiler_for( :Space , :main,{},{})
end
end
end

View File

@ -0,0 +1,47 @@
require_relative "helper"
module SlotMachine
class TestMacroMakerLoad < MiniTest::Test
include SlotHelper
def check_mini(maker)
assert_equal MacroMaker , maker.class
assert_equal SlotMachine::Label , maker.instructions.class
end
def mini_file
File.read(File.expand_path( "../codes/mini.slot" , __FILE__))
end
def test_mini_string
check_mini MacroMaker.load_string( mini_file )
end
def test_mini_source
check_mini MacroMaker.new( SlotCompiler.compile(mini_file))
end
end
class TestMacroMakerLoad2 < MiniTest::Test
def setup
@macro = MacroMaker.load_string( mini_file )
@instructions = @macro.instructions
end
def test_label
assert_equal SlotMachine::Label , @macro.instructions.class
end
def test_assign
assert_equal SlotMachine::SlotLoad , @instructions.next.class
assert_equal "message.receiver.a" , @instructions.next.left.to_s
assert_equal "message.b" , @instructions.next.right.to_s
end
def test_length
assert @instructions.next
assert_nil @instructions.next.next
end
def mini_file
File.read(File.expand_path( "../codes/mini.slot" , __FILE__))
end
end
end

View File

@ -0,0 +1,17 @@
require_relative "helper"
module SlotMachine
class TestSlotCompiler < MiniTest::Test
include SlotHelper
def test_init
assert SlotCompiler.new
end
def test_labels
assert SlotCompiler.new.labels.empty?
end
def test_basic_compile
assert_equal SlottedMessage , compile("a").class
end
end
end