-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlua2c.lua
More file actions
193 lines (161 loc) · 5.49 KB
/
lua2c.lua
File metadata and controls
193 lines (161 loc) · 5.49 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
--==============================================================================
-- Converts Lua source to Lua-based C to be compiled with C compiler
-- Written on 2016.10.11 by Tony Papadimitriou <tonyp@acm.org>
--==============================================================================
local cli = require 'cli'
local option = cli.get_all_options()
local cc = table.concat
local filename = cli.get_argument(1)
if filename == nil then
print('Usage: '..arg[0]..' filename')
print [[ Converts Lua source program to C for Lua stand-alone executable.
Options:
-E ignore environment variables
Compile with something like:]]
--print(' cl -DTONYP -I\\progs\\lua\\compiler \\progs\\lua\\compiler\\libs\\lsqlite3.c \\progs\\lua\\compiler\\libs\\lfs.c your_prog.c')
print(' tcc -B/tcc -I/progs/lua/compiler your_prog.c')
print(' (where your_proc.c is the output of this utility)')
return
end
--------------------------------------------------------------------------------
local one = require 'one'
filename = filename_extension(filename,'')
local file = one.unite(filename_default_extension(filename,'.lua'),false,option.E)
--------------------------------------------------------------------------------
local
function basename(filename)
return (filename:match '[^/\\]+$')
end
--------------------------------------------------------------------------------
local
function run(cmd,debug)
if debug == nil then debug = true else debug = false end
assert(type(cmd) == 'string','cmd should be the string of the command to run')
if debug then print('Running...',cmd) end
local f = io.popen(cmd)
local ans = f:read('*a')
f:close()
return ans
end
--------------------------------------------------------------------------------
local outfile = {}
outfile[#outfile+1] = [[
/*
Template for creating stand-alone EXE files from Lua source
Compile with something like: tcc -B/tcc -I/progs/lua/compiler your_prog.c
*/
#define l_getlocaledecpoint() '.'
#define SQLITE_DEFAULT_FOREIGN_KEYS 1
#define SQLITE_ENABLE_RTREE 1
#define SQLITE_SOUNDEX 1
#define SQLITE_ENABLE_STAT4 1
#define SQLITE_ENABLE_UPDATE_DELETE_LIMIT 1
#define SQLITE_ENABLE_FTS4 1
#define SQLITE_ENABLE_FTS5 1
#define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 1
/* core -- used by all */
#include <lapi.c>
#include <lcode.c>
#include <lctype.c>
#include <ldebug.c>
#include <ldo.c>
#include <ldump.c>
#include <lfunc.c>
#include <lgc.c>
#include <llex.c>
#include <lmem.c>
#include <lobject.c>
#include <lopcodes.c>
#include <lparser.c>
#include <lstate.c>
#include <lstring.c>
#include <ltable.c>
#include <ltm.c>
#include <lundump.c>
#include <lvm.c>
#include <lzio.c>
/* auxiliary library -- used by all */
#include <lauxlib.c>
/* standard library -- not used by luac */
#include <lbaselib.c>
#if defined(LUA_COMPAT_BITLIB)
#include <lbitlib.c>
#endif
#include <lcorolib.c>
#include <ldblib.c>
#include <liolib.c>
#include <lmathlib.c>
#include <loadlib.c>
#include <loslib.c>
#include <lstrlib.c>
#include <ltablib.c>
#include <lutf8lib.c>
#include <linit.c>
/*
** Create the 'arg' table, which stores all arguments from the
** command line ('argv'). It should be aligned so that, at index 0,
** it has 'argv[script]', which is the script name. The arguments
** to the script (everything after 'script') go to positive indices;
** other arguments (before the script name) go to negative indices.
** If there is no script name, assume interpreter's name as base.
*/
static void createargtable (lua_State *L, char **argv, int argc, int script) {
int i, narg;
if (script == argc) script = 0; /* no script name? */
narg = argc - (script + 1); /* number of positive indices */
lua_createtable(L, narg, script + 1);
for (i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i - script);
}
lua_setglobal(L, "arg");
}
/* Define the script we are going to run */
const char my_prog[] = {
]]
file = string.dump(load(file,filename_extension(filename,''),'t'))
local count = 0
local bytes = {}
for c in file:gmatch '.' do
bytes[#bytes+1] = ('0x%02x'):format(c:byte())
count = count + 1
if count > 14 then
outfile[#outfile+1] = cc(bytes,',')..','
count = 0
bytes = {}
end
end
if count > 0 then outfile[#outfile+1] = cc(bytes,',') end
outfile[#outfile+1] = [[
};
int
main(int argc, char **argv)
{
int status, result, i;
double sum;
lua_State *L;
L = luaL_newstate(); /* Create new Lua context */
luaL_openlibs(L); /* Load Lua libraries */
createargtable(L, argv, argc, 0); /* create table 'arg' */
status = luaL_loadbuffer(L,my_prog,sizeof(my_prog),"]] .. basename(filename_extension(filename,'')) .. [[");
if (status) {
/* If failed, error message is at the top of the stack */
fprintf(stderr, "Loading error: %s\n", lua_tostring(L, -1));
exit(1);
}
status = lua_pcall(L, 0, 0, 0); /* call the loaded chunk */
if (status) {
/* If failed, error message is at the top of the stack */
fprintf(stderr, "Runtime error: %s\n", lua_tostring(L, -1));
exit(1);
}
lua_close(L); /* Done with Lua */
return 0;
}
]]
--------------------------------------------------------------------------------
-- If no 2nd argument is given, assume the same as input but with .c extension
--------------------------------------------------------------------------------
filename = cli.get_argument(2) or basename(filename) --keep only filename without path if not provided
putfile(filename_extension(filename,'.c'),outfile)
--==============================================================================