feat: guard 语句 guard x >= 0 else { return -1; }

This commit is contained in:
2026-06-05 20:49:03 +08:00
parent 18172ca724
commit c6e492662e
5 changed files with 27 additions and 3 deletions
+13
View File
@@ -621,6 +621,19 @@ static AstNode* parse_statement(Parser* p, ErrorInfo* error) {
return parse_match_stmt(p, error);
}
if (t->kind == TOK_GUARD) {
// guard expr else { ... } → if !(expr) { ... }
const Token* guard_tok = advance(p);
AstNode* cond = parse_expr(p, error);
if (!cond) return NULL;
if (!expect(p, TOK_ELSE, error, "guard 缺少 'else'")) return NULL;
AstNode* body = parse_block(p, error);
if (!body) return NULL;
// 去糖: if !cond { body }
AstNode* not_cond = ast_make_unary(p->arena, OP_NOT, cond, tok_loc(guard_tok));
return ast_make_if(p->arena, not_cond, body, NULL, tok_loc(guard_tok));
}
if (t->kind == TOK_RETURN) {
advance(p);
if (match(p, TOK_SEMICOLON)) {