2018-06-29 10:23:26 +02:00
|
|
|
require_relative "builtin/compile_helper"
|
|
|
|
require_relative "builtin/space"
|
|
|
|
require_relative "builtin/integer"
|
|
|
|
require_relative "builtin/object"
|
|
|
|
require_relative "builtin/word"
|
|
|
|
|
|
|
|
module Risc
|
|
|
|
module Builtin
|
|
|
|
# classes have booted, now create a minimal set of functions
|
|
|
|
# minimal means only that which can not be coded in ruby
|
|
|
|
# Methods are grabbed from respective modules by sending the method name.
|
|
|
|
# This should return the implementation of the method (ie a method object),
|
|
|
|
# not actually try to implement it(as that's impossible in ruby)
|
|
|
|
#
|
2018-06-30 22:16:17 +02:00
|
|
|
def self.boot_functions
|
|
|
|
compilers = []
|
2018-06-29 10:23:26 +02:00
|
|
|
space = Parfait.object_space
|
2018-06-30 22:16:17 +02:00
|
|
|
# very fiddly chicken 'n egg problem. Functions need to be in the right order,
|
|
|
|
# and in fact we have to define some dummies, just for the others to compile
|
|
|
|
# TODO go through the virtual parfait layer and adjust function names
|
|
|
|
# to what they really are
|
|
|
|
space_type = space.get_class.instance_type
|
|
|
|
compilers << compiler_for( space_type , Space , :main)
|
2018-06-29 10:23:26 +02:00
|
|
|
|
2018-06-30 22:16:17 +02:00
|
|
|
obj_type = space.get_class_by_name(:Object).instance_type
|
2018-06-29 10:23:26 +02:00
|
|
|
[ :get_internal_word , :set_internal_word , :_method_missing,
|
|
|
|
:exit , :__init__].each do |f|
|
2018-06-30 22:16:17 +02:00
|
|
|
compilers << compiler_for( obj_type , Object , f)
|
2018-06-29 10:23:26 +02:00
|
|
|
end
|
|
|
|
|
2018-06-30 22:16:17 +02:00
|
|
|
word_type = space.get_class_by_name(:Word).instance_type
|
2018-06-29 10:23:26 +02:00
|
|
|
[:putstring , :get_internal_byte , :set_internal_byte ].each do |f|
|
2018-06-30 22:16:17 +02:00
|
|
|
compilers << compiler_for( word_type , Word , f)
|
2018-06-29 10:23:26 +02:00
|
|
|
end
|
|
|
|
|
2018-06-30 22:16:17 +02:00
|
|
|
int_type = space.get_class_by_name(:Integer).instance_type
|
2018-06-29 10:23:26 +02:00
|
|
|
Risc.operators.each do |op|
|
2018-06-30 22:16:17 +02:00
|
|
|
compilers << operator_compiler( int_type , op)
|
2018-06-29 10:23:26 +02:00
|
|
|
end
|
|
|
|
[:putint, :div4, :div10 , :<,:<= , :>=, :>].each do |f| #div4 is just a forward declaration
|
2018-06-30 22:16:17 +02:00
|
|
|
compilers << compiler_for( int_type , Integer , f)
|
2018-06-29 10:23:26 +02:00
|
|
|
end
|
2018-06-30 22:16:17 +02:00
|
|
|
compilers
|
2018-06-29 10:23:26 +02:00
|
|
|
end
|
|
|
|
|
2018-06-30 22:16:17 +02:00
|
|
|
def self.compiler_for( type , mod , name)
|
|
|
|
compiler = mod.send(name , nil)
|
|
|
|
type.add_method( compiler.method )
|
|
|
|
compiler
|
|
|
|
end
|
|
|
|
def self.operator_compiler(int_type , op)
|
|
|
|
compiler = Integer.operator_method(op)
|
|
|
|
int_type.add_method(compiler.method)
|
|
|
|
compiler
|
|
|
|
end
|
2018-06-29 10:23:26 +02:00
|
|
|
end
|
|
|
|
end
|