-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathppdecl.c
497 lines (397 loc) · 14 KB
/
ppdecl.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/****************************************************************/
/* */
/* Pretty-print declarations and program, procedure, and */
/* function headers. */
/* */
/* FILE: ppdecl.c */
/* */
/* Copyright (c) 1991 by Ronald Mak */
/* For instructional purposes only. No warranties. */
/* */
/****************************************************************/
#include <stdio.h>
#include "common.h"
#include "symtab.h"
#include "pprint.h"
/*--------------------------------------------------------------*/
/* Externals */
/*--------------------------------------------------------------*/
extern TYPE_STRUCT_PTR integer_typep, real_typep,
boolean_typep, char_typep;
extern char pprint_buffer[];
extern int left_margin;
extern int error_count;
/*--------------------------------------------------------------*/
/* Globals */
/*--------------------------------------------------------------*/
char string[MAX_PRINT_LINE_LENGTH]; /* buffer for literals */
BOOLEAN const_flag, type_flag, var_flag; /* TRUE if keywords
already printed */
/*--------------------------------------------------------------*/
/* analyze_routine_header Pretty-print a program, procedure, */
/* or function header: */
/* */
/* PROGRAM <id> (<parms>); */
/* PROCEDURE <id> (<parms>); */
/* FUNCTION <id> (<parms>) : <id> */
/*--------------------------------------------------------------*/
analyze_routine_header(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;
{
int save_left_margin; /* current left margin */
DEFN_KEY common_key; /* defn of parm sublist */
TYPE_STRUCT_PTR common_tp; /* type of parm sublist */
SYMTAB_NODE_PTR parm_idp = rtn_idp->defn.info.routine.parms;
if (error_count > 0) return;
const_flag = type_flag = var_flag = FALSE;
emit(" ");
flush();
indent();
switch (rtn_idp->defn.key) {
case PROG_DEFN: emit("PROGRAM "); break;
case PROC_DEFN: emit("PROCEDURE "); break;
case FUNC_DEFN: emit("FUNCTION "); break;
}
emit(rtn_idp->name);
/*
-- Print the formal parameters if there are any
-- and the routine was not previously forwarded.
*/
if ((parm_idp != NULL) &&
(! (BOOLEAN) rtn_idp->info)) {
BOOLEAN sublist_done;
emit(" (");
set_left_margin(save_left_margin);
/*
-- Loop to print the parameter sublists.
*/
do {
common_key = parm_idp->defn.key;
common_tp = parm_idp->typep;
if ((rtn_idp->defn.key != PROG_DEFN) &&
(common_key == VARPARM_DEFN)) emit("VAR ");
emit(parm_idp->name);
/*
-- Loop to print the parameters in a sublist.
*/
do {
parm_idp = parm_idp->next;
sublist_done = ((parm_idp == NULL) ||
(parm_idp->defn.key != common_key) ||
(parm_idp->typep != common_tp));
if (!sublist_done) {
emit(", ");
emit(parm_idp->name);
}
} while (!sublist_done);
if (rtn_idp->defn.key != PROG_DEFN) {
emit(" : ");
print_type(common_tp, FALSE);
}
if (parm_idp != NULL) {
emit(";");
flush();
indent();
}
} while (parm_idp != NULL);
emit(")");
if (rtn_idp->defn.key == FUNC_DEFN) {
emit(" : ");
print_type(rtn_idp->typep, FALSE);
}
reset_left_margin(save_left_margin);
}
emit(";");
flush();
/*
-- Print a forward declaration.
*/
if (rtn_idp->defn.info.routine.key == FORWARD) {
rtn_idp->info = (char *) TRUE;
advance_left_margin();
indent();
emit("FORWARD;");
flush();
retreat_left_margin();
}
else advance_left_margin();
}
/*--------------------------------------------------------------*/
/* declaration_keyword Print the keyword CONST, TYPE, or */
/* VAR in string if flag is FALSE. */
/* Each keyword is printed at most */
/* once per block. */
/*--------------------------------------------------------------*/
declaration_keyword(flag, string)
BOOLEAN *flag; /* TRUE if keyword already printed */
char *string; /* keyword */
{
if (! *flag) {
emit(" ");
flush();
indent();
emit(string);
flush();
*flag = TRUE;
}
}
/*--------------------------------------------------------------*/
/* analyze_const_defn Pretty-print a constant definition: */
/* */
/* CONST */
/* <id> = <literal>; */
/*--------------------------------------------------------------*/
analyze_const_defn(const_idp)
SYMTAB_NODE_PTR const_idp; /* constant id */
{
TYPE_STRUCT_PTR const_tp = const_idp->typep; /* constant type */
if (error_count > 0) return;
declaration_keyword(&const_flag, "CONST");
advance_left_margin();
indent();
emit(const_idp->name);
emit(" = ");
if (const_tp->form == ARRAY_FORM) { /* string */
emit("'");
emit(const_idp->defn.info.constant.value.stringp);
emit("'");
}
else if (const_tp == integer_typep) {
sprintf(string, "%d",
const_idp->defn.info.constant.value.integer);
emit(string);
}
else if (const_tp == real_typep) {
sprintf(string, "%g",
const_idp->defn.info.constant.value.real);
emit(string);
}
else if (const_tp == char_typep) {
sprintf(string, "'%c'",
const_idp->defn.info.constant.value.character);
emit(string);
}
emit(";");
flush();
retreat_left_margin();
}
/*--------------------------------------------------------------*/
/* analyze_type_defn Pretty-print a type definition: */
/* */
/* TYPE */
/* <id> = <type>; */
/*--------------------------------------------------------------*/
analyze_type_defn(type_idp)
SYMTAB_NODE_PTR type_idp; /* type id */
{
if (error_count > 0) return;
declaration_keyword(&type_flag, "TYPE");
advance_left_margin();
indent();
emit(type_idp->name);
emit(" = ");
print_type(type_idp->typep,
type_idp == type_idp->typep->type_idp);
emit(";");
flush();
retreat_left_margin();
}
/*--------------------------------------------------------------*/
/* print_type Pretty-print a type. */
/*--------------------------------------------------------------*/
print_type(tp, defn_flag)
TYPE_STRUCT_PTR tp; /* type */
BOOLEAN defn_flag; /* TRUE if named definition */
{
/*
-- Identifier type.
*/
if (!defn_flag && (tp->type_idp != NULL))
emit(tp->type_idp->name);
/*
-- Other type.
*/
else switch (tp->form) {
case ENUM_FORM: print_enum_type(tp); break;
case SUBRANGE_FORM: print_subrange_type(tp); break;
case ARRAY_FORM: print_array_type(tp); break;
case RECORD_FORM: print_record_type(tp); break;
}
}
/*--------------------------------------------------------------*/
/* print_enum_type Pretty-print an enumeration type: */
/* */
/* (<id1>, <id2>, ..., <idn>) */
/*--------------------------------------------------------------*/
print_enum_type(tp)
TYPE_STRUCT_PTR tp; /* type */
{
SYMTAB_NODE_PTR idp = tp->info.enumeration.const_idp;
emit("(");
/*
-- Loop to print the enumeration constant id list.
*/
while (idp != NULL) {
emit(idp->name);
idp = idp->next;
if (idp != NULL) emit(", ");
}
emit(")");
}
/*--------------------------------------------------------------*/
/* print_subrange_type Pretty-print a subrange type: */
/* */
/* (<const1>..<const2>) */
/*--------------------------------------------------------------*/
print_subrange_type(tp)
TYPE_STRUCT_PTR tp; /* type */
{
print_subrange_limit(tp->info.subrange.min,
tp->info.subrange.range_typep);
emit("..");
print_subrange_limit(tp->info.subrange.max,
tp->info.subrange.range_typep);
}
/*--------------------------------------------------------------*/
/* print_subrange_limit Pretty-print a subrange constant */
/* limit. */
/*--------------------------------------------------------------*/
print_subrange_limit(limit, range_tp)
int limit;
TYPE_STRUCT_PTR range_tp;
{
if (range_tp == integer_typep) {
sprintf(string, "%d", limit);
emit(string);
}
else if (range_tp == char_typep) {
sprintf(string, "'%c'", limit);
emit(string);
}
else if (range_tp->form == ENUM_FORM) {
SYMTAB_NODE_PTR idp = range_tp->info.enumeration.const_idp;
while (limit-- > 0) idp = idp->next;
emit(idp->name);
}
}
/*--------------------------------------------------------------*/
/* print_array_type Pretty-print an array type: */
/* */
/* ARRAY [<type>] OF */
/* ARRAY [<type>] OF <type> */
/*--------------------------------------------------------------*/
print_array_type(tp)
TYPE_STRUCT_PTR tp; /* type */
{
int save_left_margin; /* current left margin */
TYPE_STRUCT_PTR index_tp = tp->info.array.index_typep;
TYPE_STRUCT_PTR elmt_tp = tp->info.array.elmt_typep;
set_left_margin(save_left_margin);
emit("ARRAY [");
print_type(index_tp, FALSE);
emit("] OF ");
if (elmt_tp->type_idp != NULL) print_type(elmt_tp, FALSE);
else {
flush();
/*
-- Cascade multidimensional array definitions.
*/
advance_left_margin();
indent();
print_type(elmt_tp, FALSE);
}
reset_left_margin(save_left_margin);
}
/*--------------------------------------------------------------*/
/* print_record_type Pretty-print a record type: */
/* */
/* RECORD */
/* <field-list1> : <type>; */
/* <field-list2> : <type> */
/* END */
/*--------------------------------------------------------------*/
print_record_type(tp)
TYPE_STRUCT_PTR tp;
{
int save_left_margin; /* current left margin */
BOOLEAN sublist_done; /* TRUE iff done */
SYMTAB_NODE_PTR field_idp = tp->info.record.field_symtab;
TYPE_STRUCT_PTR common_tp; /* type of field sublist */
set_left_margin(save_left_margin);
emit("RECORD");
flush();
advance_left_margin();
/*
-- Loop to print field sublists.
*/
while (field_idp != NULL) {
indent();
emit(field_idp->name);
common_tp = field_idp->typep;
/*
-- Loop to print fields of a sublist.
*/
do {
field_idp = field_idp->next;
sublist_done = ((field_idp == NULL) ||
(field_idp->typep != common_tp));
if (!sublist_done) {
emit(", ");
emit(field_idp->name);
}
} while (!sublist_done);
emit(" : ");
print_type(common_tp, FALSE);
emit(";");
flush();
}
retreat_left_margin();
indent();
emit("END");
reset_left_margin(save_left_margin);
}
/*--------------------------------------------------------------*/
/* analyze_var_decl Pretty-print a variable declaration: */
/* */
/* VAR */
/* <id> : <type>; */
/*--------------------------------------------------------------*/
analyze_var_decl(var_idp)
SYMTAB_NODE_PTR var_idp; /* variable id */
{
TYPE_STRUCT_PTR common_tp; /* type of id sublist */
BOOLEAN sublist_done; /* TRUE iff sublist done */
static SYMTAB_NODE_PTR done_var_idp = NULL; /* id already
printed */
if (error_count > 0) return;
/*
-- If this variable is part of a sublist that has
-- already been printed, don't print it again.
*/
if (var_idp == done_var_idp) {
done_var_idp = var_idp->next;
return;
}
else done_var_idp = var_idp->next;
declaration_keyword(&var_flag, "VAR");
advance_left_margin();
indent();
emit(var_idp->name);
common_tp = var_idp->typep;
/*
-- Loop to print the variables in a sublist.
*/
do {
var_idp = var_idp->next;
sublist_done = (var_idp == NULL);
if (!sublist_done) {
emit(", ");
emit(var_idp->name);
}
} while (!sublist_done);
emit(" : ");
print_type(common_tp, FALSE);
emit(";");
flush();
retreat_left_margin();
}