-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
185 lines (172 loc) · 5.5 KB
/
tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree.h"
Node* create_node(char* token, char* value, int line, int col){ /* Creates n0d3 */
//printf("start create node\n");
Node* tmp = (Node*) malloc(sizeof(Node));
tmp->child = NULL;
tmp->brother = NULL;
tmp->token = strdup(token);
tmp->line = line;
tmp->column = col;
tmp->param = NULL;
tmp->type = NULL;
tmp->function = 0;
if(value != NULL){
tmp->value = strdup(value);
} else {
tmp->value = NULL;
}
//printf("end create node\n");
return tmp; /* returns n0d3 */
}
Token* create_token(int line, int column, char *id){ /* Create token to return from lex */
Token* token = (Token*)malloc(sizeof(Token));
token->line = line;
token->column = column;
token->id = strdup(id);
return token;
}
void insert_first_child(Node *node, Node *child_node) { /* child_node = typespec */
if(child_node == NULL) /* If node to insert is null end function */
return;
Node *tmp = (Node*) malloc(sizeof(Node));
Node *uncle = node->brother;
if(node->child == NULL){ /* If node has no child, create one */
node->child = (Node*) malloc(sizeof(Node));
node->child = child_node;
} else {
tmp = node->child;
node->child = child_node;
node->child->brother = tmp;
}
while(uncle != NULL){ /* Goes through cousins to add type spec */
tmp = uncle->child;
uncle->child = create_node(child_node->token, NULL, child_node->line, child_node->column);
uncle->child->brother = tmp;
if(uncle->brother == NULL)
break;
uncle = uncle->brother;
}
}
Node* insert_child(Node *node, Node *child_node) {
//printf("start insert child\n");
if(node == NULL) /* If node to insert is null end function */
return NULL;
if(node->child == NULL){ /* If node has no child, create one */
node->child = (Node*) malloc(sizeof(Node));
node->child = child_node;
} else {
Node *tmp = (Node*) malloc(sizeof(Node));
Node *prev = (Node*) malloc(sizeof(Node));
prev = node->child;
tmp = node->child->brother;
while (tmp != NULL){ /* Goes through all the brothers */
prev = tmp;
if(tmp->brother == NULL)
break;
tmp = tmp->brother;
}
prev->brother = (Node*) malloc(sizeof(Node));
prev->brother=child_node;
}
//printf("end insert child\n");
return node;
}
Node* insert_brother(Node *node, Node *brother_node) {
//printf("start insert brother\n");
if(node == NULL) /* If node to insert is null end function */
return NULL;
Node *tmp = (Node*) malloc(sizeof(Node));
Node *prev = (Node*) malloc(sizeof(Node));
prev = node;
tmp = node->brother;
while (tmp != NULL){ /* Goes through all the brothers */
prev = tmp;
if(tmp->brother == NULL)
break;
tmp = tmp->brother;
}
prev->brother = (Node*) malloc(sizeof(Node));
prev->brother=brother_node;
//printf("end insert brother\n");
return node;
}
void print_child_brother(Node *node){
printf("brother_node->token: %s; brother_node->value: %s\n", node->brother->token, node->brother->value);
printf("node->child->token: %s; node->child->value: %s\n", node->child->token, node->child->value);
}
void print_tree(Node *node, int dots){
if(node == NULL)
return;
for (int i = 0; i < dots; ++i) {
printf("..");
}
if(node->value != NULL){
printf("%s(%s)\n", node->token, node->value);
} else{
printf("%s\n", node->token);
}
if(node->child != NULL)
print_tree(node->child, dots+1);
if(node->brother != NULL)
print_tree(node->brother, dots);
}
void print_annotated_tree(Node *node, int dots){
//printf("Token: %s, Value: %s, Type: %s\n", node->token, node->value, node->type);
if(node == NULL)
return;
for (int i = 0; i < dots; ++i) {
printf("..");
}
if(node->value != NULL){
printf("%s(%s)", node->token, node->value);
if(node->type != NULL){
printf(" - %s", node->type);
if(node->function != 0){
Param *aux = node->param;
if(aux != NULL){
printf("(%s", aux->type);
aux = aux->next;
while(aux != NULL){
printf(",%s", aux->type);
aux = aux->next;
}
printf(")");
}
}
}
printf("\n");
} else{
printf("%s", node->token);
if(node->type != NULL){
printf(" - %s", node->type);
if(node->function != 0){
Param *aux = node->param;
if(aux != NULL){
printf("(%s", aux->type);
aux = aux->next;
while(aux != NULL){
printf(",%s", aux->type);
aux = aux->next;
}
printf(")");
}
}
}
printf("\n");
}
if(node->child != NULL)
print_annotated_tree(node->child, dots+1);
if(node->brother != NULL)
print_annotated_tree(node->brother, dots);
}
void clear(Node *node){ /* Free node memory */
if(node != NULL) {
clear(node->child);
clear(node->brother);
free(node->token);
free(node);
}
}