stage1 大量重构

This commit is contained in:
zzy committed 2026-09-16 20:09:24 +08:00
1 parent e4347cc3ea
commit 2a84b61e3d
18 files changed
+1831 -1354

No files matched your search

+48 -4
View File
@@ -77,13 +77,38 @@ static spl_dbg_node_t tok_to_dbg(const spl_tok_t *t) {
return d;
}
/* parser 层的 fatal 发射。diag 为 NULL 时静默。
* 调用者必须紧跟 return / break,因为 diag_fatal 不终止流程。 */
/* 渲染 token:<KIND> `lexeme` at line:col。
传入 NULL 表示 parser 走到了输入流末尾。 */
static void tok_desc(const spl_tok_t *t, char *buf, usize cap) {
if (!t) {
snprintf(buf, cap, "<no token>");
return;
}
if (t->type == TOK_EOF) {
snprintf(buf, cap, "EOF");
return;
}
const char *kind = spl_tok_type_name(t->type);
if (t->type == TOK_ENDLINE) {
snprintf(buf, cap, "ENDLINE at %zu:%zu", t->line, t->col);
return;
}
if (!t->lexeme || t->len == 0) {
snprintf(buf, cap, "%s at %zu:%zu", kind, t->line, t->col);
return;
}
int n = (int)(t->len > 32 ? 32 : t->len);
snprintf(buf, cap, "%s `%.*s` at %s:%zu:%zu", kind, n, t->lexeme, t->fname, t->line, t->col);
}
#define PARSE_FATAL(p, kind, tok, ...) \
do { \
(p)->failed = 1; \
if ((p)->diag) { \
char _tb[80]; \
tok_desc((tok), _tb, sizeof _tb); \
spl_dbg_node_t _pd = tok_to_dbg(tok); \
spl_diag_fatal((p)->diag, (kind), &_pd, __VA_ARGS__); \
spl_diag_fatal((p)->diag, (kind), &_pd, __VA_ARGS__ " (got %s)", _tb); \
} \
} while (0)
@@ -1058,7 +1083,26 @@ static spl_ast_node_ref_t parse_type_expr(parser_t *p) {
return ref;
}
return parse_type_base(p);
spl_ast_node_ref_t base = parse_type_base(p);
if (p->failed || !base)
return base;
while (peek(p) && peek(p)->type == TOK_DOT) {
const spl_tok_t *dot = peek(p);
advance(p);
const spl_tok_t *seg = peek(p);
if (!seg || seg->type != TOK_IDENT) {
PARSE_FATAL(p, SPL_DIAG_KIND_PARSE_EXPECTED, seg ? seg : dot,
"type: expected identifier after '.'");
return base;
}
advance(p);
spl_ast_node_ref_t sel = new_node(p, SPL_AST_EXPR_FIELD, dot);
node_at(p->ast, sel)->field.obj = base;
SET_STR_FIELD(p->ast, node_at(p->ast, sel)->field.field, seg);
base = sel;
}
return base;
}
static spl_ast_node_ref_vec_t parse_expr_list(parser_t *p) {