parser+backend: Add support for public symbols

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-02-15 22:58:06 -05:00
parent 022770df16
commit e2c729a0f9
5 changed files with 25 additions and 3 deletions

View File

@@ -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",

View File

@@ -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

View File

@@ -1,5 +1,6 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#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 <IDENT> */
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 '(' */

View File

@@ -3,6 +3,7 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#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 */

View File

@@ -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;
};