rework interpreter test framework
remove lots of copy paste
This commit is contained in:
parent
37564d9c2e
commit
cf05e7553a
@ -2,6 +2,25 @@ require_relative "../helper"
|
||||
require "interpreter/interpreter"
|
||||
|
||||
module Ticker
|
||||
|
||||
def setup
|
||||
machine = Register.machine.boot
|
||||
syntax = Parser::Salama.new.parse_with_debug(@string_input)
|
||||
parts = Parser::Transform.new.apply(syntax)
|
||||
#puts parts.inspect
|
||||
Soml.compile( parts )
|
||||
machine.collect
|
||||
@interpreter = Interpreter::Interpreter.new
|
||||
@interpreter.start Register.machine.init
|
||||
end
|
||||
|
||||
def check_chain should
|
||||
should.each_with_index do |name , index|
|
||||
got = ticks(1)
|
||||
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
|
||||
end
|
||||
end
|
||||
|
||||
def ticks num
|
||||
last = nil
|
||||
num.times do
|
||||
@ -13,7 +32,6 @@ module Ticker
|
||||
|
||||
def show_ticks
|
||||
classes = []
|
||||
error = nil
|
||||
tick = 1
|
||||
begin
|
||||
while true and (classes.length < 200)
|
||||
|
@ -1,27 +1,25 @@
|
||||
require_relative "helper"
|
||||
|
||||
class AddTest < MiniTest::Test
|
||||
include AST::Sexp
|
||||
include Ticker
|
||||
|
||||
def setup
|
||||
machine = Register.machine.boot
|
||||
code = s(:class, :Object,
|
||||
s(:derives, nil),
|
||||
s(:statements,
|
||||
s(:function, :int,
|
||||
s(:name, :main),
|
||||
s(:parameters),
|
||||
s(:statements,
|
||||
s(:return,
|
||||
s(:operator_value, :+,
|
||||
s(:int, 5),
|
||||
s(:int, 7)))))))
|
||||
@string_input = <<HERE
|
||||
class Object
|
||||
int main()
|
||||
return 5 + 7
|
||||
end
|
||||
end
|
||||
HERE
|
||||
super
|
||||
end
|
||||
|
||||
Soml.compile( code )
|
||||
machine.collect
|
||||
@interpreter = Interpreter::Interpreter.new
|
||||
@interpreter.start Register.machine.init
|
||||
def test_chain
|
||||
#show_ticks # get output of what is
|
||||
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
|
||||
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
|
||||
"RegisterTransfer","Syscall","NilClass"]
|
||||
end
|
||||
|
||||
def test_get
|
||||
@ -58,15 +56,4 @@ class AddTest < MiniTest::Test
|
||||
result = @interpreter.get_register(done_op.left)
|
||||
assert_equal result , 12
|
||||
end
|
||||
|
||||
def test_chain
|
||||
#show_ticks # get output of what is
|
||||
["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
|
||||
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
|
||||
"RegisterTransfer","Syscall","NilClass"].each_with_index do |name , index|
|
||||
got = ticks(1)
|
||||
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,4 +1,4 @@
|
||||
require_relative "test_add"
|
||||
require_relative "test_if"
|
||||
require_relative "test_puts"
|
||||
require_relative "test_puti"
|
||||
require_relative "test_ops"
|
||||
|
@ -2,9 +2,8 @@ require_relative "helper"
|
||||
|
||||
class AddTest < MiniTest::Test
|
||||
include Ticker
|
||||
include AST::Sexp
|
||||
|
||||
def test_if
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Object
|
||||
int itest(int n)
|
||||
@ -20,16 +19,12 @@ class Object
|
||||
end
|
||||
end
|
||||
HERE
|
||||
machine = Register.machine.boot
|
||||
syntax = Parser::Salama.new.parse_with_debug(@string_input)
|
||||
parts = Parser::Transform.new.apply(syntax)
|
||||
#puts parts.inspect
|
||||
Soml.compile( parts )
|
||||
machine.collect
|
||||
@interpreter = Interpreter::Interpreter.new
|
||||
@interpreter.start Register.machine.init
|
||||
super
|
||||
end
|
||||
|
||||
def test_if
|
||||
#show_ticks # get output of what is
|
||||
["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","GetSlot",
|
||||
"GetSlot","SetSlot","LoadConstant","SetSlot","LoadConstant",
|
||||
"SetSlot","LoadConstant","SetSlot","LoadConstant","SetSlot",
|
||||
@ -41,11 +36,6 @@ HERE
|
||||
"Label","FunctionReturn","RegisterTransfer","GetSlot","GetSlot",
|
||||
"Branch","Label","Label","FunctionReturn","RegisterTransfer",
|
||||
"GetSlot","GetSlot","Label","FunctionReturn","RegisterTransfer",
|
||||
"Syscall","NilClass"].each_with_index do |name , index|
|
||||
got = ticks(1)
|
||||
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
|
||||
end
|
||||
#puts @interpreter.block.inspect
|
||||
|
||||
"Syscall","NilClass"]
|
||||
end
|
||||
end
|
||||
|
42
test/interpreter/test_ops.rb
Normal file
42
test/interpreter/test_ops.rb
Normal file
@ -0,0 +1,42 @@
|
||||
require_relative "helper"
|
||||
|
||||
class AddTest < MiniTest::Test
|
||||
include Ticker
|
||||
include AST::Sexp
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Object
|
||||
int main()
|
||||
return 5 + 10
|
||||
end
|
||||
end
|
||||
HERE
|
||||
super
|
||||
end
|
||||
|
||||
def test_add
|
||||
#show_ticks # get output of what is
|
||||
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
|
||||
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
|
||||
"RegisterTransfer","Syscall","NilClass"]
|
||||
end
|
||||
|
||||
def test_mult
|
||||
@string_input = <<HERE
|
||||
class Object
|
||||
int main()
|
||||
return 5 * 10
|
||||
end
|
||||
end
|
||||
HERE
|
||||
setup
|
||||
#show_ticks # get output of what is
|
||||
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
|
||||
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
|
||||
"RegisterTransfer","Syscall","NilClass"]
|
||||
end
|
||||
|
||||
end
|
@ -1,27 +1,29 @@
|
||||
require_relative "helper"
|
||||
|
||||
class TestPuts < MiniTest::Test
|
||||
include AST::Sexp
|
||||
include Ticker
|
||||
def setup
|
||||
machine = Register.machine.boot
|
||||
code = s(:class, :Object,
|
||||
s(:derives, nil),
|
||||
s(:statements,
|
||||
s(:function, :int,
|
||||
s(:name, :main),
|
||||
s(:parameters),
|
||||
s(:statements,
|
||||
s(:call,
|
||||
s(:name, :putstring),
|
||||
s(:arguments),
|
||||
s(:receiver,
|
||||
s(:string, "Hello again")))))))
|
||||
|
||||
Soml.compile( code )
|
||||
machine.collect
|
||||
@interpreter = Interpreter::Interpreter.new
|
||||
@interpreter.start Register.machine.init
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Object
|
||||
int main()
|
||||
"Hello again".putstring()
|
||||
end
|
||||
end
|
||||
HERE
|
||||
super
|
||||
end
|
||||
|
||||
def test_chain
|
||||
#show_ticks # get output of what is
|
||||
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","GetSlot",
|
||||
"LoadConstant","SetSlot","LoadConstant","SetSlot","LoadConstant",
|
||||
"SetSlot","LoadConstant","SetSlot","RegisterTransfer","FunctionCall",
|
||||
"Label","GetSlot","RegisterTransfer","Syscall","RegisterTransfer",
|
||||
"RegisterTransfer","SetSlot","Label","FunctionReturn","RegisterTransfer",
|
||||
"GetSlot","GetSlot","Label","FunctionReturn","RegisterTransfer",
|
||||
"Syscall","NilClass"]
|
||||
end
|
||||
|
||||
def test_branch
|
||||
@ -44,22 +46,6 @@ class TestPuts < MiniTest::Test
|
||||
assert_equal Register::FunctionCall , ticks(8).class
|
||||
end
|
||||
|
||||
def test_chain
|
||||
#show_ticks # get output of what is
|
||||
["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","GetSlot",
|
||||
"LoadConstant","SetSlot","LoadConstant","SetSlot","LoadConstant",
|
||||
"SetSlot","LoadConstant","SetSlot","RegisterTransfer","FunctionCall",
|
||||
"Label","GetSlot","RegisterTransfer","Syscall","RegisterTransfer",
|
||||
"RegisterTransfer","SetSlot","Label","FunctionReturn","RegisterTransfer",
|
||||
"GetSlot","GetSlot","Label","FunctionReturn","RegisterTransfer",
|
||||
"Syscall","NilClass"].each_with_index do |name , index|
|
||||
got = ticks(1)
|
||||
#puts "TICK #{index}"
|
||||
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
|
||||
end
|
||||
end
|
||||
|
||||
def test_putstring
|
||||
done = ticks(24)
|
||||
assert_equal Register::Syscall , done.class
|
||||
|
Loading…
Reference in New Issue
Block a user