From e2c729a0f9db3fbb0bbd6ff3af87d33a163aa6bc Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sun, 15 Feb 2026 22:58:06 -0500 Subject: [PATCH] parser+backend: Add support for public symbols Signed-off-by: Ian Moffett --- backend/x86_64.c | 10 +++++++++- core/codegen.c | 2 +- core/parser.c | 10 ++++++++++ inc/rifle/mu.h | 4 +++- inc/rifle/symbol.h | 2 ++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/backend/x86_64.c b/backend/x86_64.c index 07b2b41..9b597e3 100644 --- a/backend/x86_64.c +++ b/backend/x86_64.c @@ -2,12 +2,20 @@ #include "rifle/mu.h" int -mu_gen_label(struct rifle_state *state, const char *name) +mu_gen_label(struct rifle_state *state, const char *name, bool global) { if (state == NULL || name == NULL) { return -1; } + if (global) { + fprintf( + state->out_fp, + "[global %s]\n", + name + ); + } + fprintf( state->out_fp, "%s:\n", diff --git a/core/codegen.c b/core/codegen.c index b455b00..7a8fde6 100644 --- a/core/codegen.c +++ b/core/codegen.c @@ -18,7 +18,7 @@ resolve_func(struct rifle_state *state, struct ast_node *root) return -1; } - return mu_gen_label(state, symbol->name); + return mu_gen_label(state, symbol->name, symbol->pub); } int diff --git a/core/parser.c b/core/parser.c index 2447987..d9623d4 100644 --- a/core/parser.c +++ b/core/parser.c @@ -1,5 +1,6 @@ #include #include +#include #include "rifle/parser.h" #include "rifle/token.h" #include "rifle/trace.h" @@ -402,8 +403,10 @@ parse_lbrace(struct rifle_state *state, tt_t scope, struct token *tok) static int parse_func(struct rifle_state *state, struct token *tok, struct ast_node **res) { + struct token *prevtok; struct ast_node *root; struct symbol *symbol; + bool is_pub = false; int error; if (state == NULL || tok == NULL) { @@ -418,6 +421,12 @@ parse_func(struct rifle_state *state, struct token *tok, struct ast_node **res) return -1; } + /* Is this marked as public? */ + if ((prevtok = tokbuf_lookbehind(&state->tokbuf, 1)) != NULL) { + if (prevtok->type == TT_PUB) + is_pub = true; + } + /* EXPECT */ if (parse_expect(state, TT_IDENT, tok) < 0) { return -1; @@ -440,6 +449,7 @@ parse_func(struct rifle_state *state, struct token *tok, struct ast_node **res) return -1; } + symbol->pub = is_pub; root->symbol = symbol; /* EXPECT '(' */ diff --git a/inc/rifle/mu.h b/inc/rifle/mu.h index 6e15746..831ee13 100644 --- a/inc/rifle/mu.h +++ b/inc/rifle/mu.h @@ -3,6 +3,7 @@ #include #include +#include #include "rifle/state.h" /* @@ -10,9 +11,10 @@ * * @state: Compiler state * @name: Name of label to generate + * @global: If true, symbol is to be marked global * * Returns zero on success */ -int mu_gen_label(struct rifle_state *state, const char *name); +int mu_gen_label(struct rifle_state *state, const char *name, bool global); #endif /* !RIFLE_MU_H */ diff --git a/inc/rifle/symbol.h b/inc/rifle/symbol.h index bae1bf4..49dc97d 100644 --- a/inc/rifle/symbol.h +++ b/inc/rifle/symbol.h @@ -22,11 +22,13 @@ typedef enum { * * @name: Name of symbol * @type: Type associated with symbol + * @pub: If set, symbol is public * @link: Queue link */ struct symbol { char *name; symtype_t type; + uint8_t pub : 1; TAILQ_ENTRY(symbol) link; };