another step closer to a working oo system

This commit is contained in:
Torsten Ruger 2014-06-03 22:16:57 +03:00
parent ca19f5cb16
commit 72d4adc7af
10 changed files with 43 additions and 43 deletions

View File

@ -11,7 +11,7 @@ module Arm
@attributes[:update_status] = 0 if @attributes[:update_status] == nil @attributes[:update_status] = 0 if @attributes[:update_status] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil @attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@operand = 0 @operand = 0
raise "alert" if right.is_a? Vm::Block
@pre_post_index = 0 #P flag @pre_post_index = 0 #P flag
@add_offset = 0 #U flag @add_offset = 0 #U flag
@is_load = opcode.to_s[0] == "l" ? 1 : 0 #L (load) flag @is_load = opcode.to_s[0] == "l" ? 1 : 0 #L (load) flag
@ -31,7 +31,10 @@ module Arm
@rn = arg @rn = arg
if @right if @right
@operand = @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) unless( @operand.is_a? Symbol)
puts "operand #{@operand.inspect}"
if (@operand < 0) if (@operand < 0)
@add_offset = 0 @add_offset = 0
#TODO test/check/understand #TODO test/check/understand

View File

@ -35,7 +35,7 @@ module Ast
"#{self.class.name}.new(#{name})" "#{self.class.name}.new(#{name})"
end end
def to_s def to_s
name name.to_s
end end
def attributes def attributes
[:name] [:name]
@ -54,7 +54,7 @@ module Ast
self.class.name + '.new("' + string + '")' self.class.name + '.new("' + string + '")'
end end
def to_s def to_s
'"' + string + '"' '"' + string.to_s + '"'
end end
def compile context , into def compile context , into
value = Vm::StringConstant.new(string) value = Vm::StringConstant.new(string)

View File

@ -13,15 +13,21 @@ module Ast
def compile context , into def compile context , into
params = args.collect{ |a| a.compile(context, into) } params = args.collect{ |a| a.compile(context, into) }
r = context.current_class.name if receiver.name == :self
if !receiver.nil? and receiver.name != :self function = context.current_class.get_or_create_function(name)
r = receiver.name elsif receiver.is_a? ModuleName
raise "uups #{receiver.class}.#{receiver.name.class}" unless r.is_a? Symbol 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 end
clazz = context.object_space.get_or_create_class r raise "No such method error #{clazz.to_s}:#{name}" if function == nil
function = context.current_class.get_function(name)
raise "Forward declaration not implemented for #{clazz.name}:#{name} #{inspect}" if function == nil
call = Vm::CallSite.new( name , params , function) call = Vm::CallSite.new( name , params , function)
current_function = context.function current_function = context.function
current_function.save_locals(context , into) if current_function current_function.save_locals(context , into) if current_function

View File

@ -24,10 +24,10 @@ module Boot
return_to = get_function.return_type return_to = get_function.return_type
index_function = context.object_space.get_or_create_class(:Object).get_or_create_function(:index_of) index_function = context.object_space.get_or_create_class(:Object).get_or_create_function(:index_of)
b = get_function.body b = get_function.body
b.push( me ) b.push( [me] )
index = b.call( index_function ) b.call( index_function )
b.pop(me) b.pop([me])
return_to.at_index( get_function.body , me , index ) return_to.at_index( get_function.body , me , return_to )
get_function.set_return return_to get_function.set_return return_to
return get_function return get_function
end end

View File

@ -1,26 +1,6 @@
# this is not a "normal" ruby file, ie it is not required by crystal # 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 # 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 class Array < BaseObject
def initialize size def initialize size

View File

@ -21,9 +21,11 @@ module Vm
@functions << function @functions << function
end end
def get_function name def get_function fname
name = name.to_sym fname = fname.to_sym
@functions.detect{ |f| f.name == name } f = @functions.detect{ |f| f.name == fname }
names = @functions.collect{|f| f.name }
f
end end
# way of creating new functions that have not been parsed. # way of creating new functions that have not been parsed.
@ -36,12 +38,15 @@ module Vm
end end
unless fun unless fun
fun = Core::Kernel.send(name , @context) fun = Core::Kernel.send(name , @context)
raise "no such function #{name}, #{name.class}" if fun == nil return nil if fun == nil
@functions << fun @functions << fun
end end
fun fun
end end
def inspect
"BootClass #{@name} , super #{@super_class} #{@functions.length} functions"
end
# Code interface follows. Note position is inheitted as is from Code # Code interface follows. Note position is inheitted as is from Code
# length of the class is the length of it's functions # length of the class is the length of it's functions

View File

@ -45,6 +45,7 @@ module Vm
# dummies, just for the other to compile # dummies, just for the other to compile
obj = get_or_create_class :Object obj = get_or_create_class :Object
[:index_of , :_get_instance_variable].each do |f| [:index_of , :_get_instance_variable].each do |f|
puts "adding #{f}"
obj.add_function Boot::Object.send(f , @context) obj.add_function Boot::Object.send(f , @context)
end end
end end

View File

@ -26,6 +26,7 @@ module Vm
attr_reader :string attr_reader :string
# currently aligned to 4 (ie padded with 0) and off course 0 at the end # currently aligned to 4 (ie padded with 0) and off course 0 at the end
def initialize str def initialize str
str = str.to_s if str.is_a? Symbol
length = str.length length = str.length
# rounding up to the next 4 (always adding one for zero pad) # rounding up to the next 4 (always adding one for zero pad)
pad = ((length / 4 ) + 1 ) * 4 - length pad = ((length / 4 ) + 1 ) * 4 - length

View File

@ -26,8 +26,12 @@ module Vm
def get_function name def get_function name
name = name.to_sym name = name.to_sym
@functions.detect{ |f| f.name == name } 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 end
end end

View File

@ -3,7 +3,7 @@ require_relative 'helper'
class TestWhileFragment < MiniTest::Test class TestWhileFragment < MiniTest::Test
include Fragments include Fragments
def test_while def test_while_fibo
@string_input = <<HERE @string_input = <<HERE
def fibonaccit(n) # n == r0 def fibonaccit(n) # n == r0
a = 0 # a == r1 a = 0 # a == r1