-
Notifications
You must be signed in to change notification settings - Fork 0
/
declarations.c
276 lines (249 loc) · 6.09 KB
/
declarations.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "declarations.h"
#include "symtab.h"
#include "codeGen.h"
struct ast* newast(int nodetype, struct ast *l, struct ast *r){
struct ast *a = malloc(sizeof(struct ast));
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = nodetype;
a->l = l;
a->r = r;
return a;
}
struct ast *newnum(double d){
struct numval *a = malloc(sizeof(struct numval));
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = 'K';
a->value = d;
return (struct ast *)a;
}
struct ast *newflow(int nodetype, struct ast *cond, struct ast *tl, struct ast *el){
struct flow *a = malloc(sizeof(struct flow));
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = nodetype ;
a->cond = cond ;
a->thenb = tl ;
a->elseb = el ;
return (struct ast*) a ;
}
struct ast *newcmp(int cmptype, struct ast *l, struct ast *r){
return newast('0'+cmptype,l,r) ;
}
struct ast *newref(struct symbol *s){
struct symref *a = malloc(sizeof(struct symref)) ;
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype='N';
a->s = s ;
return (struct ast*) a ;
}
struct ast *newasgn(struct symbol *s, struct ast *a){
struct symasgn *b = malloc(sizeof(struct symasgn));
if(!b) {
yyerror("out of space");
exit(0);
}
b->nodetype = '=' ;
b->s = s ;
b->a = a ;
return (struct ast*) b ;
}
/*
creates a func node
pointer to the symbol in the symbol table
ast of type L list
*/
struct ast *newfunc(struct symbol *s,struct ast *l){
struct func *a = malloc(sizeof(struct func));
if(!a){
yyerror("out of space");
exit(0);
}
a->nodetype = 'C' ;
a->l = l ;
a->s = s ;
return (struct ast *) a ;
}
double eval(struct ast *a) {
double v; // calculated value of this subtree
switch(a->nodetype) {
case 'K': v = ((struct numval *)a)->value; break;
/* return the value in the symbol table */
case 'N': v = (((struct symref *)a)->s)->value ;
/* evaluate the AST and put the value to the symbol table */
case '=': v = ((struct symasgn *)a)->s->value = eval(((struct symasgn *)a)->a) ; break ;
case '+': v = eval(a->l) + eval(a->r); break;
case '-': v = eval(a->l) - eval(a->r); break;
case '*': v = eval(a->l) * eval(a->r); break;
case '/': v = eval(a->l) / eval(a->r); break;
case 'M': v = -eval(a->l); break;
/* comparaison */
/*>*/
case '1': v = (eval(a->l)>eval(a->r))? 1 : 0 ; break ;
/*<*/
case '2': v = (eval(a->l)<eval(a->r))? 1 : 0 ; break ;
/*!=*/
case '3': v = (eval(a->l)!=eval(a->r))? 1 : 0 ; break ;
/*==*/
case '4': v = (eval(a->l)==eval(a->r))? 1 : 0 ; break ;
/*>=*/
case '5': v = (eval(a->l)>=eval(a->r))? 1 : 0 ; break ;
/*<=*/
case '6': v = (eval(a->l)<=eval(a->r))? 1 : 0 ; break ;
case 'I':
if(eval(((struct flow*)a)->cond)){
if(((struct flow*)a)->thenb){
printf("inside then \n") ;
printf("%c\n",((struct flow*)a)->thenb->nodetype) ;
printf("%c\n",((struct flow*)a)->thenb->l->nodetype) ;
printf("%c\n",((struct flow*)a)->thenb->r->nodetype) ;
v = eval(((struct flow*)a)->thenb) ;
}else{
v=0.0;
}
}else{
if(((struct flow*)a)->elseb){
v = eval(((struct flow*)a)->elseb) ;
}else{
v=0.0;
}
}
case 'L': eval(a->l); v = eval(a->r); break;
default: printf("internal error: bad node %c\n", a->nodetype);
}
return v;
}
void treefree(struct ast *a) {
switch(a->nodetype) {
/* two subtrees */
case '+':
case '-':
case '*':
case '/':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
treefree(a->r);
/* one subtree */
case 'M':
treefree(a->l);
/* no subtree */
case 'K': case 'N':
free(a);
break;
case '=':
free(((struct symasgn*)a)->a);
break;
case 'I':
free(((struct flow*)a)->cond);
if (((struct flow*)a)->thenb) free(((struct flow*)a)->thenb);
if (((struct flow*)a)->elseb) free(((struct flow*)a)->elseb);
break;
default: printf("internal error: free bad node %c\n", a->nodetype);
}
free(a);
}
void print_ast(struct ast* node, int level) {
if (node == NULL) {
return;
}
int i;
for (i = 0; i < level; i++) {
printf(" ");
}
switch (node->nodetype) {
case 'K':
printf("%f\n",((struct numval*)node)->value);
break;
case 'N':
printf("%s",((struct symref*)node)->s->name);
break;
case '=':
printf("= \n");
print_ast(node->l, level + 1);
print_ast(node->r, level + 1);
break;
case '+':
case '-':
case '*':
case '/':
printf("%c\n", node->nodetype);
print_ast(node->l, level + 1);
print_ast(node->r, level + 1);
break;
case 'I':
printf("if\n");
print_ast(((struct flow*)node)->thenb, level + 1);
print_ast(((struct flow*)node)->elseb, level + 1);
break;
case 'W':
printf("while\n");
print_ast(node->l, level + 1);
print_ast(node->r, level + 1);
break;
case 'L':
printf("list\n");
print_ast(node->l, level + 1);
print_ast(node->r, level);
break;
case 'F':
printf("call\n");
print_ast(node->l, level + 1);
break;
case 'M':
printf("modifier \n");
print_ast(node->l, level + 1);
break;
case 'C':
printf("user_func\n");
print_ast(node->l, level + 1);
print_ast(node->r, level + 1);
break;
default:
printf("unknown nodetype %c\n", node->nodetype);
break;
}
}
extern FILE *yyin;
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Usage: %s filename\n", argv[0]);
return 1;
}
FILE* fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Error: cannot open file '%s'\n", argv[1]);
return 1;
}
yyin = fp;
yyparse();
print_symtab();
printf("\n");
print_machine_code();
fclose(fp);
return 0;
}
void yyerror(char *s, ...) {
va_list ap;
va_start(ap, s);
fprintf(stderr, "%d: error: ", yylineno);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
exit(1);
}