Files
rifle/core/state.c
Ian Moffett 52826975e8 core: Add initial symbol table impl
For now, symbol lookups are to require a linear scan. We can speed this
up in the future by adding a cache and further down the line considering
a hashmap.

Signed-off-by: Ian Moffett <ian@mirocom.org>
2026-02-15 13:06:55 -05:00

53 lines
1.0 KiB
C

#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "rifle/state.h"
int
rifle_state_init(struct rifle_state *state, const char *in_path)
{
if (state == NULL || in_path == NULL) {
return -1;
}
memset(state, 0, sizeof(*state));
state->in_fd = open(in_path, O_RDONLY);
if (state->in_fd < 0) {
return -1;
}
if (ptrbox_init(&state->ptrbox) < 0) {
close(state->in_fd);
return -1;
}
if (symbol_table_init(&state->symtab) < 0) {
close(state->in_fd);
ptrbox_destroy(&state->ptrbox);
return -1;
}
if (tokbuf_init(&state->tokbuf) < 0) {
ptrbox_destroy(&state->ptrbox);
symbol_table_destroy(&state->symtab);
close(state->in_fd);
return -1;
}
state->line_num = 1;
return 0;
}
void
rifle_state_destroy(struct rifle_state *state)
{
if (state == NULL) {
return;
}
close(state->in_fd);
state->in_fd = -1;
tokbuf_destroy(&state->tokbuf);
symbol_table_destroy(&state->symtab);
}