From b7c2089046ba3018df97228da969d8c59dc85c36 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 3 Jun 2014 14:49:02 +0300 Subject: [PATCH 1/3] the further down this goes, the smaller the circles. Soon the point will come. And then back out --- lib/ast/call_site_expression.rb | 14 ++++++--- lib/ast/function_expression.rb | 12 ++++++-- lib/boot/object.rb | 51 +++++++++++++++++++++++++++++++++ lib/core/base_object.rb | 23 --------------- lib/core/kernel.rb | 21 -------------- lib/vm/boot_space.rb | 12 +++++++- test/fragments/test_class.rb | 13 ++++++--- 7 files changed, 90 insertions(+), 56 deletions(-) create mode 100644 lib/boot/object.rb delete mode 100644 lib/core/base_object.rb diff --git a/lib/ast/call_site_expression.rb b/lib/ast/call_site_expression.rb index ff9aae88..f9c01be8 100644 --- a/lib/ast/call_site_expression.rb +++ b/lib/ast/call_site_expression.rb @@ -4,15 +4,21 @@ module Ast class CallSiteExpression < Expression attr_reader :name, :args , :receiver - def initialize name, args , receiver = Ast::NameExpression.new("self") + def initialize name, args , receiver = Ast::NameExpression.new(:self) @name , @args , @receiver = name.to_sym , args , 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 + + r = context.current_class.name + if !receiver.nil? and receiver.name != :self + r = receiver.name + end + clazz = context.object_space.get_or_create_class r + + 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) current_function = context.function current_function.save_locals(context , into) if current_function diff --git a/lib/ast/function_expression.rb b/lib/ast/function_expression.rb index 806c47cc..e161ab32 100644 --- a/lib/ast/function_expression.rb +++ b/lib/ast/function_expression.rb @@ -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 diff --git a/lib/boot/object.rb b/lib/boot/object.rb new file mode 100644 index 00000000..822c660c --- /dev/null +++ b/lib/boot/object.rb @@ -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 ) + 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 + + 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 diff --git a/lib/core/base_object.rb b/lib/core/base_object.rb deleted file mode 100644 index dda53708..00000000 --- a/lib/core/base_object.rb +++ /dev/null @@ -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 diff --git a/lib/core/kernel.rb b/lib/core/kernel.rb index 09badc56..4985468d 100644 --- a/lib/core/kernel.rb +++ b/lib/core/kernel.rb @@ -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 diff --git a/lib/vm/boot_space.rb b/lib/vm/boot_space.rb index 775b039d..7dc32814 100644 --- a/lib/vm/boot_space.rb +++ b/lib/vm/boot_space.rb @@ -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,18 @@ 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| + 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 +55,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) diff --git a/test/fragments/test_class.rb b/test/fragments/test_class.rb index da32ea95..33f9e664 100644 --- a/test/fragments/test_class.rb +++ b/test/fragments/test_class.rb @@ -16,20 +16,25 @@ class Object return @layout.class() end end +class Class + def Class.new_object( length ) + return 4 + end +end class String + def String.new_string( len ) + return Class.new_object( len << 2 ) + end def length() return @length end - def self.new_string( len ) - 4 - end def plus(str) my_length = @length str_len = str.length() new_string = String.new_string(my_length + str_len) i = 0 while( i < my_length) do - char = self.get(i) + char = get(i) new_string.set(i , char) i = i + 1 end From ca19f5cb162aa936dc60e00441019b98ba4f4117 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 3 Jun 2014 20:47:06 +0300 Subject: [PATCH 2/3] move to syms for names --- lib/arm/assembler.rb | 1 + lib/ast/basic_expressions.rb | 4 +-- lib/ast/call_site_expression.rb | 5 +++- lib/ast/module_expression.rb | 2 ++ lib/vm/boot_class.rb | 14 +++++++---- lib/vm/meta_class.rb | 33 +++++++++++++++++++++++++ test/parser/test_basic.rb | 2 +- test/parser/test_call_site.rb | 4 +-- test/parser/test_class.rb | 6 ++--- test/parser/test_function_definition.rb | 2 +- test/parser/test_module.rb | 2 +- test/parser/test_operators.rb | 12 ++++----- 12 files changed, 65 insertions(+), 22 deletions(-) create mode 100644 lib/vm/meta_class.rb diff --git a/lib/arm/assembler.rb b/lib/arm/assembler.rb index e4217468..a6a3c650 100644 --- a/lib/arm/assembler.rb +++ b/lib/arm/assembler.rb @@ -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 diff --git a/lib/ast/basic_expressions.rb b/lib/ast/basic_expressions.rb index 2f87f421..67e79f50 100644 --- a/lib/ast/basic_expressions.rb +++ b/lib/ast/basic_expressions.rb @@ -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,7 +32,7 @@ module Ast context.locals[name] end def inspect - self.class.name + '.new("' + name + '")' + "#{self.class.name}.new(#{name})" end def to_s name diff --git a/lib/ast/call_site_expression.rb b/lib/ast/call_site_expression.rb index f9c01be8..b537c020 100644 --- a/lib/ast/call_site_expression.rb +++ b/lib/ast/call_site_expression.rb @@ -5,7 +5,9 @@ module Ast attr_reader :name, :args , :receiver def initialize name, args , receiver = Ast::NameExpression.new(:self) - @name , @args , @receiver = name.to_sym , args , receiver + @name = name.to_sym + @args = args + @receiver = receiver end def compile context , into @@ -14,6 +16,7 @@ module Ast r = context.current_class.name if !receiver.nil? and receiver.name != :self r = receiver.name + raise "uups #{receiver.class}.#{receiver.name.class}" unless r.is_a? Symbol end clazz = context.object_space.get_or_create_class r diff --git a/lib/ast/module_expression.rb b/lib/ast/module_expression.rb index 101da84e..0ca2a01e 100644 --- a/lib/ast/module_expression.rb +++ b/lib/ast/module_expression.rb @@ -1,6 +1,8 @@ module Ast class ModuleExpression < Expression + attr_reader :name ,:expressions + def initialize name , expressions @name = name.to_sym @expressions = expressions diff --git a/lib/vm/boot_class.rb b/lib/vm/boot_class.rb index d83a25ec..d2bcc58c 100644 --- a/lib/vm/boot_class.rb +++ b/lib/vm/boot_class.rb @@ -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 @@ -22,11 +26,11 @@ module Vm @functions.detect{ |f| f.name == name } 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 diff --git a/lib/vm/meta_class.rb b/lib/vm/meta_class.rb new file mode 100644 index 00000000..1442de37 --- /dev/null +++ b/lib/vm/meta_class.rb @@ -0,0 +1,33 @@ +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 + @functions.detect{ |f| f.name == name } + end + + end +end diff --git a/test/parser/test_basic.rb b/test/parser/test_basic.rb index 49606c09..4caa1071 100644 --- a/test/parser/test_basic.rb +++ b/test/parser/test_basic.rb @@ -35,7 +35,7 @@ class TestBasic < MiniTest::Test def test_instance_variable @string_input = '@foo_bar ' @parse_output = {:instance_variable=>{:name=>"foo_bar"}} - @transform_output = Ast::VariableExpression.new('foo_bar') + @transform_output = Ast::VariableExpression.new(:foo_bar) @parser = @parser.instance_variable end diff --git a/test/parser/test_call_site.rb b/test/parser/test_call_site.rb index f6712008..12619a47 100644 --- a/test/parser/test_call_site.rb +++ b/test/parser/test_call_site.rb @@ -22,7 +22,7 @@ class TestCallSite < MiniTest::Test def test_single_instance @string_input = '@var.foo(42)' @parse_output = {:receiver=>{:instance_variable=>{:name=>"var"}}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]} - @transform_output = Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::VariableExpression.new("var")) + @transform_output = Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::VariableExpression.new(:var)) @parser = @parser.call_site end @@ -96,7 +96,7 @@ class TestCallSite < MiniTest::Test def test_call_chaining_instance @string_input = '@class.new(self.get(4))' @parse_output = {:receiver=>{:instance_variable=>{:name=>"class"}}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:receiver=>{:name=>"self"}, :call_site=>{:name=>"get"}, :argument_list=>[{:argument=>{:integer=>"4"}}]}}]} - @transform_output = Ast::CallSiteExpression.new(:new, [Ast::CallSiteExpression.new(:get, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new("self"))] ,Ast::VariableExpression.new("class")) + @transform_output = Ast::CallSiteExpression.new(:new, [Ast::CallSiteExpression.new(:get, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new("self"))] ,Ast::VariableExpression.new(:class)) @parser = @parser.call_site end diff --git a/test/parser/test_class.rb b/test/parser/test_class.rb index 5d3f65ab..183a5f3a 100644 --- a/test/parser/test_class.rb +++ b/test/parser/test_class.rb @@ -25,7 +25,7 @@ class Opers end HERE @parse_output = {:module_name=>"Opers", :class_expressions=>[{:function_name=>{:name=>"foo"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"} - @transform_output = Ast::ClassExpression.new(:Opers ,[Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::VariableExpression.new("abba"),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] )] ) + @transform_output = Ast::ClassExpression.new(:Opers ,[Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::VariableExpression.new(:abba),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] )] ) @parser = @parser.class_definition end @@ -56,7 +56,7 @@ class Pifi end HERE @parse_output = {:module_name=>"Pifi", :class_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}, {:function_name=>{:name=>"ofthen"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}, {:parmeter=>{:name=>"m"}}], :expressions=>[{:integer=>"44"}], :end=>"end"}], :end=>"end"} - @transform_output =Ast::ClassExpression.new(:Pifi ,[Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] ), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n"),Ast::NameExpression.new("m")] , [Ast::IntegerExpression.new(44)] )] ) + @transform_output =Ast::ClassExpression.new(:Pifi ,[Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n"),Ast::NameExpression.new("m")] , [Ast::IntegerExpression.new(44)] )] ) @parser = @parser.class_definition end def test_class_module @@ -68,7 +68,7 @@ class Foo end HERE @parse_output = {:module_name=>"Foo", :class_expressions=>[{:module_name=>"Boo", :module_expressions=>[{:call_site=>{:name=>"funcall"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}], :end=>"end"} - @transform_output = Ast::ClassExpression.new(:Foo ,[Ast::ModuleExpression.new(:Boo ,[Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] )] )] ) + @transform_output = Ast::ClassExpression.new(:Foo ,[Ast::ModuleExpression.new(:Boo ,[Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] )] )] ) @parser = @parser.class_definition end end \ No newline at end of file diff --git a/test/parser/test_function_definition.rb b/test/parser/test_function_definition.rb index 84b5911b..21a6de97 100644 --- a/test/parser/test_function_definition.rb +++ b/test/parser/test_function_definition.rb @@ -22,7 +22,7 @@ def String.length(x) end HERE @parse_output = {:receiver=>{:module_name=>"String"}, :function_name=>{:name=>"length"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:instance_variable=>{:name=>"length"}}], :end=>"end"} - @transform_output = Ast::FunctionExpression.new(:length, [Ast::NameExpression.new("x")] , [Ast::VariableExpression.new("length")] ,Ast::ModuleName.new("String") ) + @transform_output = Ast::FunctionExpression.new(:length, [Ast::NameExpression.new("x")] , [Ast::VariableExpression.new(:length)] ,Ast::ModuleName.new("String") ) @parser = @parser.function_definition end diff --git a/test/parser/test_module.rb b/test/parser/test_module.rb index 35cb2650..9089d0a6 100644 --- a/test/parser/test_module.rb +++ b/test/parser/test_module.rb @@ -36,7 +36,7 @@ module Opers end HERE @parse_output = {:module_name=>"Opers", :module_expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"} - @transform_output = Ast::ModuleExpression.new(:Opers ,[Ast::OperatorExpression.new("=", Ast::VariableExpression.new("abba"),Ast::IntegerExpression.new(5))] ) + @transform_output = Ast::ModuleExpression.new(:Opers ,[Ast::OperatorExpression.new("=", Ast::VariableExpression.new(:abba),Ast::IntegerExpression.new(5))] ) @parser = @parser.module_definition end diff --git a/test/parser/test_operators.rb b/test/parser/test_operators.rb index 523e0d19..6d3c7d7f 100644 --- a/test/parser/test_operators.rb +++ b/test/parser/test_operators.rb @@ -31,25 +31,25 @@ class TestExpressions < MiniTest::Test def test_op_variable @string_input = "a + 35" @parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}} - @transform_output = Ast::OperatorExpression.new("+", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(35)) + @transform_output = Ast::OperatorExpression.new("+", Ast::NameExpression.new(:a),Ast::IntegerExpression.new(35)) @parser = @parser.operator_expression end def test_op_two_variable @string_input = "a - b" @parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:name=>"b"}} - @transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::NameExpression.new("b")) + @transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::NameExpression.new("b")) @parser = @parser.operator_expression end def test_op_instance_variable @string_input = "@a - 5" @parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"- ", :r=>{:integer=>"5"}} - @transform_output = Ast::OperatorExpression.new("-", Ast::VariableExpression.new("a"),Ast::IntegerExpression.new(5)) + @transform_output = Ast::OperatorExpression.new("-", Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5)) @parser = @parser.operator_expression end def test_op_variable_string @string_input = 'a - "st"' @parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}} - @transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::StringExpression.new("st")) + @transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::StringExpression.new("st")) @parser = @parser.operator_expression end def test_two_same_ops @@ -73,13 +73,13 @@ class TestExpressions < MiniTest::Test def test_assignment @string_input = "a = 5" @parse_output = {:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}} - @transform_output = Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(5)) + @transform_output = Ast::OperatorExpression.new("=", Ast::NameExpression.new(:a),Ast::IntegerExpression.new(5)) @parser = @parser.operator_expression end def test_assignment_instance @string_input = "@a = 5" @parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"= ", :r=>{:integer=>"5"}} - @transform_output = Ast::OperatorExpression.new("=", Ast::VariableExpression.new("a"),Ast::IntegerExpression.new(5)) + @transform_output = Ast::OperatorExpression.new("=", Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5)) @parser = @parser.operator_expression end From 72d4adc7af2d09f1e862fd36dc929264802a6fd6 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 3 Jun 2014 22:16:57 +0300 Subject: [PATCH 3/3] another step closer to a working oo system --- lib/arm/memory_instruction.rb | 5 ++++- lib/ast/basic_expressions.rb | 4 ++-- lib/ast/call_site_expression.rb | 22 ++++++++++++++-------- lib/boot/object.rb | 8 ++++---- lib/core/array.rb | 20 -------------------- lib/vm/boot_class.rb | 15 ++++++++++----- lib/vm/boot_space.rb | 1 + lib/vm/constants.rb | 1 + lib/vm/meta_class.rb | 8 ++++++-- test/fragments/test_while_fibo.rb | 2 +- 10 files changed, 43 insertions(+), 43 deletions(-) diff --git a/lib/arm/memory_instruction.rb b/lib/arm/memory_instruction.rb index 266554ad..2f75fdb9 100644 --- a/lib/arm/memory_instruction.rb +++ b/lib/arm/memory_instruction.rb @@ -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 diff --git a/lib/ast/basic_expressions.rb b/lib/ast/basic_expressions.rb index 67e79f50..c6bed8ff 100644 --- a/lib/ast/basic_expressions.rb +++ b/lib/ast/basic_expressions.rb @@ -35,7 +35,7 @@ module Ast "#{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) diff --git a/lib/ast/call_site_expression.rb b/lib/ast/call_site_expression.rb index b537c020..45fd65e6 100644 --- a/lib/ast/call_site_expression.rb +++ b/lib/ast/call_site_expression.rb @@ -13,15 +13,21 @@ module Ast def compile context , into params = args.collect{ |a| a.compile(context, into) } - r = context.current_class.name - if !receiver.nil? and receiver.name != :self - r = receiver.name - raise "uups #{receiver.class}.#{receiver.name.class}" unless r.is_a? Symbol + 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 - clazz = context.object_space.get_or_create_class r - - function = context.current_class.get_function(name) - raise "Forward declaration not implemented for #{clazz.name}:#{name} #{inspect}" if function == nil + 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 diff --git a/lib/boot/object.rb b/lib/boot/object.rb index 822c660c..e5704f22 100644 --- a/lib/boot/object.rb +++ b/lib/boot/object.rb @@ -24,10 +24,10 @@ module Boot 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 ) + 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 diff --git a/lib/core/array.rb b/lib/core/array.rb index ab588db7..31687853 100644 --- a/lib/core/array.rb +++ b/lib/core/array.rb @@ -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 diff --git a/lib/vm/boot_class.rb b/lib/vm/boot_class.rb index d2bcc58c..1b6033fe 100644 --- a/lib/vm/boot_class.rb +++ b/lib/vm/boot_class.rb @@ -21,9 +21,11 @@ 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 # way of creating new functions that have not been parsed. @@ -36,12 +38,15 @@ module Vm 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 diff --git a/lib/vm/boot_space.rb b/lib/vm/boot_space.rb index 7dc32814..3a9523c6 100644 --- a/lib/vm/boot_space.rb +++ b/lib/vm/boot_space.rb @@ -45,6 +45,7 @@ module Vm # 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 diff --git a/lib/vm/constants.rb b/lib/vm/constants.rb index cefa550e..f34c4f67 100644 --- a/lib/vm/constants.rb +++ b/lib/vm/constants.rb @@ -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 diff --git a/lib/vm/meta_class.rb b/lib/vm/meta_class.rb index 1442de37..5a65cab2 100644 --- a/lib/vm/meta_class.rb +++ b/lib/vm/meta_class.rb @@ -26,8 +26,12 @@ module Vm def get_function name 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 diff --git a/test/fragments/test_while_fibo.rb b/test/fragments/test_while_fibo.rb index 3c4f7c35..8a2881d7 100644 --- a/test/fragments/test_while_fibo.rb +++ b/test/fragments/test_while_fibo.rb @@ -3,7 +3,7 @@ require_relative 'helper' class TestWhileFragment < MiniTest::Test include Fragments - def test_while + def test_while_fibo @string_input = <