From 4d725ea1ae403f52e58ed1e5445e05e20e94fd3d Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 15 Jul 2014 18:27:13 +0300 Subject: [PATCH] adds explicit load_self instruction --- lib/ast/call_site_expression.rb | 2 ++ lib/crystal.rb | 2 +- lib/virtual/instruction.rb | 34 +++++++++++++++++++++++---------- test/virtual/test_methods.rb | 12 +++++++++++- test/virtual/virtual_helper.rb | 3 ++- 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/lib/ast/call_site_expression.rb b/lib/ast/call_site_expression.rb index 001ce17c..c33179e7 100644 --- a/lib/ast/call_site_expression.rb +++ b/lib/ast/call_site_expression.rb @@ -5,6 +5,8 @@ module Ast # attr_reader :name, :args , :receiver @@counter = 0 def compile frame , method + me = receiver.compile(frame , method ) + method.add Virtual::LoadSelf.new(me) with = args.collect{|a| a.compile(frame , method)} frame.compile_send( method , name , with ) end diff --git a/lib/crystal.rb b/lib/crystal.rb index 8bf603e5..1fcf049f 100644 --- a/lib/crystal.rb +++ b/lib/crystal.rb @@ -3,5 +3,5 @@ require 'parslet' require "elf/object_writer" require 'parser/crystal' require 'parser/transform' -require "ast/all" require "virtual/machine" +require "ast/all" diff --git a/lib/virtual/instruction.rb b/lib/virtual/instruction.rb index e4323ec7..3854f5ff 100644 --- a/lib/virtual/instruction.rb +++ b/lib/virtual/instruction.rb @@ -33,45 +33,59 @@ module Virtual end class FrameGet < Instruction - def initialize name + def initialize name , nex = nil + super(nex) @name = name end attr_reader :name def attributes - [:name] + [:name ] + super end end class FrameSend < Instruction - - def initialize name , args = [] + def initialize name , args = [] , nex = nil + super(nex) @name = name.to_sym @args = args end attr_reader :name , :args def attributes - [:name , :args] + [:name , :args ] + super end end - class FrameSet < Instruction - def initialize name , val + class FrameSet < Instruction + def initialize name , val , nex = nil + super(nex) @name = name.to_sym @value = val end attr_reader :name , :value def attributes - [:name , :value] + [:name , :value] + super + end + end + + class LoadSelf < Instruction + def initialize val , nex = nil + super(nex) + @value = val + end + attr_reader :value + def attributes + [:value] + super end end class ObjectGet < Instruction - def initialize name + def initialize name , nex = nil + super(nex) @name = name end attr_reader :name def attributes - [:name] + [:name] + super end end end diff --git a/test/virtual/test_methods.rb b/test/virtual/test_methods.rb index cda99ea2..7ebab300 100644 --- a/test/virtual/test_methods.rb +++ b/test/virtual/test_methods.rb @@ -30,7 +30,17 @@ def foo(x) 2 + 5 end HERE - @output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(nil),Virtual::Return.new( Virtual::Mystery),Virtual::MethodEnter.new(Virtual::FrameSet.new(:abba,Virtual::IntegerConstant.new(5))))] + @output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodEnter.new(Virtual::FrameSet.new(:abba,Virtual::IntegerConstant.new(5),Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil)))))] + check + end + + def test_function_ops_simple + @string_input = <