Compare commits
6 Commits
a0b204fadd
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 85c21a81bb | |||
| 5c3c78f561 | |||
| 8483d3c445 | |||
| 71f232282a | |||
| dba31ae0ad | |||
| 833978f414 |
+4
-2
@@ -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);
|
||||
|
||||
+46
-5
@@ -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,23 @@ 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;
|
||||
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 +134,6 @@ parser_parse(struct cescal_state *state)
|
||||
cc_trace("got token %s\n", tokstr(&tok));
|
||||
}
|
||||
|
||||
++state->pass;
|
||||
return 0;
|
||||
}
|
||||
|
||||
+9
-1
@@ -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;
|
||||
@@ -33,12 +35,16 @@ state_init(struct cescal_state *state, const char *pathname)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (symbol_table_init(&state->symtab) < 0) {
|
||||
close(state->in_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ptrbox_init(&state->ptrbox) < 0) {
|
||||
close(state->in_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
state->pass = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -48,4 +54,6 @@ state_close(struct cescal_state *state)
|
||||
close(state->in_fd);
|
||||
state->in_fd = -1;
|
||||
ptrbox_destroy(&state->ptrbox);
|
||||
tokbuf_destroy(&state->tokbuf);
|
||||
symbol_table_destroy(&state->symtab);
|
||||
}
|
||||
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (c) 2026, Chloe M.
|
||||
* Provided under the BSD-3 clause
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "cescal/symbol.h"
|
||||
|
||||
/*
|
||||
* Push a symbol into a symbol table
|
||||
*
|
||||
* @symtab: Symbol table to push to
|
||||
* @symbol: Symbol to push
|
||||
*
|
||||
* Returns zero on success
|
||||
*/
|
||||
static int
|
||||
symbol_table_push(struct symbol_table *symtab, struct symbol *symbol)
|
||||
{
|
||||
if (symtab == NULL || symbol == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
symbol->next = NULL;
|
||||
if (symtab->head == NULL || symtab->tail == NULL) {
|
||||
symtab->head = symbol;
|
||||
symtab->tail = symbol;
|
||||
} else {
|
||||
symtab->head->next = symbol;
|
||||
symtab->head = symbol;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
symbol_table_init(struct symbol_table *symtab)
|
||||
{
|
||||
if (symtab == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
symtab->head = NULL;
|
||||
symtab->tail = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
symbol_allocate(struct symbol_table *symtab, const char *name,
|
||||
symtype_t type, struct symbol **res)
|
||||
{
|
||||
struct symbol *symbol;
|
||||
|
||||
if (symtab == NULL || name == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (res == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
symbol = malloc(sizeof(*symbol));
|
||||
if (symbol == NULL) {
|
||||
errno = -ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
symbol->name = strdup(name);
|
||||
symbol->type = type;
|
||||
if (symbol_table_push(symtab, symbol) < 0) {
|
||||
free(symbol);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*res = symbol;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
symbol_table_destroy(struct symbol_table *symtab)
|
||||
{
|
||||
struct symbol *symbol, *tmp;
|
||||
|
||||
if (symtab == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
symbol = symtab->tail;
|
||||
while (symbol != NULL) {
|
||||
tmp = symbol;
|
||||
if (tmp->name != NULL)
|
||||
free(tmp->name);
|
||||
free(tmp);
|
||||
symbol = symbol->next;
|
||||
}
|
||||
}
|
||||
|
||||
struct symbol *
|
||||
symbol_byname(struct symbol_table *symtab, const char *name)
|
||||
{
|
||||
struct symbol *symbol;
|
||||
|
||||
if (symtab == NULL || name == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
symbol = symtab->head;
|
||||
while (symbol != NULL) {
|
||||
if (*symbol->name != *name) {
|
||||
symbol = symbol->next;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(symbol->name, name) == 0) {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
symbol = symbol->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
+50
-9
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
@@ -18,23 +19,55 @@ tokbuf_init(struct tokbuf *tokbuf)
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(tokbuf->buf, 0, sizeof(tokbuf->buf));
|
||||
tokbuf->head = 0;
|
||||
tokbuf->cap = TOKBUF_CAP;
|
||||
tokbuf->buf = malloc(sizeof(struct token) * TOKBUF_CAP);
|
||||
if (tokbuf->buf == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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->head = 0;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -48,11 +81,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;
|
||||
}
|
||||
@@ -60,3 +88,16 @@ tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res)
|
||||
*res = tokbuf->buf[off];
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
tokbuf_destroy(struct tokbuf *tokbuf)
|
||||
{
|
||||
if (tokbuf == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (tokbuf->buf != NULL) {
|
||||
free(tokbuf->buf);
|
||||
tokbuf->buf = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "cescal/readbuf.h"
|
||||
#include "cescal/tokbuf.h"
|
||||
#include "cescal/ptrbox.h"
|
||||
#include "cescal/symbol.h"
|
||||
|
||||
/*
|
||||
* Compiler state machine
|
||||
@@ -20,6 +21,7 @@
|
||||
* @ptrbox: Global pointer box
|
||||
* @lex_putback: Lexer putback buffer
|
||||
* @pass: Current pass
|
||||
* @symtab: Symbol table
|
||||
*/
|
||||
struct cescal_state {
|
||||
int in_fd;
|
||||
@@ -28,6 +30,7 @@ struct cescal_state {
|
||||
struct ptrbox ptrbox;
|
||||
char lex_putback;
|
||||
uint8_t pass;
|
||||
struct symbol_table symtab;
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2026, Chloe M.
|
||||
* Provided under the BSD-3 clause
|
||||
*/
|
||||
|
||||
#ifndef CESCAL_SYMBOL_H
|
||||
#define CESCAL_SYMBOL_H 1
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/*
|
||||
* Represents valid symbol types
|
||||
*
|
||||
* @SYMBOL_NONE: No associated type
|
||||
* @SYMBOL_MACRO: This symbol is a macro
|
||||
*/
|
||||
typedef enum {
|
||||
SYMBOL_NONE,
|
||||
SYMBOL_MACRO
|
||||
} symtype_t;
|
||||
|
||||
/*
|
||||
* Represents a program symbol
|
||||
*
|
||||
* @name: Name of symbol
|
||||
* @type: Symbol type
|
||||
* @next: Next symbol in list
|
||||
*/
|
||||
struct symbol {
|
||||
char *name;
|
||||
symtype_t type;
|
||||
struct symbol *next;
|
||||
};
|
||||
|
||||
/*
|
||||
* Represents a symbol table which holds one or
|
||||
* more symbols
|
||||
*
|
||||
* @head: List head
|
||||
* @tail: List tail
|
||||
*
|
||||
* XXX TODO: This should be a hashmap instead of a linked
|
||||
* list.
|
||||
*/
|
||||
struct symbol_table {
|
||||
struct symbol *head;
|
||||
struct symbol *tail;
|
||||
};
|
||||
|
||||
/*
|
||||
* Allocate a new symbol and add it to the specified symbol table
|
||||
*
|
||||
* @name: Name of symbol to allocate
|
||||
* @type: Symbol type to assign
|
||||
* @res: Result is written here
|
||||
*
|
||||
* Returns zero on success
|
||||
*/
|
||||
int symbol_allocate(
|
||||
struct symbol_table *symtab, const char *name,
|
||||
symtype_t type, struct symbol **res);
|
||||
|
||||
/*
|
||||
* Obtain a symbol by name from the symbol table
|
||||
*
|
||||
* @symtab: Symbol table to look up within
|
||||
* @name: Name to look up
|
||||
*
|
||||
* Returns the found symbol on success, otherwise NULL on failure
|
||||
*/
|
||||
struct symbol *symbol_byname(struct symbol_table *symtab, const char *name);
|
||||
|
||||
/*
|
||||
* Initialize a symbol table
|
||||
*
|
||||
* @symtab: Symbol table to initialize
|
||||
*
|
||||
* Returns zero on success
|
||||
*/
|
||||
int symbol_table_init(struct symbol_table *symtab);
|
||||
|
||||
/*
|
||||
* Destroy a symbol table and its allocated resources
|
||||
*
|
||||
* @symtab: Symbol table to destroy
|
||||
*/
|
||||
void symbol_table_destroy(struct symbol_table *symtab);
|
||||
|
||||
#endif /* !CESCAL_SYMBOL_H */
|
||||
+19
-1
@@ -13,7 +13,8 @@
|
||||
#define TOKBUF_CAP 4
|
||||
|
||||
struct tokbuf {
|
||||
struct token buf[TOKBUF_CAP];
|
||||
struct token *buf;
|
||||
size_t cap;
|
||||
uint8_t head;
|
||||
};
|
||||
|
||||
@@ -36,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
|
||||
*
|
||||
@@ -47,4 +58,11 @@ int tokbuf_push(struct tokbuf *tokbuf, struct token *tok);
|
||||
*/
|
||||
int tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res);
|
||||
|
||||
/*
|
||||
* Destroy a token buffer
|
||||
*
|
||||
* @tokbuf: Token buffer to destroy
|
||||
*/
|
||||
void tokbuf_destroy(struct tokbuf *tokbuf);
|
||||
|
||||
#endif /* !CESCAL_TOKBUF_H */
|
||||
|
||||
Reference in New Issue
Block a user