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

Last change on this file since 79 was 79, checked in by epyon, 12 years ago
  • lua::state::do_string can take a returns parameter
  • lua::state::load_* functions made private (for now?)
File size: 1.7 KB
Line 
1#include <nv/lib/lua.hh>
2#include <nv/lua/lua_state.hh>
3#include <nv/lua/lua_raw.hh>
4#include <nv/lua/lua_glm.hh>
5#include <nv/logger.hh>
6#include <nv/types.hh>
7#include <nv/object.hh>
8#include <string>
9#include <iostream>
10#include <functional>
11#include <nv/gui/gui_element.hh>
12
13struct test_struct
14{
15        std::string f;
16        int i;
17};
18
19int main(int, char* [])
20{
21        nv::logger log(nv::LOG_TRACE);
22        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
23        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
24       
25        NV_LOG( nv::LOG_NOTICE, "Logging started" );
26
27        // create new Lua state
28        {
29                nv::lua::state state( true );
30                nlua_register_glm( state.get_raw() );
31                // run the Lua script
32                state.do_file( "init.lua" );
33
34                for (;;)
35                {
36                        nv::lua::stack_guard guard( &state );
37                        int stack = guard.get_level();
38                        std::string input;
39                        std::cout << "LUA (" << stack << ") > ";
40                        std::getline( std::cin, input );
41                        if (input == "quit")
42                        {
43                                break;
44                        }
45
46                        if (input.find("=", 0) == std::string::npos &&
47                                input.find("if", 0) == std::string::npos &&
48                                input.find("return", 0) == std::string::npos)
49                        {
50                                input = "return " + input;
51                        }
52
53                        std::cout << "> " << input << std::endl;
54
55                        bool result = state.do_string( input, "", nv::lua::ret_multi );
56                        if ( !result )
57                        {
58                                std::string error = lua_tostring( state.get_raw(), -1 );
59                                std::cout << "ERROR : " << error << std::endl;
60                                continue;
61                        }
62
63                        if (lua_gettop( state.get_raw() ) > stack)
64                        {
65                                for ( int i = stack+1; i <= lua_gettop( state.get_raw() ); ++i )
66                                {
67                                        std::cout << nlua_typecontent( state.get_raw(), i ) << std::endl;
68                                }
69                        }
70                }
71        }
72        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
73
74        return 0;
75}
76
Note: See TracBrowser for help on using the repository browser.