From 16b3a77350a758b00d174920e6ff057839c208fc Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 13 Dec 2016 18:49:45 +0200 Subject: [PATCH] fixes from the method argument change Affects method creation argument manipulation compiler / to_code --- lib/typed/compiler.rb | 12 +++--------- lib/typed/compiler/function_statement.rb | 4 +--- lib/typed/compiler/name_expression.rb | 5 +++-- lib/typed/tree/to_code.rb | 14 ++++++++++---- test/typed/expressions/test_vars.rb | 2 +- test/typed/parfait/test_class.rb | 5 +++-- test/typed/parfait/test_meta.rb | 5 +++-- test/typed/statements/test_assign.rb | 4 ++-- 8 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/typed/compiler.rb b/lib/typed/compiler.rb index b0c9f7c1..4460e73f 100644 --- a/lib/typed/compiler.rb +++ b/lib/typed/compiler.rb @@ -104,16 +104,10 @@ module Typed # return the compiler (for chaining) def create_method_for clazz , method_name , args @clazz = clazz + raise "Args must be Hash #{args}" unless args.is_a?(Hash) raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol - arguments = [] - if( args.is_a? Array) - arguments = args - else - args.each do | name , type | - arguments << Parfait::Variable.new( type , name.to_sym) - end - end - @method = clazz.create_instance_method( method_name , Parfait.new_list(arguments)) + arguments = Parfait::Type.new_for_hash( clazz , args ) + @method = clazz.create_instance_method( method_name , arguments) self end diff --git a/lib/typed/compiler/function_statement.rb b/lib/typed/compiler/function_statement.rb index 39876e88..9673a220 100644 --- a/lib/typed/compiler/function_statement.rb +++ b/lib/typed/compiler/function_statement.rb @@ -5,9 +5,7 @@ module Typed # return_type , name , parameters, kids , receiver = *statement raise "Already in method #{@method}" if @method - args = statement.parameters.collect do |p| - Parfait::Variable.new( *p ) - end + args = statement.parameters class_method = handle_receiver( statement ) #nil for instance method diff --git a/lib/typed/compiler/name_expression.rb b/lib/typed/compiler/name_expression.rb index bd4ea197..5168e49f 100644 --- a/lib/typed/compiler/name_expression.rb +++ b/lib/typed/compiler/name_expression.rb @@ -12,7 +12,8 @@ module Typed end # either an argument, so it's stored in message if( index = @method.has_arg(name)) - ret = use_reg @method.arguments[index].value_type + ret = use_reg @method.argument_type(index) + #puts "For #{name} at #{index} got #{@method.arguments.inspect}" add_code Register.get_slot(statement , :message , Parfait::Message.get_indexed(index), ret ) return ret end @@ -24,7 +25,7 @@ module Typed def handle_local statement index = @method.has_local( statement.name ) - raise "must define variable '#{name}' before using it" unless index + raise "must define variable '#{statement.name}' before using it" unless index frame = use_reg :Frame add_code Register.get_slot(statement , :message , :frame , frame ) ret = use_reg @method.locals[index].value_type diff --git a/lib/typed/tree/to_code.rb b/lib/typed/tree/to_code.rb index d8480690..26c07e96 100644 --- a/lib/typed/tree/to_code.rb +++ b/lib/typed/tree/to_code.rb @@ -25,15 +25,21 @@ module Typed w = Tree::FunctionStatement.new() w.return_type = return_type w.name = name.children.first - w.parameters = parameters.to_a.collect do |p| - raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter - p.children - end + w.parameters = process parameters w.statements = process(statements) w.receiver = receiver w end + def on_parameters statement + params = {} + statement.children.each do |param , type , name| + type , name = *param + params[name] = type + end + params + end + def on_field_def statement type , name , value = *statement w = Tree::FieldDef.new() diff --git a/test/typed/expressions/test_vars.rb b/test/typed/expressions/test_vars.rb index 1e61cbd2..a77d9832 100644 --- a/test/typed/expressions/test_vars.rb +++ b/test/typed/expressions/test_vars.rb @@ -24,7 +24,7 @@ module Register end def test_args - Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :bar) + Register.machine.space.get_main.add_argument(:bar , :Integer) @input = s(:name, :bar) @output = Register::RegisterValue check diff --git a/test/typed/parfait/test_class.rb b/test/typed/parfait/test_class.rb index 6d347ffe..261b2d28 100644 --- a/test/typed/parfait/test_class.rb +++ b/test/typed/parfait/test_class.rb @@ -8,7 +8,7 @@ class TestClass < MiniTest::Test end def foo_method for_class = :Try - args = Parfait.new_list [ Parfait::Variable.new(:Integer , :bar )] + args = Parfait::Type.new_for_hash( @try , { bar: :Integer}) ::Parfait::TypedMethod.new @space.get_class_by_name(for_class) , :foo , args end @@ -42,7 +42,8 @@ class TestClass < MiniTest::Test end end def test_create_method - @try.create_instance_method :bar, Parfait.new_list( [ Parfait::Variable.new(:Integer , :bar )]) + args = Parfait::Type.new_for_hash( @try , { bar: :Integer}) + @try.create_instance_method :bar, args assert_equal ":bar" , @try.method_names.inspect end def test_method_get diff --git a/test/typed/parfait/test_meta.rb b/test/typed/parfait/test_meta.rb index 37b98709..b245f7cd 100644 --- a/test/typed/parfait/test_meta.rb +++ b/test/typed/parfait/test_meta.rb @@ -8,7 +8,7 @@ class TestMeta < MiniTest::Test end def foo_method for_class = :Try - args = Parfait.new_list [ Parfait::Variable.new(:Integer , :bar )] + args = Parfait::Type.new_for_hash( @try , { bar: :Integer}) ::Parfait::TypedMethod.new @space.get_class_by_name(for_class) , :foo , args end @@ -24,7 +24,8 @@ class TestMeta < MiniTest::Test end def test_create_method - @try.create_instance_method :bar, Parfait.new_list( [ Parfait::Variable.new(:Integer , :bar )]) + args = Parfait::Type.new_for_hash( @try , { bar: :Integer}) + @try.create_instance_method :bar, args assert_equal ":bar" , @try.method_names.inspect end def test_add_method diff --git a/test/typed/statements/test_assign.rb b/test/typed/statements/test_assign.rb index f95d2b3f..f9824a98 100644 --- a/test/typed/statements/test_assign.rb +++ b/test/typed/statements/test_assign.rb @@ -58,7 +58,7 @@ class TestAssignStatement < MiniTest::Test end def test_assign_arg - Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :blar) + Register.machine.space.get_main.add_argument(:blar , :Integer) @input = s(:statements, s(:assignment, s(:name, :blar), s(:int, 5))) @expect = [Label, LoadConstant, SetSlot, Label, FunctionReturn] was = check @@ -69,7 +69,7 @@ class TestAssignStatement < MiniTest::Test def test_arg_get # have to define bar externally, just because redefining main. Otherwise that would be automatic - Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :balr) + Register.machine.space.get_main.add_argument(:balr , :Integer) @input = s(:statements, s(:return, s(:name, :balr))) @expect = [Label, GetSlot, SetSlot, Label, FunctionReturn] was = check