2024-10-02 17:57:04 -04:00
|
|
|
#include "include/lexer.h"
|
|
|
|
|
|
|
|
Lexer* lexer_init(char* src) {
|
|
|
|
Lexer* lexer = malloc(sizeof(Lexer));
|
|
|
|
|
|
|
|
lexer->src = src;
|
|
|
|
lexer->srcl = strlen(src);
|
|
|
|
lexer->cchar = lexer->src;
|
2024-10-02 21:04:54 -04:00
|
|
|
|
2024-10-02 17:57:04 -04:00
|
|
|
lexer->tokens = NULL;
|
|
|
|
lexer->ntokens = 0;
|
|
|
|
lexer->state = LEXER_STATE_CONFUSED;
|
|
|
|
|
|
|
|
return lexer;
|
|
|
|
}
|
|
|
|
|
2024-10-02 21:04:54 -04:00
|
|
|
void lexer_destroy(Lexer* lexer) {
|
2024-10-02 17:57:04 -04:00
|
|
|
free(lexer->src);
|
|
|
|
|
2024-10-02 21:04:54 -04:00
|
|
|
for (int i = 0; i < lexer->ntokens; token_destroy(lexer->tokens[i++]));
|
2024-10-02 17:57:04 -04:00
|
|
|
}
|
|
|
|
|
2024-10-02 21:04:54 -04:00
|
|
|
void lexer_do_confused(Lexer* lexer) {}
|
2024-10-02 17:57:04 -04:00
|
|
|
|
|
|
|
void lexer_lex(Lexer* lexer) {
|
|
|
|
while (*lexer->cchar) {
|
|
|
|
switch (lexer->state) {
|
2024-10-02 21:04:54 -04:00
|
|
|
case LEXER_STATE_CONFUSED: lexer_do_confused(lexer); break;
|
|
|
|
case LEXER_STATE_NUM: lexer_do_number(lexer); break;
|
|
|
|
case LEXER_STATE_CALL: lexer_do_call(lexer); break;
|
|
|
|
default: break;
|
2024-10-02 17:57:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|