Lua

Appeared in:
1993
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.lua .lua5
Versions and implementations (Collapse all | Expand all):
Programming language

Lua (from Portuguese “moon”) is a lightweight scripting language with extensible semantics.

Lua was created and is maintained by members of the Pontifical Catholic University of Rio de Janeiro. It has no official standard and is described in its reference manual.

Lua is currently the most popular scripting language in the game industry, and is used in many applications in other software domains.

Since Lua is a relatively new language and was created later than many modern languages, it borrows features and concepts from several languages:

  • syntax of flow control structures — from Modula;
  • semantics of later versions — from Scheme;
  • concept of local variables — from C++;
  • concept of having one native data structure usable in multiple ways — from Lisp;
  • usage of associative arrays — from SNOBOL;
  • multiple assignments and returns from functions — from CLU etc.

The fundamental concept of Lua is extensibility of its semantics, i.e., providing meta-mechanisms for implementing a varying set of tools instead of providing a fixed set of tools. This allows the language to be small and simple while still being powerful. It can be thought of as a multiparadigm language since it allows development in multiple styles.

Lua supports boolean, numeric (default is double-precision floating point) and string atomic data types. The only native complex data structure is the table, which is virtually an associative array, which can be heterogeneous (i.e., have different data types for different pairs of keys and values). Functions are first-class objects in Lua (i.e. they can be manipulated as variables, passed and received as arguments, etc.)

These two main features allow the implementation of many data structures and concepts available in other languages by using different data types for associative array keys and values:

  • structures: use strings (~ field names) as keys and atoms of any required types (~ field values) as values; Lua provides a special syntax which allows referencing field values of such structure by field name.
  • arrays: use integers (~ indices) as keys and atoms of one required type (~ elements of the array) as values.
  • sets: store elements of set either as keys or as values;
  • maps: the natural usage of associative arrays;
  • namespaces: a table can be used to store related functions and variables of certain object domain;
  • prototypes: Lua supports object-oriented paradigm by allowing to store functions and data which describe one class in one table. These are prototypes, not classes, since new objects are created by cloning existing ones or using factory methods.

Lua is compiled into bytecode which is executed on the Lua virtual machine. Lua is a scripting language and is designed to be embedded into other languages, so it provides a C API.

Elements of syntax:

Inline comments --
Nestable comments --[[ ... ]]
Case-sensitivity yes
Variable assignment varname = value
Variable declaration none
Grouping expressions ( ... )
Block do ... end
Physical (shallow) equality ==
Physical (shallow) inequality ~=
Comparison < > <= >=
Function definition function functionName (argname1, ..., argnameN) ... end
Function call functionName(arg1, ..., argN)
Function call with no parameters functionName()
Sequence (whitespace)
If - then if condition then trueBlock end
If - then - else if condition then trueBlock else falseBlock end
While condition do while condition do loopBody end
Do until condition repeat loopBody until condition
For each value in a numeric range, 1 increment for i = first, last do loopBody end
For each value in a numeric range, 1 decrement for i = last, first, -1 do loopBody end

Lua logo
Lua logo

Examples:

Hello, World!:

Example for versions Lua 5.0.3
print("Hello, World!")

Factorial:

Example for versions Lua 5.0.3

This example uses recursive factorial definition.

function factorial(n)
    if (n == 0) then
        return 1
    else
        return n * factorial(n - 1)
    end
end

for n = 0, 16 do
    io.write(n, "! = ", factorial(n), "\n")
end

Fibonacci numbers:

Example for versions Lua 5.0.3

This example uses recursive definition of Fibonacci numbers.

function fibonacci(n)
    if n<3 then
        return 1
    else
        return fibonacci(n-1) + fibonacci(n-2)
    end
end

for n = 1, 16 do
    io.write(fibonacci(n), ", ")
end
io.write("...\n")

Fibonacci numbers:

Example for versions Lua 5.0.3

Numbers which have already been calculated are stored in associative array fib and are retrieved from it to calculate the next ones. By default Lua associative arrays use 1-based integer keys, so fib = {1, 1} creates an array with indices 1 and 2.

fib = {1, 1}
for n = 3, 16 do
    fib[n] = fib[n-1] + fib[n-2]
end
for n = 1, 16 do
    io.write(fib[n], ", ")
end
io.write("...\n")

Quadratic equation:

Example for versions Lua 5.0.3, Lua 5.1.4

*n specifies that a number has to be read. Everything else is quite evident.

local A = io.read('*n')
if A==0 then 
    io.write('Not a quadratic equation.')
    return
end
local B = io.read('*n')
local C = io.read('*n')
D = B*B-4*A*C
if D==0 then
    io.write('x = ', -B/2/A)
else if D>0 then
        io.write('x1 = ', (-B+math.sqrt(D))/2/A, '\nx2 = ', (-B-math.sqrt(D))/2/A)
     else
        io.write('x1 = (', -B/2/A, ',', math.sqrt(-D)/2/A, ')\nx2 = (', -B/2/A, ',', -math.sqrt(-D)/2/A, ')\n')
     end
end