1 | #include <nv/lib/lua.hh>
|
---|
2 | #include <nv/lua/lua_raw.hh>
|
---|
3 | #include <nv/lua/lua_glm.hh>
|
---|
4 | #include <nv/logger.hh>
|
---|
5 | #include <string>
|
---|
6 | #include <iostream>
|
---|
7 |
|
---|
8 | int main(int, char* [])
|
---|
9 | {
|
---|
10 | nv::logger log(nv::LOG_TRACE);
|
---|
11 | log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
|
---|
12 | log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
|
---|
13 | nv::load_lua_library();
|
---|
14 |
|
---|
15 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
16 |
|
---|
17 | // create new Lua state
|
---|
18 | lua_State *lua_state;
|
---|
19 | lua_state = luaL_newstate();
|
---|
20 |
|
---|
21 | // load Lua libraries
|
---|
22 | static const luaL_Reg lualibs[] =
|
---|
23 | {
|
---|
24 | { "base", luaopen_base },
|
---|
25 | { NULL, NULL}
|
---|
26 | };
|
---|
27 |
|
---|
28 | nlua_register_glm( lua_state );
|
---|
29 |
|
---|
30 | const luaL_Reg *lib = lualibs;
|
---|
31 | for(; lib->func != NULL; lib++)
|
---|
32 | {
|
---|
33 | lib->func(lua_state);
|
---|
34 | lua_settop(lua_state, 0);
|
---|
35 | }
|
---|
36 |
|
---|
37 | // run the Lua script
|
---|
38 | luaL_dofile(lua_state, "init.lua");
|
---|
39 |
|
---|
40 | while (true)
|
---|
41 | {
|
---|
42 | std::string input;
|
---|
43 | int stack = lua_gettop( lua_state );
|
---|
44 | std::cout << "LUA (" << stack << ") > ";
|
---|
45 | std::getline( std::cin, input );
|
---|
46 | if (input == "quit")
|
---|
47 | {
|
---|
48 | break;
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (input.find("=", 0) == std::string::npos &&
|
---|
52 | input.find("if", 0) == std::string::npos &&
|
---|
53 | input.find("return", 0) == std::string::npos)
|
---|
54 | {
|
---|
55 | input = "return " + input;
|
---|
56 | }
|
---|
57 |
|
---|
58 | std::cout << "> " << input << std::endl;
|
---|
59 |
|
---|
60 | int code = luaL_loadstring( lua_state, input.c_str() );
|
---|
61 | if (code == 0) code = lua_pcall( lua_state, 0, LUA_MULTRET, 0);
|
---|
62 | if (code != 0)
|
---|
63 | {
|
---|
64 | std::string error = lua_tostring( lua_state, -1 );
|
---|
65 | std::cout << "ERROR : " << error << std::endl;
|
---|
66 | lua_settop( lua_state, stack );
|
---|
67 | continue;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (lua_gettop( lua_state ) > stack)
|
---|
71 | {
|
---|
72 | for ( int i = stack+1; i <= lua_gettop( lua_state ); ++i )
|
---|
73 | {
|
---|
74 | std::cout << nlua_typecontent( lua_state, i ) << std::endl;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | lua_settop( lua_state, stack );
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | // close the Lua state
|
---|
82 | lua_close(lua_state);
|
---|
83 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
84 |
|
---|
85 | return 0;
|
---|
86 | }
|
---|
87 |
|
---|