- 在scc_abi_base_type_kind枚举中添加SCC_ABI_TYPE_VA_LIST类型 - 为Windows x64平台实现VA_LIST类型的ABI规则,遵循MSVC调用约定 - 在AST到ABI类型转换中处理SCC_AST_BUILTIN_TYPE_VA_LIST和BOOL类型 refactor(ast2ir): 实现循环控制流和跳转语句转换 - 添加break和continue缓存表用于语句跳转目标管理 - 实现while、do-while和for循环的正确控制流结构 - 添加对break、continue、goto和label语句的IR转换支持 - 修复复合表达式和声明类型的处理逻辑 refactor(parser): 重构语义分析接口和循环语句解析 - 将语义分析上下文从回调结构改为指针传递 - 为switch、while、do-while和for语句添加BEGIN/END语义标记 - 实现匿名结构体/联合体/枚举的符号命名处理 - 优化结构体/联合体/枚举声明的类型解析逻辑 feat(parser): 实现跳转语句语义分析和符号绑定 - 添加break_stack和continue_stack用于跟踪循环层级 - 实现break和continue语句的目标语句绑定检查 - 支持goto和label语句的标签符号查找和绑定 - 添加对跳转语句位置的有效性验证 Co-authored-by: Copilot <copilot@github.com>
99 lines
3.0 KiB
C
99 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 const 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);
|
|
|
|
/**
|
|
* 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;
|
|
}
|