Implicit returns for class methods

gets us parsing of parfaits second file data_object
This commit is contained in:
Torsten Rüger 2019-08-19 15:56:15 +03:00
parent d5d1df951c
commit 0e694a38f7
6 changed files with 48 additions and 25 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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