core: lexer: Add preprocessor tokens

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-05-23 08:49:17 -04:00
parent 82e68e92a9
commit 62451acdd4
3 changed files with 58 additions and 0 deletions
+52
View File
@@ -189,6 +189,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
*
@@ -236,6 +275,19 @@ lexer_nom(struct cescal_state *state, struct token *res)
case ',':
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) == '/') {
+3
View File
@@ -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"),
+3
View File
@@ -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' */