Skip to content

Commit 8fe8c2a

Browse files
committed
convert moon.moon -> moon.h, and run it through the moon.c entry point
1 parent 6284a0c commit 8fe8c2a

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ bin/binaries/moonscript.h: moonscript/*.lua moon/*.lua
6363
xxd -i moonscript.lua > $@
6464
rm moonscript.lua
6565

66-
dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h
66+
bin/binaries/moon.h: bin/moon
67+
awk 'FNR>1' bin/moon > moon.lua
68+
xxd -i moon.lua > $@
69+
rm moon.lua
70+
71+
dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h bin/binaries/moon.h
6772
mkdir -p dist
6873
gcc -static -o dist/moon \
6974
-Ilua-$(LUA_SRC_VERSION)/src/ \

bin/binaries/moon.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <lauxlib.h>
44
#include <stdio.h>
55

6+
#include "moon.h" // the CLI script
7+
68
// from moonscript.c
79
extern int luaopen_moonscript(lua_State *l);
810

@@ -12,17 +14,25 @@ int main(int argc, char **argv) {
1214

1315
// Load moonscript (this also loads lpeg)
1416
luaopen_moonscript(l);
15-
lua_pop(l, 1); // pop the return value
17+
lua_pop(l, 1);
1618

17-
// Simple test: require moonscript and compile something
18-
const char *test_code =
19-
"local moonscript = require('moonscript')\n"
20-
"local code = moonscript.to_lua('print \"hello from moonscript\"')\n"
21-
"print(code)\n"
22-
"loadstring(code)()\n";
19+
// Set up arg table
20+
lua_newtable(l);
21+
lua_pushstring(l, "moon");
22+
lua_rawseti(l, -2, -1);
23+
for (int i = 0; i < argc; i++) {
24+
lua_pushstring(l, argv[i]);
25+
lua_rawseti(l, -2, i);
26+
}
27+
lua_setglobal(l, "arg");
2328

24-
if (luaL_dostring(l, test_code) != 0) {
25-
fprintf(stderr, "Test failed: %s\n", lua_tostring(l, -1));
29+
// Load and execute the moon CLI script
30+
if (luaL_loadbuffer(l, (const char *)moon_lua, moon_lua_len, "moon") != 0) {
31+
fprintf(stderr, "Failed to load moon: %s\n", lua_tostring(l, -1));
32+
return 1;
33+
}
34+
if (lua_pcall(l, 0, 0, 0) != 0) {
35+
fprintf(stderr, "Error: %s\n", lua_tostring(l, -1));
2636
return 1;
2737
}
2838

0 commit comments

Comments
 (0)