diff --git a/Makefile b/Makefile index 7c69a73..ee1607b 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,11 @@ +GPP = g++ -Wall + sloth: src/main.c src/parser/lex.yy.o src/parser/parser.tab.o src/variables/environment.o src/variables/variable.o src/variables/value.o src/operations/node.o src/operations/operators.o src/string.o src/shell.o - gcc src/main.c src/parser/lex.yy.o src/parser/parser.tab.o src/variables/environment.o src/variables/variable.o src/variables/value.o src/operations/node.o src/operations/operators.o src/string.o src/shell.o -ledit -o sloth -src/parser/lex.yy.o: src/parser/lex.yy.c src/parser/parser.tab.h - gcc -c src/parser/lex.yy.c -o src/parser/lex.yy.o + $(GPP) src/main.c src/parser/lex.yy.o src/parser/parser.tab.o src/variables/environment.o src/variables/variable.o src/variables/value.o src/operations/node.o src/operations/operators.o src/string.o src/shell.o -ledit -o sloth +src/parser/lex.yy.o: src/parser/lex.yy.c src/parser/parser.tab.h + $(GPP) -c src/parser/lex.yy.c -o src/parser/lex.yy.o src/parser/parser.tab.o: src/parser/parser.tab.c - gcc -c src/parser/parser.tab.c -o src/parser/parser.tab.o + $(GPP) -c src/parser/parser.tab.c -o src/parser/parser.tab.o src/parser/parser.tab.h: src/parser/parser.y bison -d -o src/parser/parser.tab.c src/parser/parser.y src/parser/parser.tab.c: src/parser/parser.y @@ -11,18 +13,18 @@ src/parser/parser.tab.c: src/parser/parser.y src/parser/lex.yy.c: src/parser/lexer.l flex -o src/parser/lex.yy.c src/parser/lexer.l src/variables/environment.o: src/variables/environment.h src/variables/environment.c - gcc -c src/variables/environment.c -o src/variables/environment.o + $(GPP) -c src/variables/environment.c -o src/variables/environment.o src/variables/variable.o: src/variables/variable.h src/variables/variable.c - gcc -c src/variables/variable.c -o src/variables/variable.o + $(GPP) -c src/variables/variable.c -o src/variables/variable.o src/variables/value.o: src/variables/value.h src/variables/value.h - gcc -c src/variables/value.c -o src/variables/value.o + $(GPP) -c src/variables/value.c -o src/variables/value.o src/operations/operators.o: src/operations/operators.h src/operations/operators.c - gcc -c src/operations/operators.c -o src/operations/operators.o + $(GPP) -c src/operations/operators.c -o src/operations/operators.o src/operations/node.o: src/operations/node.h src/operations/node.c - gcc -c src/operations/node.c -o src/operations/node.o + $(GPP) -c src/operations/node.c -o src/operations/node.o src/shell.o: src/shell.h src/shell.c - gcc -c src/shell.c -o src/shell.o + $(GPP) -c src/shell.c -o src/shell.o src/string.o: src/string.h src/string.c - gcc -c src/string.c -o src/string.o + $(GPP) -c src/string.c -o src/string.o clean: - rm src/parser/lex.yy.c src/parser/parser.tab.c src/parser/parser.tab.h src/parser/lex.yy.o src/parser/parser.tab.o src/variables/environment.o src/variables/variable.o src/variables/value.o src/operations/node.o src/operations/operators.o src/shell.o sloth + rm src/parser/lex.yy.c src/parser/parser.tab.c src/parser/parser.tab.h src/parser/lex.yy.o src/parser/parser.tab.o src/variables/environment.o src/variables/variable.o src/variables/value.o src/operations/node.o src/operations/operators.o src/string.o src/shell.o sloth diff --git a/src/operations/node.c b/src/operations/node.c index 191e3e1..a1f5404 100644 --- a/src/operations/node.c +++ b/src/operations/node.c @@ -210,19 +210,19 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) { //---------- case AND: check_num_nodes(node, 2, "cannot perform logical operators on more than two expressions."); - return and(val1, val2); + return and_value(val1, val2); // return val1 && val2; break; //---------- case OR: check_num_nodes(node, 2, "cannot perform logical operators on more than two expressions."); - return or(val1, val2); + return or_value(val1, val2); // return val1 || val2; break; //---------- case NOT: check_num_nodes(node, 1, "cannot negate more than one expressions."); - return not(val1); + return not_value(val1); // return !val1; break; //---------- diff --git a/src/operations/operators.c b/src/operations/operators.c index f07ac7c..99d70db 100644 --- a/src/operations/operators.c +++ b/src/operations/operators.c @@ -248,7 +248,7 @@ struct Value* not_equals(struct Value* x, struct Value* y) { return ans; } -struct Value* and(struct Value* x, struct Value* y) { +struct Value* and_value(struct Value* x, struct Value* y) { if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in &&.\n"); } if (x->type != BOOLEAN || y->type != BOOLEAN) { fprintf(stderr, "Error, cannot use and AND operation with a non-boolean.\n"); } if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot AND a string.\n"); } @@ -256,7 +256,7 @@ struct Value* and(struct Value* x, struct Value* y) { return make_boolean(get_long(x) && get_long(y)); } -struct Value* or(struct Value* x, struct Value* y) { +struct Value* or_value(struct Value* x, struct Value* y) { if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in ||.\n"); } if (x->type != BOOLEAN || y->type != BOOLEAN) { fprintf(stderr, "Error, cannot use and OR operation with a non-boolean.\n"); } if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot OR a string.\n"); } @@ -264,7 +264,7 @@ struct Value* or(struct Value* x, struct Value* y) { return make_boolean(get_long(x) || get_long(y)); } -struct Value* not(struct Value* x) { +struct Value* not_value(struct Value* x) { if (!x) { fprintf(stderr, "Error, uninitialized values being used in !.\n"); } if (x->type != BOOLEAN) { fprintf(stderr, "Error, cannot NOT a non-boolean.\n"); } if (x->type == STRING) { fprintf(stderr, "Error, cannot negate a string.\n"); } diff --git a/src/operations/operators.h b/src/operations/operators.h index 174cea0..9bb8c5b 100644 --- a/src/operations/operators.h +++ b/src/operations/operators.h @@ -1,5 +1,6 @@ #ifndef OPERATORS_H #define OPERATORS_H +#include "../variables/value.h" struct Value* add(struct Value* x, struct Value* y); struct Value* subtract(struct Value* x, struct Value* y); @@ -11,8 +12,8 @@ struct Value* less_equal(struct Value* x, struct Value* y); struct Value* greater_equal(struct Value* x, struct Value* y); struct Value* equals(struct Value* x, struct Value* y); struct Value* not_equals(struct Value* x, struct Value* y); -struct Value* and(struct Value* x, struct Value* y); -struct Value* or(struct Value* x, struct Value* y); -struct Value* not(struct Value* x); +struct Value* and_value(struct Value* x, struct Value* y); +struct Value* or_value(struct Value* x, struct Value* y); +struct Value* not_value(struct Value* x); #endif diff --git a/src/parser/parser.h b/src/parser/parser.h index aaa1b53..3d1dc96 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -4,5 +4,6 @@ int yywrap( ); int yylex( ); void yyerror(const char* str); +int yyparse (void); #endif diff --git a/src/shell.c b/src/shell.c index 775c7d8..1cac0bf 100644 --- a/src/shell.c +++ b/src/shell.c @@ -1,8 +1,10 @@ #include #include #include "shell.h" +#include "parser/parser.h" #include "variables/environment.h" #include "operations/node.h" +#include "constants.h" // For keeping track of command history @@ -24,9 +26,6 @@ void add_history(char* unused) {} #include #endif - -struct Node* result; - FILE* stringToFile(char* str) { // Creates a temporary file with the given string as its contents int i = 0; diff --git a/src/string.c b/src/string.c index 5f55218..4118f12 100644 --- a/src/string.c +++ b/src/string.c @@ -11,7 +11,7 @@ char* substring(char* str, int start, int end) { char* substr; int c; int length = end - start; - substr = malloc(length + 1); + substr = (char*) malloc(length + 1); if (substr == NULL) { fprintf(stderr, "Unable to allocate memory for substring.\n"); diff --git a/src/variables/value.c b/src/variables/value.c index b63c44e..ee4892b 100644 --- a/src/variables/value.c +++ b/src/variables/value.c @@ -4,7 +4,7 @@ #include "../parser/parser.tab.h" -struct Value* make_value(int type, long num, double dec, struct Node* expr, char* str) { +struct Value* make_value(TypeTag type, long num, double dec, struct Node* expr, char* str) { /* allocate space */ struct Value* val = (struct Value*) malloc(sizeof(struct Value));