From db8b1488d0b0ed7767dde4c611a1a7697ed2185b Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 14 Jul 2014 14:06:09 +0300 Subject: [PATCH] function test, slow but steady --- lib/ast/basic_expressions.rb | 2 +- lib/ast/function_expression.rb | 4 +++- lib/boot/boot_class.rb | 10 ++++------ lib/boot/boot_space.rb | 4 ++-- lib/virtual/method.rb | 9 +++++---- lib/virtual/reference.rb | 4 +++- test/virtual/test_methods.rb | 7 +++---- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/ast/basic_expressions.rb b/lib/ast/basic_expressions.rb index a2413698..84e980f0 100644 --- a/lib/ast/basic_expressions.rb +++ b/lib/ast/basic_expressions.rb @@ -48,7 +48,7 @@ module Ast clazz = ::Virtual::Object.space.get_or_create_class name raise "uups #{clazz}.#{name}" unless clazz #class qualifier, means call from metaclass - clazz = clazz.meta_class + #clazz = clazz.meta_class clazz end end diff --git a/lib/ast/function_expression.rb b/lib/ast/function_expression.rb index 97c90169..4d2fcd01 100644 --- a/lib/ast/function_expression.rb +++ b/lib/ast/function_expression.rb @@ -2,7 +2,9 @@ module Ast class FunctionExpression < Expression # attr_reader :name, :params, :body , :receiver def compile frame , method - method = Virtual::Method.new(name , params ) + args = params.collect{ |p| p.compile(frame , method )} + r = receiver ? receiver.compile(frame,method) : Virtual::SelfReference.new + method = Virtual::Method.new(name , params , r ) end def scratch args = [] diff --git a/lib/boot/boot_class.rb b/lib/boot/boot_class.rb index 04083a17..37dc7920 100644 --- a/lib/boot/boot_class.rb +++ b/lib/boot/boot_class.rb @@ -4,9 +4,8 @@ module Boot # class is mainly a list of methods with a name (for now) # layout of object is seperated into Layout class BootClass < Virtual::ObjectConstant - def initialize name , scope , super_class = :Object + def initialize name , super_class = :Object super() - @scope = scope # class methods @methods = [] @name = name.to_sym @@ -14,7 +13,9 @@ module Boot @meta_class = MetaClass.new(self) end attr_reader :name , :methods , :meta_class , :context , :super_class - + def attributes + [:name , :super_class] + end def add_method method raise "not a method #{method}" unless method.is_a? Virtual::Method raise "syserr " unless method.name.is_a? Symbol @@ -40,9 +41,6 @@ module Boot fun end - def inspect - "BootClass #{@name} < #{@super_class}:#{@methods.collect(&:name)}" - end def to_s inspect end diff --git a/lib/boot/boot_space.rb b/lib/boot/boot_space.rb index 1edb7fac..3010b1fb 100644 --- a/lib/boot/boot_space.rb +++ b/lib/boot/boot_space.rb @@ -21,7 +21,7 @@ module Boot def initialize machine = nil super() @classes = {} - @main = Virtual::Method.new("main") + @main = Virtual::Method.new("main" , [] ) #global objects (data) @objects = [] boot_classes @@ -77,7 +77,7 @@ module Boot raise "uups #{name}.#{name.class}" unless name.is_a? Symbol c = @classes[name] unless c - c = BootClass.new(name,@context) + c = BootClass.new(name) @classes[name] = c end c diff --git a/lib/virtual/method.rb b/lib/virtual/method.rb index 8314be5e..cddabb1c 100644 --- a/lib/virtual/method.rb +++ b/lib/virtual/method.rb @@ -1,4 +1,5 @@ require_relative "object" + module Virtual # static description of a method # name @@ -11,12 +12,12 @@ module Virtual class Method < Virtual::Object #return the main function (the top level) into which code is compiled def Method.main - Method.new(:main) + Method.new(:main , [] , Virtual::SelfReference ) end def attributes - [:name , :args] + [:name , :args , :receiver] end - def initialize name , args = [] , receiver = Virtual::Reference , return_type = Virtual::Reference + def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Reference @name = name.to_sym @args = args @locals = [] @@ -25,7 +26,7 @@ module Virtual @start = MethodEnter.new @current = @start end - attr_reader :name , :args + attr_reader :name , :args , :receiver def add instruction @current.next = instruction diff --git a/lib/virtual/reference.rb b/lib/virtual/reference.rb index 08588f8a..36efc859 100644 --- a/lib/virtual/reference.rb +++ b/lib/virtual/reference.rb @@ -12,6 +12,8 @@ module Virtual block.ldr( self , left , right ) self end - + end + + class SelfReference < Reference end end diff --git a/test/virtual/test_methods.rb b/test/virtual/test_methods.rb index 3399d71d..22fb183c 100644 --- a/test/virtual/test_methods.rb +++ b/test/virtual/test_methods.rb @@ -9,7 +9,7 @@ def foo(x) 5 end HERE - @output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])] + @output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new())] check end @@ -19,9 +19,8 @@ def String.length(x) @length end HERE - @parse_output = {:receiver=>{:module_name=>"String"}, :function_name=>{:name=>"length"}, :parameter_list=>[{:parameter=>{: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") ) - @parser = @parser.function_definition + @output = [Virtual::Method.new(:length,[Ast::NameExpression.new(:x)],Boot::BootClass.new(:String,:Object))] + check end def test_function_ops