Lua Programming/Syntactical glossary

From Wikibooks, open books for an open world
Jump to navigation Jump to search

This is a glossary of terms that belong to the terminology of syntax. It is oriented toward Lua.

expression
a syntactical element that can be evaluated
statement
a syntactical element that can be executed
operand
an expression
function argument
an expression
function parameter
an identifier
function call
a syntactical element that can be used as both a statement and as an expression. Contains a function expression (an expression that evaluates to a function; generally simply the identifier of a function) and can contain one or many arguments.
table index
expression
table indexation
an expression made of a table expression (an expression that evaluates to a table, generally the table's identifier) and an index
operator
a syntactical element that, combined with one (unary operator), two (binary operator), three (ternary operator) or more operands, forms an expression
condition
an expression (in some languages, this expression must evaluate to a boolean)
function definition
a statement when including the function's name, and an expression when anonymous (when no name is given to the function in the function definition)
table constructor
an expression that creates a table. Can contain expressions (values) or pairs of expressions (keys and values).

The = token in assignment is not an operator because the identifier is not an operand or an expression.

Function calls can be used either as statements or as expressions. They are statements since they can be executed and put on their own line, but they are expressions since they can be evaluated (they can return values). This is why you can put a function call on its own line but also use it in an operation. In some languages, where assignment returns a value, assignment can also be used as either a statement or an expression.

As a result of the above definitions, expressions can be very complicated because they can include other expressions. Statements can include expressions (e.g. the condition in a conditional statement), but can also include other statements (e.g. the body of the count-controlled loop, also known as the for loop). Statements can also contain identifiers.

Example of syntactical analysis[edit | edit source]

for i = 1, 5 do
	var = 4 + foo("this is a " .. {4, 3, "snow" .. "ball"}[3])
end

In the example above, there is a count-controlled statement which includes an identifier (i), two expressions (1 and 5) and a statement (the line between the do keyword and the end keyword).

The statement contained in the count-controlled statement is an assignment statement that contains an identifier (var) and an operator with two operands (4 and a function call to foo with some arguments). The operator is the addition operator and it has two operands, so it is a binary operator.

The second operand is a function call. There is an expression in the arguments of that function call. That expression is the combination of the binary minus operator (-, as in 2 - 1 ; there's also an unary minus operator, as in -5) with two operands, "this is a " and a table indexation. The table indexation contains a table constructor and an index (3). In the table constructor, there are three expressions, one of which is the combination of a concatenation operator with two operands. The table indexation evaluates to "snowball". This result is concatenated with "this is a ", resulting in "this is a snowball", which is sent to the function foo as an argument. This function then returns a value, which is added to the number 4, and the result of all of this is assigned to the variable var. Because of the count-controlled loop, this is done five times.