Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Crash course on Lua

Lluís Ribas-Xirgo
Course 2020-2021
Universitat Autònoma de Barcelona
Lua.org
• Lua is an scripting language
• Interpreter written in C
• Embeddable on any platform (small footprint and memory requirements)
• Like Python, Javascript, …

• Example [https://www.lua.org/demo.html]
• name = "world" -- variables defined on the fly, nil by default
• io.write("Hello, ", name, "!\n") -- lots of modules, like the input/output

• IDE, ZeroBrane Studio [https://studio.zerobrane.com/download]

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 2
Lua language
• Reserved words and strings
• Keywords
• and break do else elseif end
• false for function goto if in
• local nil not or repeat return
• then true until while
• Other tokens
• + - * / % ^ #
• & ~ | << >> //
• == ~= <= >= < > =
• ( ) { } [ ] ::
• ; : , . .. ...
• String delimiters –single and double quotes– and special characters
• '\b' (backspace), '\f' (form feed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\v' (vertical tab),
'\\' (backslash), '\"' (quotation mark), and '\'' (apostrophe)

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 3
Lua, execution model
• Lua interpreter reads source code by chunks
• A chunk is a sequence of instructions that shares a local environment
• Conditional (if), loop (while) and function bodies are chunks
• Each chunk can have its own local variables, not accessible outside
• Main program is a chunk, though its variables are global
• How a program is executed
• Instruction pointer, IP = first instruction of the source code
• Reads instruction at IP and executes it
• Any new variable is assigned a nil value in the global or current local environment
• Let left-side variables be the computed values of right-side expressions
• In case of conditionals and loops, if result of expression is true,
open (1st time) or keep current local environment
• Otherwise, jump to first instruction outside instruction body
• In case of function calls, open new local environment, and
proceed (i.e. jump) to their parameter assignment instructions
• Set IP to next instruction or, in case of jumps, to destination instruction

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 4
Lua, execution model
• How a program is executed 1. io.write( "Adding " )
2. A = tonumber( io.read() )
• Instruction pointer,
IP = first instruction of the source code 3. if A == nil then
4. A=0
• Reads instruction at IP and executes it 5. end -- if
• Any new variable is assigned a nil value 6. io.write( "to " )
in the global or current local environment
• Let left-side variables be 7. B = tonumber( io.read() )
the computed values of right-side expressions 8. if B == nil then
• In case of conditionals and loops,
if result of expression is true, 9. B=0
open (1st time) or keep current local environment
• Otherwise, 10. end -- if
jump to first instruction outside instruction body 11. add = function( O1, O2 )
• In case of function calls,
open new local environment, and 12. local R = O1 + O2
proceed (i.e. jump) to their 13. return R
parameter assignment instructions
• Set IP to next instruction or, 14. end -- add
in case of jumps, to destination instruction
15. C = add( A, B )
16. io.write( "gives ", C, "\n" )

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 5
Lua, variables
• Variables are containers of
• nil
• true or false
• Numbers
• math.abs(x), math.ceil(x), math.floor(x), math.sqrt(x), …
• Strings
• Concatenation .. (two points)
• string.format( "gives %g\n", C )
• string.sub (s, i [, j])
• Tables
• Functions

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 6
Lua, exercise 1
• Read a series of numbers until a zero is input. At every input, the
cumulative sum of previous inputs have to be given
• Example
Accumulator
0 + ... 23.99
23.99 + ... 3.95
27.94 + ... 4.49
32.43 + ... -4.49
27.94 + ... 0

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 7
Lua, exercise 1
• Read a series of numbers until a zero is input. At every input, the
cumulative sum of previous inputs have to be given
• Example 1. io.write( "Accumulator\n" )
Accumulator 2. sum = 0
0 + ... 23.99 3. a = 1
4. while a ~= 0 do
23.99 + ... 3.95 5. io.write( string.format( "%g + ... ", sum ))
27.94 + ... 4.49 6. a = tonumber( io.read() )
7. if a == nil then
32.43 + ... -4.49 8. a=0
27.94 + ... 0 9. end -- if
10. sum = sum + a
11. end -- while

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 8
Lua, tables
• Tables are lists of objects of any class
• Empty table, t = {}
• Implicit indexes start at 1 and stop at #t (table length)
• t = {}; t[1] = "First"; t[2] = 0.2 -- is equivalent to:
• t = { "First", 0.2 }
• Explicit indexes can be of any type
• t["A"] = 65 -- which is equivalent to:
• t.A = 65

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 9
Lua, exercise 2
• Read a series of numbers until a zero is input and store them into a
table. At the end, print the number of items in the table and their
sum.
• Example
Accumulator 2
item 1 = 23.99
item 2 = 3.95
item 3 = 0
Sum of 2 items = 27.94

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 10
Lua, exercise 2
• Read a series of numbers until a zero is input and store them into a
table. At the end, print the number of items in the table and their
sum. 1. io.write( "Accumulator 2\n" )
2. t = {}
• Example 3. a = 1; i = 1
4. while a ~= 0 do
Accumulator 2 5. io.write( string.format( "item %d = ", i ))
item 1 = 23.99 6. a = tonumber( io.read() )
7. if a == nil then a = 0 end
item 2 = 3.95 8. if a ~= 0 then t[i] = a; i = i + 1 end
item 3 = 0 9. end -- while
10.sum = 0; i = 1
Sum of 2 items = 27.94 11.while i <= #t do sum = sum + t[i]; i = i + 1 end
12.io.write( string.format(
13. "Sum of %d items = %g\n", #t, sum
14. ))

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 11
Lua, files
• Open a file
• file = io.open( filename, mode )
• Mode = "r" -- read, "w" -- read, "a" -- append
• Operations with file
• file:read() -- file.read(file)
• file:write(str) -- file.write(file, str)
• file:close() -- file.close(file)

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 12
Lua, exercise 2
• Read a series of numbers until a zero is input and store them into a
table. At the end, print the number of items in the table and their
sum… and store the table in “ticket.txt”
• Example
Accumulator 2
item 1 = 23.99
item 2 = 3.95
item 3 = 0
Sum of 2 items = 27.94

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 13
Lua, exercise 2
• Read a series of numbers until a zero is input and store them into a
table. At the end, print the number of items in the table and their
sum… and store the table in “ticket.txt”
• Example
Accumulator 2
15.file = io.open("ticket.txt", "w")
item 1 = 23.99 16.i = 1
item 2 = 3.95 17.while i <= #t do
18. file:write(t[i], "\n")
item 3 = 0 19. i = i + 1
Sum of 2 items = 27.94 20.end -- while
21.file:close()

© 2020 Lluís Ribas i Xirgo, Universitat Autònoma de Barcelona 104544 Sistemes ciberfísics, 14

You might also like