From 1c75b1857c0b5f487eed443dc1bfb605d69a598a Mon Sep 17 00:00:00 2001 From: Akuli Date: Wed, 15 Jan 2025 14:17:08 +0200 Subject: [PATCH] bootstrap upgrade --- bootstrap_compiler/build_cfg.c | 8 +++++--- bootstrap_compiler/free.c | 5 ++++- bootstrap_compiler/jou_compiler.h | 3 ++- bootstrap_compiler/parse.c | 13 ++++++++++++- bootstrap_compiler/typecheck.c | 10 ++++++---- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/bootstrap_compiler/build_cfg.c b/bootstrap_compiler/build_cfg.c index 8332a35d..d1d182bd 100644 --- a/bootstrap_compiler/build_cfg.c +++ b/bootstrap_compiler/build_cfg.c @@ -873,11 +873,12 @@ static void build_match_statament(struct State *st, const AstMatchStatement *mat CfBlock *done = add_block(st); for (int i = 0; i < match_stmt->ncases; i++) { - const LocalVariable *case_obj_enum = build_expression(st, &match_stmt->cases[i].case_obj); + for (AstExpression *caseobj = match_stmt->cases[i].case_objs; caseobj < &match_stmt->cases[i].case_objs[match_stmt->cases[i].n_case_objs]; caseobj++) { + const LocalVariable *case_obj_enum = build_expression(st, caseobj); LocalVariable *case_obj_int = add_local_var(st, intType); - add_unary_op(st, match_stmt->cases[i].case_obj.location, CF_ENUM_TO_INT32, case_obj_enum, case_obj_int); + add_unary_op(st, caseobj->location, CF_ENUM_TO_INT32, case_obj_enum, case_obj_int); - const LocalVariable *cond = build_binop(st, AST_EXPR_EQ, match_stmt->cases[i].case_obj.location, match_obj_int, case_obj_int, boolType); + const LocalVariable *cond = build_binop(st, AST_EXPR_EQ, caseobj->location, match_obj_int, case_obj_int, boolType); CfBlock *then = add_block(st); CfBlock *otherwise = add_block(st); @@ -885,6 +886,7 @@ static void build_match_statament(struct State *st, const AstMatchStatement *mat build_body(st, &match_stmt->cases[i].body); add_jump(st, NULL, done, done, otherwise); } + } build_body(st, &match_stmt->case_underscore); add_jump(st, NULL, done, done, done); diff --git a/bootstrap_compiler/free.c b/bootstrap_compiler/free.c index d7c528b3..a88c4786 100644 --- a/bootstrap_compiler/free.c +++ b/bootstrap_compiler/free.c @@ -158,7 +158,10 @@ void free_ast_statement(const AstStatement *stmt) case AST_STMT_MATCH: free_expression(&stmt->data.match.match_obj); for (int i = 0; i < stmt->data.match.ncases; i++) { - free_expression(&stmt->data.match.cases[i].case_obj); + for (AstExpression *caseobj = stmt->data.match.cases[i].case_objs; caseobj < &stmt->data.match.cases[i].case_objs[stmt->data.match.cases[i].n_case_objs]; caseobj++) { + free_expression(caseobj); + } + free(stmt->data.match.cases[i].case_objs); free_ast_body(&stmt->data.match.cases[i].body); } free(stmt->data.match.cases); diff --git a/bootstrap_compiler/jou_compiler.h b/bootstrap_compiler/jou_compiler.h index 80ddc7f5..d6a1b552 100644 --- a/bootstrap_compiler/jou_compiler.h +++ b/bootstrap_compiler/jou_compiler.h @@ -267,7 +267,8 @@ struct AstForLoop { AstBody body; }; struct AstCase { - AstExpression case_obj; + AstExpression *case_objs; + int n_case_objs; AstBody body; }; struct AstMatchStatement { diff --git a/bootstrap_compiler/parse.c b/bootstrap_compiler/parse.c index cca79daf..8cb8f794 100644 --- a/bootstrap_compiler/parse.c +++ b/bootstrap_compiler/parse.c @@ -722,9 +722,20 @@ static AstMatchStatement parse_match_statement(ParserState *ps) ps->tokens++; result.case_underscore = parse_body(ps); } else { + List(AstExpression) case_objs = {0}; + while(1){ + Append(&case_objs, parse_expression(ps)); + if (is_operator(ps->tokens, "|")) + ps->tokens++; + else if (is_operator(ps->tokens, ":")) + break; + else + fail_with_parse_error(ps->tokens, "'|' or ':'"); + } result.cases = realloc(result.cases, sizeof result.cases[0] * (result.ncases + 1)); result.cases[result.ncases++] = (AstCase){ - .case_obj = parse_expression(ps), + .case_objs = case_objs.ptr, + .n_case_objs = case_objs.len, .body = parse_body(ps), }; } diff --git a/bootstrap_compiler/typecheck.c b/bootstrap_compiler/typecheck.c index 728af15c..224572ca 100644 --- a/bootstrap_compiler/typecheck.c +++ b/bootstrap_compiler/typecheck.c @@ -1292,10 +1292,12 @@ static void typecheck_match_statement(FileTypes *ft, AstMatchStatement *match_st assert(mtype->kind == TYPE_ENUM); for (int i = 0; i < match_stmt->ncases; i++) { - typecheck_expression_with_implicit_cast( - ft, &match_stmt->cases[i].case_obj, mtype, - "case value of type FROM cannot be matched against TO" - ); + for (int k = 0; k < match_stmt->cases[i].n_case_objs; k++) { + typecheck_expression_with_implicit_cast( + ft, &match_stmt->cases[i].case_objs[k], mtype, + "case value of type FROM cannot be matched against TO" + ); + } typecheck_body(ft, &match_stmt->cases[i].body); } typecheck_body(ft, &match_stmt->case_underscore);