From 7ca45808939579b427f32641b0dcb9cb585b8e80 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Sat, 23 May 2026 02:57:50 -0400 Subject: core: Add token buffer Signed-off-by: Chloe M. --- include/cescal/state.h | 7 +++++-- include/cescal/tokbuf.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 include/cescal/tokbuf.h (limited to 'include') diff --git a/include/cescal/state.h b/include/cescal/state.h index c604270..e832bc9 100644 --- a/include/cescal/state.h +++ b/include/cescal/state.h @@ -8,16 +8,19 @@ #include #include "cescal/readbuf.h" +#include "cescal/tokbuf.h" /* * Compiler state machine * - * @in_fd: Input file descriptor - * @rb: Read buffer + * @in_fd: Input file descriptor + * @rb: Read buffer + * @tokbuf: Token buffer */ struct cescal_state { int in_fd; struct readbuf rb; + struct tokbuf tokbuf; }; /* diff --git a/include/cescal/tokbuf.h b/include/cescal/tokbuf.h new file mode 100644 index 0000000..528cb02 --- /dev/null +++ b/include/cescal/tokbuf.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause + */ + +#ifndef CESCAL_TOKBUF_H +#define CESCAL_TOKBUF_H 1 + +#include +#include +#include "cescal/token.h" + +#define TOKBUF_CAP 4 + +struct tokbuf { + struct token buf[TOKBUF_CAP]; + uint8_t head; +}; + +/* + * Initialize the token buffer + * + * @tokbuf: Token buffer to initialize + * + * Returns zero on success + */ +int tokbuf_init(struct tokbuf *tokbuf); + +/* + * Push a token onto the token buffer + * + * @tokbuf: Token buffer to push to + * @tok: Token to push + * + * Returns zero on success + */ +int tokbuf_push(struct tokbuf *tokbuf, struct token *tok); + +/* + * Peek at the token buffer from a negative offset backwards + * + * @tokbuf: Token buffer to read from + * @noff: Negative offset + * @res: Result is written here + * + * Returns zero on success + */ +int tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res); + +#endif /* !CESCAL_TOKBUF_H */ -- cgit v1.2.3