compiler/test.rb
sjs 3f070cd0db [NEW] Emit x86 code for the mov instruction. Barely works 1/2 the time.
The supporting infrastructure includes a C program for reading a
binary blob of x86 code and wrapping it in an ELF executable for Linux
x86.  Unsure about getting the data for other sections of the binary
besides .text.
2009-05-19 17:01:14 -07:00

39 lines
1,002 B
Ruby

require 'compiler'
require 'stringio'
def error(msg) STDERR.puts(msg) end
def parse(input)
compiler = Compiler.new(input)
compiler.parse # tuple of [data, bss, code, binary]
rescue ParseError => e
error("[error] #{e.message}")
error("[context] #{e.context}")
# error("Aborting!")
error(e.caller)
exit(1)
end
def interpolate(template, data)
data.inject(template) do |template, mapping|
token, replacement = *mapping
template.sub("{#{token}}", replacement)
end
end
def main(arg)
input = if File.readable?(arg)
File.open(arg)
else
# StringIO.new("5*(3-5)*2+2-9/3-8/2-4*(5+5+5)\n")
StringIO.new("abc=999\nabc-888\n")
end
data, bss, code, binary = *parse(input)
template = File.read("template.asm")
asm = interpolate(template, :data => data, :bss => bss, :code => code)
File.open("test.asm", "w") { |f| f.puts(asm) }
File.open("test.bin", "wb") { |f| f.write(binary) }
end
main(ARGV[0].to_s)