前言

这里只是简单列出Lua语言的知识点


一、基础语法

1.1 注释

1
2
3
4
5
6
-- 单行注释

--[[
多行注释
可以跨越多行
]]

1.2 变量声明

1
2
3
a = 10           -- 全局变量
local b = 20 -- 局部变量
c, d = 30, "Lua" -- 多重赋值

1.3 数据类型

类型 说明 示例
nil 空值 local x = nil
boolean 布尔值 true, false
number 数字 3.14, 42
string 字符串 "Hello", 'World'
table 表/数组 {1, 2, 3}
function 函数 function() end
thread 协程 coroutine.create()
userdata 自定义数据 用于C交互

二、运算符

2.1 算术运算符

1
+ - * / % ^   -- 加减乘除取模幂

2.2 关系运算符

1
== ~= < > <= >=

2.3 逻辑运算符

1
and or not

2.4 字符串连接

1
local s = "Hello" .. " " .. "Lua!"  -- "Hello Lua!"

2.5 长度运算符

1
2
3
local len = #"Lua"  -- 3
local tbl = {1,2,3}
print(#tbl) -- 3

三、控制结构

3.1 条件语句

1
2
3
4
5
6
7
if score >= 90 then
print("A")
elseif score >= 80 then
print("B")
else
print("C")
end

3.2 循环语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- while循环
local i = 1
while i <= 5 do
print(i)
i = i + 1
end

-- for循环
for i = 1, 5 do
print(i)
end

-- 步长循环
for i = 10, 1, -2 do
print(i) -- 10,8,6,4,2
end

-- repeat until循环
local j = 1
repeat
print(j)
j = j + 1
until j > 5

四、函数

4.1 函数定义

1
2
3
4
5
6
7
8
function add(a, b)
return a + b
end

-- 匿名函数
local multiply = function(a, b)
return a * b
end

4.2 多重返回值

1
2
3
4
5
function getUser()
return "Alice", 30
end

local name, age = getUser()

4.3 变长参数

1
2
3
4
5
6
7
8
9
function sum(...)
local total = 0
for _, v in ipairs({...}) do
total = total + v
end
return total
end

print(sum(1,2,3,4)) -- 10

五、表(Table)

5.1 基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-- 数组式表
local arr = {10, 20, 30}
print(arr[1]) -- 10(Lua索引从1开始)

-- 字典式表
local person = {
name = "Bob",
age = 25,
sayHello = function()
print("Hello!")
end
}

print(person.name) -- "Bob"
person.sayHello() -- "Hello!"

5.2 表操作函数

1
2
3
4
5
local t = {3, 1, 4}

table.insert(t, 2, 5) -- 插入元素: {3,5,1,4}
table.remove(t, 3) -- 删除元素: {3,5,4}
table.sort(t) -- 排序: {3,4,5}

六、元表(Metatable)

6.1 元表基础

1
2
3
4
5
6
7
8
9
local t1 = {10, 20}
local mt = {
__index = function(t, k)
return "Key not found: " .. k
end
}

setmetatable(t1, mt)
print(t1[3]) -- "Key not found: 3"

6.2 运算符重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local v1 = {x=1, y=2}
local v2 = {x=3, y=4}

local mt = {
__add = function(a, b)
return {x = a.x + b.x, y = a.y + b.y}
end
}

setmetatable(v1, mt)
setmetatable(v2, mt)

local v3 = v1 + v2
print(v3.x, v3.y) -- 4, 6

七、模块系统

7.1 创建模块

1
2
3
4
5
6
7
8
9
10
11
12
-- mymodule.lua
local M = {}

function M.add(a, b)
return a + b
end

function M.subtract(a, b)
return a - b
end

return M

7.2 使用模块

1
2
local math = require "mymodule"
print(math.add(5, 3)) -- 8

八、协程(Coroutines)

8.1 协程基础

1
2
3
4
5
6
7
8
local co = coroutine.create(function()
print("Start")
coroutine.yield()
print("End")
end)

coroutine.resume(co) -- 输出 "Start"
coroutine.resume(co) -- 输出 "End"

8.2 生产者-消费者模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function producer()
local i = 0
return function()
i = i + 1
return i
end
end

function consumer(prod)
while true do
local value = prod()
print("Consumed:", value)
coroutine.yield()
end
end

local p = producer()
local co = coroutine.create(consumer, p)

for i = 1, 5 do
coroutine.resume(co)
end

九、文件操作

9.1 文件读写

1
2
3
4
5
6
7
8
9
10
-- 写入文件
local file = io.open("test.txt", "w")
file:write("Hello Lua!\n")
file:close()

-- 读取文件
local file = io.open("test.txt", "r")
local content = file:read("*a")
print(content) -- "Hello Lua!"
file:close()

十、错误处理

10.1 pcall 和 xpcall

1
2
3
4
5
6
7
local success, result = pcall(function()
error("Something went wrong")
end)

if not success then
print("Error:", result)
end

10.2 assert

1
2
3
local value = -5
assert(value >= 0, "Value must be non-negative")
-- 抛出错误: Value must be non-negative

十一、标准库

11.1 常用模块

模块 用途
string 字符串处理
table 表操作
math 数学函数
io 输入输出
os 操作系统接口
debug 调试工具
coroutine 协程支持
package 模块管理

11.2 常用函数

1
2
3
4
5
6
7
print("Hello")                   -- 输出
type(42) -- "number"
tonumber("123") -- 123
tostring(123) -- "123"
pcall(func) -- 保护调用
next(t) -- 遍历表
collectgarbage() -- 垃圾回收

十二、最佳实践

  1. 使用局部变量:默认使用 local 声明变量
  2. 避免全局污染:使用模块组织代码
  3. 正确处理nil:避免对nil值进行操作
  4. 利用元表:实现面向对象和运算符重载
  5. 合理使用协程:实现协作式多任务
  6. 资源管理:及时关闭文件和释放资源
  7. 错误处理:使用pcall保护关键代码
  8. 表设计:区分数组部分和哈希部分
  9. 性能优化:避免在循环中创建表
  10. 代码规范:保持一致的命名和缩进风格