This commit is contained in:
Night Kaly 2024-05-18 19:07:13 +01:00
parent 931d14f35f
commit e65557d40d
Signed by: night0721
GPG key ID: 957D67B8DB7A119B

15
lexer.c
View file

@ -5,14 +5,15 @@
#include "lexer.h" #include "lexer.h"
Token *lexer(char *buf){ Token *lexer(char *buf)
{
char *current = strdup(buf); char *current = strdup(buf);
int current_index = 0, tokens_index = 0; int current_index = 0, tokens_index = 0;
Token *tokens = malloc(sizeof(Token) * 1024); Token *tokens = malloc(sizeof(Token) * 1024);
while(current[current_index] != '\0'){ while(current[current_index] != '\0') {
Token *token = NULL; Token *token = NULL;
if(current[current_index] == ';'){ if(current[current_index] == ';') {
token = malloc(sizeof(Token)); token = malloc(sizeof(Token));
char *value = malloc(2 * sizeof(char)); char *value = malloc(2 * sizeof(char));
value[0] = current[current_index]; value[0] = current[current_index];
@ -22,7 +23,7 @@ Token *lexer(char *buf){
tokens[tokens_index] = *token; tokens[tokens_index] = *token;
tokens_index++; tokens_index++;
printf("FOUND SEMICOLON\n"); printf("FOUND SEMICOLON\n");
} else if(current[current_index] == '('){ } else if(current[current_index] == '(') {
token = malloc(sizeof(Token)); token = malloc(sizeof(Token));
char *value = malloc(2 * sizeof(char)); char *value = malloc(2 * sizeof(char));
value[0] = current[current_index]; value[0] = current[current_index];
@ -32,7 +33,7 @@ Token *lexer(char *buf){
tokens[tokens_index] = *token; tokens[tokens_index] = *token;
tokens_index++; tokens_index++;
printf("FOUND OPEN PAREN\n"); printf("FOUND OPEN PAREN\n");
} else if(current[current_index] == ')'){ } else if(current[current_index] == ')') {
token = malloc(sizeof(Token)); token = malloc(sizeof(Token));
char *value = malloc(2 * sizeof(char)); char *value = malloc(2 * sizeof(char));
value[0] = current[current_index]; value[0] = current[current_index];
@ -42,12 +43,12 @@ Token *lexer(char *buf){
tokens[tokens_index] = *token; tokens[tokens_index] = *token;
tokens_index++; tokens_index++;
printf("FOUND CLOSE PAREN\n"); printf("FOUND CLOSE PAREN\n");
} else if(isdigit(current[current_index])){ } else if(isdigit(current[current_index])) {
token = generate_number(current, &current_index); token = generate_number(current, &current_index);
tokens[tokens_index] = *token; tokens[tokens_index] = *token;
tokens_index++; tokens_index++;
current_index--; current_index--;
} else if(isalpha(current[current_index])){ } else if(isalpha(current[current_index])) {
token = generate_keyword(current, &current_index); token = generate_keyword(current, &current_index);
tokens[tokens_index] = *token; tokens[tokens_index] = *token;
tokens_index++; tokens_index++;