diff --git a/Makefile b/Makefile index 6309379..8ed1636 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ .POSIX: .SUFFIXES: -CC = cc VERSION = 1.0 TARGET = slr MANPAGE = $(TARGET).1 @@ -9,14 +8,12 @@ PREFIX ?= /usr/local BINDIR = $(PREFIX)/bin MANDIR = $(PREFIX)/share/man/man1 -# Flags -LDFLAGS = -CFLAGS = -O3 -march=native -mtune=native -pipe -s -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 +CFLAGS = -O3 -march=native -mtune=native -pipe -s -std=c99 -pedantic -Wall SRC = slr.c lexer.c parser.c $(TARGET): $(SRC) - $(CC) $(SRC) -o $@ $(CFLAGS) $(LDFLAGS) + $(CC) $(SRC) -o $@ $(CFLAGS) dist: mkdir -p $(TARGET)-$(VERSION) @@ -34,11 +31,11 @@ install: $(TARGET) chmod 644 $(DESTDIR)$(MANDIR)/$(MANPAGE) uninstall: - $(RM) $(DESTDIR)$(BINDIR)/$(TARGET) - $(RM) $(DESTDIR)$(MANDIR)/$(MANPAGE) + rm $(DESTDIR)$(BINDIR)/$(TARGET) + rm $(DESTDIR)$(MANDIR)/$(MANPAGE) clean: - $(RM) $(TARGET) + rm $(TARGET) all: $(TARGET) diff --git a/parser.c b/parser.c index e0e516f..c51c0b9 100644 --- a/parser.c +++ b/parser.c @@ -6,97 +6,97 @@ #include "lexer.h" typedef enum { - INT_LIT, - STATEMENT, - EXTRA, - BEGINNING, + INT_LIT, + STATEMENT, + EXTRA, + BEGINNING, } NodeTypes; typedef struct Node { - char *value; - NodeTypes type; - struct Node *right; - struct Node *left; + char *value; + NodeTypes type; + struct Node *right; + struct Node *left; } Node; void print_tree(Node *node){ - if(node == NULL){ - return; - } - for(size_t i = 0; node->value[i] != '\0'; i++){ - printf("%c", node->value[i]); - } - printf("\n"); - print_tree(node->left); - print_tree(node->right); + if(node == NULL){ + return; + } + for(size_t i = 0; node->value[i] != '\0'; i++){ + printf("%c", node->value[i]); + } + printf("\n"); + print_tree(node->left); + print_tree(node->right); } Node *init_node(Node *node, char *value, NodeTypes type){ - node = malloc(sizeof(Node)); - node->value = malloc(sizeof(char) * 2); - node->type = (int)type; - node->value = value; - node->left = NULL; - node->right = NULL; - return node; + node = malloc(sizeof(Node)); + node->value = malloc(sizeof(char) * 2); + node->type = (int)type; + node->value = value; + node->left = NULL; + node->right = NULL; + return node; } Token *parser(Token *tokens){ - Token *current_token = &tokens[0]; - Node *root = malloc(sizeof(Node)); - Node *left = malloc(sizeof(Node)); - Node *right = malloc(sizeof(Node)); - root = init_node(root, "PROGRAM", BEGINNING); + Token *current_token = &tokens[0]; + Node *root = malloc(sizeof(Node)); + Node *left = malloc(sizeof(Node)); + Node *right = malloc(sizeof(Node)); + root = init_node(root, "PROGRAM", BEGINNING); - print_tree(root); + print_tree(root); - Node *current = root; + Node *current = root; - while(current_token->type != END_OF_TOKENS){ - if(current == NULL){ - break; - } - if(current == root){ - //; - } + while(current_token->type != END_OF_TOKENS){ + if(current == NULL){ + break; + } + if(current == root){ + //; + } - if(current_token->type == KEYWORD && strcmp(current_token->value, "exit")){ - Node *exit_node = malloc(sizeof(Node)); - exit_node = init_node(exit_node, current_token->value, STATEMENT); - root->right = exit_node; - current = exit_node; - current_token++; - if(current_token->type != SEPARATOR){ - printf("ERROR\n"); - exit(1); - } - Node *open_paren_node = malloc(sizeof(Node)); - open_paren_node = init_node(open_paren_node, current_token->value, EXTRA); - current->left = open_paren_node; + if(current_token->type == KEYWORD && strcmp(current_token->value, "exit")){ + Node *exit_node = malloc(sizeof(Node)); + exit_node = init_node(exit_node, current_token->value, STATEMENT); + root->right = exit_node; + current = exit_node; + current_token++; + if(current_token->type != SEPARATOR){ + printf("ERROR\n"); + exit(1); + } + Node *open_paren_node = malloc(sizeof(Node)); + open_paren_node = init_node(open_paren_node, current_token->value, EXTRA); + current->left = open_paren_node; - current_token++; + current_token++; - Node *expr_node = malloc(sizeof(Node)); - expr_node = init_node(expr_node, current_token->value, INT_LIT); - current->left->left = expr_node; + Node *expr_node = malloc(sizeof(Node)); + expr_node = init_node(expr_node, current_token->value, INT_LIT); + current->left->left = expr_node; - current_token++; + current_token++; - Node *close_paren_node = malloc(sizeof(Node)); - close_paren_node = init_node(close_paren_node, current_token->value, EXTRA); - current->left->right = close_paren_node; + Node *close_paren_node = malloc(sizeof(Node)); + close_paren_node = init_node(close_paren_node, current_token->value, EXTRA); + current->left->right = close_paren_node; - current_token++; + current_token++; - Node *semi_node = malloc(sizeof(Node)); - semi_node = init_node(semi_node, current_token->value, EXTRA); - current->right = semi_node; - - printf("EXIT\n"); - break; - } - current_token++; - } - print_tree(root); - return current_token; + Node *semi_node = malloc(sizeof(Node)); + semi_node = init_node(semi_node, current_token->value, EXTRA); + current->right = semi_node; + + printf("EXIT\n"); + break; + } + current_token++; + } + print_tree(root); + return current_token; } diff --git a/slr.c b/slr.c index 715a2c2..2998224 100644 --- a/slr.c +++ b/slr.c @@ -35,3 +35,5 @@ int main(int argc, char **argv) print_token(tokens[i]); } } + +/* https://github.com/CobbCoding1/unnamed-lang/commit/10ecba484a0b47110c19b2604500c9ae49f84c52 */