This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecompiler.lua
More file actions
226 lines (186 loc) · 6.11 KB
/
precompiler.lua
File metadata and controls
226 lines (186 loc) · 6.11 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env lua
--------------------------------------------------------------------------------
-- @script Lua Script Pre-Compiler
-- @version 1.1
-- @author Renato Maia <maia@tecgraf.puc-rio.br>
--
local assert = assert
local error = error
local ipairs = ipairs
local loadfile = loadfile
local pairs = pairs
local select = select
local io = require "io"
local os = require "os"
local package = require "package"
local string = require "string"
local table = require "table"
module("precompiler", require "loop.compiler.Arguments")
local FILE_SEP = "/"
local FUNC_SEP = "_"
local PATH_SEP = ";"
local PATH_MARK = "?"
help = false
bytecodes = false
names = false
luapath = package.path
output = "precompiled"
prefix = "LUAOPEN_API"
directory = ""
_optpat = "^%-(%-?%w+)(=?)(.-)$"
_alias = { ["-help"] = "help" }
for name in pairs(_M) do
_alias[name:sub(1, 1)] = name
end
local start, errmsg = _M(...)
if not start or help then
if errmsg then io.stderr:write("ERROR: ", errmsg, "\n") end
io.stderr:write([[
Lua Script Pre-Compiler 1.1 Copyright (C) 2006-2008 Tecgraf, PUC-Rio
Usage: ]],_NAME,[[.lua [options] [inputs]
[inputs] is a sequence of names that may be file paths or package names, use
the options described below to indicate how they should be interpreted. If no
[inputs] is provided then such names are read from the standard input.
Options:
-b, -bytecodes Flag that indicates the provided [inputs] are files containing
bytecodes (e.g. instead of source code), like the output of
the 'luac' compiler. When this flag is used no compilation is
performed by this script.
-d, -directory Directory where the output files should be generated. Its
default is the current directory.
-l, -luapath Sequence os path templates used to infer package names from
file paths and vice versa. These templates follows the same
format of the 'package.path' field of Lua. Its default is the
value of 'package.path' that currently is set to:
"]],luapath,[["
-n, -names Flag that indicates provided input names are actually package
names and the real file path should be inferred from
the path defined by -luapath option. This flag can be used in
conjunction with the -bytecodes flag to indicate that inferred
file paths contains bytecodes instead of source code.
-o, -output Name used to form the name of the files generated. Two files
are generated: a source code file with the sufix '.c' with
the pre-compiled scripts and a header file with the sufix
'.h' with function signatures. Its default is ']],output,[['.
-p, -prefix Prefix added to the signature of the functions generated.
Its default is ']],prefix,[['.
]])
os.exit(1)
end
--------------------------------------------------------------------------------
local function escapepattern(pattern)
return pattern:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
end
local filesep = escapepattern(FILE_SEP)
local pathsep = escapepattern(PATH_SEP)
local pathmark = escapepattern(PATH_MARK)
local function adjustpath(path)
if path ~= "" and not path:find(filesep.."$") then
return path..FILE_SEP
end
return path
end
local filepath = adjustpath(directory)..output
--------------------------------------------------------------------------------
local function readinput(file, name)
if bytecodes then
file = assert(io.open(file))
file = file:read("*a"), file:close()
else
file = string.dump(assert(loadfile(file)))
end
return file
end
local template = "[^"..pathsep.."]+"
local function getbytecodes(name)
if names then
local file = name:gsub("%.", FILE_SEP)
local err = {}
for path in luapath:gmatch(template) do
path = path:gsub(pathmark, file)
local file = io.open(path)
if file then
file:close()
return readinput(path)
end
table.insert(err, string.format("\tno file '%s'", path))
end
err = table.concat(err, "\n")
error(string.format("module '%s' not found:\n%s", name, err))
end
return readinput(name)
end
local function allequals(...)
local name = ...
for i = 2, select("#", ...) do
if name ~= select(i, ...) then return nil end
end
return name
end
local function funcname(name)
if not names then
local result
for path in luapath:gmatch(template) do
path = path:gsub(pathmark, "\0")
path = escapepattern(path)
path = path:gsub("%z", "(.-)")
path = string.format("^%s$", path)
result = allequals(name:match(path)) or result
end
if not result then
return nil, "unable to figure package name for file '"..name.."'"
end
return result:gsub(filesep, FUNC_SEP)
end
return name:gsub("%.", FUNC_SEP)
end
--------------------------------------------------------------------------------
local inputs = { select(start, ...) }
if #inputs == 0 then
for name in io.stdin:lines() do
inputs[#inputs+1] = name
end
end
local outc = assert(io.open(filepath..".c", "w"))
local outh = assert(io.open(filepath..".h", "w"))
local guard = output:upper():gsub("[^%w]", "_")
outh:write([[
#ifndef __]],guard,[[__
#define __]],guard,[[__
#include <lua.h>
#ifndef ]],prefix,[[
#define ]],prefix,[[
#endif
]])
outc:write([[
#include <lua.h>
#include <lauxlib.h>
#include "]],output,[[.h"
]])
for i, input in ipairs(inputs) do
local bytecodes = getbytecodes(input)
outc:write("static const unsigned char B",i,"[]={\n")
for j = 1, #bytecodes do
outc:write(string.format("%3u,", bytecodes:byte(j)))
if j % 20 == 0 then outc:write("\n") end
end
outc:write("\n};\n\n")
end
for i, input in ipairs(inputs) do
local func = assert(funcname(input))
outh:write(prefix," int luaopen_",func,"(lua_State *L);\n")
outc:write(
prefix,[[ int luaopen_]],func,[[(lua_State *L) {
int arg = lua_gettop(L);
luaL_loadbuffer(L,(const char*)B]],i,[[,sizeof(B]],i,[[),"]],input,[[");
lua_insert(L,1);
lua_call(L,arg,1);
return 1;
}
]])
end
outh:write([[
#endif /* __]],guard,[[__ */
]])
outh:close()
outc:close()