rubyx/lib/boot/boot_space.rb

87 lines
3.1 KiB
Ruby
Raw Normal View History

2014-06-13 22:51:53 +02:00
require "boot/boot_class"
#require "vm/call_site"
require "kernel/all"
require "boot/object"
require "boot/string"
2014-05-03 14:13:44 +02:00
2014-06-13 22:51:53 +02:00
module Boot
2014-07-16 18:24:41 +02:00
# The BootSpace contains all objects for a program. In functional terms it is a program, but in oo
# it is a collection of objects, some of which are data, some classes, some functions
2014-05-03 14:13:44 +02:00
# The main entry is a function called (of all things) "main", This _must be supplied by the compling
# There is a start and exit block that call main, which receives an array of strings
2014-07-16 18:24:41 +02:00
# While data ususally would live in a .data section, we may also "inline" it into the code
2014-05-03 14:13:44 +02:00
# in an oo system all data is represented as objects
class BootSpace
2014-05-03 14:13:44 +02:00
2014-05-03 17:51:47 +02:00
# Initialize with a string for cpu. Naming conventions are: for Machine XXX there exists a module XXX
2014-06-26 16:52:15 +02:00
# with a XXXMachine in it that derives from Virtual::RegisterMachine
def initialize machine = nil
super()
@classes = {}
2014-07-16 18:24:41 +02:00
@main = Virtual::MethodDefinition.new("main" , [] )
#global objects (data)
2014-05-03 14:13:44 +02:00
@objects = []
boot_classes
@passes = [ ]
2014-05-03 14:13:44 +02:00
end
attr_reader :context , :main , :classes , :entry , :exit
def run_passes
@passes.each do |pass|
all = main.blocks
@classes.each_value do |c|
c.functions.each {|f| all += f.blocks }
end
all.each do |block|
pass.run(block)
end
end
end
# boot the classes, ie create a minimal set of classes with a minimal set of functions
# minimal means only that which can not be coded in ruby
2014-07-16 18:24:41 +02:00
# MethodDefinitions 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)
def boot_classes
# 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 other to compile
obj = get_or_create_class :Object
2014-06-24 11:20:10 +02:00
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
#puts "Boot Object::#{f}"
2014-07-16 18:24:41 +02:00
obj.add_method_definition Boot::Object.send(f , @context)
end
[:putstring,:putint,:fibo,:exit].each do |f|
2014-06-24 11:20:10 +02:00
#puts "Boot Kernel::#{f}"
2014-07-17 01:26:13 +02:00
obj.add_method_definition Kide::Kernel.send(f , @context)
2014-06-13 22:41:45 +02:00
end
obj = get_or_create_class :String
[:get , :set].each do |f|
2014-06-24 11:20:10 +02:00
#puts "Boot String::#{f}"
2014-07-16 18:24:41 +02:00
obj.add_method_definition Boot::String.send(f , @context)
end
end
# Objects are data and get assembled after functions
2014-05-03 14:13:44 +02:00
def add_object o
2014-05-05 08:35:40 +02:00
return if @objects.include? o
# raise "must be derived from Code #{o.inspect}" unless o.is_a? Virtual::Code
2014-05-03 14:13:44 +02:00
@objects << o # TODO check type , no basic values allowed (must be wrapped)
end
# this is the way to instantiate classes (not BootClass.new)
# so we get and keep exactly one per name
def get_or_create_class name
raise "uups #{name}.#{name.class}" unless name.is_a? Symbol
c = @classes[name]
unless c
2014-07-14 13:06:09 +02:00
c = BootClass.new(name)
@classes[name] = c
end
c
end
2014-05-03 14:13:44 +02:00
end
end