Introduction to Lua Language
Identifiers and Variables
Lua supports identifiers starting with English letters and underscores as variable or function names.
The Lua interpreter for LEBAI robots has extended this rule to support UTF-8 characters as identifiers.
In Lua, global variables can be used without declaration, with a default value of nil
, and there's no limit on their quantity. To use local variables, add local
before the declaration statement. You can declare up to 200 local variables. Both local and global variables will be garbage collected after the program finishes running.
The following statements are all legal in LEBAI robots:
local a = 0
b = a
_test = {}
变量 = b
Comments
Single-line comments start with --
:
-- I am a comment
Multi-line comments start with --
followed by two square brackets [[
]]
:
--[[
Multi-line comment
Second line
--]]
To uncomment a multi-line comment, just add a -
before --[[
:
---[[
a = 0
b = a + 1
--]]
Types and Values
Lua is a dynamically typed language. You can use the type()
function to get the type. Lua has the following 8 basic types:
- Nil
type(nil) --> nil
- Boolean
type(true) --> boolean
- Number
type(10.4 * 3) --> number
- String
type("Hello World") --> string
- Userdata
type(io.stdin) --> userdata
- Function
type(print) --> function
- Table
type({}) --> table
- Thread
thread
Undeclared global variables are nil
. Assigning nil
to a variable deletes it.
In Lua, condition tests consider all values other than false
and nil
as true. It supports binary operators and
(and) or
(or), and unary operator not
(not).
The following statement is commonly used for variable initialization:
count = (count or 0) + 1
This is equivalent to:
if not count then
count = 0
end
count = count + 1
Numbers
In Lua 5.2, the number type doesn't distinguish between integers and floating-point numbers; all are represented in double-precision floating-point format.
Lua 5.3 started supporting integer types.
Numeric Constants
Decimal representation and scientific notation:
assert(1 == 1.0)
assert(-3 == -3.0)
assert(0.2e3 == 200)
assert(4.57e-3 == 0.00457)
Hexadecimal representation and formatting:
assert(0xff == 255)
assert(0x1A3 == 419)
assert(0x0.2 == 0.125)
assert(0x1p-1 == 0.5)
assert(0xa.bp2 == 42.75)
string.format("%a", 419) -- 0x1.a3p+8
string.format("%a", 0.1) -- 0x1.999999999999ap-4
Operations and Math Library
Arithmetic operations:
assert(13 + 15 == 28) -- Addition
assert(13.0 + 25 == 38.0) -- Addition
assert(-(3 * 6.0) == -18.0) -- Subtraction, multiplication, negative
assert(3 / 2 == 1.5) -- Division
assert(3 // 2 == 1) -- Integer division (Lua 5.3)
assert(3 % 2 == 1) -- Modulo
assert(2 ^ 3 == 8) -- Exponentiation
Relational operations:
assert(1 + 1 == 2) -- Equal
assert(1 + 1 ~= 3) -- Not equal
assert(1 < 3) -- Less than
assert(1 <= 3) -- Less than or equal
assert(3 > 1) -- Greater than
assert(3 >= 1) -- Greater than or equal
Random number generation:
math.randomseed(os.time()) -- Set random seed
print(math.random()) -- Generate random number in [0,1)
print(math.random(6)) -- Generate random number in [1,6]
print(math.random(10, 20)) -- Generate random number in [10, 20]
Rounding functions:
assert(math.floor(3.3) == 3) -- Round down
assert(math.ceil(3.3) == 4) -- Round up
a, b = math.modf(3.5) -- Round towards zero
assert(a == 3 and b == 0.5)
Angles and trigonometric functions:
deg = 30
rad = math.rad(deg) -- Degrees to radians
print(math.deg(rad)) -- Radians to degrees
sin = math.sin(rad) -- Sine
print(math.asin(sin)) -- Arcsine
cos = math.cos(rad) -- Cosine
print(math.acos(cos)) -- Arccosine
tan = math.tan(rad) -- Tangent
print(math.atan(tan)) -- Arctangent
Other math functions:
assert(math.abs(-1) == 1) -- Absolute value
assert(math.min(1, 2, 3) == 1) -- Minimum value
assert(math.max(1, 2, 3) == 3) -- Maximum value
assert(math.sqrt(4) == 4^0.5) -- Square root
print(math.exp(10)) -- e^10
print(math.log(10)) -- Natural logarithm of 10
print(math.log(10, 2)) -- Base-2 logarithm of 10
Mathematical constants:
print(math.pi) -- Pi
print(math.huge) -- Infinity
Error Handling
When an error occurs, if not caught and handled, the task will exit abnormally.
In Lua, to handle errors, you can use the pcall (protected call) function to wrap the code that needs to be executed.
The syntax format is as follows:
success, result1, result2, ... = pcall(function, arg1, arg2, ...)
Example:
function divide(a, b)
if b == 0 then
error("Cannot divide by zero")
end
return a / b
end
success, result = pcall(divide, 10, 0)
if not success then
print("Error occurred: ", result)
else
print("Result: ", result)
end