From ed094396f380fb0679194b1e3c51faf996dc2bfe Mon Sep 17 00:00:00 2001 From: Baitinq Date: Sun, 13 Jul 2025 23:41:40 +0200 Subject: Boostrap: Support break and continue --- src/bootstrap/codegen.pry | 20 ++++++++++++++++++++ src/bootstrap/parser.pry | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/bootstrap/codegen.pry b/src/bootstrap/codegen.pry index 99324fb..5f63038 100644 --- a/src/bootstrap/codegen.pry +++ b/src/bootstrap/codegen.pry @@ -1127,6 +1127,18 @@ let codegen_generate_function_call_statement = (c: *codegen, statement: *Node) = return codegen_create_variable(c, v); }; +let codegen_generate_break_statement = (c: *codegen) => i64 { + assert((*c).whil_loop_exit != cast(LLVMBasicBlockRef, null)); + LLVMBuildBr((*c).builder, (*c).whil_loop_exit); + return 0; +}; + +let codegen_generate_continue_statement = (c: *codegen) => i64 { + assert((*c).whil_block != cast(LLVMBasicBlockRef, null)); + LLVMBuildBr((*c).builder, (*c).whil_block); + return 0; +}; + let codegen_generate_if_statement = (c: *codegen, statement: *NODE_IF_STATEMENT_DATA) => *void { let condition_value = codegen_generate_expression_value(c, (*statement).condition, cast(*i8, null)); assert(condition_value != cast(*Variable, null)); @@ -1230,6 +1242,14 @@ let codegen_generate_statement = (c: *codegen, statement: *Node) => i64 { return codegen_generate_import_declaration(c, cast(*NODE_IMPORT_DECLARATION_DATA, stmt.data)); }; + if stmt.type == NODE_CONTINUE_STATEMENT { + return codegen_generate_continue_statement(c); + }; + + if stmt.type == NODE_BREAK_STATEMENT { + return codegen_generate_break_statement(c); + }; + printf("ASSERT 3 %d\n", stmt.type); assert(false); diff --git a/src/bootstrap/parser.pry b/src/bootstrap/parser.pry index cf5cabd..f77bc77 100644 --- a/src/bootstrap/parser.pry +++ b/src/bootstrap/parser.pry @@ -1389,6 +1389,39 @@ let parser_parse_statement = (p: *parser) => *Node { }; return retu; }; + + /* Break and continue */ + let retu = parser_accept_parse(p, (ip: *parser) => *Node { + if parser_accept_token(ip, TOKEN_BREAK) == cast(*token, null) { + return cast(*Node, null); + }; + + let n = Node{}; + n.type = NODE_BREAK_STATEMENT; + return create_node(ip, n); + }); + if retu != cast(*Node, null) { + if parser_accept_token(p, TOKEN_SEMICOLON) == cast(*token, null) { + return cast(*Node, null); + }; + return retu; + }; + + let retu = parser_accept_parse(p, (ip: *parser) => *Node { + if parser_accept_token(ip, TOKEN_CONTINUE) == cast(*token, null) { + return cast(*Node, null); + }; + + let n = Node{}; + n.type = NODE_CONTINUE_STATEMENT; + return create_node(ip, n); + }); + if retu != cast(*Node, null) { + if parser_accept_token(p, TOKEN_SEMICOLON) == cast(*token, null) { + return cast(*Node, null); + }; + return retu; + }; printf("None\n"); -- cgit 1.4.1