Sunday, April 15, 2007

Lua: Hello Mundo

The C API in Programming in Lua does not have a Hello World program. In addition, if you try to follow the example, and compile your code, the program crashes and displays the following
PANIC: unprotected error in call to Lua API (no calling environment)
So, I went to the Lua Users Wiki and found some tutorials but still no "hello world" program. So, here it is below

#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main(int argc, char* argv[])
{
char buff[] = "print(\"hello world\")";
int error;
lua_State *L = lua_open();

luaL_openlibs(L);

error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0, 0);

if(error)
{
printf("ERROR: %s", lua_tostring(L, -1));
lua_pop(L, 1);
}

lua_close(L);

return 0;
}
The code itself is self explanatory and the API interface is very interesting. A stack based approach to pass information to/from C from/to lua.

Myopic view of

No comments: