diff --git a/src/variables/environment.cpp b/src/variables/environment.cpp index 255a329..31e3530 100644 --- a/src/variables/environment.cpp +++ b/src/variables/environment.cpp @@ -1,6 +1,6 @@ -#include +#include #include -#include +#include #include "environment.hpp" #include "variable.hpp" @@ -13,9 +13,9 @@ struct Environment* create_environment(void) { return env; } -struct Variable* find_variable(struct Environment* env, char* id) { +struct Variable* find_variable(struct Environment* env, std::string id) { for (int i = 0; i < env->num_vars; i++) { - if (strcmp(env->vars[i]->id, id) == 0) { + if (id.compare(env->vars[i]->id) == 0) { return env->vars[i]; } } @@ -24,7 +24,7 @@ struct Variable* find_variable(struct Environment* env, char* id) { void add_variable(struct Environment* env, struct Variable* var) { if (env->num_vars >= MAX_VARIABLES) { - fprintf(stderr, "Error: Maximum number of variables reached.\n"); + std::cerr << "Error: Maximum number of variables reached." << std::endl; return; } diff --git a/src/variables/environment.hpp b/src/variables/environment.hpp index 2408410..3f18d15 100644 --- a/src/variables/environment.hpp +++ b/src/variables/environment.hpp @@ -1,6 +1,7 @@ #ifndef ENVIRONMENT_H #define ENVIRONMENT_H +#include #define MAX_VARIABLES 200 struct Environment { @@ -11,7 +12,7 @@ struct Environment { // Variable Lookup Functions struct Environment* create_environment(void); void delete_environment(struct Environment* env); -struct Variable* find_variable(struct Environment* env, char* id); +struct Variable* find_variable(struct Environment* env, std::string id); void add_variable(struct Environment* env, struct Variable* var); #endif diff --git a/src/variables/variable.cpp b/src/variables/variable.cpp index 6261bef..3a95395 100644 --- a/src/variables/variable.cpp +++ b/src/variables/variable.cpp @@ -1,15 +1,15 @@ #include #include -#include +#include #include "variable.hpp" /* creates a new variable and returns it */ -struct Variable* make_variable(char* id, struct Value* value) { +struct Variable* make_variable(std::string id, struct Value* value) { /* allocate space */ struct Variable* var = new Variable(); /* set properties */ - strcpy(var->id, id); + var->id = id; var->value = value; /* return new variable */ diff --git a/src/variables/variable.hpp b/src/variables/variable.hpp index 7138076..f951089 100644 --- a/src/variables/variable.hpp +++ b/src/variables/variable.hpp @@ -1,15 +1,16 @@ #ifndef VARIABLE_H #define VARIABLE_H +#include #include "../operations/node.hpp" struct Variable { - char id[ID_SIZE]; + std::string id; struct Value* value; }; // Variable Functions -struct Variable* make_variable(char* id, struct Value* value); +struct Variable* make_variable(std::string id, struct Value* value); void set_value(struct Variable* var, struct Value* value); struct Value* get_value(struct Variable* var); struct Value* make_long(long num); @@ -20,3 +21,15 @@ struct Value* make_boolean(int x); struct Value* make_expression(struct Node* expr); #endif +// /* creates a new variable and returns it */ +// struct Variable* make_variable(char* id, struct Value* value) { +// /* allocate space */ +// struct Variable* var = new Variable(); + +// /* set properties */ +// strcpy(var->id, id); +// var->value = value; + +// /* return new variable */ +// return var; +// } \ No newline at end of file