rubyx/lib/mom/mom_collection.rb
Torsten Rüger dcbc3e17be refactor risc_collection
move code that operates on compiler to the compiler class
this leaves the collection thin, very thin indeed
2019-09-28 15:37:02 +03:00

83 lines
2.5 KiB
Ruby

module Mom
# The Compiler/Collection for the Mom level is a collection of Mom level Method
# compilers These will transform to Risc MethodCompilers on the way down.
#
# As RubyCompiler pools source at the vool level, when several classes are compiled
# from vool to mom, several MomCompilers get instantiated. They must be merged before
# proceeding with translate. Thus we have a append method.
#
class MomCollection
attr_reader :method_compilers
# Initialize with an array of risc MethodCompilers
def initialize(compilers = [])
@method_compilers = nil
compilers.each{|c| add_compiler(c)}
end
# lazily instantiate the compiler for __init__ function and __method_missing__
def init_compilers
return if @init_compilers
@init_compilers = true
add_compiler MomCollection.create_init_compiler
add_compiler MomCollection.create_mm_compiler
self
end
# Return all compilers, namely the MethodCompilers instanc,
# plus the init_compilers
def compilers
init_compilers
@method_compilers
end
def add_compiler(compiler)
if(@method_compilers)
@method_compilers.add_method_compiler(compiler)
else
@method_compilers = compiler
end
self
end
# Append another MomCompilers method_compilers to this one.
def append(collection)
@method_compilers.add_method_compiler( collection.method_compilers)
self
end
def to_risc( )
init_compilers
riscs =[]
@method_compilers.each_compiler do | mom_c |
riscs << mom_c.to_risc
end
# to_risc all compilers
# for each suffling constnts and fist label, then all instructions (see below)
# then create risc collection
Risc::RiscCollection.new(riscs)
end
# See Init instruction. We must have an init (ie we need it in code), so it is created in code
def self.create_init_compiler
compiler = compiler_for(:Object,:__init__ ,{})
compiler._reset_for_init # no return, just for init
compiler.add_code Init.new("missing")
return compiler
end
def self.create_mm_compiler
compiler = compiler_for(:Object,:__method_missing__ ,{})
compiler.add_code MethodMissing.new("missing" , :r1)
return compiler
end
def self.compiler_for( clazz_name , method_name , arguments , locals = {})
frame = Parfait::Type.for_hash( locals )
args = Parfait::Type.for_hash( arguments )
MethodCompiler.compiler_for_class(clazz_name , method_name , args, frame )
end
end
end