Lua Language Introduction
Identifiers and Variables
Lua supports identifiers starting with English letters and underscores as variable or function names.
Lebai's Lua interpreter extends this rule, supporting UTF-8 characters as identifiers.
In Lua, global variables can be used without declaration, defaulting to nil, with no quantity limit. To use local variables, add local before the declaration statement. Up to 200 local variables can be declared. Both local and global variables are garbage collected after program execution completes.
The following statements are all valid in Lebai robots:
local a = 0
b = a
_test = {}
variable = b
Comments
Single-line comments start with --:
-- I am a comment
Multi-line comments use two brackets [[ ]] after --:
--[[
Multi-line comment
Second line
--]]
To uncomment, 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 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
Undeclared global variables are nil, assigning nil to a variable deletes it.
In Lua, conditional tests treat all values except false and nil as true. Supports binary operators and and or, and unary operator not.
Common variable initialization:
count = (count or 0) + 1
Equivalent to:
if not count then
count = 0
end
count = count + 1
Numbers
In Lua 5.2, the number type does not distinguish between integers and floats, both represented in double-precision floating-point format.
Lua 5.3 starts supporting integer types.
Number Constants
Decimal and scientific notation:
assert(1 == 1.0)
assert(-3 == -3.0)
assert(0.2e3 == 200)
assert(4.57e-3 == 0.00457)
Hexadecimal 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, negation
assert(3 / 2 == 1.5) -- Division
assert(3 // 2 == 1) -- Floor division (Lua 5.3)
assert(3 % 2 == 1) -- Modulo
assert(2 ^ 3 == 8) -- Power
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) -- Floor
assert(math.ceil(3.3) == 4) -- Ceil
a, b = math.modf(3.5) -- Round toward zero
assert(a == 3 and b == 0.5)
Angle 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)) -- Arc sine
cos = math.cos(rad) -- Cosine
print(math.acos(cos)) -- Arc cosine
tan = math.tan(rad) -- Tangent
print(math.atan(tan)) -- Arc tangent
Other math functions:
assert(math.abs(-1) == 1) -- Absolute value
assert(math.min(1, 2, 3) == 1) -- Minimum
assert(math.max(1, 2, 3) == 3) -- Maximum
assert(math.sqrt(4) == 4^0.5) -- Square root
print(math.exp(10)) -- e^10
print(math.log(10)) -- Natural logarithm
print(math.log(10, 2)) -- Logarithm base 2
Math 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.
To handle errors in Lua, use the pcall (protected call) function to wrap the code to be executed.
Syntax:
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
