| Lua Programming Language |
Article Index for Lua |
Website Links For Lua |
Information AboutLua Programming Language |
The Lua (pronounced LOO-ah, or in IPA ) Programming Language is a lightweight, Reflective , Imperative and Procedural language, designed as a Scripting Language with Extensible Semantics as a primary goal. The name is derived from the Portuguese word for ''moon''. PHILOSOPHY Lua is commonly described as a "multi- Paradigm " language, providing a small set of general features that can be extended to fit different problem types, rather than providing a more complex and rigid specification to match a single paradigm. Lua, for instance, does not contain explicit support for Inheritance , but allows it to be implemented relatively easily with fallbacks. Similarly, Lua allows programmers to implement Namespaces , Classes , and other related features using its single table implementation; First Class Function s allow the employment of many powerful techniques from Functional Programming ; and full lexical scoping allows fine-grained Information Hiding to enforce the Principle Of Least Privilege . In general, Lua strives to provide flexible meta-features that can be extended as needed, rather than supply a feature-set specific to one programming paradigm. As a result, the base language is light—in fact, the full reference Interpreter is only about 150KB compiled—and easily adaptable to a broad range of applications. HISTORY Lua was created in 1993 by Roberto Ierusalimschy , Luiz Henrique De Figueiredo , and Waldemar Celes , members of the Computer Graphics Technology Group at PUC-Rio , the Pontifical University of Rio De Janeiro , in Brazil . Versions of Lua prior to version 5.0 were released under a license similar to the BSD License . From version 5.0 onwards, Lua has been licensed under the MIT License . Lua has been used in many commercial applications (e.g., in LucasArts ' '' Escape From Monkey Island '' Adventure Game , '' World Of Warcraft '', robot control software, Homeworld 2 , and Adobe Lightroom ) as well as non-commercial applications (like '' Angband '' and its variants). A ported version of Lua has been used to program Homebrew for the Playstation Portable and then Nintendo DS . Some of its closest relatives include Icon for its design and Python for its ease of use by non-programmers. FEATURES Lua is intended for use as an Extension or Scripting language, and is compact enough to fit on a variety of host platforms. It supports only a small number of atomic data structures such as Boolean values, Numbers (double-precision Floating Point by default), and Strings . Typical data structures such as Array s, Sets , Hash Table s, List s, and Record s can be represented using Lua's single native data structure, the table, which is essentially a heterogeneous Map . Namespaces and objects can also be created using tables. By including only a minimum of data types, Lua attempts to strike a balance between power and size. Lua's semantics can be extended and modified by redefining certain built-in functions in metatables. Furthermore, Lua supports advanced features such as Higher-order Function s and Garbage Collection . By combining many of its features, it is possible to write Object-oriented programs in Lua. Example code The classic Hello World Program can be written as follows: print "Hello, world!" The Factorial is an example of a Recursive function: function factorial(n) if n == 0 then return 1 end
end Lua's treatment of functions as first class variables is shown in the following example, where the print function's behavior is modified: do local oldprint = print -- store current print function as old print print = function(s) -- redefine print function if s == "foo" then oldprint("bar") else oldprint(s) end end end Any future calls to "print" will now be routed through the new function, and thanks to Lua's Lexical Scoping , the old print function will only be accessible by the new, modified print. Extensible semantics is a key feature of Lua, and the "metatable" concept allows Lua's tables to be customized in powerful and unique ways. The following example demonstrates an "infinite" table. For any n, fibs {Link without Title} will give the nth Fibonacci Number using Dynamic Programming . fibs = { 1, 1 } -- Initial values for fibs and fibs[2 . setmetatable(fibs, { -- Give fibs some magic behavior. __index = function(fibs,n) -- Call this function if fibs {Link without Title} does not exist. fibs = fibs[n-2 + fibs[n-1] -- Calculate and memorize fibs[n]. return fibs {Link without Title} end }) Tables Tables are powerful and the most important data-structuring tool in Lua. In fact, it is the only way to create data structures in Lua. The table is a collection of key and data pairs (known also as Hashed Heterogeneous Associative Array ), where the data is referenced by key. The key (index) can be of any data type except nil. Table as structure Tables are often used as Structure s (or objects) by using Strings as keys. Because such use is very common, Lua features a special syntax for accessing such fields. Example: point = { x = 10, y = 20 } -- Create new table print( point {Link without Title} ) -- Prints 10 print( point.x ) -- Has exactly the same meaning as line above Table as array By using a numerical key, the table resembles an Array data type. A simple array of the strings: array = { "a", "b", "c", "d" } -- Indexes are assigned automatically print( array {Link without Title} ) -- Prints "b" An array of objects: function Point(x,y) -- "Point" object Constructor return {x = x, y = y} -- Creates and returns a new object (table) end array = { Point(10,20), Point(30,40), Point(50,60) } -- Creates array of points print( array {Link without Title} .y ) -- Prints 40 Object-oriented programing Although Lua does not have a built-in concept of s and tables. By simply placing functions and related data into a table, an object is formed. Inheritance (both single and multiple) can be implemented via the "metatable" mechanism, telling the object to lookup nonexistent methods and field in parent object(s). Note that there is no such concept as "class" with these techniques, rather "prototypes" are used as in Self Programming Language . New objects are created either with a Factory Method (that constructs new objects from scratch) or by cloning an existing object. INTERNALS Lua programs are not Interpreted directly, but are Compiled to Bytecode which is then run on the Lua Virtual Machine . The compilation process is typically transparent to the user and is performed during Run-time , but it can be done offline in order to increase performance or reduce the memory footprint of the host environment by leaving out the compiler. There is also a third-party Just-in-time Lua run-time, called LuaJIT , for the new 5.1 version (currently in beta stage). This example is the bytecode listing of the factorial function described above (in Lua 5.0): function 1 param, 3 stacks, 0 upvalues, 1 local, 3 constants, 0 functions 1 {Link without Title} EQ 0 0 250 ; compare value to 0 2 {Link without Title} JMP 0 2 ; to line 5 3 {Link without Title} LOADK 1 1 ; 1 4 {Link without Title} RETURN 1 2 0 5 {Link without Title} GETGLOBAL 1 2 ; fact 6 {Link without Title} SUB 2 0 251 ; - 1 7 {Link without Title} CALL 1 2 2 8 {Link without Title} MUL 1 0 1 9 {Link without Title} RETURN 1 2 0 10 {Link without Title} RETURN 0 1 0 APPLICATIONS Lua features prominently in many games, such as '' Far Cry '', a First-person Shooter , '' World Of Warcraft '', a Massively Multiplayer Online Role-playing Game , where users are able to customize its user interface, character animation and world appearance via Lua, and Bioware 's Baldur's Gate Series and '' MDK2 '' Video Games , where it is used as a module scripting language. It also appears in some open-source games such as '' Daimonin '' and the Roguelikes '' ToME '' and '' H-World ''. Lua is implemented in the popular '' Half-Life 2 '' Mod , Garry's Mod , where players can script things such as custom game modes, weapons, HUDs , and more. Examples include a kick/ban gun, an ASCII snake game, and an RPG with player lock and laser tracking. Examples for real-world practices:
A list of projects known to use Lua is located at Lua.org . BOOKS
EXTERNAL LINKS
|
|
|