2026-05-23 02:57:50 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2026, Chloe M.
|
|
|
|
|
* Provided under the BSD-3 clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <stdint.h>
|
2026-05-23 11:35:31 -04:00
|
|
|
#include <stdlib.h>
|
2026-05-23 02:57:50 -04:00
|
|
|
#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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokbuf->head = 0;
|
2026-05-23 11:35:31 -04:00
|
|
|
tokbuf->cap = TOKBUF_CAP;
|
|
|
|
|
tokbuf->buf = malloc(sizeof(struct token) * TOKBUF_CAP);
|
|
|
|
|
if (tokbuf->buf == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 02:57:50 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 19:01:51 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2026-05-23 02:57:50 -04:00
|
|
|
int
|
|
|
|
|
tokbuf_push(struct tokbuf *tokbuf, struct token *tok)
|
|
|
|
|
{
|
2026-05-23 11:35:31 -04:00
|
|
|
void *p;
|
2026-05-23 19:01:51 -04:00
|
|
|
size_t newcap;
|
2026-05-23 11:35:31 -04:00
|
|
|
|
2026-05-23 02:57:50 -04:00
|
|
|
if (tokbuf == NULL || tok == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 19:01:51 -04:00
|
|
|
if (tokbuf->head >= tokbuf->cap) {
|
|
|
|
|
newcap = tokbuf->cap * 8;
|
|
|
|
|
p = realloc(tokbuf->buf, sizeof(struct token) * newcap);
|
|
|
|
|
if (p == NULL) {
|
2026-05-23 11:35:31 -04:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokbuf->buf = p;
|
2026-05-23 19:01:51 -04:00
|
|
|
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;
|
2026-05-23 02:57:50 -04:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 19:01:51 -04:00
|
|
|
*res = tokbuf->buf[tokbuf->head--];
|
2026-05-23 02:57:50 -04:00
|
|
|
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 ((off = (tokbuf->head - noff + 1)) < 0) {
|
|
|
|
|
off = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*res = tokbuf->buf[off];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2026-05-23 11:35:31 -04:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
tokbuf_destroy(struct tokbuf *tokbuf)
|
|
|
|
|
{
|
|
|
|
|
if (tokbuf == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tokbuf->buf != NULL) {
|
|
|
|
|
free(tokbuf->buf);
|
|
|
|
|
tokbuf->buf = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|