stash old vm

moving on to getting mom to work and can’t have both
interpreter and elf broke, about 100 tests  went
This commit is contained in:
Torsten Ruger
2018-03-11 17:02:42 +05:30
parent f7aac1d1a4
commit 5fe0ba06ab
56 changed files with 80 additions and 143 deletions

View File

@ -12,7 +12,7 @@ class HelloTest < MiniTest::Test
writer.save "test/hello.o"
end
def test_string_put
def pest_string_put
@input = s(:statements, s(:return, s(:call, :putstring, s(:arguments),
s(:receiver, s(:string, "Hello again\\n")))))
check

View File

@ -9,7 +9,7 @@ module Risc
def setup
Risc.machine.boot
do_clean_compile
Vm.compile_ast( @input )
#FIXME Vm.compile_ast( @input )
Collector.collect_space
@interpreter = Interpreter.new
@interpreter.start Risc.machine.init

View File

@ -16,7 +16,7 @@ HERE
super
end
def test_chain
def pest_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
@ -25,18 +25,18 @@ HERE
NilClass]
end
def test_get
def pest_get
assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r2 )
assert Integer , @interpreter.get_register( :r2 ).class
end
def test_transfer
def pest_transfer
transfer = ticks 19
assert_equal RiscTransfer , transfer.class
assert_equal @interpreter.get_register(transfer.to) , @interpreter.get_register(transfer.from)
end
def test_call
def pest_call
ret = ticks(18)
assert_equal FunctionReturn , ret.class
@ -45,7 +45,7 @@ HERE
assert_equal Label , link.class
end
def test_adding
def pest_adding
done_op = ticks(12)
assert_equal OperatorInstruction , done_op.class
left = @interpreter.get_register(done_op.left)

View File

@ -19,7 +19,7 @@ HERE
super
end
def test_chain
def pest_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
@ -33,31 +33,31 @@ HERE
Label, FunctionReturn, RiscTransfer, Syscall, NilClass]
end
def test_branch
def pest_branch
was = @interpreter.instruction
assert_equal Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
def pest_load
assert_equal LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
def pest_get
assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
def pest_call
assert_equal FunctionCall , ticks(8).class
end
def test_exit
def pest_exit
done = ticks(50)
assert_equal NilClass , done.class
end
def test_reg_to_byte
def pest_reg_to_byte
done = ticks(37)
assert_equal RegToByte , done.class
assert_equal "h".ord , @interpreter.get_register(done.register)

View File

@ -32,7 +32,7 @@ HERE
s(:true_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "then")))),
s(:false_statements, s(:call, :putstring , s(:arguments), s(:receiver, s(:string, "else"))))))
end
def test_if
def pest_if
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,

View File

@ -19,7 +19,7 @@ module Risc
@instruction_events << was
end
def test_state_change
def pest_state_change
@interpreter.register_event :state_changed , self
ticks 30
assert @state_events[:state_changed]
@ -28,14 +28,14 @@ module Risc
@interpreter.unregister_event :state_changed , self
end
def test_instruction_events
def pest_instruction_events
@interpreter.register_event :instruction_changed , self
ticks 30
assert_equal 20 , @instruction_events.length
@interpreter.unregister_event :instruction_changed , self
end
def test_chain
def pest_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,

View File

@ -17,7 +17,7 @@ HERE
super
end
def test_mult
def pest_mult
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
@ -26,12 +26,12 @@ HERE
NilClass]
check_return 0
end
def test_overflow
def pest_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_zero
def pest_zero
ticks( 12 )
assert @interpreter.flags[:zero]
end

View File

@ -16,7 +16,7 @@ HERE
super
end
def test_add
def pest_add
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,
@ -26,12 +26,12 @@ HERE
check_return 0
end
def test_overflow
def pest_overflow
ticks( 12 )
assert @interpreter.flags[:overflow]
end
def test_zero
def pest_zero
ticks( 12 )
assert @interpreter.flags[:zero]
end

View File

@ -16,7 +16,7 @@ HERE
super
end
def test_chain
def pest_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
@ -29,40 +29,40 @@ HERE
Label, FunctionReturn, RiscTransfer, Syscall, NilClass]
end
def test_branch
def pest_branch
was = @interpreter.instruction
assert_equal Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
def pest_load
assert_equal LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
def pest_get
assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
def pest_call
assert_equal FunctionCall , ticks(8).class
end
def test_putstring
def pest_putstring
done = ticks(29)
assert_equal Syscall , done.class
assert_equal "Hello again" , @interpreter.stdout
end
def test_return
def pest_return
done = ticks(34)
assert_equal FunctionReturn , done.class
assert Label , @interpreter.instruction.class
assert @interpreter.instruction.is_a?(Instruction) , "not instruction #{@interpreter.instruction}"
end
def test_exit
def pest_exit
done = ticks(45)
assert_equal NilClass , done.class
assert_equal "Hello again" , @interpreter.stdout

View File

@ -19,7 +19,7 @@ HERE
super
end
def test_chain
def pest_chain
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, SlotToReg,
@ -33,31 +33,31 @@ HERE
NilClass]
end
def test_branch
def pest_branch
was = @interpreter.instruction
assert_equal Branch , ticks(1).class
assert was != @interpreter.instruction
assert @interpreter.instruction , "should have gone to next instruction"
end
def test_load
def pest_load
assert_equal LoadConstant , ticks(3).class
assert_equal Parfait::Space , @interpreter.get_register(:r2).class
assert_equal :r2, @interpreter.instruction.array.symbol
end
def test_get
def pest_get
assert_equal SlotToReg , ticks(4).class
assert @interpreter.get_register( :r1 )
assert Integer , @interpreter.get_register( :r1 ).class
end
def test_call
def pest_call
assert_equal FunctionCall , ticks(8).class
end
def test_exit
def pest_exit
done = ticks(46)
assert_equal NilClass , done.class
end
def test_byte_to_reg
def pest_byte_to_reg
done = ticks(32)
assert_equal ByteToReg , done.class
assert_equal "H".ord , @interpreter.get_register(done.register)

View File

@ -24,7 +24,7 @@ HERE
super
end
def test_if
def pest_if
#show_ticks # get output of what is
check_chain [Branch, Label, LoadConstant, SlotToReg, RegToSlot,
LoadConstant, RegToSlot, FunctionCall, Label, LoadConstant,

View File

@ -1,3 +0,0 @@
require_relative "../helper"
Risc.machine.boot unless Risc.machine.booted

View File

@ -1,82 +0,0 @@
require_relative '../helper'
module Risc
module SpaceHack
# test hack to in place change object type
def add_space_field(name,type)
class_type = Parfait.object_space.get_class_by_name(:Space).instance_type
class_type.send(:private_add_instance_variable, name , type)
end
end
module ExpressionHelper
include SpaceHack
def check
Risc.machine.boot unless Risc.machine.booted
compiler = Vm::MethodCompiler.new Parfait.object_space.get_main
code = Vm.ast_to_code @input
assert code.to_s , @input
produced = compiler.process( code )
assert @output , "No output given"
assert_equal produced.class , @output , "Wrong class"
produced
end
end
module Statements
include AST::Sexp
include CleanCompile
include SpaceHack
def setup
Risc.machine.boot # force boot to reset main
end
def preamble
[Label, SlotToReg , LoadConstant, RegToSlot, LoadConstant,RegToSlot, LoadConstant, SlotToReg, SlotToReg ]
end
def postamble
[ Label, FunctionReturn]
end
def check_nil
assert @expect , "No output given"
compiler = Vm::MethodCompiler.new(:main)
code = Vm.ast_to_code( @input )
assert code.to_s , @input
produced = compiler.process( code )
produced = Parfait.object_space.get_main.instructions
compare_instructions produced , @expect
end
def check_return
was = check_nil
raise was if was
Parfait.object_space.get_main.instructions
end
def compare_instructions( instruction , expect )
index = 0
all = instruction.to_arr
full_expect = preamble + expect + postamble
full_expect = expect
begin
should = full_expect[index]
return "No instruction at #{index}" unless should
return "Expected at #{index+1}\n#{should(all)}" unless instruction.class == should
index += 1
instruction = instruction.next
end while( instruction )
nil
end
def should( all )
#preamble.each {all.shift}
#postamble.each {all.pop}
str = all.to_s.gsub("Risc::","")
ret = ""
str.split(",").each_slice(6).each do |line|
ret += " " + line.join(",") + " ,\n"
end
ret
end
end
end

View File

@ -1,112 +0,0 @@
require_relative 'helper'
module Risc
class TestAssignStatement < MiniTest::Test
include Statements
def test_assign_op
Parfait.object_space.get_main.add_local(:r , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:operator_value, :+, s(:int, 10), s(:int, 1))))
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, SlotToReg, RegToSlot ,
LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_assign_ivar_notpresent
@input =s(:statements, s(:i_assignment, s(:ivar, :r), s(:int, 5)))
@expect = []
assert_raises{ check_nil }
end
def test_assign_ivar
add_space_field(:r , :Integer)
@input =s(:statements, s(:i_assignment, s(:ivar, :r), s(:int, 5)))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_assign_local_assign
Parfait.object_space.get_main.add_local(:r , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:int, 5)))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_assign_call
Parfait.object_space.get_main.add_local(:r , :Object)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:call, :main, s(:arguments))))
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer ,
FunctionCall, Label, RiscTransfer, SlotToReg, SlotToReg, SlotToReg ,
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_named_list_get
Parfait.object_space.get_main.add_local(:r , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:int, 5)), s(:return, s(:local, :r)))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg ,
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
was = check_return
get = was.next(5)
assert_equal SlotToReg , get.class
assert_equal 1 + 1, get.index , "Get to named_list index must be offset, not #{get.index}"
end
def test_assign_local_int
Parfait.object_space.get_main.add_local(:r , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:int, 5)) )
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
was = check_return
set = was.next(3)
assert_equal RegToSlot , set.class
assert_equal 1 + 1, set.index , "Set to named_list index must be offset, not #{set.index}"
end
def test_misassign_local
Parfait.object_space.get_main.add_local(:r , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :r), s(:string, "5")) )
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_raises {check }
end
def test_assign_arg
Parfait.object_space.get_main.add_argument(:blar , :Integer)
@input = s(:statements, s(:a_assignment, s(:arg, :blar), s(:int, 5)))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
was = check_return
set = was.next(3)
assert_equal RegToSlot , set.class
assert_equal 1 + 1, set.index , "Set to args index must be offset, not #{set.index}"
end
def test_misassign_arg
Parfait.object_space.get_main.add_argument(:blar , :Integer)
@input = s(:statements, s(:a_assignment, s(:arg, :blar), s(:string, "5")))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_raises {check }
end
def test_arg_get
# have to define bar externally, just because redefining main. Otherwise that would be automatic
Parfait.object_space.get_main.add_argument(:balr , :Integer)
@input = s(:statements, s(:return, s(:arg, :balr)))
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
was = check_return
get = was.next(2)
assert_equal SlotToReg , get.class
assert_equal 1 + 1, get.index , "Get to args index must be offset, not #{get.index}"
end
end
end

View File

@ -1,41 +0,0 @@
require_relative "helper"
module Risc
class TestBasic < MiniTest::Test
include ExpressionHelper
include AST::Sexp
def setup
Risc.machine.boot
@output = Risc::RiscValue
end
def test_number
@input = s(:int , 42)
assert_equal 42 , check.value
end
def test_true
@input = s(:true)
check
end
def test_false
@input = s(:false)
check
end
def test_nil
@input = s(:nil)
check
end
def test_self
@input = s(:known, :self)
check
end
def test_string
@input = s(:string , "hello")
check
end
end
end

View File

@ -1,43 +0,0 @@
require_relative "helper"
module Risc
class TestCall < MiniTest::Test
include ExpressionHelper
include AST::Sexp
def setup
Risc.machine.boot
@output = Risc::RiscValue
end
def test_call_main_plain
@input = s(:call , :main ,s(:arguments))
check
end
def test_call_main_int
Parfait.object_space.get_main.add_argument(:blar , :Integer)
@input =s(:call, :main ,s(:arguments , s(:int, 1)))
check
end
def test_call_main_string
Parfait.object_space.get_main.add_argument(:blar , :Word)
@input =s(:call, :main ,s(:arguments , s(:string, "1") ))
check
end
def test_call_main_op
Parfait.object_space.get_main.add_local(:bar , :Integer)
Parfait.object_space.get_main.add_argument(:blar , :Integer)
@input =s(:call, :main ,s(:arguments , s(:local, :bar) ))
check
end
def test_call_string_put
@input = s(:call, :putstring,s(:arguments),s(:receiver,s(:string, "Hello Raisa, I am rubyx")))
check
end
end
end

View File

@ -1,68 +0,0 @@
require_relative 'helper'
require_relative "test_call_expression"
module Risc
class TestCallStatement < MiniTest::Test
include Statements
def test_call_constant_int
clean_compile :Integer, :puti, {}, s(:statements, s(:return, s(:int, 1)))
@input = s(:call, :puti , s(:arguments), s(:receiver, s(:int, 42)))
@expect = [Label, SlotToReg, LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant ,
SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer, FunctionCall, Label ,
RiscTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot, Label ,
FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_call_constant_string
clean_compile :Word, :putstr,{}, s(:statements, s(:return, s(:int, 1)))
@input =s(:call, :putstr, s(:arguments), s(:receiver, s(:string, "Hello")))
@expect = [Label, SlotToReg, LoadConstant, RegToSlot, LoadConstant, RegToSlot, LoadConstant ,
SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer, FunctionCall, Label ,
RiscTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot, Label ,
FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_call_local_int
Parfait.object_space.get_main.add_local(:testi , :Integer)
clean_compile :Integer, :putint, {}, s(:statements, s(:return, s(:int, 1)))
@input = s(:statements, s(:l_assignment, s(:local, :testi), s(:int, 20)), s(:call, :putint, s(:arguments), s(:receiver, s(:local, :testi))))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg, SlotToReg ,
RegToSlot, LoadConstant, RegToSlot, LoadConstant, SlotToReg, RegToSlot, LoadConstant ,
RegToSlot, RiscTransfer, FunctionCall, Label, RiscTransfer, SlotToReg, SlotToReg ,
LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_call_local_class
Parfait.object_space.get_main.add_local(:test_l , :List)
clean_compile :List, :add, {}, s(:statements, s(:return, s(:int, 1)))
@input =s(:statements, s(:call, :add, s(:arguments), s(:receiver, s(:local, :test_l))))
@expect = [Label, SlotToReg, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer, FunctionCall ,
Label, RiscTransfer, SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot ,
Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_call_puts
clean_compile :Space, :putstr, {str: :Word}, s(:statements, s(:return, s(:arg, :str)))
@input =s(:call, :putstr , s(:arguments, s(:string, "Hello") ) )
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot ,
LoadConstant, RegToSlot, RiscTransfer, FunctionCall, Label, RiscTransfer ,
SlotToReg, SlotToReg, LoadConstant, SlotToReg, RegToSlot, Label ,
FunctionReturn]
was = check_return
set = was.next(8)
assert_equal RegToSlot , set.class
assert_equal 1, set.index , "Set to message must be offset, not #{set.index}"
end
end
end

View File

@ -1,34 +0,0 @@
require_relative "helper"
module Risc
class TestFields < MiniTest::Test
include ExpressionHelper
include AST::Sexp
def setup
Risc.machine.boot
end
def test_field_not_defined
@root = :field_access
@input = s(:field_access, s(:receiver, s(:known, :self)), s(:field, s(:ivar, :a)))
assert_raises(RuntimeError) { check }
end
def test_field_not_space
@root = :field_access
@input = s(:field_access, s(:receiver, s(:known, :self)), s(:field, s(:ivar, :space)))
assert_raises(RuntimeError) { check }
end
def test_field
add_space_field(:bro,:Object)
@root = :field_access
@input = s(:field_access,s(:receiver, s(:known, :self)),s(:field,s(:ivar, :bro)))
@output = Risc::RiscValue
check
end
end
end

View File

@ -1,42 +0,0 @@
require_relative 'helper'
module Risc
class TestFieldStatement < MiniTest::Test
include Statements
def test_field_named_list
Parfait.object_space.get_main.add_local( :m , :Message)
@input = s(:statements, s(:return, s(:field_access,
s(:receiver, s(:local, :m)), s(:field, s(:ivar, :name)))))
@expect = [Label, SlotToReg, SlotToReg, SlotToReg, RegToSlot, LoadConstant ,
SlotToReg, RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_field_arg
Parfait.object_space.get_main.add_local( :m , :Message)
clean_compile :Space, :get_name, { :main => :Message},
s(:statements, s(:return, s(:field_access,
s(:receiver, s(:arg, :main)), s(:field, s(:ivar, :name)))))
@input =s(:statements, s(:return, s(:call, :get_name, s(:arguments, s(:local, :m)))))
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg, SlotToReg ,
RegToSlot, LoadConstant, RegToSlot, RiscTransfer, FunctionCall, Label ,
RiscTransfer, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_message_field
Parfait.object_space.get_main.add_local(:name , :Word)
@input = s(:statements, s(:l_assignment, s(:local, :name), s(:field_access, s(:receiver, s(:known, :message)), s(:field, s(:ivar, :name)))), s(:return, s(:local, :name)))
@expect = [Label, RiscTransfer, SlotToReg, SlotToReg, RegToSlot, SlotToReg ,
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label ,
FunctionReturn]
assert_nil msg = check_nil , msg
end
end
end

View File

@ -1,36 +0,0 @@
require_relative 'helper'
module Risc
class TestIfStatement < MiniTest::Test
include Statements
def test_if_basicr
@input = s(:statements, s(:if_statement, :plus, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))), s(:true_statements, s(:return, s(:int, 3))), s(:false_statements, s(:return, s(:int, 4)))))
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsPlus, LoadConstant ,
RegToSlot, Branch, Label, LoadConstant, RegToSlot, Label ,
LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_if_small_minus
@input = s(:statements, s(:if_statement, :minus, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))), s(:true_statements, s(:return, s(:int, 3))), s(:false_statements, nil)))
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsMinus, Branch ,
Label, LoadConstant, RegToSlot, Label, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_if_small_zero
@input = s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:int, 10), s(:int, 12))), s(:true_statements, s(:return, s(:int, 3))), s(:false_statements, nil)))
@expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, IsZero, Branch ,
Label, LoadConstant, RegToSlot, Label, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
end
end

View File

@ -1,34 +0,0 @@
require_relative "helper"
module Risc
class TestFields < MiniTest::Test
include ExpressionHelper
include AST::Sexp
def setup
Risc.machine.boot
end
def test_local
Parfait.object_space.get_main.add_local(:bar , :Integer)
@input = s(:local, :bar)
@output = Risc::RiscValue
check
end
def test_space
@root = :name
@input = s(:known, :space)
@output = Risc::RiscValue
check
end
def test_args
Parfait.object_space.get_main.add_argument(:bar , :Integer)
@input = s(:arg, :bar)
@output = Risc::RiscValue
check
end
end
end

View File

@ -1,46 +0,0 @@
require_relative "helper"
module Risc
class TestOps < MiniTest::Test
include ExpressionHelper
include AST::Sexp
def setup
Risc.machine.boot
@root = :operator_value
@output = Risc::RiscValue
end
def operators
[:+ , :- , :* , :/ , :== ]
end
def test_ints
operators.each do |op|
@input = s(:operator_value, op , s(:int, 2), s(:int, 3))
check
end
end
def test_local_int
Parfait.object_space.get_main.add_local(:bar , :Integer)
@input = s(:operator_value, :+, s(:local, :bar), s(:int, 3))
check
end
def test_int_local
Parfait.object_space.get_main.add_local(:bar , :Integer)
@input = s(:operator_value, :+, s(:int, 3), s(:local, :bar))
check
end
def test_field_int
add_space_field(:bro,:Integer)
@input = s(:operator_value, :+, s(:field_access,s(:receiver, s(:known, :self)), s(:field, s(:ivar, :bro))), s(:int, 3))
check
end
def test_int_field
add_space_field(:bro,:Integer)
@input = s(:operator_value, :+, s(:int, 3), s(:field_access, s(:receiver, s(:known, :self)), s(:field,s(:ivar, :bro))))
check
end
end
end

View File

@ -1,47 +0,0 @@
require_relative 'helper'
module Risc
class TestReturnStatement < MiniTest::Test
include Statements
def test_return_int
@input = s(:statements, s(:return, s(:int, 5)))
@expect = [Label, LoadConstant, RegToSlot, LoadConstant, SlotToReg, RegToSlot ,
Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_return_local
Parfait.object_space.get_main.add_local(:runner , :Integer)
@input = s(:statements, s(:return, s(:local , :runner)))
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg ,
RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_return_local_assign
Parfait.object_space.get_main.add_local(:runner , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :runner), s(:int, 5)), s(:return, s(:local, :runner)))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, SlotToReg, SlotToReg ,
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_return_call
@input =s(:statements, s(:return, s(:call, :main, s(:arguments))))
@expect = [Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant, RegToSlot ,
LoadConstant, SlotToReg, RegToSlot, LoadConstant, RegToSlot, RiscTransfer ,
FunctionCall, Label, RiscTransfer, SlotToReg, SlotToReg, RegToSlot ,
LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def pest_return_space_length # need to add runtime first
Parfait.object_space.get_main.add_local(:l , :Type)
@input = s(:statements, s(:l_assignment, s(:local, :l), s(:call, :get_type, s(:arguments), s(:receiver, s(:known, :space)))), s(:return, s(:field_access, s(:receiver, s(:known, :self)), s(:field, s(:ivar, :runner)))))
@expect = [Label, SlotToReg,SlotToReg ,RegToSlot,Label,FunctionReturn]
assert_nil msg = check_nil , msg
end
end
end

View File

@ -1,49 +0,0 @@
require_relative 'helper'
module Risc
class TestWhile < MiniTest::Test
include Statements
def test_while_mini
@input = s(:statements, s(:while_statement, :plus, s(:conditional, s(:int, 1)), s(:statements, s(:return, s(:int, 3)))))
@expect = [Label, Branch, Label, LoadConstant, RegToSlot, Label ,
LoadConstant, IsPlus, LoadConstant, SlotToReg, RegToSlot, Label ,
FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_while_assign
Parfait.object_space.get_main.add_local(:n , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :n), s(:int, 5)),
s(:while_statement, :plus, s(:conditional, s(:local, :n)),
s(:statements, s(:l_assignment, s(:local, :n),
s(:operator_value, :-, s(:local, :n), s(:int, 1))))),
s(:return, s(:local, :n)))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Branch, Label ,
SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, SlotToReg, RegToSlot ,
Label, SlotToReg, SlotToReg, IsPlus, SlotToReg, SlotToReg ,
RegToSlot, LoadConstant, SlotToReg, RegToSlot, Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
def test_while_return
Parfait.object_space.get_main.add_local(:n , :Integer)
@input = s(:statements, s(:l_assignment, s(:local, :n), s(:int, 10)), s(:while_statement, :plus, s(:conditional, s(:operator_value, :-, s(:local, :n), s(:int, 5))),
s(:statements, s(:l_assignment, s(:local, :n), s(:operator_value, :+, s(:local, :n), s(:int, 1))),
s(:return, s(:local, :n)))))
@expect = [Label, LoadConstant, SlotToReg, RegToSlot, Branch, Label ,
SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, SlotToReg, RegToSlot ,
SlotToReg, SlotToReg, RegToSlot, Label, SlotToReg, SlotToReg ,
LoadConstant, OperatorInstruction, IsPlus, LoadConstant, SlotToReg, RegToSlot ,
Label, FunctionReturn]
assert_nil msg = check_nil , msg
end
end
end

View File

@ -1,55 +0,0 @@
require_relative "helper"
class ToCodeTest < MiniTest::Test
include AST::Sexp
def check clazz
tree = Vm.ast_to_code @statement
assert_equal tree.class , Vm::Tree.const_get( clazz )
end
def test_field_access
@statement = s(:field_access, s(:receiver, s(:ivar, :m)), s(:field, s(:ivar, :index)))
check "FieldAccess"
end
def test_simple_while
@statement = s(:while_statement, :false, s(:conditional,s(:int, 1)), s(:statements))
check "WhileStatement"
end
def test_l_assignment
@statement = s(:l_assignment, s(:local, :i), s(:int, 0))
check "LocalAssignment"
end
def test_a_assignment
@statement = s(:a_assignment, s(:arg, :i), s(:int, 0))
check "ArgAssignment"
end
def test_i_assignment
@statement = s(:i_assignment, s(:ivar, :i), s(:int, 0))
check "IvarAssignment"
end
def test_nil
@statement = s(:nil)
check "NilExpression"
end
def test_true
@statement = s(:true)
check "TrueExpression"
end
def test_false
@statement = s(:false)
check "FalseExpression"
end
def test_known
@statement = s(:known, :self)
check "KnownName"
end
def test_ivar
@statement = s(:ivar, :you)
check "InstanceName"
end
def test_class_name
@statement =s(:class_name, :FooBar)
check "ClassExpression"
end
end