From a9b5fef2eb126500974a1cfe9ce4a8f7cc6e0490 Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 3 Jun 2025 01:20:50 +0200 Subject: Parse all 4 types of for-loops Signed-off-by: Mel --- boot/tree.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'boot/tree.c') diff --git a/boot/tree.c b/boot/tree.c index 64eb23b..3ae90b8 100644 --- a/boot/tree.c +++ b/boot/tree.c @@ -592,6 +592,7 @@ enum Statement_Kind // NOTE: a block could be an expression in the future. STATEMENT_BLOCK, STATEMENT_CONDITIONAL, + STATEMENT_LOOP, }; struct Statement_Value_Expression @@ -624,12 +625,25 @@ struct Statement_Value_Conditional uint condition_count; }; +struct Statement_Value_Loop +{ + // exists for iterator-style + c-style loops. + struct Statement* declaration; + // exists for all loop types, except for infinite loops. + struct Expression* condition; + // exists for c-style loops. + struct Expression* iteration; + + struct Block_Node body; +}; + union Statement_Value { struct Statement_Value_Expression expression; struct Statement_Value_Declaration declaration; struct Statement_Value_Block block; struct Statement_Value_Conditional conditional; + struct Statement_Value_Loop loop; }; struct Statement @@ -731,6 +745,29 @@ statement_print(const struct Statement* statement) printf(")"); break; } + case STATEMENT_LOOP: { + printf("(loop "); + if (statement->value.loop.declaration) { + printf("(declaration "); + statement_print(statement->value.loop.declaration); + printf(") "); + } + + if (statement->value.loop.condition) { + printf("(condition "); + expression_print(statement->value.loop.condition); + printf(") "); + } + + if (statement->value.loop.iteration) { + printf("(iteration "); + expression_print(statement->value.loop.iteration); + printf(") "); + } + + block_node_print(&statement->value.loop.body); + break; + } default: failure("unexpected statement kind passed to `statement_print`"); break; -- cgit 1.4.1