- 在ABI类型计算中添加FLOAT和DOUBLE类型的映射 - 修复AST操作符注释中的歧义描述 - 为ast2ir上下文添加类型缓存以解决递归结构体定义问题 - 实现复合初始化表达式的支持,包括数组和结构体初始化 - 添加前置和后置自增/自减操作符的IR转换 - 实现三元条件表达式的IR生成 - 添加类型转换(cast)和sizeof操作符的支持 - 重构数组长度推断逻辑并添加类型大小计算函数 - 实现结构体和联合体的递归类型解析 - 添加函数指针调用相关的IR节点类型定义 fix(ast): 修正间接操作符的注释说明 refactor(ast2ir): 优化代码结构并添加必要的断言验证
101 lines
3.1 KiB
C
101 lines
3.1 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 {
|
|
parser->errcode = 1;
|
|
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;
|
|
}
|