feat(parser): 完善类型解析和表达式解析功能

完善了scc_parse_type函数以正确解析基本类型,修复了条件表达式解析逻辑,
实现了for循环中声明和表达式的混合处理,并添加了对赋值语句和复杂表达式的支持。

fix(parser): 修复内存泄漏和解析器状态管理问题

修复了当tok参数为null时的内存泄漏问题,在标签语句解析中正确处理解析器状态回退,
并改进了表达式和声明的错误处理机制。

test(parser): 更新单元测试以验证修复的功能

更新了返回语句的测试值,添加了包含变量声明、赋值语句和复杂表达式的综合测试用例,
验证了赋值运算符的右结合性和复杂表达式的解析正确性。
This commit is contained in:
zzy
2026-03-09 22:45:18 +08:00
parent 1fceeca011
commit 80714fe7e5
6 changed files with 182 additions and 39 deletions

View File

@@ -175,30 +175,13 @@ scc_ast_decl_t *scc_parse_declaration(scc_parser_t *parser) {
* declarator = initializer
*/
cbool ok;
const scc_lexer_tok_t *tok_ptr = scc_parser_next(parser);
scc_lexer_tok_t tok;
if (tok_ptr == null) {
return null;
}
scc_ast_type_t *type = scc_malloc(sizeof(scc_ast_type_t));
scc_ast_type_t *type = scc_parse_type(parser);
if (type == null) {
LOG_FATAL("out of memory");
return null;
}
if (tok_ptr->type != SCC_TOK_INT) {
// TODO back it
scc_parser_reset(parser);
return null;
} else {
type->base.type = SCC_AST_TYPE_BUILTIN;
type->base.loc = tok_ptr->loc;
type->builtin.type = SCC_AST_BUILTIN_TYPE_INT;
type->builtin.quals = (scc_ast_decl_specifier_t){0};
}
scc_parser_commit(parser);
ok = scc_parser_next_consume(parser, &tok);
if (ok == false) {
return null;
@@ -231,7 +214,7 @@ scc_ast_decl_t *scc_parse_declaration(scc_parser_t *parser) {
decl->base.type = SCC_AST_DECL_VAR;
decl->var.type = type;
decl->var.name = scc_cstring_as_cstr(&tok.lexeme);
decl->var.init = null; // scc_parse_expression(parser);
decl->var.init = scc_parse_expression(parser);
goto RETURN;
}
// TODO
@@ -255,7 +238,7 @@ scc_ast_decl_t *scc_parse_declaration(scc_parser_t *parser) {
return null;
}
tok_ptr = scc_parser_peek(parser);
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
if (tok_ptr == null) {
return null;
}