frontend: lexer: Add scanning of .cc and .ld directives

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-03-21 15:41:09 -04:00
parent 520d2176a2
commit 046e49c7b3
3 changed files with 57 additions and 0 deletions

View File

@@ -13,6 +13,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#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;

View File

@@ -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("::")
};

View File

@@ -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;