TExcelTAD
Home › Lua › Lua Syntax

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.

lua · try it yourself
local temperature = 23

if temperature > 20 then
  print("Warm")
else
  print("Cool")
end

for day = 1, 3 do
  print("Day " .. day)
end
Warm
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 ;:

lua · try it yourself
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:

lua · try it yourself
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     while

Expressions 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.

lua · try it yourself
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:

lua · try it yourself
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?

  1. }
  2. fi
  3. end
  4. Indentation

Which loop does NOT end with the keyword end?

  1. while
  2. numeric for
  3. repeat … until
  4. generic for

Are Count and count the same variable?

  1. Yes
  2. No, Lua is case-sensitive
  3. Only for globals
  4. Only for locals