From 57b0ad2c32480a0c22872654647edb5f5df22b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Mon, 19 Aug 2019 18:48:13 +0300 Subject: [PATCH] Fix super as statement Super is a statement, a send really. Not an expression (as maybe in c++) The actual implementation will be a bit tricky, like raise, a bit of stack walking, but not impossible. Still, later --- lib/parfait/integer.rb | 4 ++-- lib/ruby/basic_values.rb | 5 ----- lib/ruby/ruby_compiler.rb | 11 +++++------ lib/ruby/send_statement.rb | 6 ++++++ lib/vool/basic_values.rb | 5 ----- lib/vool/statement.rb | 1 + lib/vool/super_statement.rb | 5 +++++ test/ruby/test_send_statement.rb | 8 ++++---- test/ruby/test_send_statement1.rb | 12 +++++------- test/rubyx/parfait/test_integer.rb | 28 +++++++++++++++++++--------- 10 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 lib/vool/super_statement.rb diff --git a/lib/parfait/integer.rb b/lib/parfait/integer.rb index 77be3532..85e66e85 100644 --- a/lib/parfait/integer.rb +++ b/lib/parfait/integer.rb @@ -31,7 +31,7 @@ end def to_s - "Integer #{value}" + "Integer " + value.to_s end # compile time method to set the actual value. # this should not really be part of parfait, as ints are immutable at runtime. @@ -59,7 +59,7 @@ class ReturnAddress < Integer def to_s - "ReturnAddress 0x#{object_id.to_s(16)}:#{value}" + "ReturnAddress 0x" + object_id.to_s(16) + ":" + value.to_s end end diff --git a/lib/ruby/basic_values.rb b/lib/ruby/basic_values.rb index f7040e98..79d9aeeb 100644 --- a/lib/ruby/basic_values.rb +++ b/lib/ruby/basic_values.rb @@ -44,11 +44,6 @@ module Ruby "self" end end - class SuperExpression < Constant - def to_s(depth = 0) - "super" - end - end class StringConstant < ValueConstant attr_reader :value def initialize(value) diff --git a/lib/ruby/ruby_compiler.rb b/lib/ruby/ruby_compiler.rb index fc3fd8ee..27746258 100644 --- a/lib/ruby/ruby_compiler.rb +++ b/lib/ruby/ruby_compiler.rb @@ -74,6 +74,9 @@ module Ruby def on_arg( arg ) arg.first end + def on_optarg(arg) + arg.first + end def on_block(block_node) sendd = process(block_node.children[0]) @@ -232,14 +235,14 @@ module Ruby # this is a call to super without args (z = zero arity) def on_zsuper exp - SendStatement.new( nil , SuperExpression.new , nil) + SuperStatement.new([]) end # this is a call to super with args and # same name as current method, which is set later def on_super( statement ) arguments = process_all(statement.children) - SendStatement.new( nil , SuperExpression.new , arguments) + SuperStatement.new( arguments) end def on_assignment statement @@ -250,10 +253,6 @@ module Ruby w end - def handler_missing(node) - not_implemented(node) - end - private def instance_name sym diff --git a/lib/ruby/send_statement.rb b/lib/ruby/send_statement.rb index 3d23ad62..5611bd5a 100644 --- a/lib/ruby/send_statement.rb +++ b/lib/ruby/send_statement.rb @@ -8,4 +8,10 @@ module Ruby at_depth( depth , "#{receiver}.#{name}(#{arguments.join(', ')})") end end + class SuperStatement < SendStatement + def initialize(args) + super(:super , SelfExpression.new , args) + end + end + end diff --git a/lib/vool/basic_values.rb b/lib/vool/basic_values.rb index 3fe511d6..b4eee094 100644 --- a/lib/vool/basic_values.rb +++ b/lib/vool/basic_values.rb @@ -86,11 +86,6 @@ module Vool "self" end end - class SuperExpression < Statement - def to_s(depth = 0) - "super" - end - end class StringConstant < Constant attr_reader :value def initialize(value) diff --git a/lib/vool/statement.rb b/lib/vool/statement.rb index aab229e2..44d6b184 100644 --- a/lib/vool/statement.rb +++ b/lib/vool/statement.rb @@ -75,6 +75,7 @@ require_relative "class_method_expression" require_relative "return_statement" require_relative "statements" require_relative "send_statement" +require_relative "super_statement" require_relative "variables" require_relative "while_statement" require_relative "yield_statement" diff --git a/lib/vool/super_statement.rb b/lib/vool/super_statement.rb new file mode 100644 index 00000000..740be26b --- /dev/null +++ b/lib/vool/super_statement.rb @@ -0,0 +1,5 @@ +module Vool + class SuperStatement < SendStatement + + end +end diff --git a/test/ruby/test_send_statement.rb b/test/ruby/test_send_statement.rb index 6c8475d2..51f07b1e 100644 --- a/test/ruby/test_send_statement.rb +++ b/test/ruby/test_send_statement.rb @@ -44,16 +44,16 @@ module Ruby include RubyTests def test_super0_receiver lst = compile( "super") - assert_equal SuperExpression , lst.receiver.class + assert_equal SelfExpression , lst.receiver.class end def test_super0 lst = compile( "super") - assert_equal SendStatement , lst.class + assert_equal SuperStatement , lst.class end def test_super_receiver lst = compile( "super(1)") - assert_equal SuperExpression , lst.receiver.class + assert_equal SelfExpression , lst.receiver.class end def test_super_args lst = compile( "super(1)") @@ -61,7 +61,7 @@ module Ruby end def test_super_name #is nil lst = compile( "super(1)") - assert_nil lst.name + assert_equal :super , lst.name end class TestSendSendArgs < MiniTest::Test include RubyTests diff --git a/test/ruby/test_send_statement1.rb b/test/ruby/test_send_statement1.rb index 480845be..affdef5a 100644 --- a/test/ruby/test_send_statement1.rb +++ b/test/ruby/test_send_statement1.rb @@ -41,12 +41,11 @@ module Ruby include RubyTests def test_super0 lst = compile( "super").to_vool - assert_equal Vool::Statements , lst.class - assert_equal Vool::SendStatement , lst.last.class + assert_equal Vool::SuperStatement , lst.class end def test_super0_receiver lst = compile( "super").to_vool - assert_equal Vool::SuperExpression , lst.first.value.class + assert_equal Vool::SelfExpression , lst.receiver.class end end class TestSendSuperArgsVool < MiniTest::Test @@ -55,14 +54,13 @@ module Ruby @lst = compile( "super(1)").to_vool end def test_super_class - assert_equal Vool::Statements , @lst.class - assert_equal Vool::SendStatement , @lst.last.class + assert_equal Vool::SuperStatement , @lst.class end def test_super_receiver - assert_equal Vool::SuperExpression , @lst.first.value.class + assert_equal Vool::SelfExpression , @lst.receiver.class end def test_super_name - assert @lst.first.name.to_s.start_with?("tmp") + assert_equal :super, @lst.name end end class TestSendReceiverTypeVool < MiniTest::Test diff --git a/test/rubyx/parfait/test_integer.rb b/test/rubyx/parfait/test_integer.rb index 3bf8a885..f22b2ba0 100644 --- a/test/rubyx/parfait/test_integer.rb +++ b/test/rubyx/parfait/test_integer.rb @@ -4,27 +4,37 @@ module RubyX class TestIntegerCompile < MiniTest::Test include ParfaitHelper + def setup + @compiler = compiler + @compiler.ruby_to_vool load_parfait(:object) + @compiler.ruby_to_vool load_parfait(:data_object) + end def source load_parfait(:integer) end def test_load assert source.include?("class Integer") - assert source.length > 2000 + assert source.length > 1500 , source.length end - def qtest_vool - vool = compiler.ruby_to_vool source - assert_equal Vool::ClassStatement , vool.class - assert_equal :Object , vool.name + def test_vool + vool = @compiler.ruby_to_vool source + 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 qtest_mom - mom = compiler.ruby_to_mom source + 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