From 08bbad0fdc93cddb7a3cb72eaa6467e9ad000c8f Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 1 Jun 2014 21:03:08 +0300 Subject: [PATCH] more on classes, have to work on asm next --- lib/arm/arm_machine.rb | 6 ++++++ lib/ast/module_expression.rb | 4 +++- lib/core/kernel.rb | 5 +++-- lib/vm/boot_class.rb | 8 +++++++- lib/vm/boot_space.rb | 1 + lib/vm/values.rb | 3 +++ test/fragments/test_class.rb | 10 +++++----- 7 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/arm/arm_machine.rb b/lib/arm/arm_machine.rb index c1f6d96e..e6f19f08 100644 --- a/lib/arm/arm_machine.rb +++ b/lib/arm/arm_machine.rb @@ -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 diff --git a/lib/ast/module_expression.rb b/lib/ast/module_expression.rb index 5d6ddfe4..6334eae4 100644 --- a/lib/ast/module_expression.rb +++ b/lib/ast/module_expression.rb @@ -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 diff --git a/lib/core/kernel.rb b/lib/core/kernel.rb index b4156f52..397d0931 100644 --- a/lib/core/kernel.rb +++ b/lib/core/kernel.rb @@ -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 diff --git a/lib/vm/boot_class.rb b/lib/vm/boot_class.rb index 20904adb..d83a25ec 100644 --- a/lib/vm/boot_class.rb +++ b/lib/vm/boot_class.rb @@ -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 diff --git a/lib/vm/boot_space.rb b/lib/vm/boot_space.rb index 04846e86..775b039d 100644 --- a/lib/vm/boot_space.rb +++ b/lib/vm/boot_space.rb @@ -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) diff --git a/lib/vm/values.rb b/lib/vm/values.rb index cdfe8990..7bc8e36b 100644 --- a/lib/vm/values.rb +++ b/lib/vm/values.rb @@ -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 diff --git a/test/fragments/test_class.rb b/test/fragments/test_class.rb index 8357a3ca..f38f42c4 100644 --- a/test/fragments/test_class.rb +++ b/test/fragments/test_class.rb @@ -6,14 +6,14 @@ class TestClass < MiniTest::Test def test_class @string_input = <