2011-05-20 08:08:36 +00:00
|
|
|
#!/usr/bin/lua
|
|
|
|
|
|
|
|
module("moonscript", package.seeall)
|
|
|
|
|
|
|
|
require "moonscript.parse"
|
2011-07-04 16:20:46 +00:00
|
|
|
require "moonscript.compile"
|
2011-05-29 04:27:31 +00:00
|
|
|
require "moonscript.util"
|
2011-05-20 08:08:36 +00:00
|
|
|
|
|
|
|
require "alt_getopt"
|
|
|
|
|
2011-06-16 15:45:25 +00:00
|
|
|
local opts, ind = alt_getopt.get_opts(arg, "hbto:", { help = "h" })
|
2011-05-20 08:08:36 +00:00
|
|
|
|
2011-07-14 04:09:50 +00:00
|
|
|
local pos_to_line, get_line = util.pos_to_line, util.get_line
|
|
|
|
|
2011-05-20 08:08:36 +00:00
|
|
|
local help = [[Usage: %s [options] file...
|
|
|
|
|
|
|
|
-h Print this message
|
2011-05-21 08:11:25 +00:00
|
|
|
-t Dump parse tree
|
2011-06-16 15:45:25 +00:00
|
|
|
-b Dump time to parse and compile
|
2011-05-21 08:11:25 +00:00
|
|
|
-o fname Write output to file
|
2011-05-20 08:08:36 +00:00
|
|
|
]]
|
|
|
|
|
2011-06-16 15:45:25 +00:00
|
|
|
local gettime = nil
|
|
|
|
pcall(function()
|
|
|
|
require "socket"
|
|
|
|
gettime = socket.gettime
|
|
|
|
end)
|
|
|
|
|
|
|
|
function format_time(time)
|
|
|
|
return ("%.3fms"):format(time*1000)
|
|
|
|
end
|
|
|
|
|
2011-05-20 08:08:36 +00:00
|
|
|
function print_help(err)
|
|
|
|
if err then print("Error: "..err) end
|
|
|
|
print(help:format(arg[0]))
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
|
|
|
function read_file(fname)
|
|
|
|
local f = io.open(fname)
|
|
|
|
if not f then return nil end
|
|
|
|
return f:read("*a")
|
|
|
|
end
|
|
|
|
|
|
|
|
local files = {}
|
|
|
|
for i = ind, #arg do
|
|
|
|
table.insert(files, arg[i])
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.h then print_help() end
|
|
|
|
if #files == 0 then
|
|
|
|
print_help"Missing input file"
|
|
|
|
end
|
|
|
|
|
|
|
|
local fname = files[1]
|
|
|
|
|
|
|
|
local file_str = read_file(fname)
|
|
|
|
if not file_str then
|
|
|
|
print_help("Failed to find file `"..fname.."`")
|
|
|
|
end
|
|
|
|
|
2011-06-16 15:45:25 +00:00
|
|
|
|
|
|
|
local start_parse = gettime()
|
2011-05-20 08:08:36 +00:00
|
|
|
local tree, err = parse.string(file_str)
|
2011-06-16 15:45:25 +00:00
|
|
|
local parse_time = gettime() - start_parse
|
2011-05-20 08:08:36 +00:00
|
|
|
|
|
|
|
if not tree then
|
|
|
|
print("Parse error: "..err)
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.t then
|
|
|
|
print(dump.tree(tree))
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
2011-06-16 15:45:25 +00:00
|
|
|
local start_compile = gettime()
|
2011-07-14 04:09:50 +00:00
|
|
|
|
|
|
|
local code, err, pos = compile.tree(tree)
|
|
|
|
if not code then
|
|
|
|
print(compile.format_error(err, pos, file_str))
|
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
|
2011-06-16 15:45:25 +00:00
|
|
|
local compile_time = gettime() - start_compile
|
|
|
|
|
|
|
|
if opts.b then
|
|
|
|
print("Parse time ", format_time(parse_time))
|
|
|
|
print("Compile time", format_time(compile_time))
|
2011-05-20 08:08:36 +00:00
|
|
|
else
|
2011-06-16 15:45:25 +00:00
|
|
|
if opts.o then
|
|
|
|
io.open(opts.o, "w"):write(code.."\n")
|
|
|
|
else
|
|
|
|
print(code)
|
|
|
|
end
|
2011-05-20 08:08:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|