-
Notifications
You must be signed in to change notification settings - Fork 4
/
tree.c
205 lines (156 loc) · 3.77 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*Tree.c: Syntax tree constructing functions*/
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
/*construction of the root*/
struct s_tree *
mkfile(struct s_tree *prog)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=MYFILE;
t->u.Program=prog;
return(t);
}
/*construction of a program node*/
struct s_tree *
mkprog(struct s_tree *variable,struct s_tree *makefun,struct s_tree *prog)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=PROG;
t->u.Prog.variable=variable;
t->u.Prog.makefun=makefun;
t->u.Prog.prog=prog;
return(t);
}
/*construction of a user function node*/
struct s_tree *
mkfun(struct s_tree *commands,char *name)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=MAKEFUN;
t->u.Makefun.commands=commands;
t->u.Makefun.name=name;
return(t);
}
/*construction of a variable declaration node*/
struct s_tree *
mkint(char *variable)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=INT;
t->u.Variable=variable;
return(t);
}
/*construction of a while command node*/
struct s_tree *
mkwhile(struct s_tree *cmp,struct s_tree *commands)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=WHILE;
t->u.While.cmp=cmp;
t->u.While.commands=commands;
return(t);
}
/*construction of an if and else command node*/
struct s_tree *
mkifelse(struct s_tree *cmp,struct s_tree *commands,struct s_tree *else_commands)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=IFELSE;
t->u.Ifelse.cmp=cmp;
t->u.Ifelse.commands=commands;
t->u.Ifelse.else_commands=else_commands;
return(t);
}
/*construction of command nodes*/
struct s_tree *
mkcommands(struct s_tree *command,struct s_tree *commands)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=COMMANDS;
t->u.Commands.command=command;
t->u.Commands.commands=commands;
return(t);
}
struct s_tree *
mkputstr(char *string)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=PUTSTR;
t->u.String=string;
return(t);
}
struct s_tree *
mkputint(struct s_tree *binop)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=PUTINT;
t->u.Putint.binop=binop;
return(t);
}
struct s_tree *
mkgetint(char *variable)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=GETINT;
t->u.Variable=variable;
return(t);
}
/*construction of a node which handles calls of user defined functions*/
struct s_tree *
mkuserfun(char *variable)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=USERFUN;
t->u.Variable=variable;
return(t);
}
/*construction of a binary operation node*/
struct s_tree *
mkbinop(struct s_tree *left,struct s_tree *right,char op)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=BINOP;
t->u.Binop.left=left;
t->u.Binop.right=right;
t->u.Binop.op=op;
return(t);
}
/*construction of a comparison node*/
struct s_tree *
mkcmp(struct s_tree *left,struct s_tree *right,char *op)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=CMP;
t->u.Cmp.left=left;
t->u.Cmp.right=right;
t->u.Cmp.op=op;
return(t);
}
struct s_tree *
mkunop(struct s_tree *exp,char op)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=UNOP;
t->u.Unop.exp=exp;
t->u.Unop.op=op;
return(t);
}
/*construction of a node which carries a variable*/
struct s_tree *
mkvariable(char *variable)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=VAR;
t->u.Variable=variable;
return(t);
}
/*construction of a node which carries a nnumber*/
struct s_tree *
mknumber(int number)
{
struct s_tree *t=(struct s_tree *)malloc(sizeof(struct s_tree));
t->nt=NUM;
t->u.Number=number;
return (t);
}