mirror of
https://github.com/samsonjs/compiler.git
synced 2026-03-25 08:45:52 +00:00
38 lines
902 B
Ruby
38 lines
902 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]
|
|
|
|
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 = *parse(input)
|
|
template = File.read("template.asm")
|
|
asm = interpolate(template, :data => data, :bss => bss, :code => code)
|
|
STDOUT.puts(asm)
|
|
end
|
|
|
|
main(ARGV[0].to_s)
|