#include #include #include /* exit() */ #include "ag.h" using namespace std; static void usage(); static void report_errors(lua_State * L, int status); static void register_libraries(lua_State * L); int main(int argc, char * argv[]) { const char * program = NULL; for (int i = 1; i < argc; i++) { if (argv[i][0] != '-') { if (program == NULL) { program = argv[i]; } else { cerr << "Warning: argument " << argv[i] << " ignored!" << endl; } } else { cerr << "Warning: Unrecognized option '" << argv[i]+1 << "'" << endl; } } if (program == NULL) { usage(); } lua_State * L = lua_open(); register_libraries(L); int s = luaL_loadfile(L, program); if ( s == 0 ) { // execute Lua program s = lua_pcall(L, 0, LUA_MULTRET, 0); } else { cerr << "Warning: problem loading '" << program << "'" << endl; return 1; } report_errors(L, s); lua_close(L); return 0; } static void report_errors(lua_State * L, int status) { if ( status!=0 ) { cerr << "-- " << lua_tostring(L, -1) << endl; lua_pop(L, 1); // remove error message } } static void usage() { cerr << "Usage: anaglym [options] program.lua[c]" << endl; exit(42); } static void register_libraries(lua_State * L) { /* Load the Lua string library */ lua_pushcfunction(L, luaopen_string); lua_pcall(L, 0, 0, 0); /* Load the Lua math library */ lua_pushcfunction(L, luaopen_math); lua_pcall(L, 0, 0, 0); /* Load the Lua table library */ lua_pushcfunction(L, luaopen_table); lua_pcall(L, 0, 0, 0); ag::register_functions(L); }