Lumesh 语法概览
Lumesh 是一个现代化的 shell 和脚本语言,采用 Pratt 解析器实现复杂的表达式解析。
基础语法结构
运算符优先级系统
Lumesh 使用精确定义的运算符优先级,从低到高排列:
- 赋值运算符 (优先级 1):
=
, :=
, +=
, -=
, *=
, /=
- 重定向和管道 (优先级 2):
|
, |_
, |>
, |^
, >>
, >!
- 错误处理 (优先级 3):
?.
, ?+
, ??
, ?>
, ?!
- Lambda 表达式 (优先级 4):
->
- 条件运算符 (优先级 5):
?:
- 逻辑或 (优先级 6):
||
- 逻辑与 (优先级 7):
&&
- 比较运算 (优先级 8):
==
, !=
, >
, <
, >=
, <=
- 四则运算…
数据类型
基础类型
Lumesh 支持多种基础数据类型:
let num = 42 let negative = -100
let pi = 3.14159 let percent = 85%
let str1 = "双引号字符串,\n支持转义" let str2 = '单引号原始字符串,\n不转义' let template = `模板字符串 $var ${var + 1}`
let flag = True let disabled = False
let empty = None
let a = 3000K
|
集合类型
let arr = [1, 2, 3, "mixed", True] let nested = [[1, 2], [3, 4]]
let map = {a: 1, b: 2, c: 3}
let range1 = 1..10 let range2 = 1..=10 let range3 = 1..10:2
let array = 1...10
|
变量和赋值
基本赋值
let x = 10 let name = "Lumesh"
let a, b, c = 1, 2, 3 let x, y = getValue()
|
延迟赋值
Lumesh 独有的延迟赋值功能:
let cmd := ls -l /tmp let calculation := 2 + 3 * 4
eval(cmd) eval(calculation)
cmd
|
解构赋值
支持数组和映射的解构赋值:
let [first, second, *rest] = [1, 2, 3, 4, 5]
let {name, age} = {name: "Bob", age: 30, city: "NYC"}
let {name: username, age: userAge} = user_data
|
运算符
算术运算符
let a = 10 + 5 let b = 10 - 3 let c = 4 * 6 let d = 15 / 3 let e = 17 % 5 let f = 2 ^ 3
|
比较运算符
a == b a != b a > b a < b a >= b a <= b
text ~= "pattern" text ~: "substring" text !~= "pattern" text !~: "substring"
|
逻辑运算符
condition1 && condition2 condition1 || condition2 !condition
|
管道操作
Lumesh 提供多种管道类型,这是其独特特性:
cmd1 | cmd2
data |_ process_func arg1 _ arg3
list |> transform(param1, param2, _)
cmd1 |^ interactive_program
|
错误处理
Lumesh 内置了强大的错误处理机制:
risky_command ?.
command ?+
command ??
command ?>
command ?!
command ?: error_handler
|
控制流
条件语句
let result = if condition { "true branch" } else { "false branch" }
let value = condition ? true_value : false_value
|
循环结构
while condition { update_condition() }
for item in collection { process(item) }
for i in 0..10 { println(i) }
loop { if break_condition { break } }
repeat 10 {a += 1}
|
模式匹配
支持正则
match value { 1 => "one", 2, 3 => "two or three", xx => "is symbol/string xx", '\w' => "is word", '\d+' => "is digit", _ => "default case" }
|
函数
函数定义
fn greet(name) { "Hello, " + name }
fn add(a, b = 0) { a + b }
fn sum(a, *numbers) { numbers | List.fold(0, (acc, x) -> acc + x) }
|
Lambda 表达式
let square = x -> x * x
let add = (a, b) -> a + b
let process = data -> { let filtered = data | List.filter(x -> x > 0) filtered | List.map(x -> x * 2) }
|
函数装饰器
支持函数装饰器语法:
@timing @cache(300) fn expensive_calculation(input) { heavy_computation(input) }
|
链式调用
支持面向对象风格的链式方法调用:
"hello world" .split(' ') .map(s -> s.to_upper()) .join('-')
data .filter(x -> x.active) .sort(x -> x.priority) .take(10)
|
索引和切片
let arr = [1, 2, 3, 4, 5] let first = arr[0]
let slice1 = arr[1:4] let slice2 = arr[1:] let slice3 = arr[:3] let slice3 = arr[-3:] let slice4 = arr[::2]
let obj = {name: "Alice", age: 25} let name = obj[name] let ages = obj.age let age = obj@age
|
范围操作
1..10 1..<10
1..10:2 0..100:10
|
字符串处理
let name = "World" let age =18 let greeting = `Hello, ${age>18 ? "Mr.":"Dear"} $name !`
let multiline = " 这是一个 多行字符串 "
let raw = 'C:\path\to\file'
|
集合操作
let numbers = [1, 2, 3, 4, 5] numbers.append(6) numbers + 6
numbers - 4 numbers.len()
let person = {name: "Bob", age: 30}
person + {city: "NYC"}
person.keys()
|
模块系统
use my_mod use my_mod as a
Fs.ls("/tmp") String.split("hello world", " ") Math.sin(Math.PI / 2)
|
注释
高级特性
自定义运算符
let _+ = (a, b) -> a.concat(b) let __! = x -> Math.sum(x)
[1, 2] _+ [3, 4] [5,6,7] __!
|
Notes
Lumesh 的设计理念是将现代编程语言的优雅语法与 shell 的实用性相结合,提供强大的错误处理、管道操作和模块系统。通过 Pratt 解析器实现的优先级系统确保了复杂表达式的正确解析,而丰富的内置模块和配置选项使其适用于各种场景,从简单的命令行操作到复杂的系统管理脚本。
了解更多: