Lua is a script language. It is light weight and easily embedded in C. Below is a code snippet that embeds Lua and retrieves result back from the script.
And the script used from above C++ code can be like below.
To further assist the embedding and interacting with C/C++, there is Luabind. You can make method and class binded to Lua script. And it also provides utility class that hide the details of stack.
Refer below code snippet to include Luabind in the C++ code.
Lua syntax is quite simple and intuitive. It can be applied to a configuration file and can be a good alternative to the INI. Refer below. Each name value pair can be string, real number, boolean or table. A name represents a table if it contains another sets of name-value pair like variable 'b' at below.
It is not necessary to enclose configuration items with 'config={ }'. Any variable can be defined at the highest level - global level. But there are other global variables used in Lua - e.g. string, preload, package, os, setmetable and so on - which cause problem when we want to iterate through config items. If we choose to use a fixed name, then it will be straight forward on iteration. Just find the 'config' variable and iterate through all variables inside of it. It will be shown in code later.
With above code, you can simply access each variable presumed that you know the variable names and hierachy.
But what if you want to access configuration like SAX does ? There should be a way to iterate through configuration items. Lua can iterate through items in the table, and we can make the script calls you with predefined interface. Here is code snippet that can do it.
essay on programming languages, computer science, information techonlogies and all.
Saturday, February 1, 2014
Subscribe to:
Posts (Atom)