Archived
1
0
Fork 0

Replaced char* with string in node functionality

This commit is contained in:
Brandon Rozek 2018-09-26 17:51:17 -04:00
parent 24e3003c00
commit f11a422819
2 changed files with 8 additions and 7 deletions

View file

@ -1,6 +1,6 @@
#include <stdio.h> #include <iostream>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string>
#include <assert.h> #include <assert.h>
#include "node.h" #include "node.h"
#include "operators.h" #include "operators.h"
@ -10,7 +10,7 @@
#include "../variables/variable.h" #include "../variables/variable.h"
/* creates a new node and returns it */ /* creates a new node and returns it */
struct Node* make_node(int type, struct Value* value, char* id) { struct Node* make_node(int type, struct Value* value, std::string id) {
int i; int i;
/* allocate space */ /* allocate space */
@ -19,7 +19,7 @@ struct Node* make_node(int type, struct Value* value, char* id) {
/* set properties */ /* set properties */
node->type = type; node->type = type;
node->value = value; node->value = value;
strcpy(node->id, id); id.copy(node->id, id.length(), 0);
node->num_children = 0; node->num_children = 0;
for(i = 0; i < MAX_CHILDREN; i++) { for(i = 0; i < MAX_CHILDREN; i++) {
node->children[i] = NULL; node->children[i] = NULL;
@ -37,9 +37,9 @@ void attach_node(struct Node* parent, struct Node* child) {
assert(parent->num_children <= MAX_CHILDREN); assert(parent->num_children <= MAX_CHILDREN);
} }
void check_num_nodes(struct Node* node, int num_children, char* error) { void check_num_nodes(struct Node* node, int num_children, std::string error) {
if (node && node->num_children != num_children) { if (node && node->num_children != num_children) {
fprintf(stderr, "%s%s%s", "Error, ", error, "\n"); std::cerr << "Error, " << error << std::endl;
} }
} }

View file

@ -1,6 +1,7 @@
#ifndef NODE_H #ifndef NODE_H
#define NODE_H #define NODE_H
#include <string>
#include "../variables/value.h" #include "../variables/value.h"
#include "../variables/environment.h" #include "../variables/environment.h"
@ -21,7 +22,7 @@ struct Node {
}; };
// Abstract Syntax Tree Functions // Abstract Syntax Tree Functions
struct Node* make_node(int type, struct Value* value, char* id); struct Node* make_node(int type, struct Value* value, std::string id);
void attach_node(struct Node* parent, struct Node* child); void attach_node(struct Node* parent, struct Node* child);
void print_tree(struct Node* node, int tabs); void print_tree(struct Node* node, int tabs);
void delete_tree(struct Node* node); void delete_tree(struct Node* node);