move assembly from visitor into objects, part one
This commit is contained in:
@ -39,6 +39,10 @@ module Virtual
|
||||
method
|
||||
end
|
||||
|
||||
@@CLAZZ = { :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}
|
||||
def layout
|
||||
@@CLAZZ
|
||||
end
|
||||
def to_s
|
||||
inspect
|
||||
end
|
||||
|
@ -1,5 +1,6 @@
|
||||
require_relative "boot_class"
|
||||
require "builtin/object"
|
||||
require "parfait/hash"
|
||||
|
||||
module Virtual
|
||||
# The BootSpace contains all objects for a program. In functional terms it is a program, but in oo
|
||||
@ -17,7 +18,7 @@ module Virtual
|
||||
# with a XXXMachine in it that derives from Virtual::RegisterMachine
|
||||
def initialize machine = nil
|
||||
super()
|
||||
@classes = {}
|
||||
@classes = Parfait::Hash.new
|
||||
@main = Virtual::CompiledMethod.new("main" , [] )
|
||||
#global objects (data)
|
||||
@objects = []
|
||||
@ -28,7 +29,7 @@ module Virtual
|
||||
def run_passes
|
||||
@passes.each do |pass|
|
||||
all = main.blocks
|
||||
@classes.each_value do |c|
|
||||
@classes.values.each do |c|
|
||||
c.instance_methods.each {|f| all += f.blocks }
|
||||
end
|
||||
all.each do |block|
|
||||
@ -90,6 +91,11 @@ module Virtual
|
||||
end
|
||||
end
|
||||
|
||||
@@SPACE = { :names => [:classes,:objects] , :types => [Virtual::Reference,Virtual::Reference]}
|
||||
def layout
|
||||
@@SPACE
|
||||
end
|
||||
|
||||
# Objects are data and get assembled after functions
|
||||
def add_object o
|
||||
return if @objects.include? o
|
||||
|
@ -149,6 +149,9 @@ module Virtual
|
||||
Ast::NameExpression.new(name)
|
||||
end
|
||||
|
||||
def layout
|
||||
Virtual::Object.layout
|
||||
end
|
||||
# sugar to create instructions easily.
|
||||
# any method will be passed on to the RegisterMachine and the result added to the insertion block
|
||||
# With this trick we can write what looks like assembler,
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user