2018-03-15 16:21:46 +01:00
|
|
|
require_relative '../helper'
|
|
|
|
|
|
|
|
module Risc
|
|
|
|
module Statements
|
2019-08-23 14:31:04 +02:00
|
|
|
include Output
|
2018-03-15 16:21:46 +01:00
|
|
|
def setup
|
|
|
|
end
|
|
|
|
|
|
|
|
def preamble
|
2018-05-23 17:06:55 +02:00
|
|
|
[ Label ]
|
2018-03-15 16:21:46 +01:00
|
|
|
end
|
|
|
|
def postamble
|
2019-08-23 14:31:04 +02:00
|
|
|
[Label, SlotToReg, SlotToReg, RegToSlot,SlotToReg,
|
|
|
|
SlotToReg, SlotToReg, FunctionReturn, Label,]
|
2018-03-15 16:21:46 +01:00
|
|
|
end
|
2018-03-17 06:43:44 +01:00
|
|
|
def produce_body
|
2019-08-14 10:11:26 +02:00
|
|
|
produced = produce_main
|
2018-03-17 06:43:44 +01:00
|
|
|
preamble.each{ produced = produced.next }
|
|
|
|
produced
|
|
|
|
end
|
2019-08-13 18:32:17 +02:00
|
|
|
|
|
|
|
def as_block( block_input , method_input = "main_local = 5")
|
|
|
|
"#{method_input} ; self.main{|val| #{block_input}}"
|
|
|
|
end
|
2018-05-23 17:06:55 +02:00
|
|
|
def as_test_main
|
2019-08-14 10:11:26 +02:00
|
|
|
"class Test; #{@class_input if @class_input};def main(arg);#{@input};end;end"
|
2018-05-23 17:06:55 +02:00
|
|
|
end
|
2019-08-13 18:32:17 +02:00
|
|
|
def to_target
|
2018-03-15 16:21:46 +01:00
|
|
|
assert @expect , "No output given"
|
2019-08-18 19:35:01 +02:00
|
|
|
compiler = RubyX::RubyXCompiler.new(RubyX.default_test_options)
|
|
|
|
vool = compiler.ruby_to_vool(as_test_main)
|
|
|
|
compiler.to_target(:interpreter)
|
2018-03-17 06:43:44 +01:00
|
|
|
end
|
2019-08-14 10:11:26 +02:00
|
|
|
def produce_main
|
|
|
|
produce_target(:main)
|
2019-08-13 18:32:17 +02:00
|
|
|
end
|
|
|
|
def produce_block
|
2019-08-14 10:11:26 +02:00
|
|
|
produce_target(:main_block)
|
|
|
|
end
|
|
|
|
def produce_target(name = :main_block)
|
2019-08-13 18:32:17 +02:00
|
|
|
linker = to_target
|
2019-08-14 10:11:26 +02:00
|
|
|
block = linker.assemblers.find {|c| c.callable.name == name }
|
2019-08-13 19:35:27 +02:00
|
|
|
assert_equal Risc::Assembler , block.class
|
|
|
|
block.instructions
|
2019-08-13 18:32:17 +02:00
|
|
|
end
|
2019-08-14 10:11:26 +02:00
|
|
|
def check_nil( name = :main )
|
|
|
|
produced = produce_target( name )
|
2019-08-13 19:35:27 +02:00
|
|
|
compare_instructions( produced , @expect )
|
2018-03-15 16:21:46 +01:00
|
|
|
end
|
|
|
|
def check_return
|
|
|
|
was = check_nil
|
|
|
|
raise was if was
|
2018-03-19 09:00:14 +01:00
|
|
|
test = Parfait.object_space.get_class_by_name :Test
|
|
|
|
test.instance_type.get_method :main
|
2018-03-15 16:21:46 +01:00
|
|
|
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]
|
2018-03-30 19:01:31 +02:00
|
|
|
return "No instruction at #{index-1}\n#{should(all)[0..100]}" unless should
|
|
|
|
return "Expected at #{index-1}\n#{should(all)} was #{instruction.to_s[0..100]}" unless instruction.class == should
|
|
|
|
#puts "#{index-1}:#{instruction.to_s}" if (index > preamble.length) and (index + postamble.length <= full_expect.length)
|
2018-03-15 16:21:46 +01:00
|
|
|
index += 1
|
|
|
|
instruction = instruction.next
|
|
|
|
end while( instruction )
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
def should( all )
|
|
|
|
preamble.each {all.shift}
|
|
|
|
postamble.each {all.pop}
|
2019-08-23 14:31:04 +02:00
|
|
|
class_list(all.collect{|i| i.class})
|
2018-03-15 16:21:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|