Archived
1
0
Fork 0

Converted project to C++

This commit is contained in:
Brandon Rozek 2018-09-26 16:44:22 -04:00
parent aa7e348552
commit 24e3003c00
8 changed files with 29 additions and 26 deletions

View file

@ -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 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 $(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 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) -c src/parser/lex.yy.c -o src/parser/lex.yy.o
src/parser/parser.tab.o: src/parser/parser.tab.c 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 src/parser/parser.tab.h: src/parser/parser.y
bison -d -o src/parser/parser.tab.c 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 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 src/parser/lex.yy.c: src/parser/lexer.l
flex -o 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 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 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 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 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 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 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 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: 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

View file

@ -210,19 +210,19 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) {
//---------- //----------
case AND: case AND:
check_num_nodes(node, 2, "cannot perform logical operators on more than two expressions."); 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; // return val1 && val2;
break; break;
//---------- //----------
case OR: case OR:
check_num_nodes(node, 2, "cannot perform logical operators on more than two expressions."); 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; // return val1 || val2;
break; break;
//---------- //----------
case NOT: case NOT:
check_num_nodes(node, 1, "cannot negate more than one expressions."); check_num_nodes(node, 1, "cannot negate more than one expressions.");
return not(val1); return not_value(val1);
// return !val1; // return !val1;
break; break;
//---------- //----------

View file

@ -248,7 +248,7 @@ struct Value* not_equals(struct Value* x, struct Value* y) {
return ans; 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 || !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 != 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"); } 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)); 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 || !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 != 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"); } 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)); 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) { 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 != BOOLEAN) { fprintf(stderr, "Error, cannot NOT a non-boolean.\n"); }
if (x->type == STRING) { fprintf(stderr, "Error, cannot negate a string.\n"); } if (x->type == STRING) { fprintf(stderr, "Error, cannot negate a string.\n"); }

View file

@ -1,5 +1,6 @@
#ifndef OPERATORS_H #ifndef OPERATORS_H
#define OPERATORS_H #define OPERATORS_H
#include "../variables/value.h"
struct Value* add(struct Value* x, struct Value* y); struct Value* add(struct Value* x, struct Value* y);
struct Value* subtract(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* greater_equal(struct Value* x, struct Value* y);
struct Value* equals(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* not_equals(struct Value* x, struct Value* y);
struct Value* and(struct Value* x, struct Value* y); struct Value* and_value(struct Value* x, struct Value* y);
struct Value* or(struct Value* x, struct Value* y); struct Value* or_value(struct Value* x, struct Value* y);
struct Value* not(struct Value* x); struct Value* not_value(struct Value* x);
#endif #endif

View file

@ -4,5 +4,6 @@
int yywrap( ); int yywrap( );
int yylex( ); int yylex( );
void yyerror(const char* str); void yyerror(const char* str);
int yyparse (void);
#endif #endif

View file

@ -1,8 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "shell.h" #include "shell.h"
#include "parser/parser.h"
#include "variables/environment.h" #include "variables/environment.h"
#include "operations/node.h" #include "operations/node.h"
#include "constants.h"
// For keeping track of command history // For keeping track of command history
@ -24,9 +26,6 @@ void add_history(char* unused) {}
#include <editline/readline.h> #include <editline/readline.h>
#endif #endif
struct Node* result;
FILE* stringToFile(char* str) { // Creates a temporary file with the given string as its contents FILE* stringToFile(char* str) { // Creates a temporary file with the given string as its contents
int i = 0; int i = 0;

View file

@ -11,7 +11,7 @@ char* substring(char* str, int start, int end) {
char* substr; char* substr;
int c; int c;
int length = end - start; int length = end - start;
substr = malloc(length + 1); substr = (char*) malloc(length + 1);
if (substr == NULL) { if (substr == NULL) {
fprintf(stderr, "Unable to allocate memory for substring.\n"); fprintf(stderr, "Unable to allocate memory for substring.\n");

View file

@ -4,7 +4,7 @@
#include "../parser/parser.tab.h" #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 */ /* allocate space */
struct Value* val = (struct Value*) malloc(sizeof(struct Value)); struct Value* val = (struct Value*) malloc(sizeof(struct Value));