From 6f2affaf36068e9375c4086ac3016d6ff96617b5 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 8 May 2015 14:34:46 +0300 Subject: [PATCH] fix name expression and test plus various cleaning and rename off call_site --- lib/compiler.rb | 8 ++-- lib/compiler/basic_expressions.rb | 44 +++++++++---------- ...e_expression.rb => callsite_expression.rb} | 0 lib/virtual/boot_class.rb | 7 ++- test/virtual/test_basic.rb | 5 +-- 5 files changed, 31 insertions(+), 33 deletions(-) rename lib/compiler/{call_site_expression.rb => callsite_expression.rb} (100%) diff --git a/lib/compiler.rb b/lib/compiler.rb index 6327e467..7e8cec8c 100644 --- a/lib/compiler.rb +++ b/lib/compiler.rb @@ -1,12 +1,12 @@ module Compiler - def self.compile expression , method + def self.compile expression , method exp_name = expression.class.name.split("::").last.sub("Expression","").downcase #puts "Expression #{exp_name}" begin - self.send "compile_#{exp_name}".to_sym , expression, method + self.send "compile_#{exp_name}".to_sym , expression, method rescue NoMethodError => e - puts exp_name + puts "no compile method foudn for " + exp_name raise e end end @@ -14,7 +14,7 @@ module Compiler end require_relative "compiler/basic_expressions" -require_relative "compiler/call_site_expression" +require_relative "compiler/callsite_expression" require_relative "compiler/compound_expressions" require_relative "compiler/if_expression" require_relative "compiler/function_expression" diff --git a/lib/compiler/basic_expressions.rb b/lib/compiler/basic_expressions.rb index cff5f532..70a85bb2 100644 --- a/lib/compiler/basic_expressions.rb +++ b/lib/compiler/basic_expressions.rb @@ -11,28 +11,28 @@ module Compiler # But in the future (in the one that holds great things) we optimize those unneccesay moves away # attr_reader :value - def self.compile_integer expession , method - int = Virtual::IntegerConstant.new(expession.value) + def self.compile_integer expression , method + int = Virtual::IntegerConstant.new(expression.value) to = Virtual::Return.new(Virtual::Integer , int) method.add_code Virtual::Set.new( to , int) to end - def self.compile_true expession , method + def self.compile_true expression , method value = Virtual::TrueConstant.new to = Virtual::Return.new(Virtual::Reference , value) method.add_code Virtual::Set.new( to , value ) to end - def self.compile_false expession , method + def self.compile_false expression , method value = Virtual::FalseConstant.new to = Virtual::Return.new(Virtual::Reference , value) method.add_code Virtual::Set.new( to , value ) to end - def self.compile_nil expession , method + def self.compile_nil expression , method value = Virtual::NilConstant.new to = Virtual::Return.new(Virtual::Reference , value) method.add_code Virtual::Set.new( to , value ) @@ -41,20 +41,20 @@ module Compiler # attr_reader :name # compiling name needs to check if it's a variable and if so resolve it - # otherwise it's a method without args and a send is usued. + # otherwise it's a method without args and a send is issued. # this makes the namespace static, ie when eval and co are implemented method needs recompilation - def self.compile_name expession , method - return Virtual::Self.new( Virtual::Mystery ) if expession.name == :self - if method.has_var(expession.name) - message.compile_get(method , expession.name ) + def self.compile_name expression , method + return Virtual::Self.new( Virtual::Mystery ) if expression.name == :self + if method.has_var(expression.name) + message.compile_get(method , expression.name ) else - raise "TODO unimplemented branch #{expession.class}(#{expession})" - message.compile_send( method , expession.name , Virtual::Self.new( Virtual::Mystery ) ) + call = Ast::CallSiteExpression.new(expression.name , [] ) #receiver self is implicit + Compiler.compile(call, method) end end - def self.compile_module expession , method + def self.compile_module expression , method clazz = Virtual::BootSpace.space.get_or_create_class name raise "uups #{clazz}.#{name}" unless clazz to = Virtual::Return.new(Virtual::Reference , clazz ) @@ -63,8 +63,8 @@ module Compiler end # attr_reader :string - def self.compile_string expession , method - value = Virtual::StringConstant.new(expession.string) + def self.compile_string expression , method + value = Virtual::StringConstant.new(expression.string) to = Virtual::Return.new(Virtual::Reference , value) Virtual::BootSpace.space.add_object value method.add_code Virtual::Set.new( to , value ) @@ -72,22 +72,22 @@ module Compiler end #attr_reader :left, :right - def self.compile_assignment expession , method - raise "must assign to NameExpression , not #{expession.left}" unless expession.left.instance_of? Ast::NameExpression - r = Compiler.compile(expession.right , method ) - raise "oh noo, nil from where #{expession.right.inspect}" unless r + def self.compile_assignment expression , method + raise "must assign to NameExpression , not #{expression.left}" unless expression.left.instance_of? Ast::NameExpression + r = Compiler.compile(expression.right , method ) + raise "oh noo, nil from where #{expression.right.inspect}" unless r index = method.has_arg(name) if index method.add_code Virtual::Set.new(Virtual::Return.new , Virtual::MessageSlot.new(index , r,type , r )) else - index = method.ensure_local(expession.left.name) + index = method.ensure_local(expression.left.name) method.add_code Virtual::Set.new(Virtual::Return.new , Virtual::FrameSlot.new(index , r.type , r )) end r end - def self.compile_variable expession, method - method.add_code Virtual::InstanceGet.new(expession.name) + def self.compile_variable expression, method + method.add_code Virtual::InstanceGet.new(expression.name) Virtual::Return.new( Virtual::Mystery ) end end diff --git a/lib/compiler/call_site_expression.rb b/lib/compiler/callsite_expression.rb similarity index 100% rename from lib/compiler/call_site_expression.rb rename to lib/compiler/callsite_expression.rb diff --git a/lib/virtual/boot_class.rb b/lib/virtual/boot_class.rb index e62b216b..6a3bd3b5 100644 --- a/lib/virtual/boot_class.rb +++ b/lib/virtual/boot_class.rb @@ -1,7 +1,7 @@ require_relative "meta_class" module Virtual - + # class is mainly a list of methods with a name (for now) # layout of object is seperated into Layout class BootClass < Virtual::ObjectConstant @@ -22,8 +22,7 @@ module Virtual def get_instance_method fname fname = fname.to_sym - f = @instance_methods.detect{ |f| f.name == fname } - f + @instance_methods.detect{ |fun| fun.name == fname } end # get the method and if not found, try superclasses. raise error if not found @@ -52,4 +51,4 @@ module Virtual end -end \ No newline at end of file +end diff --git a/test/virtual/test_basic.rb b/test/virtual/test_basic.rb index 197a0c25..e2811c6e 100644 --- a/test/virtual/test_basic.rb +++ b/test/virtual/test_basic.rb @@ -25,10 +25,9 @@ class TestBasic < MiniTest::Test check end - def pest_name - #TODO + def test_name @string_input = 'foo ' - @output = "---RETURN_MARKER- !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER" + @output = "-Virtual::Return(:index => 5, :type => Virtual::Mystery)" check end