From d899d542a44b199e430d9cecd4480f1d331fafba Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 14 Oct 2015 14:02:34 +0300 Subject: [PATCH] checking return type is a type --- lib/phisol/compiler/call_site.rb | 2 +- lib/phisol/compiler/function_definition.rb | 7 ++----- lib/register/builtin/integer.rb | 6 +++--- lib/register/builtin/kernel.rb | 6 +++--- lib/virtual/method_source.rb | 11 ++++++++--- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/phisol/compiler/call_site.rb b/lib/phisol/compiler/call_site.rb index 36841660..8c89a29c 100644 --- a/lib/phisol/compiler/call_site.rb +++ b/lib/phisol/compiler/call_site.rb @@ -56,7 +56,7 @@ module Phisol raise "unimplemented: \n#{code} \nfor #{ref.inspect}" end else - if( me.type == :int) + if( me.type.is_a? Phisol::Integer) name = :plus if name == :+ method = Virtual.machine.space.get_class_by_name(:Integer).get_instance_method(name) puts Virtual.machine.space.get_class_by_name(:Integer).method_names.to_a diff --git a/lib/phisol/compiler/function_definition.rb b/lib/phisol/compiler/function_definition.rb index c42f3b9d..1a141fa5 100644 --- a/lib/phisol/compiler/function_definition.rb +++ b/lib/phisol/compiler/function_definition.rb @@ -40,13 +40,10 @@ module Phisol @method.source.receiver = r puts "compile method #{@method.name}" - - #frame = frame.new_frame kids.to_a.each do |ex| - return_type = process(ex) - raise return_type.inspect if return_type.is_a? Virtual::Instruction + ret = process(ex) + raise ret.inspect if ret.is_a? Virtual::Instruction end - @method.source.return_type = return_type @method = nil Virtual::Return.new(return_type) end diff --git a/lib/register/builtin/integer.rb b/lib/register/builtin/integer.rb index 11a7ea5a..377fe050 100644 --- a/lib/register/builtin/integer.rb +++ b/lib/register/builtin/integer.rb @@ -5,7 +5,7 @@ module Register module ClassMethods def plus c plus_function = Virtual::MethodSource.create_method(:Integer,:int,:plus , [:int] ) - plus_function.source.return_type = Phisol::Type.int + plus_function.source.set_return_type Phisol::Type.int plus_function.source.receiver = Phisol::Integer tmp = Register.tmp_reg :int @@ -44,7 +44,7 @@ module Register def putint context putint_function = Virtual::MethodSource.create_method(:Integer,:int,:putint , [] ) - putint_function.source.return_type = Phisol::Type.int + putint_function.source.set_return_type Phisol::Type.int putint_function.source.receiver = Phisol::Integer return putint_function # buffer = Parfait::Word.new(" ") # create a buffer @@ -73,7 +73,7 @@ module Register # not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html def fibo context fibo_function = Virtual::MethodSource.create_method(:Integer,:int,:fibo , [] ) - fibo_function.source.return_type = Phisol::Type.int + fibo_function.source.set_return_type Phisol::Type.int fibo_function.source.receiver = Phisol::Integer return fibo_function # result = fibo_function.return_type diff --git a/lib/register/builtin/kernel.rb b/lib/register/builtin/kernel.rb index 4a806100..29479f4c 100644 --- a/lib/register/builtin/kernel.rb +++ b/lib/register/builtin/kernel.rb @@ -7,7 +7,7 @@ module Register # so it is responsible for initial setup def __init__ context function = Virtual::MethodSource.create_method(:Kernel,:int,:__init__ , []) - function.source.return_type = Phisol::Type.int + function.source.set_return_type Phisol::Type.int # no method enter or return (automatically added), remove function.source.blocks.first.codes.pop # no Method enter function.source.blocks.last.codes.pop # no Method return @@ -26,7 +26,7 @@ module Register end def exit context function = Virtual::MethodSource.create_method(:Kernel,:int,:exit , []) - function.source.return_type = Phisol::Type.int + function.source.set_return_type Phisol::Type.int return function ret = Virtual::RegisterMachine.instance.exit(function) function.set_return ret @@ -34,7 +34,7 @@ module Register end def __send context function = Virtual::MethodSource.create_method(:Kernel,:int ,:__send , [] ) - function.source.return_type = Phisol::Type.int + function.source.set_return_type Phisol::Type.int return function end diff --git a/lib/virtual/method_source.rb b/lib/virtual/method_source.rb index e2de1e89..9175f66e 100644 --- a/lib/virtual/method_source.rb +++ b/lib/virtual/method_source.rb @@ -58,15 +58,20 @@ module Virtual def init method , return_type = nil # first block we have to create with .new , as new_block assumes a current enter = Block.new( "enter" , method ).add_code(MethodEnter.new( method )) - @return_type = return_type if return_type + set_return_type( return_type ) @blocks = [enter] @current = enter new_block("return").add_code(MethodReturn.new(method)) @constants = [] end - attr_reader :blocks , :constants - attr_accessor :return_type , :current , :receiver + attr_reader :blocks , :constants , :return_type + attr_accessor :current , :receiver + def set_return_type type + return if type.nil? + raise "not type #{type}" unless type.is_a? Phisol::Type + @return_type = type + end # add an instruction after the current (insertion point) # the added instruction will become the new insertion point def add_code instruction