Appearance
Lua5.3 参考手册
本章节提供 Lua 5.3 的参考手册,包括语法、函数和库的详细说明。
语法
词法结构
Lua 的词法结构包括标识符、关键字、字面量和运算符。
标识符
标识符是用来命名变量、函数和其他实体的名称,它由字母、数字和下划线组成,不能以数字开头。
关键字
Lua 的关键字包括:
- and, break, do, else, elseif, end, false, for, function, if, in, local, nil, not, or, repeat, return, then, true, until, while
字面量
- 字符串:用单引号、双引号或
[[和]]表示 - 数字:包括整数和浮点数
- 布尔值:true 和 false
- nil:表示未定义的值
运算符
| 运算符 | 描述 | 优先级 |
|---|---|---|
| ^ | 幂运算 | 1 |
| not, #, - | 一元运算符 | 2 |
| *, /, % | 乘法、除法、取模 | 3 |
| +, - | 加法、减法 | 4 |
| .. | 字符串连接 | 5 |
| <, >, <=, >=, ~=, == | 比较运算符 | 6 |
| and | 逻辑与 | 7 |
| or | 逻辑或 | 8 |
表达式
Lua 的表达式包括变量、字面量、函数调用、表构造器、运算符表达式等。
变量
变量可以是全局变量或局部变量,局部变量使用 local 关键字声明。
函数调用
函数调用使用圆括号,如 print("Hello")。
表构造器
表构造器用于创建表,如 {1, 2, 3} 或 {name = "张三", age = 30}。
运算符表达式
运算符表达式由运算符和操作数组成,如 1 + 2、"Hello" .. " World" 等。
语句
Lua 的语句包括赋值语句、控制语句、循环语句、函数定义语句等。
赋值语句
lua
x = 10
a, b = 1, 2控制语句
lua
if condition then
-- 代码块
elseif condition then
-- 代码块
else
-- 代码块
end循环语句
lua
-- while 循环
while condition do
-- 代码块
end
-- for 循环
for var = start, end, step do
-- 代码块
end
-- repeat-until 循环
repeat
-- 代码块
until condition函数定义语句
lua
function func(param1, param2)
-- 代码块
return value
end标准库
basic
- assert (v [, message]):断言,检查 v 是否为真
- collectgarbage ([opt [, arg]]):控制垃圾回收器
- dofile ([filename]):加载并执行文件
- error (message [, level]):抛出错误
- getmetatable (object):获取对象的元表
- ipairs (t):返回迭代器,用于遍历表的数组部分
- load ([chunk [, chunkname [, mode [, env]]]]):加载 Lua 代码
- loadfile ([filename [, mode [, env]]]):加载文件中的 Lua 代码
- next (t [, index]):返回表中的下一个键值对
- pairs (t):返回迭代器,用于遍历表中的所有键值对
- pcall (f [, arg1, ···]):保护调用函数
- print (...):打印值
- rawequal (v1, v2):比较两个值是否相等,不调用元方法
- rawget (t, k):获取表中键对应的值,不调用元方法
- rawlen (v):获取值的长度,不调用元方法
- rawset (t, k, v):设置表中键对应的值,不调用元方法
- select (index, ···):返回可变参数列表中的元素
- setmetatable (table, metatable):设置表的元表
- tonumber (e [, base]):将值转换为数字
- tostring (v):将值转换为字符串
- type (v):返回值的类型
- xpcall (f, msgh [, arg1, ···]):保护调用函数,并指定错误处理函数
coroutine
- coroutine.create (f):创建协同程序
- coroutine.resume (co [, val1, ···]):启动或恢复协同程序
- coroutine.running ():返回当前正在运行的协同程序
- coroutine.status (co):返回协同程序的状态
- coroutine.wrap (f):创建协同程序,并返回一个函数
- coroutine.yield (···):挂起协同程序的执行
debug
- debug.debug ():进入交互式调试模式
- debug.gethook ([thread]):获取钩子函数
- debug.getinfo ([thread,] f [, what]):获取函数的信息
- debug.getlocal ([thread,] f, local):获取局部变量
- debug.getmetatable (value):获取值的元表
- debug.getregistry ():获取注册表
- debug.getupvalue (f, up):获取上值
- debug.info ([thread,] f [, what]):获取函数的信息(Lua 5.2+)
- debug.sethook ([thread,] hook, mask [, count]):设置钩子函数
- debug.setlocal ([thread,] f, local, value):设置局部变量
- debug.setmetatable (value, metatable):设置值的元表
- debug.setupvalue (f, up, value):设置上值
- debug.traceback ([thread,] [message [, level]]):获取调用栈
io
- io.close ([file]):关闭文件
- io.flush ():刷新输出缓冲区
- io.input ([file]):设置或获取标准输入
- io.lines ([filename]):返回迭代器,用于遍历文件中的行
- io.open (filename [, mode]):打开文件
- io.output ([file]):设置或获取标准输出
- io.popen (prog [, mode]):创建管道
- io.read (···):从标准输入读取数据
- io.stderr:标准错误
- io.stdin:标准输入
- io.stdout:标准输出
- io.tmpfile ():创建临时文件
- io.type (obj):检查对象是否为文件
- io.write (···):向标准输出写入数据
math
- math.abs (x):绝对值
- math.acos (x):反余弦
- math.asin (x):反正弦
- math.atan (y [, x]):反正切
- math.ceil (x):向上取整
- math.cos (x):余弦
- math.cosh (x):双曲余弦
- math.deg (x):弧度转角度
- math.exp (x):指数函数
- math.floor (x):向下取整
- math.fmod (x, y):取模
- math.huge:正无穷大
- math.log (x [, base]):对数
- math.log10 (x):以 10 为底的对数
- math.max (x, ···):最大值
- math.min (x, ···):最小值
- math.modf (x):分解为整数和小数部分
- math.pi:圆周率
- math.pow (x, y):幂运算
- math.rad (x):角度转弧度
- math.random ([m [, n]]):随机数
- math.randomseed (x):设置随机数种子
- math.sin (x):正弦
- math.sinh (x):双曲正弦
- math.sqrt (x):平方根
- math.tan (x):正切
- math.tanh (x):双曲正切
- math.tointeger (x):转换为整数
- math.type (x):检查值是否为数字
- math.ult (m, n):无符号整数比较
os
- os.clock ():返回 CPU 时间
- os.date ([format [, time]]):格式化日期和时间
- os.difftime (t2, t1):计算时间差
- os.execute ([command]):执行系统命令
- os.exit ([code [, close]]):退出程序
- os.getenv (varname):获取环境变量
- os.remove (filename):删除文件
- os.rename (oldname, newname):重命名文件
- os.setlocale ([locale [, category]]):设置区域设置
- os.time ([table]):获取当前时间或转换表为时间
- os.tmpname ():生成临时文件名
package
- package.config:包配置
- package.cpath:C 库的搜索路径
- package.loaded:已加载的包
- package.loaders:包加载器(Lua 5.1)
- package.searchers:包搜索器(Lua 5.2+)
- package.loadlib (libname, funcname):加载 C 库
- package.path:Lua 库的搜索路径
- package.preload:预加载的包
- package.searchpath (name, path [, sep [, rep]]):搜索包的路径
string
- string.byte (s [, i [, j]]):返回字符串中字符的 ASCII 码
- string.char (···):将 ASCII 码转换为字符
- string.dump (function):将函数转换为二进制字符串
- string.find (s, pattern [, init [, plain]]):查找字符串中的子串
- string.format (formatstring, ···):格式化字符串
- string.gmatch (s, pattern):返回迭代器,用于遍历匹配的子串
- string.gsub (s, pattern, repl [, n]):替换字符串中的子串
- string.len (s):返回字符串长度
- string.lower (s):将字符串转换为小写
- string.match (s, pattern [, init]):匹配字符串中的子串
- string.rep (s, n [, sep]):重复字符串
- string.reverse (s):反转字符串
- string.sub (s, i [, j]):返回字符串的子串
- string.upper (s):将字符串转换为大写
table
- table.concat (t [, sep [, i [, j]]]):拼接表中的元素
- table.insert (t, [pos,] value):在表中插入元素
- table.maxn (t):返回表中最大的整数索引(Lua 5.1)
- table.move (a1, f, e, t [, a2]):移动表中的元素(Lua 5.3+)
- table.pack (···):将可变参数打包为表(Lua 5.2+)
- table.remove (t [, pos]):删除表中的元素
- table.sort (t [, comp]):对表进行排序
- table.unpack (t [, i [, j]]):将表解包为可变参数
元表和元方法
元方法
| 元方法 | 描述 |
|---|---|
| __add | 加法操作 |
| __sub | 减法操作 |
| __mul | 乘法操作 |
| __div | 除法操作 |
| __mod | 取模操作 |
| __pow | 幂运算操作 |
| __unm | 一元负号操作 |
| __concat | 字符串连接操作 |
| __len | 长度操作 |
| __eq | 等于比较操作 |
| __lt | 小于比较操作 |
| __le | 小于等于比较操作 |
| __index | 索引访问操作 |
| __newindex | 索引赋值操作 |
| __call | 函数调用操作 |
| __tostring | 字符串转换操作 |
| __metatable | 元表访问控制 |
| __gc | 垃圾回收操作 |
全局变量
| 全局变量 | 描述 |
|---|---|
| _G | 全局环境 |
| _VERSION | Lua 版本号 |
| assert | 断言函数 |
| collectgarbage | 垃圾回收函数 |
| dofile | 加载并执行文件 |
| error | 抛出错误 |
| getmetatable | 获取元表 |
| ipairs | 数组迭代器 |
| load | 加载 Lua 代码 |
| loadfile | 加载文件中的 Lua 代码 |
| next | 表迭代器 |
| pairs | 表迭代器 |
| pcall | 保护调用函数 |
| 打印函数 | |
| rawequal | 原始相等性比较 |
| rawget | 原始索引访问 |
| rawlen | 原始长度获取 |
| rawset | 原始索引赋值 |
| select | 选择可变参数 |
| setmetatable | 设置元表 |
| tonumber | 转换为数字 |
| tostring | 转换为字符串 |
| type | 获取类型 |
| xpcall | 带错误处理的保护调用 |
小结
本章节提供了 Lua 5.3 的参考手册,包括语法、标准库、元表和元方法、全局变量等内容。这份参考手册可以作为 Lua 编程的参考资料,帮助你更好地理解和使用 Lua 语言。