开发者生态
morning
Rust 但 Lisp
2026-05-09
1 阅读
thatxliner
rlisp Rust 语义与 LISP 语法。一个透明的 s-表达式前端,直接编译为 Rust — 没有运行时,没有 GC,只是 (s-expr → .rs → 二进制) 。 (struct Point (x f64) (y f64)) (impl Point (fn distance (( &self ) (other &Point )) f64 ( let dx ( - ( . self x) ( . other x))) ( let dy ( - ( . self y) ( . other y))) (( . dx powf 2.0 ) + ( . dy powf 2.0 )) sqrt )) (fn main () () ( let p1 (new Point (x 0.0 ) (y 0.0 ))) ( let p2 (new Point (x 3.0 ) (y 4.0 ))) (println! " Distance: {} " ( . p1 distance (& p2)))) Rust 拥有的一切 — 所有权、借用、生命周期、泛型、特征、模式匹配 — 都表达为 s 表达式。没有语义差距。 rustc 进行类型检查、借用检查和优化。 rlisp 只处理语法。安装 git clone https://github.com/ThatXliner/rlisp.git cd rlisp Cargo install --path 。用法 rlisp compile file.lisp # 转译为 file.rs rlisp build file.lisp # 使用 rustc 转译和编译 rlisp run file.lisp # 转译、编译和运行 语法映射 LISP Rust (fn add ((x i32) (y i32)) i32 (+ x y)) fn add(x: i32, y: i32) -> i32 { (x + y) (让 x i32 42) 让 x: i32 = 42; (struct Point (x f64) (y f64)) struct Point { x: f64, y: f64 } (enum Option (T) (Some T) None) enum Option { Some(T), None } (match val ((Some x) (handle x)) (None ())) match val { Some(x) => { handle(x) }, None => { } } (if (> x) 0) (println! "是") (println! "否")) if (x > 0) { println!("是") } else { println!("否") } (impl Point (fn new (...) ...)) impl Point { fn new(...) ... } (特征显示 (fn fmt (...) 结果)) 特征显示 { fn fmt(...) -> 结果; } (new Point (x 1.0) (y 2.0)) Point { x: 1.0, y: 2.0 } (.obj 字段) obj.field (.obj 方法 arg) obj.method(arg) ([] arr 0) arr[0] (foo!args) foo!(args) (println! "{}" x) println!("{}", x) (loop (println!"tick")) 循环 { println!("tick") } (while (> x 0) (-= x 1)) while x > 0 { x -= 1 } (for x in 0..10 (println! "{}" x)) for x in 0..10 { println!("{}", x) } (lambda (x y) (+ x y)) |x, y| { x + y } (pub fn foo () i32 42) pub fn foo() -> i32 { 42 } (pub (crate) mod m (fn f () () ())) pub(crate) mod m { fn f() {} } (使用 std::collections::HashMap) 使用 std::collections::HashMap; (const MAX usize 1024) const MAX: usize = 1024; (rust "让 x: i32 = 42; x") 让 x: i32 = 42; x 二元运算符( + 、 - 、 * 、 / 、 == 、 != 、 < 、 > 、 && 等)发出中缀: (+ a b) → (a + b) 。宏 rlisp 宏是编译时 s 表达式转换器 — 没有 proc_macro crate、没有 token 流、没有 syn/quote。宏只是从 s 表达式到 s 表达式的函数。宏体使用从 LISP 借用的三种特殊形式: 形式 含义(quasiquote 模板) “引用此模板,但允许在内部取消引用” — 就像标记的模板文字(取消引用名称) “在此处插入名称的值” — 模板中的一个洞(取消引用拼接名称) “将列表名称拼接到周围的列表中” — 用于插入多个形式 将 quasiquote 视为“返回这个精确的 s 表达式,除了取消引用之外洞。”如果没有它,您必须使用 list 和 cons 手动构造每个括号。 ; ;定义一个when宏:(when条件体...)(defmacrowhen(条件&restbody)(quasiquote(if(unquote条件)(do(unquote-拼接体))))); ;宏展开: ; ; (当 (> x 10) (打印“大”) (打印“巨大”)) ; ; → (if (> x 10) (do (print "big") (print "huge"))) ; ; → 如果 x > 10 { print("大"); print("huge") } ( defmacro double (x) (quasiquote ( + (unquote x) (unquote x)))) ; ; (double 21) → (+ 21 21) → 21 + 21 (fn main () () ( let x 21 ) (println! " Double: {} " (double x)) ( when ( > x 10 ) (println! " x is Greater than 10 " ) (println! " this too " ))) &rest 将所有剩余参数捕获到列表中,并且取消引号拼接将该列表展平为周围的形式。这就是可变参数宏的工作原理。循环 (while ( > x 0 ) (println! " {} " x) (-= x 1 )) (loop (println! " tick " )) (for x in 0..10 (println! " {} " x)) ; ; for 与解构