use include so we can test with is_a

using the well documented included/extend trick
This commit is contained in:
Torsten Ruger 2015-10-25 19:16:12 +02:00
parent 3318b5026c
commit 60098257e9
6 changed files with 171 additions and 143 deletions

View File

@ -8,159 +8,167 @@
# #
# Using ruby include does not work for similar reasons, so Indexed.at is the main # Using ruby include does not work for similar reasons, so Indexed.at is the main
# function that generates the methods # function that generates the methods
# ( do have to use a marker module so we can test with is_a?)
module Indexed module Parfait
module Indexed # marker module
def self.at( offset , clazz ) def self.included(base)
base.extend(Methods)
clazz.send :define_method , :get_length do
internal_object_length - 1
end end
# index of item, remeber first item has index 1 module Methods
# return nil if no such item def offset( offset )
clazz.send :define_method , :index_of do |item|
max = self.get_length define_method :get_length do
counter = 1 internal_object_length - 1
while( counter <= max )
if( get(counter) == item)
return counter
end end
counter = counter + 1
end
return nil
end
# include? means non nil index # index of item, remeber first item has index 1
clazz.send :define_method , :include? do |item| # return nil if no such item
return index_of(item) != nil define_method :index_of do |item|
end max = self.get_length
counter = 1
while( counter <= max )
if( get(counter) == item)
return counter
end
counter = counter + 1
end
return nil
end
# push means add to the end # include? means non nil index
# this automatically grows the List define_method :include? do |item|
clazz.send :define_method , :push do |value| return index_of(item) != nil
set( self.get_length + 1 , value) end
end
clazz.send :define_method , :delete do |value| # push means add to the end
index = index_of value # this automatically grows the List
return false unless index define_method :push do |value|
delete_at index set( self.get_length + 1 , value)
end end
clazz.send :define_method , :delete_at do |index| define_method :delete do |value|
# TODO bounds check index = index_of value
while(index < self.get_length) return false unless index
set( index , get(index + 1)) delete_at index
index = index + 1 end
end
set_length( self.get_length - 1) define_method :delete_at do |index|
true # TODO bounds check
end while(index < self.get_length)
set( index , get(index + 1))
index = index + 1
end
set_length( self.get_length - 1)
true
end
clazz.send :define_method , :first do define_method :first do
return nil if empty? return nil if empty?
get(1) get(1)
end end
clazz.send :define_method , :last do define_method :last do
return nil if empty? return nil if empty?
get(get_length()) get(get_length())
end end
# set the value at index. # set the value at index.
# Lists start from index 1 # Lists start from index 1
clazz.send :define_method , :set do | index , value| define_method :set do | index , value|
raise "Only positive indexes #{index}" if index <= 0 raise "Only positive indexes #{index}" if index <= 0
if index > self.get_length if index > self.get_length
grow_to(index) grow_to(index)
end end
# internally 1 is reserved for the layout # internally 1 is reserved for the layout
internal_object_set( index + 1, value) internal_object_set( index + 1, value)
end end
# set the value at index.
# Lists start from index 1
define_method :get do | index|
raise "Only positive indexes, #{index}" if index <= 0
if index > self.get_length
return nil
else
return internal_object_get(index + 1)
end
end
define_method :empty? do
self.get_length == 0
end
define_method :set_length do | len|
was = self.get_length
return if was == len
if(was < len)
grow_to len
else
shrink_to len
end
end
define_method :grow_to do | len|
raise "Only positive lenths, #{len}" if len < 0
old_length = self.get_length
return if old_length >= len
internal_object_grow(len + 1)
end
define_method :shrink_to do | len|
raise "Only positive lenths, #{len}" if len < 0
old_length = self.get_length
return if old_length <= len
internal_object_shrink(len + 1)
end
define_method :equal? do | other|
# this should call parfait get_class, alas that is not implemented yet
return false if other.class != self.class
return false if other.get_length != self.get_length
index = self.get_length
while(index > 0)
return false if other.get(index) != self.get(index)
index = index - 1
end
return true
end
# above, correct, implementation causes problems in the machine object space
# because when a second empty (newly created) list is added, it is not actually
# added as it exists already. TODO, but hack with below identity function
define_method :== do | other|
self.object_id == other.object_id
end
# word length (padded) is the amount of space taken by the object
# For your basic object this means the number of instance variables as determined by layout
# This is off course 0 for a list, unless someone squeezed an instance variable in
# but additionally, the amount of data comes on top.
# unfortuntely we can't just use super because of the Padding
define_method :word_length do
padded_words( get_layout().get_length() + get_length() )
end
define_method :inspect do
inspect_from 1
end
define_method :inspect_from do |index|
ret = ""
while index <= self.get_length
item = get(index)
ret += item.inspect
ret += "," unless index == self.get_length
index = index + 1
end
ret
end
# set the value at index.
# Lists start from index 1
clazz.send :define_method , :get do | index|
raise "Only positive indexes, #{index}" if index <= 0
if index > self.get_length
return nil
else
return internal_object_get(index + 1)
end end
end end
clazz.send :define_method , :empty? do
self.get_length == 0
end
clazz.send :define_method , :set_length do | len|
was = self.get_length
return if was == len
if(was < len)
grow_to len
else
shrink_to len
end
end
clazz.send :define_method , :grow_to do | len|
raise "Only positive lenths, #{len}" if len < 0
old_length = self.get_length
return if old_length >= len
internal_object_grow(len + 1)
end
clazz.send :define_method , :shrink_to do | len|
raise "Only positive lenths, #{len}" if len < 0
old_length = self.get_length
return if old_length <= len
internal_object_shrink(len + 1)
end
clazz.send :define_method , :equal? do | other|
# this should call parfait get_class, alas that is not implemented yet
return false if other.class != self.class
return false if other.get_length != self.get_length
index = self.get_length
while(index > 0)
return false if other.get(index) != self.get(index)
index = index - 1
end
return true
end
# above, correct, implementation causes problems in the machine object space
# because when a second empty (newly created) list is added, it is not actually
# added as it exists already. TODO, but hack with below identity function
clazz.send :define_method , :== do | other|
self.object_id == other.object_id
end
# word length (padded) is the amount of space taken by the object
# For your basic object this means the number of instance variables as determined by layout
# This is off course 0 for a list, unless someone squeezed an instance variable in
# but additionally, the amount of data comes on top.
# unfortuntely we can't just use super because of the Padding
clazz.send :define_method , :word_length do
padded_words( get_layout().get_length() + get_length() )
end
clazz.send :define_method , :inspect do
inspect_from 1
end
clazz.send :define_method , :inspect_from do |index|
ret = ""
while index <= self.get_length
item = get(index)
ret += item.inspect
ret += "," unless index == self.get_length
index = index + 1
end
ret
end
end end
end end

View File

@ -21,7 +21,22 @@
# Together they turn the object into a hash like structure # Together they turn the object into a hash like structure
module Parfait module Parfait
class Layout < List class Layout < Object
include Indexed
self.offset(0)
def each
# not sure how to do this with define_method, because of the double block issue.
# probably some clever way around that, but not important
index = 1
while index <= self.get_length
item = get(index)
yield item
index = index + 1
end
self
end
attribute :object_class attribute :object_class
def initialize( object_class ) def initialize( object_class )

View File

@ -14,7 +14,8 @@ require_relative "indexed"
module Parfait module Parfait
class List < Object class List < Object
Indexed.at(0 , self) include Indexed
self.offset(0)
def initialize( ) def initialize( )
super() super()

View File

@ -168,7 +168,7 @@ module Register
end end
#puts "layout length=#{layout.get_length.to_s(16)} mem_len=#{layout.word_length.to_s(16)}" #puts "layout length=#{layout.get_length.to_s(16)} mem_len=#{layout.word_length.to_s(16)}"
l = layout.get_length l = layout.get_length
if( object.is_a? Parfait::List) if( object.is_a? Parfait::Indexed)
object.each do |inst| object.each do |inst|
write_ref_for(inst) write_ref_for(inst)
end end

View File

@ -27,7 +27,7 @@ module Register
inst = object.get_instance_variable name inst = object.get_instance_variable name
keep(inst , depth + 1) keep(inst , depth + 1)
end end
if object.is_a? Parfait::List if object.is_a? Parfait::Indexed
object.each do |item| object.each do |item|
#puts "Keep item " #puts "Keep item "
keep(item , depth + 1) keep(item , depth + 1)

View File

@ -6,6 +6,10 @@ class TestList < MiniTest::Test
Register.machine.boot unless Register.machine.booted Register.machine.boot unless Register.machine.booted
@list = ::Parfait::List.new @list = ::Parfait::List.new
end end
def test_isa
assert @list.is_a? Parfait::List
assert @list.is_a? Parfait::Indexed
end
def test_old_layout def test_old_layout
assert_equal Parfait::Layout , Register.machine.space.classes.keys.get_layout.class assert_equal Parfait::Layout , Register.machine.space.classes.keys.get_layout.class
end end