introduce goto instead of using machines jump
fix labels so compiler does not return duplicates
This commit is contained in:
36
test/slot_language/test_goto.rb
Normal file
36
test/slot_language/test_goto.rb
Normal file
@ -0,0 +1,36 @@
|
||||
require_relative "helper"
|
||||
|
||||
module SlotLanguage
|
||||
class TestGoto < MiniTest::Test
|
||||
include SlotHelper
|
||||
|
||||
def test_goto_class
|
||||
assert_equal Goto , compile_class("goto(exit_label)")
|
||||
end
|
||||
def test_goto_label
|
||||
goto = compile("goto(exit_label)")
|
||||
assert_equal SlotMachine::Label , goto.label.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[0].name
|
||||
assert_equal :exit_label , labels[1].name
|
||||
assert_equal labels[0].object_id , labels[1].object_id
|
||||
end
|
||||
|
||||
def test_goto_with_label
|
||||
gotos = compile("exit_label;goto(exit_label)")
|
||||
assert_equal :exit_label , gotos[0].name
|
||||
assert_equal :exit_label , gotos[1].label.name
|
||||
assert_equal gotos[0].object_id , gotos[1].label.object_id
|
||||
end
|
||||
end
|
||||
end
|
@ -10,11 +10,6 @@ module SlotLanguage
|
||||
def test_compile
|
||||
assert_equal SlotMaker , compile("a").class
|
||||
end
|
||||
def test_label
|
||||
label = compile("while_label")
|
||||
assert_equal SlotMachine::Label , label.class
|
||||
assert_equal :while_label , label.name
|
||||
end
|
||||
def test_slot_load_rinst
|
||||
assert_equal LoadMaker , compile_class("a = @b")
|
||||
end
|
||||
@ -30,13 +25,10 @@ module SlotLanguage
|
||||
def test_slot_load_linst_trav2
|
||||
assert_equal LoadMaker , compile_class("@a.c = b.c")
|
||||
end
|
||||
def test_goto
|
||||
assert_equal SlotMachine::Jump , compile_class("goto(exit_label)")
|
||||
end
|
||||
def test_if
|
||||
check = compile("goto(exit_label) if(a == b)")
|
||||
assert_equal CheckMaker , check.class
|
||||
assert_equal SlotMachine::Jump , check.goto.class
|
||||
assert_equal Goto , check.goto.class
|
||||
end
|
||||
def test_assign
|
||||
assign = compile("c = d")
|
||||
@ -54,7 +46,7 @@ module SlotLanguage
|
||||
multi = compile("start_label;c = c.next;goto(start_label)")
|
||||
assert_equal Array , multi.class
|
||||
assert_equal SlotMachine::Label , multi.first.class
|
||||
assert_equal SlotMachine::Jump , multi.last.class
|
||||
assert_equal Goto , multi.last.class
|
||||
end
|
||||
def test_shift
|
||||
load = compile("word = name.member")
|
||||
|
Reference in New Issue
Block a user