refactor: AST Visitor dispatch — sema analyze_expr switch→vtable
新增 src/ast/visit.h/c: AstDispatch 函数指针表 + ast_visit() 统一入口 analyze_expr 的 switch 替换为 dispatch 表, 10 个 handler 通过 SEMA_HANDLER 宏注册 新增 AST 节点: 在 analyze_expr_init() 加一行即可, 编译器会检查函数签名 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
#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 = 28 };
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user