Converted NULL to nullptr
Converted fprintf to cout/cerr Removed as much C dependencies as possible
This commit is contained in:
parent
492a581d19
commit
53575d25f9
9 changed files with 68 additions and 74 deletions
|
@ -1,4 +1,4 @@
|
||||||
#include <stdio.h>
|
#include <iostream>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "sloth.hpp"
|
#include "sloth.hpp"
|
||||||
#include "shell.hpp"
|
#include "shell.hpp"
|
||||||
|
@ -14,8 +14,8 @@ int main(int argc, char* argv[]) {
|
||||||
} else if (argc == 2) {
|
} else if (argc == 2) {
|
||||||
interpret_file(argv[1]);
|
interpret_file(argv[1]);
|
||||||
} else {
|
} else {
|
||||||
printf("Incorrect number of arguments passed. Expected %d or %d, got %d.\n", 0, 1, argc - 1);
|
std::cout << "Incorrect number of arguments passed. Expected " << 0 << " or " << 1 << ", got " << argc - 1 << std::endl;
|
||||||
printf("Usage: lexer [program_name].sl\n");
|
std::cout << "Usage: sloth [program_name]" << std::endl;
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <assert.h>
|
|
||||||
#include "node.hpp"
|
#include "node.hpp"
|
||||||
#include "operators.hpp"
|
#include "operators.hpp"
|
||||||
#include "../constants.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);
|
id.copy(node->id, id.length(), 0);
|
||||||
node->num_children = 0;
|
node->num_children = 0;
|
||||||
for(i = 0; i < MAX_CHILDREN; i++) {
|
for(i = 0; i < MAX_CHILDREN; i++) {
|
||||||
node->children[i] = NULL;
|
node->children[i] = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return new node */
|
/* return new node */
|
||||||
|
@ -34,7 +32,7 @@ void attach_node(struct Node* parent, struct Node* child) {
|
||||||
/* connect it */
|
/* connect it */
|
||||||
parent->children[parent->num_children] = child;
|
parent->children[parent->num_children] = child;
|
||||||
parent->num_children++;
|
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) {
|
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
|
// Needed if we are going to take input from the user
|
||||||
double temp;
|
double temp;
|
||||||
struct Variable* var = NULL;
|
struct Variable* var = nullptr;
|
||||||
struct Environment* local_env = NULL;
|
struct Environment* local_env = nullptr;
|
||||||
struct Node* tempNode = NULL;
|
struct Node* tempNode = nullptr;
|
||||||
struct Value* tempVal = NULL;
|
struct Value* tempVal = nullptr;
|
||||||
|
|
||||||
// Evaluate subexpressions if existent and node is not a lambda expression
|
// Evaluate subexpressions if existent and node is not a lambda expression
|
||||||
struct Value* val1 = NULL;
|
struct Value* val1 = nullptr;
|
||||||
struct Value* val2 = NULL;
|
struct Value* val2 = nullptr;
|
||||||
// struct Value* val3 = NULL;
|
// struct Value* val3 = nullptr;
|
||||||
if (node->num_children > 0 && node->type != LAMBDA) {
|
if (node->num_children > 0 && node->type != LAMBDA) {
|
||||||
val1 = eval_expression(node->children[0], env);
|
val1 = eval_expression(node->children[0], env);
|
||||||
if (node->num_children > 1) {
|
if (node->num_children > 1) {
|
||||||
|
@ -233,7 +231,7 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) {
|
||||||
//----------
|
//----------
|
||||||
case IDENTIFIER:
|
case IDENTIFIER:
|
||||||
var = find_variable(env, node->id);
|
var = find_variable(env, node->id);
|
||||||
if (var == NULL) {
|
if (var == nullptr) {
|
||||||
fprintf(stderr, "Error: Symbol %s not found.\n", node->id);
|
fprintf(stderr, "Error: Symbol %s not found.\n", node->id);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
#include <stdio.h>
|
#include <iostream>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "operators.hpp"
|
#include "operators.hpp"
|
||||||
#include "../variables/value.hpp"
|
#include "../variables/value.hpp"
|
||||||
|
|
||||||
struct Value* add(struct Value* x, struct Value* y) {
|
struct Value* add(struct Value* x, struct Value* y) {
|
||||||
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in add.\n"); }
|
if (!x || !y) { std::cerr << "Error, uninitialized values being used in add." << std::endl; }
|
||||||
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot add a boolean.\n"); }
|
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)) {
|
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;
|
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) {
|
struct Value* subtract(struct Value* x, struct Value* y) {
|
||||||
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in subtract.\n"); }
|
if (!x || !y) { std::cerr << "Error, uninitialized values being used in subtract." << std::endl; }
|
||||||
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot subtract a boolean.\n"); }
|
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot subtract a boolean." << std::endl; }
|
||||||
if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot subtract a string.\n"); }
|
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot subtract a string." << std::endl; }
|
||||||
|
|
||||||
struct Value* ans;
|
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) {
|
struct Value* division(struct Value* x, struct Value* y) {
|
||||||
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in divide.\n"); }
|
if (!x || !y) { std::cerr << "Error, uninitialized values being used in divide." << std::endl; }
|
||||||
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot divide a boolean.\n"); }
|
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot divide a boolean." << std::endl; }
|
||||||
if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot division a string.\n"); }
|
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot division a string." << std::endl; }
|
||||||
|
|
||||||
struct Value* ans;
|
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) {
|
struct Value* multiplication(struct Value* x, struct Value* y) {
|
||||||
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in multiply.\n"); }
|
if (!x || !y) { std::cerr << "Error, uninitialized values being used in multiply." << std::endl; }
|
||||||
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot multiply a boolean.\n"); }
|
if (x->type == BOOLEAN || y->type == BOOLEAN) { std::cerr << "Error, cannot multiply a boolean." << std::endl; }
|
||||||
if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot multiply a string.\n"); }
|
if (x->type == STRING || y->type == STRING) { std::cerr << "Error, cannot multiply a string." << std::endl; }
|
||||||
|
|
||||||
struct Value* ans;
|
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) {
|
struct Value* less(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 == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot numerically compare a boolean.\n"); }
|
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)) {
|
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;
|
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) {
|
struct Value* greater(struct Value* x, struct Value* y) {
|
||||||
if (!x || !y) { fprintf(stderr, "Error, uninitialized values being used in greater.\n"); }
|
if (!x || !y) { std::cerr << "Error, uninitialized values being used in greater." << std::endl; }
|
||||||
if (x->type == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot numerically compare a boolean.\n"); }
|
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)) {
|
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;
|
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) {
|
struct Value* less_equal(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 == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot numerically compare a boolean.\n"); }
|
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)) {
|
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;
|
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) {
|
struct Value* greater_equal(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 == BOOLEAN || y->type == BOOLEAN) { fprintf(stderr, "Error, cannot numerically compare a boolean.\n"); }
|
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)) {
|
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;
|
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) {
|
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)) {
|
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;
|
struct Value* ans;
|
||||||
|
@ -214,16 +212,16 @@ struct Value* equals(struct Value* x, struct Value* y) {
|
||||||
} else if (x->type == STRING && y->type == STRING) {
|
} else if (x->type == STRING && y->type == STRING) {
|
||||||
ans = make_boolean(get_string(x).compare(get_string(y)) == 0);
|
ans = make_boolean(get_string(x).compare(get_string(y)) == 0);
|
||||||
} else { // Type is a mix between boolean and another type
|
} 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;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Value* not_equals(struct Value* x, struct Value* y) {
|
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)) {
|
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;
|
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) {
|
} else if (x->type == STRING && y->type == STRING) {
|
||||||
ans = make_boolean(get_string(x).compare(get_string(y)) != 0);
|
ans = make_boolean(get_string(x).compare(get_string(y)) != 0);
|
||||||
} else { // Type is a mix between boolean and another type
|
} 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;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Value* and_value(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) { std::cerr << "Error, uninitialized values being used in &&." << std::endl; }
|
||||||
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) { std::cerr << "Error, cannot use and AND operation with a non-boolean." << std::endl; }
|
||||||
if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot AND a string.\n"); }
|
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));
|
return make_boolean(get_long(x) && get_long(y));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Value* or_value(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) { std::cerr << "Error, uninitialized values being used in ||." << std::endl; }
|
||||||
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) { std::cerr << "Error, cannot use and OR operation with a non-boolean." << std::endl; }
|
||||||
if (x->type == STRING || y->type == STRING) { fprintf(stderr, "Error, cannot OR a string.\n"); }
|
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));
|
return make_boolean(get_long(x) || get_long(y));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Value* not_value(struct Value* x) {
|
struct Value* not_value(struct Value* x) {
|
||||||
if (!x) { fprintf(stderr, "Error, uninitialized values being used in !.\n"); }
|
if (!x) { std::cerr << "Error, uninitialized values being used in !." << std::endl; }
|
||||||
if (x->type != BOOLEAN) { fprintf(stderr, "Error, cannot NOT a non-boolean.\n"); }
|
if (x->type != BOOLEAN) { std::cerr << "Error, cannot NOT a non-boolean." << std::endl; }
|
||||||
if (x->type == STRING) { fprintf(stderr, "Error, cannot negate a string.\n"); }
|
if (x->type == STRING) { std::cerr << "Error, cannot negate a string." << std::endl; }
|
||||||
|
|
||||||
return make_boolean(!get_long(x));
|
return make_boolean(!get_long(x));
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ DIGIT [0-9]
|
||||||
\".*\" {yylval.value = make_node(VALUE, make_string(substring(yytext, 1, strlen(yytext) - 1)), ""); return VALUE; }
|
\".*\" {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} {yylval.value = make_node(VALUE, make_long(atoi(yytext)), ""); return VALUE;}
|
||||||
{DIGIT}*"."?{DIGIT}+ {yylval.value = make_node(VALUE, make_double(atof(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++;}
|
[\n] {linenum++;}
|
||||||
[ \t\r]+ {}
|
[ \t\r]+ {}
|
||||||
. {printf("Error: invlaid lexeme '%s'.\n", yytext); return 0;}
|
. {printf("Error: invlaid lexeme '%s'.\n", yytext); return 0;}
|
||||||
|
|
|
@ -30,9 +30,9 @@ FILE* stringToFile(char* str) { // Creates a temporary file with the given strin
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
FILE* tmp = tmpfile();
|
FILE* tmp = tmpfile();
|
||||||
if (tmp == NULL) {
|
if (tmp == nullptr) {
|
||||||
fprintf(stderr, "Unable to create temp file");
|
fprintf(stderr, "Unable to create temp file");
|
||||||
return NULL;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (str[i] != '\0') {
|
while (str[i] != '\0') {
|
||||||
|
|
|
@ -13,7 +13,7 @@ char* substring(char* str, int start, int end) {
|
||||||
int length = end - start;
|
int length = end - start;
|
||||||
substr = (char*) malloc(length + 1);
|
substr = (char*) malloc(length + 1);
|
||||||
|
|
||||||
if (substr == NULL) {
|
if (substr == nullptr) {
|
||||||
fprintf(stderr, "Unable to allocate memory for substring.\n");
|
fprintf(stderr, "Unable to allocate memory for substring.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "environment.hpp"
|
#include "environment.hpp"
|
||||||
#include "variable.hpp"
|
#include "variable.hpp"
|
||||||
|
@ -8,7 +7,7 @@ struct Environment* create_environment(void) {
|
||||||
struct Environment* env = new Environment();
|
struct Environment* env = new Environment();
|
||||||
env->num_vars = 0;
|
env->num_vars = 0;
|
||||||
for(int i = 0; i < MAX_VARIABLES; i++) {
|
for(int i = 0; i < MAX_VARIABLES; i++) {
|
||||||
env->vars[i] = NULL;
|
env->vars[i] = nullptr;
|
||||||
}
|
}
|
||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +18,7 @@ struct Variable* find_variable(struct Environment* env, std::string id) {
|
||||||
return env->vars[i];
|
return env->vars[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_variable(struct Environment* env, struct Variable* var) {
|
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
|
// If variable exists, replace it
|
||||||
struct Variable* temp_var = find_variable(env, var->id);
|
struct Variable* temp_var = find_variable(env, var->id);
|
||||||
if (temp_var != NULL) {
|
if (temp_var != nullptr) {
|
||||||
temp_var->value = var->value;
|
temp_var->value = var->value;
|
||||||
free(var);
|
free(var);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -26,16 +26,16 @@ struct Value* make_value(TypeTag type, long num, double dec, struct Node* expr,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Value* make_long(long num) {
|
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) {
|
struct Value* make_double(double dec) {
|
||||||
return make_value(DOUBLE, 0, dec, NULL, "");
|
return make_value(DOUBLE, 0, dec, nullptr, "");
|
||||||
}
|
}
|
||||||
struct Value* make_true() {
|
struct Value* make_true() {
|
||||||
return make_value(BOOLEAN, 1, 0, NULL, "");
|
return make_value(BOOLEAN, 1, 0, nullptr, "");
|
||||||
}
|
}
|
||||||
struct Value* make_false() {
|
struct Value* make_false() {
|
||||||
return make_value(BOOLEAN, 0, 0, NULL, "");
|
return make_value(BOOLEAN, 0, 0, nullptr, "");
|
||||||
}
|
}
|
||||||
struct Value* make_boolean(int x) {
|
struct Value* make_boolean(int x) {
|
||||||
return (x)? make_true() : make_false();
|
return (x)? make_true() : make_false();
|
||||||
|
@ -44,7 +44,7 @@ struct Value* make_expression(struct Node* expr) {
|
||||||
return make_value(LAMBDA, 0, 0, expr, "");
|
return make_value(LAMBDA, 0, 0, expr, "");
|
||||||
}
|
}
|
||||||
struct Value* make_string(std::string str) {
|
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) {
|
void delete_value(struct Value* val) {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include <stdio.h>
|
#include <iostream>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "variable.hpp"
|
#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) {
|
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;
|
var->value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Value* get_value(struct Variable* var) {
|
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;
|
return var->value;
|
||||||
}
|
}
|
Reference in a new issue