Files
l-language/test/programs/44_lambda.l
T
Serendipity 06d80f441a feat: 闭包/lambda — 匿名函数表达式
fn(x: T) -> R { body } 作为表达式, 可赋值给变量并间接调用。

全流水线实现:
- Parser: TOK_FN 前缀 → AST_LAMBDA 节点
- Sema: 自动生成 __lambda_N 顶层函数 + 符号注册
- Sema: analyze_call_expr 支持 TYPE_CLOSURE 变量调用
- Codegen: lambda 表达式返回函数指针(i64), 调用点载入+IntToPtr+间接call
- VarEntry.closure_fn 追踪闭包变量对应的生成函数

限制(MVP v0.1): 非捕获 lambda, 返回类型固定 i64

+6 sema 测试 + 1 集成测试, 209 测试全部通过

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-07 15:07:03 +08:00

27 lines
720 B
Common Lisp
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 闭包测试 lambda 表达式 + 调用
fn apply_op(x: i64, op: i64) -> i64 {
// 闭包作为参数暂不支持直接调用返回 x * 2
return x * 2;
}
fn main() -> void {
// 测试1: 基本 lambda
let double = fn(x: i64) -> i64 { return x * 2; };
let r1 = double(21);
print_i64(r1); // 42
// 测试2: lambda with multiple params
let add = fn(a: i64, b: i64) -> i64 { return a + b; };
let r2 = add(30, 12);
print_i64(r2); // 42
// 测试3: nested lambda call
let r3 = double(add(10, 11));
print_i64(r3); // 42
// 测试4: lambda in sequence
let triple = fn(x: i64) -> i64 { return x * 3; };
let r4 = triple(14);
print_i64(r4); // 42
}