06d80f441a
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>
26 lines
736 B
C
26 lines
736 B
C
#ifndef AST_VISIT_H
|
|
#define AST_VISIT_H
|
|
|
|
#include "ast.h"
|
|
|
|
// 通用节点处理器: ctx 为模块上下文, 返回模块相关值(sema 返回 NULL)
|
|
typedef void* (*VisitFn)(void* ctx, AstNode* node);
|
|
|
|
// 遍历表 — 按 AstKind 索引, 未处理的条目为 NULL
|
|
// 新增 AST 节点: 在此表新增一条目, 编译器会警告未初始化的函数指针
|
|
enum { VISIT_TABLE_SIZE = 29 };
|
|
|
|
typedef struct {
|
|
void* ctx;
|
|
VisitFn table[VISIT_TABLE_SIZE];
|
|
} AstDispatch;
|
|
|
|
// 获取未处理节点类型的默认处理器
|
|
VisitFn ast_dispatch_get(AstDispatch* d, AstKind k);
|
|
void ast_dispatch_set(AstDispatch* d, AstKind k, VisitFn fn);
|
|
|
|
// 统一入口: 查表 + 调用
|
|
void* ast_visit(AstDispatch* d, AstNode* node);
|
|
|
|
#endif
|