NETB131 Programming Project
Programming language Lua
An overview


Marin Ralinov Ralinov, F38714


1. History and Special Features

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. Some of its closest relatives include Icon for its design and Python for its ease of use by non-programmers. In an article published in Dr. Dobb's Journal, Lua's creators also state that Lisp and Scheme with their single, ubiquitous data structure mechanism (the list) were a major influence on their decision to develop the table as the primary data structure of Lua.

Lua has been used the most in video game development.

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 metatables. Similarly, Lua allows programmers to implement namespaces, classes, and other related features using its single table implementation; first class functions 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. Lua is a dynamically typed language 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 arrays, sets, hash tables, lists, and records can be represented using Lua's single native data structure, the table, which is essentially a heterogeneous map. Lua has no built-in support for namespaces and object-oriented programming. Instead, metatable and metamethods are used to extend the language to support both programming paradigms in an elegant and straight-forward manner. Lua implements a small set of advanced features such as higher-order functions, garbage collection, first-class functions, closures, proper tail calls, coercion (automatic conversion between string and number values at run time), coroutines (cooperative multitasking) and dynamic module loading. By including only a minimum set of data types, Lua attempts to strike a balance between power and size.


2. "Hello World" Program



print "Hello World!"


3. Fundamental Data Types (integer, floating point, string) and Assignment Operator

Lua is a dynamically typed language. This means that variables do not have types; only values do. There are no type definitions in the language. All values carry their own type.
All values in Lua are first-class values. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results.

There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.
Nil is the type of the value nil, whose main property is to be different from any other value; it usually represents the absence of a useful value.
Boolean is the type of the values false and true. Both nil and false make a condition false; any other value makes it true.
Number represents real (double-precision floating-point) numbers. (It is easy to build Lua interpreters that use other internal representations for numbers, such as single-precision float or long integers; see file luaconf.h.)
String represents arrays of characters. Lua is 8-bit clean: strings may contain any 8-bit character, including embedded zeros ('\0').

The type function gives the type name of a given value:



print(type("Hello world"))  --> string
print(type(10.4*3))         --> number
print(type(print))          --> function
print(type(type))           --> function
print(type(true))           --> boolean
print(type(nil))            --> nil
print(type(type(X)))        --> string


The last example will result in "string" no matter the value of X, because the result of type is always a string.


Assign Operator:
= operator assigns the value of the expression on the right to the variable on the left.

total = pennies * 0.01
total = count * 0.05 + total



4. Basic Control Flow (conditional and loop statements)

    Conditional operator:
if condition then operator
// optional
else operator
    Loop operator:
while condition do 
operator
end
Task: Input an integer number n and output the sum: 1+22+32+...+n2. Use input validation for n to be positive.


n = io.read()
n = tonumber(n)

if (n > 0) then
sum = 0
i = 0
while
(i <= n)
do
sum = sum + (i * i)
i = i + 1
end
print(sum)
end


5. Functions - syntax, writing and using functions, example

The following is function's format:
function name ( parameter1, parameter2, ...) 
statements
end
where:     Function example:



// function definition
function addition (a, b)
r = a + b
return r
end

// invoking the function
z = addition (5,3);
print ("The result is " .. z);


6. Arrays - syntax, definition, example

Arrays in LUA are declared by the following way:
name = {}

and they are accessed like:
name[position];
where:
    Array example:


//we declare the array and the variable for the loop
a = {}
i = 0
for i = 1, 10 do
a[i] = i*i
end


Note: Arrays in Lua are indexed from 1 instead from 0, unlike in most programming languages.

7. Compilers

Since Lua is an extension programming language it doesn't have it's own compiler - the code written in Lua could be compiled in many other languages such as C, C++, Python, Ruby, Java, etc. by including the Lua libraries...
You can download an interpretter from the official site to test the functions that come with the language, but you still can't make an exeutable file with it (at least not under Windows - in Linux you can do it in a way similar to the bash executable scripts).


8. Projects and Software Written in Lua


Detailed info about the use of Lua in every mentioned project could be found here.

9. Standard

The latest version of Lua is 5.1.2 and was released at 02 Apr 2007.
Information about the older versions could be found here.

10. References

[1] Lua.org, Lua 5.1 Reference Manual
[2] Lua in Wikipedia
[3] Programming in Lua E-Book