Syntax of Lua Language

Here is a quick summary of the frequently used syntax of the Lua language.

The portion enclosed in a dotted frame in each form can be omitted.

Values and Types

All the values that can be handled by the Lua language have their types. The types below are available for the Lua language.

Number Type

There are two numeric types: FIXNUM, which internally represents a 32-bit integer with a sign, and BIGNUM, which represents an integer with an unlimited number of digits with a sign. Although the only difference between FIXNUM and BIGNUM is the range of the numbers that can be represented, many library functions accept only FIXNUM as the argument of the numeric type.

Hereafter, for the generic term of FIXNUM and BIGNUM, "numeric type" and "numeric value" are used, for the FIXNUM only, "numeric type (FIXNUM)" or "FIXNUM" are used, and for BIGNUM only, "numeric type (BIGNUM)" and "BIGNUM" are used. Note that BIGNUM can be handled in the firmware with _RT_LUA_VERSION of "1.01" or later. The numerical value that the firmware with _RT_LUA_VERSION of "1.0" can handle is FIXNUM only.

You can directly write a numerical-type value in a script as a decimal number or a hexadecimal number starting with 0x. To express a negative value, put the minus sign (-) to the left of the number.

Note that you cannot put the plus sign (+) to the left of the number. The plus symbol (+) can be used only expressing addition.

In the original Lua language, the numerical type handles floating-point numbers, but the Lua language embedded in the Yamaha router has been modified to handle integers only.

The number of digits available for the numeric type (BIGNUM) is limited only by the memory size of the router that can use the Lua script functions. Handling a large digit number uses a lot of memory and CPU, so it is recommended not to use such a value as much as possible. Handling an integer of 64 bits (20 digits) will cause no problem at all.

String Type

The character string type represents the string of characters. In a script, enclose a character string-type value with quotations. The following quotations are available:

  • Double quotation (")
  • Single quotation (')

Put the same quotation at both ends of a character string. You can include the following as special characters in a character string.

  • \a ... bell
  • \b ... back space
  • \f ... form feed
  • \n ... new line
  • \r ... return
  • \t ... horizontal tab
  • \v ... vertical tab
  • \\ ... back slash or Yen symbol
  • \" ... double quotation
  • \' ... single quotation
  • \ddd (ddd is a decimal number of up to 3 digits) ... character whose internal code is ddd
  • You can put \ immediately before a new line to include a line break in a character string.
  • The Yamaha router uses ASCII and Shift-JIS as the internal character codes.
  • In a character string output by a command of the Yamaha router, a line break is expressed by two characters, "\r\n". To include a new line in a parameter of a command, you can use either one character, "\n", or two characters, "r\n". The character "\r" only is not supported as a line break.

A character string can also be enclosed by long brackets. The long brackets consist of two square brackets ([, ]) between which there are zero or more equal symbols (=). When the number of equal symbols is N, it is called "long bracket of level N." The following are examples of the long brackets.

  • [[    Opening long brackets of level 0
  • ]]    Closing long brackets of level 0
  • [=[    Opening long brackets of level 1
  • ]=]    Closing long brackets of level 1
  • [===[    Opening long brackets of level 3
  • ]===]    Closing long brackets of level 3

In the character string in the long form, the portion from the opening long brackets until the closing brackets of the same level is a character string. Unlike a character string in a form using double or single quotations, a character string in the long form can include a line break without requiring a special escape symbol. Note that the line break immediately after the opening long brackets is ignored, not included in the character string.

The following example assigns the same character string to the variable a.

a = 'abc\n123'
a = "abc\n123"
a = '\97bc\n\04923'
a = [[abc
a = [==[

Development of Expression

With $ placed immediately before a character string, the enclosed portion in ${~} in the character string is developed as an expression of Lua.

Note that the development of expression can be handled in the firmware with _RT_LUA_VERSION of "1.03" or later.

The expression to be developed is concatenated with the character strings before and after the enclosed portion in the format of ..tostring (expression)... The entire character string is also enclosed by the brackets and expressed as the resulted character string.

.. represents the concatenation operator, and tostring, the character string converting function.


$"My name is ${name}."  ==> ("My name is "..tostring(name)..".") 

$"x + y = ${x + y}" ==> ("x + y"..tostring(x + y).."") 

$[[I am now ${now() - birthday(me)} years old.]]  ==>
	([[I am now ]]..tostring(now() - birthday(me))..[[years old.]]) 

In a character string in the short form (a character string enclosed by single or double quotations), special characters even in the developed expressions are treated in the same manner as treated in the other portions of the character string. Therefore, the quotations in a character string in an expression should be escaped like \' or \", so when the expression is expanded, the \ remains with these quotation marks. Because of this, in a character string in the short format, quotation marks cannot be included in an expression to be developed. If you need to use quotation marks, use a character string in the long format.

$"f(\"a\") == ${f(\"a\")}" ==> ("f(\"a\") == "..tostring(f(\"a\")).."") ... Errors 

$[[f("a") == ${f("a")}]] ==> ([[f("a") == ]]..tostring(f("a"))..[[]]) ... OK

Logical Type

The logical type has two values: true and false. A value of the logical type is used as a result of the comparison operator. In the conditional expression of the control statement, false is judged as false.

A conditional expression in a control statement judges false and nil, which is described later, as false, and the rest are judged as true. Especially, note that the numerical value 0 and an empty character string are judged as true, not as false.

nil Type

The nil type has only the value, nil. The nil differs from any other values and is judged as false in a conditional expression in a control statement.

Function Type

The function type is a function of the Lua language. The Lua language can also assign a function to a variable as a value or pass a function as the argument of another function.

Table Type

The table type is the only structured type in the Lua language. The arrays, structures, and lists in other languages can be expressed in the table type in the Lua language.

The table type can store multiple combinations of keys and values. The combinations of keys and values are called the elements of the table. The keys and values can be expressed in all the types that the Lua language can handle.

To access the elements in a table, use the square brackets ([ ]).

expression 1 [expression 2]

The evaluated value of the expression 1 is express in the table type. Usually, the expression 1 is the name of the variable where a value in the table type is stored. The expression 2 is the value to be used as the key.

If the key is a character string, you can use a period to express the same element. The following two lines are different in just the way of expressing but show the same table elements.

expression 1 . Name
expression 1 [ "name" ]

To write a value in the table type in a program, use the table constructor. The format of the table constructor is like as follows:

  [expression] = expression,
  name = expression,

For the "[expression] = expression" format, store the value in the latter expression with the value of the first expression used as the key. In the format of "name = expression", use the character string ""name"" as the key. If the key is omitted, use the numerical value starting with 1, 2, 3, and so on as the keys.

As the delimiter of each initialization part, place a comma (,) or semicolon (;). If all the initialization parts are omitted, the table contains no element.


Among the tables, when the keys of all the elements are 1 or more in the numerical type (FIXNUM) and when the keys starting with 1 to the maximum value exist without any omission, these values are called an array in this document and the related documents. The array is a special form of the table type, and treated somewhat different from the normal table type, like that some library functions are applicable only to an array.


As the other types, the Lua language provides the user data type and the thread type. However, these types are not frequently used and are not described in this document.


A variable is a place to store a value. There are two types of variables: local and global variables.

The local variable is a variable that has been declared in a local variable declaration statement. That variable is effective only in the block that the variable is declared.

The global variable is effective throughout the script. There is no method of declaring a global variable. In other words, all the variables that are not declared in the local variable declaration statement are global variables.

All variables have names. For the names of variables, you can use alphabets, numbers, underscores (_), but the first letter should be anything other than a number. The alphabets are case sensitive. Note that the following are the reserved names; you cannot use these for the names of variables:

and, break, do, else, elseif, end, false, for, function, if, in, local, nil, not, or, repeat, return, then, true, until, while

Unlike the variables of the C or BASIC languages, the Lua language has no limitation as to the types of values to be assigned. You can assign any type of values.


Expressions are used for the left-hand side of an assignment statement, conditional expression of a control statement, function definition, and arguments of a function call. Evaluating an expression results in some value.

Expression includes variables, literals (values are directly written in a program), function definitions, and operators applied to them.

The value of an expression consisting of a variable only is the value of the variable. The literal represents the value itself. As for the function definition, the function represents the value.

The operators include the following types.

Arithmetic Operators

The arithmetic operator performs the arithmetic operation of numerical values. The arithmetic operators have the binary operator (written between numbers, the values before and after this operator are computed) and the unary operator (written before a number, and the value after this operator is computed), each of which includes the following operators.

  • Binary operator
    +Addition5 + 2 => 7
    -Subtraction5 - 2 => 3
    *Multiplication5 * 2 => 10
    /Division5 / 2 => 2
    %Remainder (Residue of division)5 % 2 => 1
    ^Power5 ^ 2 => 25
  • Unary operator
    -Sign inversion-(-5) => 5

For numerical values, only integers are available; therefore, a division results in a quotient rounded toward negative infinity. If both the dividend and the divisor are positive, the result is the integer part of the quotient. Please remember the arithmetic you learned in elementary school before learning decimals or fractions.

5 divided by 2 is 2 with a remainder of 1. This can be expressed as 5/2 => 2, 5%2 => 1.

In the C language, a division with integers only results in a quotient rounded toward 0. Note that this rounding direction differs from that of the Lua language, so if a quotient is negative, the result will differ.

Lua : 5 / (-2) => -3, 5 % (-2) => -1
C : 5 / (-2) => -2, 5 % (-2) => 1

The arithmetic operators are applied only to normal numbers. If they are applied to a character string, only when the character string has a numerical presentation, it is converted into a numerical value, which in turn is computed.

5 + "2" => 7
5 + "a" => Error
"5" + "2" => 7

Especially, note also that the case where the addition operator (+) is applied to two-character strings is treated as an addition of the numbers, not the concatenation of the character strings. For concatenation of character strings, the special operator is provided.

Comparison Operator

The comparison operator includes the equality sign, inequality sign, and comparator. The comparison operator returns true or false in the logical type as a result of a computation.

The equality sign (==) and the inequality sign (~=) judges whether the values of before and after the sign are equal to each other. In both cases, check if the types of the values are matched or not, and only when matched, compare the values.

In the case of the equality sign (==), both the types and values of the values are matched, true is returned; otherwise false is returned.

In the case of the inequality sign (~=), if either the types or the values differ from each other, true is returned; otherwise false.

While the arithmetic operation converts a character string into a number, whenever possible, the comparison operation does not automatically convert a character string into a number. Because of this, for 0 == "0", false is returned. To compare a number with a character string, you can use the library function, tonumber, to write 0 == tonumber("0").

The comparators include the greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=). Even when a comparator is used, if the types of the values differ from each other, the values are not compared and false is returned.

Numbers are compared by the common method. For character strings, the internal code values are compared one by one from the first letter of each string. When the lengths of two-character strings differ from each other, if the shorter string matches the longer one when compared from the top down, the longer character string is judged as larger.

a = "a"; b = "a"; a == b => true
a = "a"; b = "b"; a < b => true
a = "aa"; b = "a"; a > b => true

For the table type, function type, user data type, and thread type, only the equality sign and the inequality sign are available. Only when the data on the left and right sides of the sign match, true is returned for the equality. For example, even if all the elements within a table are the same, if the tables on both sides of the equality sign are different from each other, false is returned.

a = { 1 }; b = a; a == b => true
a = { 1 }; b = { 1 }; a == b => false

Logical Operator

The logical operator includes logical NOT (not), logical OR (or), and logical AND (and).

In the case of not, when the value of the subsequent expression is false or nil, true is returned; otherwise false.

In the case of or, if the value of the expression on the left-hand side is not false or nil, the value is returned. If the value on the left-hand side is false or nil, the value on the right-hand side is returned.

In the case of and, if the value of the expression on the left-hand side is false or nil, the value is returned. If the value on the left-hand side is not false or nil, the value on the right-hand side is returned.

In the case of or or and, the expression on the right side is not evaluated until the value becomes required. For example, even when a function call is used on the right-hand side, if the result of the or or and computation is determined on the left-hand side, the function on the right-hand side is not called.

function f(x)
  print("called function f with ", x)
  return x + 1

a = 1
b = 1 
c = (a == b) or f(10) -- f is not called, and the print statement in f is not executed.

Concatenation Operator

The concatenation operator is expressed with two periods (..), concatenating the character strings on the left- and right-hand sides. If the values of the expressions on both hand sides are numbers, use the tostring function to convert them into character strings before concatenation.

a = "Hello "
b = "World"
c = a .. b  -- => "Hello World"

a = 1
b = 2
c = a .. b  -- => "12"

Length Operator

The length operator, represented by the hash sign (#), returns the length of the value of the subsequent expression.

The length of a character string is the number of bytes of it.

#"Hello World" => 11

The length of a table, t, is defined as n, where t[n] is not nil but t[n+1] is nil. If a table is an array, the length operator returns the number of elements in the table. However, if nil exists in the table and if there are multiple n that meet this condition, you cannot predict which result is returned. In the following cases, the second and third lines use the length operators for the tables in the same form but have different results.

#{ "a", "b", "c" } => 3
#{ "a", nil, "c" } => 3
#{ "a", nil, [3] = "c" } => 1


The operators have priority. The operator with the highest priority is executed first. In the Lua language, the priority of the operator is as follows:

|  ^
|  not, #, -(Unary)
|  *, /, %
|  +, -(Binomial)
|  ..
|  ==, ~=, >, >=, <, <=
|  and
|  or

You can change the priority by enclosing an expression with parentheses (()).

The right association is applied to the power (^) and the concatenation (..). That is, a^b^c is computed in the order of a^(b^c). The left association is applied to the other binary operators.

Function Definition

In the Lua language, the function definition is not a statement but an element of an expression that returns a function. The following format is used for the function definition:

function name (arguments list)

name = function (arguments list)

These two formats are the same in meaning. As for the meaning of the program, the assignment statement in the latter format is the only function definition. In other words, the variable represented as "name" in the format above is not differentiated from the other variables, and a value in the function type is assigned to it.

function f(x) print(x) end
function g(x) print(x + 1) end 

f(1)          -- '1' is displayed.
g(1)          -- '2' is displayed.
f = g         -- Not an error
f(1)          -- '2' is displayed.
f = 10        -- Not an error
f(1)          -- Error; The value of f (10) is not a function.

As shown below, when you define a function together with local, you can assign the function to a local variable.

local function name (arguments list)

local name; name = function (arguments list)

Also, the former and the latter are the same in meaning. In the latter format, the function can call itself in the function. Note that the scope of a local variable starts with the statement after the local statement, so in the following format, the function cannot call itself in the function.

local name = function (arguments list)

The arguments list enumerates comma (,)-separated variable names to be used for temporary arguments. Temporary arguments are optional. Temporary arguments are treated as local in a function, and initialized with the actual argument used when the function was called.

At the end of the arguments list, you can add three periods (...) to indicate that the list has a variable length. If actual arguments whose number exceeds the specified number of temporary arguments are passed, when ... is found at the end of the temporary arguments, you can access the subsequent actual arguments via the table having ... in its initialization part.

function f(...)
  local a = { ... }
  print(a[1], a[2], a[3])

f(1, 2, 3)

In this example, the function f receives 0 or more arguments and displays three values from the top.

To return the result of a function, use the return statement. Without the return statement, the function does not return anything.

You can also use a colon (:) for the function definition, which is called a method. In a method, the following conversion is performed.

function name 1:name 2(...) ... end

name 2 = function (self, ...) ... end

The name 2 is the key character string of the table of the name 1. Also, the self is added to the top of the arguments. This argument is initialized to the variable of the name 1 when the function is called.

A method is used for I/O functions in an embedded library.


A Lua script consists of a sequence of statements. For appropriate interpretation, you can insert a space or line break between statements. You can also put a semicolon (;) to clearly delimit statements.

The statement includes the following types.

Assignment Statement

The assignment statement is used to assign values to variables. The format is as follows:

variables list = statements list

The variables list includes a comma (,) separated variables, and the statements list includes a comma (,) separated statements.

The value of the statement on the right-hand side is assigned to the variables list written on the left-hand side of the equal sign (=). For example, the following assigns the value, 1, to the variable, a.

a = 1

In addition, the Lua language can assign values to multiple variables at a time. The following assigns the values, 1 and 2 to the variable, a and b, respectively.

a, b = 1, 2

The evaluation of the expression on the right-hand side is executed before all the variables are assigned; therefore, the values of the variables a and b can be switched to each other by writing as follows:

a, b = b, a

Control Statement

The control statement changes the execution order of statements or repeatedly executes a statement. The control statement includes the following types.

if Statement

The if statement executes any of multiple processing tasks according to the value of an expression. The typical format of the if statement is as follows:

if expression 1 then
  THEN block
elseif expression 2 then
  ELSEIF block
  ELSE block

If the value of the expression 1 is not false or nil, the expression 1 is true, and the THEN block is executed. After the THEN block is executed, the entire if statement ends, and the process continues from the statement after the end.

If the value of the expression 1 is false or nil, the expression 1 is false. In that case, the THEN block is not executed, and the process continues to the next elseif or else.

If the expression 1 is false and elseif exists, the value of the expression 2 is evaluated. If the result is true, like the case of the expression 1, the ELSEIF block is executed. An if statement can include multiple elseif conditions. The values of the expressions are evaluated in the order of presentation, and when any expression with its value evaluated as true is found, the ELSEIF block is executed and then the if statement ends.

If all the expressions are evaluated as false and there is an else condition, the statement in the ELSE block is executed.

Note that 0 or more elseif conditions can be included. There must be one or no else condition.

while Statement

The while statement repeats execution as long as the condition is met. The format of the while statement is as follows:

while statement do

The program first evaluates the expression, and if it's true, that is, if the value of the expression is not false or nil, it executes the block and then returns to the evaluation of the expression.

repeat-until Statement

The repeat-until statement repeats execution until the condition is met. This statement differs from the while statement in the timing when the expression is evaluated. The format of the repeat-until statement is as follows:

until expression

The block is executed first, and the expression is evaluated. If the value of the expression is false, that is, false or nil, the process returns to the execution of the block.

Local variables declared in a block in a repeat-until statement are effective up to the end of the expression after until.

Numeric 'for' Statement

The numeric 'for' statement repeats the execution of the block while incrementing the counter variable by a certain value. The format of the numeric 'for' statement is as follows:

for variable = initial value, exit value , increment do

In a numeric 'for' statement, an initial value is set in the variable, and the block is executed. After that, the block is executed while the variable is incremented by a certain value. When the value of the variable exceeds the exit value, the statement ends.

When the increment is omitted, 1 is assumed. A negative value can also be specified in the increment. A negative value can also be specified in the increment.

A variable used as the counter in a numeric 'for' statement is considered local to the 'for' statement. This means that if the counter variable is read after the 'for' statement ends, the value as the counter cannot be read.

Generic 'for' statement

The generic 'for' statement uses a special function, called the iterator (repeat) function, to repeat processing. The format of the generic 'for' statement is as follows:

for variables list in iterator function, status , initial value do

The variables list is the return value of the iterator function. The status and the initial value are the arguments used for calling the iterator function. The initial value changes to the value of the first variable in the list at each loop. If the status and initial value are omitted, nil is assumed. When the iterator function returns nil, the loop ends.

Usually, the set of three items--iterator function, status, and initial value--are obtained by calling a library function that returns these three values, so you do not have to be aware of the set of these three items. pairs(), which enumerates all the elements in the table, ipairs(), which enumerates the elements in an array, and string.gsub(), which enumerates the partial character string that matches the pattern from the character strings. For example, the following example displays all the elements of the table t by using print.

for key, value in pairs(t) do
  print(key, value)

Like the numeric 'for' statement, variables enumerated by a variables list are considered local to the 'for' statement.

do Statement

The do statement is used to set an effective scope of a local variable.


break Statement

The break statement ends the innermost loop (while statement, repeat-until statement, numeric/generic 'for' statement). You cannot use a break statement outside the loop.


The break statement must be written at the end of a block. When writing a program, you do not much care about this because a break statement is always placed at the end, but if you need to put a break statement in the middle of a block for debugging or other reasons, you can make a temporary block with a do statement as follows:

do break end

The Lua language provides a break statement to end a loop completely; however, no control statement (like the continue statement in the C language) is provided to end this round of the loop and forcibly start the next round of the loop.

return Statement

The return statement is used in a function definition, terminating the execution of the function with values returned. A return statement can return multiple values. You cannot use a return statement outside of a function definition.

return expression 1 , expression 2...

Like the break statement, the return statement must be written at the end of a block. If you need to write a return statement in the middle of a block, again like the break statement, make a temporary block using a do statement.

do return end

Function Calling Statement

A function calling statement only calls a function. The function is executed but the return value is discarded. The format of a function calling statement is as follows:

expression 1 (expression 2, ...)

If the evaluation result of the expression 1 is a function, call the function by using the value in the subsequent parentheses as the argument. In the most usual pattern, just the name of the function is used for expression 1.

For example, print, which we have used many times so far, is also a function. Any of the following examples calls the print function.

f = print
g = f

As a special case, if there is only one argument and only when it is a string literal or a table constructor, you can omit parentheses used to call the function. For example, the following function calls are all correct.

print "Hello, world"     -- Calls the print function by one argument, "Hello, world".

require "strict"         -- Calls the require function by one argument, "strict".

t = table.sort{4,2,3,1}  -- Calls the table.sort function by one argument, {4,2,3,1}.

The following shows examples of errors.

a = "Hello, world"
print a                   -- Because a is a character string type but not a string literal, 
                             the parentheses cannot be omitted. 

table.insert{4,2,3,1}, 5  -- Because the function, table.insert, requires at least two arguments, 
                             the parentheses cannot be omitted.

Local Variable Declaration Statement

The local variable declaration statement declares that the scope and effective range of a variable is local. The variable declared by the local variable declaration statement is effective from the next statement to the declaration statement through the end of the block. The format of the local variable declaration statement is as follows:

local variables list= expressions list

With an expressions list, the variables are initialized under the same rule as that of the assignment statement. Without an expression list, the variables are initialized with nil.

Chunk and Block

In Lua, the enumeration of statements is called a chunk. Lua scripts are all chunks. Among chunks, one that determines the scope (effective range) of a local variable is called a block. The following are all blocks.

  • From do until end in a do statement
  • Between the following elements in an if statement: then, elseif, else, and end
  • From for until end in a 'for' statement
  • From do until end in a while statement
  • From repeat to the expression that follows until in a repeat-until statement
  • From function until end in a function definition


The scope indicates the effective range of a variable.

The scope of a local variable is from the next statement to the local variable declaration statement until the end of the block where the local variable declaration statement is included. In this case, note that the initialization expression of the local variable declaration statement is not included in the scope of the local variable.

There is no explicit declaration statement for a global variable. When a global variable appears, the scope starts, and the global variable is effective until the end of the program.

The Lua language uses the lexical scope. This means that a variable declared outside the block can be accessed from inside the block.

x = 10                 -- global variable 'x'
y = 20                 -- global variable 'y'
do                     -- new block
  local x = x          -- new variable 'x', value is 10
  print(x)             -- 10
  print(y)             -- 20
  x = x + 1
  do                   -- another block
    local x = x + 1    -- another new 'x’
    print(x)           -- 12
    print(y)           -- 20
  print(x)             -- 11
  print(y)             -- 20
print(x)               -- 10 (global variable)
print(y)               -- 20

In the example above, for the variable x, a new local variable is declared for each block, so the local variable declared in each block is used in this block while the variable y accesses the topmost global variable.

Because there is no explicit declaration statement for a global variable, a typing error may occur relatively easily. Even if a new variable name appears due to a typing error, the Lua script does not take the variable as an error and proceeds with the processing assuming that the value is nil. Be careful not to make typing errors.

Using strict.lua included in the distribution package of Lua 5.1.4/5.1.5 allows you to prevent, to some extent, problems caused by typing errors of global variables. For details, see How to Use strict.lua.

Handling of Multiple Values

The Lua language can handle multiple results of function calls and values of the variable arguments. These multiple values are adjusted to the appropriate length as needed when handled in an expression.

When a function call is not part of an expression but a statement alone, all the returned values are discarded.

function f() 
  return 1, 2, 3  -- Returns multiple values as returned values.

f()               -- Discards all the returned values.

If multiple values are found as elements in another expression, only the first value is used and the rest are discarded. Especially, you can enclose the function by parentheses to force the first value only to be used.

function f() return 1, 2, 3 end

a = f() + 1    -- For the return value of f(), '1' only is used; a will be 2.
a, b = (f())   -- For the return value of f(), '1' only is used; a will be 1, b will be nil.

Also, when multiple values are found in the middle of a list (right-hand side of an assignment statement, initialization part of a local statement, initialization part of a table constructor, actual argument of a function call), only the first value is used, and the rest are discarded.

function f() return 1, 2, 3 end

a, b, c = f(), 10   -- For the return value of f(), '1' only is used; a, b, and c will be 1, 
                       10 and nil, respectively.

function g(...)
  t = { ..., 10 }   -- t[1] will be the first value of the actual argument when g() is called; 
                       t[2] will be 10.

g(f(), 10)          -- For the return value of f(), '1' only is used; g() is called as g(1, 10).

If multiple values are found at the end of a list, all the necessary values are used.

function f() return 1, 2, 3 end

a, b, c = 10, f()     -- All the return values of f() are used; a, b, and c will be 10, 1, and 2, 
                      -- No variable exists to be assigned to for '3', the last return value of f(), 
                         and thus, it is discarded.

function g(...)
  t = { ... }         -- All the actual arguments used when g() is called are assigned to 
                         the elements of t. 

g(f())                -- All the return values of f() are used, and g() is called as g(1, 2, 3).


Comments can be written anywhere outside character strings. When a program is executed, an entire comment is considered as one space character. There are short and long comment formats.

The short comment format starts with two hyphens (--) and lasts to the end of the line. No long bracket should be written immediately after the two hyphens.

The long comment format starts with a text with opening long brackets ([[, [==[, etc.) immediately after two hyphens (--), and ends with the closing long brackets of the same level (]], ]==], etc.). No character string including a space should be written between the two hyphens and the opening long brackets.

Use the long comment format to write a multi-line comment or insert a comment in a line.

-- The entire line is a comment.
  multi-line comment
a = 1 --[[comment in the line]] + 2


The errors include syntax and type errors. Basically, if an error occurs, the entire Lua script stops executing and outputs an error message. The exception is that, if errors occur in the processing called by the following functions, these errors are reflected in the return values of the respective functions, and the script does not stop executing.

Return to Top