2024-05-16 20:03:04 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "lexer.h"
|
|
|
|
|
2024-05-18 20:07:13 +02:00
|
|
|
Token *lexer(char *buf)
|
|
|
|
{
|
2024-05-16 20:03:04 +02:00
|
|
|
char *current = strdup(buf);
|
|
|
|
int current_index = 0, tokens_index = 0;
|
|
|
|
|
|
|
|
Token *tokens = malloc(sizeof(Token) * 1024);
|
2024-05-18 20:07:13 +02:00
|
|
|
while(current[current_index] != '\0') {
|
2024-05-16 20:03:04 +02:00
|
|
|
Token *token = NULL;
|
2024-05-18 20:07:13 +02:00
|
|
|
if(current[current_index] == ';') {
|
2024-05-22 12:12:48 +02:00
|
|
|
token = generate_separator(current, ¤t_index);
|
2024-05-16 20:03:04 +02:00
|
|
|
tokens[tokens_index] = *token;
|
|
|
|
tokens_index++;
|
2024-05-18 20:07:13 +02:00
|
|
|
} else if(current[current_index] == '(') {
|
2024-05-22 12:12:48 +02:00
|
|
|
token = generate_separator(current, ¤t_index);
|
2024-05-16 20:03:04 +02:00
|
|
|
tokens[tokens_index] = *token;
|
|
|
|
tokens_index++;
|
2024-05-18 20:07:13 +02:00
|
|
|
} else if(current[current_index] == ')') {
|
2024-05-22 12:12:48 +02:00
|
|
|
token = generate_separator(current, ¤t_index);
|
2024-05-16 20:03:04 +02:00
|
|
|
tokens[tokens_index] = *token;
|
|
|
|
tokens_index++;
|
2024-05-18 20:07:13 +02:00
|
|
|
} else if(isdigit(current[current_index])) {
|
2024-05-16 20:03:04 +02:00
|
|
|
token = generate_number(current, ¤t_index);
|
|
|
|
tokens[tokens_index] = *token;
|
|
|
|
tokens_index++;
|
|
|
|
current_index--;
|
2024-05-18 20:07:13 +02:00
|
|
|
} else if(isalpha(current[current_index])) {
|
2024-05-16 20:03:04 +02:00
|
|
|
token = generate_keyword(current, ¤t_index);
|
|
|
|
tokens[tokens_index] = *token;
|
|
|
|
tokens_index++;
|
|
|
|
current_index--;
|
|
|
|
}
|
|
|
|
current_index++;
|
|
|
|
}
|
|
|
|
tokens[tokens_index].type = END_OF_TOKENS;
|
2024-11-05 00:43:37 +01:00
|
|
|
tokens[tokens_index].value = NULL;
|
2024-05-16 20:03:04 +02:00
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2024-05-22 12:12:48 +02:00
|
|
|
void print_token(Token token)
|
|
|
|
{
|
|
|
|
printf("Token Value: \"");
|
|
|
|
for (int i = 0; token.value[i] != '\0'; i++) {
|
|
|
|
printf("%c", token.value[i]);
|
|
|
|
}
|
|
|
|
printf("\"\nToken Type: ");
|
|
|
|
switch (token.type) {
|
|
|
|
case INT:
|
|
|
|
printf("int\n");
|
|
|
|
break;
|
|
|
|
case KEYWORD:
|
|
|
|
printf("Keyword\n");
|
|
|
|
break;
|
|
|
|
case SEPARATOR:
|
|
|
|
printf("Separator\n");
|
|
|
|
break;
|
|
|
|
case END_OF_TOKENS:
|
|
|
|
printf("End of Tokens\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *generate_separator(char *current, int *current_index)
|
|
|
|
{
|
|
|
|
Token *token = malloc(sizeof(Token));
|
|
|
|
token->value = malloc(sizeof(char) * 2);
|
|
|
|
token->value[0] = current[*current_index];
|
|
|
|
token->value[1] = '\0';
|
|
|
|
token->type = SEPARATOR;
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *generate_number(char *current, int *current_index)
|
|
|
|
{
|
|
|
|
Token *token = malloc(sizeof(Token));
|
|
|
|
token->type = INT;
|
|
|
|
char *value = malloc(8 * sizeof(char));
|
|
|
|
int value_index = 0;
|
|
|
|
while(isdigit(current[*current_index]) && current[*current_index] != '\0') {
|
|
|
|
value[value_index] = current[*current_index];
|
|
|
|
value_index++;
|
|
|
|
(*current_index)++;
|
|
|
|
}
|
|
|
|
value[value_index] = '\0';
|
|
|
|
token->value = value;
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token *generate_keyword(char *current, int *current_index)
|
|
|
|
{
|
|
|
|
Token *token = malloc(sizeof(Token));
|
|
|
|
char *keyword = malloc(8 * sizeof(char));
|
|
|
|
int keyword_index = 0;
|
|
|
|
while(isalpha(current[*current_index]) && current[*current_index] != '\0') {
|
|
|
|
keyword[keyword_index] = current[*current_index];
|
|
|
|
keyword_index++;
|
|
|
|
(*current_index)++;
|
|
|
|
}
|
|
|
|
keyword[keyword_index] = '\0';
|
|
|
|
if(strcmp(keyword, "exit") == 0) {
|
|
|
|
printf("TYPE EXIT\n");
|
|
|
|
token->type = KEYWORD;
|
|
|
|
}
|
|
|
|
token->type = KEYWORD;
|
|
|
|
token->value = keyword;
|
|
|
|
return token;
|
|
|
|
}
|