moving to instance variables in parfait

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

View File

@ -18,7 +18,7 @@ module Parfait
def method_names def method_names
names = List.new names = List.new
self.methods.each do |method| methods.each do |method|
names.push method.name names.push method.name
end end
names names
@ -31,13 +31,13 @@ module Parfait
def remove_instance_method( method_name ) def remove_instance_method( method_name )
found = get_instance_method( method_name ) found = get_instance_method( method_name )
found ? self.methods.delete(found) : false found ? @methods.delete(found) : false
end end
def get_instance_method( fname ) def get_instance_method( fname )
raise "get_instance_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol) 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 #if we had a hash this would be easier. Detect or find would help too
self.instance_methods.find {|m| m.name == fname } @instance_methods.find {|m| m.name == fname }
end end
# get the method and if not found, try superclasses. raise error if not found # get the method and if not found, try superclasses. raise error if not found
@ -46,7 +46,7 @@ module Parfait
method = get_instance_method(m_name) method = get_instance_method(m_name)
return method if method return method if method
if( super_class_name && super_class_name != :Object ) if( super_class_name && super_class_name != :Object )
method = self.super_class.resolve_method(m_name) method = @super_class.resolve_method(m_name)
end end
method method
end end

View File

@ -7,7 +7,7 @@ module Parfait
# and as the last code of each link is a jump to the next link. # and as the last code of each link is a jump to the next link.
# #
class BinaryCode < Data32 class BinaryCode < Data32
attr :type, :next_code attr_reader :type, :next_code
def self.type_length def self.type_length
2 #type + next (could get from space, maybe later) 2 #type + next (could get from space, maybe later)
@ -27,11 +27,12 @@ module Parfait
def initialize(total_size) def initialize(total_size)
super() super()
self.next_code = nil @next_code = nil
extend_to(total_size ) extend_to(total_size )
(0 ... data_length).each{ |index| set_word(index , 0) } (0 ... data_length).each{ |index| set_word(index , 0) }
set_last(0) set_last(0)
end end
def extend_to(total_size) def extend_to(total_size)
return if total_size < data_length return if total_size < data_length
extend_one() unless next_code extend_one() unless next_code
@ -39,7 +40,7 @@ module Parfait
end end
def extend_one() def extend_one()
self.next_code = BinaryCode.new(1) @next_code = BinaryCode.new(1)
Risc::Position.get(self).trigger_inserted if Risc::Position.set?(self) Risc::Position.get(self).trigger_inserted if Risc::Position.set?(self)
end end
@ -107,9 +108,9 @@ module Parfait
set_internal_word(word_index , char) set_internal_word(word_index , char)
end end
def total_byte_length(start = 0 ) def total_byte_length(start = 0 )
start += self.byte_length start += byte_length
return start unless self.next_code return start unless self.next_code
self.next_code.total_byte_length(start) @next_code.total_byte_length(start)
end end
end end
end end

View File

@ -8,15 +8,15 @@
module Parfait module Parfait
class CacheEntry < Object class CacheEntry < Object
attr :type, :cached_type, :cached_method attr_reader :type, :cached_type, :cached_method
def initialize(type , method) def initialize(type , method)
self.cached_type = type @cached_type = type
self.cached_method = method @cached_method = method
end end
def to_s def to_s
"CacheEntry" + "#{cached_method&.name}" "CacheEntry" + "#{@cached_method&.name}"
end end
end end
end end

View File

@ -12,9 +12,9 @@ module Parfait
# #
class Callable < Object class Callable < Object
attr :type, :self_type , :arguments_type , :frame_type , :binary attr_reader :type, :self_type , :arguments_type , :frame_type , :binary
attr :blocks , :name attr_reader :blocks , :name
attr :next_callable attr_reader :next_callable
def self.type_length def self.type_length
8 8
@ -25,8 +25,8 @@ module Parfait
raise "No class #{self}" unless self_type raise "No class #{self}" unless self_type
raise "For type, not class #{self_type}" unless self_type.is_a?(Type) raise "For type, not class #{self_type}" unless self_type.is_a?(Type)
raise "Mixup" unless name.is_a?(Symbol) raise "Mixup" unless name.is_a?(Symbol)
self.name = name @name = name
self.self_type = self_type @self_type = self_type
init(arguments_type, frame_type) init(arguments_type, frame_type)
end end
@ -38,9 +38,9 @@ module Parfait
def init(arguments_type, frame_type) def init(arguments_type, frame_type)
raise "Wrong argument type, expect Type not #{arguments_type.class}" unless arguments_type.is_a? Type raise "Wrong argument type, expect Type not #{arguments_type.class}" unless arguments_type.is_a? Type
raise "Wrong frame type, expect Type not #{frame_type.class}" unless frame_type.is_a? Type raise "Wrong frame type, expect Type not #{frame_type.class}" unless frame_type.is_a? Type
self.arguments_type = arguments_type @arguments_type = arguments_type
self.frame_type = frame_type @frame_type = frame_type
self.binary = BinaryCode.new(0) @binary = BinaryCode.new(0)
end end
# determine if method has a local variable or tmp (anonymous local) by given name # determine if method has a local variable or tmp (anonymous local) by given name
@ -52,7 +52,7 @@ module Parfait
def add_local( name , type ) def add_local( name , type )
index = has_local( name ) index = has_local( name )
return index if index return index if index
self.frame_type = frame_type.add_instance_variable(name,type) @frame_type = frame_type.add_instance_variable(name,type)
end end
def each_binary( &block ) def each_binary( &block )
@ -64,7 +64,7 @@ module Parfait
end end
def set_next( method ) def set_next( method )
self.next_callable = method @next_callable = method
end end
end end

View File

@ -14,16 +14,16 @@ module Parfait
def ==(other) def ==(other)
return false unless other.is_a?(CallableMethod) return false unless other.is_a?(CallableMethod)
return false if name != other.name return false if @name != other.name
super super
end end
def rxf_reference_name def rxf_reference_name
"Method: " + name.to_s "Method: " + @name.to_s
end end
def inspect def inspect
"#{self_type.object_class.name}:#{name}(#{arguments_type.inspect})" "#{@self_type.object_class.name}:#{@name}(#{@arguments_type.inspect})"
end end
def each_method( &block ) def each_method( &block )
@ -32,14 +32,14 @@ module Parfait
end end
def create_block(args , frame) def create_block(args , frame)
block_name = "#{name}_block".to_sym #TODO with id, to distinguish block_name = "#{@name}_block".to_sym #TODO with id, to distinguish
add_block( Block.new(block_name , self_type , args , frame)) add_block( Block.new(block_name , @self_type , args , frame))
end end
def add_block(bl) def add_block(bl)
was = blocks was = blocks
bl.set_next(was) if(was) bl.set_next(was) if(was)
self.blocks = bl @blocks = bl
end end
def has_block(block) def has_block(block)
@ -47,7 +47,7 @@ module Parfait
false false
end end
def each_block(&bl) def each_block(&bl)
blo = blocks blo = @blocks
while( blo ) while( blo )
yield(blo) yield(blo)
blo = blo.next_callable blo = blo.next_callable

View File

@ -18,8 +18,8 @@ module Parfait
class Class < Object class Class < Object
include Behaviour include Behaviour
attr :type, :instance_type , :name , :instance_methods attr_reader :type, :instance_type , :name , :instance_methods
attr :super_class_name , :meta_class attr_reader :super_class_name , :meta_class
def self.type_length def self.type_length
6 6
@ -27,11 +27,11 @@ module Parfait
def initialize( name , superclass , instance_type) def initialize( name , superclass , instance_type)
super() super()
self.name = name @name = name
self.super_class_name = superclass @super_class_name = superclass
self.instance_methods = List.new @instance_methods = List.new
set_instance_type( instance_type ) set_instance_type( instance_type )
self.meta_class = MetaClass.new( self ) @meta_class = MetaClass.new( self )
end end
def rxf_reference_name def rxf_reference_name
@ -59,14 +59,14 @@ module Parfait
# adding an instance changes the instance_type to include that variable # adding an instance changes the instance_type to include that variable
def add_instance_variable( name , type) def add_instance_variable( name , type)
self.instance_type = instance_type.add_instance_variable( name , type ) @instance_type = instance_type.add_instance_variable( name , type )
end end
# setting the type generates all methods for this type # setting the type generates all methods for this type
# (or will do, once we store the methods code to do that) # (or will do, once we store the methods code to do that)
def set_instance_type( type ) def set_instance_type( type )
raise "type must be type #{type}" unless type.is_a?(Type) raise "type must be type #{type}" unless type.is_a?(Type)
self.instance_type = type @instance_type = type
end end
# return the super class, but raise exception if either the super class name # return the super class, but raise exception if either the super class name

View File

@ -3,7 +3,7 @@
module Parfait module Parfait
class Dictionary < Object class Dictionary < Object
attr :type, :i_keys , :i_values attr_reader :type, :i_keys , :i_values
def self.type_length def self.type_length
3 3
@ -14,30 +14,30 @@ module Parfait
# 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.i_keys = List.new() @i_keys = List.new()
self.i_values = List.new() @i_values = List.new()
end end
def keys def keys
i_keys.dup @i_keys.dup
end end
def values def values
i_values.dup @i_values.dup
end end
# are there any key/value items in the list # are there any key/value items in the list
def empty? def empty?
i_keys.empty? @i_keys.empty?
end end
# How many key/value pairs there are # How many key/value pairs there are
def length() def length()
return i_keys.get_length() return @i_keys.get_length()
end end
def next_value(val) def next_value(val)
return i_values.next_value(val) return @i_values.next_value(val)
end end
# get a value fot the given key # get a value fot the given key
@ -46,7 +46,7 @@ module Parfait
def get(key) def get(key)
index = key_index(key) index = key_index(key)
if( index ) if( index )
i_values.get(index) @i_values.get(index)
else else
nil nil
end end
@ -59,17 +59,17 @@ module Parfait
# private method # private method
def key_index(key) def key_index(key)
i_keys.index_of(key) @i_keys.index_of(key)
end end
# set key with value, returns value # set key with value, returns value
def set(key , value) def set(key , value)
index = key_index(key) index = key_index(key)
if( index ) if( index )
i_values.set(index , value) @i_values.set(index , value)
else else
i_keys.push(key) @i_keys.push(key)
i_values.push(value) @i_values.push(value)
end end
value value
end end
@ -82,9 +82,9 @@ module Parfait
# yield to each key value pair # yield to each key value pair
def each def each
index = 0 index = 0
while index < i_keys.get_length while index < @i_keys.get_length
key = i_keys.get(index) key = @i_keys.get(index)
value = i_values.get(index) value = @i_values.get(index)
yield key , value yield key , value
index = index + 1 index = index + 1
end end

View File

@ -14,15 +14,15 @@
module Parfait module Parfait
class Factory < Object class Factory < Object
attr :type , :for_type , :next_object , :reserve , :attribute_name , :page_size attr_reader :type , :for_type , :next_object , :reserve , :attribute_name , :page_size
# initialize for a given type (for_type). The attribute that is used to create the # initialize for a given type (for_type). The attribute that is used to create the
# list is the first that starts with next_ . "next" itself would have been nice and general # list is the first that starts with next_ . "next" itself would have been nice and general
# but is a keyword, so no go. # but is a keyword, so no go.
def initialize(type , page) def initialize(type , page)
self.for_type = type @for_type = type
self.attribute_name = type.names.find {|name| name.to_s.start_with?("next")} @attribute_name = type.names.find {|name| name.to_s.start_with?("next")}
self.page_size = page @page_size = page
raise "No next found for #{type.class_name}" unless attribute_name raise "No next found for #{type.class_name}" unless attribute_name
end end
@ -32,7 +32,7 @@ module Parfait
# used, as it get's called from risc (or will) # used, as it get's called from risc (or will)
def get_next_object def get_next_object
unless( next_object ) unless( next_object )
self.next_object = reserve @next_object = reserve
get_more get_more
end end
get_head get_head
@ -41,7 +41,7 @@ module Parfait
# this gets the head of the freelist, swaps it out agains the next and returns it # this gets the head of the freelist, swaps it out agains the next and returns it
def get_head def get_head
nekst = next_object nekst = next_object
self.next_object = get_next_for(nekst) @next_object = get_next_for(nekst)
return nekst return nekst
end end
@ -49,15 +49,15 @@ module Parfait
# and rebuilt the reserve (get_next already instantiates the reserve) # and rebuilt the reserve (get_next already instantiates the reserve)
# #
def get_more def get_more
self.reserve = get_chain @reserve = get_chain
last_link = self.reserve last_link = @reserve
count = self.page_size / 100 count = @page_size / 100
count = 15 if count < 15 count = 15 if count < 15
while(count > 0) while(count > 0)
last_link = get_next_for(last_link) last_link = get_next_for(last_link)
count -= 1 count -= 1
end end
self.next_object = get_next_for(last_link) @next_object = get_next_for(last_link)
set_next_for( last_link , nil ) set_next_for( last_link , nil )
self self
end end
@ -65,10 +65,10 @@ module Parfait
# this initiates the syscall to get more memory. # this initiates the syscall to get more memory.
# it creates objects from the mem and link them into a chain # it creates objects from the mem and link them into a chain
def get_chain def get_chain
raise "type is nil" unless self.for_type raise "type is nil" unless @for_type
first = sys_mem( for_type , self.page_size) first = sys_mem( for_type , @page_size)
chain = first chain = first
counter = self.page_size counter = @page_size
while( counter > 0) while( counter > 0)
nekst = get_next_raw( chain ) nekst = get_next_raw( chain )
set_next_for(chain, nekst) set_next_for(chain, nekst)
@ -87,7 +87,7 @@ module Parfait
# set the next_* attribute of the given object, with the value. # set the next_* attribute of the given object, with the value.
# the attribute name is determined in initialize # the attribute name is determined in initialize
def set_next_for(object , value) def set_next_for(object , value)
object.send("#{attribute_name}=".to_sym , value) object.send("_set_#{attribute_name}".to_sym , value)
end end
# Return the object _after the given one. In memory terms the next object starts # Return the object _after the given one. In memory terms the next object starts

View File

@ -6,11 +6,11 @@
module Parfait module Parfait
class Integer < Data4 class Integer < Data4
attr :type, :next_integer attr_reader :type, :next_integer
def initialize(value , next_i = nil) def initialize(value , next_i = nil)
super() super()
self.next_integer = next_i @next_integer = next_i
set_internal_word(Integer.integer_index, value) set_internal_word(Integer.integer_index, value)
end end
@ -39,6 +39,9 @@ module Parfait
set_internal_word(Integer.integer_index, value) set_internal_word(Integer.integer_index, value)
end end
def _set_next_integer(nekst)
@next_integer = nekst
end
# :integer?, :odd?, :even?, :upto, :downto, :times, :succ, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, # :integer?, :odd?, :even?, :upto, :downto, :times, :succ, :next, :pred, :chr, :ord, :to_i, :to_int, :floor,
# :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, # :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize,
# :singleton_method_added, :coerce, :i, :+, :-, :fdiv, :div, :divmod, :%, :modulo, :remainder, :abs, :magnitude, # :singleton_method_added, :coerce, :i, :+, :-, :fdiv, :div, :divmod, :%, :modulo, :remainder, :abs, :magnitude,

View File

@ -7,7 +7,7 @@
module Parfait module Parfait
class List < Data16 class List < Data16
attr :type, :indexed_length , :next_list attr_reader :type, :indexed_length , :next_list
def self.type_length def self.type_length
3 # 0 type , 1 length , 2 - next_list 3 # 0 type , 1 length , 2 - next_list
@ -18,20 +18,20 @@ module Parfait
def initialize def initialize
super super
self.indexed_length = 0 @indexed_length = 0
end end
def data_length def data_length
self.class.data_length self.class.data_length
end end
def get_length def get_length
r = indexed_length r = @indexed_length
r.nil? ? 0 : r r.nil? ? 0 : r
end end
def ensure_next def ensure_next
self.next_list = List.new unless next_list @next_list = List.new unless @next_list
self.next_list @next_list
end end
# set the value at index. # set the value at index.
@ -43,7 +43,7 @@ module Parfait
end end
if index >= data_length if index >= data_length
ensure_next ensure_next
next_list.set( index - data_length , value) @next_list.set( index - data_length , value)
else else
set_internal_word( index + self.class.type_length, value) set_internal_word( index + self.class.type_length, value)
end end
@ -54,8 +54,8 @@ module Parfait
def get( index ) def get( index )
raise "Only positive indexes, #{index}" if index < 0 raise "Only positive indexes, #{index}" if index < 0
if index >= data_length if index >= data_length
return nil unless next_list return nil unless @next_list
return next_list.get( index - data_length) return @next_list.get( index - data_length)
else else
ret = nil ret = nil
if(index < get_length) if(index < get_length)
@ -273,7 +273,7 @@ module Parfait
end end
private private
def internal_set_length( i ) def internal_set_length( i )
self.indexed_length = i @indexed_length = i
end end
end end

View File

@ -15,15 +15,15 @@ module Parfait
# :return_address => :Integer, :return_value => :Integer, # :return_address => :Integer, :return_value => :Integer,
# :caller => :Message , :name => :Word , :arguments => :NamedList # :caller => :Message , :name => :Word , :arguments => :NamedList
attr :type, :next_message attr_reader :type, :next_message
attr :receiver attr_reader :receiver
attr :return_address, :return_value attr_reader :return_address, :return_value
attr :caller , :method attr_reader :caller , :method
attr :arguments_given attr_reader :arguments_given
attr :arg1 , :arg2, :arg3, :arg4, :arg5, :arg6 attr_reader :arg1 , :arg2, :arg3, :arg4, :arg5, :arg6
attr :locals_used attr_reader :locals_used
attr :local1 , :local2, :local3, :local4, :local5, :local6 ,:local7,:local8 attr_reader :local1 , :local2, :local3, :local4, :local5, :local6 ,:local7,:local8
attr :local9 ,:local10, :local11 , :local12, :local13, :local14 attr_reader :local9 ,:local10, :local11 , :local12, :local13, :local14
def self.type_length def self.type_length
31 31
@ -40,25 +40,38 @@ module Parfait
def initialize( ) def initialize( )
super() super()
self.locals_used = Parfait::Integer.new(0) @locals_used = Parfait::Integer.new(0)
self.arguments_given = Parfait::Integer.new(0) @arguments_given = Parfait::Integer.new(0)
end end
public :initialize public :initialize
def set_receiver(rec) def set_receiver(rec)
self.receiver = rec @receiver = rec
end end
def set_caller(caller) def set_caller(caller)
caller = caller @caller = caller
end end
def get_type_for(name) def get_type_for(name)
index = type.get_index(name) index = @type.get_index(name)
get_at(index) get_at(index)
end end
def method_name
return "" unless @method
return "" unless @method == NilClass
@method.name
end
def to_s def to_s
"Message:#{method&.name}(#{arguments_given})" "Message:#{method_name}(#{@arguments_given})"
end
def _set_next_message(nekst)
@next_message = nekst
end
def _set_caller(prev)
@caller = prev
end end
end end
end end

View File

@ -20,7 +20,7 @@ module Parfait
class MetaClass < Object class MetaClass < Object
include Behaviour include Behaviour
attr :type, :instance_type , :instance_methods , :clazz attr_reader :type, :instance_type , :instance_methods , :clazz
def self.type_length def self.type_length
4 4
@ -28,17 +28,17 @@ module Parfait
def initialize( clazz ) def initialize( clazz )
super() super()
self.clazz = clazz @clazz = clazz
self.instance_methods = List.new @instance_methods = List.new
set_instance_type( clazz.get_type() ) set_instance_type( clazz.get_type() )
end end
def rxf_reference_name def rxf_reference_name
clazz.name @clazz.name
end end
def inspect def inspect
"MetaClass(#{clazz.name})" "MetaClass(#{@clazz.name})"
end end
def add_method_for(name , type , frame , body ) def add_method_for(name , type , frame , body )
@ -49,23 +49,23 @@ module Parfait
def add_method(method) def add_method(method)
raise "Must be untyped method #{method}" unless method.is_a? Parfait::VoolMethod raise "Must be untyped method #{method}" unless method.is_a? Parfait::VoolMethod
instance_methods.push(method) @instance_methods.push(method)
end end
def get_method(name) def get_method(name)
instance_methods.find{|m| m.name == name } @instance_methods.find{|m| m.name == name }
end end
# adding an instance changes the instance_type to include that variable # adding an instance changes the instance_type to include that variable
def add_instance_variable( name , type) def add_instance_variable( name , type)
self.instance_type = instance_type.add_instance_variable( name , type ) @instance_type = @instance_type.add_instance_variable( name , type )
end end
# setting the type generates all methods for this type # setting the type generates all methods for this type
# (or will do, once we store the methods code to do that) # (or will do, once we store the methods code to do that)
def set_instance_type( type ) def set_instance_type( type )
raise "type must be type #{type}" unless type.is_a?(Type) raise "type must be type #{type.class}:#{type}" unless type.is_a?(Type)
self.instance_type = type @instance_type = type
end end
# Nil name means no superclass, and so nil returned # Nil name means no superclass, and so nil returned

View File

@ -23,7 +23,7 @@
module Parfait module Parfait
class NamedList < Object class NamedList < Object
attr :type attr_reader :type
def self.memory_size def self.memory_size
16 16

View File

@ -14,7 +14,10 @@
module Parfait module Parfait
class Object class Object
attr :type attr_reader :type
def type=(t)
set_type( t )
end
def == other def == other
self.object_id == other.object_id self.object_id == other.object_id
@ -34,38 +37,39 @@ module Parfait
# private # private
def set_type(typ) def set_type(typ)
raise "not type" + typ.class.to_s + "in " + object_id.to_s(16) unless typ.is_a?(Type) raise "not type" + typ.class.to_s + "in " + object_id.to_s(16) unless typ.is_a?(Type)
self.type = typ @type = typ
end end
# so we can keep the raise in get_type # so we can keep the raise in get_type
def has_type? def has_type?
! type.nil? ! @type.nil?
end end
def get_type() def get_type()
raise "No type " + self.object_id.to_s(16) + ":" + self.class.name unless has_type? raise "No type " + self.object_id.to_s(16) + ":" + self.class.name unless has_type?
type @type
end end
def get_instance_variables def get_instance_variables
type.names @type.names
end end
def get_instance_variable( name ) def get_instance_variable( name )
index = instance_variable_defined(name) index = instance_variable_defined(name)
#puts "getting #{name} at #{index}" #raise "at :#{name}:" if name.to_s[0] == "@"
return nil if index == nil return nil if index == nil
return get_internal_word(index) return get_internal_word(index)
end end
def set_instance_variable( name , value ) def set_instance_variable( name , value )
index = instance_variable_defined(name) index = instance_variable_defined(name)
#puts "setting #{name} at #{index}"
return nil if index == nil return nil if index == nil
return set_internal_word(index , value) return set_internal_word(index , value)
end end
def instance_variable_defined( name ) def instance_variable_defined( name )
type.variable_index(name) @type.variable_index(name)
end end
# objects only come in lengths of multiple of 8 words / 32 bytes # objects only come in lengths of multiple of 8 words / 32 bytes
@ -81,7 +85,7 @@ module Parfait
end end
def padded_length def padded_length
Object.padded_words( type.instance_length ) Object.padded_words( @type.instance_length )
end end
# parfait versions are deliberately called different, so we "relay" # parfait versions are deliberately called different, so we "relay"

View File

@ -30,16 +30,16 @@ module Parfait
class Space < Object class Space < Object
attr :type, :classes , :types , :factories attr_reader :type, :classes , :types , :factories
attr :true_object , :false_object , :nil_object attr_reader :true_object , :false_object , :nil_object
def initialize( classes , pages) def initialize( classes , pages)
self.classes = classes @classes = classes
self.types = Dictionary.new @types = Dictionary.new
classes.each do |name , cl| classes.each do |name , cl|
add_type(cl.instance_type) add_type(cl.instance_type)
end end
self.factories = Dictionary.new @factories = Dictionary.new
[:Integer , :ReturnAddress , :Message].each do |fact_name| [:Integer , :ReturnAddress , :Message].each do |fact_name|
for_type = classes[fact_name].instance_type for_type = classes[fact_name].instance_type
page_size = pages[fact_name] || 1024 page_size = pages[fact_name] || 1024
@ -49,9 +49,9 @@ module Parfait
end end
init_message_chain( factories[ :Message ].reserve ) init_message_chain( factories[ :Message ].reserve )
init_message_chain( factories[ :Message ].next_object ) init_message_chain( factories[ :Message ].next_object )
self.true_object = Parfait::TrueClass.new @true_object = Parfait::TrueClass.new
self.false_object = Parfait::FalseClass.new @false_object = Parfait::FalseClass.new
self.nil_object = Parfait::NilClass.new @nil_object = Parfait::NilClass.new
end end
def self.type_length def self.type_length
@ -65,7 +65,7 @@ module Parfait
prev = nil prev = nil
while(message) while(message)
message.initialize message.initialize
message.caller = prev if prev message._set_caller(prev) if prev
prev = message prev = message
message = message.next_message message = message.next_message
end end
@ -73,18 +73,18 @@ module Parfait
# return the factory for the given type # return the factory for the given type
# or more exactly the type that has a class_name "name" # or more exactly the type that has a class_name "name"
def get_factory_for(name) def get_factory_for(name)
factories[name] @factories[name]
end end
# use the factory of given name to generate next_object # use the factory of given name to generate next_object
# just a shortcut basically # just a shortcut basically
def get_next_for(name) def get_next_for(name)
factories[name].get_next_object @factories[name].get_next_object
end end
# yield each type in the space # yield each type in the space
def each_type def each_type
types.values.each do |type| @types.values.each do |type|
yield(type) yield(type)
end end
end end
@ -100,7 +100,7 @@ module Parfait
# get a type by the type hash (the hash is what uniquely identifies the type) # get a type by the type hash (the hash is what uniquely identifies the type)
def get_type_for( hash ) def get_type_for( hash )
types[hash] @types[hash]
end end
# all methods form all types # all methods form all types
@ -137,7 +137,7 @@ module Parfait
# return nili if no such class. Use bang version if create should be implicit # 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) raise "get_class_by_name #{name}.#{name.class}" unless name.is_a?(Symbol)
c = classes[name] c = @classes[name]
#puts "MISS, no class #{name} #{name.class}" unless c # " #{classes}" #puts "MISS, no class #{name} #{name.class}" unless c # " #{classes}"
#puts "CLAZZ, #{name} #{c.get_type.get_length}" if c #puts "CLAZZ, #{name} #{c.get_type.get_length}" if c
c c
@ -159,7 +159,7 @@ module Parfait
raise "create_class #{superclass.class}" unless superclass.is_a? Symbol raise "create_class #{superclass.class}" unless superclass.is_a? Symbol
type = get_type_by_class_name(superclass) type = get_type_by_class_name(superclass)
c = Class.new(name , superclass , type ) c = Class.new(name , superclass , type )
classes[name] = c @classes[name] = c
end end
def rxf_reference_name def rxf_reference_name

View File

@ -36,7 +36,7 @@ module Parfait
class Type < Object class Type < Object
attr :type, :object_class , :names , :types , :methods attr_reader :type, :object_class , :names , :types , :methods
def self.type_length def self.type_length
5 5
@ -57,18 +57,18 @@ module Parfait
# this part of the init is seperate because at boot time we can not use normal new # this part of the init is seperate because at boot time we can not use normal new
# new is overloaded to grab the type from space, and before boot, that is not set up # new is overloaded to grab the type from space, and before boot, that is not set up
def init_lists(hash) def init_lists(hash)
self.methods = nil @methods = nil
self.names = List.new @names = List.new
self.types = List.new @types = List.new
raise "No type Type in #{hash}" unless hash[:type] raise "No type Type in #{hash}" unless hash[:type]
private_add_instance_variable(:type , hash[:type]) #first private_add_instance_variable(:type , hash[:type]) #first
hash.each do |name , type| hash.keys.each do |name |
private_add_instance_variable(name , type) unless name == :type private_add_instance_variable(name , hash[name]) unless name == :type
end end
end end
def class_name def class_name
object_class.name @object_class.name
end end
def to_s def to_s
@ -87,7 +87,7 @@ module Parfait
def method_names def method_names
names = List.new names = List.new
return names unless methods return names unless @methods
methods.each_method do |method| methods.each_method do |method|
names.push method.name names.push method.name
end end
@ -119,7 +119,7 @@ module Parfait
remove_method(method.name) remove_method(method.name)
end end
method.set_next( methods ) method.set_next( methods )
self.methods = method @methods = method
#puts "#{self.name} add #{method.name}" #puts "#{self.name} add #{method.name}"
method method
end end
@ -127,7 +127,7 @@ module Parfait
def remove_method( method_name ) def remove_method( method_name )
raise "No such method #{method_name} in #{self.name}" unless methods raise "No such method #{method_name} in #{self.name}" unless methods
if( methods.name == method_name) if( methods.name == method_name)
self.methods = methods.next_callable @methods = methods.next_callable
return true return true
end end
method = methods method = methods
@ -190,7 +190,7 @@ module Parfait
def set_object_class(oc) def set_object_class(oc)
raise "object class should be a class, not #{oc.class}" unless oc.is_a?(Class) raise "object class should be a class, not #{oc.class}" unless oc.is_a?(Class)
self.object_class = oc @object_class = oc
end end
def instance_length def instance_length

View File

@ -12,13 +12,13 @@ module Parfait
# #
class VoolMethod < Object class VoolMethod < Object
attr :type, :name , :args_type , :frame_type attr_reader :type, :name , :args_type , :frame_type
attr_reader :source attr_reader :source
def initialize(name , args_type , frame_type , source ) def initialize(name , args_type , frame_type , source )
self.name = name @name = name
self.args_type = args_type @args_type = args_type
self.frame_type = frame_type @frame_type = frame_type
@source = source @source = source
raise "Name must be symbol" unless name.is_a?(Symbol) raise "Name must be symbol" unless name.is_a?(Symbol)
raise "args_type must be type" unless args_type.is_a?(Parfait::Type) raise "args_type must be type" unless args_type.is_a?(Parfait::Type)
@ -29,7 +29,7 @@ module Parfait
def create_callable_method( type ) def create_callable_method( type )
raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type
type.create_method( name , args_type , frame_type) type.create_method( @name , @args_type , @frame_type)
end end
def compiler_for(self_type) def compiler_for(self_type)

View File

@ -13,7 +13,7 @@ module Parfait
# Object length is measured in non-type cells though # Object length is measured in non-type cells though
class Word < Data8 class Word < Data8
attr :type, :char_length attr_reader :type, :char_length
def self.type_length def self.type_length
2 # 0 type , 1 char_length 2 # 0 type , 1 char_length
@ -26,7 +26,7 @@ module Parfait
# Risc provides methods to create Parfait objects from ruby # Risc provides methods to create Parfait objects from ruby
def initialize( len ) def initialize( len )
super() super()
self.char_length = 0 @char_length = 0
raise "Must init with int, not #{len.class}" unless len.kind_of? ::Integer raise "Must init with int, not #{len.class}" unless len.kind_of? ::Integer
raise "Must init with positive, not #{len}" if len < 0 raise "Must init with positive, not #{len}" if len < 0
fill_to( len , 32 ) unless len == 0 #32 being ascii space fill_to( len , 32 ) unless len == 0 #32 being ascii space
@ -36,10 +36,10 @@ module Parfait
# return a copy of self # return a copy of self
def copy def copy
cop = Word.new( self.length ) cop = Word.new( @char_length )
index = 0 index = 0
while( index < self.length ) while( index < @char_length )
cop.set_char(index , self.get_char(index)) cop.set_char(index , get_char(index))
index = index + 1 index = index + 1
end end
cop cop
@ -47,8 +47,7 @@ module Parfait
# return the number of characters # return the number of characters
def length() def length()
obj_len = char_length @char_length
return obj_len
end end
# make every char equal the given one # make every char equal the given one
@ -57,7 +56,7 @@ module Parfait
end end
def fill_from_with( from , char ) def fill_from_with( from , char )
len = self.length() len = @char_length
return if from < 0 return if from < 0
while( from < len) while( from < len)
set_char( from , char) set_char( from , char)
@ -68,16 +67,16 @@ module Parfait
# true if no characters # true if no characters
def empty? def empty?
return self.length == 0 return @char_length == 0
end end
# pad the string with the given character to the given length # pad the string with the given character to the given length
# #
def fill_to(len , fill_char) def fill_to(len , fill_char)
return if len <= 0 return if len <= 0
old = char_length old = @char_length
return if old >= len return if old >= len
self.char_length = len @char_length = len
check_length check_length
fill_from_with( old , fill_char ) fill_from_with( old , fill_char )
end end
@ -170,7 +169,7 @@ module Parfait
string = "" string = ""
index = 0 index = 0
#puts "Length = #{char_length}" #puts "Length = #{char_length}"
while( index < char_length) while( index < @char_length)
char = get_char(index) char = get_char(index)
string += char ? char.chr : "*" string += char ? char.chr : "*"
index = index + 1 index = index + 1
@ -184,12 +183,12 @@ module Parfait
end end
def padded_length def padded_length
Object.padded( 4 * get_type().instance_length + char_length ) Object.padded( 4 * get_type().instance_length + @char_length )
end end
private private
def check_length def check_length
raise "Length out of bounds #{char_length}" if char_length > 1000 raise "Length out of bounds #{char_length}" if @char_length > 1000
end end
end end

View File

@ -1,31 +1,43 @@
require_relative "fake_memory" require_relative "fake_memory"
module Parfait
class Object
module Parfait
class Object ; end
class DataObject < Object
def self.allocate def self.allocate
r = super r = super
r.instance_variable_set(:@memory , Risc::FakeMemory.new(r ,self.type_length , self.memory_size)) r.instance_variable_set(:@memory , Risc::FakeMemory.new(r ,self.type_length , self.memory_size))
r r
end end
# 0 -based index # 0 -based index
def get_internal_word(index) def get_internal_word(index)
@memory[index] super(index) if index < self.class.type_length
@memory[ index ]
end end
# 0 -based index # 0 -based index
def set_internal_word(index , value) def set_internal_word(index , value)
@memory[index] = value return super(index,value) if index < self.class.type_length
value @memory[ index ] = value
end
end end
def self.attr( *attributes ) class Object
attributes = [attributes] unless attributes.is_a?(Array)
attributes.each do |name| # 0 -based index
define_getter(name) def get_internal_word(index)
define_setter(name) return @type if index == Parfait::TYPE_INDEX
name = Parfait.name_for_index(self , index)
return nil unless name
instance_eval("@#{name}")
end 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 end
def self.variable_index( name) def self.variable_index( name)
@ -39,22 +51,6 @@ module Parfait
i + 1 i + 1
end 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 ) def self.cattr( *names )
names.each do |ca| names.each do |ca|
class_eval "@@#{ca} = 0" class_eval "@@#{ca} = 0"

View File

@ -181,4 +181,13 @@ module Parfait
Word: {char_length: :Integer , next_word: :Word} , Word: {char_length: :Integer , next_word: :Word} ,
} }
end 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 end