Doing something useful with lua.Enough setup, lets get something functional. Currently we have enough code under our belt to create an interpreter. I'm going to focus the next section on building a very limited interpreter. No fancy editor, no inline debugging. Just a console that allows us to type in lua commands and have the interpreter performs actions. This is going to require us to know one more lua based function: lua_dostring(). Essentially, what this function does is perform actions in lua. I don't know how else to describe that, other than actually illustrating it: #include <stdio.h> extern "C" { #include <lua.h> } int main(int argc, char* argv[ ]) { lua_State* luaVM = lua_open(0); if (NULL == luaVM) { printf("Error Initializing lua\n"); return -1; } // Do stuff with lua code. char* strLuaInput = "a = 1 + 1;\n"; lua_dostring(luaVM, strLuaInput); lua_close(luaVM); return 0; } Running and compiling this app returns the following:
That's really, really … useless. So how do we go about making it less useless? Well, if you've gone out and downloaded lua, or looked at some lua samples, you'll see that lua has a function called print. So, let's add this into the code and give it a whirl. Here's the code that we would use. #include <stdio.h> extern "C" { #include <lua.h> } int main(int argc, char* argv[ ]) { lua_State* luaVM = lua_open(0); if (NULL == luaVM) { printf("Error Initializing lua\n"); return -1; } // Do stuff with lua code. char* strLuaInput = "a = 1 + 1;\nprint( a);\n"; lua_dostring(luaVM, strLuaInput); lua_close(luaVM); return 0; } Compiling has zero problems. Running it, however is another story all together:
So what's the deal? Did I lie? I mean, first and foremost, it's telling me that 'print' is a nil (read: null) value. So is there actually a function called print()? Well, there is, but it's not defined by the standard lua environment. It's in a library that we are going to have to link into our application. This mechanism is essentially how we are going to extend lua for our own selfish goals. Anyway, to get print(), and some other relevant functions to work, we are going to need more functions linked into the system. We can do that as follows: #include <stdio.h> extern "C" { #include <lua.h> #include <lualib.h> } int main(int argc, char* argv[ ]) { lua_State* luaVM = lua_open(0); if (NULL == luaVM) { printf("Error Initializing lua\n"); return -1; } // initialize lua standard library functions lua_baselibopen(luaVM); lua_iolibopen(luaVM); lua_strlibopen(luaVM); lua_mathlibopen(luaVM); // Do stuff with lua code. char* strLuaInput = "a = 1 + 1;\nprint( a);\n"; lua_dostring(luaVM, strLuaInput); lua_close(luaVM); return 0; } Running the code now produces a real result:
I can also clean up the output by changing the instructions sent to lua: #include <stdio.h> extern "C" { #include <lua.h> #include <lualib.h> } int main(int argc, char* argv[ ]) { lua_State* luaVM = lua_open(0); if (NULL == luaVM) { printf("Error Initializing lua\n"); return -1; } // initialize lua standard library functions lua_baselibopen(luaVM); lua_iolibopen(luaVM); lua_strlibopen(luaVM); lua_mathlibopen(luaVM); // Do stuff with lua code. char* strLuaInput = "a = 1 + 1;\nprint( \"1 + 1: \" .. a);\n"; lua_dostring(luaVM, strLuaInput); lua_close(luaVM); return 0; } Simply put, this outputs the summation of 1+1. So what I've done is create a simple, yet effective illustration of getting a scripting language into your app. This final source can be found in the included workspace as a project named SimpleInterpreter. It's a relatively simple matter to take this rudimentary example and build a simple interpreter with it. All that we need to do that this point is add some text capturing facilities. I've done this in the project FunctionalInterpreter. This is a rudimentary interpreter, and is nothing other than a simple extension of the project SimpleInterpreter. I won't go over the code here. It's simply an extension of what I've shown so far. |
|