From 07db3ee834b53c688f11ff49b2ceab47cfbe605a Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sun, 15 Feb 2026 23:11:52 -0500 Subject: [PATCH] parser+codegen: Handle function epilogue Signed-off-by: Ian Moffett --- core/codegen.c | 4 ++++ core/parser.c | 27 ++++++++++++++++++++++----- inc/rifle/ast.h | 2 ++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/core/codegen.c b/core/codegen.c index 7a8fde6..b52e56c 100644 --- a/core/codegen.c +++ b/core/codegen.c @@ -13,6 +13,10 @@ resolve_func(struct rifle_state *state, struct ast_node *root) return -1; } + if (root->epilogue) { + return mu_gen_ret(state); + } + if ((symbol = root->symbol) == NULL) { trace_error(state, "internal: no symbol associated with func node\n"); return -1; diff --git a/core/parser.c b/core/parser.c index 09eabae..ff32eb1 100644 --- a/core/parser.c +++ b/core/parser.c @@ -495,6 +495,9 @@ parse_func(struct rifle_state *state, struct token *tok, struct ast_node **res) static int parse_rbrace(struct rifle_state *state, struct token *tok, struct ast_node **res) { + struct ast_node *root = NULL; + tt_t scope; + if (state == NULL || tok == NULL) { return -1; } @@ -507,7 +510,21 @@ parse_rbrace(struct rifle_state *state, struct token *tok, struct ast_node **res return -1; } - scope_pop(state); + scope = scope_pop(state); + switch (scope) { + case TT_F: + if (ast_alloc_node(state, AST_FUNC, &root) < 0) { + trace_error(state, "failed to allocate AST_FUNC\n"); + return -1; + } + + root->epilogue = 1; + break; + default: + break; + } + + *res = root; return 0; } @@ -537,11 +554,11 @@ parse_begin(struct rifle_state *state) utok1(state, &tok); return -1; } - } - if (root != NULL) { - if (cg_resolve_node(state, root) < 0) - return -1; + if (root != NULL) { + if (cg_resolve_node(state, root) < 0) + return -1; + } } return 0; diff --git a/inc/rifle/ast.h b/inc/rifle/ast.h index 5a4efa4..9fd2651 100644 --- a/inc/rifle/ast.h +++ b/inc/rifle/ast.h @@ -25,6 +25,7 @@ typedef enum { * @left: Left leaf * @right: Right leaf * @symbol: Symbol associated with node + * @epilogue: If true, represents block epilogue */ struct ast_node { ast_type_t type; @@ -32,6 +33,7 @@ struct ast_node { struct ast_node *left; struct ast_node *right; struct symbol *symbol; + uint8_t epilogue : 1; }; /*