2024-10-02 17:57:04 -04:00
|
|
|
#include "include/lexer.h"
|
2024-10-05 09:24:12 -04:00
|
|
|
#include "include/token.h"
|
|
|
|
#include <stdlib.h>
|
2024-10-02 17:57:04 -04:00
|
|
|
|
|
|
|
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-05 09:24:12 -04:00
|
|
|
void lexer_do_confused(Lexer* lexer) {
|
|
|
|
int c = atoi(lexer->cchar);
|
|
|
|
|
|
|
|
if (c) lexer_add_token(lexer, token_init(TOKEN_TYPE_NUMBER, lexer->cchar));
|
|
|
|
else lexer_add_token(lexer, token_init(TOKEN_TYPE_CALL, lexer->cchar));
|
|
|
|
}
|
|
|
|
|
|
|
|
void lexer_do_number(Lexer* lexer) {}
|
|
|
|
void lexer_do_call(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-05 09:24:12 -04:00
|
|
|
|
|
|
|
void lexer_add_token(Lexer* lexer, Token* token) {
|
|
|
|
(void)reallocarray(lexer->tokens, lexer->ntokens++, sizeof(Token));
|
|
|
|
lexer->tokens[lexer->ntokens-1] = token;
|
|
|
|
}
|