Programming with Multiple Paradigms in Lua, tutoriel Lua document PDF.
Introduction
Lua is an embeddable scripting language used in many industrial applications (e.g., Adobe’s Photoshop Lightroom), with an emphasis on embedded systems and games. It is embedded in devices ranging from cameras (Canon) to keyboards (Logitech G15) to network security appliances (Cisco ASA). In 2003 it was voted the most popular language for scripting games by a poll on the site Gamedev In 2006 it was called a \de facto standard for game scripting » [1]. Lua is also part of the Brazilian standard middleware for digital TV [2].
Data Description
Lua was born from a data-description language, called SOL [8], a language some-what similar to XML in intent. Lua inherited from SOL the support for data description, but integrated that support into its procedural semantics.
SOL was somewhat inspired by BibTeX, a tool for creating and formating lists of bibliographic references [9]. A main dierence between SOL and BibTeX was that SOL had the ability to name and nest declarations. Figure 1 shows a typical fragment of SOL code, slightly adapted to meet the current syntax of Lua. SOL acted like an XML DOM reader, reading the data le and building
an internal tree representing that data; an application then could use an API to traverse that tree.
Lua mostly kept the original SOL syntax, with small changes. The semantics, however, was very dierent.
Functional Programming
Lua oers rst-class functions with lexical scoping. For instance, the following code is valid Lua code:
(function (a,b) print(a+b) end)(10, 20)
It creates an anonymous function that prints the sum of its two parameters and applies that function to arguments 10 and 20.
All functions in Lua are anonymous dynamic values, created at run time.
Lua oers a quite conventional syntax for creating functions, like in the following denition of a factorial function:
function fact (n)
if n <= 1 then return 1
else return n * fact(n – 1)
end
end
However, this syntax is simply sugar for an assignment:
fact = function (n)
…
end
This is quite similar to a define in Scheme [12].
Lua does not oer a letrec primitive. Instead, it relies on assignment to close
a recursive reference. For instance, a strict recursive xed-point operator can be
dened like this:
local Y
Y = function (f)
return function (x)
return f(Y(f))(x)
end
end
Or, using some syntactic sugar, like this:
local function Y (f)
return function (x)
return f(Y(f))(x)
end
end
Object-Oriented Programming
Lua has only one data-structure mechanism, the table. Tables are rst-class, dynamically created associative arrays.
Tables plus rst-class functions already give Lua partial support for objects.
An object may be represented by a table: instance variables are regular table elds and methods are table elds containing functions. In particular, tables have identity. That is, a table is dierent from other tables even if they have the same contents, but it is equal to itself even if it changes its contents over time.
One missing ingredient in the mix of tables with rst-class functions is how to connect method calls with their respective objects. If obj is a table with a method foo and we call obj.foo(), foo will have no reference to obj. We could solve this problem by making foo a closure with an internal reference to obj, but that is expensive, as each object would need its own closure for each of its methods.
A better mechanism would be to pass the receiver as a hidden argument to the method, as most object-oriented languages do. Lua supports this mechanism with a dedicated syntactic sugar, the colon operator : the syntax orb:foo() is sugar for orb.foo(orb), so that the receiver is passed as an extra argument to the method. There is a similar sugar for method denitions.
The syntax:
function obj:foo (…) … end
is sugar for
obj.foo = function (self, …) … end
Goal-Oriented Programming
Goal-oriented programming involves solving a goal that is either a primitive goal or a disjunction of alternative goals. These alternative goals may be, in turn, conjunctions of subgoals that must be satised in succession, each of them giving a partial outcome to the nal result. Two typical examples of goal-oriented programming are text pattern matching [19] and Prolog-like queries [20].
In pattern-matching problems, the primitive goal is the matching of string literals, disjunctions are alternative patterns, and conjunctions represent sequences. In Prolog, the unication process is an example of a primitive goal, a relation constitutes a disjunction, and rules are conjunctions. In those contexts, a problem solver uses a backtracking mechanism that successively tries each alternative until it nds an adequate result.
……..
Programming with Multiple Paradigms in Lua (149 KO) (Cours PDF)