Introduction to Lua Tutorials

LSP is a programming language which combines HTML and Lua. In order to understand LSP, you need to understand Lua. The following tutorials, starting with a 'Lua types' tutorial, provide an introduction to Lua.

Note: some examples depend on declarations defined in the previous example(s) and will fail if not executed in order.

Lua Types Tutorial

This is a brief introduction to the eight basic types of values in Lua: number, string, boolean, table, function, nil, userdata, ba.thread. Each section introduces a different type.

We will use the print() function to print out values or calculations on those values.

Numbers

Lua allows simple arithmetic on numbers using the usual operators to add, subtract, multiply, and divide.

Notice that the numbers are not rounded into integers. They are floating point or real numbers. We can assign values to variables using the = operator.

The variable x is created when the number 7 is assigned to it. We use the print() function again to print out the value of x. We can now use the value in x for other calculations.

Notice how print(x*2) does not change the value of x because it was not assigned using the =, but x = x * 9 multiplies the current value of x by 9 and stores the new value in x again.

Strings

Lua also uses strings (i.e. text) types:

print("hello")

We can assign strings to variables just like we can numbers:

We can concatenate (join together) strings using the .. operator between two strings.

Notice that the .. operator does not change the value of message unless the = assignment operator is used just like numbers.

Unlike some other languages, you cannot use the + operator to concatenate strings. The following code produces an error message:

Boolean

Boolean values have either the value true or false. If a value is not true, it must be false and vice versa. The not operator can be placed before a boolean value to invert it; i.e. not true is equal to false.

Boolean values are used to represent the results of logic tests. The equals == and not equals ~= operators will return boolean values depending on the values supplied to them.

Tables

Lua has a general-purpose aggregate datatype called a table. Aggregate data types are used for storing collections (such as lists, sets, arrays, and associative arrays) containing other objects (including numbers, strings, or even other aggregates). Lua is a unique language in that tables are used for representing most all other aggregate types.

Tables are created using a pair of curly brackets {} . Let's create an empty table:

Notice how the print function prints the address of the table.

We can construct tables containing other objects such as the numbers and strings described above:

We can print the values out using the notation: table.item. We can also put tables inside other tables.

Functions

In Lua, functions are assigned to variables just like numbers and strings. Functions are created using the function keyword. Here we create a simple function which will print a friendly message.

Since a function is a value just like any other, we should be able to assign functions to variables just like the other values, and we can:

The ability to do this is because Lua has first class values. This means that all values are treated the same way. This is a very powerful and useful feature of Lua.

nil values

nil is a special value which indicates no value. If a variable has the value nil, then it has no value assigned to it and will therefore no longer exist (or doesn't exist yet). By setting a variable to nil, you can delete a variable.

You can test to see if a variable exists by checking whether its value is nil.

Userdata

Userdata values are objects foreign to Lua such as objects implemented in C. These typically come about when an object in a C library is exposed to Lua. An example of a userdata value is a file handle. A userdata often behaves like a table, and you can largely ignore the difference unless you are implementing one. Userdata is a more advanced topic discussed further in the Lua Reference Manual.

Thread

A thread value represents an independent (cooperative) thread of execution. These are discussed further in CoroutinesTutorial.

Dynamic typing

You might have noticed that while we created the above variables, we did not have to specify which type of variable we were creating.

In other languages such as C, we have to specify the type of a variable when we create it. In Lua, we can also assign different types of values to the same variable.

This is called dynamic typing. This means that you do not have to specify what type a variable is. The variable knows what type it is from the value or object assigned to it.

Querying type

As Lua is a reflective language, we can use the Lua function type() to get a description of the type of a particular object.

Next Tutorial