2018-05-28 13:46:26 +02:00
|
|
|
require_relative "fake_memory"
|
|
|
|
|
|
|
|
module Parfait
|
2018-08-11 18:13:09 +02:00
|
|
|
class Object
|
2018-05-28 13:46:26 +02:00
|
|
|
|
|
|
|
def self.allocate
|
|
|
|
r = super
|
2018-08-11 18:13:09 +02:00
|
|
|
r.instance_variable_set(:@memory , Risc::FakeMemory.new(r ,self.type_length , self.memory_size))
|
2018-05-28 13:46:26 +02:00
|
|
|
r
|
|
|
|
end
|
|
|
|
|
|
|
|
# 0 -based index
|
|
|
|
def get_internal_word(index)
|
|
|
|
@memory[index]
|
|
|
|
end
|
|
|
|
|
2018-05-28 17:20:09 +02:00
|
|
|
# 0 -based index
|
2018-05-28 13:46:26 +02:00
|
|
|
def set_internal_word(index , value)
|
|
|
|
@memory[index] = value
|
|
|
|
value
|
|
|
|
end
|
2018-08-11 18:13:09 +02:00
|
|
|
|
|
|
|
def self.attr( *attributes )
|
|
|
|
attributes = [attributes] unless attributes.is_a?(Array)
|
|
|
|
attributes.each do |name|
|
|
|
|
define_getter(name)
|
|
|
|
define_setter(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-12 12:09:34 +02:00
|
|
|
def self.variable_index( name)
|
|
|
|
if(name == :type)
|
|
|
|
return Parfait::TYPE_INDEX
|
|
|
|
end
|
|
|
|
clazz = self.name.split("::").last.to_sym
|
|
|
|
type = Parfait.type_names[clazz]
|
|
|
|
i = type.keys.index(name)
|
|
|
|
raise "no #{name} for #{clazz}:#{type.keys}" unless i
|
|
|
|
i + 1
|
|
|
|
end
|
|
|
|
|
2018-08-11 18:13:09 +02:00
|
|
|
def self.define_getter(name)
|
2018-08-12 12:09:34 +02:00
|
|
|
index = variable_index(name)
|
2018-08-11 18:13:09 +02:00
|
|
|
define_method(name) do
|
|
|
|
#puts "GETTING #{name} for #{self.class.name} in #{object_id.to_s(16)}"
|
2018-08-12 12:09:34 +02:00
|
|
|
@memory._get(index)
|
2018-08-11 18:13:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.define_setter(name)
|
2018-08-12 12:09:34 +02:00
|
|
|
index = variable_index(name)
|
2018-08-11 18:13:09 +02:00
|
|
|
define_method("#{name}=".to_sym ) do |value|
|
|
|
|
#puts "SETTING #{name}= for #{self.class.name} in #{object_id.to_s(16)}"
|
2018-08-12 12:09:34 +02:00
|
|
|
@memory._set(index , value)
|
2018-08-11 18:13:09 +02:00
|
|
|
end
|
|
|
|
end
|
2018-05-28 13:46:26 +02:00
|
|
|
end
|
2018-06-29 13:26:25 +02:00
|
|
|
|
|
|
|
# new list from ruby array to be precise
|
|
|
|
def self.new_list array
|
|
|
|
list = Parfait::List.new
|
|
|
|
list.set_length array.length
|
|
|
|
index = 0
|
|
|
|
while index < array.length do
|
|
|
|
list.set(index , array[index])
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
|
|
|
# Word from string
|
|
|
|
def self.new_word( string )
|
|
|
|
string = string.to_s if string.is_a? Symbol
|
|
|
|
word = Word.new( string.length )
|
|
|
|
string.codepoints.each_with_index do |code , index |
|
|
|
|
word.set_char(index , code)
|
|
|
|
end
|
|
|
|
word
|
|
|
|
end
|
|
|
|
|
2018-05-28 13:46:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
class Symbol
|
|
|
|
|
|
|
|
def has_type?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def get_type
|
|
|
|
l = Parfait.object_space.classes[:Word].instance_type
|
|
|
|
#puts "LL #{l.class}"
|
|
|
|
l
|
|
|
|
end
|
|
|
|
def padded_length
|
|
|
|
Padding.padded( to_s.length + 4)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|