-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsave.c
436 lines (406 loc) · 9.02 KB
/
save.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/****************************************************************/
/* This modules carries out the storing of a graph on a textual */
/* file, to permit consultation. */
/* In particular, a list is stored of all the links existing */
/* among the forms (specifying form types and ports), also */
/* provided with an index of the forms and their associated */
/* information. */
/* The following function is external: */
/* - save(): Saves a graph on a file. */
/* The followinq functions are internal: */
/* - present(): Checks whether a form has already been copied */
/* once. */
/* - stampa(): Saves on file a link */
/* - save_aux(): Saves any graph part. */
/* - put_form(): Prints form name. */
/* - put_int(): Prints NIL, INT and BOOL forms names. */
/* - num_port(): Returns a form's ports number. */
/* - eindex(): Saves a file index row */
/****************************************************************/
/****************************************************************/
/* 1. Inclusion of header files. */
/****************************************************************/
#define ENTRY 17
#define NUM 13
#include "bohm.h"
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/****************************************************************/
/* 2. Inclusion of declarations that are being imported. */
/****************************************************************/
/****************************************************************/
/* 3. Declaration of names strictly local to the module. */
/****************************************************************/
FILE *save_file;
ELEM *head,*tail;
int max;
static int present();
static void save_aux();
static void stampa();
static void put_int();
static void put_form();
static int num_port();
static void eindex();
/****************************************************************/
/* 4. Definitions of functions to be exported. */
/****************************************************************/
/* The following function saves a graph on a file. */
void save(name,root,id)
char *name;
FORM *root;
char *id;
{
ELEM *p,*dep;
if(root!=NULL){
save_file=fopen(name,"w");
if (save_file==NULL)
exit(0);
head=tail=NULL;
max=1;
stampa(root,0,present(root));
if(root->nport[0]>=0)
save_aux(root->nform[0],root->nport[0]);
p=head;
fprintf(save_file,"\n\n\nI N D E X :\n\n");
while (p!=NULL) {
eindex(p);
dep=p;
p=p->next;
free(dep);
}
fclose(save_file);
if (id==NULL)
printf("\nSaved last input term in the file %s.\n",name);
else
printf("\nSaved %s term in the file %s.\n",id,name);
}
else {
printf("******************************************\n");
printf("* No terms inserted yet . . . *\n");
printf("******************************************\n");
}
}
/****************************************************************/
/* 5. Definitions of functions strictly local to the module. */
/****************************************************************/
/* The following function checks whether a form has already */
/* been copied once. */
static int
present(form)
FORM *form;
{
ELEM *p;
int risp=true;
p=head;
while(p!=NULL && risp)
if (p->node==form)
risp=false;
else
p=p->next;
if (risp) {
if (head==NULL) {
head=tail=(ELEM *)malloc_da(sizeof(ELEM));
}
else {
tail->next=(ELEM *)malloc_da(sizeof(ELEM));
tail=tail->next;
}
tail->node=form;
tail->next=NULL;
tail->num=risp=max++;
}
return risp;
}
/* The following function saves on file a link */
static void
stampa(form,p,card)
FORM *form;
int p;
int card;
{
int p1;
p1=form->nport[p];
fprintf(save_file,"%4d ",card);
put_form(form);
if (p1<0) {
fprintf(save_file,"%d -> 0 ",p);
put_int(form->nform[p],p1);
}
else {
fprintf(save_file,"%d -> %d ",p,p1);
put_form(form->nform[p]);
}
fprintf(save_file,"\n");
}
/* The following function saves any graph part. */
static void
save_aux(root,p)
FORM *root;
int p;
{
int n,p1,card;
card = present(root);
if(card) {
n=num_port(root->name);
for (p1=0;p1<n;p1++)
stampa(root,p1,card);
for (p1=0;p1<n;p1++)
if(root->nport[p1]>=0)
save_aux(root->nform[p1],root->nport[p1]);
}
}
/* The following function prints form name. */
static void
put_form(f)
FORM *f;
{
switch (f->name)
{
case FAN:
fprintf(save_file,"FAN ");
break;
case TRIANGLE:
fprintf(save_file,"TRIANGLE ");
break;
case ROOT:
fprintf(save_file,"ROOT ");
break;
case APP:
fprintf(save_file,"APP ");
break;
case LAMBDA:
fprintf(save_file,"LAMBDA ");
break;
case IFELSE:
fprintf(save_file,"IFELSE ");
break;
case AND:
fprintf(save_file,"AND ");
break;
case OR:
fprintf(save_file,"OR ");
break;
case NOT:
fprintf(save_file,"NOT ");
break;
case LESS:
fprintf(save_file,"LESS ");
break;
case LESS1:
fprintf(save_file,"LESS1 ");
break;
case EQ:
fprintf(save_file,"EQ ");
break;
case EQ1:
fprintf(save_file,"EQ1 ");
break;
case NOTEQ:
fprintf(save_file,"NOTEQ ");
break;
case NOTEQ1:
fprintf(save_file,"NOTEQ1 ");
break;
case MORE:
fprintf(save_file,"MORE ");
break;
case MORE1:
fprintf(save_file,"MORE1 ");
break;
case LEQ:
fprintf(save_file,"LEQ ");
break;
case LEQ1:
fprintf(save_file,"LEQ1 ");
break;
case MEQ:
fprintf(save_file,"MEQ ");
break;
case MEQ1:
fprintf(save_file,"MEQ1 ");
break;
case ADD:
fprintf(save_file,"ADD ");
break;
case ADD1:
fprintf(save_file,"ADD1 ");
break;
case SUB:
fprintf(save_file,"SUB ");
break;
case SUB1:
fprintf(save_file,"SUB1 ");
break;
case PROD:
fprintf(save_file,"PROD ");
break;
case PROD1:
fprintf(save_file,"PROD1 ");
break;
case DIV:
fprintf(save_file,"DIV ");
break;
case DIV1:
fprintf(save_file,"DIV1 ");
break;
case MOD:
fprintf(save_file,"MOD ");
break;
case MOD1:
fprintf(save_file,"MOD1 ");
break;
case CONS:
fprintf(save_file,"CONS ");
break;
case CAR:
fprintf(save_file,"CAR ");
break;
case CDR:
fprintf(save_file,"CDR ");
break;
case TESTNIL:
fprintf(save_file,"TESTNIL ");
break;
case LAMBDAUNB:
fprintf(save_file,"LAMBDAUNB ");
break;
case UNS_FAN1:
fprintf(save_file,"UNS_FAN1 ");
break;
case UNS_FAN2:
fprintf(save_file,"UNS_FAN2 ");
break;
case CAR1:
fprintf(save_file,"CAR1 ");
break;
case CDR1:
fprintf(save_file,"CDR1 ");
break;
case TESTNIL1:
fprintf(save_file,"TESTNIL1 ");
break;
case CONS1:
fprintf(save_file,"CONS1 ");
break;
}
}
/* The following function prints NIL, INT and BOOL forms names. */
static void
put_int(f,p)
FORM *f;
int p;
{
switch(p) {
case T:
fprintf(save_file,"True ");
break;
case F:
fprintf(save_file,"False ");
break;
case INT:
fprintf(save_file,"Int: %" PRIdPTR " ", (intptr_t)f);
break;
case NIL:
fprintf(save_file,"Nil ");
break;
}
}
/* The following function returns a form's ports number. */
static int
num_port(name)
int name;
{
int risp;
switch (name)
{
case ROOT:
risp=1;
break;
case TRIANGLE:
case NOT:
case LESS1:
case EQ1:
case NOTEQ1:
case MORE1:
case LEQ1:
case MEQ1:
case ADD1:
case SUB1:
case PROD1:
case DIV1:
case MOD1:
case CAR:
case CDR:
case TESTNIL:
case LAMBDAUNB:
case UNS_FAN1:
case UNS_FAN2:
risp=2;
break;
case FAN:
case APP:
case LAMBDA:
case IFELSE:
case AND:
case OR:
case LESS:
case EQ:
case NOTEQ:
case MORE:
case ADD:
case LEQ:
case MEQ:
case SUB:
case PROD:
case DIV:
case MOD:
case CONS:
case CAR1:
case CDR1:
case TESTNIL1:
case CONS1:
risp=3;
break;
}
return risp;
}
/* The following function saves a file index row */
static void
eindex(elem)
ELEM *elem;
{
fprintf(save_file,"%3d ",elem->num);
put_form(elem->node);
fprintf(save_file,"index: %2d",elem->node->index);
switch (elem->node->name)
{
case FAN:
case CAR1:
case CDR1:
case TESTNIL1:
fprintf(save_file," nlevel[1]: %2d",elem->node->nlevel[1]);
fprintf(save_file," nlevel[2]: %2d",elem->node->nlevel[2]);
break;
case TRIANGLE:
case UNS_FAN1:
case UNS_FAN2:
fprintf(save_file," nlevel[1]: %2d",elem->node->nlevel[1]);
break;
case LESS1:
case EQ1:
case NOTEQ1:
case MORE1:
case LEQ1:
case MEQ1:
case ADD1:
case SUB1:
case PROD1:
case DIV1:
case MOD1:
fprintf(save_file," value %-d",elem->node->num_safe);
break;
}
fprintf(save_file,"\n");
}