Prof. Dr. Jürgen Döllner


Lua is an extension programming language designed to support general procedural programming with data description facilities. It also offers good support for object-oriented programming, functional programming, and data-driven programming. Lua is intended to be used as a powerful, light-weight scripting language for any program that needs one. Lua is implemented as a library, written in clean C (that is, in the common subset of ANSI C and C++).

Being an extension language, Lua has no notion of a "main" program: it only works embedded in a host client, called the embedding program or simply the host. This host program can invoke functions to execute a piece of Lua code, can write and read Lua variables, and can register C functions to be called by Lua code. Through the use of C functions, Lua can be augmented to cope with a wide range of different domains, thus creating customized programming languages sharing a syntactical framework. The Lua distribution includes a sample host program called lua, which uses the Lua library to offer a complete, stand-alone Lua interpreter.

Lua is free software, and is provided as usual with no guarantees, as stated in its license. The implementation described in this manual is available at Lua's official web site, www.lua.org.

Like any other reference manual, this document is dry in places. For a discussion of the decisions behind the design of Lua, see the technical papers available at Lua's web site. For a detailed introduction to programming in Lua, see Roberto's book, Programming in Lua (Second Edition).

Lua Basics

Virtually anyone with any kind of programming experience should find Lua's syntax concise and easy to read. Two dashes introduce comments. An end statement delimits control structures (if, for, while). All variables are global unless explicitly declared local. Lua's fundamental data types include numbers (typically represented as double-precision floating-point values), strings, and Booleans. Lua has true and false as keywords; any expression that does not evaluate to nil is true. Note that 0 and arithmetic expressions that evaluate to 0 do not evaluate to nil. Thus Lua considers them as true when you use them as part of a conditional statement.

Finally, Lua supports userdata as one of its fundamental data types. By definition, a userdata value can hold an ANSI C pointer and thus is useful for passing data references back and forth across the C-Lua boundary.

Despite the small size of the Lua interpreter, the language itself is quite rich. Lua uses subtle but powerful forms of syntactic sugar to allow the language to be used in a natural way in a variety of problem domains, without adding complexity (or size) to the underlying virtual machine. The carefully chosen sugar results in very clean-looking Lua programs that effectively convey the nature of the problem being solved.

The only built-in data structure in Lua is the table. Perl programmers will recognize this as a hash; Python programmers will no doubt see a dictionary. Here are some examples of table usage in Lua:

a = {} -- Initializes an empty table
a[1]   = "Fred" -- Assigns "Fred" to the entry indexed by the number 1
a["1"] = 7 -- Assigns the number 7 to the entry indexed by the string "1"

Any Lua data type can serve as a table index, making tables a very powerful construct in and of themselves. Lua extends the capabilities of the table by providing different syntactic styles for referencing table data. The standard table constructor looks like this:

t = { "Name"="Keith", "Address"="Ballston Lake, New York"}

A table constructor written like

t2 = { "First", "Second","Third"}

is the equivalent of

t3 = { 1="First", 2="Second", 3="Third" }

This last form essentially initializes a table that for all practical purposes behaves as an array. Arrays created in this way have as their first index the integer 1 rather than 0, as is the case in other languages.

The following two forms of accessing the table are equivalent when the table keys are strings:

t3["Name"] = "Keith"
t3.Name    = "Keith"

Tables behave like a standard struct or record when accessed in this fashion.

Variations on a Theme

The following variations on the venerable "Hello World" program illustrate the malleable and extensible nature of the language. First, the minimal implementation:

print("Hello World")

Here's a function declaration that accepts the name of the person to greet. The double "dot" operator does string concatenation:

function sayhello(person)
   print("Hello "..person)

This form of function definition masks a powerful feature of Lua: functions themselves are first-class data types. The underlying form of function definition is actually the following:

sayhello = function(person)
    print("Hello "..person)

You may assign functions to variables, pass them to other functions as parameters, place them in tables, and generally deal with them in the same way as any other Lua value. Functions in Lua do not have names. Rather, you apply the () operator to variables that hold function references in order to call the function. To call a function reference stored in a table element, use the following:

t = {}
function t.sayhello(name)
    print (Hello..name)


This starts to look a lot like a method invocation except that there's no self or this reference for the function to access. Lua takes care of this by adding another piece of syntactic sugar, the : operator. Referencing the table element with a : instead of a . causes Lua to automatically pass a parameter, self, to the function. So:

t      = {}
t.Name = Fred

function t:sayhello()
   print (Hello..self.Name)

-- Or, other expressions that mean the same thing: