-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
executable file
·86 lines (68 loc) · 2.25 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env lua5.3
--
-- Copyright (c) 2022 chiya.dev
--
-- Use of this source code is governed by the MIT License
-- which can be found in the LICENSE file and at:
--
-- https://opensource.org/licenses/MIT
--
-- This assembler was quickly hacked together using parts of the WindSeed compiler ;)
--
local argparse = require("argparse")
local log = require("log")
local pipeline = require("pipeline")
local tasks = {
resolve = require("pipeline.resolve"),
assemble = require("pipeline.assemble"),
call_entrypoint = require("pipeline.call_entrypoint"),
print_ast = require("pipeline.print_ast"),
output = require("pipeline.output"),
}
local function get_args()
local parser = argparse():name("luasm"):description("Lua module assembler")
parser:option("-m --main"):description("Name of the entrypoint module"):default("main")
parser:option("-s --spec"):description("Path to package spec"):default("package.lua")
parser:option("-o --out"):description("Path to write output to"):default("-")
parser:flag("-e --executable"):description("Make output executable (Linux only)")
parser:flag("-p --pretty"):description("Pretty print output")
parser:mutex(
parser:flag("-q --quiet"):description("Do not print any logs."),
parser:flag("-v --verbose"):description("Print more logs.")
)
return parser:parse()
end
local function build_pipeline(args)
local main = pipeline.new("luasm")
main:add(tasks.resolve.new())
main:add(tasks.assemble.new())
main:add(tasks.call_entrypoint.new {
module = args.main,
})
main:add(tasks.print_ast.new {
pretty = args.pretty,
})
main:add(tasks.output.new {
path = args.out,
executable = args.executable,
})
return main
end
local function main(args)
log.enabled.error = not args.quiet
log.enabled.warn = not args.quiet
log.enabled.info = not args.quiet
log.enabled.debug = not args.quiet and args.verbose
local pipeline = build_pipeline(args)
log.debug("execution plan: %s", pipeline:desc(true))
local err = pipeline:run(args.spec)
if err then
log.error("%s", err)
os.exit(1)
end
end
local ok, result = xpcall(main, debug.traceback, get_args())
if not ok then
log.error("unhandled compiler error: %s", result)
log.error("Please report this error to @luaneko.")
end