-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.lua
57 lines (47 loc) · 1.4 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
--[[ Vortex 0.1 main program
Author: q66 <[email protected]>
Available under the terms of the MIT license.
]]
package.path = package.path .. ";./src/?.lua;./src/?/init.lua"
local util = require("util")
local parser = require("parser")
local stderr, exit = io.stderr, os.exit
local error_exit = function(msg)
stderr:write(msg, "\n")
stderr:flush()
exit(1)
end
local vxpcall = util.vxpcall
local compile_all = function(args)
for i = 1, #args do
local ifname = args[i]
local rs = io.open(ifname, "r")
if not rs then
io.stderr:write(ifname .. ": No such file or directory\n")
return 1
end
local st = rs:read("*all")
io.close(rs)
local stat, ret = vxpcall(parser.parse, ifname, st)
if not stat then error_exit(ret) end
stat, ret = vxpcall(parser.build, ret)
if not stat then error_exit(ret) end
local ofname
local has_ext = ifname:find("%.vx")
if not has_ext then
ofname = ifname .. ".lua"
else
ofname = ifname:gsub("%.vx", ".lua")
end
local ws = io.open(ofname, "w")
if not ws then
io.stderr:write("Cannot open " .. ofname ..
" for writing.\n")
io.close(rs)
return 1
end
ws:write(ret)
io.close(ws)
end
end
os.exit(compile_all(arg) or 0)