-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
executable file
·106 lines (90 loc) · 2.71 KB
/
ast.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
#include <stdlib.h>
#include "ast.h"
tree_node* create_simple_node(char* name,int line,int col) {
tree_node* new_node = (tree_node*) malloc( sizeof(tree_node) );
if (new_node != NULL) {
strcpy(new_node->name, name);
new_node->next_brother = NULL;
new_node->son = NULL;
new_node->father = NULL;
new_node->line = line;
new_node->col = col;
new_node->an_params = NULL;
new_node->an_n_params = 0;
strcpy(new_node->an_type,"");
} else {
printf("ERROR SIMPLE NODE\n");
}
return new_node;
}
tree_node* create_value_node(char* name, char* value,int line,int col) {
tree_node* new_node = (tree_node*)malloc(sizeof(tree_node));
if(new_node != NULL) {
strcpy(new_node->name, name);
new_node->next_brother = NULL;
new_node->son = NULL;
new_node->father = NULL;
new_node->line = line;
new_node->col = col;
new_node->an_params = NULL;
new_node->an_n_params = 0;
strcpy(new_node->an_type,"");
strcpy(new_node->value, value);
} else {
printf("ERROR STR NODE\n");
}
return new_node;
}
void add_child(tree_node * father , tree_node * son){
if(father->son != NULL) {
son->next_brother = father->son;
son->father = father;
father->son = son;
} else {
son->father = father;
father->son = son;
}
}
void add_brother(tree_node* brother, tree_node* new_son) {
tree_node* aux = brother;
if(aux!= NULL && new_son != NULL) {
while(aux->next_brother != NULL) {
aux = aux->next_brother;
}
aux->next_brother = new_son;
new_son->father = brother->father;
}
}
/* PRINTING */
void print_terminal(tree_node* node){
//printf("%s(%s)\tline: %d\tcol: %d\n", node->name, node->value,node->line,node->col);
printf("%s(%s)\n", node->name, node->value);
}
void print_points(int n){
while(n > 0){
printf("..");
n--;
}
}
void print_node(tree_node *node){
if(strcmp(node->name, "Id") == 0 || strcmp(node->name, "ChrLit") == 0 || strcmp(node->name, "RealLit") == 0 || strcmp(node->name, "IntLit") == 0){
print_terminal(node);
} else {
//printf("%s\tline: %d\tcol: %d\n", node->name,node->line,node->col);
printf("%s\n", node->name);
}
}
void print_tree(tree_node* node, int level){
if(node != NULL) {
print_points(level);
print_node(node);
tree_node* child = node->son;
if(child != NULL){
print_tree(child, level + 1);
while(child->next_brother != NULL){
child = child->next_brother;
print_tree(child, level + 1);
}
}
}
}