more work on send, remove yaml hack in test

This commit is contained in:
Torsten Ruger 2014-08-21 22:57:20 +03:00
parent 2260c680b2
commit 5b1e86da49
8 changed files with 49 additions and 23 deletions

View File

@ -8,11 +8,13 @@ module Ast
me = receiver.compile( method, message )
method.add_code Virtual::Set.new(Virtual::NewSelf.new(me.type), me)
method.add_code Virtual::Set.new(Virtual::NewName.new(), name)
compiled_args = []
args.each_with_index do |arg , i|
val = arg.compile( method, message) #compile in the running method, ie before passing control
compiled_args << val
method.add_code Virtual::Set.new(Virtual::NewMessageSlot.new(i ,val.type ) , val )
end
method.add_code Virtual::MessageSend.new(name) #and pass control
method.add_code Virtual::MessageSend.new(name , me , compiled_args) #and pass control
Virtual::Return.new( method.return_type )
end

View File

@ -6,7 +6,10 @@ module Trickle
def run block
block.codes.dup.each do |code|
next unless code.is_a? MessageSend
if( code.me.type == Virtual::Reference)
next
end
end
end
end

View File

@ -12,6 +12,9 @@ module Virtual
# another abstract "marker" class (so we can check for it)
# derived classes are Boot/Meta Class and StringConstant
class ObjectConstant < Constant
def type
Virtual::Reference
end
end
class IntegerConstant < Constant
@ -34,9 +37,6 @@ module Virtual
@string = str
end
attr_reader :string
def type
Virtual::Reference
end
def result= value
class_for(MoveInstruction).new(value , self , :opcode => :mov)
end

View File

@ -67,11 +67,12 @@ module Virtual
end
class MessageSend < Instruction
def initialize name , args = []
def initialize name , me , args = []
@name = name.to_sym
@me = me
@args = args
end
attr_reader :name , :args
attr_reader :name , :me , :args
end
# class for Set instructions, A set is basically a mem move.

34
test/virtual/hello.rb Normal file
View File

@ -0,0 +1,34 @@
require_relative "virtual_helper"
class HelloTest < MiniTest::Test
include VirtualHelper
def check
machine = Virtual::Machine.boot
expressions = machine.compile_main @string_input
puts ""
puts Sof::Writer.write(expressions)
Virtual::Object.space.run_passes
puts ""
puts Sof::Writer.write(expressions)
end
def test_simplest_function
@string_input = <<HERE
def foo(x)
5
end
HERE
check
end
def test_puts_string
@string_input = <<HERE
def foo()
puts("Hello")
end
foo()
HERE
check
end
end

View File

@ -20,7 +20,7 @@ def foo()
end
foo()
HERE
@output = "---RETURN_MARKER- &2 !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args: []RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfRETURN_MARKER index: 1RETURN_MARKER type: &1 !ruby/class 'Virtual::Mystery'RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER index: 0RETURN_MARKER type: *1RETURN_MARKER blocks:RETURN_MARKER - &3 !ruby/object:Virtual::BlockRETURN_MARKER method: *2RETURN_MARKER name: :fooRETURN_MARKER branch: RETURN_MARKER codes:RETURN_MARKER - !ruby/object:Virtual::MethodEnter {}RETURN_MARKER - !ruby/object:Virtual::SetRETURN_MARKER to: !ruby/object:Virtual::NewSelfRETURN_MARKER index: 1RETURN_MARKER type: *1RETURN_MARKER from: !ruby/object:Virtual::SelfRETURN_MARKER index: 1RETURN_MARKER type: *1RETURN_MARKER - !ruby/object:Virtual::SetRETURN_MARKER to: !ruby/object:Virtual::NewNameRETURN_MARKER index: 3RETURN_MARKER type: *1RETURN_MARKER from: :putsRETURN_MARKER - !ruby/object:Virtual::SetRETURN_MARKER to: !ruby/object:Virtual::NewMessageSlotRETURN_MARKER index: 4RETURN_MARKER type: !ruby/class 'Virtual::Reference'RETURN_MARKER from: !ruby/object:Virtual::StringConstantRETURN_MARKER string: HelloRETURN_MARKER - !ruby/object:Virtual::MessageSendRETURN_MARKER name: :putsRETURN_MARKER args: []RETURN_MARKER - !ruby/object:Virtual::BlockRETURN_MARKER method: *2RETURN_MARKER name: :foo_returnRETURN_MARKER branch: RETURN_MARKER codes:RETURN_MARKER - !ruby/object:Virtual::MethodReturn {}RETURN_MARKER current: *3RETURN_MARKER- !ruby/object:Virtual::ReturnRETURN_MARKER index: 0RETURN_MARKER type: *1RETURN_MARKER"
@output = "---RETURN_MARKER- &2 !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args: []RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfRETURN_MARKER index: 1RETURN_MARKER type: &1 !ruby/class 'Virtual::Mystery'RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER index: 0RETURN_MARKER type: *1RETURN_MARKER blocks:RETURN_MARKER - &5 !ruby/object:Virtual::BlockRETURN_MARKER method: *2RETURN_MARKER name: :fooRETURN_MARKER branch: RETURN_MARKER codes:RETURN_MARKER - !ruby/object:Virtual::MethodEnter {}RETURN_MARKER - !ruby/object:Virtual::SetRETURN_MARKER to: !ruby/object:Virtual::NewSelfRETURN_MARKER index: 1RETURN_MARKER type: *1RETURN_MARKER from: &3 !ruby/object:Virtual::SelfRETURN_MARKER index: 1RETURN_MARKER type: *1RETURN_MARKER - !ruby/object:Virtual::SetRETURN_MARKER to: !ruby/object:Virtual::NewNameRETURN_MARKER index: 3RETURN_MARKER type: *1RETURN_MARKER from: :putsRETURN_MARKER - !ruby/object:Virtual::SetRETURN_MARKER to: !ruby/object:Virtual::NewMessageSlotRETURN_MARKER index: 4RETURN_MARKER type: !ruby/class 'Virtual::Reference'RETURN_MARKER from: &4 !ruby/object:Virtual::StringConstantRETURN_MARKER string: HelloRETURN_MARKER - !ruby/object:Virtual::MessageSendRETURN_MARKER name: :putsRETURN_MARKER me: *3RETURN_MARKER args:RETURN_MARKER - *4RETURN_MARKER - !ruby/object:Virtual::BlockRETURN_MARKER method: *2RETURN_MARKER name: :foo_returnRETURN_MARKER branch: RETURN_MARKER codes:RETURN_MARKER - !ruby/object:Virtual::MethodReturn {}RETURN_MARKER current: *5RETURN_MARKER- !ruby/object:Virtual::ReturnRETURN_MARKER index: 0RETURN_MARKER type: *1RETURN_MARKER"
check
end

View File

@ -1,5 +1,6 @@
require_relative '../helper'
require 'parslet/convenience'
require "yaml"
module VirtualHelper
# need a code generator, for arm
@ -17,18 +18,3 @@ module VirtualHelper
end
end
require "yaml" # not my first choice, but easy with graphs
# for readability of the yaml output :next of instructions last
Psych::Visitors::YAMLTree.class_eval do
private
def dump_ivars target
ivars = find_ivars target
ivars << :@next if ivars.delete(:@next)
ivars.each do |iv|
@emitter.scalar("#{iv.to_s.sub(/^@/, '')}", nil, nil, true, false, Psych::Nodes::Scalar::ANY)
accept target.instance_variable_get(iv)
end
end
end