12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 | #!/usr/bin/env moon
util = require 'util'
import log, viz, set_file from util
number_code = (code, color="1")->
line = 0
(code\gsub("\n$","")\gsub "[^\n]*", =>
line += 1
"\x1b[2m#{("% 4d")\format line}|\x1b[22m \x1b[#{color}m#{@}\x1b[m"
)
verbose = false
files, args = {}, {}
for i,a in ipairs arg
if a == "-v" or a == "--verbose"
verbose = true
elseif a == "-h" or a == "--help"
print "blang - run blang programs\nUsage: blang [-v|--verbose] files... [-- arguments...]"
os.exit!
elseif a == "--"
args = {table.unpack(arg, i+1, #arg)}
break
else
table.insert files, a
unless verbose
log = ->
util.log = log
mktemp = (suffix)-> io.popen("mktemp --suffix=#{suffix} /tmp/blang.XXXXXX")\read("l")
run = (cmd)->
log "> \x1b[1m#{cmd}\x1b[m"
success,how,what = os.execute cmd
if not success
log "\x1b[31;1mCommand `#{cmd}` #{how}ed: #{what}\x1b[m"
os.exit(1)
run_file = (filename)->
log "\x1b[1;4mFile #{filename}\x1b[m"
qbe_file,asm_file,bin_file = mktemp(".qbe"), mktemp(".s"), mktemp(".o")
run "./blangc #{verbose and "-v" or ""} -q #{qbe_file} -a #{asm_file} -o #{bin_file} #{filename}"
log "\x1b[2mRunning program:\x1b[m"
exec_path = bin_file\match("^/") and bin_file or "./"..bin_file
run "#{exec_path} #{table.concat args, " "}"
os.execute "rm -f \"#{qbe_file}\" \"#{asm_file}\" \"#{bin_file}\""
if #files == 0
run_file "/dev/stdin"
for filename in *files
run_file filename
|