Eradicated the char* existent in the variable struct
This commit is contained in:
parent
3cdf1f439c
commit
492a581d19
4 changed files with 25 additions and 11 deletions
|
@ -1,6 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef ENVIRONMENT_H
|
||||
#define ENVIRONMENT_H
|
||||
|
||||
#include <string>
|
||||
#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
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#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 */
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
#ifndef VARIABLE_H
|
||||
#define VARIABLE_H
|
||||
|
||||
#include <string>
|
||||
#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;
|
||||
// }
|
Reference in a new issue