2014-04-28 11:19:28 +03:00
|
|
|
require_relative 'helper'
|
2014-05-03 15:13:44 +03:00
|
|
|
require "yaml"
|
2014-05-08 19:31:36 +03:00
|
|
|
require "parslet/convenience"
|
2014-04-28 11:19:28 +03:00
|
|
|
class TestRunner < MiniTest::Test
|
|
|
|
|
|
|
|
# this creates test methods dynamically , one for each file in runners directory
|
|
|
|
def self.runnable_methods
|
|
|
|
methods = []
|
2014-04-28 16:47:12 +03:00
|
|
|
Dir[File.join(File.dirname(__FILE__) , "runners" , "*.rb")].each do |file|
|
2014-04-28 11:19:28 +03:00
|
|
|
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 11:03:23 +03:00
|
|
|
parser = Parser::Crystal.new
|
2014-05-03 18:51:47 +03:00
|
|
|
program = Vm::Program.new "Arm"
|
2014-05-10 11:23:45 +03:00
|
|
|
syntax = parser.parse_with_debug(string)
|
|
|
|
parts = Parser::Transform.new.apply(syntax)
|
2014-05-14 12:02:54 +03:00
|
|
|
# file is a list of expressions, all but the last must be a function
|
2014-05-10 11:23:45 +03:00
|
|
|
# and the last is wrapped as a main
|
2014-05-10 10:59:36 +03:00
|
|
|
parts.each_with_index do |part,index|
|
2014-05-14 12:02:54 +03:00
|
|
|
if index == (parts.length - 1)
|
2014-05-13 17:06:42 +03:00
|
|
|
expr = part.compile( program.context , program.main )
|
2014-05-14 12:02:54 +03:00
|
|
|
else
|
|
|
|
expr = part.compile( program.context , nil )
|
|
|
|
raise "should be function definition for now" unless expr.is_a? Vm::Function
|
2014-05-10 10:59:36 +03:00
|
|
|
end
|
|
|
|
end
|
2014-05-05 11:03:43 +03:00
|
|
|
|
|
|
|
program.link_at( 0 , program.context )
|
|
|
|
|
|
|
|
binary = program.assemble(StringIO.new )
|
|
|
|
|
2014-05-05 15:59:29 +03:00
|
|
|
writer = Elf::ObjectWriter.new(Elf::Constants::TARGET_ARM)
|
2014-05-14 16:15:47 +03:00
|
|
|
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 15:59:29 +03:00
|
|
|
|
2014-05-14 22:04:03 +03:00
|
|
|
writer.set_text binary.string
|
2014-05-05 22:21:11 +03:00
|
|
|
writer.save(file.gsub(".rb" , ".o"))
|
2014-05-05 15:59:29 +03:00
|
|
|
|
2014-05-10 10:59:36 +03:00
|
|
|
# puts program.to_yaml
|
2014-04-28 11:19:28 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|