Cours Lua Core Language, tutoriel & guide de travaux pratiques en pdf.
Lua Core Language
Reserved words
and !break !do !else!elseif!end !false!for function!if !in !local !nil!not!or repeat !return !then!true !until !while _A…#A system variable, where A any uppercase letter.
Other reserved strings
+!-!*!/!%!^!#!==!~=!<=!>=!<!>!=!(!)
{!}![!]!;!:!,!.!..!…
Identifiers
Any string of letters, digits and underscores not starting with a digit and not a reserved word. Identifiers starting with underscore and uppercase letter are reserved.
Comments
— #Comment to end of line.
–[[ … ]]#Multi-line comment (commonly –[[ to –]] )
#! #At start of first line for Linux executable.
Types
Type belongs to the value, NOT the variable:
boolean!nil and false count as false, all other true including 0 and null string. Use type(x) to discover type of x.
number! 64 bit IEEE floating point string##Can include zero, internally hashed. table ##Index by numbers, strings function#Can return multiple values thread#A cooperative coroutine.
userdata#C pointer to a C object. Can be assigned a metatable to allow use like a table or function
nil# #A special value meaning “nothing”.
Operators in precedence order
^ #(right-associative, math lib required)
not !# (length)! – (unary negative)(unary positive illegal)
* !/!% +! –
.. #(string concatenation, right-associative)
< !> !<= !>= !~= !==
and #(stops on false or nil, returns last evaluated value)
or #(stops ontrue (not false or nil), returns last evaluated
#value)
Assignment and coercion examples
a = 5#Simple assignment.
a = “hi” #Variables are not typed, they can hold different types.
a, b, c = 1, 2, 3#Multiple assignment.
a, b = b, a #Swap values, because right side values evaluated before assignment.
a, b = 4, 5, 6 #Too many values, 6 is discarded.
a, b = “there” #Too few values, nil is assigned to b.
a = nil # aʼs prior value will be garbage collected if
a = #b#Size of unreferenced elsewhere.
b. If table, first index followed by nil.
a = z #If z is not defined a = nil. a = 5.
a = “3” + “2” #Strings converted to numbers:
a = 3 .. 2 #Numbers are converted to strings: a = « 32 ».
Conditional expression results
False: false and nil values only
True: anything not false, including 0 and empty strings
Relational and boolean examples
“abc” < “abe”#True: based first different character
“ab” < “abc”#True: missing character is less than any
Scope, blocks and chunks By default all variables have global scope from first use.
local#Reduces scope from point of definition to end of block.
local var_name initialized to nil. Locals significantly faster to access block#Is the body of a control structure, body of a function or a chunk.
chunk # A file or string of script.
Control structures
In following exp and var have local scope if exp then block {elseif exp then block} [else block] end
do block end (simply a means of forcing local scope) while exp do block end
repeat block until exp for var = from_exp, to_exp [, step_exp] do block end for var(s) in iterator do block end (var(s) local to loop) break, return exits loop, but must be last statement in block