Archived
1
0
Fork 0

Eradicated the char* existent in the variable struct

This commit is contained in:
Brandon Rozek 2018-09-26 19:42:22 -04:00
parent 3cdf1f439c
commit 492a581d19
4 changed files with 25 additions and 11 deletions

View file

@ -1,6 +1,6 @@
#include <stdio.h> #include <iostream>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string>
#include "environment.hpp" #include "environment.hpp"
#include "variable.hpp" #include "variable.hpp"
@ -13,9 +13,9 @@ struct Environment* create_environment(void) {
return env; 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++) { 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]; 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) { void add_variable(struct Environment* env, struct Variable* var) {
if (env->num_vars >= MAX_VARIABLES) { 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; return;
} }

View file

@ -1,6 +1,7 @@
#ifndef ENVIRONMENT_H #ifndef ENVIRONMENT_H
#define ENVIRONMENT_H #define ENVIRONMENT_H
#include <string>
#define MAX_VARIABLES 200 #define MAX_VARIABLES 200
struct Environment { struct Environment {
@ -11,7 +12,7 @@ struct Environment {
// Variable Lookup Functions // Variable Lookup Functions
struct Environment* create_environment(void); struct Environment* create_environment(void);
void delete_environment(struct Environment* env); 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); void add_variable(struct Environment* env, struct Variable* var);
#endif #endif

View file

@ -1,15 +1,15 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string>
#include "variable.hpp" #include "variable.hpp"
/* creates a new variable and returns it */ /* 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 */ /* allocate space */
struct Variable* var = new Variable(); struct Variable* var = new Variable();
/* set properties */ /* set properties */
strcpy(var->id, id); var->id = id;
var->value = value; var->value = value;
/* return new variable */ /* return new variable */

View file

@ -1,15 +1,16 @@
#ifndef VARIABLE_H #ifndef VARIABLE_H
#define VARIABLE_H #define VARIABLE_H
#include <string>
#include "../operations/node.hpp" #include "../operations/node.hpp"
struct Variable { struct Variable {
char id[ID_SIZE]; std::string id;
struct Value* value; struct Value* value;
}; };
// Variable Functions // 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); void set_value(struct Variable* var, struct Value* value);
struct Value* get_value(struct Variable* var); struct Value* get_value(struct Variable* var);
struct Value* make_long(long num); struct Value* make_long(long num);
@ -20,3 +21,15 @@ struct Value* make_boolean(int x);
struct Value* make_expression(struct Node* expr); struct Value* make_expression(struct Node* expr);
#endif #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;
// }