- 新增
libdecrypt.so,支持 Lua 加密脚本自动解密加载- 提供
decrypt.loadfile()/decrypt.dofile()/decrypt.load()接口 - 兼容
.luae加密格式及普通.lua/.luac文件 - 复用
ldump.c/lundump.c保护配置,加解密算法一致
- 提供
- 支持 Maven 依赖自动下载与打包(JAR 类型)
- 优化补全数据加载机制:检测到版本变更时自动重建补全数据缓存,确保代码补全与新版本一致
- 优化应用启动流程
- 增加
com.luaforge.studio.widget.textfield.MaterialTextField组件
- 增加
onActivityReenter回调函数
本文档基于 Lua 5.5 的深度魔改版本,全面支持现代编程语言的语法特性。主要特性包括:
- 增强型语法结构:支持 try-catch-finally 异常处理、switch-case 多分支、defer 延迟执行、when 条件分支、continue 跳转。
- 现代化操作符:三元运算符、复合赋值、可选链、空值合并、管道运算符。
- 函数式编程增强:Lambda 表达式(匿名函数)简洁语法。
- 完整的特性组合:所有扩展语法可以无缝嵌套使用。
| 符号 | 等价于/描述 | 说明 |
|---|---|---|
? : |
if-else 三元 | 条件运算符 (cond) ? true_expr : false_expr |
?. |
安全访问 | 可选链操作符,避免 nil 报错 |
?? |
空值合并 | 左侧为 nil 时返回右侧值 |
| ` | >` | 管道操作符 |
! |
not |
逻辑非 |
!= |
~= |
不等于 |
&& |
and |
逻辑与 |
| ` | ` | |
$ |
local |
局部变量声明缩写 |
@ |
:: |
标签声明(用于 goto) |
支持完整的 C 风格复合赋值操作符:
| 操作符 | 示例 | 等价于 |
|---|---|---|
+= |
a += 5 |
a = a + 5 |
-= |
a -= 3 |
a = a - 3 |
*= |
a *= 2 |
a = a * 2 |
/= |
a /= 4 |
a = a / 4 |
//= |
a //= 2 |
a = a // 2 |
%= |
a %= 3 |
a = a % 3 |
^= |
a ^= 2 |
a = a ^ 2 |
..= |
s ..= "x" |
s = s .. "x" |
&= |
bits &= mask |
bits = bits & mask |
| ` | =` | `flags |
<<= |
a <<= 2 |
a = a << 2 |
>>= |
a >>= 1 |
a = a >> 1 |
示例代码:
$ counter = 10
counter += 5 -- 15
counter *= 2 -- 30
$ message = "Hello"
message ..= " World" -- "Hello World"
$ flags = 0b1100
flags &= 0b1010 -- 0b1000简洁的条件表达式,支持嵌套使用。
语法格式:
(condition) ? true_expression : false_expression示例代码:
$ age = 18
$ status = (age >= 18) ? "adult" : "minor"
print(status) -- adult
-- 嵌套使用
$ score = 85
$ grade = score >= 90 ? "A" : score >= 80 ? "B" : "C"
print(grade) -- B安全访问嵌套对象属性,避免因中间值为 nil 而抛出错误。
语法格式:
obj?.field -- 安全访问字段
obj?.[index] -- 安全访问数组元素
obj?.method?() -- 安全调用方法示例代码:
$ user = {
profile = {
name = "Alice",
settings = { theme = "dark" }
}
}
print(user?.profile?.name) -- Alice
print(user?.profile?.address?.city) -- nil(不会报错)
print(user?.nonexist?.field) -- nil
-- 与空值合并结合使用
$ city = user?.profile?.address?.city ?? "unknown"
print(city) -- unknown当左侧值为 nil 时返回右侧值,否则返回左侧值。
语法格式:
value ?? default_value示例代码:
$ name = nil
$ display = name ?? "Anonymous" -- "Anonymous"
$ count = 0
$ result = count ?? 100 -- 0(0 不是 nil)
$ a = nil; $ b = nil; $ c = 42
$ value = a ?? b ?? c ?? 0 -- 42简洁的匿名函数定义语法,支持多种写法。
语法格式:
\参数列表 -> 表达式 -- 单表达式自动返回
\参数列表 => 语句块 -- 多语句需显式 return
lambda 参数列表 -> 表达式 -- 完整写法示例代码:
-- 基础用法
$ add = \x, y -> x + y
print(add(3, 5)) -- 8
$ square = \x -> x * x
print(square(4)) -- 16
-- 多语句块
$ complex = \x, y -> do
$ temp = x + y
return temp * 2
end
-- 闭包
$ factor = 3
$ multiplier = \x -> x * factor
print(multiplier(5)) -- 15
-- 高阶函数
$ make_adder = \n -> \x -> x + n
$ add5 = make_adder(5)
print(add5(10)) -- 15
-- 与管道结合
$ numbers = {1, 2, 3, 4, 5}
$ doubled = map(numbers, \x -> x * 2)将值从左到右传递通过一系列函数,提高代码可读性。
语法格式:
value |> function1 |> function2 |> function3示例代码:
$ double = \x -> x * 2
$ add1 = \x -> x + 1
$ square = \x -> x * x
$ result = 5 |> double |> add1 |> square
print(result) -- ((5*2)+1)^2 = 121
-- 与三元结合
$ value = (x > 0) ? x : 0 |> double |> add1
-- 与可选链结合
$ user = { score = 80 }
$ level = user?.score |> \s -> s >= 60 ? "pass" : "fail"完整的异常处理机制,支持错误捕获和资源清理。
语法格式:
try
-- 可能抛出错误的代码
error("something wrong")
catch (error_variable)
-- 错误处理代码
finally
-- 无论是否出错都会执行的清理代码
end示例代码:
-- 基础用法
try
$ file = io.open("data.txt", "r")
$ content = file:read("*a")
print(content)
catch (err)
print("Error reading file:", err)
finally
if file then file:close() end
end
-- 嵌套使用
try
print("Outer try")
try
error("inner error")
catch (e)
print("Inner catch:", e)
error("rethrown")
finally
print("Inner finally")
end
catch (e)
print("Outer catch:", e)
finally
print("Outer finally")
end
-- 与 return 结合
function test()
try
return "from try"
catch (e)
return "from catch"
finally
print("finally runs before return")
end
end多分支选择结构,支持多值匹配。
语法格式:
switch expression do
case value1 then
-- 代码块
case value2, value3 then
-- 多值匹配
default
-- 默认分支
end示例代码:
$ command = "start"
switch command do
case "start", "run" then
print("Starting...")
-- 执行启动逻辑
case "stop", "halt" then
print("Stopping...")
case "restart" then
print("Restarting...")
default
print("Unknown command:", command)
end
-- 数值匹配
$ score = 85
switch math.floor(score / 10) do
case 9, 10 then
print("Grade A")
case 8 then
print("Grade B")
case 7 then
print("Grade C")
default
print("Grade D")
end延迟执行,在作用域结束时自动运行,常用于资源释放。
语法格式:
defer statement end示例代码:
function processFile(filename)
$ file = io.open(filename, "r")
defer file:close() end
-- 无论发生什么,file:close() 都会在函数退出前执行
$ data = file:read("*a")
if #data == 0 then
return nil -- defer 仍会执行
end
return process(data)
end
-- 多个 defer 按后进先出顺序执行
function test()
defer print("first") end
defer print("second") end
defer print("third") end
print("body")
end
-- 输出顺序: body, third, second, first
-- 在块中使用
do
$ resource = acquire()
defer release(resource) end
-- 使用资源
end -- 退出块时自动释放简洁的条件分支,类似多路 if-elseif。
语法格式:
when condition1 then
-- 代码块
case condition2 then
-- 代码块
else
-- 默认分支
end示例代码:
$ temperature = 25
when temperature > 30 then
print("Hot")
case temperature > 20 then
print("Warm")
case temperature > 10 then
print("Cool")
else
print("Cold")
end
-- 与逻辑操作符结合
$ age = 25
$ hasLicense = true
when age >= 18 && hasLicense then
print("Can drive")
case age >= 18 && !hasLicense then
print("Need license")
else
print("Too young")
end跳过当前循环迭代,进入下一次循环。
语法格式:
continue -- 在 for, while, repeat 循环中使用示例代码:
-- for 循环
for i = 1, 10 do
if i % 2 == 0 then
continue -- 跳过偶数
end
print("odd:", i) -- 输出 1,3,5,7,9
end
-- while 循环
$ i = 0
while i < 10 do
i = i + 1
if i == 5 then
continue -- 跳过 5
end
print(i) -- 输出 1,2,3,4,6,7,8,9,10
end
-- repeat 循环
$ j = 0
repeat
j = j + 1
if j == 3 then
continue
end
print(j) -- 输出 1,2,4,5
until j >= 5
-- 嵌套循环
for i = 1, 3 do
for j = 1, 3 do
if j == 2 then
continue -- 只跳过内层循环的当前迭代
end
print(i, j)
end
end使用 $ 快速声明局部变量。
语法格式:
$ variable = value
$ var1, var2 = value1, value2示例代码:
$ name = "Lua"
$ x, y = 10, 20
$ result = x + y
-- 与三元结合
$ max = (x > y) ? x : y
-- 在块中使用
do
$ temp = calculate()
print(temp)
end
-- temp 在这里不可访问使用 @ 声明标签,支持 goto 跳转。
语法格式:
@label@ -- 标签声明
goto label示例代码:
-- 循环模拟
$ count = 1
@start@
print("count:", count)
count = count + 1
if count <= 3 then
goto start
end
-- 错误处理
$ success, err = pcall(function()
if some_condition then
goto error_handler
end
-- 正常逻辑
return
@error_handler@
print("Error occurred")
end)所有扩展语法可以无缝组合,实现简洁而强大的代码。
-- 复杂数据处理管道
$ processUserData = \data -> do
try
$ result = data?.users
?.[0]
?.profile
?.name ?? "anonymous"
|> upper
|> \name -> name .. " (" .. (data?.version ?? 1) .. ")"
$ counter = 0
counter += (result != "anonymous") ? 10 : 0
when counter > 5 then
print("High priority user")
case counter > 0 then
print("Normal user")
else
print("Anonymous user")
end
return result
catch (e)
return "error: " .. e
finally
print("Processing completed")
end
end
-- 资源管理
function safeFileOperation(filename, operation)
$ file = io.open(filename, "r")
if !file then
return nil, "Cannot open file"
end
defer file:close() end
$ content = file:read("*a")
$ result = content
|> operation
|> \r -> r ?? "no result"
return result
end
-- 状态机
$ state = "initial"
while true do
when state == "initial" then
print("Initializing...")
state = "running"
case state == "running" then
print("Running...")
state = (counter++ > 10) ? "finished" : "running"
case state == "finished" then
print("Finished")
break
else
print("Unknown state:", state)
break
end
end
-- 错误处理与清理
try
$ conn = createConnection()
defer conn:close() end
$ data = conn:query("SELECT * FROM users")
$ processed = data
|> filter(\u -> u.age > 18)
|> map(\u -> {
name = u.name |> upper,
adult = true
})
switch #processed do
case 0 then
print("No adult users")
case 1 then
print("One adult user:", processed[1].name)
default
print("Multiple adult users:", #processed)
end
catch (err)
print("Database error:", err)
finally
print("Database operation completed")
end| 特性类别 | 具体特性 | 语法示例 |
|---|---|---|
| 条件表达式 | 三元运算符 | (a>b) ? a : b |
| 空值合并 | value ?? default |
|
| 可选链 | obj?.field?.[index] |
|
| 赋值操作 | 复合赋值 | `+= -= *= /= //= %= ^= ..= &= |
| 逻辑操作 | 逻辑非 | !condition |
| 不等于 | a != b |
|
| 逻辑与 | a && b |
|
| 逻辑或 | `a | |
| 函数式编程 | Lambda | \x,y -> x+y |
| 管道 | `value | |
| 异常处理 | Try-Catch-Finally | try ... catch(e) ... finally ... end |
| 控制流 | Switch-Case | switch x do case v: ... default ... end |
| When | when c1 then ... case c2 then ... else ... end |
|
| Continue | continue |
|
| 资源管理 | Defer | defer cleanup() end |
| 语法糖 | 局部声明 | $ var = value |
| 标签 | @label@ |