added anaglym.cc and ag.cc
git-svn-id: svn://anubis/anaglym/trunk@4 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
parent
6a3d938e75
commit
0935ce484d
70
ag.cc
Normal file
70
ag.cc
Normal file
@ -0,0 +1,70 @@
|
||||
|
||||
#include "ag.h"
|
||||
#include <iostream>
|
||||
#include <lua.hpp>
|
||||
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 << "<function>";
|
||||
break;
|
||||
case LUA_TUSERDATA:
|
||||
cout << "<userdata>";
|
||||
break;
|
||||
case LUA_TTHREAD:
|
||||
cout << "<thread>";
|
||||
break;
|
||||
case LUA_TLIGHTUSERDATA:
|
||||
cout << "<lightuserdata>";
|
||||
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;
|
||||
}
|
||||
}
|
13
ag.h
Normal file
13
ag.h
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
#ifndef AG_H
|
||||
#define AG_H
|
||||
|
||||
#include <lua.hpp>
|
||||
|
||||
namespace ag
|
||||
{
|
||||
void register_functions(lua_State * L);
|
||||
int ag_print(lua_State * L);
|
||||
}
|
||||
|
||||
#endif
|
93
anaglym.cc
Normal file
93
anaglym.cc
Normal file
@ -0,0 +1,93 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <lua.hpp>
|
||||
#include <stdlib.h> /* 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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user