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
This commit is contained in:
Torsten Rüger 2019-08-19 18:48:13 +03:00
parent 0e694a38f7
commit 57b0ad2c32
10 changed files with 47 additions and 38 deletions

View File

@ -31,7 +31,7 @@
end end
def to_s def to_s
"Integer #{value}" "Integer " + value.to_s
end end
# compile time method to set the actual value. # compile time method to set the actual value.
# this should not really be part of parfait, as ints are immutable at runtime. # this should not really be part of parfait, as ints are immutable at runtime.
@ -59,7 +59,7 @@
class ReturnAddress < Integer class ReturnAddress < Integer
def to_s def to_s
"ReturnAddress 0x#{object_id.to_s(16)}:#{value}" "ReturnAddress 0x" + object_id.to_s(16) + ":" + value.to_s
end end
end end

View File

@ -44,11 +44,6 @@ module Ruby
"self" "self"
end end
end end
class SuperExpression < Constant
def to_s(depth = 0)
"super"
end
end
class StringConstant < ValueConstant class StringConstant < ValueConstant
attr_reader :value attr_reader :value
def initialize(value) def initialize(value)

View File

@ -74,6 +74,9 @@ module Ruby
def on_arg( arg ) def on_arg( arg )
arg.first arg.first
end end
def on_optarg(arg)
arg.first
end
def on_block(block_node) def on_block(block_node)
sendd = process(block_node.children[0]) sendd = process(block_node.children[0])
@ -232,14 +235,14 @@ module Ruby
# this is a call to super without args (z = zero arity) # this is a call to super without args (z = zero arity)
def on_zsuper exp def on_zsuper exp
SendStatement.new( nil , SuperExpression.new , nil) SuperStatement.new([])
end end
# this is a call to super with args and # this is a call to super with args and
# same name as current method, which is set later # same name as current method, which is set later
def on_super( statement ) def on_super( statement )
arguments = process_all(statement.children) arguments = process_all(statement.children)
SendStatement.new( nil , SuperExpression.new , arguments) SuperStatement.new( arguments)
end end
def on_assignment statement def on_assignment statement
@ -250,10 +253,6 @@ module Ruby
w w
end end
def handler_missing(node)
not_implemented(node)
end
private private
def instance_name sym def instance_name sym

View File

@ -8,4 +8,10 @@ module Ruby
at_depth( depth , "#{receiver}.#{name}(#{arguments.join(', ')})") at_depth( depth , "#{receiver}.#{name}(#{arguments.join(', ')})")
end end
end end
class SuperStatement < SendStatement
def initialize(args)
super(:super , SelfExpression.new , args)
end
end
end end

View File

@ -86,11 +86,6 @@ module Vool
"self" "self"
end end
end end
class SuperExpression < Statement
def to_s(depth = 0)
"super"
end
end
class StringConstant < Constant class StringConstant < Constant
attr_reader :value attr_reader :value
def initialize(value) def initialize(value)

View File

@ -75,6 +75,7 @@ require_relative "class_method_expression"
require_relative "return_statement" require_relative "return_statement"
require_relative "statements" require_relative "statements"
require_relative "send_statement" require_relative "send_statement"
require_relative "super_statement"
require_relative "variables" require_relative "variables"
require_relative "while_statement" require_relative "while_statement"
require_relative "yield_statement" require_relative "yield_statement"

View File

@ -0,0 +1,5 @@
module Vool
class SuperStatement < SendStatement
end
end

View File

@ -44,16 +44,16 @@ module Ruby
include RubyTests include RubyTests
def test_super0_receiver def test_super0_receiver
lst = compile( "super") lst = compile( "super")
assert_equal SuperExpression , lst.receiver.class assert_equal SelfExpression , lst.receiver.class
end end
def test_super0 def test_super0
lst = compile( "super") lst = compile( "super")
assert_equal SendStatement , lst.class assert_equal SuperStatement , lst.class
end end
def test_super_receiver def test_super_receiver
lst = compile( "super(1)") lst = compile( "super(1)")
assert_equal SuperExpression , lst.receiver.class assert_equal SelfExpression , lst.receiver.class
end end
def test_super_args def test_super_args
lst = compile( "super(1)") lst = compile( "super(1)")
@ -61,7 +61,7 @@ module Ruby
end end
def test_super_name #is nil def test_super_name #is nil
lst = compile( "super(1)") lst = compile( "super(1)")
assert_nil lst.name assert_equal :super , lst.name
end end
class TestSendSendArgs < MiniTest::Test class TestSendSendArgs < MiniTest::Test
include RubyTests include RubyTests

View File

@ -41,12 +41,11 @@ module Ruby
include RubyTests include RubyTests
def test_super0 def test_super0
lst = compile( "super").to_vool lst = compile( "super").to_vool
assert_equal Vool::Statements , lst.class assert_equal Vool::SuperStatement , lst.class
assert_equal Vool::SendStatement , lst.last.class
end end
def test_super0_receiver def test_super0_receiver
lst = compile( "super").to_vool lst = compile( "super").to_vool
assert_equal Vool::SuperExpression , lst.first.value.class assert_equal Vool::SelfExpression , lst.receiver.class
end end
end end
class TestSendSuperArgsVool < MiniTest::Test class TestSendSuperArgsVool < MiniTest::Test
@ -55,14 +54,13 @@ module Ruby
@lst = compile( "super(1)").to_vool @lst = compile( "super(1)").to_vool
end end
def test_super_class def test_super_class
assert_equal Vool::Statements , @lst.class assert_equal Vool::SuperStatement , @lst.class
assert_equal Vool::SendStatement , @lst.last.class
end end
def test_super_receiver def test_super_receiver
assert_equal Vool::SuperExpression , @lst.first.value.class assert_equal Vool::SelfExpression , @lst.receiver.class
end end
def test_super_name def test_super_name
assert @lst.first.name.to_s.start_with?("tmp") assert_equal :super, @lst.name
end end
end end
class TestSendReceiverTypeVool < MiniTest::Test class TestSendReceiverTypeVool < MiniTest::Test

View File

@ -4,27 +4,37 @@ module RubyX
class TestIntegerCompile < MiniTest::Test class TestIntegerCompile < MiniTest::Test
include ParfaitHelper include ParfaitHelper
def setup
@compiler = compiler
@compiler.ruby_to_vool load_parfait(:object)
@compiler.ruby_to_vool load_parfait(:data_object)
end
def source def source
load_parfait(:integer) load_parfait(:integer)
end end
def test_load def test_load
assert source.include?("class Integer") assert source.include?("class Integer")
assert source.length > 2000 assert source.length > 1500 , source.length
end end
def qtest_vool def test_vool
vool = compiler.ruby_to_vool source vool = @compiler.ruby_to_vool source
assert_equal Vool::ClassStatement , vool.class assert_equal Vool::ScopeStatement , vool.class
assert_equal :Object , vool.name 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 end
def qtest_mom def test_mom
mom = compiler.ruby_to_mom source mom = @compiler.ruby_to_mom source
assert_equal Mom::MomCollection , mom.class assert_equal Mom::MomCollection , mom.class
end end
def qtest_risc def test_risc
risc = compiler.ruby_to_risc source risc = compiler.ruby_to_risc source
assert_equal Risc::RiscCollection , risc.class assert_equal Risc::RiscCollection , risc.class
end end
def qtest_binary def test_binary
risc = compiler.ruby_to_binary source , :interpreter risc = compiler.ruby_to_binary source , :interpreter
assert_equal Risc::Linker , risc.class assert_equal Risc::Linker , risc.class
end end