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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
/* do not edit automatically generated by mc from SymbolKey. */
/* SymbolKey.mod binary tree operations for storing symbols.
Copyright (C) 2001-2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Modula-2 is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Modula-2; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
# if !defined (PROC_D)
# define PROC_D
typedef void (*PROC_t) (void);
typedef struct { PROC_t proc; } PROC;
# endif
# if !defined (FALSE)
# define FALSE (1==0)
# endif
#include <stddef.h>
# include "GStorage.h"
#if defined(__cplusplus)
# undef NULL
# define NULL 0
#endif
#define _SymbolKey_H
#define _SymbolKey_C
# include "GStorage.h"
# include "GStrIO.h"
# include "GNumberIO.h"
# include "GNameKey.h"
# include "GAssertion.h"
# include "GDebug.h"
# define SymbolKey_NulKey 0
typedef struct SymbolKey_IsSymbol_p SymbolKey_IsSymbol;
typedef struct SymbolKey_PerformOperation_p SymbolKey_PerformOperation;
typedef struct SymbolKey_Node_r SymbolKey_Node;
typedef SymbolKey_Node *SymbolKey_SymbolTree;
typedef unsigned int (*SymbolKey_IsSymbol_t) (unsigned int);
struct SymbolKey_IsSymbol_p { SymbolKey_IsSymbol_t proc; };
typedef void (*SymbolKey_PerformOperation_t) (unsigned int);
struct SymbolKey_PerformOperation_p { SymbolKey_PerformOperation_t proc; };
struct SymbolKey_Node_r {
NameKey_Name KeyName;
unsigned int KeySym;
SymbolKey_SymbolTree Left;
SymbolKey_SymbolTree Right;
};
extern "C" void SymbolKey_InitTree (SymbolKey_SymbolTree *t);
extern "C" void SymbolKey_KillTree (SymbolKey_SymbolTree *t);
/*
ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
*/
extern "C" unsigned int SymbolKey_GetSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey);
/*
ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
*/
extern "C" void SymbolKey_PutSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey, unsigned int SymKey);
/*
DelSymKey - deletes an entry in the binary tree.
NB in order for this to work we must ensure that the InitTree sets
both Left and Right to NIL.
*/
extern "C" void SymbolKey_DelSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey);
/*
IsEmptyTree - returns true if SymbolTree, t, is empty.
*/
extern "C" unsigned int SymbolKey_IsEmptyTree (SymbolKey_SymbolTree t);
/*
DoesTreeContainAny - returns true if SymbolTree, t, contains any
symbols which in turn return true when procedure,
P, is called with a symbol as its parameter.
The SymbolTree root is empty apart from the field,
Left, hence we need two procedures.
*/
extern "C" unsigned int SymbolKey_DoesTreeContainAny (SymbolKey_SymbolTree t, SymbolKey_IsSymbol P);
/*
ForeachNodeDo - for each node in SymbolTree, t, a procedure, P,
is called with the node symbol as its parameter.
The tree root node only contains a legal Left pointer,
therefore we need two procedures to examine this tree.
*/
extern "C" void SymbolKey_ForeachNodeDo (SymbolKey_SymbolTree t, SymbolKey_PerformOperation P);
/*
ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
*/
extern "C" unsigned int SymbolKey_ContainsSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey);
/*
NoOfNodes - returns the number of nodes in the tree t.
*/
extern "C" unsigned int SymbolKey_NoOfNodes (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition);
/*
ForeachNodeConditionDo - traverse the tree t and for any node which satisfied
condition call P.
*/
extern "C" void SymbolKey_ForeachNodeConditionDo (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition, SymbolKey_PerformOperation P);
/*
FindNodeParentInTree - find a node, child, in a binary tree, t, with name equal to n.
if an entry is found, parent is set to the node above child.
*/
static void FindNodeParentInTree (SymbolKey_SymbolTree t, NameKey_Name n, SymbolKey_SymbolTree *child, SymbolKey_SymbolTree *parent);
/*
SearchForAny - performs the search required for DoesTreeContainAny.
The root node always contains a nul data value,
therefore we must skip over it.
*/
static unsigned int SearchForAny (SymbolKey_SymbolTree t, SymbolKey_IsSymbol P);
/*
SearchAndDo - searches all the nodes in SymbolTree, t, and
calls procedure, P, with a node as its parameter.
It traverse the tree in order.
*/
static void SearchAndDo (SymbolKey_SymbolTree t, SymbolKey_PerformOperation P);
/*
CountNodes - wrapper for NoOfNodes.
*/
static unsigned int CountNodes (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition, unsigned int count);
/*
SearchConditional - wrapper for ForeachNodeConditionDo.
*/
static void SearchConditional (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition, SymbolKey_PerformOperation P);
/*
FindNodeParentInTree - find a node, child, in a binary tree, t, with name equal to n.
if an entry is found, parent is set to the node above child.
*/
static void FindNodeParentInTree (SymbolKey_SymbolTree t, NameKey_Name n, SymbolKey_SymbolTree *child, SymbolKey_SymbolTree *parent)
{
/* remember to skip the sentinal value and assign parent and child */
(*parent) = t;
if (t == NULL)
{
Debug_Halt ((const char *) "parameter t should never be NIL", 31, 240, (const char *) "../../gcc-read-write/gcc/m2/gm2-compiler/SymbolKey.mod", 54);
}
Assertion_Assert (t->Right == NULL);
(*child) = t->Left;
if ((*child) != NULL)
{
do {
if (n < (*child)->KeyName)
{
(*parent) = (*child);
(*child) = (*child)->Left;
}
else if (n > (*child)->KeyName)
{
/* avoid dangling else. */
(*parent) = (*child);
(*child) = (*child)->Right;
}
} while (! (((*child) == NULL) || (n == (*child)->KeyName)));
}
}
/*
SearchForAny - performs the search required for DoesTreeContainAny.
The root node always contains a nul data value,
therefore we must skip over it.
*/
static unsigned int SearchForAny (SymbolKey_SymbolTree t, SymbolKey_IsSymbol P)
{
if (t == NULL)
{
return FALSE;
}
else
{
return (((*P.proc) (t->KeySym)) || (SearchForAny (t->Left, P))) || (SearchForAny (t->Right, P));
}
/* static analysis guarentees a RETURN statement will be used before here. */
__builtin_unreachable ();
}
/*
SearchAndDo - searches all the nodes in SymbolTree, t, and
calls procedure, P, with a node as its parameter.
It traverse the tree in order.
*/
static void SearchAndDo (SymbolKey_SymbolTree t, SymbolKey_PerformOperation P)
{
if (t != NULL)
{
SearchAndDo (t->Right, P);
(*P.proc) (t->KeySym);
SearchAndDo (t->Left, P);
}
}
/*
CountNodes - wrapper for NoOfNodes.
*/
static unsigned int CountNodes (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition, unsigned int count)
{
if (t != NULL)
{
if ((*condition.proc) (t->KeySym))
{
count += 1;
}
count = CountNodes (t->Left, condition, count);
count = CountNodes (t->Right, condition, count);
}
return count;
/* static analysis guarentees a RETURN statement will be used before here. */
__builtin_unreachable ();
}
/*
SearchConditional - wrapper for ForeachNodeConditionDo.
*/
static void SearchConditional (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition, SymbolKey_PerformOperation P)
{
if (t != NULL)
{
SearchConditional (t->Right, condition, P);
if ((t->KeySym != 0) && ((*condition.proc) (t->KeySym)))
{
(*P.proc) (t->KeySym);
}
SearchConditional (t->Left, condition, P);
}
}
extern "C" void SymbolKey_InitTree (SymbolKey_SymbolTree *t)
{
Storage_ALLOCATE ((void **) &(*t), sizeof (SymbolKey_Node)); /* The value entity */
(*t)->Left = NULL;
(*t)->Right = NULL;
}
extern "C" void SymbolKey_KillTree (SymbolKey_SymbolTree *t)
{
/*
we used to get problems compiling KillTree below - so it was split
into the two procedures below.
PROCEDURE KillTree (VAR t: SymbolTree) ;
BEGIN
IF t#NIL
THEN
Kill(t) ; Would like to place Kill in here but the compiler
gives a type incompatible error... so i've split
the procedure into two. - Problem i think with
VAR t at the top?
t := NIL
END
END KillTree ;
PROCEDURE Kill (t: SymbolTree) ;
BEGIN
IF t#NIL
THEN
Kill(t^.Left) ;
Kill(t^.Right) ;
DISPOSE(t)
END
END Kill ;
*/
if ((*t) != NULL)
{
SymbolKey_KillTree (&(*t)->Left);
SymbolKey_KillTree (&(*t)->Right);
Storage_DEALLOCATE ((void **) &(*t), sizeof (SymbolKey_Node));
(*t) = NULL;
}
}
/*
ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
*/
extern "C" unsigned int SymbolKey_GetSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey)
{
SymbolKey_SymbolTree father;
SymbolKey_SymbolTree child;
FindNodeParentInTree (t, NameKey, &child, &father);
if (child == NULL)
{
return static_cast<unsigned int> (SymbolKey_NulKey);
}
else
{
return child->KeySym;
}
/* static analysis guarentees a RETURN statement will be used before here. */
__builtin_unreachable ();
}
/*
ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
*/
extern "C" void SymbolKey_PutSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey, unsigned int SymKey)
{
SymbolKey_SymbolTree father;
SymbolKey_SymbolTree child;
FindNodeParentInTree (t, NameKey, &child, &father);
if (child == NULL)
{
/* no child found, now is NameKey less than father or greater? */
if (father == t)
{
/* empty tree, add it to the left branch of t */
Storage_ALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
father->Left = child;
}
else
{
if (NameKey < father->KeyName)
{
Storage_ALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
father->Left = child;
}
else if (NameKey > father->KeyName)
{
/* avoid dangling else. */
Storage_ALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
father->Right = child;
}
}
child->Right = NULL;
child->Left = NULL;
child->KeySym = SymKey;
child->KeyName = NameKey;
}
else
{
Debug_Halt ((const char *) "symbol already stored", 21, 156, (const char *) "../../gcc-read-write/gcc/m2/gm2-compiler/SymbolKey.mod", 54);
}
}
/*
DelSymKey - deletes an entry in the binary tree.
NB in order for this to work we must ensure that the InitTree sets
both Left and Right to NIL.
*/
extern "C" void SymbolKey_DelSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey)
{
SymbolKey_SymbolTree i;
SymbolKey_SymbolTree child;
SymbolKey_SymbolTree father;
FindNodeParentInTree (t, NameKey, &child, &father); /* find father and child of the node */
if ((child != NULL) && (child->KeyName == NameKey))
{
/* Have found the node to be deleted */
if (father->Right == child)
{
/* most branch of child^.Left. */
if (child->Left != NULL)
{
/* Scan for Right most node of child^.Left */
i = child->Left;
while (i->Right != NULL)
{
i = i->Right;
}
i->Right = child->Right;
father->Right = child->Left;
}
else
{
/* (as in a single linked list) to child^.Right */
father->Right = child->Right;
}
Storage_DEALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
}
else
{
/* branch of child^.Right */
if (child->Right != NULL)
{
/* Scan for Left most node of child^.Right */
i = child->Right;
while (i->Left != NULL)
{
i = i->Left;
}
i->Left = child->Left;
father->Left = child->Right;
}
else
{
/* (as in a single linked list) to child^.Left. */
father->Left = child->Left;
}
Storage_DEALLOCATE ((void **) &child, sizeof (SymbolKey_Node));
}
}
else
{
Debug_Halt ((const char *) "trying to delete a symbol that is not in the tree - the compiler never expects this to occur", 92, 223, (const char *) "../../gcc-read-write/gcc/m2/gm2-compiler/SymbolKey.mod", 54);
}
}
/*
IsEmptyTree - returns true if SymbolTree, t, is empty.
*/
extern "C" unsigned int SymbolKey_IsEmptyTree (SymbolKey_SymbolTree t)
{
return t->Left == NULL;
/* static analysis guarentees a RETURN statement will be used before here. */
__builtin_unreachable ();
}
/*
DoesTreeContainAny - returns true if SymbolTree, t, contains any
symbols which in turn return true when procedure,
P, is called with a symbol as its parameter.
The SymbolTree root is empty apart from the field,
Left, hence we need two procedures.
*/
extern "C" unsigned int SymbolKey_DoesTreeContainAny (SymbolKey_SymbolTree t, SymbolKey_IsSymbol P)
{
return SearchForAny (t->Left, P);
/* static analysis guarentees a RETURN statement will be used before here. */
__builtin_unreachable ();
}
/*
ForeachNodeDo - for each node in SymbolTree, t, a procedure, P,
is called with the node symbol as its parameter.
The tree root node only contains a legal Left pointer,
therefore we need two procedures to examine this tree.
*/
extern "C" void SymbolKey_ForeachNodeDo (SymbolKey_SymbolTree t, SymbolKey_PerformOperation P)
{
SearchAndDo (t->Left, P);
}
/*
ContainsSymKey - return TRUE if tree, t, contains an entry for, NameKey.
*/
extern "C" unsigned int SymbolKey_ContainsSymKey (SymbolKey_SymbolTree t, NameKey_Name NameKey)
{
SymbolKey_SymbolTree father;
SymbolKey_SymbolTree child;
FindNodeParentInTree (t, NameKey, &child, &father);
return child != NULL;
/* static analysis guarentees a RETURN statement will be used before here. */
__builtin_unreachable ();
}
/*
NoOfNodes - returns the number of nodes in the tree t.
*/
extern "C" unsigned int SymbolKey_NoOfNodes (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition)
{
return CountNodes (t->Left, condition, 0);
/* static analysis guarentees a RETURN statement will be used before here. */
__builtin_unreachable ();
}
/*
ForeachNodeConditionDo - traverse the tree t and for any node which satisfied
condition call P.
*/
extern "C" void SymbolKey_ForeachNodeConditionDo (SymbolKey_SymbolTree t, SymbolKey_IsSymbol condition, SymbolKey_PerformOperation P)
{
if (t != NULL)
{
Assertion_Assert (t->Right == NULL);
SearchConditional (t->Left, condition, P);
}
}
extern "C" void _M2_SymbolKey_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
{
}
extern "C" void _M2_SymbolKey_finish (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
{
}
|