Skip to content

Commit ef9f892

Browse files
committed
add support for embedding argparse
1 parent 5c920e5 commit ef9f892

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ count:
4949
wc -l $$(git ls-files | grep 'moon$$') | sort -n | tail
5050

5151
# Binary build targets for local verification (Linux only)
52+
lua_modules:
53+
luarocks install argparse --tree=lua_modules
54+
5255
lua-$(LUA_SRC_VERSION)/src/liblua.a:
5356
curl -L -O https://www.lua.org/ftp/lua-$(LUA_SRC_VERSION).tar.gz
5457
tar -xzf lua-$(LUA_SRC_VERSION).tar.gz
@@ -68,7 +71,12 @@ bin/binaries/moon.h: bin/moon
6871
xxd -i moon.lua > $@
6972
rm moon.lua
7073

71-
dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h bin/binaries/moon.h
74+
bin/binaries/argparse.h: lua_modules
75+
bin/splat.moon -l argparse $$(find lua_modules/share/lua -name "*.lua" -exec dirname {} \; | head -1) > argparse.lua
76+
xxd -i argparse.lua > $@
77+
rm argparse.lua
78+
79+
dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h bin/binaries/moon.h bin/binaries/argparse.h
7280
mkdir -p dist
7381
gcc -static -o dist/moon \
7482
-Ilua-$(LUA_SRC_VERSION)/src/ \

bin/binaries/moonscript.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdio.h>
55

66
#include "moonscript.h"
7+
#include "argparse.h"
78

89
// put whatever is on top of stack into package.loaded under name if something
910
// is not already there
@@ -27,6 +28,11 @@ LUALIB_API int luaopen_moonscript(lua_State *l) {
2728
luaopen_lpeg(l);
2829
setloaded(l, "lpeg");
2930

31+
// Load argparse (splat output sets up package.preload)
32+
if (luaL_loadbuffer(l, (const char *)argparse_lua, argparse_lua_len, "argparse.lua") == 0) {
33+
lua_call(l, 0, 0);
34+
}
35+
3036
if (luaL_loadbuffer(l, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua") == 0) {
3137
lua_call(l, 0, 1);
3238
return 1;

0 commit comments

Comments
 (0)