refactor: tok_is_type 统一 + 架构改进文档

- parser.c: 删除重复的is_type_token, 统一使用token.c的tok_is_type
- docs/architecture-improvements.md: TypeKind解耦/Visitor/SourceLoc/去糖方案
This commit is contained in:
2026-06-05 13:13:51 +08:00
parent a7fca5964e
commit 4046ab1875
3 changed files with 598 additions and 9 deletions
+5 -9
View File
@@ -238,10 +238,6 @@ static AstNode* parse_expr(Parser* p, ErrorInfo* error) {
}
// === 类型工具 ===
static bool is_type_token(TokenKind k) {
return k == TOK_I64 || k == TOK_F64 || k == TOK_BOOL || k == TOK_STR || k == TOK_VOID;
}
static TypeKind token_to_type(TokenKind k) {
switch (k) { case TOK_I64: return TYPE_I64; case TOK_F64: return TYPE_F64;
case TOK_BOOL: return TYPE_BOOL; case TOK_STR: return TYPE_STR;
@@ -264,7 +260,7 @@ static AstNode* parse_struct_decl(Parser* p, ErrorInfo* error) {
const Token* ftype = advance(p);
TypeKind field_kind;
const char* field_struct_name = NULL;
if (is_type_token(ftype->kind)) {
if (tok_is_type(ftype->kind)) {
field_kind = token_to_type(ftype->kind);
} else if (ftype->kind == TOK_IDENT) {
field_kind = TYPE_STRUCT;
@@ -327,11 +323,11 @@ static AstNode* parse_statement(Parser* p, ErrorInfo* error) {
const char* struct_type_name = NULL;
if (match(p, TOK_COLON)) {
const Token* type_tok = advance(p);
if (!is_type_token(type_tok->kind) && type_tok->kind != TOK_IDENT) {
if (!tok_is_type(type_tok->kind) && type_tok->kind != TOK_IDENT) {
error->message = "无效的类型标注"; error->filename = p->filename;
error->line = type_tok->line; error->col = type_tok->col; return NULL;
}
if (is_type_token(type_tok->kind)) {
if (tok_is_type(type_tok->kind)) {
annot_type = token_to_type(type_tok->kind);
} else {
// struct 类型名
@@ -516,7 +512,7 @@ static AstNode* parse_function(Parser* p, ErrorInfo* error) {
if (!pname) return NULL;
if (!expect(p, TOK_COLON, error, "缺少 ':'")) return NULL;
const Token* ptype = advance(p);
if (!is_type_token(ptype->kind)) {
if (!tok_is_type(ptype->kind)) {
error->message = "无效的参数类型"; error->filename = p->filename;
error->line = ptype->line; error->col = ptype->col; return NULL;
}
@@ -532,7 +528,7 @@ static AstNode* parse_function(Parser* p, ErrorInfo* error) {
TypeKind ret = TYPE_VOID;
if (match(p, TOK_ARROW)) {
const Token* rt = advance(p);
if (!is_type_token(rt->kind)) {
if (!tok_is_type(rt->kind)) {
error->message = "无效的返回类型"; error->filename = p->filename;
error->line = rt->line; error->col = rt->col; return NULL;
}