forked from leafo/moonscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoon.c
71 lines (55 loc) · 1.45 KB
/
moon.c
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
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "moonscript.h"
#include "moon.h"
#include "alt_getopt.h"
#include <stdio.h>
#include "luafilesystem/src/lfs.h"
// put whatever is on top of stack into package.loaded under name something is
// already there
void setloaded(lua_State* l, char* name) {
int top = lua_gettop(l);
lua_getglobal(l, "package");
lua_getfield(l, -1, "loaded");
lua_getfield(l, -1, name);
if (lua_isnil(l, -1)) {
lua_pop(l, 1);
lua_pushvalue(l, top);
lua_setfield(l, -2, name);
}
lua_settop(l, top);
}
int main(int argc, char **argv) {
lua_State *l = luaL_newstate();
luaL_openlibs(l);
luaopen_lpeg(l);
setloaded(l, "lpeg");
luaopen_lfs(l);
setloaded(l, "lfs");
if (!luaL_loadbuffer(l, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua") == 0) {
fprintf(stderr, "Failed to load moonscript.lua\n");
return 1;
}
lua_call(l, 0, 0);
if (!luaL_loadbuffer(l, (const char *)alt_getopt_lua, alt_getopt_lua_len, "alt_getopt.lua") == 0) {
fprintf(stderr, "Failed to load alt_getopt.lua\n");
return 1;
}
lua_call(l, 0, 0);
int i;
lua_newtable(l);
lua_pushstring(l, "moon");
lua_rawseti(l, -2, -1);
for (i = 0; i < argc; i++) {
lua_pushstring(l, argv[i]);
lua_rawseti(l, -2, i);
}
lua_setglobal(l, "arg");
if (!luaL_loadbuffer(l, (const char *)moon_lua, moon_lua_len, "moon") == 0) {
fprintf(stderr, "Failed to load moon\n");
return 1;
}
lua_call(l, 0, 0);
return 0;
}