From cdea4915f9e897254acf2d18f57a95c7748e8b57 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 5 May 2015 15:11:09 +0300 Subject: [PATCH] bit more cleanup after compiler --- lib/compiler/basic_expressions.rb | 4 ++-- lib/compiler/call_site_expression.rb | 6 +++--- lib/compiler/function_expression.rb | 20 ++++++++++---------- lib/compiler/module_expression.rb | 4 ++-- lib/virtual/compiled_method.rb | 8 ++++---- lib/virtual/message.rb | 8 ++++---- test/virtual/test_machine.rb | 6 +++--- test/virtual/virtual_helper.rb | 4 +++- 8 files changed, 31 insertions(+), 29 deletions(-) diff --git a/lib/compiler/basic_expressions.rb b/lib/compiler/basic_expressions.rb index 6b935e3b..393bed6f 100644 --- a/lib/compiler/basic_expressions.rb +++ b/lib/compiler/basic_expressions.rb @@ -73,8 +73,8 @@ module Compiler #attr_reader :left, :right def self.compile_assignment expession , method , message - raise "must assign to NameExpression , not #{expession.left}" unless expession.left.instance_of? NameExpression - r = right.compile(method,message) + raise "must assign to NameExpression , not #{expession.left}" unless expession.left.instance_of? Ast::NameExpression + r = Compiler.compile(expession.right , method,message) raise "oh noo, nil from where #{expession.right.inspect}" unless r message.compile_set( method , expession.left.name , r ) end diff --git a/lib/compiler/call_site_expression.rb b/lib/compiler/call_site_expression.rb index f2deb1d6..05bc0ab9 100644 --- a/lib/compiler/call_site_expression.rb +++ b/lib/compiler/call_site_expression.rb @@ -3,15 +3,15 @@ module Compiler # call_site - attr_reader :name, :args , :receiver - def self.compile_call_site expession , method , message - me = expession.receiver.compile( method, message ) + def self.compile_callsite expession , method , message + me = Compiler.compile( expession.receiver , method, message ) method.add_code Virtual::NewMessage.new method.add_code Virtual::Set.new(Virtual::NewSelf.new(me.type), me) method.add_code Virtual::Set.new(Virtual::NewName.new(), Virtual::StringConstant.new(expession.name)) compiled_args = [] expession.args.each_with_index do |arg , i| #compile in the running method, ie before passing control - val = arg.compile( method, message) + val = Compiler.compile( arg , method, message) # move the compiled value to it's slot in the new message to = Virtual::NewMessageSlot.new(i ,val.type , val) # (doing this immediately, not after the loop, so if it's a return it won't get overwritten) diff --git a/lib/compiler/function_expression.rb b/lib/compiler/function_expression.rb index 2b9e0d94..de64ecd9 100644 --- a/lib/compiler/function_expression.rb +++ b/lib/compiler/function_expression.rb @@ -1,20 +1,20 @@ module Compiler # function attr_reader :name, :params, :body , :receiver def self.compile_function expression, method , message - args = expession.params.collect do |p| - raise "error, argument must be a identifier, not #{p}" unless p.is_a? NameExpression + args = expression.params.collect do |p| + raise "error, argument must be a identifier, not #{p}" unless p.is_a? Ast::NameExpression p.name end - r = expession.receiver ? expession.receiver.compile(method,message) : Virtual::Self.new() - new_method = Virtual::CompiledMethod.new(expession.name , args , r ) + r = expression.receiver ? expression.receiver.compile(method,message) : Virtual::Self.new() + new_method = Virtual::CompiledMethod.new(expression.name , args , r ) new_method.class_name = r.is_a?(Virtual::BootClass) ? r.name : method.class_name clazz = Virtual::BootSpace.space.get_or_create_class(new_method.class_name) clazz.add_instance_method new_method #frame = frame.new_frame return_type = nil - expession.body.each do |ex| - return_type = ex.compile(new_method,message ) + expression.body.each do |ex| + return_type = Compiler.compile(ex,new_method,message ) raise return_type.inspect if return_type.is_a? Virtual::Instruction end new_method.return_type = return_type @@ -23,7 +23,7 @@ module Compiler def scratch args = [] locals = {} - expession.params.each_with_index do |param , index| + expression.params.each_with_index do |param , index| arg = param.name register = Virtual::RegisterReference.new(Virtual::RegisterMachine.instance.receiver_register).next_reg_use(index + 1) arg_value = Virtual::Integer.new(register) @@ -32,10 +32,10 @@ module Compiler end # class depends on receiver me = Virtual::Integer.new( Virtual::RegisterMachine.instance.receiver_register ) - if expession.receiver.nil? + if expression.receiver.nil? clazz = context.current_class else - c = context.object_space.get_or_create_class expession.receiver.name.to_sym + c = context.object_space.get_or_create_class expression.receiver.name.to_sym clazz = c.meta_class end @@ -48,7 +48,7 @@ module Compiler context.function = function last_compiled = nil - expession.body.each do |b| + expression.body.each do |b| puts "compiling in function #{b}" last_compiled = b.compile(context) raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Virtual::Word diff --git a/lib/compiler/module_expression.rb b/lib/compiler/module_expression.rb index a3c2cd69..c051ee32 100644 --- a/lib/compiler/module_expression.rb +++ b/lib/compiler/module_expression.rb @@ -5,14 +5,14 @@ module Compiler end def self.compile_class expression , method , message - clazz = ::Virtual::BootSpace.space.get_or_create_class name + clazz = ::Virtual::BootSpace.space.get_or_create_class expression.name puts "Created class #{clazz.name.inspect}" expression.expressions.each do |expr| # check if it's a function definition and add # if not, execute it, but that does means we should be in salama (executable), not ruby. ie throw an error for now raise "only functions for now #{expr.inspect}" unless expr.is_a? Ast::FunctionExpression #puts "compiling expression #{expression}" - expression_value = expr.compile(method,message ) + expression_value = Compiler.compile(expr,method,message ) clazz.add_instance_method(expression_value) #puts "compiled expression #{expression_value.inspect}" end diff --git a/lib/virtual/compiled_method.rb b/lib/virtual/compiled_method.rb index 932f13d4..437f4b60 100644 --- a/lib/virtual/compiled_method.rb +++ b/lib/virtual/compiled_method.rb @@ -112,16 +112,16 @@ module Virtual # used to determine if a send must be issued def has_var name name = name.to_sym - var = @arg_names.find {|a| a.name == name } - var = @locals.find {|a| a.name == name } unless var - var = @tmps.find {|a| a.name == name } unless var + var = @arg_names.find {|a| a == name } + var = @locals.find {|a| a == name } unless var + var = @tmps.find {|a| a == name } unless var var end # determine whether this method has an argument by the name def has_arg name name = name.to_sym - var = @arg_names.find {|a| a.name == name } + var = @arg_names.find {|a| a == name } var end diff --git a/lib/virtual/message.rb b/lib/virtual/message.rb index dd3725c4..4f99193b 100644 --- a/lib/virtual/message.rb +++ b/lib/virtual/message.rb @@ -8,13 +8,13 @@ module Virtual # The methods that are there, are nevertheless meant to be called at compile time and generate code, rather than # executing it. - + # The caller creates the Message and passes control to the receiver's method # The receiver create a new Frame to hold local and temporary variables and (later) creates default values for # arguments that were not passed - # How the actual finding of the method takes place (acording to the ruby rules) is not simple, but as there is a + # How the actual finding of the method takes place (acording to the ruby rules) is not simple, but as there is a # guaranteed result (be it method_missing) it does not matter to the passing mechanism described # During compilation Message and frame objects are created to do type analysis @@ -27,7 +27,7 @@ module Virtual NEW_MESSAGE_REG = :r3 TMP_REG = :r4 - + def initialize me , normal , exceptional @me = me @next_normal = normal @@ -42,7 +42,7 @@ module Virtual def new_frame raise self.inspect end - # + # def compile_get method , name raise "CALLED" if method.has_arg(name) diff --git a/test/virtual/test_machine.rb b/test/virtual/test_machine.rb index ab6406b3..e3634136 100644 --- a/test/virtual/test_machine.rb +++ b/test/virtual/test_machine.rb @@ -2,7 +2,7 @@ require_relative "virtual_helper" class TestMethods < MiniTest::Test include VirtualHelper - + def test_object @string_input = <