changing to ruby instance variables

from the fake memory
This commit is contained in:
Torsten Ruger 2016-12-29 18:47:45 +02:00
parent b5f04ec718
commit 6214040888
4 changed files with 51 additions and 55 deletions

View File

@ -4,20 +4,16 @@
module Parfait module Parfait
module Behaviour module Behaviour
# when included we set up the instance_methods attribute
def self.included(base)
base.attribute :instance_methods
end
def initialize def initialize
super() super()
self.instance_methods = List.new @instance_methods = List.new
end end
def methods def methods
m = self.instance_methods m = @instance_methods
return m if m return m if m
self.instance_methods = List.new @instance_methods = List.new
end end
def method_names def method_names
@ -49,7 +45,7 @@ module Parfait
raise "resolve_method #{m_name}.#{m_name.class}" unless m_name.is_a?(Symbol) raise "resolve_method #{m_name}.#{m_name.class}" unless m_name.is_a?(Symbol)
method = get_instance_method(m_name) method = get_instance_method(m_name)
return method if method return method if method
if( self.super_class_name ) if( @super_class_name != :Object )
method = self.super_class.resolve_method(m_name) method = self.super_class.resolve_method(m_name)
end end
method method

View File

@ -16,20 +16,17 @@
module Parfait module Parfait
class Class < Object class Class < Object
include Behaviour include Behaviour
attributes [:instance_type , :name , :super_class_name , :instance_names] def self.attributes
[:instance_type , :name , :super_class_name , :instance_names , :instance_methods]
def initialize name , superclass
super()
self.name = name
self.super_class_name = superclass
# the type for this class (class = object of type Class) carries the class
# as an instance. The relation is from an object through the Type to it's class
# TODO the object type should copy the stuff from superclass
self.instance_type = Type.new(self)
end end
def allocate_object attr_reader :instance_type , :name , :instance_methods , :super_class_name
#space, and ruby allocate
def initialize( name , superclass , instance_type)
super()
@name = name
@super_class_name = superclass
@instance_type = instance_type
end end
def sof_reference_name def sof_reference_name
@ -43,21 +40,13 @@ module Parfait
# setting the type generates all methods for this type # setting the type generates all methods for this type
# (or will do, once we storet the methods code to do that) # (or will do, once we storet the methods code to do that)
def set_instance_type( type ) def set_instance_type( type )
self.instance_type = type @instance_type = type
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...
def set_super_class_name sup
raise "super_class_name must be a name, not #{sup}" unless sup.is_a?(Symbol)
self.super_class_name = sup
end end
def super_class def super_class
raise "No super_class for class #{self.name}" unless self.super_class_name raise "No super_class for class #{@name}" unless @super_class_name
s = Parfait::Space.object_space.get_class_by_name(self.super_class_name) s = Parfait::Space.object_space.get_class_by_name(@super_class_name)
raise "superclass not found for class #{self.name} (#{self.super_class_name})" unless s raise "superclass not found for class #{@name} (#{@super_class_name})" unless s
s s
end end

View File

@ -2,25 +2,29 @@
module Parfait module Parfait
class Dictionary < Object class Dictionary < Object
attribute :keys def self.attributes
attribute :values [:keys, :values]
end
# only empty initialization for now # only empty initialization for now
# #
# internally we store keys and values in lists, which means this does **not** scale well # internally we store keys and values in lists, which means this does **not** scale well
def initialize def initialize
super() super()
self.keys = List.new() @keys = List.new()
self.values = List.new() @values = List.new()
end end
attr_reader :values , :keys #FIXME these should be dupped, not handed out
# are there any key/value items in the list # are there any key/value items in the list
def empty? def empty?
self.keys.empty? @keys.empty?
end end
# How many key/value pairs there are # How many key/value pairs there are
def length() def length()
return self.keys.get_length() return @keys.get_length()
end end
# get a value fot the given key # get a value fot the given key
@ -29,7 +33,7 @@ module Parfait
def get(key) def get(key)
index = key_index(key) index = key_index(key)
if( index ) if( index )
self.values.get(index) @values.get(index)
else else
nil nil
end end
@ -42,11 +46,11 @@ module Parfait
# private method # private method
def key_index(key) def key_index(key)
len = self.keys.get_length() len = @keys.get_length()
index = 1 index = 1
found = nil found = nil
while(index <= len) while(index <= len)
if( self.keys.get(index) == key) if( @keys.get(index) == key)
found = index found = index
break break
end end
@ -59,10 +63,10 @@ module Parfait
def set(key , value) def set(key , value)
index = key_index(key) index = key_index(key)
if( index ) if( index )
self.keys.set(index , value) @keys.set(index , value)
else else
self.keys.push(key) @keys.push(key)
self.values.push(value) @values.push(value)
end end
value value
end end
@ -75,9 +79,9 @@ module Parfait
# yield to each key value pair # yield to each key value pair
def each def each
index = 1 index = 1
while index <= self.keys.get_length while index <= @keys.get_length
key = self.keys.get(index) key = @keys.get(index)
value = self.values.get(index) value = @values.get(index)
yield key , value yield key , value
index = index + 1 index = index + 1
end end

View File

@ -9,20 +9,27 @@
module Parfait module Parfait
class Message < Object class Message < Object
attributes [:next_message , :receiver , :locals , :return_address ] def self.attributes
attributes [:return_value, :caller , :name , :arguments] [:next_message , :receiver , :locals , :return_address ,
:return_value, :caller , :name , :arguments]
end
attr_reader :locals , :receiver , :return_value , :name
attr_accessor :next_message
def initialize next_m def initialize next_m
self.next_message = next_m @next_message = next_m
self.locals = NamedList.new() @locals = NamedList.new()
self.arguments = NamedList.new() @arguments = NamedList.new()
self.caller = nil
super() super()
end end
def set_receiver(rec)
@receiver = rec
end
def set_caller caller def set_caller(caller)
self.caller = caller @caller = caller
end end
def get_type_for(name) def get_type_for(name)