2018-03-15 16:21:46 +01:00
|
|
|
require_relative '../helper'
|
|
|
|
|
|
|
|
module Risc
|
|
|
|
module Statements
|
|
|
|
|
|
|
|
def setup
|
2018-06-29 23:39:23 +02:00
|
|
|
Parfait.boot!
|
|
|
|
Risc::Builtin.boot_functions
|
2018-03-15 16:21:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def preamble
|
2018-05-23 17:06:55 +02:00
|
|
|
[ Label ]
|
2018-03-15 16:21:46 +01:00
|
|
|
end
|
|
|
|
def postamble
|
2018-05-23 17:06:55 +02:00
|
|
|
[ Label ]
|
2018-03-15 16:21:46 +01:00
|
|
|
end
|
2018-03-16 15:55:01 +01:00
|
|
|
# test hack to in place change object type
|
|
|
|
def add_space_field(name,type)
|
2018-07-13 20:50:40 +02:00
|
|
|
class_type = Parfait.object_space.get_type_by_class_name(:Space)
|
2018-03-16 15:55:01 +01:00
|
|
|
class_type.send(:private_add_instance_variable, name , type)
|
|
|
|
end
|
2018-03-17 06:43:44 +01:00
|
|
|
def produce_body
|
|
|
|
produced = produce_instructions
|
|
|
|
preamble.each{ produced = produced.next }
|
|
|
|
produced
|
|
|
|
end
|
2018-05-23 17:06:55 +02:00
|
|
|
def as_test_main
|
|
|
|
"class Test; def main(arg);#{@input};end;end"
|
|
|
|
end
|
2018-03-17 06:43:44 +01:00
|
|
|
def produce_instructions
|
2018-03-15 16:21:46 +01:00
|
|
|
assert @expect , "No output given"
|
2018-07-02 16:01:07 +02:00
|
|
|
linker = RubyX::RubyXCompiler.new(as_test_main).ruby_to_risc(:interpreter)
|
2018-07-30 09:23:42 +02:00
|
|
|
compiler = linker.assemblers.find{|c| c.callable.name == :main and c.callable.self_type.object_class.name == :Test}
|
2018-07-02 16:01:07 +02:00
|
|
|
compiler.instructions
|
2018-03-17 06:43:44 +01:00
|
|
|
end
|
|
|
|
def check_nil
|
|
|
|
produced = produce_instructions
|
2018-03-15 16:21:46 +01:00
|
|
|
compare_instructions produced , @expect
|
|
|
|
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}
|
2018-03-30 17:05:38 +02:00
|
|
|
str = all.collect{|i| i.class.name}.join(", ").gsub("Risc::","")
|
|
|
|
str = "[#{str}]"
|
2018-04-05 19:10:00 +02:00
|
|
|
all = str.split(",").each_slice(5).collect { |line| " " + line.join(",")}
|
2018-04-03 13:31:49 +02:00
|
|
|
all.join(",\n")
|
2018-03-15 16:21:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|