From af94d40cabf8d77ecdfb40acb955cedbc2d7c190 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 18 Mar 2018 22:09:27 +0530 Subject: [PATCH] passing frame (locals) into method creation so typed_method have correct frame information and can resolve slots correctly (next step) --- lib/parfait/type.rb | 12 +++++++---- lib/parfait/typed_method.rb | 5 +++-- lib/parfait/vool_method.rb | 2 +- test/vool/compilers/test_method_compiler.rb | 24 +++++++++++++++------ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/parfait/type.rb b/lib/parfait/type.rb index 2954ec64..f85d129d 100644 --- a/lib/parfait/type.rb +++ b/lib/parfait/type.rb @@ -74,14 +74,18 @@ module Parfait names end - def create_method( method_name , arguments ) + def create_method( method_name , arguments , frame) raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol) #puts "Self: #{self.class} clazz: #{clazz.name}" found = get_method( method_name ) + #TODO check types and then what ? return found if found - arg_type = arguments - arg_type = NamedList.type_for( arguments ) if arguments.is_a?(Hash) - add_method TypedMethod.new( self , method_name , arg_type ) + if arguments.is_a?(Hash) + raise "May want to get rid of this way" + arguments = NamedList.type_for( arguments ) + end + raise "frame must be a type, not:#{frame}" unless frame.is_a?(Type) + add_method TypedMethod.new( self , method_name , arguments , frame ) end def add_method( method ) diff --git a/lib/parfait/typed_method.rb b/lib/parfait/typed_method.rb index b57c8053..5c38ea2f 100644 --- a/lib/parfait/typed_method.rb +++ b/lib/parfait/typed_method.rb @@ -26,16 +26,17 @@ module Parfait # not part of the parfait model, hence ruby accessor attr_accessor :source - def initialize( type , name , arguments ) + def initialize( type , name , arguments , frame) super() raise "No class #{name}" unless type raise "For type, not class #{type}" unless type.is_a?(Type) raise "Wrong argument type, expect Type not #{arguments.class}" unless arguments.is_a? Type + raise "Wrong frame type, expect Type not #{frame.class}" unless frame.is_a? Type @for_type = type @name = name @binary = BinaryCode.new 0 @arguments = arguments - @frame = Parfait.object_space.get_class_by_name( :NamedList ).instance_type + @frame = frame end def set_instructions(inst) diff --git a/lib/parfait/vool_method.rb b/lib/parfait/vool_method.rb index a03f9392..58888a0e 100644 --- a/lib/parfait/vool_method.rb +++ b/lib/parfait/vool_method.rb @@ -28,7 +28,7 @@ module Parfait def create_parfait_method( type ) raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type - type.create_method( @name , @args_type )#FIXME, @frame_type) + type.create_method( @name , @args_type , @frame_type) end def create_tmp diff --git a/test/vool/compilers/test_method_compiler.rb b/test/vool/compilers/test_method_compiler.rb index 9ede1f91..618923b3 100644 --- a/test/vool/compilers/test_method_compiler.rb +++ b/test/vool/compilers/test_method_compiler.rb @@ -50,16 +50,28 @@ module Vool assert_equal Parfait::Class , clazz.body.clazz.class end - def test_method_statement_has_class_in_main - clazz = VoolCompiler.ruby_to_vool in_Space("def meth; @ivar = 5;end") - assert clazz.body.clazz + def test_typed_method_instance_type + VoolCompiler.ruby_to_vool in_Test("def meth; @ivar = 5; @ibar = 4;end") + test = Parfait.object_space.get_class_by_name(:Test) + method = test.instance_type.get_method(:meth) + assert_equal 2, method.for_type.variable_index(:ivar) + assert_equal 3, method.for_type.variable_index(:ibar) end - def test_method_has_one_local - VoolCompiler.ruby_to_vool in_Test("def meth; local = 5 ;end") + def test_vool_method_has_one_local + VoolCompiler.ruby_to_vool in_Test("def meth; local = 5 ; a = 6;end") test = Parfait.object_space.get_class_by_name(:Test) method = test.get_method(:meth) - assert_equal 2 , method.frame_type.instance_length + assert_equal 3 , method.frame_type.instance_length + assert_equal 2 , method.frame_type.variable_index(:local) + end + + def test_typed_method_has_one_local + VoolCompiler.ruby_to_vool in_Test("def meth; local = 5 ; a = 6;end") + test = Parfait.object_space.get_class_by_name(:Test) + method = test.instance_type.get_method(:meth) + assert_equal 3 , method.frame.instance_length + assert_equal 2 , method.frame.variable_index(:local) end end