-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathASTnode.c
More file actions
108 lines (102 loc) · 2.43 KB
/
Copy pathASTnode.c
File metadata and controls
108 lines (102 loc) · 2.43 KB
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
#include "ASTnode.h"
extern int yylineno;
ASTnode* createNodeOp(char* name)
{
ASTnode *node ;
node = malloc(sizeof(*node)) ;
strcpy(node->name,name) ;
node->child = NULL ;
node->next = NULL ;
node->lineno = yylineno ;
return node ;
}
ASTnode* createNodeString(char* name,char* value)
{
ASTnode *node ;
node = malloc(sizeof(*node)) ;
strcpy(node->name,name) ;
strcpy(node->data.value,value) ;
node->child = NULL ;
node->next = NULL ;
node->lineno = yylineno ;
return node ;
}
ASTnode* createNodeInt(char* name,int int_val)
{
ASTnode *node ;
node = malloc(sizeof(*node)) ;
strcpy(node->name,name) ;
node->data.int_val = int_val ;
node->child = NULL ;
node->next = NULL ;
node->lineno = yylineno ;
return node ;
}
ASTnode* createNodeFloat(char* name,float float_val)
{
ASTnode *node ;
node = malloc(sizeof(*node)) ;
strcpy(node->name,name) ;
node->data.float_val = float_val ;
node->child = NULL ;
node->next = NULL ;
node->lineno = yylineno ;
return node ;
}
void addChild(ASTnode* fa,ASTnode* child)
{
if(child == NULL)
{
fa->child = NULL ;
return ;
}
if(fa != NULL && child != NULL)
{
child->next = fa->child ;
fa->child = child ;
fa->lineno = child->lineno ;
}
}
void printTree(ASTnode* root,int count)
{
if(root == NULL) return ;
if(root->child == NULL)
{
for(int i = 0 ; i < count ; i++)
{
printf(" ") ;
}
if(strcmp(root->name,"TYPE") == 0
|| strcmp(root->name,"INTCONST") == 0
|| strcmp(root->name,"ID") == 0)
{
if(strcmp(root->child->name,"INTCONST") == 0)
{
printf("%s: %d\n",root->name,root->data.int_val) ;
}
else
{
printf("%s: %s\n",root->name,root->data.value) ;
}
}
else
{
printf("%s\n",root->name) ;
}
}
else
{
for(int i = 0 ; i < count ; i++)
{
printf(" ") ;
}
printf("%s(%d)\n",root->name,root->lineno) ;
ASTnode* nxt = root->child ;
while(nxt != NULL)
{
printTree(nxt,count+1) ;
nxt = nxt->next ;
}
}
return ;
}