refactor(argparse): 将null替换为nullptr以提高C++兼容性

- 在argparse库中将所有null指针常量替换为nullptr
- 更新头文件和源文件中的指针初始化和比较操作
- 修改测试文件中的相关断言检查
- 更新AST定义文件中的注释说明
This commit is contained in:
zzy
2026-04-05 20:18:09 +08:00
parent 27d86d5685
commit 4144f7841c
76 changed files with 1430 additions and 998 deletions

View File

@@ -324,7 +324,7 @@ static scc_ast_expr_op_t map_token_to_assign_op(scc_tok_type_t type) {
// 跳过直到遇到同步 token分号、右括号、逗号、EOF
static void parser_sync(scc_parser_t *parser) {
const scc_lexer_tok_t *tok_ptr;
while ((tok_ptr = scc_parser_peek(parser)) != null) {
while ((tok_ptr = scc_parser_peek(parser)) != nullptr) {
scc_tok_type_t type = tok_ptr->type;
if (type == SCC_TOK_SEMICOLON || type == SCC_TOK_R_PAREN ||
type == SCC_TOK_R_BRACE || type == SCC_TOK_COMMA ||
@@ -344,7 +344,7 @@ static scc_ast_expr_t *parse_expression_with_precedence(scc_parser_t *parser,
// 从最底层cast-expression开始
scc_ast_expr_t *left = parse_cast_expression(parser);
if (!left)
return null;
return nullptr;
while (1) {
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
@@ -367,11 +367,11 @@ static scc_ast_expr_t *parse_expression_with_precedence(scc_parser_t *parser,
parse_expression_with_precedence(parser, prec + 1);
if (!right) {
parser_sync(parser);
return null;
return nullptr;
}
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
// FIXME pos
scc_ast_expr_binary_init(expr, op, left, right, left->base.loc);
left = expr;
@@ -382,10 +382,10 @@ static scc_ast_expr_t *parse_expression_with_precedence(scc_parser_t *parser,
scc_ast_expr_t *scc_parse_assignment_expression(scc_parser_t *parser) {
// 先解析左侧的 unary-expressionC 标准规定赋值左边必须是
// unary-expression
scc_ast_expr_t *left = null;
scc_ast_expr_t *left = nullptr;
left = parse_conditional_expression(parser);
if (!left)
return null;
return nullptr;
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
if (!tok_ptr)
@@ -406,11 +406,11 @@ scc_ast_expr_t *scc_parse_assignment_expression(scc_parser_t *parser) {
if (!right) {
// 错误恢复
parser_sync(parser);
return null;
return nullptr;
}
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
scc_ast_expr_binary_init(expr, op, left, right, pos);
left = expr;
}
@@ -422,7 +422,7 @@ static scc_ast_expr_t *parse_conditional_expression(scc_parser_t *parser) {
scc_ast_expr_t *cond_expr =
parse_expression_with_precedence(parser, PREC_LOGICAL_OR);
if (!cond_expr)
return null;
return nullptr;
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
if (tok_ptr && tok_ptr->type == SCC_TOK_COND) {
@@ -437,7 +437,7 @@ static scc_ast_expr_t *parse_conditional_expression(scc_parser_t *parser) {
scc_ast_expr_t *then_expr = scc_parse_expression(parser);
if (!then_expr) {
parser_sync(parser);
return null;
return nullptr;
}
// 消耗 ':'
@@ -445,18 +445,18 @@ static scc_ast_expr_t *parse_conditional_expression(scc_parser_t *parser) {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected ':' after '?'");
parser_sync(parser);
return null;
return nullptr;
}
// 解析 else 部分(右结合,再次调用 parse_conditional_expression
scc_ast_expr_t *else_expr = parse_conditional_expression(parser);
if (!else_expr) {
parser_sync(parser);
return null;
return nullptr;
}
scc_ast_expr_t *cond = scc_malloc(sizeof(scc_ast_expr_t));
Assert(cond != null);
Assert(cond != nullptr);
scc_ast_expr_cond_init(cond, cond_expr, then_expr, else_expr, pos);
cond_expr = cond;
}
@@ -466,7 +466,7 @@ static scc_ast_expr_t *parse_conditional_expression(scc_parser_t *parser) {
// 类型转换表达式 (type-name) cast-expression
static scc_ast_expr_t *parse_cast_expression(scc_parser_t *parser) {
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
scc_ast_type_t *type = null;
scc_ast_type_t *type = nullptr;
if (tok_ptr && tok_ptr->type == SCC_TOK_L_PAREN) {
// 尝试解析类型名
scc_parser_store(parser);
@@ -487,7 +487,7 @@ static scc_ast_expr_t *parse_cast_expression(scc_parser_t *parser) {
return operand;
}
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
// FIXME pos
scc_ast_expr_cast_init(expr, type, operand, type->base.loc);
return expr;
@@ -523,7 +523,7 @@ static scc_ast_expr_t *parse_unary_expression(scc_parser_t *parser) {
*/
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
if (!tok_ptr)
return null;
return nullptr;
scc_lexer_tok_t tok = {0};
@@ -539,14 +539,14 @@ static scc_ast_expr_t *parse_unary_expression(scc_parser_t *parser) {
case SCC_TOK_NOT: // !x
{
if (!scc_parser_next_consume(parser, &tok))
return null;
return nullptr;
scc_ast_expr_op_t op = map_token_to_unary_op(tok.type, true);
scc_pos_t pos = tok.loc;
scc_lexer_tok_drop(&tok);
// 一元运算符右结合,递归调用 parse_unary_expression
scc_ast_expr_t *operand = null;
scc_ast_expr_t *operand = nullptr;
if (tok_ptr->type == SCC_TOK_ADD_ADD ||
tok_ptr->type == SCC_TOK_SUB_SUB) {
operand = parse_unary_expression(parser);
@@ -555,11 +555,11 @@ static scc_ast_expr_t *parse_unary_expression(scc_parser_t *parser) {
}
if (!operand) {
parser_sync(parser);
return null;
return nullptr;
}
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
scc_ast_expr_unary_init(expr, op, operand, pos);
return expr;
}
@@ -575,7 +575,7 @@ static scc_ast_expr_t *parse_sizeof_expression(scc_parser_t *parser) {
scc_lexer_tok_t tok_ptr;
if (!scc_parser_next_consume(parser, &tok_ptr) ||
tok_ptr.type != SCC_TOK_SIZEOF) {
return null;
return nullptr;
}
scc_pos_t pos = tok_ptr.loc;
scc_lexer_tok_drop(&tok_ptr);
@@ -584,7 +584,7 @@ static scc_ast_expr_t *parse_sizeof_expression(scc_parser_t *parser) {
if (!next) {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Unexpected end after sizeof");
return null;
return nullptr;
}
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
@@ -593,7 +593,7 @@ static scc_ast_expr_t *parse_sizeof_expression(scc_parser_t *parser) {
scc_parser_store(parser);
scc_parser_next(parser);
scc_ast_type_t *type_name = scc_parse_type_name(parser);
if (type_name == null) {
if (type_name == nullptr) {
scc_parser_restore(parser);
goto next;
}
@@ -603,15 +603,15 @@ static scc_ast_expr_t *parse_sizeof_expression(scc_parser_t *parser) {
"expected ')' after type-name in sizeof expression");
}
Assert(type_name != null);
scc_ast_expr_sizeof_init(expr, type_name, null, pos);
Assert(type_name != nullptr);
scc_ast_expr_sizeof_init(expr, type_name, nullptr, pos);
return expr;
}
next:
// 尝试解析 sizeof unary-expression
scc_ast_expr_t *operand = parse_unary_expression(parser);
if (operand != null) {
scc_ast_expr_sizeof_init(expr, null, operand, pos);
if (operand != nullptr) {
scc_ast_expr_sizeof_init(expr, nullptr, operand, pos);
return expr;
}
@@ -622,12 +622,12 @@ next:
// 后缀表达式
static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
scc_ast_expr_t *left = parse_primary_expression(parser);
const scc_lexer_tok_t *tok_ptr = null;
const scc_lexer_tok_t *tok_ptr = nullptr;
scc_pos_t pos = scc_pos_create();
if (!left) {
tok_ptr = scc_parser_peek(parser);
if (!(tok_ptr && tok_ptr->type == SCC_TOK_L_PAREN)) {
return null;
return nullptr;
}
pos = tok_ptr->loc;
@@ -636,7 +636,7 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
scc_ast_type_t *type = scc_parse_type_name(parser);
if (!type) {
scc_parser_restore(parser);
return null;
return nullptr;
}
scc_parser_commit(parser);
if (!scc_parser_consume_if(parser, SCC_TOK_R_PAREN)) {
@@ -656,30 +656,30 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
switch (tok_ptr->type) {
case SCC_TOK_L_BRACKET: // left[expr]
{
if (!scc_parser_next_consume(parser, null))
if (!scc_parser_next_consume(parser, nullptr))
return left;
pos = left->base.loc;
scc_ast_expr_t *index = scc_parse_expression(parser);
if (!index) {
parser_sync(parser);
return null;
return nullptr;
}
if (!scc_parser_consume_if(parser, SCC_TOK_R_BRACKET)) {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected ']' after subscript");
parser_sync(parser);
return null;
return nullptr;
}
scc_ast_expr_t *subscript = scc_malloc(sizeof(scc_ast_expr_t));
Assert(subscript != null);
Assert(subscript != nullptr);
scc_ast_expr_array_subscript_init(subscript, left, index, pos);
left = subscript;
break;
}
case SCC_TOK_L_PAREN: // left(args)
{
if (!scc_parser_next_consume(parser, null))
if (!scc_parser_next_consume(parser, nullptr))
return left;
pos = left->base.loc;
scc_ast_expr_vec_t args;
@@ -705,11 +705,11 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
// 释放已解析的参数
// TODO: 释放 args 中的表达式
parser_sync(parser);
return null;
return nullptr;
}
}
scc_ast_expr_t *call = scc_malloc(sizeof(scc_ast_expr_t));
Assert(call != null);
Assert(call != nullptr);
scc_ast_expr_call_init(call, left, &args, pos);
left = call;
break;
@@ -726,12 +726,12 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
"Expected identifier after member access");
scc_lexer_tok_drop(&op_tok);
parser_sync(parser);
return null;
return nullptr;
}
const char *name = scc_str_as_cstr(&ident_tok.lexeme);
scc_ast_expr_t *member = scc_malloc(sizeof(scc_ast_expr_t));
Assert(member != null);
Assert(member != nullptr);
if (op_tok.type == SCC_TOK_DOT) {
scc_ast_expr_member_init(member, left, name, ident_tok.loc);
} else {
@@ -750,7 +750,7 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
return left;
scc_ast_expr_op_t op = map_token_to_unary_op(op_tok.type, false);
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
scc_ast_expr_unary_init(expr, op, left, op_tok.loc);
scc_lexer_tok_drop(&op_tok);
left = expr;
@@ -776,43 +776,43 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
*/
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
if (!tok_ptr)
return null;
return nullptr;
scc_lexer_tok_t tok = {0};
scc_ast_expr_t *expr = null;
scc_ast_expr_t *expr = nullptr;
switch (tok_ptr->type) {
case SCC_TOK_IDENT: {
if (!scc_parser_next_consume(parser, &tok))
return null;
return nullptr;
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
scc_ast_expr_identifier_init(expr, scc_str_as_cstr(&tok.lexeme),
tok.loc);
break;
}
case SCC_TOK_INT_LITERAL: {
if (!scc_parser_next_consume(parser, &tok))
return null;
return nullptr;
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
scc_ast_expr_literal_int_init(expr, scc_str_as_cstr(&tok.lexeme), false,
tok.loc);
break;
}
case SCC_TOK_FLOAT_LITERAL: {
if (!scc_parser_next_consume(parser, &tok))
return null;
return nullptr;
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
scc_ast_expr_literal_float_init(expr, scc_str_as_cstr(&tok.lexeme),
false, tok.loc);
break;
}
case SCC_TOK_CHAR_LITERAL: {
if (!scc_parser_next_consume(parser, &tok))
return null;
return nullptr;
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
scc_ast_expr_literal_char_init(expr, scc_str_as_cstr(&tok.lexeme),
false, tok.loc);
break;
@@ -822,7 +822,7 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
scc_lexer_tok_t tok;
while (1) {
tok_ptr = scc_parser_peek(parser);
if (tok_ptr == null) {
if (tok_ptr == nullptr) {
break;
}
if (tok_ptr->type != SCC_TOK_STRING_LITERAL) {
@@ -835,14 +835,14 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
}
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
// FIXME loc
scc_ast_expr_literal_string_init(expr, scc_str_as_cstr(&string), true,
tok.loc);
break;
}
case SCC_TOK_L_PAREN:
scc_parser_next_consume(parser, null);
scc_parser_next_consume(parser, nullptr);
expr = scc_parse_expression(parser);
if (!scc_parser_consume_if(parser, SCC_TOK_R_PAREN)) {
SCC_ERROR(scc_parser_got_current_pos(parser),
@@ -853,8 +853,8 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
break;
}
if (expr == null) {
return null;
if (expr == nullptr) {
return nullptr;
}
scc_parse_expr_sema(parser, expr);
return expr;
@@ -863,24 +863,24 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
scc_ast_expr_t *scc_parse_expression(scc_parser_t *parser) {
scc_ast_expr_t *left = scc_parse_assignment_expression(parser);
if (!left)
return null;
return nullptr;
const scc_lexer_tok_t *tok_ptr = null;
const scc_lexer_tok_t *tok_ptr = nullptr;
while (1) {
tok_ptr = scc_parser_peek(parser);
if (tok_ptr == null || tok_ptr->type != SCC_TOK_COMMA) {
if (tok_ptr == nullptr || tok_ptr->type != SCC_TOK_COMMA) {
break;
}
scc_pos_t pos = tok_ptr->loc;
scc_parser_next_consume(parser, null);
scc_parser_next_consume(parser, nullptr);
scc_ast_expr_t *right = scc_parse_assignment_expression(parser);
if (!right) {
parser_sync(parser);
return null;
return nullptr;
}
scc_ast_expr_t *expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
Assert(expr != nullptr);
scc_ast_expr_binary_init(expr, SCC_AST_OP_COMMA, left, right, pos);
left = expr;
}