Files
scc/libs/parser/src/scc_parser.c
zzy 096177e7e8 feat(ast2ir): 添加多种表达式和语句类型的TODO实现
添加了对CAST、COMPOUND、LVALUE、BUILTIN等表达式类型的支持,
以及SWITCH、CASE、DEFAULT等语句类型的框架实现。

fix(hir_dump): 修复整数值格式化显示问题

修改了整数值的获取方式,从原来的const_int.int32改为integer.data.digit,
并添加了hack注释说明。

fix(lir_module): 修复数据符号添加中的比较操作符错误

将赋值操作符'='改为相等比较操作符'==',修正了条件判断逻辑。

refactor(mir_x86): 改进寄存器分配和指令选择逻辑

添加了函数元数据字段用于虚拟寄存器计数,改进了移动指令的处理逻辑,
将条件分支相关代码替换为setcc指令序列。

fix(parser): 修正类型指针返回类型一致性

统一了类型获取函数的返回类型,从const指针改为非const指针,
确保类型系统的一致性。

fix(parser): 修复结构体类型解析中的类型分配问题

修改了匿名结构体类型的处理逻辑,确保类型声明能够正确挂载到AST中。

fix(config): 修正emit-target参数类型配置

将emit-target选项的参数类型从字符串改为布尔型,修正了配置解析。

test: 增加全局超时控制和测试优化

添加了全局超时机制防止测试无限等待,改进了测试运行器的统计信息输出。
2026-05-06 18:06:33 +08:00

100 lines
3.0 KiB
C

#include <parser_utils.h>
#include <scc_parser.h>
static void dummy_sema_callback(scc_sema_ctx_t *context,
scc_ast_node_kind_t node_type, void *node) {
(void)context;
(void)node_type;
(void)node;
return;
}
static scc_ast_qual_type_t *dummy_got_type_callback(scc_sema_ctx_t *context,
const char *name) {
(void)context;
(void)name;
return nullptr;
}
#define ASSIGN_PTR_OR_DEFAULT(assigned_val, value, default) \
assigned_val = value ? value : default
void scc_parser_init(scc_parser_t *parser, scc_lexer_tok_ring_t *tok_ring,
scc_ast_ctx_t *ast_ctx, scc_sema_ctx_t *sema_ctx) {
Assert(parser != nullptr && tok_ring != nullptr);
Assert(ast_ctx != nullptr);
parser->ast_ctx = ast_ctx;
parser->ring = tok_ring;
parser->errcode = 0;
parser->translation_unit = nullptr;
if (sema_ctx == nullptr) {
parser->sema_ctx = scc_malloc(sizeof(scc_sema_ctx_t));
Assert(parser->sema_ctx != nullptr);
parser->sema_ctx->on_decl = dummy_sema_callback;
parser->sema_ctx->on_stmt = dummy_sema_callback;
parser->sema_ctx->on_expr = dummy_sema_callback;
parser->sema_ctx->on_type = dummy_sema_callback;
parser->sema_ctx->got_type = dummy_got_type_callback;
parser->sema_ctx->context = nullptr;
parser->owned_sema = true;
} else {
parser->owned_sema = false;
parser->sema_ctx = sema_ctx;
}
}
void scc_parser_drop(scc_parser_t *parser) {
// TODO: 释放 AST 内存
scc_ring_free(*parser->ring);
if (parser->owned_sema) {
scc_free(parser->sema_ctx);
} else {
scc_sema_drop(parser->sema_ctx);
}
}
scc_ast_translation_unit_t *scc_parse_translation_unit(scc_parser_t *parser) {
scc_ast_translation_unit_t *unit =
scc_malloc(sizeof(scc_ast_translation_unit_t));
if (!unit)
return nullptr;
unit->base.type = SCC_AST_TRANSLATION_UNIT;
scc_vec_init(unit->declarations);
parser->translation_unit = unit;
/**
* Program := (Declaration | Definition)*
* same as
* Program := Declaration* Definition*
*/
while (1) {
scc_ast_decl_t *decl = scc_parse_declaration(parser);
if (decl != nullptr) {
scc_vec_push(unit->declarations, decl);
} else {
break;
}
if (parser->errcode != 0) { // FIXME errcode
SCC_ERROR(scc_parser_got_current_pos(parser), "parser error: %d",
parser->errcode);
break;
}
const scc_lexer_tok_t *tok = scc_parser_peek(parser);
if (tok == nullptr || tok->type == SCC_TOK_EOF) {
break;
}
}
if (parser->errcode) {
// TODO: 清理
SCC_ERROR(scc_parser_got_current_pos(parser), "parser error: %d",
parser->errcode);
scc_free(unit);
return nullptr;
}
Assert(unit->base.type == SCC_AST_TRANSLATION_UNIT);
return unit;
}