-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmoonbeam.lua
More file actions
214 lines (172 loc) · 5.27 KB
/
moonbeam.lua
File metadata and controls
214 lines (172 loc) · 5.27 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
local VERSION = "0.1.0"
function template_head()
return [[
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
// lua libraries
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
// we implement a stringbuilder here to hold the Lua source code
typedef struct {
char* data;
size_t count;
size_t cap;
} SBuilder;
// heap-allocates string builder
SBuilder* sbuilder_init() {
SBuilder* sb = (SBuilder*) malloc(sizeof(SBuilder));
sb->count = 0;
sb->cap = 1;
sb->data = (char*) calloc(sb->cap, sizeof(char));
if ((sb == NULL) || (sb->data == NULL)) {
fprintf(stderr, "Failed to allocate memory for stringbuilder.\n");
exit(71);
}
return sb;
}
// helper fn for appending a char to the stringbuilder
void sbuilder_appendchar(SBuilder* sb, char c) {
sb->data[sb->count] = c;
sb->count++;
if (sb->count > sb->cap) {
sb->cap = sb->cap * 2; // growth factor of 2
sb->data = (char*) realloc(sb->data, sb->cap * sizeof(char));
if (sb->data == NULL) {
fprintf(stderr, "Failed to allocate memory for stringbuilder.\n");
exit(71);
}
}
}
// appends a string to the string builder
void sbuilder_append(SBuilder* sb, const char* text) {
while (*text != 0) {
sbuilder_appendchar(sb, (char) *text);
text++;
}
}
// null-terminates the string, shrinks the string down to size
// and returns a pointer to it
char* sbuilder_string(SBuilder* sb) {
sbuilder_appendchar(sb, 0); // null terminate
// shrink to size
sb->data = (char*) realloc(sb->data, sb->count * sizeof(char));
if (sb->data == NULL) {
fprintf(stderr, "Failed to resize memory for stringbuilder.\n");
exit(71);
}
// reset cap to count
sb->cap = sb->count;
return sb->data;
}
// deallocate the string builder
void sbuilder_free(SBuilder* sb) {
free(sb->data);
free(sb);
}
int main(int argc, char* argv[]) {
// create the lua source stringbuilder
SBuilder* sb = sbuilder_init();
// TODO LUA SOURCE CODE GETS INSERTED HERE
]]
end
function template_tail()
return [[
// use the lua library to run it
char* source = sbuilder_string(sb);
lua_State* L = luaL_newstate();
luaL_openlibs(L);
// push cli args into "arg" table
lua_newtable(L);
for (int i = 0; i < argc; i++) {
lua_pushinteger(L, i);
lua_pushstring(L, argv[i]);
lua_settable(L, -3);
}
lua_setglobal(L, "arg");
luaL_dostring(L, source);
lua_close(L);
// free it after running
sbuilder_free(sb);
return 0;
}
]]
end
function c_escape(s)
s = s:gsub("\\", "\\\\") -- backslash first
s = s:gsub("\"", "\\\"") -- double quote
s = s:gsub("'", "\\\'") -- single quote (optional)
s = s:gsub("\n", "\\n") -- newline
s = s:gsub("\r", "\\r") -- carriage return
s = s:gsub("\t", "\\t") -- tab
s = s:gsub("\0", "\\0") -- NUL byte
return s
end
-- write to luafile.c
function lua_to_c(src)
c_src = template_head()
for line in src:gmatch("[^\r\n]+") do
c_src = c_src .. "\nsbuilder_append(sb, \"" .. c_escape(line) .. "\\n\");\n"
end
c_src = c_src .. template_tail()
return c_src
end
-- TODO maybe call the c compiler from in here?
-- command line arg parsing region
function print_usage() print("Usage: moonbeam [-v] [-h] [-c] file.lua") end
local input_file = nil
local c_only = false
local help = false
local version = false
local argi = 1
while argi <= #arg do
local a = arg[argi]
if a == "-v" then
version = true
elseif a == "-h" then
help = true
elseif a == "-c" then
c_only = true
else
if not input_file then
input_file = a
else
print("Unexpected argument: " .. a)
print_usage()
os.exit(1)
end
end
argi = argi + 1
end
if help then
print_usage()
print(" -h print help text")
print(" -v print version info")
print(" -c output C code rather than compiling to executable")
os.exit(0)
end
if version then
print("moonbeam - convert a lua script to an exe, version " .. VERSION)
os.exit(0)
end
if not input_file then print_usage() os.exit(1) end
-- now to actually read our input file
local f = io.open(input_file, "r")
if not f then print("Could not open file: " .. input_file) os.exit(1) end
local source = f:read("*a")
f:close()
-- convert to C code
local c_source = lua_to_c(source)
local cfilename = input_file:gsub("%.lua$", "") .. ".c"
local cfile = io.open(cfilename, "w")
cfile:write(c_source)
cfile:close()
if c_only then os.exit(0) end
-- otherwise, compile it with cc, remove the .c file, then exit
local command = string.format("cc -o %s %s", cfilename:gsub("%.c$", ""), cfilename)
command = command .. " lua/liblua.a -Ilua -lm -ldl"
local ret = os.execute(command) -- TODO how to handle this failing?
-- now to remove the .c file:
local ok, err = os.remove(cfilename)
if not ok then print("Failed to delete " .. cfilename) os.exit(1) end