Archived
1
0
Fork 0

Converted NULL to nullptr

Converted fprintf to cout/cerr
Removed as much C dependencies as possible
This commit is contained in:
Brandon Rozek 2018-09-26 20:03:54 -04:00
parent 492a581d19
commit 53575d25f9
9 changed files with 68 additions and 74 deletions

View file

@ -1,4 +1,4 @@
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include "sloth.hpp"
#include "shell.hpp"
@ -14,8 +14,8 @@ int main(int argc, char* argv[]) {
} else if (argc == 2) {
interpret_file(argv[1]);
} else {
printf("Incorrect number of arguments passed. Expected %d or %d, got %d.\n", 0, 1, argc - 1);
printf("Usage: lexer [program_name].sl\n");
std::cout << "Incorrect number of arguments passed. Expected " << 0 << " or " << 1 << ", got " << argc - 1 << std::endl;
std::cout << "Usage: sloth [program_name]" << std::endl;
exit(-1);
}

View file

@ -1,7 +1,5 @@
#include <iostream>
#include <stdlib.h>
#include <string>
#include <assert.h>
#include "node.hpp"
#include "operators.hpp"
#include "../constants.hpp"
@ -22,7 +20,7 @@ struct Node* make_node(int type, struct Value* value, std::string id) {
id.copy(node->id, id.length(), 0);
node->num_children = 0;
for(i = 0; i < MAX_CHILDREN; i++) {
node->children[i] = NULL;
node->children[i] = nullptr;
}
/* return new node */
@ -34,7 +32,7 @@ void attach_node(struct Node* parent, struct Node* child) {
/* connect it */
parent->children[parent->num_children] = child;
parent->num_children++;
assert(parent->num_children <= MAX_CHILDREN);
if (parent->num_children > MAX_CHILDREN) { std::cerr << "Error, max children attached to a node" << std::endl; }
}
void check_num_nodes(struct Node* node, int num_children, std::string error) {
@ -114,15 +112,15 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) {
// Needed if we are going to take input from the user
double temp;
struct Variable* var = NULL;
struct Environment* local_env = NULL;
struct Node* tempNode = NULL;
struct Value* tempVal = NULL;
struct Variable* var = nullptr;
struct Environment* local_env = nullptr;
struct Node* tempNode = nullptr;
struct Value* tempVal = nullptr;
// Evaluate subexpressions if existent and node is not a lambda expression
struct Value* val1 = NULL;
struct Value* val2 = NULL;
// struct Value* val3 = NULL;
struct Value* val1 = nullptr;
struct Value* val2 = nullptr;
// struct Value* val3 = nullptr;
if (node->num_children > 0 && node->type != LAMBDA) {
val1 = eval_expression(node->children[0], env);
if (node->num_children > 1) {
@ -233,7 +231,7 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) {
//----------
case IDENTIFIER:
var = find_variable(env, node->id);
if (var == NULL) {
if (var == nullptr) {
fprintf(stderr, "Error: Symbol %s not found.\n", node->id);
return 0;
}

View file

@ -1,14 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "operators.hpp"
#include "../variables/value.hpp"
struct Value* add(struct Value* x, struct Value* y) {
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in add.\n"); }
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot add a boolean.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in add." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot add a boolean." << std::endl; }
if ((x->type == STRING || y->type == STRING) && (x->type != STRING || y->type != STRING)) {
fprintf(stderr, "Error, cannot add a string with another data type.\n");
std::cerr << "Error, cannot add a string with another data type." << std::endl;
}
struct Value* ans;
@ -30,9 +28,9 @@ struct Value* add(struct Value* x, struct Value* y) {
}
struct Value* subtract(struct Value* x, struct Value* y) {
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in subtract.\n"); }
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot subtract a boolean.\n"); }
if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot subtract a string.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in subtract." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot subtract a boolean." << std::endl; }
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot subtract a string." << std::endl; }
struct Value* ans;
@ -51,9 +49,9 @@ struct Value* subtract(struct Value* x, struct Value* y) {
}
struct Value* division(struct Value* x, struct Value* y) {
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in divide.\n"); }
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot divide a boolean.\n"); }
if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot division a string.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in divide." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot divide a boolean." << std::endl; }
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot division a string." << std::endl; }
struct Value* ans;
@ -72,9 +70,9 @@ struct Value* division(struct Value* x, struct Value* y) {
}
struct Value* multiplication(struct Value* x, struct Value* y) {
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in multiply.\n"); }
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot multiply a boolean.\n"); }
if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot multiply a string.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in multiply." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot multiply a boolean." << std::endl; }
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot multiply a string." << std::endl; }
struct Value* ans;
@ -93,10 +91,10 @@ struct Value* multiplication(struct Value* x, struct Value* y) {
}
struct Value* less(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 numerically compare a boolean.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in <." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot numerically compare a boolean." << std::endl; }
if ((x->type == STRING || y->type == STRING) && (x->type != STRING || y->type != STRING)) {
fprintf(stderr, "Error, cannot compare a string with another data type.\n");
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
struct Value* ans;
@ -118,10 +116,10 @@ struct Value* less(struct Value* x, struct Value* y) {
}
struct Value* greater(struct Value* x, struct Value* y) {
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in greater.\n"); }
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot numerically compare a boolean.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in greater." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot numerically compare a boolean." << std::endl; }
if ((x->type == STRING || y->type == STRING) && (x->type != STRING || y->type != STRING)) {
fprintf(stderr, "Error, cannot compare a string with another data type.\n");
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
struct Value* ans;
@ -143,10 +141,10 @@ struct Value* greater(struct Value* x, struct Value* y) {
}
struct Value* less_equal(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 numerically compare a boolean.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in <=." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot numerically compare a boolean." << std::endl; }
if ((x->type == STRING || y->type == STRING) && (x->type != STRING || y->type != STRING)) {
fprintf(stderr, "Error, cannot compare a string with another data type.\n");
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
struct Value* ans;
@ -168,10 +166,10 @@ struct Value* less_equal(struct Value* x, struct Value* y) {
}
struct Value* greater_equal(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 numerically compare a boolean.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in >=." << std::endl; }
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot numerically compare a boolean." << std::endl; }
if ((x->type == STRING || y->type == STRING) && (x->type != STRING || y->type != STRING)) {
fprintf(stderr, "Error, cannot compare a string with another data type.\n");
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
struct Value* ans;
@ -193,9 +191,9 @@ struct Value* greater_equal(struct Value* x, struct Value* y) {
}
struct Value* equals(struct Value* x, struct Value* y) {
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in ==.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in ==." << std::endl; }
if ((x->type == STRING || y->type == STRING) && (x->type != STRING || y->type != STRING)) {
fprintf(stderr, "Error, cannot compare a string with another data type.\n");
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
struct Value* ans;
@ -214,16 +212,16 @@ struct Value* equals(struct Value* x, struct Value* y) {
} else if (x->type == STRING && y->type == STRING) {
ans = make_boolean(get_string(x).compare(get_string(y)) == 0);
} else { // Type is a mix between boolean and another type
fprintf(stderr, "Error, cannot compare a boolean with another type.\n");
std::cerr << "Error, cannot compare a boolean with another type." << std::endl;
}
return ans;
}
struct Value* not_equals(struct Value* x, struct Value* y) {
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in !=.\n"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in !=." << std::endl; }
if ((x->type == STRING || y->type == STRING) && (x->type != STRING || y->type != STRING)) {
fprintf(stderr, "Error, cannot compare a string with another data type.\n");
std::cerr << "Error, cannot compare a string with another data type." << std::endl;
}
struct Value* ans;
@ -242,32 +240,32 @@ struct Value* not_equals(struct Value* x, struct Value* y) {
} else if (x->type == STRING && y->type == STRING) {
ans = make_boolean(get_string(x).compare(get_string(y)) != 0);
} else { // Type is a mix between boolean and another type
fprintf(stderr, "Error, cannot compare a boolean with another type.\n");
std::cerr << "Error, cannot compare a boolean with another type." << std::endl;
}
return ans;
}
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"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in &&." << std::endl; }
if (x->type != BOOLEAN || y->type != BOOLEAN) { std::cerr << "Error, cannot use and AND operation with a non-boolean." << std::endl; }
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot AND a string." << std::endl; }
return make_boolean(get_long(x) && get_long(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"); }
if (!x || !y) { std::cerr << "Error, uninitialized values being used in ||." << std::endl; }
if (x->type != BOOLEAN || y->type != BOOLEAN) { std::cerr << "Error, cannot use and OR operation with a non-boolean." << std::endl; }
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot OR a string." << std::endl; }
return make_boolean(get_long(x) || get_long(y));
}
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"); }
if (!x) { std::cerr << "Error, uninitialized values being used in !." << std::endl; }
if (x->type != BOOLEAN) { std::cerr << "Error, cannot NOT a non-boolean." << std::endl; }
if (x->type == STRING) { std::cerr << "Error, cannot negate a string." << std::endl; }
return make_boolean(!get_long(x));
}

View file

@ -48,7 +48,7 @@ DIGIT [0-9]
\".*\" {yylval.value = make_node(VALUE, make_string(substring(yytext, 1, strlen(yytext) - 1)), ""); return VALUE; }
{DIGIT} {yylval.value = make_node(VALUE, make_long(atoi(yytext)), ""); return VALUE;}
{DIGIT}*"."?{DIGIT}+ {yylval.value = make_node(VALUE, make_double(atof(yytext)), ""); return VALUE;}
[_a-zA-Z][_a-zA-Z0-9]* {yylval.value = make_node(IDENTIFIER, NULL, strdup(yytext)); return IDENTIFIER;}
[_a-zA-Z][_a-zA-Z0-9]* {yylval.value = make_node(IDENTIFIER, nullptr, strdup(yytext)); return IDENTIFIER;}
[\n] {linenum++;}
[ \t\r]+ {}
. {printf("Error: invlaid lexeme '%s'.\n", yytext); return 0;}

View file

@ -30,9 +30,9 @@ FILE* stringToFile(char* str) { // Creates a temporary file with the given strin
int i = 0;
FILE* tmp = tmpfile();
if (tmp == NULL) {
if (tmp == nullptr) {
fprintf(stderr, "Unable to create temp file");
return NULL;
return nullptr;
}
while (str[i] != '\0') {

View file

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

View file

@ -1,5 +1,4 @@
#include <iostream>
#include <stdlib.h>
#include <string>
#include "environment.hpp"
#include "variable.hpp"
@ -8,7 +7,7 @@ struct Environment* create_environment(void) {
struct Environment* env = new Environment();
env->num_vars = 0;
for(int i = 0; i < MAX_VARIABLES; i++) {
env->vars[i] = NULL;
env->vars[i] = nullptr;
}
return env;
}
@ -19,7 +18,7 @@ struct Variable* find_variable(struct Environment* env, std::string id) {
return env->vars[i];
}
}
return NULL;
return nullptr;
}
void add_variable(struct Environment* env, struct Variable* var) {
@ -30,7 +29,7 @@ void add_variable(struct Environment* env, struct Variable* var) {
// If variable exists, replace it
struct Variable* temp_var = find_variable(env, var->id);
if (temp_var != NULL) {
if (temp_var != nullptr) {
temp_var->value = var->value;
free(var);
return;

View file

@ -26,16 +26,16 @@ struct Value* make_value(TypeTag type, long num, double dec, struct Node* expr,
}
struct Value* make_long(long num) {
return make_value(LONG, num, 0, NULL, "");
return make_value(LONG, num, 0, nullptr, "");
}
struct Value* make_double(double dec) {
return make_value(DOUBLE, 0, dec, NULL, "");
return make_value(DOUBLE, 0, dec, nullptr, "");
}
struct Value* make_true() {
return make_value(BOOLEAN, 1, 0, NULL, "");
return make_value(BOOLEAN, 1, 0, nullptr, "");
}
struct Value* make_false() {
return make_value(BOOLEAN, 0, 0, NULL, "");
return make_value(BOOLEAN, 0, 0, nullptr, "");
}
struct Value* make_boolean(int x) {
return (x)? make_true() : make_false();
@ -44,7 +44,7 @@ struct Value* make_expression(struct Node* expr) {
return make_value(LAMBDA, 0, 0, expr, "");
}
struct Value* make_string(std::string str) {
return make_value(STRING, 0, 0, NULL, str);
return make_value(STRING, 0, 0, nullptr, str);
}
void delete_value(struct Value* val) {

View file

@ -1,5 +1,4 @@
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include "variable.hpp"
@ -17,11 +16,11 @@ struct Variable* make_variable(std::string id, struct Value* value) {
}
void set_value(struct Variable* var, struct Value* value) {
if (!var) { fprintf(stderr, "Error: Invalid Variable\n"); return; }
if (!var) { std::cerr << "Error: Invalid Variable" << std::endl; return; }
var->value = value;
}
struct Value* get_value(struct Variable* var) {
if (!var) { fprintf(stderr, "Error: Invalid Variable\n"); return 0; }
if (!var) { std::cerr << "Error: Invalid Variable" << std::endl; return 0; }
return var->value;
}