moving to instance variables in parfait

This commit is contained in:
2019-09-09 20:26:54 +03:00
parent fc8de10964
commit 81e3c0c270
21 changed files with 212 additions and 187 deletions

View File

@ -1,31 +1,43 @@
require_relative "fake_memory"
module Parfait
class Object
module Parfait
class Object ; end
class DataObject < Object
def self.allocate
r = super
r.instance_variable_set(:@memory , Risc::FakeMemory.new(r ,self.type_length , self.memory_size))
r
end
# 0 -based index
def get_internal_word(index)
@memory[index]
super(index) if index < self.class.type_length
@memory[ index ]
end
# 0 -based index
def set_internal_word(index , value)
@memory[index] = value
value
return super(index,value) if index < self.class.type_length
@memory[ index ] = value
end
end
class Object
# 0 -based index
def get_internal_word(index)
return @type if index == Parfait::TYPE_INDEX
name = Parfait.name_for_index(self , index)
return nil unless name
instance_eval("@#{name}")
end
def self.attr( *attributes )
attributes = [attributes] unless attributes.is_a?(Array)
attributes.each do |name|
define_getter(name)
define_setter(name)
end
# 0 -based index
def set_internal_word(index , value)
name = Parfait.name_for_index(self , index)
raise "no string #{name.class}" unless name.is_a?(Symbol)
instance_eval("@#{name}=value" )
value
end
def self.variable_index( name)
@ -39,22 +51,6 @@ module Parfait
i + 1
end
def self.define_getter(name)
index = variable_index(name)
define_method(name) do
#puts "GETTING #{name} for #{self.class.name} in #{object_id.to_s(16)}"
@memory._get(index)
end
end
def self.define_setter(name)
index = variable_index(name)
define_method("#{name}=".to_sym ) do |value|
#puts "SETTING #{name}= for #{self.class.name} in #{object_id.to_s(16)}"
@memory._set(index , value)
end
end
def self.cattr( *names )
names.each do |ca|
class_eval "@@#{ca} = 0"

View File

@ -181,4 +181,13 @@ module Parfait
Word: {char_length: :Integer , next_word: :Word} ,
}
end
def self.name_for_index(object , index)
return :type if index == 0
clazz = object.class.name.split("::").last.to_sym
cl = self.type_names[clazz]
keys = cl.keys
keys[index - 1] # -1 because type is excluded in the lists (FIX)
# FIXME Now that we use instance variables in parfait, they should be parsed
# and the type_names generated automatically
end
end