Merge branch 'master' of https://github.com/ruby-in-ruby/crystal
This commit is contained in:
@ -63,6 +63,7 @@ module Arm
|
||||
# return the block of the given name
|
||||
# or raise an exception, as this is meant to be called when the block is available
|
||||
def get_block name
|
||||
name = name.to_sym
|
||||
block = @blocks.find {|b| b.name == name}
|
||||
raise "No block found for #{name} (in #{blocks.collect{|b|b.name}.join(':')})" unless block
|
||||
block
|
||||
|
@ -11,7 +11,7 @@ module Arm
|
||||
@attributes[:update_status] = 0 if @attributes[:update_status] == nil
|
||||
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
|
||||
@operand = 0
|
||||
|
||||
raise "alert" if right.is_a? Vm::Block
|
||||
@pre_post_index = 0 #P flag
|
||||
@add_offset = 0 #U flag
|
||||
@is_load = opcode.to_s[0] == "l" ? 1 : 0 #L (load) flag
|
||||
@ -31,7 +31,10 @@ module Arm
|
||||
@rn = arg
|
||||
if @right
|
||||
@operand = @right
|
||||
#TODO better test, this operand integer (register) does not work. but sleep first
|
||||
@operand = @operand.register if @operand.is_a? Vm::Integer
|
||||
unless( @operand.is_a? Symbol)
|
||||
puts "operand #{@operand.inspect}"
|
||||
if (@operand < 0)
|
||||
@add_offset = 0
|
||||
#TODO test/check/understand
|
||||
|
@ -24,7 +24,7 @@ module Ast
|
||||
class NameExpression < Expression
|
||||
attr_reader :name
|
||||
def initialize name
|
||||
@name = name
|
||||
@name = name.to_sym
|
||||
end
|
||||
# compiling a variable resolves it.
|
||||
# if it wasn't defined, nli is returned
|
||||
@ -32,10 +32,10 @@ module Ast
|
||||
context.locals[name]
|
||||
end
|
||||
def inspect
|
||||
self.class.name + '.new("' + name + '")'
|
||||
"#{self.class.name}.new(#{name})"
|
||||
end
|
||||
def to_s
|
||||
name
|
||||
name.to_s
|
||||
end
|
||||
def attributes
|
||||
[:name]
|
||||
@ -54,7 +54,7 @@ module Ast
|
||||
self.class.name + '.new("' + string + '")'
|
||||
end
|
||||
def to_s
|
||||
'"' + string + '"'
|
||||
'"' + string.to_s + '"'
|
||||
end
|
||||
def compile context , into
|
||||
value = Vm::StringConstant.new(string)
|
||||
|
@ -4,15 +4,30 @@ module Ast
|
||||
class CallSiteExpression < Expression
|
||||
attr_reader :name, :args , :receiver
|
||||
|
||||
def initialize name, args , receiver = Ast::NameExpression.new("self")
|
||||
@name , @args , @receiver = name.to_sym , args , receiver
|
||||
def initialize name, args , receiver = Ast::NameExpression.new(:self)
|
||||
@name = name.to_sym
|
||||
@args = args
|
||||
@receiver = receiver
|
||||
end
|
||||
|
||||
def compile context , into
|
||||
params = args.collect{ |a| a.compile(context, into) }
|
||||
#TOOD, this needs dynamic resolution
|
||||
function = context.current_class.get_or_create_function(name)
|
||||
raise "Forward declaration not implemented (#{name}) #{inspect}" if function == nil
|
||||
|
||||
if receiver.name == :self
|
||||
function = context.current_class.get_or_create_function(name)
|
||||
elsif receiver.is_a? ModuleName
|
||||
c_name = receiver.name
|
||||
clazz = context.object_space.get_or_create_class c_name
|
||||
raise "uups #{clazz}.#{c_name}" unless clazz
|
||||
#class qualifier, means call from metaclass
|
||||
clazz = clazz.meta_class
|
||||
function = clazz.get_or_create_function(name)
|
||||
elsif receiver.is_a? VariableExpression
|
||||
raise "not implemented instance var:#{receiver}"
|
||||
else
|
||||
raise "not implemented case (dare i say system error):#{receiver}"
|
||||
end
|
||||
raise "No such method error #{clazz.to_s}:#{name}" if function == nil
|
||||
call = Vm::CallSite.new( name , params , function)
|
||||
current_function = context.function
|
||||
current_function.save_locals(context , into) if current_function
|
||||
|
@ -31,10 +31,16 @@ module Ast
|
||||
locals[arg] = arg_value
|
||||
args << arg_value
|
||||
end
|
||||
class_name = context.current_class.name
|
||||
function = Vm::Function.new(name , args )
|
||||
# class depends on receiver
|
||||
context.current_class.add_function function
|
||||
if receiver.nil?
|
||||
clazz = context.current_class
|
||||
else
|
||||
c = context.object_space.get_or_create_class receiver.name.to_sym
|
||||
clazz = c.meta_class
|
||||
end
|
||||
|
||||
function = Vm::Function.new(name , args )
|
||||
clazz.add_function function
|
||||
|
||||
parent_locals = context.locals
|
||||
parent_function = context.function
|
||||
|
@ -1,6 +1,8 @@
|
||||
module Ast
|
||||
class ModuleExpression < Expression
|
||||
|
||||
attr_reader :name ,:expressions
|
||||
|
||||
def initialize name , expressions
|
||||
@name = name.to_sym
|
||||
@expressions = expressions
|
||||
|
51
lib/boot/object.rb
Normal file
51
lib/boot/object.rb
Normal file
@ -0,0 +1,51 @@
|
||||
module Boot
|
||||
class Object
|
||||
module ClassMethods
|
||||
|
||||
# return the index of the variable. Now "normal" code can't really do anything with that, but
|
||||
# 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 = Vm::Integer
|
||||
index_function = Vm::Function.new(:index_of , [Vm::Integer] , Vm::Integer )
|
||||
return index_function
|
||||
end
|
||||
|
||||
|
||||
# in ruby, how this goes is
|
||||
# def _get_instance_variable var
|
||||
# i = self.index_of(var)
|
||||
# return at_index(i)
|
||||
# end
|
||||
# The at_index is just "below" the api, somehting we need but don't want to expose, so we can't code the above in ruby
|
||||
def _get_instance_variable context , name = Vm::Integer
|
||||
get_function = Vm::Function.new(:_get_instance_variable , [Vm::Integer , Vm::Integer ] , Vm::Integer )
|
||||
me = get_function.args[0]
|
||||
var_name = get_function.args[1]
|
||||
return_to = get_function.return_type
|
||||
index_function = context.object_space.get_or_create_class(:Object).get_or_create_function(:index_of)
|
||||
b = get_function.body
|
||||
b.push( [me] )
|
||||
b.call( index_function )
|
||||
b.pop([me])
|
||||
return_to.at_index( get_function.body , me , return_to )
|
||||
get_function.set_return return_to
|
||||
return get_function
|
||||
end
|
||||
|
||||
def _set_instance_variable(name , value)
|
||||
raise name
|
||||
end
|
||||
|
||||
def _get_singleton_method(name )
|
||||
raise name
|
||||
end
|
||||
def _add_singleton_method(method)
|
||||
raise "4"
|
||||
end
|
||||
def initialize()
|
||||
raise "4"
|
||||
end
|
||||
end
|
||||
extend ClassMethods
|
||||
end
|
||||
end
|
@ -1,26 +1,6 @@
|
||||
# this is not a "normal" ruby file, ie it is not required by crystal
|
||||
# instead it is parsed by crystal to define part of the crystal that runs
|
||||
|
||||
class BaseObject
|
||||
|
||||
def _set_instance_variable(name , value)
|
||||
|
||||
end
|
||||
|
||||
def _get_instance_variable( name )
|
||||
|
||||
end
|
||||
|
||||
def _get_singleton_method(name )
|
||||
|
||||
end
|
||||
def _add_singleton_method(method)
|
||||
|
||||
end
|
||||
def initialize
|
||||
end
|
||||
end
|
||||
|
||||
class Array < BaseObject
|
||||
def initialize size
|
||||
|
||||
|
@ -1,23 +0,0 @@
|
||||
module Core
|
||||
class BaseObject
|
||||
|
||||
def _set_instance_variable(name , value)
|
||||
return name
|
||||
end
|
||||
|
||||
def _get_instance_variable( name )
|
||||
return name
|
||||
end
|
||||
|
||||
def _get_singleton_method(name )
|
||||
return name
|
||||
end
|
||||
def _add_singleton_method(method)
|
||||
return 4
|
||||
end
|
||||
def initialize()
|
||||
return 4
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -20,27 +20,6 @@ module Core
|
||||
def function_exit block , f_name
|
||||
Vm::RegisterMachine.instance.function_exit block , f_name
|
||||
end
|
||||
|
||||
# in ruby, how this goes is
|
||||
# def _get_instance_variable var
|
||||
# i = self.index_of(var)
|
||||
# return at_index(i)
|
||||
# end
|
||||
# The at_index is just "below" the api, somehting we need but don't want to expose, so we can't code the above in ruby
|
||||
def _get_instance_variable context , name = Vm::Integer
|
||||
get_function = Vm::Function.new(:_get_instance_variable , [Vm::Integer , Vm::Integer ] , Vm::Integer )
|
||||
me = get_function.args[0]
|
||||
var_name = get_function.args[1]
|
||||
return_to = get_function.return_type
|
||||
index_function = context.object_space.get_or_create_class(:Object).get_or_create_function(:index_of)
|
||||
b = get_function.body
|
||||
b.push( me )
|
||||
index = b.call( index_function )
|
||||
b.pop(me)
|
||||
return_to.at_index( get_function.body , me , index )
|
||||
get_function.set_return return_to
|
||||
return get_function
|
||||
end
|
||||
|
||||
#TODO this is in the wrong place. It is a function that returns a function object
|
||||
# while all other methods add their code into some block. --> kernel
|
||||
|
@ -1,15 +1,19 @@
|
||||
require_relative "meta_class"
|
||||
|
||||
module Vm
|
||||
# class is mainly a list of functions with a name (for now)
|
||||
# layout of object is seperated into Layout
|
||||
class BootClass < Code
|
||||
def initialize name , context , superclass = :Object
|
||||
def initialize name , context , super_class = :Object
|
||||
super()
|
||||
@context = context
|
||||
# class functions
|
||||
@functions = []
|
||||
@name = name.to_sym
|
||||
@superclass = superclass
|
||||
@super_class = super_class
|
||||
@meta_class = MetaClass.new(self)
|
||||
end
|
||||
attr_reader :name , :functions
|
||||
attr_reader :name , :functions , :meta_class
|
||||
|
||||
def add_function function
|
||||
raise "not a function #{function}" unless function.is_a? Function
|
||||
@ -17,27 +21,32 @@ module Vm
|
||||
@functions << function
|
||||
end
|
||||
|
||||
def get_function name
|
||||
name = name.to_sym
|
||||
@functions.detect{ |f| f.name == name }
|
||||
def get_function fname
|
||||
fname = fname.to_sym
|
||||
f = @functions.detect{ |f| f.name == fname }
|
||||
names = @functions.collect{|f| f.name }
|
||||
f
|
||||
end
|
||||
|
||||
# preferred way of creating new functions (also forward declarations, will flag unresolved later)
|
||||
# way of creating new functions that have not been parsed.
|
||||
def get_or_create_function name
|
||||
fun = get_function name
|
||||
unless fun or name == :Object
|
||||
supr = @context.object_space.get_or_create_class(@superclass)
|
||||
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
|
||||
end
|
||||
unless fun
|
||||
fun = Core::Kernel.send(name , @context)
|
||||
raise "no such function #{name}, #{name.class}" if fun == nil
|
||||
return nil if fun == nil
|
||||
@functions << fun
|
||||
end
|
||||
fun
|
||||
end
|
||||
|
||||
|
||||
def inspect
|
||||
"BootClass #{@name} , super #{@super_class} #{@functions.length} functions"
|
||||
end
|
||||
# Code interface follows. Note position is inheitted as is from Code
|
||||
|
||||
# length of the class is the length of it's functions
|
||||
|
@ -3,6 +3,7 @@ require_relative "boot_class"
|
||||
require_relative "call_site"
|
||||
require "arm/arm_machine"
|
||||
require "core/kernel"
|
||||
require "boot/object"
|
||||
|
||||
module Vm
|
||||
# The BootSpace is contains all objects for a program. In functional terms it is a program, but on oo
|
||||
@ -35,9 +36,19 @@ module Vm
|
||||
#main gets executed between entry and exit
|
||||
@main = Block.new("main",nil)
|
||||
@exit = Core::Kernel::main_exit Vm::Block.new("main_exit",nil)
|
||||
boot_classes
|
||||
end
|
||||
attr_reader :context , :main , :classes , :entry , :exit
|
||||
|
||||
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].each do |f|
|
||||
puts "adding #{f}"
|
||||
obj.add_function Boot::Object.send(f , @context)
|
||||
end
|
||||
end
|
||||
def add_object o
|
||||
return if @objects.include? o
|
||||
raise "must be derived from Code #{o.inspect}" unless o.is_a? Code
|
||||
@ -45,7 +56,7 @@ module Vm
|
||||
end
|
||||
|
||||
def get_or_create_class name
|
||||
raise "uups #{name}" unless name.is_a? Symbol
|
||||
raise "uups #{name}.#{name.class}" unless name.is_a? Symbol
|
||||
c = @classes[name]
|
||||
unless c
|
||||
c = BootClass.new(name,@context)
|
||||
|
@ -26,6 +26,7 @@ module Vm
|
||||
attr_reader :string
|
||||
# currently aligned to 4 (ie padded with 0) and off course 0 at the end
|
||||
def initialize str
|
||||
str = str.to_s if str.is_a? Symbol
|
||||
length = str.length
|
||||
# rounding up to the next 4 (always adding one for zero pad)
|
||||
pad = ((length / 4 ) + 1 ) * 4 - length
|
||||
|
37
lib/vm/meta_class.rb
Normal file
37
lib/vm/meta_class.rb
Normal file
@ -0,0 +1,37 @@
|
||||
module Vm
|
||||
# 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 < Code
|
||||
# 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? 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 }
|
||||
puts "no function for #{name} in Meta #{@me_self.inspect}" unless f
|
||||
f
|
||||
end
|
||||
def inspect
|
||||
"#{@me_self}, #{@functions.length} functions"
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user