summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorChloe M. <chloe@mirocom.org>2026-05-23 19:01:51 -0400
committerChloe M. <chloe@mirocom.org>2026-05-23 19:01:51 -0400
commit71f232282a6bb3a45bd020e3681ac0dd90371c63 (patch)
treec0d9a93318c13360fced71a6d71c6054c1a89f99 /core
parentdba31ae0adcef456fe2405f7c59afae716b3bef9 (diff)
core: tokbuf: Fix up token buffer
Signed-off-by: Chloe M. <chloe@mirocom.org>
Diffstat (limited to 'core')
-rw-r--r--core/tokbuf.c37
1 files changed, 27 insertions, 10 deletions
diff --git a/core/tokbuf.c b/core/tokbuf.c
index 6df5686..1c9783f 100644
--- a/core/tokbuf.c
+++ b/core/tokbuf.c
@@ -29,26 +29,48 @@ tokbuf_init(struct tokbuf *tokbuf)
return 0;
}
+#include <stdio.h>
+
int
tokbuf_push(struct tokbuf *tokbuf, struct token *tok)
{
void *p;
+ size_t newcap;
if (tokbuf == NULL || tok == NULL) {
return -1;
}
- if ((tokbuf->head++) >= tokbuf->cap - 1) {
- tokbuf->cap += 8;
- p = realloc(tokbuf->buf, sizeof(struct token) * tokbuf->cap);
- if (tokbuf->buf == NULL) {
+ if (tokbuf->head >= tokbuf->cap) {
+ newcap = tokbuf->cap * 8;
+ p = realloc(tokbuf->buf, sizeof(struct token) * newcap);
+ if (p == NULL) {
return -1;
}
tokbuf->buf = p;
+ tokbuf->cap = newcap;
+ }
+
+ printf("%d %d\n", tokbuf->head, tok->type);
+ tokbuf->buf[tokbuf->head++] = *tok;
+ return 0;
+}
+
+int
+tokbuf_pop(struct tokbuf *tokbuf, struct token *res)
+{
+ if (tokbuf == NULL || res == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (tokbuf->head >= 0) {
+ errno = EAGAIN;
+ return -1;
}
- tokbuf->buf[tokbuf->head] = *tok;
+ *res = tokbuf->buf[tokbuf->head--];
return 0;
}
@@ -62,11 +84,6 @@ tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res)
return -1;
}
- if (noff == 0) {
- *res = tokbuf->buf[tokbuf->head + 1];
- return 0;
- }
-
if ((off = (tokbuf->head - noff + 1)) < 0) {
off = 0;
}