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 class ClassMethodStatement < MethodStatement
def to_vool 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 end
def to_s(depth = 0) def to_s(depth = 0)

View File

@ -8,13 +8,17 @@ module Ruby
@clazz = clazz @clazz = clazz
end end
def to_vool # At the moment normalizing means creating implicit returns for some cases
if @body.is_a?(Statements) # see replace_return for details.
@body << replace_return( @body.pop ) def normalized_body
else return replace_return( @body ) unless @body.is_a?(Statements)
@body = replace_return( @body ) body = Statements.new( @body.statements.dup )
body << replace_return( body.pop )
end end
Vool::MethodExpression.new( @name , @args.dup , @body.to_vool)
def to_vool
body = normalized_body
Vool::MethodExpression.new( @name , @args.dup , body.to_vool)
end end
def replace_return(statement) def replace_return(statement)

View File

@ -20,7 +20,7 @@ module Risc
def test_method_has_source def test_method_has_source
method = create_method method = create_method
assert_equal Vool::ScopeStatement , method.source.class assert_equal Vool::Statements , method.source.class
end end
def test_method_has_no_locals def test_method_has_no_locals

View File

@ -49,7 +49,26 @@ module Ruby
assert_equal [:arg1, :arg2] , @lst.args assert_equal [:arg1, :arg2] , @lst.args
end end
def test_body_is_scope_zero_statement 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
end end
class TestClassMethodStatement < MiniTest::Test class TestClassMethodStatement < MiniTest::Test

View File

@ -2,7 +2,7 @@ require_relative "helper"
module RubyX module RubyX
class TestDatObjectCompile #< MiniTest::Test class TestDatObjectCompile < MiniTest::Test
include ParfaitHelper include ParfaitHelper
def setup def setup
@compiler = compiler @compiler = compiler
@ -17,23 +17,23 @@ module RubyX
end end
def test_vool def test_vool
vool = @compiler.ruby_to_vool source vool = @compiler.ruby_to_vool source
assert_equ Vool::ScopeStatement , vool.class assert_equal Vool::ScopeStatement , vool.class
assert_equal Vool::ClassStatement , vool[0].class assert_equal Vool::ClassExpression , vool[0].class
assert_equal Vool::ScopeStatement , vool[1].class assert_equal Vool::ClassExpression , vool[1].class
assert_equal Vool::ClassStatement , vool[1][0].class assert_equal Vool::ClassExpression , vool[2].class
assert_equal :DataObject , vool[1][0].name assert_equal :DataObject , vool[1].name
assert_equal :Data4 , vool[1][1].name assert_equal :Data4 , vool[2].name
assert_equal :Data8 , vool[1][2].name assert_equal :Data8 , vool[3].name
end end
def test_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

View File

@ -26,22 +26,21 @@ module RubyX
assert_equal 22 , linker.assemblers.length assert_equal 22 , linker.assemblers.length
end end
end end
class TestRubyXCompilerParfait #< MiniTest::Test class TestRubyXCompilerParfait < MiniTest::Test
include ScopeHelper include ScopeHelper
include RubyXHelper include RubyXHelper
def setup def setup
super super
code = "class Space ; def self.class_method(); return 1; end;def main(arg);return Space.class_method;end; end" 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 end
def test_load def test_load
object = Parfait.object_space.get_class_by_name(:Object) object = Parfait.object_space.get_class_by_name(:Object)
assert_equal Parfait::Class , object.class #object.methods.each_method {|m| puts m.name}
object = object.instance_type assert_equal Parfait::VoolMethod , object.get_method(:set_type).class
object.methods.each_method {|m| puts m.name} assert_equal Parfait::CallableMethod , object.instance_type.get_method(:set_type).class
assert_equal Parfait::Type , object.get_method(:set_type)
end end
end end
end end