fix test result, but not test yet

This commit is contained in:
Torsten Ruger 2015-08-07 16:46:55 +03:00
parent 4afd7f78ca
commit ca14ef8914
6 changed files with 40 additions and 16 deletions

View File

@ -170,5 +170,17 @@ module Interpreter
# we jump back to the call instruction. so it is as if the call never happened and we continue # we jump back to the call instruction. so it is as if the call never happened and we continue
true true
end end
def execute_OperatorInstruction
case @instruction.operator
when :add
puts @instruction
else
raise "unimplemented operator #{@instruction}"
end
true
end
end end
end end

View File

@ -7,9 +7,15 @@ module Register
plus_function = Virtual::MethodSource.create_method(:Integer,:plus , [:Integer] ) plus_function = Virtual::MethodSource.create_method(:Integer,:plus , [:Integer] )
plus_function.source.return_type = Virtual::Integer plus_function.source.return_type = Virtual::Integer
plus_function.source.receiver = Virtual::Integer plus_function.source.receiver = Virtual::Integer
plus_function.source.add_code Register::OperatorInstruction.new( plus_function, :add , 0 , 0 )
tmp = Register.tmp_reg
index = Register.arg_index 1
plus_function.source.add_code Register.get_slot( plus_function , :message , index , tmp )
add = Register::OperatorInstruction.new( plus_function, :add , tmp , Register.self_reg )
plus_function.source.add_code add
return plus_function return plus_function
end end
# The conversion to base10 is quite a bit more complicated than i thought. # The conversion to base10 is quite a bit more complicated than i thought.
# The bulk of it is in div10 # The bulk of it is in div10
# We set up variables, do the devision and write the result to the string # We set up variables, do the devision and write the result to the string

View File

@ -7,7 +7,7 @@ module Register
@left = left @left = left
@right = right @right = right
end end
attr_reader :left , :right attr_reader :operator, :left , :right
def to_s def to_s
"OperatorInstruction: #{left} #{operator} #{right}" "OperatorInstruction: #{left} #{operator} #{right}"

View File

@ -126,4 +126,11 @@ module Register
end end
return register return register
end end
# when knowing the index of the argument, return the index into the message
# index passed is parfait, ie stats at 1
def self.arg_index i
last = resolve_index :message , :name
return last + i
end
end end

View File

@ -4,7 +4,7 @@ class AddTest < MiniTest::Test
def setup def setup
Virtual.machine.boot Virtual.machine.boot
code = Ast::FunctionExpression.new(:foo, [] , [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ,nil ) code =Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))
Virtual::Compiler.compile( code , Virtual.machine.space.get_main ) Virtual::Compiler.compile( code , Virtual.machine.space.get_main )
Virtual.machine.run_before "Register::CallImplementation" Virtual.machine.run_before "Register::CallImplementation"
@interpreter = Interpreter::Interpreter.new @interpreter = Interpreter::Interpreter.new
@ -44,24 +44,23 @@ class AddTest < MiniTest::Test
assert_equal Register::FunctionCall , ticks(7).class assert_equal Register::FunctionCall , ticks(7).class
assert @interpreter.link assert @interpreter.link
end end
def test_get def test_adding
done = ticks(10) done = ticks(23)
assert_equal Register::GetSlot , done.class assert_equal Register::OperatorInstruction , done.class
assert @interpreter.get_register done.register.symbol assert @interpreter.get_register done.left.symbol
puts @interpreter.get_register(done.register.symbol).class puts @interpreter.get_register(done.left.symbol).class
done = ticks(1) done = ticks(1)
assert_equal Register::GetSlot , done.class assert_equal Register::RegisterTransfer , done.class
assert @interpreter.get_register done.register.symbol assert @interpreter.get_register done.from.symbol
end end
def test_chain def test_chain
["Branch" , "LoadConstant" , "GetSlot" , "SetSlot" , "RegisterTransfer" , ["Branch" , "LoadConstant" , "GetSlot" , "SetSlot" , "RegisterTransfer" ,
"GetSlot" , "FunctionCall" , "SaveReturn" , "RegisterTransfer" , "GetSlot" , "GetSlot" , "FunctionCall" , "SaveReturn" , "LoadConstant" , "SetSlot" ,
"GetSlot" , "GetSlot" , "SetSlot" , "LoadConstant" , "SetSlot" , "GetSlot" , "GetSlot" , "SetSlot" , "LoadConstant" , "SetSlot" ,
"RegisterTransfer" , "GetSlot" , "FunctionCall" , "SaveReturn" , "RegisterTransfer" , "LoadConstant" , "SetSlot" , "RegisterTransfer" , "GetSlot" , "FunctionCall" ,
"Syscall" , "RegisterTransfer" , "RegisterTransfer" , "SetSlot" , "GetSlot" , "SaveReturn" , "GetSlot", "OperatorInstruction" , "RegisterTransfer" , "GetSlot" , "GetSlot" ,
"GetSlot" , "RegisterTransfer" ,"GetSlot" , "GetSlot","GetSlot", "GetSlot" , "FunctionReturn" ,"RegisterTransfer" , "Syscall", "NilClass"].each_with_index do |name , index|
"FunctionReturn" , "RegisterTransfer" , "Syscall" , "NilClass"].each_with_index do |name , index|
got = ticks(1) got = ticks(1)
puts got puts got
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}" assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"

View File

@ -1,2 +1,2 @@
require_relative "test_puts" require_relative "test_puts"
#require_relative "test_add" require_relative "test_add"