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

View File

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