rubyx/test/test_runner.rb

56 lines
1.7 KiB
Ruby
Raw Normal View History

require_relative 'helper'
2014-05-03 14:13:44 +02:00
require "yaml"
2014-05-08 18:31:36 +02:00
require "parslet/convenience"
class TestRunner < MiniTest::Test
# this creates test methods dynamically , one for each file in runners directory
def self.runnable_methods
methods = []
Dir[File.join(File.dirname(__FILE__) , "runners" , "*.rb")].each do |file|
meth = File.basename(file).split(".").first
name = "test_#{meth}"
methods << name
self.send(:define_method, name ) {
execute file
}
end
methods
end
def execute file
string = File.read(file)
2014-05-10 10:03:23 +02:00
parser = Parser::Crystal.new
2014-05-03 17:51:47 +02:00
program = Vm::Program.new "Arm"
syntax = parser.parse_with_debug(string)
parts = Parser::Transform.new.apply(syntax)
2014-05-14 11:02:54 +02:00
# file is a list of expressions, all but the last must be a function
# and the last is wrapped as a main
parts.each_with_index do |part,index|
2014-05-14 11:02:54 +02:00
if index == (parts.length - 1)
2014-05-13 16:06:42 +02:00
expr = part.compile( program.context , program.main )
2014-05-14 11:02:54 +02:00
else
expr = part.compile( program.context , nil )
raise "should be function definition for now" unless expr.is_a? Vm::Function
program.add_function expr
end
end
program.link_at( 0 , program.context )
binary = program.assemble(StringIO.new )
2014-05-05 14:59:29 +02:00
writer = Elf::ObjectWriter.new(Elf::Constants::TARGET_ARM)
blocks = program.functions.collect{ |f| [f.entry , f.exit , f.body] }
blocks += [program.entry , program.exit , program.main]
blocks.flatten.each do |b|
writer.add_symbol b.name.to_s , b.position
end
2014-05-05 14:59:29 +02:00
assembly = program.assemble(StringIO.new)
writer.set_text assembly.string
writer.save(file.gsub(".rb" , ".o"))
2014-05-05 14:59:29 +02:00
# puts program.to_yaml
end
end