-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathpretty_scope.c
More file actions
372 lines (308 loc) · 9.68 KB
/
pretty_scope.c
File metadata and controls
372 lines (308 loc) · 9.68 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
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
/** \file pretty_scope.c
* split out of exppp.c 9/21/13
*/
#include "pp.h"
#include "exppp.h"
#include "pretty_expr.h"
#include "pretty_rule.h"
#include "pretty_type.h"
#include "pretty_func.h"
#include "pretty_entity.h"
#include "pretty_proc.h"
#include "pretty_scope.h"
/** add items from s to list alphabetically */
void SCOPEadd_inorder( Linked_List list, Scope s ) {
Link k = 0;
LISTdo_links( list, link )
if( 0 > strcmp(
SCOPEget_name( s ),
SCOPEget_name( ( Type )( link->data ) ) ) ) {
k = link;
break;
} LISTod
LISTadd_before( list, k, s );
}
/** like SCOPEadd_inorder, but for Variables */
void SCOPEaddvars_inorder( Linked_List list, Variable v ) {
Link k = 0;
LISTdo_links( list, link )
if( 0 > strcmp( v->name->symbol.name, ( ( Variable ) link->data )->name->symbol.name ) ) {
k = link;
break;
} LISTod
LISTadd_before( list, k, v );
}
/** print the rules in a scope */
void SCOPErules_out( Scope s, int level ) {
Rule r;
DictionaryEntry de;
if( exppp_alphabetize == false ) {
DICTdo_type_init( s->symbol_table, &de, OBJ_RULE );
while( 0 != ( r = ( Rule )DICTdo( &de ) ) ) {
RULE_out( r, level );
}
} else {
Linked_List alpha = LISTcreate();
DICTdo_type_init( s->symbol_table, &de, OBJ_RULE );
while( 0 != ( r = ( Rule )DICTdo( &de ) ) ) {
SCOPEadd_inorder( alpha, r );
}
LISTdo( alpha, ru, Rule ) {
RULE_out( ru, level );
} LISTod
LISTfree( alpha );
}
}
/** print the functions in a scope */
void SCOPEfuncs_out( Scope s, int level ) {
Function f;
DictionaryEntry de;
if( exppp_alphabetize == false ) {
DICTdo_type_init( s->symbol_table, &de, OBJ_FUNCTION );
while( 0 != ( f = ( Function )DICTdo( &de ) ) ) {
FUNC_out( f, level );
}
} else {
Linked_List alpha = LISTcreate();
DICTdo_type_init( s->symbol_table, &de, OBJ_FUNCTION );
while( 0 != ( f = ( Function )DICTdo( &de ) ) ) {
SCOPEadd_inorder( alpha, f );
}
LISTdo( alpha, fun, Function ) {
FUNC_out( fun, level );
} LISTod
LISTfree( alpha );
}
}
/* print the procs in a scope */
void SCOPEprocs_out( Scope s, int level ) {
Procedure p;
DictionaryEntry de;
if( exppp_alphabetize == false ) {
DICTdo_type_init( s->symbol_table, &de, OBJ_PROCEDURE );
while( 0 != ( p = ( Procedure )DICTdo( &de ) ) ) {
PROC_out( p, level );
}
} else {
Linked_List alpha = LISTcreate();
DICTdo_type_init( s->symbol_table, &de, OBJ_PROCEDURE );
while( 0 != ( p = ( Procedure )DICTdo( &de ) ) ) {
SCOPEadd_inorder( alpha, p );
}
LISTdo( alpha, pr, Procedure ) {
PROC_out( pr, level );
} LISTod
LISTfree( alpha );
}
}
/* output order for DIS & IS schemas:
* CONSTANT
* TYPE
* ENTITY
* RULE
* FUNCTION
* PROCEDURE
*
* Within each of those groups, declarations must be sorted alphabetically.
*/
/* print the algorithms in a scope */
void SCOPEalgs_out( Scope s, int level ) {
/* Supplementary Directivies 2.1.1 requires rules to be separated */
/* might as well separate funcs and procs, too */
SCOPErules_out( s, level );
SCOPEfuncs_out( s, level );
SCOPEprocs_out( s, level );
}
/** output one const - used in SCOPEconsts_out, below */
void SCOPEconst_out( Variable v, int level, size_t max_indent ) {
size_t old_indent2;
/* print attribute name */
raw( "%*s%-*s :", level + 2, "",
max_indent, v->name->symbol.name );
/* print attribute type */
if( VARget_optional( v ) ) {
wrap( " OPTIONAL" );
}
/* let type definition stick out a bit to the left if it's on a new line */
old_indent2 = indent2;
if( indent2 > 4 ) {
indent2 -= 4;
}
TYPE_head_out( v->type, NOLEVEL );
indent2 = old_indent2;
if( v->initializer ) {
int old_ll = exppp_linelength; /* so exppp_linelength can be restored */
raw( " :=" );
/* let '[' on first line of initializer stick out so strings are aligned */
raw( "\n%*s", indent2 - 2, "" );
if( exppp_aggressively_wrap_consts ) {
/* causes wrap() to always begin new line */
exppp_linelength = indent2;
}
EXPR_out( v->initializer, 0 );
exppp_linelength = old_ll;
}
raw( ";\n" );
}
/** output all consts in this scope */
void SCOPEconsts_out( Scope s, int level ) {
Variable v;
DictionaryEntry de;
size_t max_indent = 0;
Dictionary d = s->symbol_table;
/* checks length of constant names */
DICTdo_type_init( d, &de, OBJ_VARIABLE );
while( 0 != ( v = ( Variable )DICTdo( &de ) ) ) {
if( !v->flags.constant ) {
continue;
}
if( strlen( v->name->symbol.name ) > max_indent ) {
max_indent = strlen( v->name->symbol.name );
}
}
if( !max_indent ) {
return;
}
first_newline();
raw( "%*sCONSTANT\n", level, "" );
/* if max_indent is too big, wrap() won't insert any newlines
* fiddled with this until it looked ok on 242 arm
*/
if( ( max_indent + 20 ) > ( size_t ) exppp_linelength / 2 ) {
max_indent = ( size_t ) exppp_linelength / 3;
}
indent2 = level + max_indent + strlen( ": ab" ) + exppp_continuation_indent;
if( !exppp_alphabetize ) {
DICTdo_type_init( d, &de, OBJ_VARIABLE );
while( 0 != ( v = ( Variable )DICTdo( &de ) ) ) {
if( !v->flags.constant ) {
continue;
}
SCOPEconst_out( v, level, max_indent );
}
} else {
Linked_List alpha = LISTcreate();
DICTdo_type_init( d, &de, OBJ_VARIABLE );
while( 0 != ( v = ( Variable )DICTdo( &de ) ) ) {
if( !v->flags.constant ) {
continue;
}
SCOPEaddvars_inorder( alpha, v );
}
LISTdo( alpha, cnst, Variable ) {
SCOPEconst_out( cnst, level, max_indent );
} LISTod
LISTfree( alpha );
}
raw( "%*sEND_CONSTANT;\n", level, "" );
}
/** insert variable v into list, keeping the list ordered by ascending v->offset */
void SCOPElocals_order( Linked_List list, Variable v ) {
LISTdo_links( list, link ) {
if( v->offset < ( (Variable) link->data )->offset ) {
LISTadd_before( list, link, v );
return;
}
} LISTod
LISTadd_last( list, v );
}
void SCOPElocals_out( Scope s, int level ) {
Variable v;
DictionaryEntry de;
Linked_List orderedLocals = 0; /**< this list is used to order the vars the same way they were in the file */
size_t max_indent = 0;
Dictionary d = s->symbol_table;
DICTdo_type_init( d, &de, OBJ_VARIABLE );
while( 0 != ( v = ( Variable )DICTdo( &de ) ) ) {
if( v->flags.constant ) {
continue;
}
if( v->flags.parameter ) {
continue;
}
if( strlen( v->name->symbol.name ) > max_indent ) {
max_indent = strlen( v->name->symbol.name );
}
}
if( !max_indent ) {
return;
}
first_newline();
raw( "%*sLOCAL\n", level, "" );
indent2 = level + max_indent + strlen( ": " ) + exppp_continuation_indent;
DICTdo_type_init( d, &de, OBJ_VARIABLE );
while( 0 != ( v = ( Variable )DICTdo( &de ) ) ) {
if( v->flags.constant ) {
continue;
}
if( v->flags.parameter ) {
continue;
}
if( !orderedLocals ) {
orderedLocals = LISTcreate();
LISTadd_first( orderedLocals, v );
} else {
/* sort by v->offset */
SCOPElocals_order( orderedLocals, v );
}
}
LISTdo( orderedLocals, var, Variable ) {
/* print attribute name */
raw( "%*s%-*s :", level + exppp_nesting_indent, "",
max_indent, var->name->symbol.name );
/* print attribute type */
if( VARget_optional( var ) ) {
wrap( " OPTIONAL" );
}
TYPE_head_out( var->type, NOLEVEL );
if( var->initializer ) {
wrap( " := " );
EXPR_out( var->initializer, 0 );
}
raw( ";\n" );
} LISTod
LISTfree( orderedLocals );
raw( "%*sEND_LOCAL;\n", level, "" );
}
/** print all entities in a scope */
void SCOPEentities_out( Scope s, int level ) {
Entity e;
DictionaryEntry de;
if( exppp_alphabetize == false ) {
DICTdo_type_init( s->symbol_table, &de, OBJ_ENTITY );
while( 0 != ( e = ( Entity )DICTdo( &de ) ) ) {
ENTITY_out( e, level );
}
} else {
Linked_List alpha = LISTcreate();
DICTdo_type_init( s->symbol_table, &de, OBJ_ENTITY );
while( 0 != ( e = ( Entity )DICTdo( &de ) ) ) {
SCOPEadd_inorder( alpha, e );
}
LISTdo( alpha, en, Entity ) {
ENTITY_out( en, level );
} LISTod
LISTfree( alpha );
}
}
/** print all types in a scope */
void SCOPEtypes_out( Scope s, int level ) {
DictionaryEntry de;
Type t;
if( exppp_alphabetize == false ) {
DICTdo_type_init( s->symbol_table, &de, OBJ_TYPE );
while( 0 != ( t = ( Type )DICTdo( &de ) ) ) {
TYPE_out( t, level );
}
} else {
Linked_List alpha = LISTcreate();
DICTdo_type_init( s->symbol_table, &de, OBJ_TYPE );
while( 0 != ( t = ( Type )DICTdo( &de ) ) ) {
SCOPEadd_inorder( alpha, t );
}
LISTdo( alpha, ty, Type ) {
TYPE_out( ty, level );
} LISTod
LISTfree( alpha );
}
}