summaryrefslogtreecommitdiff
path: root/lexer.c
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2026-03-27 11:27:08 -1000
committerCarson Fleming <cflems@cflems.net>2026-03-27 11:27:08 -1000
commit414a608c36b2d8f208ad0223219736d7582948ae (patch)
treeeeeb284023236a4ee53bb4a78608c3cd1e3992bb /lexer.c
parentfca3bf239cfdf03c4479f5d0c14a21c1fd96ea3e (diff)
downloadccc-414a608c36b2d8f208ad0223219736d7582948ae.tar.gz
fix some stuff
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c49
1 files changed, 25 insertions, 24 deletions
diff --git a/lexer.c b/lexer.c
index 9d0e596..f5072a1 100644
--- a/lexer.c
+++ b/lexer.c
@@ -10,6 +10,8 @@ static int lookahead;
static const char* PATH;
static unsigned long LINE, COL;
+static struct token tok = {.type = TK_NOT_FOUND};
+
#define LEXER_PANIC(format, ...) {\
fprintf(\
stderr,\
@@ -21,6 +23,8 @@ static unsigned long LINE, COL;
exit(1);\
}
+static void lexer_advance();
+
void lexer_load(const char* path) {
if (file != NULL) {
fclose(file);
@@ -32,6 +36,7 @@ void lexer_load(const char* path) {
PATH = path;
LINE = 1;
COL = 1;
+ lexer_advance();
}
void lexer_close() {
@@ -41,20 +46,9 @@ void lexer_close() {
}
bool lexer_peek(struct token* p_token) {
- if (file == NULL) return false;
-
- long orig_offset = ftell(file);
- int orig_lookahead = lookahead;
- unsigned long orig_line = LINE, orig_col = COL;
-
- bool rv = lexer_pop(p_token);
-
- LINE = orig_line;
- COL = orig_col;
- lookahead = orig_lookahead;
- fseek(file, orig_offset, SEEK_SET);
-
- return rv;
+ if (tok.type == TK_NOT_FOUND) return false;
+ if (p_token != NULL) *p_token = tok;
+ return true;
}
#define is_whitespace(c) (c == ' ' || c == '\t' || c == '\n')
@@ -328,7 +322,7 @@ static enum token_type lex_simple_operator(char c) {
LEXER_PANIC("unexpected token %c", c);
}
-bool lexer_pop(struct token* p_token) {
+static bool lexer_read() {
if (file == NULL) return false;
// consume all whitespace and comments preceding the next token
@@ -355,21 +349,28 @@ bool lexer_pop(struct token* p_token) {
}
if (is_numeric(c))
- lex_int_lit(p_token, c - '0');
+ lex_int_lit(&tok, c - '0');
else if (c == '.' && is_numeric(lookahead))
- lex_float_lit(p_token, 10, 0);
+ lex_float_lit(&tok, 10, 0);
else if (is_ident_legal(c))
- lex_ident(p_token, c);
+ lex_ident(&tok, c);
else if (c == '\'')
- lex_char_lit(p_token);
+ lex_char_lit(&tok);
else if (c == '"')
- lex_str_lit(p_token);
- else if (!lex_complex_operator(p_token, c))
- p_token->type = lex_simple_operator(c);
+ lex_str_lit(&tok);
+ else if (!lex_complex_operator(&tok, c))
+ tok.type = lex_simple_operator(c);
return true;
}
-bool lexer_eof() {
- return lookahead == EOF;
+static void lexer_advance() {
+ if (!lexer_read()) tok.type = TK_NOT_FOUND;
+}
+
+bool lexer_pop(struct token* p_token) {
+ if (tok.type == TK_NOT_FOUND) return false;
+ if (p_token != NULL) *p_token = tok;
+ lexer_advance();
+ return true;
}