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,18 +8,24 @@
# #
# 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.included(base)
base.extend(Methods)
end
def self.at( offset , clazz ) module Methods
def offset( offset )
clazz.send :define_method , :get_length do define_method :get_length do
internal_object_length - 1 internal_object_length - 1
end end
# index of item, remeber first item has index 1 # index of item, remeber first item has index 1
# return nil if no such item # return nil if no such item
clazz.send :define_method , :index_of do |item| define_method :index_of do |item|
max = self.get_length max = self.get_length
counter = 1 counter = 1
while( counter <= max ) while( counter <= max )
@ -32,23 +38,23 @@ module Indexed
end end
# include? means non nil index # include? means non nil index
clazz.send :define_method , :include? do |item| define_method :include? do |item|
return index_of(item) != nil return index_of(item) != nil
end end
# push means add to the end # push means add to the end
# this automatically grows the List # this automatically grows the List
clazz.send :define_method , :push do |value| define_method :push do |value|
set( self.get_length + 1 , value) set( self.get_length + 1 , value)
end end
clazz.send :define_method , :delete do |value| define_method :delete do |value|
index = index_of value index = index_of value
return false unless index return false unless index
delete_at index delete_at index
end end
clazz.send :define_method , :delete_at do |index| define_method :delete_at do |index|
# TODO bounds check # TODO bounds check
while(index < self.get_length) while(index < self.get_length)
set( index , get(index + 1)) set( index , get(index + 1))
@ -59,19 +65,19 @@ module Indexed
end 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)
@ -82,7 +88,7 @@ module Indexed
# set the value at index. # set the value at index.
# Lists start from index 1 # Lists start from index 1
clazz.send :define_method , :get do | index| define_method :get do | index|
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
return nil return nil
@ -91,11 +97,11 @@ module Indexed
end end
end end
clazz.send :define_method , :empty? do define_method :empty? do
self.get_length == 0 self.get_length == 0
end end
clazz.send :define_method , :set_length do | len| define_method :set_length do | len|
was = self.get_length was = self.get_length
return if was == len return if was == len
if(was < len) if(was < len)
@ -105,21 +111,21 @@ module Indexed
end end
end end
clazz.send :define_method , :grow_to do | len| define_method :grow_to do | len|
raise "Only positive lenths, #{len}" if len < 0 raise "Only positive lenths, #{len}" if len < 0
old_length = self.get_length old_length = self.get_length
return if old_length >= len return if old_length >= len
internal_object_grow(len + 1) internal_object_grow(len + 1)
end end
clazz.send :define_method , :shrink_to do | len| define_method :shrink_to do | len|
raise "Only positive lenths, #{len}" if len < 0 raise "Only positive lenths, #{len}" if len < 0
old_length = self.get_length old_length = self.get_length
return if old_length <= len return if old_length <= len
internal_object_shrink(len + 1) internal_object_shrink(len + 1)
end end
clazz.send :define_method , :equal? do | other| define_method :equal? do | other|
# this should call parfait get_class, alas that is not implemented yet # this should call parfait get_class, alas that is not implemented yet
return false if other.class != self.class return false if other.class != self.class
return false if other.get_length != self.get_length return false if other.get_length != self.get_length
@ -134,7 +140,7 @@ module Indexed
# above, correct, implementation causes problems in the machine object space # above, correct, implementation causes problems in the machine object space
# because when a second empty (newly created) list is added, it is not actually # 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 # added as it exists already. TODO, but hack with below identity function
clazz.send :define_method , :== do | other| define_method :== do | other|
self.object_id == other.object_id self.object_id == other.object_id
end end
@ -143,15 +149,15 @@ module Indexed
# This is off course 0 for a list, unless someone squeezed an instance variable in # This is off course 0 for a list, unless someone squeezed an instance variable in
# but additionally, the amount of data comes on top. # but additionally, the amount of data comes on top.
# unfortuntely we can't just use super because of the Padding # unfortuntely we can't just use super because of the Padding
clazz.send :define_method , :word_length do define_method :word_length do
padded_words( get_layout().get_length() + get_length() ) padded_words( get_layout().get_length() + get_length() )
end end
clazz.send :define_method , :inspect do define_method :inspect do
inspect_from 1 inspect_from 1
end end
clazz.send :define_method , :inspect_from do |index| define_method :inspect_from do |index|
ret = "" ret = ""
while index <= self.get_length while index <= self.get_length
item = get(index) item = get(index)
@ -164,3 +170,5 @@ module Indexed
end 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