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
@@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

View File

@ -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"

View File

@ -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

View File

@ -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 = <<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
end

View File

@ -12,7 +12,8 @@ module VirtualHelper
syntax = parser.parse_with_debug(@string_input)
parts = Parser::Transform.new.apply(syntax)
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
end