2014-05-31 11:52:29 +02:00
|
|
|
module Vm
|
2014-05-31 13:35:33 +02:00
|
|
|
# class is mainly a list of functions with a name (for now)
|
|
|
|
# layout of object is seperated into Layout
|
|
|
|
class BootClass < Code
|
2014-06-01 20:03:08 +02:00
|
|
|
def initialize name , context , superclass = :Object
|
2014-05-31 13:35:33 +02:00
|
|
|
@context = context
|
|
|
|
# class functions
|
|
|
|
@functions = []
|
|
|
|
@name = name.to_sym
|
2014-06-01 20:03:08 +02:00
|
|
|
@superclass = superclass
|
2014-05-31 13:35:33 +02:00
|
|
|
end
|
|
|
|
attr_reader :name , :functions
|
|
|
|
|
|
|
|
def add_function function
|
|
|
|
raise "not a function #{function}" unless function.is_a? Function
|
|
|
|
raise "syserr " unless function.name.is_a? Symbol
|
|
|
|
@functions << function
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_function name
|
|
|
|
name = name.to_sym
|
|
|
|
@functions.detect{ |f| f.name == name }
|
|
|
|
end
|
|
|
|
|
|
|
|
# preferred way of creating new functions (also forward declarations, will flag unresolved later)
|
|
|
|
def get_or_create_function name
|
|
|
|
fun = get_function name
|
2014-06-01 20:03:08 +02:00
|
|
|
unless fun or name == :Object
|
|
|
|
supr = @context.object_space.get_or_create_class(@superclass)
|
|
|
|
fun = supr.get_function name
|
|
|
|
puts "#{supr.functions.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of
|
|
|
|
end
|
2014-05-31 13:35:33 +02:00
|
|
|
unless fun
|
|
|
|
fun = Core::Kernel.send(name , @context)
|
2014-05-31 16:02:55 +02:00
|
|
|
raise "no such function #{name}, #{name.class}" if fun == nil
|
2014-05-31 13:35:33 +02:00
|
|
|
@functions << fun
|
|
|
|
end
|
|
|
|
fun
|
|
|
|
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
|
2014-05-31 11:52:29 +02:00
|
|
|
end
|
|
|
|
end
|