Compare commits

..

3 Commits

Author SHA1 Message Date
chloe 8483d3c445 core: parser: Add initial preprocessing stubs
Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-05-23 19:03:15 -04:00
chloe 71f232282a core: tokbuf: Fix up token buffer
Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-05-23 19:01:51 -04:00
chloe dba31ae0ad core: state: Ensure compiler FSM to zeroed
Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-05-23 18:08:17 -04:00
5 changed files with 90 additions and 18 deletions
+4 -2
View File
@@ -29,8 +29,10 @@ compile(const char *pathname)
return -1;
}
if (parser_parse(&st) < 0) {
return -1;
for (int i = 0; i < 2; ++i) {
if (parser_parse(&st) < 0) {
return -1;
}
}
state_close(&st);
+47 -5
View File
@@ -58,6 +58,35 @@ static const char *toktab[] = {
[TT_U64] = qtok("u64")
};
/*
* Push a token to the token buffer in the preprocessing stage
*
* @tokbuf: Token buffer to push to
* @tok: Token to push
*
* Returns zero on success
*/
static int
preprocessor_push(struct tokbuf *tokbuf, struct token *tok)
{
if (tokbuf == NULL || tok == NULL) {
return -1;
}
switch (tok->type) {
case TT_IFNDEF:
case TT_IFDEF:
case TT_DEFINE:
break;
default:
if (tokbuf_push(tokbuf, tok) < 0) {
return -1;
}
}
return 0;
}
static int
parser_nom(struct cescal_state *state, struct token *res)
{
@@ -68,12 +97,24 @@ parser_nom(struct cescal_state *state, struct token *res)
return -1;
}
if (lexer_nom(state, &tok) < 0) {
return -1;
}
switch (state->pass) {
case 0: /* Pre-processor */
if (lexer_nom(state, &tok) < 0) {
return -1;
}
if (tokbuf_push(&state->tokbuf, &tok) < 0) {
return -1;
printf("* %s\n", tokstr(&tok));
if (preprocessor_push(&state->tokbuf, &tok) < 0) {
return -1;
}
break;
case 1: /* Parse */
if (tokbuf_pop(&state->tokbuf, &tok) < 0) {
return -1;
}
break;
}
*res = tok;
@@ -94,5 +135,6 @@ parser_parse(struct cescal_state *state)
cc_trace("got token %s\n", tokstr(&tok));
}
++state->pass;
return 0;
}
+2 -1
View File
@@ -6,6 +6,7 @@
#include <stdint.h>
#include <stddef.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "cescal/state.h"
@@ -18,6 +19,7 @@ state_init(struct cescal_state *state, const char *pathname)
return -1;
}
memset(state, 0, sizeof(*state));
state->in_fd = open(pathname, O_RDONLY);
if (state->in_fd < 0) {
return -1;
@@ -38,7 +40,6 @@ state_init(struct cescal_state *state, const char *pathname)
return -1;
}
state->pass = 0;
return 0;
}
+27 -10
View File
@@ -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;
}
tokbuf->buf[tokbuf->head] = *tok;
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;
}
*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;
}
+10
View File
@@ -37,6 +37,16 @@ int tokbuf_init(struct tokbuf *tokbuf);
*/
int tokbuf_push(struct tokbuf *tokbuf, struct token *tok);
/*
* Pop a token from the token buffer
*
* @tokbuf: Token buffer to pop from
* @res: Result is written here
*
* Returns zero on success
*/
int tokbuf_pop(struct tokbuf *tokbuf, struct token *res);
/*
* Peek at the token buffer from a negative offset backwards
*