Lua is, according to its creators:

...a powerful, light-weight programming language designed for extending applications. Lua is also frequently used as a general-purpose, stand-alone language. Lua is free software.

I have found out about Lua a few hours ago, looking for a simple scripting language to embed in a game I am (trying) to write... It seems that everyone's beaten me to the idea of using Lua; Baldur's Gate, Grim Fandango, MDK2 and ClanLib already use it.

Anyway, I'm beginning to like it. It has got a clear syntax which isn't directly derived from C's (don't get me wrong, I love C, but everyone uses its syntax), and it has a lot of nice features. It provides features to add new features to the language, mostly using what it calls tag methods, which are methods issued on objects during the execution of the program, like assignments, comparisons et al.

Apparently, you can make Lua object-oriented with those, even though it isn't out-of-the-box.

Of course, it is very easily integrated into existing C programs, as you link to a library and add functions to it via a call-back interface.

It was made by TeCGraf, the Computer Graphics Technology Group of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro in Brazil), a laboratory of the Department of Computer Science.

And, as they say... Lua means Moon in Portuguese

More info at: http://www.tecgraf.puc-rio.br/lua/

Lua (http://www.tecgraf.puc-rio.br/lua/home.html) is a programming language extension which has several nice features. For one thing, a variable is not restricted to being of any one type - for example, the pair of statements
V = 7.2
V = "groovy"
does not cause an error. In most programming languages, V must be declared to be a string (in which case the first statement would cause an error, unless some unusual casting were going on) or a number (in which case the second statement would cause an error). In Lua, V does not have to be declared at all, except when the programmer wishes to declare it as local. Any variable that has not had a value explicitly put into it, contains the value nil. nil is not a number, or a string, or anything else - just a value meaning 'uninitialised'. Of course, nil can be assigned to any variable also; a variable that has had 5 assigned to it and then has had nil assigned to it is indistinguishable from a variable that has never had a value written to it at all.

The really innovative part of the language, though, is the table data structure. The table in Lua is like the array in many other programming languages, except to index into the array you can use not only numbers but strings too! Again, any index in the table which has not had a value put into it has the value nil. Thus, sets can be easily represented in Lua tables. To make an empty set, use:
Set = {}
To add "red" to the set, use something like:
Set["red"] = 0
(any value other than nil will do.) To remove "blue" from the set, use:
Set["blue"] = nil
Simply, X (a string) is in the set iff Set[X] is not nil. A bag can be almost as easily represented, Bag[X] being nil or 0 if there are no Xs in the bag, and a positive integer representing the number of Xs in the bag if there are any. Trees are easy to represent, too:
Tree["left"] = LeftSubtree
Tree["right"] = RightSubtree
where LeftSubtree and RightSubtree are trees themselves.

Another feature is that functions are first-level values. This has the implication that variable identifiers and function identifiers share the same namespace, so one cannot have a variable and a function with the same name. But it also allows for some object-oriented mimicry; an object is a table which has methods for entries! For example:
Object.Increment = function ()
  Object.Value = (Object.Value + 1)
  return (Object.Value)
end
will define the entry "Increment" in the table "Object" to be a function which increments the "Value" entry in "Object", and returns that value. (A.B is syntactic sugar for A["B"].) There is more syntactic sugar to make the mimicry even more convincing:
function A:B(parameters)
code
end
translates to:
A.B = function (self, parmeters)
code
end
and A:B(parameters) in any other context translates to A.B(A, parameters).

Finally, Lua has a facility called fallbacks. Since variables are not statically typed, unusual run-time events can occur, such as performing an arithmetic operation on a table. Lua allows for events like these to be trapped. A particular type of fallback can be used to simulate inheritance: if a method for an object is invoked and it is not defined, a fallback can be defined to check the object's "parent" (i.e., object.Parent) for the method.

Lua is an intriguing programming language extension. I like it a lot. Give it a spin. Currently, interpreters are available for Unix, Windows, Power Macintosh, Mac OS 9.1, Mac OS X, Acorn RISC OS, EPOC, BeOS, and Palm.


Below is an extract from the Lua documentation:

Copyright (C) 1994-2000 TeCGraf, PUC-Rio. All rights reserved.

Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, translate, and distribute this software and its documentation (hereby called the "package") for any purpose, including commercial applications, subject to the following conditions:

  • The above copyright notice and this permission notice shall appear in all copies or substantial portions of this package.
  • The origin of this package must not be misrepresented; you must not claim that you wrote the original package. If you use this package in a product, an acknowledgement in the product documentation would be greatly appreciated (but it is not required).
  • Altered source versions must be plainly marked as such, and must not be misrepresented as being the original package.

The Complete Syntax of Lua

chunk → {stat [';']}
blockchunk
statvarlist1 '=' explist1 | functioncall | do block end | while exp1 do block end | repeat block until exp1 | if exp1 then block {elseif exp1 then block} [else block] end | return [explist1] | break | for name '=' exp1 ',' exp1 [',' exp1] do block end | for name ',' name in exp1 do block end | function funcname '(' [parlist1] ')' block end | local declist [init]
funcnamename | name '.' name | name ':' name
varlist1var {',' var}
varname | varorfunc '[' exp1 ']' | varorfunc '.' name
varorfuncvar | functioncall
declistname {',' name}
init → '=' explist1
explist1 → {exp1 ','} exp
exp1exp
expnil | number | literal | var | function | upvalue | functioncall | tableconstructor | '(' exp ')' | exp binop exp | unop exp
functioncallvarorfunc args | varorfunc ':' name args
args → '(' [explist1] ')' | tableconstructor | literal
functionfunction '(' [parlist1] ')' block end
parlist1 → '...' | name {',' name} [',' '...']
upvalue → '%' name
tableconstructor → '{' fieldlist '}'
fieldlistlfieldlist | ffieldlist | lfieldlist ';' ffieldlist | ffieldlist ';' lfieldlist
lfieldlist → [lfieldlist1]
ffieldlist → [ffieldlist1]
lfieldlist1exp {',' exp} [',']
ffieldlist1ffield {',' ffield} [',']
ffield → '[' exp ']' '=' exp | name '=' exp
binop → '+' | '-' | '*' | '/' | '^' | '..' | '<' | '<=' | '>' | '>=' | '==' | '~=' | and | or
unop → '-' | not


00100 says re Lua: Lua's scalar variables sound much like perl's in that they are not restricted to a single category of data. Also, the table data structure you describe is very similar to perl's hash data type. I don't know much about perl, but I do know it is a very large language. Lua is not. I like the look of Lua's simplicity a lot more than the look of perl, but of course this is just one programmer's opinion.

Log in or register to write something here or to contact authors.