Lua Syntax
Lua's syntax is small and readable. Blocks are closed with the keyword end, statements don't need semicolons, and whitespace (including newlines and indentation) has no meaning to the interpreter.
local temperature = 23
if temperature > 20 then
print("Warm")
else
print("Cool")
end
for day = 1, 3 do
print("Day " .. day)
endWarm
Day 1
Day 2
Day 3
Statements and chunks
A Lua file (or any piece of code you run) is called a chunk. A chunk is a sequence of statements executed from top to bottom. You can put several statements on one line, optionally separated by ;:
local a = 1 local b = 2 print(a + b)
local c = 3; local d = 4; print(c * d)3
12
Both lines are legal, but one statement per line is far easier to read. Most Lua style guides leave semicolons out.
Blocks
Each control structure opens a block that ends with end:
| Construct | Shape |
|---|---|
if |
if cond then … elseif cond then … else … end |
while |
while cond do … end |
numeric for |
for i = 1, 10 do … end |
generic for |
for k, v in pairs(t) do … end |
repeat |
repeat … until cond (no end!) |
| function | function name(args) … end |
| plain block | do … end |
A do … end block on its own is useful to limit the lifetime of local variables:
local message = "outer"
do
local message = "inner"
print(message)
end
print(message)inner
outer
Names
Identifiers (names of variables and functions) are made of letters, digits and underscores, and must not start with a digit. They are case-sensitive: score, Score and SCORE are three different variables.
Names starting with an underscore followed by capital letters, such as _VERSION or _G, are reserved by convention for Lua itself. A lone _ is commonly used for a value you want to ignore.
Reserved words
These 22 words can't be used as names:
and break do else elseif end
false for function goto if in
local nil not or repeat return
then true until whileExpressions vs statements
Unlike C or JavaScript, an expression on its own is not a valid statement. x + 1 on a line by itself is a syntax error; only assignments, function calls and control structures can stand alone. This is also why x = y = 0 doesn't work in Lua: assignment is a statement, not an expression.
local x, y = 0, 0 -- assign several variables at once instead
print(x, y)
local ok, err = load("x + 1")
print(ok, err)0 0
nil [string "x + 1"]:1: syntax error near '+'
load compiles a string of Lua code at run time. It returns nil and an error message when the code doesn't compile, which lets us show a syntax error without stopping the program.
Calling functions without parentheses
When a function is called with a single literal string or table, the parentheses can be dropped. You will see this a lot in configuration files and DSLs:
print "no parentheses needed"
print [[a long string literal]]
print(type {})no parentheses needed
a long string literal
table
Test yourself
How is an if block closed in Lua?
}fiend- Indentation
Which loop does NOT end with the keyword end?
while- numeric
for repeat … until- generic
for
Are Count and count the same variable?
- Yes
- No, Lua is case-sensitive
- Only for globals
- Only for locals