Archived
1
0
Fork 0

Replaced ints with uints and added comments indicating where I'll want delete statements in the future.

This commit is contained in:
Brandon Rozek 2018-09-28 14:11:58 -04:00
parent 9a727aa83b
commit 33bb265d27
5 changed files with 18 additions and 7 deletions

View file

@ -38,6 +38,7 @@ void interpret_file(char* fileName) {
struct Environment* env = new Environment(); struct Environment* env = new Environment();
eval_statement(result, env); eval_statement(result, env);
delete_environment(env); delete_environment(env);
// delete env;
delete result; delete result;
} }

View file

@ -15,14 +15,14 @@ void attach_node(struct Node* parent, struct Node* child) {
if (parent->num_children > MAX_CHILDREN) { std::cerr << "Error, max children attached to a node" << std::endl; } if (parent->num_children > MAX_CHILDREN) { std::cerr << "Error, max children attached to a node" << std::endl; }
} }
void check_num_nodes(struct Node* node, int num_children, std::string error) { void check_num_nodes(struct Node* node, uint num_children, std::string error) {
if (node && node->num_children != num_children) { if (node && node->num_children != num_children) {
std::cerr << "Error, " << error << std::endl; std::cerr << "Error, " << error << std::endl;
} }
} }
void print_tree(struct Node* node, int tabs) { void print_tree(struct Node* node, uint tabs) {
int i; uint i;
/* base case */ /* base case */
if(!node) { if(!node) {
std::cerr << "NO TREE STRUCTURE" << std::endl; std::cerr << "NO TREE STRUCTURE" << std::endl;
@ -113,6 +113,7 @@ struct Value* eval_expression(struct Node* node, struct Environment* env) {
eval_expression(node->children[1], env))); eval_expression(node->children[1], env)));
tempVal = eval_expression(tempNode->children[1], local_env); tempVal = eval_expression(tempNode->children[1], local_env);
delete_environment(local_env); delete_environment(local_env);
// delete local_env;
return tempVal; return tempVal;
break; break;
case PLUS: case PLUS:

View file

@ -17,19 +17,21 @@ struct Node {
std::string id; std::string id;
/* at most three children nodes */ /* at most three children nodes */
int num_children; uint num_children;
std::array<struct Node*, MAX_CHILDREN> children; std::array<struct Node*, MAX_CHILDREN> children;
Node(int t, struct Value* v, std::string s) { Node(int t, struct Value* v, std::string s) {
type = t; type = t;
value = v; value = v;
id = s; id = s;
for (int i = 0; i < MAX_CHILDREN; i++) { for (uint i = 0; i < MAX_CHILDREN; i++) {
children[i] = nullptr; children[i] = nullptr;
} }
} }
~Node() { ~Node() {
for (int i = 0; i < num_children; i++) { if (value) { delete value; }
// delete value;
for (uint i = 0; i < num_children; i++) {
delete children[i]; delete children[i];
} }
} }

View file

@ -70,5 +70,6 @@ void start_shell() {
} }
delete_environment(env); delete_environment(env);
// delete env;
delete result; delete result;
} }

View file

@ -1,12 +1,18 @@
#ifndef ENVIRONMENT_H #ifndef ENVIRONMENT_H
#define ENVIRONMENT_H #define ENVIRONMENT_H
#include "variable.hpp"
#include <string> #include <string>
#include <vector> #include <vector>
struct Environment { struct Environment {
std::vector<struct Variable*> vars; std::vector<struct Variable*> vars;
Environment() { } Environment() { }
// ~Environment() {
// for (uint i = 0; i < size(vars); i++) {
// delete vars[i];
// }
// }
}; };
// Variable Lookup Functions // Variable Lookup Functions