adds explicit load_self instruction

This commit is contained in:
Torsten Ruger 2014-07-15 18:27:13 +03:00
parent 930740e1db
commit 4d725ea1ae
5 changed files with 40 additions and 13 deletions

View File

@ -5,6 +5,8 @@ module Ast
# attr_reader :name, :args , :receiver # attr_reader :name, :args , :receiver
@@counter = 0 @@counter = 0
def compile frame , method def compile frame , method
me = receiver.compile(frame , method )
method.add Virtual::LoadSelf.new(me)
with = args.collect{|a| a.compile(frame , method)} with = args.collect{|a| a.compile(frame , method)}
frame.compile_send( method , name , with ) frame.compile_send( method , name , with )
end end

View File

@ -3,5 +3,5 @@ require 'parslet'
require "elf/object_writer" require "elf/object_writer"
require 'parser/crystal' require 'parser/crystal'
require 'parser/transform' require 'parser/transform'
require "ast/all"
require "virtual/machine" require "virtual/machine"
require "ast/all"

View File

@ -33,45 +33,59 @@ module Virtual
end end
class FrameGet < Instruction class FrameGet < Instruction
def initialize name def initialize name , nex = nil
super(nex)
@name = name @name = name
end end
attr_reader :name attr_reader :name
def attributes def attributes
[:name] [:name ] + super
end end
end end
class FrameSend < Instruction class FrameSend < Instruction
def initialize name , args = [] , nex = nil
def initialize name , args = [] super(nex)
@name = name.to_sym @name = name.to_sym
@args = args @args = args
end end
attr_reader :name , :args attr_reader :name , :args
def attributes def attributes
[:name , :args] [:name , :args ] + super
end end
end end
class FrameSet < Instruction
def initialize name , val class FrameSet < Instruction
def initialize name , val , nex = nil
super(nex)
@name = name.to_sym @name = name.to_sym
@value = val @value = val
end end
attr_reader :name , :value attr_reader :name , :value
def attributes 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
end end
class ObjectGet < Instruction class ObjectGet < Instruction
def initialize name def initialize name , nex = nil
super(nex)
@name = name @name = name
end end
attr_reader :name attr_reader :name
def attributes def attributes
[:name] [:name] + super
end end
end end
end end

View File

@ -30,7 +30,17 @@ def foo(x)
2 + 5 2 + 5
end end
HERE 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 = <<HERE
def foo()
2 + 5
end
HERE
@output = [Virtual::Method.new(:foo,[],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodEnter.new(Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil))))]
check check
end end

View File

@ -12,7 +12,8 @@ module VirtualHelper
syntax = parser.parse_with_debug(@string_input) syntax = parser.parse_with_debug(@string_input)
parts = Parser::Transform.new.apply(syntax) parts = Parser::Transform.new.apply(syntax)
machine = Virtual::Machine.new machine = Virtual::Machine.new
expressions = parts.compile(machine.frame , Virtual::Method.main ) main = Virtual::Method.main
expressions = parts.compile(machine.frame , main )
assert_equal @output , expressions assert_equal @output , expressions
end end