-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.lua
executable file
·126 lines (106 loc) · 4.46 KB
/
compile.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env lua
--[[
Copyright (c) 2021-2025 Jason Morley, Tom Sutcliffe
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
dofile(arg[0]:match("^(.-)[a-z]+%.lua$").."cmdline.lua")
function main()
local args = getopt({
"filename",
"output",
dump = true, d = "dump",
include = table, i = "include",
source = string, s = "source",
help = true, h = "help",
aif = true,
})
if args.help then
print([[
Syntax: compile.lua [options] <filename> [<ouput>]
Compile a Series 5 era OPL program. If neither <output> nor --dump are
specified, the result is written to <filename>.opo alongside <filename>.
<filename> can be either a plain text file, or an OPL file. OPL files are
converted to text as per opltotext.lua.
Options:
--dump, -d
Prints the bytecode of the resulting binary to stdout.
--include <dir>, -i <dir>
Add <dir> to the list of locations to be searched when an INCLUDE
statement is encountered. If no paths are specified, only the standard
built-in headers can be INCLUDEed. Note, paths MUST end in the
appropriate filesystem path separator, for example "-i ./". Note also
that while includes of built-ins are case-insensitive, when including
files from the filesystem the include name is case-sensitive if the
filesystem is.
--source <path>, -s <path>
Override the source file path included in the output. If not specified,
will be set to <filename>.
--aif
If specified, an AIF file will be written alongside <output>.
The file being compiled must have a "APP .. ENDA" section.
]])
os.exit(false)
end
local compiler = require("compiler")
local progText = readFile(args.filename)
if progText:sub(1, 4) == string.pack("<I4", KUidDirectFileStore) then
-- Assume it's a .opl file
progText = require("recognizer").getOplText(progText)
end
local ok, result, aif = xpcall(compiler.compile, traceback, args.source or args.filename, args.filename, progText, args.include)
if not ok then
if type(result) == "string" then
print(result)
print("Internal compiler error, please report to https://github.com/inseven/opolua/issues")
print("including the above error message and if possible the file being compiled.")
else
printf("%s:%d:%d: %s\n%s\n", result.src.path, result.src.line, result.src.column, result.msg, result.traceback)
end
os.exit(false)
return
end
assert(not args.aif or aif, "No APP section found in OPL source")
if args.dump then
opofile = require("opofile")
runtime = require("runtime")
local procTable, opxTable, era = opofile.parseOpo(result, true)
local rt = runtime.newRuntime(nil, era)
rt:addModule("C:\\module", procTable, opxTable)
for i, proc in ipairs(procTable) do
printf("%d: ", i)
opofile.printProc(proc)
rt:dumpProc(proc.name)
end
end
if args.output or not args.dump then
local outName = args.output or (args.filename .. ".opo")
writeFile(outName, result)
if args.aif then
local aifName = oplpath.splitext(outName)..".aif"
writeFile(aifName, aif)
end
end
end
function traceback(err)
if type(err) == "table" then
err.traceback = debug.traceback(nil, 2)
return err
else
return debug.traceback(err, 2)
end
end
pcallMain()