rubyx/lib/register/collector.rb

38 lines
1.1 KiB
Ruby
Raw Normal View History

2015-10-22 17:16:29 +02:00
module Register
2015-05-31 10:07:49 +02:00
# collect anything that is in the space but and reachable from init
module Collector
2016-12-30 17:39:49 +01:00
def collect_space
@objects.clear
keep Parfait.object_space , 0
2016-12-30 18:17:15 +01:00
constants.each {|obj| keep(obj,0)}
2015-05-31 10:07:49 +02:00
end
2015-07-30 18:18:41 +02:00
def keep object , depth
return if object.nil?
2015-07-30 18:18:41 +02:00
#puts "adding #{' ' * depth}:#{object.class}"
2016-12-30 18:17:15 +01:00
#puts "ADD #{object.inspect}, #{object.hash}" if object.is_a? Parfait::Type
return unless add_object( object )
# probably should make labels or even instructions derive from Parfait::Object, but . .
if object.is_a? Register::Label
object.each_label { |l| self.add_object(l)}
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|
#puts "Keep #{name} for #{object.class}"
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-08-06 17:27:25 +02:00
#puts "Keep item "
2015-07-30 18:18:41 +02:00
keep(item , depth + 1)
2015-05-31 10:07:49 +02:00
end
end
end
end
end