From b4b1e6e13b4a8080990d199f94ab99154b2a1088 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 5 Mar 2019 20:36:40 +0200 Subject: [PATCH] start rewriting for parfait #25 just some infrastructure for now. --- lib/ruby/class_statement.rb | 18 ++++++++++++--- test/ruby/helper.rb | 6 +++++ test/ruby/test_class_statement.rb | 37 ++++++++++++++++++++++++++++++ test/rubyx/test_rubyx_compiler2.rb | 18 +++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/lib/ruby/class_statement.rb b/lib/ruby/class_statement.rb index 20886e38..2f6a2d93 100644 --- a/lib/ruby/class_statement.rb +++ b/lib/ruby/class_statement.rb @@ -5,22 +5,34 @@ module Ruby def initialize( name , supe , body) @name , @super_class_name = name , supe case body - when MethodStatement + when MethodStatement , SendStatement @body = Statements.new([body]) when Statements @body = body when nil @body = Statements.new([]) else - raise "what body #{body}" + raise "what body #{body.class}:#{body}" end end def to_vool - meths = body.statements.collect{|meth| meth.to_vool} + meths = body.statements.collect do |meth| + meth.is_a?(MethodStatement) ? meth.to_vool : tranform_statement(meth) + end Vool::ClassStatement.new(@name , @super_class_name, Vool::Statements.new(meths) ) end + def tranform_statement( class_send ) + unless class_send.is_a?(SendStatement) + raise "Other than methods, only class methods allowed, not #{class_send.class}" + end + unless class_send.name == :attr + raise "Only remapping attr and cattr, not #{class_send.name}" + end + raise "todo" + end + def to_s(depth = 0) at_depth(depth , "class #{name}" , @body.to_s(depth + 1) , "end") end diff --git a/test/ruby/helper.rb b/test/ruby/helper.rb index d07efeee..44535654 100644 --- a/test/ruby/helper.rb +++ b/test/ruby/helper.rb @@ -13,5 +13,11 @@ module Ruby FIXMERubyXCompiler.new(input).ruby_to_vool end + def assert_raises_muted &block + orig_stdout = $stdout + $stdout = StringIO.new + assert_raises &block + $stdout = orig_stdout + end end end diff --git a/test/ruby/test_class_statement.rb b/test/ruby/test_class_statement.rb index 4a5298d1..229fb0ad 100644 --- a/test/ruby/test_class_statement.rb +++ b/test/ruby/test_class_statement.rb @@ -57,6 +57,43 @@ module Ruby assert_equal ScopeStatement , lst.body.first.body.class assert_equal TrueConstant , lst.body.first.body.statements[1].class end + end + class TestClassStatementTransformFail < MiniTest::Test + include RubyTests + + def test_if + input = "class Tryout < Base; false if(true) ; end" + assert_raises_muted { compile( input ).to_vool} + end + def test_instance + input = "class Tryout < Base; @var = 5 ; end" + assert_raises_muted { compile( input ).to_vool} + end + def test_wrong_send + input = "class Tryout < Base; hi() ; end" + assert_raises_muted { compile( input ).to_vool} + end + end + class TestClassStatementTransform < MiniTest::Test + include RubyTests + + def setup + input = "class Tryout < Base;attr :page ;end" + @vool = compile( input ).to_vool + end + def test_class + assert_equal Vool::ClassStatement , @vool.class + end + def test_body + assert_equal Vool::Statements , @vool.body.class + end + def test_compile_class_name + assert_equal :Tryout , @vool.name + end + def test_compile_class_super + assert_equal :Base , @vool.super_class_name + end end + end diff --git a/test/rubyx/test_rubyx_compiler2.rb b/test/rubyx/test_rubyx_compiler2.rb index a87a50c3..79bd039b 100644 --- a/test/rubyx/test_rubyx_compiler2.rb +++ b/test/rubyx/test_rubyx_compiler2.rb @@ -20,4 +20,22 @@ module RubyX assert_equal 23 , @linker.assemblers.length end end + 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_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) + end + end end