This commit is contained in:
Night Kaly 2024-05-16 19:03:04 +01:00
parent e0b13a6590
commit a1aa95e3f9
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
6 changed files with 96 additions and 72 deletions

View file

@ -13,7 +13,7 @@ MANDIR = $(PREFIX)/share/man/man1
LDFLAGS = $(shell pkg-config --libs)
CFLAGS = -O3 -march=native -mtune=native -pipe -g -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 $(shell pkg-config --cflags)
SRC = slr.c
SRC = slr.c lexer.c
$(TARGET): $(SRC)
$(CC) $(SRC) -o $@ $(CFLAGS) $(LDFLAGS)

65
lexer.c Normal file
View file

@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "lexer.h"
Token *lexer(char *buf){
char *current = strdup(buf);
int current_index = 0, tokens_index = 0;
Token *tokens = malloc(sizeof(Token) * 1024);
while(current[current_index] != '\0'){
Token *token = NULL;
if(current[current_index] == ';'){
token = malloc(sizeof(Token));
char *value = malloc(2 * sizeof(char));
value[0] = current[current_index];
value[1] = '\0';
token->value = value;
token->type = SEPARATOR;
tokens[tokens_index] = *token;
tokens_index++;
printf("FOUND SEMICOLON\n");
} else if(current[current_index] == '('){
token = malloc(sizeof(Token));
char *value = malloc(2 * sizeof(char));
value[0] = current[current_index];
value[1] = '\0';
token->value = value;
token->type = SEPARATOR;
tokens[tokens_index] = *token;
tokens_index++;
printf("FOUND OPEN PAREN\n");
} else if(current[current_index] == ')'){
token = malloc(sizeof(Token));
char *value = malloc(2 * sizeof(char));
value[0] = current[current_index];
value[1] = '\0';
token->value = value;
token->type = SEPARATOR;
tokens[tokens_index] = *token;
tokens_index++;
printf("FOUND CLOSE PAREN\n");
} else if(isdigit(current[current_index])){
token = generate_number(current, &current_index);
tokens[tokens_index] = *token;
tokens_index++;
current_index--;
} else if(isalpha(current[current_index])){
token = generate_keyword(current, &current_index);
tokens[tokens_index] = *token;
tokens_index++;
current_index--;
}
if (token != NULL) {
print_token(*token);
}
current_index++;
}
tokens[tokens_index].type = END_OF_TOKENS;
tokens[tokens_index].value = '\0';
return tokens;
}

8
lexer.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef LEXER_H_
#define LEXER_H_
#include "slr.h"
Token *lexer(char *buf);
#endif

88
slr.c
View file

@ -4,21 +4,25 @@
#include <string.h>
#include "slr.h"
#include "lexer.h"
void print_token(Token token){
void print_token(Token token)
{
printf("Token Value: ");
for(int i = 0; token.value[i] != '\0'; i++){
for (int i = 0; token.value[i] != '\0'; i++) {
printf("%c", token.value[i]);
}
printf("\nToken Type: ");
if(token.type == INT){
printf("int\n");
}
if(token.type == KEYWORD){
printf("Keyword\n");
}
if(token.type == SEPARATOR){
printf("Separator\n");
switch (token.type) {
case INT:
printf("int\n");
break;
case KEYWORD:
printf("Keyword\n");
break;
case SEPARATOR:
printf("Separator\n");
break;
}
}
@ -28,7 +32,7 @@ Token *generate_number(char *current, int *current_index)
token->type = INT;
char *value = malloc(8 * sizeof(char));
int value_index = 0;
while(isdigit(current[*current_index]) && current[*current_index] != '\0'){
while(isdigit(current[*current_index]) && current[*current_index] != '\0') {
value[value_index] = current[*current_index];
value_index++;
(*current_index)++;
@ -43,13 +47,13 @@ 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'){
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){
if(strcmp(keyword, "exit") == 0) {
printf("TYPE EXIT\n");
token->type = KEYWORD;
}
@ -58,64 +62,6 @@ Token *generate_keyword(char *current, int *current_index)
return token;
}
Token *lexer(char *buf){
char *current = strdup(buf);
int current_index = 0, tokens_index = 0;
Token *tokens = malloc(sizeof(Token) * 1024);
while(current[current_index] != '\0'){
Token *token = NULL;
if(current[current_index] == ';'){
token = malloc(sizeof(Token));
char *value = malloc(2 * sizeof(char));
value[0] = current[current_index];
value[1] = '\0';
token->value = value;
token->type = SEPARATOR;
tokens[tokens_index] = *token;
tokens_index++;
printf("FOUND SEMICOLON\n");
} else if(current[current_index] == '('){
token = malloc(sizeof(Token));
char *value = malloc(2 * sizeof(char));
value[0] = current[current_index];
value[1] = '\0';
token->value = value;
token->type = SEPARATOR;
tokens[tokens_index] = *token;
tokens_index++;
printf("FOUND OPEN PAREN\n");
} else if(current[current_index] == ')'){
token = malloc(sizeof(Token));
char *value = malloc(2 * sizeof(char));
value[0] = current[current_index];
value[1] = '\0';
token->value = value;
token->type = SEPARATOR;
tokens[tokens_index] = *token;
tokens_index++;
printf("FOUND CLOSE PAREN\n");
} else if(isdigit(current[current_index])){
token = generate_number(current, &current_index);
tokens[tokens_index] = *token;
tokens_index++;
current_index--;
} else if(isalpha(current[current_index])){
token = generate_keyword(current, &current_index);
tokens[tokens_index] = *token;
tokens_index++;
current_index--;
}
if (token != NULL) {
print_token(*token);
}
current_index++;
}
tokens[tokens_index].type = END_OF_TOKENS;
tokens[tokens_index].value = '\0';
return tokens;
}
long long get_file_length(FILE *file)
{
fseeko(file, 0, SEEK_END);

4
slr.h
View file

@ -13,4 +13,8 @@ typedef struct {
char *value;
} Token;
void print_token(Token token);
Token *generate_number(char *current, int *current_index);
Token *generate_keyword(char *current, int *current_index);
#endif

1
test.sl Normal file
View file

@ -0,0 +1 @@
exit(1000);