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) 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) 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) $(TARGET): $(SRC)
$(CC) $(SRC) -o $@ $(CFLAGS) $(LDFLAGS) $(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

74
slr.c
View file

@ -4,21 +4,25 @@
#include <string.h> #include <string.h>
#include "slr.h" #include "slr.h"
#include "lexer.h"
void print_token(Token token){ void print_token(Token token)
{
printf("Token Value: "); 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("%c", token.value[i]);
} }
printf("\nToken Type: "); printf("\nToken Type: ");
if(token.type == INT){ switch (token.type) {
case INT:
printf("int\n"); printf("int\n");
} break;
if(token.type == KEYWORD){ case KEYWORD:
printf("Keyword\n"); printf("Keyword\n");
} break;
if(token.type == SEPARATOR){ case SEPARATOR:
printf("Separator\n"); printf("Separator\n");
break;
} }
} }
@ -58,64 +62,6 @@ Token *generate_keyword(char *current, int *current_index)
return token; 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) long long get_file_length(FILE *file)
{ {
fseeko(file, 0, SEEK_END); fseeko(file, 0, SEEK_END);

4
slr.h
View file

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

1
test.sl Normal file
View file

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