From 8d7a2fe4d6ef9f0034a118abc7ca37d1224dafb2 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 29 Jun 2018 23:29:10 +0300 Subject: [PATCH] fix mom tests and always have a list inside a class --- lib/rubyx/rubyx_compiler.rb | 8 +++++-- lib/vool/class_statement.rb | 15 +++++++++++-- lib/vool/statements.rb | 2 +- test/risc/test_risc_compiler.rb | 7 +++--- test/rubyx/helper.rb | 3 +++ .../ruby_compiler/test_class_statement.rb | 14 ++++++------ test/rubyx/test_rubyx_compiler.rb | 3 ++- test/rubyx/test_rubyx_compiler1.rb | 22 +++++-------------- 8 files changed, 42 insertions(+), 32 deletions(-) diff --git a/lib/rubyx/rubyx_compiler.rb b/lib/rubyx/rubyx_compiler.rb index 1aa78a4b..e6d96df7 100644 --- a/lib/rubyx/rubyx_compiler.rb +++ b/lib/rubyx/rubyx_compiler.rb @@ -9,10 +9,14 @@ module RubyX vool end + def self.ruby_to_mom( ruby_source ) + vool = self.ruby_to_vool(ruby_source) + vool.to_mom(nil) + end + def self.ruby_to_binary(source , platform = :arm) machine = Risc.machine.boot - vool = self.ruby_to_vool(source) - vool.to_mom(nil).class + ruby_to_mom(source) machine.translate(platform) machine.position_all machine.create_binary diff --git a/lib/vool/class_statement.rb b/lib/vool/class_statement.rb index 66c26b77..bcd8080d 100644 --- a/lib/vool/class_statement.rb +++ b/lib/vool/class_statement.rb @@ -4,11 +4,22 @@ module Vool attr_reader :clazz def initialize( name , supe , body) - @name , @super_class_name , @body = name , supe , body + @name , @super_class_name = name , supe + case body + when MethodStatement + @body = Statements.new([body]) + when Statements + @body = body + when nil + @body = Statements.new([]) + else + raise "what body #{body}" + end end def normalize - ClassStatement.new(@name , @super_class_name, @body&.normalize ) + meths = body.statements.collect{|meth| meth.normalize} + ClassStatement.new(@name , @super_class_name, Statements.new(meths) ) end def to_mom( _ ) diff --git a/lib/vool/statements.rb b/lib/vool/statements.rb index 9644d29b..0632a0b2 100644 --- a/lib/vool/statements.rb +++ b/lib/vool/statements.rb @@ -30,7 +30,7 @@ module Vool # create mom instructions def to_mom( method ) - raise "Empty list ? #{statements.length}" unless @statements[0] + raise "Empty list ? #{statements.length}" if empty? flat = @statements.shift.to_mom(method) while( nekst = @statements.shift ) flat.append nekst.to_mom(method) diff --git a/test/risc/test_risc_compiler.rb b/test/risc/test_risc_compiler.rb index a6bb904c..161e819d 100644 --- a/test/risc/test_risc_compiler.rb +++ b/test/risc/test_risc_compiler.rb @@ -38,19 +38,20 @@ module Risc def test_creates_method_statement_in_class clazz = RubyX::RubyXCompiler.ruby_to_vool in_Test("def meth; @ivar = 5 ;end") - assert_equal Vool::MethodStatement , clazz.body.class + assert_equal Vool::Statements , clazz.body.class + assert_equal Vool::MethodStatement , clazz.body.first.class end def test_method_statement_has_class vool = RubyX::RubyXCompiler.ruby_to_vool in_Test("def meth; @ivar = 5;end") clazz = vool.to_mom(nil) - assert vool.body.clazz + assert vool.body.first.clazz end def test_parfait_class_creation vool = RubyX::RubyXCompiler.ruby_to_vool in_Test("def meth; @ivar = 5;end") clazz = vool.to_mom(nil) - assert_equal Parfait::Class , vool.body.clazz.class + assert_equal Parfait::Class , vool.body.first.clazz.class end def test_typed_method_instance_type diff --git a/test/rubyx/helper.rb b/test/rubyx/helper.rb index 448c239d..6f5a7d0d 100644 --- a/test/rubyx/helper.rb +++ b/test/rubyx/helper.rb @@ -8,6 +8,9 @@ module RubyX def ruby_to_vool(input) RubyXCompiler.ruby_to_vool(input) end + def ruby_to_mom(input) + RubyXCompiler.ruby_to_mom(input) + end def compile_in_test input vool = ruby_to_vool in_Test(input) vool.to_mom(nil) diff --git a/test/rubyx/ruby_compiler/test_class_statement.rb b/test/rubyx/ruby_compiler/test_class_statement.rb index 36a58da4..043900a0 100644 --- a/test/rubyx/ruby_compiler/test_class_statement.rb +++ b/test/rubyx/ruby_compiler/test_class_statement.rb @@ -22,7 +22,7 @@ module Vool end def test_compile_class_body - assert_nil @lst.body + assert @lst.body.empty? end end @@ -31,13 +31,13 @@ module Vool include RubyTests def test_compile_one_method - lst = compile( in_Test("@ivar = 4") ) - assert_equal IvarAssignment , lst.body.class + lst = compile( as_test_main("@ivar = 4") ) + assert_equal IvarAssignment , lst.body.first.body.class end - def test_compile_two_methods - lst = compile( in_Test("false; true;") ) - assert_equal ScopeStatement , lst.body.class - assert_equal TrueConstant , lst.body.statements[1].class + def test_compile_two_stats + lst = compile( as_test_main("false; true;") ) + assert_equal ScopeStatement , lst.body.first.body.class + assert_equal TrueConstant , lst.body.first.body.statements[1].class end end diff --git a/test/rubyx/test_rubyx_compiler.rb b/test/rubyx/test_rubyx_compiler.rb index 29a40020..973dcddc 100644 --- a/test/rubyx/test_rubyx_compiler.rb +++ b/test/rubyx/test_rubyx_compiler.rb @@ -24,7 +24,8 @@ module RubyX def test_class_body_is_scope clazz = ruby_to_vool in_Test("def meth; @ivar = 5 ;end") - assert_equal Vool::MethodStatement , clazz.body.class + assert_equal Vool::Statements , clazz.body.class + assert_equal Vool::MethodStatement , clazz.body.first.class end def test_space_is_unchanged_by_compile diff --git a/test/rubyx/test_rubyx_compiler1.rb b/test/rubyx/test_rubyx_compiler1.rb index a2f6a1a2..f0c0bd2b 100644 --- a/test/rubyx/test_rubyx_compiler1.rb +++ b/test/rubyx/test_rubyx_compiler1.rb @@ -1,34 +1,24 @@ -require_relative "../helper" +require_relative "helper" module RubyX class TestVoolCompiler < MiniTest::Test include ScopeHelper - - def setup - Risc.machine.boot - end - def ruby_to_vool(input) - RubyXCompiler.ruby_to_vool(input) - end + include RubyXHelper def test_creates_class_without_deriviation - vool = ruby_to_vool "class Testing ; end" - vool.to_mom(nil) + ruby_to_mom "class Testing ; end" clazz = Parfait.object_space.get_class_by_name(:Testing) assert clazz , "No classes created" assert_equal :Object , clazz.super_class_name end def test_creates_class_deriviation - vool = ruby_to_vool "class Testing ; end" - mom = vool.to_mom(nil) - assert_equal Vool::ClassStatement , vool.class - #assert mom , "No classes created" + mom = ruby_to_mom "class Testing ; end" + assert mom , "No classes created" end def test_creates_class_with_deriviation - vool = ruby_to_vool "class Test2 < List ;end" - vool.to_mom(nil) + ruby_to_mom "class Test2 < List ;end" clazz = Parfait.object_space.get_class_by_name(:Test2) assert clazz, "No classes created" assert_equal :List , clazz.super_class_name