Skip to content

Commit 6284a0c

Browse files
committed
working through binaries some more
1 parent 905a24a commit 6284a0c

File tree

5 files changed

+83
-7
lines changed

5 files changed

+83
-7
lines changed

Makefile

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,26 @@ lpeg-$(LPEG_VERSION)/lptree.c:
5858
curl -L -o lpeg.tar.gz https://www.inf.puc-rio.br/~roberto/lpeg/lpeg-$(LPEG_VERSION).tar.gz
5959
tar -xzf lpeg.tar.gz
6060

61-
dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c
61+
bin/binaries/moonscript.h: moonscript/*.lua moon/*.lua
62+
bin/splat.moon -l moonscript moonscript moon > moonscript.lua
63+
xxd -i moonscript.lua > $@
64+
rm moonscript.lua
65+
66+
dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h
6267
mkdir -p dist
63-
gcc -static -o dist/moon -Ilua-$(LUA_SRC_VERSION)/src/ bin/binaries/moon.c lpeg-$(LPEG_VERSION)/lpvm.c lpeg-$(LPEG_VERSION)/lpcap.c lpeg-$(LPEG_VERSION)/lptree.c lpeg-$(LPEG_VERSION)/lpcode.c lpeg-$(LPEG_VERSION)/lpprint.c lua-$(LUA_SRC_VERSION)/src/liblua.a -lm -ldl
68+
gcc -static -o dist/moon \
69+
-Ilua-$(LUA_SRC_VERSION)/src/ \
70+
-Ilpeg-$(LPEG_VERSION)/ \
71+
-Ibin/binaries/ \
72+
bin/binaries/moon.c \
73+
bin/binaries/moonscript.c \
74+
lpeg-$(LPEG_VERSION)/lpvm.c \
75+
lpeg-$(LPEG_VERSION)/lpcap.c \
76+
lpeg-$(LPEG_VERSION)/lptree.c \
77+
lpeg-$(LPEG_VERSION)/lpcode.c \
78+
lpeg-$(LPEG_VERSION)/lpprint.c \
79+
lua-$(LUA_SRC_VERSION)/src/liblua.a \
80+
-lm -ldl
6481

6582
test_binary: dist/moon
66-
dist/moon -h
67-
dist/moon -e 'print "hello world"'
83+
dist/moon

bin/binaries/moon.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
1+
#include <lua.h>
2+
#include <lualib.h>
3+
#include <lauxlib.h>
4+
#include <stdio.h>
5+
6+
// from moonscript.c
7+
extern int luaopen_moonscript(lua_State *l);
18

2-
// this does nothing until we get a build working for all deps
39
int main(int argc, char **argv) {
10+
lua_State *l = luaL_newstate();
11+
luaL_openlibs(l);
12+
13+
// Load moonscript (this also loads lpeg)
14+
luaopen_moonscript(l);
15+
lua_pop(l, 1); // pop the return value
16+
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";
23+
24+
if (luaL_dostring(l, test_code) != 0) {
25+
fprintf(stderr, "Test failed: %s\n", lua_tostring(l, -1));
26+
return 1;
27+
}
28+
29+
lua_close(l);
30+
return 0;
431
}

bin/binaries/moonscript.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <lua.h>
2+
#include <lualib.h>
3+
#include <lauxlib.h>
4+
#include <stdio.h>
5+
6+
#include "moonscript.h"
7+
8+
// put whatever is on top of stack into package.loaded under name if something
9+
// is not already there
10+
void setloaded(lua_State* l, const char* name) {
11+
int top = lua_gettop(l);
12+
lua_getglobal(l, "package");
13+
lua_getfield(l, -1, "loaded");
14+
lua_getfield(l, -1, name);
15+
if (lua_isnil(l, -1)) {
16+
lua_pop(l, 1);
17+
lua_pushvalue(l, top);
18+
lua_setfield(l, -2, name);
19+
}
20+
21+
lua_settop(l, top);
22+
}
23+
24+
extern int luaopen_lpeg(lua_State *l);
25+
26+
LUALIB_API int luaopen_moonscript(lua_State *l) {
27+
luaopen_lpeg(l);
28+
setloaded(l, "lpeg");
29+
30+
if (luaL_loadbuffer(l, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua") == 0) {
31+
lua_call(l, 0, 1);
32+
return 1;
33+
}
34+
return 0;
35+
}

moon.lua

Lines changed: 0 additions & 1 deletion
This file was deleted.

moonscript.lua

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)