reditribute boot dir evenly. some was parfait, some virtual, some kernel
This commit is contained in:
133
lib/virtual/boot_space.rb
Normal file
133
lib/virtual/boot_space.rb
Normal file
@ -0,0 +1,133 @@
|
||||
require "parfait/boot_class"
|
||||
require "kernel/all"
|
||||
|
||||
module Virtual
|
||||
# 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
|
||||
|
||||
# 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
|
||||
|
||||
# While data ususally would live in a .data section, we may also "inline" it into the code
|
||||
# in an oo system all data is represented as objects
|
||||
|
||||
class BootSpace
|
||||
|
||||
# Initialize with a string for cpu. Naming conventions are: for Machine XXX there exists a module XXX
|
||||
# with a XXXMachine in it that derives from Virtual::RegisterMachine
|
||||
def initialize machine = nil
|
||||
super()
|
||||
@classes = {}
|
||||
@main = Virtual::MethodDefinition.new("main" , [] )
|
||||
#global objects (data)
|
||||
@objects = []
|
||||
boot_classes
|
||||
@passes = [ Virtual::SendImplementation ]
|
||||
end
|
||||
attr_reader :main , :classes , :objects
|
||||
|
||||
def run_passes
|
||||
@passes.each do |pass|
|
||||
puts "Runnning pass #{pass}"
|
||||
all = main.blocks
|
||||
@classes.each_value do |c|
|
||||
c.instance_methods.each {|f| all += f.blocks }
|
||||
end
|
||||
all.each do |block|
|
||||
pass.new.run(block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Passes are initiated empty and added to by anyone who want (basically)
|
||||
# Even linking and assembly are passes and so there are quite a few system passes neccesary to result in a
|
||||
# working binary. Other than that, this is intentionally quite flexible
|
||||
|
||||
def add_pass_after( pass , after)
|
||||
index = @passes.index(after)
|
||||
raise "No such pass to add after: #{after}" unless index
|
||||
@passes.insert(index+1 , pass)
|
||||
end
|
||||
def add_pass_before( pass , after)
|
||||
index = @passes.index(after)
|
||||
raise "No such pass to add after: #{after}" unless index
|
||||
@passes.insert(index , pass)
|
||||
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
|
||||
# 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
|
||||
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
|
||||
#puts "Boot Object::#{f}"
|
||||
obj.add_instance_method Salama::Object.send(f , @context)
|
||||
end
|
||||
[:putstring,:putint,:fibo,:exit].each do |f|
|
||||
#puts "Boot Kernel::#{f}"
|
||||
obj.add_instance_method Salama::Kernel.send(f , @context)
|
||||
end
|
||||
obj = get_or_create_class :String
|
||||
[:get , :set , :puts].each do |f|
|
||||
#puts "Boot String::#{f}"
|
||||
obj.add_instance_method Salama::String.send(f , @context)
|
||||
end
|
||||
end
|
||||
|
||||
# Objects are data and get assembled after functions
|
||||
def add_object o
|
||||
return if @objects.include? o
|
||||
# raise "must be derived from Code #{o.inspect}" unless o.is_a? Virtual::Code
|
||||
@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
|
||||
c = BootClass.new(name)
|
||||
@classes[name] = c
|
||||
end
|
||||
c
|
||||
end
|
||||
|
||||
# linking entry , exit , main , classes , objects
|
||||
def link_at( start , context)
|
||||
super
|
||||
@entry.link_at( start , context )
|
||||
start += @entry.length
|
||||
@exit.link_at( start , context)
|
||||
start += @exit.length
|
||||
@main.link_at( start , context )
|
||||
start += @main.length
|
||||
@classes.values.each do |clazz|
|
||||
clazz.link_at(start , context)
|
||||
start += clazz.length
|
||||
end
|
||||
@objects.each do |o|
|
||||
o.link_at(start , context)
|
||||
start += o.length
|
||||
end
|
||||
end
|
||||
|
||||
# assemble in the same order as linked
|
||||
def assemble( io )
|
||||
link_at( @position , nil) #second link in case of forward declarations
|
||||
@entry.assemble( io )
|
||||
@exit.assemble( io )
|
||||
@main.assemble( io )
|
||||
@classes.values.each do |clazz|
|
||||
clazz.assemble(io)
|
||||
end
|
||||
@objects.each do |o|
|
||||
o.assemble(io)
|
||||
end
|
||||
io
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -83,5 +83,5 @@ require_relative "value"
|
||||
require_relative "type"
|
||||
require_relative "object"
|
||||
require_relative "constants"
|
||||
require "boot/boot_space"
|
||||
require_relative "boot_space"
|
||||
require_relative "send_implementation"
|
||||
|
54
lib/virtual/meta_class.rb
Normal file
54
lib/virtual/meta_class.rb
Normal file
@ -0,0 +1,54 @@
|
||||
module Virtual
|
||||
# class that acts like a class, but is really the object
|
||||
|
||||
# described in the ruby language book as the eigenclass, what you get with
|
||||
# class MyClass
|
||||
# class << self <--- this is called the eigenclass, or metaclass, and really is just
|
||||
# .... the class object but gives us the ability to use the
|
||||
# syntax as if it were a class
|
||||
# PS: can't say i fancy the << self syntax and am considerernig adding a
|
||||
# keyword for it, like meta
|
||||
# In effect it is a very similar construct to def self.function(...)
|
||||
# So one could write def meta.function(...) and thus define on the meta-class
|
||||
class MetaClass < Virtual::Object
|
||||
# no name, nor nothing. as this is just the object really
|
||||
|
||||
def initialize(object)
|
||||
super()
|
||||
@functions = []
|
||||
@me_self = object
|
||||
end
|
||||
|
||||
# in a non-booting version this should map to _add_singleton_method
|
||||
def add_function function
|
||||
raise "not a function #{function}" unless function.is_a? Virtual::Function
|
||||
raise "syserr " unless function.name.is_a? Symbol
|
||||
@functions << function
|
||||
end
|
||||
|
||||
def get_function name
|
||||
name = name.to_sym
|
||||
f = @functions.detect{ |f| f.name == name }
|
||||
return f if f
|
||||
if( @me_self == :Object )
|
||||
puts "no function for :#{name} in Meta #{@me_self.inspect}"
|
||||
return nil
|
||||
else #recurse up class hierachy unless we're at Object
|
||||
return @me_self.context.object_space.get_or_create_class(@me_self.super_class).get_function name
|
||||
end
|
||||
end
|
||||
|
||||
# get the function and if not found, try superclasses. raise error if not found
|
||||
def resolve_function name
|
||||
fun = get_function name
|
||||
# TODO THE BOOK says is class A derives from B , then the metaclass of A derives from the metaclass of B
|
||||
# just get to it ! (and stop whimpering)
|
||||
raise "Method not found #{name} , for #{inspect}" unless fun
|
||||
fun
|
||||
end
|
||||
|
||||
def to_s
|
||||
"#{inspect} on #{@me_self}, #{@functions.length} functions"
|
||||
end
|
||||
end
|
||||
end
|
@ -32,7 +32,7 @@ module Virtual
|
||||
if defined? @@space
|
||||
@@space
|
||||
else
|
||||
@@space = ::Boot::BootSpace.new
|
||||
@@space = BootSpace.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -9,8 +9,7 @@ module Virtual
|
||||
me = code.me
|
||||
next unless ( me.type == Reference)
|
||||
if( me.is_a? Constant)
|
||||
Boot::BootClass
|
||||
if( me.is_a? Boot::BootClass )
|
||||
if( me.is_a? BootClass )
|
||||
raise "unimplemented"
|
||||
elsif( me.is_a? ObjectConstant )
|
||||
clazz = me.clazz
|
||||
|
Reference in New Issue
Block a user