From 8cf09e43c91eafea396f0daab403ce0dec6fb2a6 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Sat, 2 Nov 2024 11:02:18 -0400 Subject: [PATCH] Worked on yylex(). --- src/include/lexer.h | 6 ++--- src/lexer.c | 55 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/include/lexer.h b/src/include/lexer.h index fc0ca20..4b4aea8 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -35,7 +35,7 @@ typedef struct { } Lexer; // Create a lexer. -Lexer* lexer_init(char* src); +void lexer_init(char* src); // Destroy a lexer. // Does not destroy `lexer->src`! @@ -68,7 +68,7 @@ void lexer_print_i(Lexer* lexer, int ilvl); // Print a representation of a LexerState. void lexerstate_print_raw(LexerState s); -// Interface with bison. -int yylex(); +// Create the input string. +void lexer_set_global(const char* str); #endif diff --git a/src/lexer.c b/src/lexer.c index 6693afc..c9d9170 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -7,20 +7,21 @@ #include "include/dstr.h" #include "include/token.h" #include "include/util.h" -Lexer* lexer_init(char* src) { - Lexer* lexer = malloc(sizeof(Lexer)); - lexer->src = src; - lexer->srcln = strlen(src); - lexer->cchar = lexer->src; +Lexer* thelexer = NULL; - lexer->tokens = calloc(TOKENS_MAX, sizeof(Token*)); - lexer->ntokens = 0; - lexer->state = LEXER_STATE_CONFUSED; +void lexer_init(char* src) { + thelexer = malloc(sizeof(Lexer)); - log_dbgf("created new lexer @ %p", lexer); + thelexer->src = src; + thelexer->srcln = strlen(src); + thelexer->cchar = thelexer->src; - return lexer; + thelexer->tokens = calloc(TOKENS_MAX, sizeof(Token*)); + thelexer->ntokens = 0; + thelexer->state = LEXER_STATE_CONFUSED; + + log_dbgf("created thelexer @ %p", thelexer); } void lexer_destroy(Lexer* lexer) { @@ -144,6 +145,36 @@ void lexerstate_print_raw(LexerState s) { } else printf("%s", lexerstate_names[s]); } -int yylex() { - lexer_lex() +#include "../build/grammars/grammar.tab.h" + +int yylex(void) { + if (*thelexer->cchar == '\0') return YYEOF; + + switch (*thelexer->cchar) { + case ' ': + case '\t': + thelexer->cchar++; + } + + // Assign & consume current character. + int c = *thelexer->cchar++; + + switch (c) { + case '+': + return PLUS; + } + + if (isdigit(c)) { + int value = c - '0'; // Start with the first digit + while (isdigit(*thelexer->cchar)) { + value = value * 10 + (*thelexer->cchar - '0'); // Accumulate value + thelexer++; + } + yylval.intval = value; // Set the token value + return NUM; // Return the INTEGER token type + } + + fprintf(stderr, "Unexpected character: %c\n", c); + + return 0; }