Replaced arrays for environment with vectors that way we can hold "unlimited" variables.
This commit is contained in:
parent
f8f7829cb6
commit
0bd0cf3435
2 changed files with 5 additions and 16 deletions
|
@ -5,15 +5,11 @@
|
|||
|
||||
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] = nullptr;
|
||||
}
|
||||
return env;
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
if (env->num_vars >= MAX_VARIABLES) {
|
||||
std::cerr << "Error: Maximum number of variables reached." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// If variable exists, replace it
|
||||
struct Variable* temp_var = find_variable(env, var->id);
|
||||
if (temp_var != nullptr) {
|
||||
|
@ -36,12 +27,11 @@ void add_variable(struct Environment* env, struct Variable* var) {
|
|||
}
|
||||
|
||||
// If not, add variable to environment
|
||||
env->vars[env->num_vars] = var;
|
||||
env->num_vars += 1;
|
||||
env->vars.push_back(var);
|
||||
}
|
||||
|
||||
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);
|
||||
|
|
|
@ -2,11 +2,10 @@
|
|||
#define ENVIRONMENT_H
|
||||
|
||||
#include <string>
|
||||
#define MAX_VARIABLES 200
|
||||
#include <vector>
|
||||
|
||||
struct Environment {
|
||||
int num_vars;
|
||||
struct Variable* vars[MAX_VARIABLES];
|
||||
std::vector<struct Variable*> vars;
|
||||
};
|
||||
|
||||
// Variable Lookup Functions
|
||||
|
|
Reference in a new issue