Archived
1
0
Fork 0

Replaced arrays for environment with vectors that way we can hold "unlimited" variables.

This commit is contained in:
Brandon Rozek 2018-09-27 22:57:00 -04:00
parent f8f7829cb6
commit 0bd0cf3435
2 changed files with 5 additions and 16 deletions

View file

@ -5,15 +5,11 @@
struct Environment* create_environment(void) { struct Environment* create_environment(void) {
struct Environment* env = new Environment(); struct Environment* env = new Environment();
env->num_vars = 0;
for(int i = 0; i < MAX_VARIABLES; i++) {
env->vars[i] = nullptr;
}
return env; return env;
} }
struct Variable* find_variable(struct Environment* env, std::string id) { struct Variable* find_variable(struct Environment* env, std::string id) {
for (int i = 0; i < env->num_vars; i++) { for (uint i = 0; i < size(env->vars); i++) {
if (id.compare(env->vars[i]->id) == 0) { if (id.compare(env->vars[i]->id) == 0) {
return env->vars[i]; return env->vars[i];
} }
@ -22,11 +18,6 @@ struct Variable* find_variable(struct Environment* env, std::string id) {
} }
void add_variable(struct Environment* env, struct Variable* var) { void add_variable(struct Environment* env, struct Variable* var) {
if (env->num_vars >= MAX_VARIABLES) {
std::cerr << "Error: Maximum number of variables reached." << std::endl;
return;
}
// 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 != nullptr) { if (temp_var != nullptr) {
@ -36,12 +27,11 @@ void add_variable(struct Environment* env, struct Variable* var) {
} }
// If not, add variable to environment // If not, add variable to environment
env->vars[env->num_vars] = var; env->vars.push_back(var);
env->num_vars += 1;
} }
void delete_environment(struct Environment* env) { void delete_environment(struct Environment* env) {
for (int i = 0; i < env->num_vars; i++) { for (uint i = 0; i < size(env->vars); i++) {
free(env->vars[i]); free(env->vars[i]);
} }
free(env); free(env);

View file

@ -2,11 +2,10 @@
#define ENVIRONMENT_H #define ENVIRONMENT_H
#include <string> #include <string>
#define MAX_VARIABLES 200 #include <vector>
struct Environment { struct Environment {
int num_vars; std::vector<struct Variable*> vars;
struct Variable* vars[MAX_VARIABLES];
}; };
// Variable Lookup Functions // Variable Lookup Functions