source: trunk/tests/lualib_test/lualib_test.cc @ 52

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