a lot of work to get one more test to pass

This commit is contained in:
Torsten Ruger
2014-07-12 21:59:17 +03:00
parent 842c4e3044
commit dc6cb2bb52
16 changed files with 89 additions and 153 deletions

View File

@ -1,73 +1,51 @@
require_relative "meta_class"
module Boot
# class is mainly a list of functions with a name (for now)
# class is mainly a list of methods with a name (for now)
# layout of object is seperated into Layout
class BootClass < Virtual::ObjectConstant
def initialize name , context , super_class = :Object
def initialize name , scope , super_class = :Object
super()
@context = context
# class functions
@functions = []
@scope = scope
# class methods
@methods = []
@name = name.to_sym
@super_class = super_class
@meta_class = MetaClass.new(self)
end
attr_reader :name , :functions , :meta_class , :context , :super_class
attr_reader :name , :methods , :meta_class , :context , :super_class
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
def add_method method
raise "not a method #{method}" unless method.is_a? Virtual::Method
raise "syserr " unless method.name.is_a? Symbol
@methods << method
end
def get_function fname
def get_method fname
fname = fname.to_sym
f = @functions.detect{ |f| f.name == fname }
names = @functions.collect{|f| f.name }
f = @methods.detect{ |f| f.name == fname }
names = @methods.collect{|f| f.name }
f
end
# get the function and if not found, try superclasses. raise error if not found
def resolve_function name
fun = get_function name
# get the method and if not found, try superclasses. raise error if not found
def resolve_method name
fun = get_method name
unless fun or name == :Object
supr = @context.object_space.get_or_create_class(@super_class)
fun = supr.get_function name
puts "#{supr.functions.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of
fun = supr.get_method name
puts "#{supr.methods.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of
end
raise "Method not found :#{name}, for #{inspect}" unless fun
fun
end
def inspect
"BootClass #{@name} < #{@super_class}:#{@functions.collect(&:name)}"
"BootClass #{@name} < #{@super_class}:#{@methods.collect(&:name)}"
end
def to_s
inspect
end
# Code interface follows. Note position is inheitted as is from Code
# length of the class is the length of it's functions
def length
@functions.inject(0) {| sum , item | sum + item.length}
end
# linking functions
def link_at( start , context)
super
@functions.each do |function|
function.link_at(start , context)
start += function.length
end
end
# assemble functions
def assemble( io )
@functions.each do |function|
function.assemble(io)
end
io
end
end
end

View File

@ -1,7 +1,5 @@
require "vm/context"
require "boot/boot_class"
require "vm/call_site"
require "arm/arm_machine"
#require "vm/call_site"
require "kernel/all"
require "boot/object"
require "boot/string"
@ -9,37 +7,25 @@ require "boot/string"
module Boot
# The BootSpace is contains all objects for a program. In functional terms it is a program, but on 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
# throwing in a context for unspecified use (well one is to pass the programm/globals around)
class BootSpace < Virtual::Code
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()
machine = RbConfig::CONFIG["host_cpu"] unless machine
machine = "intel" if machine == "x86_64"
machine = machine.capitalize
Virtual::RegisterMachine.instance = eval("#{machine}::#{machine}Machine").new
@classes = {}
@context = Virtual::Context.new(self)
@context.current_class = get_or_create_class :Object
@main = Virtual::Function.new("main")
@context.function = @main
@main = Virtual::Method.new("main")
#global objects (data)
@objects = []
@entry = Virtual::RegisterMachine.instance.main_start @context
#main gets executed between entry and exit
@exit = Virtual::RegisterMachine.instance.main_exit @context
boot_classes
@passes = [ Virtual::MoveMoveReduction.new , Virtual::LogicMoveReduction.new, Virtual::NoopReduction.new, Virtual::SaveLocals.new ]
@passes = [ ]
end
attr_reader :context , :main , :classes , :entry , :exit
@ -65,27 +51,27 @@ module Boot
obj = get_or_create_class :Object
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
#puts "Boot Object::#{f}"
obj.add_function Boot::Object.send(f , @context)
obj.add_method Boot::Object.send(f , @context)
end
[:utoa, :putstring,:putint,:fibo,:exit].each do |f|
[:putstring,:putint,:fibo,:exit].each do |f|
#puts "Boot Kernel::#{f}"
obj.add_function Crystal::Kernel.send(f , @context)
obj.add_method Crystal::Kernel.send(f , @context)
end
obj = get_or_create_class :String
[:get , :set].each do |f|
#puts "Boot String::#{f}"
obj.add_function Boot::String.send(f , @context)
obj.add_method Boot::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
# 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 BootBlass.new)
# 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
@ -96,39 +82,5 @@ module Boot
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

View File

@ -6,12 +6,12 @@ module Boot
# set/get instance variable use it.
# This is just a placeholder, as we code this in ruby, but the instance methods need the definition before.
def index_of context , name = Virtual::Integer
index_function = Virtual::Function.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer )
index_function = Virtual::Method.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer )
return index_function
end
def self.layout
layout_function = Virtual::Function.new(:layout , Virtual::Reference , [ ] , Virtual::Reference )
layout_function = Virtual::Function.new(:layout , [ ] , Virtual::Reference , Virtual::Reference )
layout_function.at_index 2
layout_function
end
@ -23,7 +23,8 @@ module Boot
# end
# The at_index is just "below" the api, something we need but don't want to expose, so we can't code the above in ruby
def _get_instance_variable context , name = Virtual::Integer
get_function = Virtual::Function.new(:_get_instance_variable , Virtual::Reference , [ Virtual::Reference ] , Virtual::Mystery )
get_function = Virtual::Method.new(:_get_instance_variable , [ Virtual::Reference ] , Virtual::Reference ,Virtual::Mystery )
return get_function
me = get_function.receiver
var_name = get_function.args.first
return_to = get_function.return_type
@ -43,7 +44,9 @@ module Boot
end
def _set_instance_variable(context , name = Virtual::Integer , value = Virtual::Integer )
set_function = Virtual::Function.new(:_set_instance_variable , Virtual::Reference ,[Virtual::Reference ,Virtual::Reference], Virtual::Mystery )
set_function = Virtual::Method.new(:_set_instance_variable ,[Virtual::Reference ,Virtual::Reference], Virtual::Reference ,Virtual::Mystery )
return set_function
receiver set_function
me = set_function.receiver
var_name = set_function.args.first
return_to = set_function.return_type

View File

@ -2,11 +2,11 @@ module Boot
class String
module ClassMethods
def get context , index = Virtual::Integer
get_function = Virtual::Function.new(:get , Virtual::Integer , [ Virtual::Integer] , Virtual::Integer )
get_function = Virtual::Method.new(:get , [ Virtual::Integer] , Virtual::Integer , Virtual::Integer )
return get_function
end
def set context , index = Virtual::Integer , char = Virtual::Integer
set_function = Virtual::Function.new(:set , Virtual::Integer ,[Virtual::Integer, Virtual::Integer] , Virtual::Integer )
set_function = Virtual::Method.new(:set , [Virtual::Integer, Virtual::Integer] , Virtual::Integer ,Virtual::Integer )
return set_function
end
end