From 046e49c7b3d08f6127f9c062f926ac9b5fb82d4b Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 21 Mar 2026 15:41:09 -0400 Subject: [PATCH] frontend: lexer: Add scanning of .cc and .ld directives Signed-off-by: Ian Moffett --- frontend/lexer.c | 53 ++++++++++++++++++++++++++++++++++++++++ frontend/parser.c | 2 ++ include/frontend/token.h | 2 ++ 3 files changed, 57 insertions(+) diff --git a/frontend/lexer.c b/frontend/lexer.c index ca7e5e2..649a1da 100644 --- a/frontend/lexer.c +++ b/frontend/lexer.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include "frontend/token.h" @@ -200,6 +201,52 @@ lexer_scan_name(struct quip_state *state, int lc, struct token *tokres) return 0; } +/* + * Scan for a directive token + * + * @state: Quip state + * @tokres: Token result is written here + * + * Returns zero on success + */ +static int +lexer_scan_directive(struct quip_state *state, struct token *tokres) +{ + char c; + + if ((c = lexer_consume(state, true)) == '\0') { + return -1; + } + + if (!isalpha(c)) { + return -1; + } + + if (lexer_scan_name(state, c, tokres) < 0) { + trace_error(state, "fatal: error scanning directive\n"); + return -1; + } + + switch (*tokres->s) { + case 'c': + if (strcmp(tokres->s, "cc") == 0) { + tokres->type = TT_CC; + return 0; + } + + break; + case 'l': + if (strcmp(tokres->s, "ld") == 0) { + tokres->type = TT_LD; + return 0; + } + + break; + } + + return -1; +} + int lexer_scan(struct quip_state *state, struct token *tokres) { @@ -217,6 +264,12 @@ lexer_scan(struct quip_state *state, struct token *tokres) case '\n': tokres->type = TT_NEWLINE; tokres->c = c; + return 0; + case '.': + if (lexer_scan_directive(state, tokres) != 0) { + return -1; + } + return 0; case ':': tokres->c = c; diff --git a/frontend/parser.c b/frontend/parser.c index 040ae01..67b8576 100644 --- a/frontend/parser.c +++ b/frontend/parser.c @@ -37,6 +37,8 @@ static const char *toktab[] = { [TT_NONE] = symtok("none"), [TT_NAME] = symtok("name"), [TT_NEWLINE] = symtok("newline"), + [TT_CC] = qtok(".cc"), + [TT_LD] = qtok(".ld"), [TT_COLON] = qtok(":"), [TT_COLONDUB] = qtok("::") }; diff --git a/include/frontend/token.h b/include/frontend/token.h index bc298f6..e8f909f 100644 --- a/include/frontend/token.h +++ b/include/frontend/token.h @@ -18,6 +18,8 @@ typedef enum { TT_NONE, /* [none] */ TT_NAME, /* [name] */ TT_NEWLINE, /* [newline] */ + TT_CC, /* '.cc' */ + TT_LD, /* '.ld' */ TT_COLON, /* ':' */ TT_COLONDUB, /* '::' */ } tt_t;