slr

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit a99d8d75970541a4672275d4ee2e356fd50a98be
Author: night0721 <[email protected]>
Date:   Thu, 16 May 2024 18:31:53 +0100

Initial commit

Diffstat:
AMakefile | 45+++++++++++++++++++++++++++++++++++++++++++++
AREADME.md | 1+
Aslr | 0
Aslr.c | 141+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aslr.h | 16++++++++++++++++
Atest.sl | 1+
6 files changed, 204 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,45 @@ +.POSIX: +.SUFFIXES: + +CC = cc +VERSION = 1.0 +TARGET = slr +MANPAGE = slr.1 +PREFIX ?= /usr/local +BINDIR = $(PREFIX)/bin +MANDIR = $(PREFIX)/share/man/man1 + +# Flags +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 + +$(TARGET): $(SRC) + $(CC) $(SRC) -o $@ $(CFLAGS) $(LDFLAGS) + +dist: + mkdir -p $(TARGET)-$(VERSION) + cp -R README.md $(MANPAGE) $(TARGET) $(TARGET)-$(VERSION) + tar -cf $(TARGET)-$(VERSION).tar $(TARGET)-$(VERSION) + gzip $(TARGET)-$(VERSION).tar + rm -rf $(TARGET)-$(VERSION) + +install: $(TARGET) + mkdir -p $(DESTDIR)$(BINDIR) + mkdir -p $(DESTDIR)$(MANDIR) + cp -p $(TARGET) $(DESTDIR)$(BINDIR)/$(TARGET) + chmod 755 $(DESTDIR)$(BINDIR)/$(TARGET) + cp -p $(MANPAGE) $(DESTDIR)$(MANDIR)/$(MANPAGE) + chmod 644 $(DESTDIR)$(MANDIR)/$(MANPAGE) + +uninstall: + $(RM) $(DESTDIR)$(BINDIR)/$(TARGET) + $(RM) $(DESTDIR)$(MANDIR)/$(MANPAGE) + +clean: + $(RM) $(TARGET) + +all: $(TARGET) + +.PHONY: all dist install uninstall clean diff --git a/README.md b/README.md @@ -0,0 +1 @@ +# S Lang Runtime diff --git a/slr b/slr Binary files differ. diff --git a/slr.c b/slr.c @@ -0,0 +1,141 @@ +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> + +#include "slr.h" + +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){ + printf("int\n"); + } + if(token.type == KEYWORD){ + printf("Keyword\n"); + } + if(token.type == SEPARATOR){ + printf("Separator\n"); + } +} + +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; +} + +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); + long long length = ftello(file); + fseeko(file, 0, SEEK_SET); + return length; +} + + +int main(int argc, char **argv) +{ + if (argc < 2) { + fprintf(stderr, "\033[1;37mslr:\033[0m \033[1;31mfatal error:\033[0m no input files\ninterpretation termined.\n"); + exit(1); + } + FILE *file = fopen(argv[1], "r"); + long long length = get_file_length(file); + char *buf = malloc(length * sizeof(char)); + fread(buf, 1, length, file); + fclose(file); + buf[length + 1] = '\0'; + Token *tokens = lexer(buf); +} diff --git a/slr.h b/slr.h @@ -0,0 +1,16 @@ +#ifndef SLR_H_ +#define SLR_H_ + +typedef enum { + INT, + KEYWORD, + SEPARATOR, + END_OF_TOKENS, +} TokenType; + +typedef struct { + TokenType type; + char *value; +} Token; + +#endif diff --git a/test.sl b/test.sl @@ -0,0 +1 @@ +kys(1000);