Worked on yylex().

This commit is contained in:
Jacob Signorovitch 2024-11-02 11:02:18 -04:00
parent ecc12f6f3b
commit 8cf09e43c9
2 changed files with 46 additions and 15 deletions

View File

@ -35,7 +35,7 @@ typedef struct {
} Lexer; } Lexer;
// Create a lexer. // Create a lexer.
Lexer* lexer_init(char* src); void lexer_init(char* src);
// Destroy a lexer. // Destroy a lexer.
// Does not destroy `lexer->src`! // Does not destroy `lexer->src`!
@ -68,7 +68,7 @@ void lexer_print_i(Lexer* lexer, int ilvl);
// Print a representation of a LexerState. // Print a representation of a LexerState.
void lexerstate_print_raw(LexerState s); void lexerstate_print_raw(LexerState s);
// Interface with bison. // Create the input string.
int yylex(); void lexer_set_global(const char* str);
#endif #endif

View File

@ -7,20 +7,21 @@
#include "include/dstr.h" #include "include/dstr.h"
#include "include/token.h" #include "include/token.h"
#include "include/util.h" #include "include/util.h"
Lexer* lexer_init(char* src) {
Lexer* lexer = malloc(sizeof(Lexer));
lexer->src = src; Lexer* thelexer = NULL;
lexer->srcln = strlen(src);
lexer->cchar = lexer->src;
lexer->tokens = calloc(TOKENS_MAX, sizeof(Token*)); void lexer_init(char* src) {
lexer->ntokens = 0; thelexer = malloc(sizeof(Lexer));
lexer->state = LEXER_STATE_CONFUSED;
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) { void lexer_destroy(Lexer* lexer) {
@ -144,6 +145,36 @@ void lexerstate_print_raw(LexerState s) {
} else printf("%s", lexerstate_names[s]); } else printf("%s", lexerstate_names[s]);
} }
int yylex() { #include "../build/grammars/grammar.tab.h"
lexer_lex()
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;
} }