refactor
This commit is contained in:
parent
e0b13a6590
commit
a1aa95e3f9
6 changed files with 96 additions and 72 deletions
2
Makefile
2
Makefile
|
@ -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
65
lexer.c
Normal 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, ¤t_index);
|
||||
tokens[tokens_index] = *token;
|
||||
tokens_index++;
|
||||
current_index--;
|
||||
} else if(isalpha(current[current_index])){
|
||||
token = generate_keyword(current, ¤t_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
8
lexer.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef LEXER_H_
|
||||
#define LEXER_H_
|
||||
|
||||
#include "slr.h"
|
||||
|
||||
Token *lexer(char *buf);
|
||||
|
||||
#endif
|
74
slr.c
74
slr.c
|
@ -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++) {
|
||||
printf("%c", token.value[i]);
|
||||
}
|
||||
printf("\nToken Type: ");
|
||||
if(token.type == INT){
|
||||
switch (token.type) {
|
||||
case INT:
|
||||
printf("int\n");
|
||||
}
|
||||
if(token.type == KEYWORD){
|
||||
break;
|
||||
case KEYWORD:
|
||||
printf("Keyword\n");
|
||||
}
|
||||
if(token.type == SEPARATOR){
|
||||
break;
|
||||
case SEPARATOR:
|
||||
printf("Separator\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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, ¤t_index);
|
||||
tokens[tokens_index] = *token;
|
||||
tokens_index++;
|
||||
current_index--;
|
||||
} else if(isalpha(current[current_index])){
|
||||
token = generate_keyword(current, ¤t_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
4
slr.h
|
@ -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
1
test.sl
Normal file
|
@ -0,0 +1 @@
|
|||
exit(1000);
|
Loading…
Reference in a new issue