move typed method's home to type

class should in the future have ruby_methods
This commit is contained in:
Torsten Ruger 2016-12-14 13:21:55 +02:00
parent 012b5d683c
commit bd875be023
4 changed files with 76 additions and 19 deletions

View File

@ -44,14 +44,6 @@ module Parfait
"Class(#{name})"
end
def create_instance_method method_name , arguments
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
clazz = instance_type().object_class()
raise "??? #{method_name}" unless clazz
#puts "Self: #{self.class} clazz: #{clazz.name}"
add_instance_method TypedMethod.new( clazz , method_name , arguments )
end
# this needs to be done during booting as we can't have all the classes and superclassses
# instantiated. By that logic it should maybe be part of vm rather.
# On the other hand vague plans to load the hierachy from sof exist, so for now...

View File

@ -53,17 +53,17 @@ module Parfait
def get_main
kernel = get_class_by_name :Space
kernel.get_instance_method :main
kernel.instance_type.get_instance_method :main
end
def get_init
kernel = get_class_by_name :Kernel
kernel.get_instance_method :__init__
kernel.instance_type.get_instance_method :__init__
end
# get a class by name (symbol)
# return nili if no such class. Use bang version if create should be implicit
def get_class_by_name name
def get_class_by_name( name )
raise "get_class_by_name #{name}.#{name.class}" unless name.is_a?(Symbol)
c = self.classes[name]
#puts "MISS, no class #{name} #{name.class}" unless c # " #{self.classes}"

View File

@ -34,9 +34,7 @@
module Parfait
class Type < Object
attribute :object_class
include Behaviour
attributes [:object_class , :instance_methods]
include Indexed
self.offset(3)
@ -65,6 +63,72 @@ module Parfait
hash.each do |name , type|
private_add_instance_variable(name , type) unless name == :type
end if hash
self.instance_methods = List.new
end
def methods
m = self.instance_methods
return m if m
self.instance_methods = List.new
end
def method_names
names = List.new
self.methods.each do |method|
names.push method.name
end
names
end
def create_instance_method( method_name , arguments )
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
#puts "Self: #{self.class} clazz: #{clazz.name}"
add_instance_method TypedMethod.new( self , method_name , arguments )
end
def add_instance_method( method )
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? TypedMethod
raise "syserr #{method.name.class}" unless method.name.is_a? Symbol
if self.is_a?(Class) and (method.for_type != self)
raise "Adding to wrong class, should be #{method.for_class}"
end
found = get_instance_method( method.name )
if found
self.methods.delete(found)
end
self.methods.push method
#puts "#{self.name} add #{method.name}"
method
end
def remove_instance_method method_name
found = get_instance_method( method_name )
if found
self.methods.delete(found)
else
raise "No such method #{method_name} in #{self.name}"
end
return true
end
def get_instance_method fname
raise "get_instance_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol)
#if we had a hash this would be easier. Detect or find would help too
self.methods.each do |m|
return m if(m.name == fname )
end
nil
end
# get the method and if not found, try superclasses. raise error if not found
def resolve_method m_name
raise "resolve_method #{m_name}.#{m_name.class}" unless m_name.is_a?(Symbol)
method = get_instance_method(m_name)
return method if method
if( self.super_class_name )
method = self.super_class.resolve_method(m_name)
end
method
end
def == other

View File

@ -15,22 +15,23 @@
# - binary: The binary (jumpable) code that the instructions get assembled into
# - arguments: A type object describing the arguments (name+types) to be passed
# - locals: A type object describing the local variables that the method has
# - for_class: The class the Method is for (TODO, should be Type)
# - for_type: The Type the Method is for
module Parfait
class TypedMethod < Object
attributes [:name , :source , :instructions , :binary ,:arguments , :for_class, :locals ]
attributes [:name , :source , :instructions , :binary ,:arguments , :for_type, :locals ]
# not part of the parfait model, hence ruby accessor
attr_accessor :source
def initialize( clazz , name , arguments )
def initialize( type , name , arguments )
super()
raise "No class #{name}" unless clazz
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
self.for_class = clazz
self.for_type = type
self.name = name
self.binary = BinaryCode.new 0
self.arguments = arguments