rubyx/lib/risc/collector.rb

59 lines
1.8 KiB
Ruby
Raw Normal View History

module Risc
2015-05-31 10:07:49 +02:00
# collect anything that is in the space but and reachable from init
module Collector
def self.collect_space
@objects = {}
keep Parfait.object_space , 0
Risc.machine.constants.each {|obj| keep(obj,0)}
@objects
2015-05-31 10:07:49 +02:00
end
def self.keep( object , depth )
return if object.nil?
return unless add_object( object , depth )
# probably should make labels or even instructions derive from Parfait::Object, but . .
if object.is_a? Risc::Label
object.each_label { |l| self.add_object(l ,depth)}
end
return unless object.respond_to? :has_type?
type = object.get_type
keep(type , depth + 1)
return if object.is_a? Symbol
type.names.each do |name|
keep(name , depth + 1)
inst = object.get_instance_variable name
2015-07-30 18:18:41 +02:00
keep(inst , depth + 1)
2015-05-31 10:07:49 +02:00
end
if object.is_a? Parfait::List
2015-05-31 10:07:49 +02:00
object.each do |item|
2015-07-30 18:18:41 +02:00
keep(item , depth + 1)
2015-05-31 10:07:49 +02:00
end
end
end
# Objects are data and get assembled after functions
def self.add_object( objekt , depth)
return false if @objects[objekt.object_id]
return true if objekt.is_a? Fixnum
#puts message(objekt , depth)
#puts "ADD #{objekt.inspect}, #{objekt.name}" if objekt.is_a? Parfait::TypedMethod
unless objekt.is_a?( Parfait::Object) or objekt.is_a?( Symbol) or objekt.is_a?( Risc::Label)
raise "adding non parfait #{objekt.class}"
end
#raise "Method #{objekt.name}" if objekt.is_a? Parfait::TypedMethod
@objects[objekt.object_id] = objekt
true
end
def self.message(object , depth)
msg = "adding #{depth}#{' ' * depth}:"
if( object.respond_to?(:sof_reference_name))
msg + object.sof_reference_name.to_s
else
msg + object.class.name
end
end
2015-05-31 10:07:49 +02:00
end
end