summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/state.c5
-rw-r--r--core/tokbuf.c62
-rw-r--r--include/cescal/state.h7
-rw-r--r--include/cescal/tokbuf.h50
4 files changed, 122 insertions, 2 deletions
diff --git a/core/state.c b/core/state.c
index 7d80acd..912508c 100644
--- a/core/state.c
+++ b/core/state.c
@@ -23,6 +23,11 @@ state_init(struct cescal_state *state, const char *pathname)
return -1;
}
+ if (tokbuf_init(&state->tokbuf) < 0) {
+ close(state->in_fd);
+ return -1;
+ }
+
return 0;
}
diff --git a/core/tokbuf.c b/core/tokbuf.c
new file mode 100644
index 0000000..e880c15
--- /dev/null
+++ b/core/tokbuf.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2026, Chloe M.
+ * Provided under the BSD-3 clause
+ */
+
+#include <sys/types.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <errno.h>
+#include "cescal/tokbuf.h"
+
+int
+tokbuf_init(struct tokbuf *tokbuf)
+{
+ if (tokbuf == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ memset(tokbuf->buf, 0, sizeof(tokbuf->buf));
+ tokbuf->head = 0;
+ return 0;
+}
+
+int
+tokbuf_push(struct tokbuf *tokbuf, struct token *tok)
+{
+ if (tokbuf == NULL || tok == NULL) {
+ return -1;
+ }
+
+ if ((tokbuf->head++) >= TOKBUF_CAP) {
+ tokbuf->head = 0;
+ }
+
+ tokbuf->buf[tokbuf->head] = *tok;
+ return 0;
+}
+
+int
+tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res)
+{
+ ssize_t off;
+
+ if (tokbuf == NULL || res == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (noff == 0) {
+ *res = tokbuf->buf[tokbuf->head + 1];
+ return 0;
+ }
+
+ if ((off = (tokbuf->head - noff + 1)) < 0) {
+ off = 0;
+ }
+
+ *res = tokbuf->buf[off];
+ return 0;
+}
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 <stdint.h>
#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 <stdint.h>
+#include <stddef.h>
+#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 */