2014-08-29 14:49:59 +02:00
|
|
|
require_relative "boot_class"
|
2015-05-04 10:12:31 +02:00
|
|
|
require "register/builtin/object"
|
2014-08-23 09:25:55 +02:00
|
|
|
|
2014-08-23 23:25:15 +02:00
|
|
|
module Virtual
|
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
|
2014-05-31 11:52:29 +02:00
|
|
|
# it is a collection of objects, some of which are data, some classes, some functions
|
2014-07-12 20:59:17 +02:00
|
|
|
|
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
|
2015-05-04 10:12:31 +02:00
|
|
|
|
2014-09-27 13:59:16 +02:00
|
|
|
class BootSpace < Virtual::ObjectConstant
|
2015-05-04 10:12:31 +02:00
|
|
|
|
2014-09-24 17:25:18 +02:00
|
|
|
def initialize
|
2014-05-31 11:38:15 +02:00
|
|
|
super()
|
2014-09-16 15:06:56 +02:00
|
|
|
@classes = Parfait::Hash.new
|
2014-05-05 10:03:43 +02:00
|
|
|
#global objects (data)
|
2014-05-03 14:13:44 +02:00
|
|
|
@objects = []
|
2014-09-19 18:11:30 +02:00
|
|
|
@symbols = []
|
2015-05-12 08:44:34 +02:00
|
|
|
@frames = 100.times.collect{ ::Parfait::Frame.new([],[])}
|
|
|
|
@messages = 100.times.collect{ ::Parfait::Message.new }
|
2014-09-24 17:25:18 +02:00
|
|
|
@next_message = @messages.first
|
2015-03-28 11:25:04 +01:00
|
|
|
@next_frame = @frames.first
|
2014-10-03 13:52:47 +02:00
|
|
|
@passes = [ "Virtual::SendImplementation" ]
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
2014-10-07 11:01:33 +02:00
|
|
|
attr_reader :init , :main , :classes , :objects , :symbols,:messages, :next_message , :next_frame
|
2014-06-11 10:41:50 +02:00
|
|
|
|
|
|
|
def run_passes
|
2014-10-03 13:52:47 +02:00
|
|
|
@passes.each do |pass_class|
|
2015-03-28 11:25:04 +01:00
|
|
|
blocks = [@init] + main.blocks
|
|
|
|
@classes.values.each do |c|
|
|
|
|
c.instance_methods.each {|f| blocks += f.blocks }
|
2014-06-11 10:41:50 +02:00
|
|
|
end
|
2014-10-05 00:13:57 +02:00
|
|
|
#puts "running #{pass_class}"
|
2014-06-11 10:41:50 +02:00
|
|
|
all.each do |block|
|
2014-10-03 13:52:47 +02:00
|
|
|
pass = eval pass_class
|
2015-05-04 10:12:31 +02:00
|
|
|
raise "no such pass-class as #{pass_class}" unless pass
|
2014-08-22 08:21:59 +02:00
|
|
|
pass.new.run(block)
|
2014-06-11 10:41:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-24 20:32:21 +02:00
|
|
|
def self.space
|
|
|
|
if defined? @@space
|
|
|
|
@@space
|
|
|
|
else
|
|
|
|
@@space = BootSpace.new
|
2014-09-09 17:03:13 +02:00
|
|
|
@@space
|
2014-08-24 20:32:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-28 11:25:04 +01:00
|
|
|
# Passes may be added to by anyone who wants
|
2014-10-05 00:13:57 +02:00
|
|
|
# This is intentionally quite flexible, though one sometimes has to watch the order of them
|
|
|
|
# most ordering is achieved by ordering the requires and using add_pass
|
|
|
|
# but more precise control is possible with the _after and _before versions
|
|
|
|
|
|
|
|
def add_pass pass
|
|
|
|
@passes << pass
|
|
|
|
end
|
2014-08-23 09:25:55 +02:00
|
|
|
def add_pass_after( pass , after)
|
|
|
|
index = @passes.index(after)
|
2014-10-03 13:52:47 +02:00
|
|
|
raise "No such pass (#{pass}) to add after: #{after}" unless index
|
2014-08-23 09:25:55 +02:00
|
|
|
@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
|
2014-06-11 20:36:22 +02:00
|
|
|
# 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
|
2015-05-04 10:12:31 +02:00
|
|
|
# CompiledMethods are grabbed from respective modules by sending the method name. This should return the
|
2014-07-16 18:24:41 +02:00
|
|
|
# implementation of the method (ie a method object), not actually try to implement it (as that's impossible in ruby)
|
2014-08-25 16:03:39 +02:00
|
|
|
def boot_classes!
|
2015-05-04 10:12:31 +02:00
|
|
|
# very fiddly chicken 'n egg problem. Functions need to be in the right order, and in fact we have to define some
|
2014-06-03 13:49:02 +02:00
|
|
|
# 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|
|
2015-03-28 11:25:04 +01:00
|
|
|
obj.add_instance_method Builtin::Object.send(f , nil)
|
2014-06-03 13:49:02 +02:00
|
|
|
end
|
2014-09-10 20:35:52 +02:00
|
|
|
obj = get_or_create_class :Kernel
|
2014-10-07 14:35:13 +02:00
|
|
|
# create main first, __init__ calls it
|
|
|
|
@main = Builtin::Kernel.send(:main , @context)
|
|
|
|
obj.add_instance_method @main
|
2015-03-28 11:25:04 +01:00
|
|
|
underscore_init = Builtin::Kernel.send(:__init__ ,nil) #store , so we don't have to resolve it below
|
2014-10-07 14:35:13 +02:00
|
|
|
obj.add_instance_method underscore_init
|
|
|
|
[:putstring,:exit,:__send].each do |f|
|
2015-03-28 11:25:04 +01:00
|
|
|
obj.add_instance_method Builtin::Kernel.send(f , nil)
|
2014-06-13 22:41:45 +02:00
|
|
|
end
|
2014-10-07 14:35:13 +02:00
|
|
|
# and the @init block in turn _jumps_ to __init__
|
|
|
|
# the point of which is that by the time main executes, all is "normal"
|
|
|
|
@init = Virtual::Block.new(:_init_ , nil )
|
|
|
|
@init.add_code(Register::RegisterMain.new(underscore_init))
|
2014-09-10 20:35:52 +02:00
|
|
|
obj = get_or_create_class :Integer
|
|
|
|
[:putint,:fibo].each do |f|
|
2015-03-28 11:25:04 +01:00
|
|
|
obj.add_instance_method Builtin::Integer.send(f , nil)
|
2014-09-10 20:35:52 +02:00
|
|
|
end
|
2014-06-05 17:17:00 +02:00
|
|
|
obj = get_or_create_class :String
|
2014-08-22 14:08:46 +02:00
|
|
|
[:get , :set , :puts].each do |f|
|
2015-03-28 11:25:04 +01:00
|
|
|
obj.add_instance_method Builtin::String.send(f , nil)
|
2014-06-05 17:17:00 +02:00
|
|
|
end
|
2014-08-28 15:39:35 +02:00
|
|
|
obj = get_or_create_class :Array
|
|
|
|
[:get , :set , :push].each do |f|
|
2015-03-28 11:25:04 +01:00
|
|
|
obj.add_instance_method Builtin::Array.send(f , nil)
|
2014-08-28 15:39:35 +02:00
|
|
|
end
|
2014-06-03 13:49:02 +02:00
|
|
|
end
|
2014-06-11 20:36:22 +02:00
|
|
|
|
2015-05-04 10:12:31 +02:00
|
|
|
@@SPACE = { :names => [:classes,:objects,:symbols,:messages, :next_message , :next_frame] ,
|
2014-09-19 18:11:30 +02:00
|
|
|
:types => [Virtual::Reference,Virtual::Reference,Virtual::Reference,Virtual::Reference,Virtual::Reference]}
|
2014-09-16 15:06:56 +02:00
|
|
|
def layout
|
|
|
|
@@SPACE
|
|
|
|
end
|
|
|
|
|
2014-06-11 20:36:22 +02:00
|
|
|
# Objects are data and get assembled after functions
|
2014-05-03 14:13:44 +02:00
|
|
|
def add_object o
|
2014-09-19 18:11:30 +02:00
|
|
|
return if @objects.include?(o)
|
|
|
|
@objects << o
|
|
|
|
if o.is_a? Symbol
|
|
|
|
@symbols << o
|
|
|
|
end
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
2014-06-11 20:36:22 +02:00
|
|
|
|
2014-07-12 20:59:17 +02:00
|
|
|
# this is the way to instantiate classes (not BootClass.new)
|
2014-06-11 20:36:22 +02:00
|
|
|
# so we get and keep exactly one per name
|
2014-05-31 11:38:15 +02:00
|
|
|
def get_or_create_class name
|
2014-06-03 13:49:02 +02:00
|
|
|
raise "uups #{name}.#{name.class}" unless name.is_a? Symbol
|
2014-05-31 13:35:33 +02:00
|
|
|
c = @classes[name]
|
|
|
|
unless c
|
2014-07-14 13:06:09 +02:00
|
|
|
c = BootClass.new(name)
|
2014-05-31 13:35:33 +02:00
|
|
|
@classes[name] = c
|
|
|
|
end
|
|
|
|
c
|
2014-05-31 11:38:15 +02:00
|
|
|
end
|
2014-09-16 16:16:56 +02:00
|
|
|
def mem_length
|
2014-09-19 18:11:30 +02:00
|
|
|
padded_words( 5 )
|
2014-09-16 16:16:56 +02:00
|
|
|
end
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
end
|