move assembly from visitor into objects, part one

This commit is contained in:
Torsten Ruger
2014-09-16 16:06:56 +03:00
parent 914d8af8c6
commit 45977ecc01
9 changed files with 121 additions and 148 deletions

View File

@ -1,3 +1,5 @@
require_relative "type"
module Virtual
# our machine is made up of objects, some of which are code, some data
#
@ -14,28 +16,64 @@ module Virtual
# (ruby)Array Array
# String String
class Object
# This could be in test, as it is used only there
def == other
return false unless other.class == self.class
Sof::Util.attributes(self).each do |a|
begin
left = send(a)
rescue NoMethodError
next # not using instance variables that are not defined as attr_readers for equality
end
begin
right = other.send(a)
rescue NoMethodError
return false
end
return false unless left.class == right.class
return false unless left == right
end
return true
def initialize
@position = -1
@length = -1
end
attr_accessor :position , :length , :layout
def position
raise "position accessed but not set at #{length} for #{self.objekt}" if @position == -1
@position
end
def inspect
Sof::Writer.write(self)
end
@@EMPTY = { :names => [] , :types => []}
def layout
raise "Find me #{self}"
self.class.layout
end
def self.layout
@@EMPTY
end
# class variables to have _identical_ objects passed back (stops recursion)
@@ARRAY = { :names => [] , :types => []}
# @@HASH = { :names => [:keys,:values] , :types => [Virtual::Reference,Virtual::Reference]}
# @@CLAZZ = { :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}
# @@SPACE = { :names => [:classes,:objects] , :types => [Virtual::Reference,Virtual::Reference]}
def layout_for(object)
case object
when Array , Symbol , String , Virtual::CompiledMethod , Virtual::Block , Virtual::StringConstant
@@ARRAY
when Hash
@@HASH.merge :keys => object.keys , :values => object.values
when Virtual::BootClass
@@CLAZZ
when Virtual::BootSpace
@@SPACE
else
raise "linker encounters unknown class #{object.class}"
end
end
end
end
Parfait::Hash.class_eval do
@@HASH = { :names => [:keys,:values] , :types => [Virtual::Reference,Virtual::Reference]}
def layout
@@HASH
end
end
Array.class_eval do
def layout
Virtual::Object.layout
end
end
Symbol.class_eval do
def layout
Virtual::Object.layout
end
end