2019-10-03 19:55:41 +02:00
|
|
|
module SlotMachine
|
|
|
|
# The Compiler/Collection for the SlotMachine level is a collection of SlotMachine level Method
|
2019-08-06 17:33:27 +02:00
|
|
|
# compilers These will transform to Risc MethodCompilers on the way down.
|
|
|
|
#
|
|
|
|
# As RubyCompiler pools source at the vool level, when several classes are compiled
|
2019-10-03 19:55:41 +02:00
|
|
|
# from vool to mom, several SlotMachineCompilers get instantiated. They must be merged before
|
2019-08-06 17:33:27 +02:00
|
|
|
# proceeding with translate. Thus we have a append method.
|
|
|
|
#
|
2019-10-03 19:55:41 +02:00
|
|
|
class SlotCollection
|
2019-08-06 17:33:27 +02:00
|
|
|
attr_reader :method_compilers
|
|
|
|
|
|
|
|
# Initialize with an array of risc MethodCompilers
|
|
|
|
def initialize(compilers = [])
|
2019-09-28 11:41:38 +02:00
|
|
|
@method_compilers = nil
|
|
|
|
compilers.each{|c| add_compiler(c)}
|
2019-08-06 17:33:27 +02:00
|
|
|
end
|
|
|
|
|
2019-09-15 14:13:11 +02:00
|
|
|
# lazily instantiate the compiler for __init__ function and __method_missing__
|
2019-09-28 11:41:38 +02:00
|
|
|
def init_compilers
|
|
|
|
return if @init_compilers
|
|
|
|
@init_compilers = true
|
2019-10-03 19:55:41 +02:00
|
|
|
add_compiler SlotCollection.create_init_compiler
|
|
|
|
add_compiler SlotCollection.create_mm_compiler
|
2019-09-28 11:41:38 +02:00
|
|
|
self
|
2019-08-06 17:33:27 +02:00
|
|
|
end
|
|
|
|
|
2019-09-28 14:34:09 +02:00
|
|
|
# Return all compilers, namely the MethodCompilers instanc,
|
|
|
|
# plus the init_compilers
|
2019-08-06 17:33:27 +02:00
|
|
|
def compilers
|
2019-09-28 11:41:38 +02:00
|
|
|
init_compilers
|
|
|
|
@method_compilers
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_compiler(compiler)
|
|
|
|
if(@method_compilers)
|
|
|
|
@method_compilers.add_method_compiler(compiler)
|
|
|
|
else
|
|
|
|
@method_compilers = compiler
|
|
|
|
end
|
|
|
|
self
|
2019-08-06 17:33:27 +02:00
|
|
|
end
|
|
|
|
|
2019-10-03 19:55:41 +02:00
|
|
|
# Append another SlotMachineCompilers method_compilers to this one.
|
2019-09-28 11:41:38 +02:00
|
|
|
def append(collection)
|
|
|
|
@method_compilers.add_method_compiler( collection.method_compilers)
|
2019-08-06 17:33:27 +02:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2019-08-12 15:12:17 +02:00
|
|
|
def to_risc( )
|
2019-09-28 11:41:38 +02:00
|
|
|
init_compilers
|
|
|
|
riscs =[]
|
|
|
|
@method_compilers.each_compiler do | mom_c |
|
|
|
|
riscs << mom_c.to_risc
|
2019-08-10 20:30:00 +02:00
|
|
|
end
|
2019-08-08 11:19:27 +02:00
|
|
|
# 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
|
|
|
|
|
2019-09-15 14:13:11 +02:00
|
|
|
# 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__ ,{})
|
2019-09-17 19:18:00 +02:00
|
|
|
compiler.add_code MethodMissing.new("missing" , :r1)
|
2019-09-15 14:13:11 +02:00
|
|
|
return compiler
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.compiler_for( clazz_name , method_name , arguments , locals = {})
|
2019-09-18 21:07:58 +02:00
|
|
|
frame = Parfait::Type.for_hash( locals )
|
|
|
|
args = Parfait::Type.for_hash( arguments )
|
2019-09-15 14:13:11 +02:00
|
|
|
MethodCompiler.compiler_for_class(clazz_name , method_name , args, frame )
|
|
|
|
end
|
2019-09-12 12:09:30 +02:00
|
|
|
|
2019-08-06 17:33:27 +02:00
|
|
|
end
|
|
|
|
end
|