slr

S Lang Runtime
git clone https://codeberg.org/night0721/slr
Log | Files | Refs | README | LICENSE

commit a1aa95e3f99f2795e04d2f54192ad3243dccca10
parent e0b13a65905b5b6ebfa186ff41dfa039c309cb67
Author: night0721 <[email protected]>
Date:   Thu, 16 May 2024 19:03:04 +0100

refactor

Diffstat:
MMakefile | 2+-
Alexer.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alexer.h | 8++++++++
Mslr.c | 88++++++++++++++++---------------------------------------------------------------
Mslr.h | 4++++
Atest.sl | 1+
6 files changed, 96 insertions(+), 72 deletions(-)

diff --git a/Makefile b/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) diff --git a/lexer.c b/lexer.c @@ -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; +} + diff --git a/lexer.h b/lexer.h @@ -0,0 +1,8 @@ +#ifndef LEXER_H_ +#define LEXER_H_ + +#include "slr.h" + +Token *lexer(char *buf); + +#endif diff --git a/slr.c b/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++){ + 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); diff --git a/slr.h b/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 diff --git a/test.sl b/test.sl @@ -0,0 +1 @@ +exit(1000);