Lua

Lua is a free language of script whose interpreter is designed with an aim of compactness (95 to 185 KB for version 5.0.2, according to the compiler used and the target system). Lua is conceived so as to be able to be embarked within an other application, which makes it possible to extend this one. Lua (which means the Moon in Portuguese) was developed by Luiz Henrique de Figueiredo, Roberto Ierusalimschy and Waldemar Those, members of the group of TeCGraf research, the Université of Rio de Janeiro with the Brésil.

Lua is written in language C strict ANSI, and of this fact is compilable on a large variety of systems. It is often used in embarked systems where its compactness is very appreciated. It benefits from the compatibility which C with a great number of languages has to be integrated easily in the majority of the projects.

Lua was related lately to the portable console of Sony, the PSP. It is also the language used for the interface of World off Warcraft and many other plays. One can as note as a version Playstation 2 is in the course of bearing.

General information

Comments

In Lua, the comments begin with -- and finishes at the end of the line. If the first line starts with #! (shebang) it is also treated like a comment. One can make multiple-line comments with syntax -- …

Variables

The variables can contain nil (the zero value, empties), Boolean, a character string, a number, a table or a function. There also exists of other types like userdata or thread less often met. The userdata type is very practical, it corresponds to an object C/C++ defined by the utilisateur : it is thus possible to create a structure C or a C++ class, and to use objects of this type in a Lua script, after having attached to this object a métatable which is given the responsability to call the functions of the C++ class. One can thus imagine to create the classes basic trade of a program out of C, and to code the application itself in Lua, which accelerates its development considerably (not recompiling in the event of programming error…).

By defect, when a variable is declared, it is total with all the environment of execution, except if it is preceded by the keyword local.

The character strings are delimited by simple quotation marks (') or doubles ("), even the mark of chain litérale (multiple-line, not of special character). The operator of concatenation of chains is .

Functions

The functions are regarded as variables. That brings large a flexibilité : it is for example possible to create local functions with another function, to pass a function like parameter of another function (or like value of retour !) or to put functions in tables. If one assigns nil to a function (eg. print = nil), the function does not exist any more, is not appealable any more.

An advantage of the functions in Lua is that they can take an indefinite number of parameters but especially they can turn over several values .

Tables, métatables

The tables are a Lua structure which make it possible to store variables indexed by a key (as in for example). The tables are used for many things in Lua, it is for example the structure used to represent pseudo-classes and objects.

The métatables are tables for which one associated a function with each core operator (+, -, (),…). This mechanism is very close to the redefinition of the operators in C++. For example, if variable X contains a table associated with a suitable métatable, the call of function X (arguments) will be valid because Lua will seek then in the métatable how to treat this call.

Thus one can implement the heritage between tables. If a field is not found when he is asked, Lua will seek in the métatable which table is relationship and will require of the table relationship to provide the suitable element.

Examples

Condition

yew condition1 then
 -- instructions carried out if condition 1 is true
elseif condition2 then
 -- instructions carried out if condition 2 is true and condition 1 is false
else
 -- instructions carried out if the two conditions are false
end

Loops

repeat
 -- code
until condition
and
while condition C
 -- code
end
like
for VAr = start, end, step C
 -- code
end
or
for var_1,…, var_n in explist C
 -- code
end

Declaration of functions

function mafonction (arg1, arg2,…)
 -- code
 return résultat1, résultat2
end
-- or --
mafonction = function (arg1, arg2,…)
 -- code
 return résultat1, résultat2
end

Calls of function

res1, res2 = mafonction (VAr)

Unconditional blocks

The unconditional blocks are used for example to declare variables local with this bloc :
C
 room mavariable = " chaîne" -- declaration of variable, optional
 -- code using mavariable
end

Declaration of table

Count with numerical indexes growing (first indice : 1)
a = {5, " foo" , C:\Lua\Lua.exe, “bar”, 42} 
Count with textual indices -- the keys having the syntax of a variable do not need to be between quotes
a = {D = 5, S = " foo" , with espace' = true} 
Count storing variables complexes : other cross tables, functions, references…
fonction = function (has, b) return (has + b)/A * B end
T =
{
	B =
	{
		-- Function like key
		 = “Function! ”,
		-- Numerical index
		 = 42,
		-- Index chains simple (IE. syntax of a variable)
		ls = Value
Multiple-line,
		-- Index chains unspecified
		 rational =,
		 [[C:\Lua\Lua.exe]] = true,
	},
	-- Storage of a function
	F = function () return math.random (100) end,
	FF = function,
	-- Count like key
	 {“has”, “B”} = {" aa" , " bb" },
}
-- Reference in the table
t.reference = t.a
T {t.a, t.b} = t.f

External bonds

  • Official site of Lua

  • Site of the users of Lua
  • Official site of Lua related to the PSP
  • LuaForge, a service of lodging for the draft reports with Lua
  • XtreamLua, French community of the LUA on PSP and Nintendo DS.

Random links:Saint-Sebastien (Digs) | Louise de Montpensier | Didz Hammond | Grand boulevards (Grenoble) | Shapefile | Le_parc_national_de_paumes