From 0935ce484daa990047454602682bfec412cdeb9f Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 13 Sep 2009 23:00:04 +0000 Subject: [PATCH] added anaglym.cc and ag.cc git-svn-id: svn://anubis/anaglym/trunk@4 99a6e188-d820-4881-8870-2d33a10e2619 --- ag.cc | 70 ++++++++++++++++++++++++++++++++++++++++ ag.h | 13 ++++++++ anaglym.cc | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 ag.cc create mode 100644 ag.h create mode 100644 anaglym.cc diff --git a/ag.cc b/ag.cc new file mode 100644 index 0000000..a0fadd4 --- /dev/null +++ b/ag.cc @@ -0,0 +1,70 @@ + +#include "ag.h" +#include +#include +using namespace std; + +namespace ag +{ + void register_functions(lua_State * L) + { + static const luaL_Reg functions[] = { + { "print", ag_print }, + { NULL, NULL } + }; + luaL_register(L, "ag", functions); + } + + static int ag_print_val(lua_State * L, int index) + { + int type = lua_type(L, index); + switch (type) + { + case LUA_TNUMBER: + case LUA_TSTRING: + cout << lua_tostring(L, index); + break; + case LUA_TTABLE: + cout << '['; + + /* traverse the table and print the keys/values */ + lua_checkstack(L, 3); + lua_pushnil(L); + for (bool first = true; lua_next(L, index) != 0; first = false) + { + if (!first) + cout << ", "; + ag_print_val(L, -2); + cout << " => "; + ag_print_val(L, -1); + lua_pop(L, 1); + } + + cout << ']'; + break; + case LUA_TFUNCTION: + cout << ""; + break; + case LUA_TUSERDATA: + cout << ""; + break; + case LUA_TTHREAD: + cout << ""; + break; + case LUA_TLIGHTUSERDATA: + cout << ""; + break; + } + } + + int ag_print(lua_State * L) + { + int argc = lua_gettop(L); + + for ( int n = 1; n <= argc; n++ ) + { + ag_print_val(L, n); + } + return 0; + } +} diff --git a/ag.h b/ag.h new file mode 100644 index 0000000..5da44c9 --- /dev/null +++ b/ag.h @@ -0,0 +1,13 @@ + +#ifndef AG_H +#define AG_H + +#include + +namespace ag +{ + void register_functions(lua_State * L); + int ag_print(lua_State * L); +} + +#endif diff --git a/anaglym.cc b/anaglym.cc new file mode 100644 index 0000000..29561f1 --- /dev/null +++ b/anaglym.cc @@ -0,0 +1,93 @@ + +#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); +}