From c9d75394791d0462d1fb8e87c3de7c5eb9137c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Fri, 13 Sep 2019 10:41:13 +0300 Subject: [PATCH] rework macro tests, or are they builtin small fixes too --- lib/mom/instruction/return_jump.rb | 4 ++ lib/mom/macro/exit.rb | 2 +- lib/mom/macro/macro.rb | 4 ++ lib/mom/macro/method_missing.rb | 2 +- lib/mom/macro/putstring.rb | 2 +- test/mom/macro/helper.rb | 26 +++------ test/mom/macro/test_comparison.rb | 18 ++++-- test/mom/macro/test_div10.rb | 9 ++- test/mom/macro/test_div4.rb | 9 ++- test/mom/macro/test_exit.rb | 8 ++- test/mom/macro/test_get_internal_byte.rb | 8 ++- test/mom/macro/test_get_internal_word.rb | 8 ++- test/mom/macro/test_init.rb | 8 ++- test/mom/macro/test_integer.rb | 71 ------------------------ test/mom/macro/test_method_missing.rb | 9 ++- test/mom/macro/test_object.rb | 69 ----------------------- test/mom/macro/test_operator.rb | 35 +++++++----- test/mom/macro/test_putstring.rb | 9 ++- test/mom/macro/test_set_internal_byte.rb | 6 +- test/mom/macro/test_set_internal_word.rb | 9 ++- test/mom/macro/test_word.rb | 42 -------------- 21 files changed, 111 insertions(+), 247 deletions(-) delete mode 100644 test/mom/macro/test_integer.rb delete mode 100644 test/mom/macro/test_object.rb delete mode 100644 test/mom/macro/test_word.rb diff --git a/lib/mom/instruction/return_jump.rb b/lib/mom/instruction/return_jump.rb index 2d9665d6..c4bd8fbc 100644 --- a/lib/mom/instruction/return_jump.rb +++ b/lib/mom/instruction/return_jump.rb @@ -22,6 +22,10 @@ module Mom def to_risc(compiler) compiler.add_code Risc::Branch.new(self , return_label.risc_label(compiler)) end + + def to_s + "ReturnJump: #{return_label}" + end end diff --git a/lib/mom/macro/exit.rb b/lib/mom/macro/exit.rb index e9816b62..38572aa2 100644 --- a/lib/mom/macro/exit.rb +++ b/lib/mom/macro/exit.rb @@ -3,7 +3,7 @@ module Mom def to_risc(compiler) builder = compiler.builder(compiler.source) builder.prepare_int_return # makes integer_tmp variable as return - Builtin.exit_sequence(builder) + Macro.exit_sequence(builder) return compiler end end diff --git a/lib/mom/macro/macro.rb b/lib/mom/macro/macro.rb index 77c6e682..e1084f4c 100644 --- a/lib/mom/macro/macro.rb +++ b/lib/mom/macro/macro.rb @@ -1,6 +1,10 @@ module Mom class Macro < Instruction + def to_s + self.class.name.split("::").last + end + # emit the syscall with given name # there is a Syscall instruction, but the message has to be saved and restored def self.emit_syscall( builder , name ) diff --git a/lib/mom/macro/method_missing.rb b/lib/mom/macro/method_missing.rb index e116750a..7cab2be7 100644 --- a/lib/mom/macro/method_missing.rb +++ b/lib/mom/macro/method_missing.rb @@ -3,7 +3,7 @@ module Mom def to_risc(compiler) builder = compiler.builder(compiler.source) builder.prepare_int_return # makes integer_tmp variable as return - Builtin.emit_syscall( builder , :exit ) + Macro.emit_syscall( builder , :exit ) return compiler end end diff --git a/lib/mom/macro/putstring.rb b/lib/mom/macro/putstring.rb index 636ce67a..695eeccc 100644 --- a/lib/mom/macro/putstring.rb +++ b/lib/mom/macro/putstring.rb @@ -7,7 +7,7 @@ module Mom word! << message[:receiver] integer! << word[Parfait::Word.get_length_index] end - Mom::Builtin.emit_syscall( builder , :putstring ) + Mom::Macro.emit_syscall( builder , :putstring ) compiler end end diff --git a/test/mom/macro/helper.rb b/test/mom/macro/helper.rb index e5a1050c..b6831a91 100644 --- a/test/mom/macro/helper.rb +++ b/test/mom/macro/helper.rb @@ -3,27 +3,15 @@ require_relative "../helper" module Mom module Builtin class BootTest < MiniTest::Test - def setup - Parfait.boot!(Parfait.default_test_options) - MomCollection.compiler_for( Parfait.object_space.get_class.instance_type , Parfait::Space , :main) - end - def get_int_compiler(name) - obj_type = Parfait.object_space.get_type_by_class_name(:Integer) - Builtin.compiler_for( obj_type , Integer , name) - end - def get_operator_compiler(name) - obj_type = Parfait.object_space.get_type_by_class_name(:Integer) - Builtin.operator_compiler( obj_type , name) + 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 + @method end - def get_object_compiler(name) - obj_type = Parfait.object_space.get_type_by_class_name(:Object) - Builtin.compiler_for( obj_type , Object , name) - end - def get_word_compiler(name) - obj_type = Parfait.object_space.get_type_by_class_name(:Word) - Builtin.compiler_for( obj_type , Word , name) - end end end end diff --git a/test/mom/macro/test_comparison.rb b/test/mom/macro/test_comparison.rb index 54fd23a3..7921c4f8 100644 --- a/test/mom/macro/test_comparison.rb +++ b/test/mom/macro/test_comparison.rb @@ -4,26 +4,32 @@ module Mom module Builtin class TestIntComp1Risc < BootTest def setup - super - @method = get_int_compiler(:<) + @method = get_compiler("Integer",:lt) + end + def test_mom_length + assert_equal :< , @method.callable.name + assert_equal 7 , @method.mom_instructions.length end def test_compile assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - assert_equal 23 , @method.to_risc.risc_instructions.length + assert_equal 26 , @method.to_risc.risc_instructions.length end end class TestIntComp2Risc < BootTest def setup - super - @method = get_int_compiler(:>=) + @method = get_compiler("Integer",:gt) + end + def test_mom_length + assert_equal :> , @method.callable.name + assert_equal 7 , @method.mom_instructions.length end def test_compile assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - assert_equal 22 , @method.to_risc.risc_instructions.length + assert_equal 26 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_div10.rb b/test/mom/macro/test_div10.rb index 2b0af89c..db89b158 100644 --- a/test/mom/macro/test_div10.rb +++ b/test/mom/macro/test_div10.rb @@ -4,14 +4,17 @@ module Mom module Builtin class TestIntDiv10Risc < BootTest def setup - super - @method = get_int_compiler(:div10) + @method = get_compiler("Integer",:div10) + end + def test_mom_length + assert_equal :div10 , @method.callable.name + assert_equal 7 , @method.mom_instructions.length end def test_compile assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - assert_equal 67 , @method.to_risc.risc_instructions.length + assert_equal 70 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_div4.rb b/test/mom/macro/test_div4.rb index 571a58b4..a0689269 100644 --- a/test/mom/macro/test_div4.rb +++ b/test/mom/macro/test_div4.rb @@ -4,15 +4,18 @@ module Mom module Builtin class TestIntDiv4Risc < BootTest def setup - super - @method = get_int_compiler(:div4) + @method = get_compiler("Integer",:div4) + end + def test_mom_length + assert_equal :div4 , @method.callable.name + assert_equal 7 , @method.mom_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 38 , @method.to_risc.risc_instructions.length + assert_equal 41 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_exit.rb b/test/mom/macro/test_exit.rb index ae29c441..67097953 100644 --- a/test/mom/macro/test_exit.rb +++ b/test/mom/macro/test_exit.rb @@ -5,13 +5,17 @@ module Mom class TestObjectExitRisc < BootTest def setup super - @method = get_object_compiler(:exit) + @method = get_compiler("Object",:exit) + end + def test_mom_length + assert_equal :exit , @method.callable.name + assert_equal 7 , @method.mom_instructions.length end def test_compile assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - assert_equal 37 , @method.to_risc.risc_instructions.length + assert_equal 40 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_get_internal_byte.rb b/test/mom/macro/test_get_internal_byte.rb index a6e894af..587a7eb4 100644 --- a/test/mom/macro/test_get_internal_byte.rb +++ b/test/mom/macro/test_get_internal_byte.rb @@ -5,13 +5,17 @@ module Mom class TestWordGetRisc < BootTest def setup super - @method = get_word_compiler(:get_internal_byte) + @method = get_compiler("Word",:get) + end + def test_mom_length + assert_equal :get_internal_byte , @method.callable.name + assert_equal 7 , @method.mom_instructions.length end def test_compile assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - assert_equal 38 , @method.to_risc.risc_instructions.length + assert_equal 41 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_get_internal_word.rb b/test/mom/macro/test_get_internal_word.rb index 9939f6be..371f8f22 100644 --- a/test/mom/macro/test_get_internal_word.rb +++ b/test/mom/macro/test_get_internal_word.rb @@ -5,13 +5,17 @@ module Mom class TestWordGetRisc < BootTest def setup super - @method = get_object_compiler(:get_internal_word) + @method = get_compiler("Object",:get) + end + def test_mom_length + assert_equal :get_internal_word , @method.callable.name + assert_equal 7 , @method.mom_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 + assert_equal 18 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_init.rb b/test/mom/macro/test_init.rb index d7b28288..96888354 100644 --- a/test/mom/macro/test_init.rb +++ b/test/mom/macro/test_init.rb @@ -5,13 +5,17 @@ module Mom class TestObjectInitRisc < BootTest def setup super - @method = get_object_compiler(:__init__) + @method = get_compiler("Object",:init) + end + def test_mom_length + assert_equal :__init__ , @method.callable.name + assert_equal 7 , @method.mom_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 + assert_equal 31 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_integer.rb b/test/mom/macro/test_integer.rb deleted file mode 100644 index 56fb0229..00000000 --- a/test/mom/macro/test_integer.rb +++ /dev/null @@ -1,71 +0,0 @@ -require_relative "helper" - -module Mom - module Builtin - class TestIntDiv4 < BootTest - def setup - super - @method = get_int_compiler(:div4) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - class TestIntDiv10 < BootTest - def setup - super - @method = get_int_compiler(:div10) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - class TestIntComp1 < BootTest - def setup - super - @method = get_int_compiler(:<) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - class TestIntComp2 < BootTest - def setup - super - @method = get_int_compiler(:>=) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - class TestIntOperators < BootTest - def setup - super - end - def each_method &block - Risc.operators.each do |name| - method = get_operator_compiler(name) - block.yield(method) - end - end - def test_has_get_internal - each_method do |method| - assert_equal Mom::MethodCompiler , method.class - assert_equal 5 , method.mom_instructions.length - end - end - end - end -end diff --git a/test/mom/macro/test_method_missing.rb b/test/mom/macro/test_method_missing.rb index 59440213..7fdcf73f 100644 --- a/test/mom/macro/test_method_missing.rb +++ b/test/mom/macro/test_method_missing.rb @@ -4,14 +4,17 @@ module Mom module Builtin class TestObjectMissingRisc < BootTest def setup - super - @method = get_object_compiler(:_method_missing) + @method = get_compiler("Object",:missing) + end + def test_mom_length + assert_equal :method_missing , @method.callable.name + assert_equal 7 , @method.mom_instructions.length end def test_compile assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - assert_equal 39 , @method.to_risc.risc_instructions.length + assert_equal 42 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_object.rb b/test/mom/macro/test_object.rb deleted file mode 100644 index 85c50a5f..00000000 --- a/test/mom/macro/test_object.rb +++ /dev/null @@ -1,69 +0,0 @@ -require_relative "helper" - -module Mom - module Builtin - class TestObjectGet < BootTest - def setup - super - @method = get_object_compiler(:get_internal_word) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - def test_return - assert_equal ReturnSequence , @method.mom_instructions.next(3).class - end - end - class TestObjectSet < BootTest - def setup - super - @method = get_object_compiler(:set_internal_word) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - class TestObjectMissing < BootTest - def setup - super - @method = get_object_compiler(:_method_missing) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - class TestObjectExit < BootTest - def setup - super - @method = get_object_compiler(:exit) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - class TestObjectInit < BootTest - def setup - super - @method = get_object_compiler(:__init__) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 2 , @method.mom_instructions.length - end - end - end -end diff --git a/test/mom/macro/test_operator.rb b/test/mom/macro/test_operator.rb index ae576a2e..0ac1532a 100644 --- a/test/mom/macro/test_operator.rb +++ b/test/mom/macro/test_operator.rb @@ -2,25 +2,34 @@ require_relative "helper" module Mom module Builtin - class TestIntOperatorsRisc < BootTest + class TestIntOpPl < BootTest def setup - super + @method = get_compiler("Integer",:and) end - def each_method &block - Risc.operators.each do |name| - method = get_operator_compiler(name) - block.yield(method) - end + def test_mom_length + assert_equal :& , @method.callable.name + assert_equal 7 , @method.mom_instructions.length end def test_compile - each_method do |method| - assert_equal Risc::MethodCompiler , method.to_risc.class - end + assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - each_method do |method| - assert_equal 39 , method.to_risc.risc_instructions.length - end + assert_equal 42 , @method.to_risc.risc_instructions.length + end + end + class TestIntOpMM < BootTest + def setup + @method = get_compiler("Integer",:or) + end + def test_mom_length + assert_equal :| , @method.callable.name + assert_equal 7 , @method.mom_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 diff --git a/test/mom/macro/test_putstring.rb b/test/mom/macro/test_putstring.rb index 1eecaf54..4b133a03 100644 --- a/test/mom/macro/test_putstring.rb +++ b/test/mom/macro/test_putstring.rb @@ -4,14 +4,17 @@ module Mom module Builtin class TestWordPutRisc < BootTest def setup - super - @method = get_word_compiler(:putstring) + @method = get_compiler("Word",:put) + end + def test_mom_length + assert_equal :putstring , @method.callable.name + assert_equal 7 , @method.mom_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 + assert_equal 44 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_set_internal_byte.rb b/test/mom/macro/test_set_internal_byte.rb index 76fd61ad..b7bfda67 100644 --- a/test/mom/macro/test_set_internal_byte.rb +++ b/test/mom/macro/test_set_internal_byte.rb @@ -5,7 +5,11 @@ module Mom class TestWordSetRisc < BootTest def setup super - @method = get_word_compiler(:set_internal_byte) + @method = get_compiler("Word",:set) + end + def test_mom_length + assert_equal :set_internal_byte , @method.callable.name + assert_equal 5 , @method.mom_instructions.length end def test_compile assert_equal Risc::MethodCompiler , @method.to_risc.class diff --git a/test/mom/macro/test_set_internal_word.rb b/test/mom/macro/test_set_internal_word.rb index a2d532d9..1da3d6d3 100644 --- a/test/mom/macro/test_set_internal_word.rb +++ b/test/mom/macro/test_set_internal_word.rb @@ -4,14 +4,17 @@ module Mom module Builtin class TestWordSetRisc < BootTest def setup - super - @method = get_object_compiler(:set_internal_word) + @method = get_compiler("Word",:set) + end + def test_mom_length + assert_equal :set_internal_byte , @method.callable.name + assert_equal 7 , @method.mom_instructions.length end def test_compile assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - assert_equal 16 , @method.to_risc.risc_instructions.length + assert_equal 20 , @method.to_risc.risc_instructions.length end end end diff --git a/test/mom/macro/test_word.rb b/test/mom/macro/test_word.rb deleted file mode 100644 index 00e7e75c..00000000 --- a/test/mom/macro/test_word.rb +++ /dev/null @@ -1,42 +0,0 @@ -require_relative "helper" - -module Mom - module Builtin - class TestWordPut < BootTest - def setup - super - @method = get_word_compiler(:putstring) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - class TestWordGet < BootTest - def setup - super - @method = get_word_compiler(:get_internal_byte) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - class TestWordSet < BootTest - def setup - super - @method = get_word_compiler(:set_internal_byte) - end - def test_has_get_internal - assert_equal Mom::MethodCompiler , @method.class - end - def test_mom_length - assert_equal 5 , @method.mom_instructions.length - end - end - end -end