more on classes, have to work on asm next

This commit is contained in:
Torsten Ruger 2014-06-01 21:03:08 +03:00
parent 1d10c2c03e
commit 08bbad0fdc
7 changed files with 28 additions and 9 deletions

View File

@ -31,6 +31,12 @@ module Arm
Vm::BranchCondition.new :gt
end
# TODO wrong type, should be object_reference. But that needs the actual typing to work
def integer_at_index block , result ,left , right
block << ldr( result , left , right )
result
end
def integer_plus block , result , left , right
block << add( result , left , right )
result

View File

@ -16,13 +16,15 @@ module Ast
end
def compile context , into
clazz = context.object_space.get_or_create_class name
puts "Created class #{clazz.name.inspect}"
context.current_class = clazz
expressions.each do |expression|
# check if it's a function definition and add
# if not, execute it, but that does means we should be in crystal (executable), not ruby. ie throw an error for now
raise "only functions for now #{expression.inspect}" unless expression.is_a? Ast::FunctionExpression
puts "compiling expression #{expression.inspect}"
expression_value = expression.compile(context , nil )
puts "compiled return expression #{expression_value.inspect}, now return in 7"
#puts "compiled expression #{expression_value.inspect}"
end
return nil

View File

@ -31,13 +31,14 @@ module Core
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)
b.mov( var_name , index )
Vm::RegisterMachine.instance.at_index( get_function.body , number , remainder )
return_to.at_index( get_function.body , me , index )
get_function.set_return return_to
return get_function
end

View File

@ -2,11 +2,12 @@ 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
def initialize name , context , superclass = :Object
@context = context
# class functions
@functions = []
@name = name.to_sym
@superclass = superclass
end
attr_reader :name , :functions
@ -24,6 +25,11 @@ module Vm
# preferred way of creating new functions (also forward declarations, will flag unresolved later)
def get_or_create_function name
fun = get_function name
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
unless fun
fun = Core::Kernel.send(name , @context)
raise "no such function #{name}, #{name.class}" if fun == nil

View File

@ -45,6 +45,7 @@ module Vm
end
def get_or_create_class name
raise "uups #{name}" unless name.is_a? Symbol
c = @classes[name]
unless c
c = BootClass.new(name,@context)

View File

@ -118,6 +118,9 @@ module Vm
def - other
class_for(LogicInstruction).new(nil , self , other , opcode: :sub )#, update_status: 1 )
end
def at_index block , left , right
RegisterMachine.instance.integer_at_index block , self , left , right
end
def plus block , first , right
RegisterMachine.instance.integer_plus block , self , first , right
end

View File

@ -6,14 +6,14 @@ class TestClass < MiniTest::Test
def test_class
@string_input = <<HERE
class Object
def index_of( name )
return @layout.index_of(name)
end
def layout()
return @layout
end
def class()
return layout.class()
end
def index_of( name )
return layout.index_of(name)
return @layout.class()
end
end
class String
@ -21,7 +21,7 @@ class String
return @length
end
def plus(str)
my_length = self.length()
my_length = @length
str_len = str.length()
new_string = String.new(my_length + str_len)
i = 0