33#include <lauxlib.h>
44#include <stdio.h>
55
6+ #include "moon.h" // the CLI script
7+
68// from moonscript.c
79extern 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