From 0e694a38f7b44adf3a80fb901724381c76e91c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Mon, 19 Aug 2019 15:56:15 +0300 Subject: [PATCH] Implicit returns for class methods gets us parsing of parfaits second file data_object --- lib/ruby/class_method_statement.rb | 3 ++- lib/ruby/method_statement.rb | 16 ++++++++++------ test/risc/test_method_compiler.rb | 2 +- test/ruby/test_class_method_statement.rb | 21 ++++++++++++++++++++- test/rubyx/parfait/test_data_object.rb | 20 ++++++++++---------- test/rubyx/test_rubyx_compiler2.rb | 11 +++++------ 6 files changed, 48 insertions(+), 25 deletions(-) diff --git a/lib/ruby/class_method_statement.rb b/lib/ruby/class_method_statement.rb index faf60240..7bc63db9 100644 --- a/lib/ruby/class_method_statement.rb +++ b/lib/ruby/class_method_statement.rb @@ -2,7 +2,8 @@ module Ruby class ClassMethodStatement < MethodStatement def to_vool - Vool::ClassMethodExpression.new( @name , @args.dup , @body.to_vool) + body = normalized_body + Vool::ClassMethodExpression.new( @name , @args.dup , body.to_vool) end def to_s(depth = 0) diff --git a/lib/ruby/method_statement.rb b/lib/ruby/method_statement.rb index d32f13f6..4f75194a 100644 --- a/lib/ruby/method_statement.rb +++ b/lib/ruby/method_statement.rb @@ -8,13 +8,17 @@ module Ruby @clazz = clazz end + # At the moment normalizing means creating implicit returns for some cases + # see replace_return for details. + def normalized_body + return replace_return( @body ) unless @body.is_a?(Statements) + body = Statements.new( @body.statements.dup ) + body << replace_return( body.pop ) + end + def to_vool - if @body.is_a?(Statements) - @body << replace_return( @body.pop ) - else - @body = replace_return( @body ) - end - Vool::MethodExpression.new( @name , @args.dup , @body.to_vool) + body = normalized_body + Vool::MethodExpression.new( @name , @args.dup , body.to_vool) end def replace_return(statement) diff --git a/test/risc/test_method_compiler.rb b/test/risc/test_method_compiler.rb index 58e2be99..bd9f54d8 100644 --- a/test/risc/test_method_compiler.rb +++ b/test/risc/test_method_compiler.rb @@ -20,7 +20,7 @@ module Risc def test_method_has_source method = create_method - assert_equal Vool::ScopeStatement , method.source.class + assert_equal Vool::Statements , method.source.class end def test_method_has_no_locals diff --git a/test/ruby/test_class_method_statement.rb b/test/ruby/test_class_method_statement.rb index 79874bab..a05a55bc 100644 --- a/test/ruby/test_class_method_statement.rb +++ b/test/ruby/test_class_method_statement.rb @@ -49,7 +49,26 @@ module Ruby assert_equal [:arg1, :arg2] , @lst.args end def test_body_is_scope_zero_statement - assert_equal Vool::LocalAssignment , @lst.body.class + assert_equal Vool::Statements , @lst.body.class + end + def test_body_is_scope_zero_statement + assert_equal Vool::LocalAssignment , @lst.body.first.class + end + end + class TestClassMethodStatementImplicitReturn < MiniTest::Test + include RubyTests + def setup() + input = "def self.tryout(arg1, arg2) ; arg1 ; end " + @lst = compile( input ).to_vool + end + def test_method + assert_equal Vool::ClassMethodExpression , @lst.class + end + def test_method_args + assert_equal [:arg1, :arg2] , @lst.args + end + def test_body_is_scope_zero_statement + assert_equal Vool::ReturnStatement , @lst.body.class end end class TestClassMethodStatement < MiniTest::Test diff --git a/test/rubyx/parfait/test_data_object.rb b/test/rubyx/parfait/test_data_object.rb index 4f2497e1..759e83e2 100644 --- a/test/rubyx/parfait/test_data_object.rb +++ b/test/rubyx/parfait/test_data_object.rb @@ -2,7 +2,7 @@ require_relative "helper" module RubyX - class TestDatObjectCompile #< MiniTest::Test + class TestDatObjectCompile < MiniTest::Test include ParfaitHelper def setup @compiler = compiler @@ -17,23 +17,23 @@ module RubyX end def test_vool vool = @compiler.ruby_to_vool source - assert_equ Vool::ScopeStatement , vool.class - assert_equal Vool::ClassStatement , vool[0].class - assert_equal Vool::ScopeStatement , vool[1].class - assert_equal Vool::ClassStatement , vool[1][0].class - assert_equal :DataObject , vool[1][0].name - assert_equal :Data4 , vool[1][1].name - assert_equal :Data8 , vool[1][2].name + assert_equal Vool::ScopeStatement , vool.class + assert_equal Vool::ClassExpression , vool[0].class + assert_equal Vool::ClassExpression , vool[1].class + assert_equal Vool::ClassExpression , vool[2].class + assert_equal :DataObject , vool[1].name + assert_equal :Data4 , vool[2].name + assert_equal :Data8 , vool[3].name end def test_mom mom = @compiler.ruby_to_mom source assert_equal Mom::MomCollection , mom.class end - def qtest_risc + def test_risc risc = compiler.ruby_to_risc source assert_equal Risc::RiscCollection , risc.class end - def qtest_binary + def test_binary risc = compiler.ruby_to_binary source , :interpreter assert_equal Risc::Linker , risc.class end diff --git a/test/rubyx/test_rubyx_compiler2.rb b/test/rubyx/test_rubyx_compiler2.rb index 6979383e..0ae731ec 100644 --- a/test/rubyx/test_rubyx_compiler2.rb +++ b/test/rubyx/test_rubyx_compiler2.rb @@ -26,22 +26,21 @@ module RubyX assert_equal 22 , linker.assemblers.length end end - class TestRubyXCompilerParfait #< MiniTest::Test + class TestRubyXCompilerParfait < MiniTest::Test include ScopeHelper include RubyXHelper def setup super code = "class Space ; def self.class_method(); return 1; end;def main(arg);return Space.class_method;end; end" - @comp = RubyXCompiler.ruby_to_risc(code , load_parfait: true)# , platform: :interpreter) + @comp = RubyXCompiler.ruby_to_binary(code , load_parfait: true , platform: :interpreter) end def test_load object = Parfait.object_space.get_class_by_name(:Object) - assert_equal Parfait::Class , object.class - object = object.instance_type - object.methods.each_method {|m| puts m.name} - assert_equal Parfait::Type , object.get_method(:set_type) + #object.methods.each_method {|m| puts m.name} + assert_equal Parfait::VoolMethod , object.get_method(:set_type).class + assert_equal Parfait::CallableMethod , object.instance_type.get_method(:set_type).class end end end