summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@mirocom.org>2026-05-23 08:49:17 -0400
committerIan Moffett <ian@mirocom.org>2026-05-23 08:49:17 -0400
commit62451acdd4dd4cf369a801b0603e2e0841bfb627 (patch)
tree853db2d031239dd9e7e27c8ed679f6e0f884bc8c
parent82e68e92a92e8973dcca8d94f175c9d16be94c80 (diff)
core: lexer: Add preprocessor tokens
Signed-off-by: Ian Moffett <ian@mirocom.org>
-rw-r--r--core/lexer.c52
-rw-r--r--core/parser.c3
-rw-r--r--include/cescal/token.h3
3 files changed, 58 insertions, 0 deletions
diff --git a/core/lexer.c b/core/lexer.c
index 1a62e71..6c7a4c9 100644
--- a/core/lexer.c
+++ b/core/lexer.c
@@ -190,6 +190,45 @@ lexer_check_kw(struct cescal_state *state, struct token *res)
}
/*
+ * Check if an identifier token is actually a directive
+ *
+ * @state: Compiler state
+ * @res: Token result is written here
+ */
+static int
+lexer_check_direc(struct cescal_state *state, struct token *res)
+{
+ if (state == NULL || res == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ switch (*res->s) {
+ case 'd':
+ if (strcmp(res->s, "define") == 0) {
+ res->type = TT_DEFINE;
+ return 0;
+ }
+
+ break;
+ case 'i':
+ if (strcmp(res->s, "ifndef") == 0) {
+ res->type = TT_IFNDEF;
+ return 0;
+ }
+
+ if (strcmp(res->s, "ifdef") == 0) {
+ res->type = TT_IFDEF;
+ return 0;
+ }
+
+ break;
+ }
+
+ return -1;
+}
+
+/*
* Skip anything after a comment
*
* @state: Compiler state
@@ -237,6 +276,19 @@ lexer_nom(struct cescal_state *state, struct token *res)
res->type = TT_COMMA;
res->c = c;
return 0;
+ case '#':
+ if ((c = lexer_consume_single(state, true)) == '\0') {
+ return -1;
+ }
+
+ if (lexer_scan_ident(state, c, res) == 0) {
+ if (lexer_check_direc(state, res) != 0) {
+ cc_error("bad directive '%s'\n", res->s);
+ return -1;
+ }
+ }
+
+ return 0;
case '/':
if (lexer_consume_single(state, true) == '/') {
res->type = TT_COMMENT;
diff --git a/core/parser.c b/core/parser.c
index 5d64bb7..684a1ff 100644
--- a/core/parser.c
+++ b/core/parser.c
@@ -38,6 +38,9 @@ static const char *toktab[] = {
[TT_RPAREN] = qtok(")"),
[TT_COMMA] = qtok(","),
[TT_ARROW] = qtok("->"),
+ [TT_DEFINE] = qtok("#define"),
+ [TT_IFNDEF] = qtok("#ifndef"),
+ [TT_IFDEF] = qtok("#ifdef"),
[TT_RETURN] = qtok("return"),
[TT_PUB] = qtok("pub"),
[TT_PROC] = qtok("proc"),
diff --git a/include/cescal/token.h b/include/cescal/token.h
index e215371..21db2ac 100644
--- a/include/cescal/token.h
+++ b/include/cescal/token.h
@@ -18,6 +18,9 @@ typedef enum {
TT_RPAREN, /* '( */
TT_COMMA, /* ',' */
TT_ARROW, /* '->' */
+ TT_DEFINE, /* '#define' */
+ TT_IFNDEF, /* '#ifndef' */
+ TT_IFDEF, /* '#ifdef' */
TT_RETURN, /* 'return' */
TT_PUB, /* 'pub' */
TT_PROC, /* 'proc' */