2016-12-10 17:06:57 +01:00
|
|
|
require_relative '../helper'
|
2015-10-15 08:47:11 +02:00
|
|
|
|
2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2017-01-15 13:44:23 +01:00
|
|
|
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
|
2017-01-03 21:37:25 +01:00
|
|
|
module ExpressionHelper
|
2017-01-15 13:44:23 +01:00
|
|
|
include SpaceHack
|
2017-01-03 21:37:25 +01:00
|
|
|
|
|
|
|
def check
|
2017-01-19 08:02:29 +01:00
|
|
|
Risc.machine.boot unless Risc.machine.booted
|
2017-01-14 18:28:44 +01:00
|
|
|
compiler = Vm::MethodCompiler.new Parfait.object_space.get_main
|
|
|
|
code = Vm.ast_to_code @input
|
2017-01-03 21:37:25 +01:00
|
|
|
assert code.to_s , @input
|
|
|
|
produced = compiler.process( code )
|
|
|
|
assert @output , "No output given"
|
|
|
|
assert_equal produced.class , @output , "Wrong class"
|
|
|
|
produced
|
|
|
|
end
|
2017-01-03 21:18:41 +01:00
|
|
|
|
|
|
|
end
|
2015-10-15 08:47:11 +02:00
|
|
|
|
2017-01-03 21:37:25 +01:00
|
|
|
module Statements
|
|
|
|
include AST::Sexp
|
2017-04-10 15:12:15 +02:00
|
|
|
include CleanCompile
|
2017-01-15 13:44:23 +01:00
|
|
|
include SpaceHack
|
2017-01-17 20:23:58 +01:00
|
|
|
|
2017-01-03 21:37:25 +01:00
|
|
|
def setup
|
2017-01-19 08:02:29 +01:00
|
|
|
Risc.machine.boot # force boot to reset main
|
2017-01-03 21:37:25 +01:00
|
|
|
end
|
2016-12-10 17:48:41 +01:00
|
|
|
|
2017-01-04 20:38:03 +01:00
|
|
|
def preamble
|
|
|
|
[Label, SlotToReg , LoadConstant, RegToSlot, LoadConstant,RegToSlot, LoadConstant, SlotToReg, SlotToReg ]
|
|
|
|
end
|
|
|
|
def postamble
|
|
|
|
[ Label, FunctionReturn]
|
|
|
|
end
|
|
|
|
def check_nil
|
2017-01-03 21:37:25 +01:00
|
|
|
assert @expect , "No output given"
|
2017-01-17 20:23:58 +01:00
|
|
|
compiler = Vm::MethodCompiler.new(:main)
|
2017-01-14 18:28:44 +01:00
|
|
|
code = Vm.ast_to_code( @input )
|
2017-01-03 21:37:25 +01:00
|
|
|
assert code.to_s , @input
|
|
|
|
produced = compiler.process( code )
|
|
|
|
produced = Parfait.object_space.get_main.instructions
|
|
|
|
compare_instructions produced , @expect
|
2017-01-04 20:38:03 +01:00
|
|
|
end
|
|
|
|
def check_return
|
|
|
|
was = check_nil
|
|
|
|
raise was if was
|
|
|
|
Parfait.object_space.get_main.instructions
|
2017-01-03 21:37:25 +01:00
|
|
|
end
|
2015-10-23 20:27:36 +02:00
|
|
|
|
2017-01-04 20:38:03 +01:00
|
|
|
def compare_instructions( instruction , expect )
|
2017-01-03 21:37:25 +01:00
|
|
|
index = 0
|
2017-01-04 20:38:03 +01:00
|
|
|
all = instruction.to_arr
|
|
|
|
full_expect = preamble + expect + postamble
|
|
|
|
full_expect = expect
|
2017-01-03 21:37:25 +01:00
|
|
|
begin
|
2017-01-04 20:38:03 +01:00
|
|
|
should = full_expect[index]
|
|
|
|
return "No instruction at #{index}" unless should
|
|
|
|
return "Expected at #{index+1}\n#{should(all)}" unless instruction.class == should
|
2017-01-03 21:37:25 +01:00
|
|
|
index += 1
|
|
|
|
instruction = instruction.next
|
|
|
|
end while( instruction )
|
2017-01-04 20:38:03 +01:00
|
|
|
nil
|
2017-01-03 21:37:25 +01:00
|
|
|
end
|
2017-01-04 20:38:03 +01:00
|
|
|
def should( all )
|
|
|
|
#preamble.each {all.shift}
|
|
|
|
#postamble.each {all.pop}
|
2017-01-19 08:02:29 +01:00
|
|
|
str = all.to_s.gsub("Risc::","")
|
2017-01-03 21:37:25 +01:00
|
|
|
ret = ""
|
2017-01-04 20:38:03 +01:00
|
|
|
str.split(",").each_slice(6).each do |line|
|
2017-01-03 21:37:25 +01:00
|
|
|
ret += " " + line.join(",") + " ,\n"
|
|
|
|
end
|
|
|
|
ret
|
2015-11-02 19:11:40 +01:00
|
|
|
end
|
|
|
|
end
|
2015-10-15 08:47:11 +02:00
|
|
|
end
|