-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
62 lines (49 loc) · 1.25 KB
/
Copy pathmain.cpp
File metadata and controls
62 lines (49 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifdef __cplusplus
# include <lua.hpp>
#else
# include <lua.h>
# include <lualib.h>
# include <lauxlib.h>
#endif
void print_error(lua_State* state) {
// The error message is on top of the stack.
// Fetch it, print it and then pop it off the stack.
const char* message = lua_tostring(state, -1);
puts(message);
lua_pop(state, 1);
}
void execute(const char* filename)
{
lua_State *state = luaL_newstate();
// Make standard libraries available in the Lua object
luaL_openlibs(state);
int result;
// Load the program; this supports both source code and bytecode files.
result = luaL_loadfile(state, filename);
if ( result != LUA_OK ) {
print_error(state);
return;
}
// Finally, execute the program by calling into it.
// Change the arguments if you're not running vanilla Lua code.
puts("File loaded correctly");
result = lua_pcall(state, 0, LUA_MULTRET, 0);
if ( result != LUA_OK ) {
print_error(state);
return;
}
}
int main(int argc, char** argv)
{
if ( argc <= 1 ) {
puts("Usage: runlua file(s)");
puts("Loads and executes Lua programs.");
return 1;
}
// Execute all programs on the command line
for ( int n=1; n<argc; ++n ) {
execute(argv[n]);
}
//execute("myfile.lua");
return 0;
}