show PC of labels
[xorcyst.git] / RCS / symtab.c,v
blob64a309f83aef93bca8815e08abeafd282423701e
1 head    1.7;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
8 1.7
9 date    2007.11.11.22.35.22;    author khansen; state Exp;
10 branches;
11 next    1.6;
13 1.6
14 date    2007.08.09.20.33.57;    author khansen; state Exp;
15 branches;
16 next    1.5;
18 1.5
19 date    2007.08.08.22.40.43;    author khansen; state Exp;
20 branches;
21 next    1.4;
23 1.4
24 date    2007.07.22.13.33.26;    author khansen; state Exp;
25 branches;
26 next    1.3;
28 1.3
29 date    2004.12.14.01.50.05;    author kenth;   state Exp;
30 branches;
31 next    1.2;
33 1.2
34 date    2004.12.06.04.52.53;    author kenth;   state Exp;
35 branches;
36 next    1.1;
38 1.1
39 date    2004.06.30.07.55.57;    author kenth;   state Exp;
40 branches;
41 next    ;
44 desc
48 1.7
49 log
50 @compile on mac
52 text
53 @/*
54  * $Id: symtab.c,v 1.6 2007/08/09 20:33:57 khansen Exp khansen $
55  * $Log: symtab.c,v $
56  * Revision 1.6  2007/08/09 20:33:57  khansen
57  * progress
58  *
59  * Revision 1.5  2007/08/08 22:40:43  khansen
60  * recursive symbol table lookup
61  *
62  * Revision 1.4  2007/07/22 13:33:26  khansen
63  * convert tabs to whitespaces
64  *
65  * Revision 1.3  2004/12/14 01:50:05  kenth
66  * xorcyst 1.3.0
67  *
68  * Revision 1.2  2004/12/06 04:52:53  kenth
69  * xorcyst 1.1.0
70  *
71  * Revision 1.1  2004/06/30 07:55:57  kenth
72  * Initial revision
73  *
74  */
76 /**
77  *    (C) 2004 Kent Hansen
78  *
79  *    The XORcyst is free software; you can redistribute it and/or modify
80  *    it under the terms of the GNU General Public License as published by
81  *    the Free Software Foundation; either version 2 of the License, or
82  *    (at your option) any later version.
83  *
84  *    The XORcyst is distributed in the hope that it will be useful,
85  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
86  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
87  *    GNU General Public License for more details.
88  *
89  *    You should have received a copy of the GNU General Public License
90  *    along with The XORcyst; if not, write to the Free Software
91  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
92  */
94 /**
95  * This file contains functions for symbol table management.
96  */
98 #include "symtab.h"
99 #include <stdlib.h>
100 #include <string.h>
101 #include <stdio.h>
103 #define SAFE_FREE(a) if (a) { free(a); a = NULL; }
105 /* Stack of symbol tables */
106 static symtab *symtab_stack[32] = { NULL };
107 static int stack_index = 0;
109 static void symtab_entry_finalize(symtab_entry *);
111 /*---------------------------------------------------------------------------*/
114  * Gets the entry in the tree with minimum key.
115  */
116 static symtab_entry *binary_min(symtab_entry *p)
118     if (p == NULL) { return NULL; }
119     while (p->left != NULL) {
120         p = p->left;
121     }
122     return p;
125 #if 0
127  * Gets the entry in the tree with maximum key.
128  */
129 static symtab_entry *binary_max(symtab_entry *p)
131     if (p == NULL) { return NULL; }
132     while (p->right != NULL) {
133         p = p->right;
134     }
135     return p;
137 #endif
140  * Gets the successor of an entry.
141  */
142 static symtab_entry *binary_succ(symtab_entry *p)
144     symtab_entry *e;
145     if (p == NULL) { return NULL; }
146     if (p->right != NULL) { return binary_min(p->right); }
147     e = p->parent;
148     while ((e != NULL) && (p == e->right)) {
149         p = e;
150         e = e->parent;
151     }
152     return e;
155 #if 0
157  * Gets the predecessor of an entry.
158  */
159 static symtab_entry *binary_pred(symtab_entry *p)
161     symtab_entry *e;
162     if (p == NULL) { return NULL; }
163     if (p->left != NULL) { return binary_max(p->left); }
164     e = p->parent;
165     while ((e != NULL) && (p == e->left)) {
166         p = e;
167         e = e->parent;
168     }
169     return e;
171 #endif
174  * Inserts a new entry in a binary tree.
175  * It's implemented recursively although that's a bad thing.
176  * @@param p The root of the tree
177  * @@param e A new entry to be inserted
178  */
179 static void binary_insert(symtab_entry *p, symtab_entry *e)
181     int r = strcmp(p->id, e->id);
182     if (r < 0) {
183         /* Follow left branch */
184         if (p->left == NULL) {
185             p->left = e;    /* Entry inserted successfully */
186             e->parent = p;
187         }
188         else {
189             binary_insert(p->left, e);   /* Call recursively */
190         }
191     }
192     else if (r > 0) {
193         /* Follow right branch */
194         if (p->right == NULL) {
195             p->right = e;   /* Entry inserted successfully */
196             e->parent = p;
197         }
198         else {
199             binary_insert(p->right, e);  /* Call recursively */
200         }
201     }
202     else {
203         /* Symbol already present */
204     }
208  * Deletes an entry from a binary tree.
209  * @@param p Root node
210  * @@param z Entry to delete
211  */
212 static void binary_delete(symtab *st, symtab_entry *z)
214     symtab_entry *y;
215     symtab_entry *x;
216     symtab_entry *p;
217     if ((st == NULL) || (z == NULL)) { return; }
219     if ((z->left == NULL) || (z->right == NULL)) {
220         y = z;
221     }
222     else {
223         y = binary_succ(z);
224     }
226     if (y->left != NULL) {
227         x = y->left;
228     }
229     else {
230         x = y->right;
231     }
233     p = y->parent;
234     if (x != NULL) {
235         x->parent = p;
236     }
238     if (p == NULL) {
239         st->root = x;
240     }
241     else if (y == p->left) {
242         p->left = x;
243     }
244     else {
245         p->right = x;
246     }
248     if (y != z) {
249         symtab_finalize(z->symtab);
250         SAFE_FREE(z->id);
251         z->id = (char *)malloc(strlen(y->id)+1);
252         if (z->id != NULL) {
253             strcpy(z->id, y->id);
254         }
255         z->type = y->type;
256         z->flags = y->flags;
257         z->ref_count = y->ref_count;
258         z->def = y->def;
259         z->symtab = y->symtab;
260         z->tag = y->tag;
261     }
263     if (y != NULL) {
264         symtab_entry_finalize(y);
265     }
269  * Searches for an entry in a binary tree.
270  * @@param p Root node
271  * @@param id Identifier (search key)
272  */
273 static symtab_entry *binary_search(symtab_entry *p, const char *id)
275     int r;
276     while ((p != NULL) && ((r = strcmp(p->id, id)) != 0)) {
277         p = (r < 0) ? p->left : p->right;
278     }
279     return p;
282 /*---------------------------------------------------------------------------*/
284 static symtab_entry *lookup(symtab *st, const char *id, int recurse)
286     symtab_entry *e;
287     do {
288         e = binary_search(st->root, id);
289         if (e != NULL) { return e; }
290         st = st->parent;
291     } while (recurse && st);
292     return NULL;
296  * Looks up the specified id in the current symbol table.
297  */
298 symtab_entry *symtab_lookup(const char *id)
300     return lookup(symtab_tos(), id, 0);
304  * Looks up the specified id in the current symbol table.
305  */
306 symtab_entry *symtab_lookup_recursive(const char *id)
308     return lookup(symtab_tos(), id, 1);
312  * Looks up the specified id in the global symbol table.
313  */
314 symtab_entry *symtab_global_lookup(const char *id)
316     return lookup(symtab_stack[0], id, 0);
320  * Enters something into a symbol table.
321  * @@param id Identifier
322  * @@param type Type of symbol (*_SYMBOL)
323  * @@param def External definition (may be <code>NULL</code>)
324  * @@param flags Initial flags
325  */
326 symtab_entry *symtab_enter(const char *id, symbol_type type, astnode *def, int flags)
328     symtab *st = symtab_tos();
329     /* See if this id already exists */
330     symtab_entry *e = symtab_lookup(id);
331     if (e != NULL) {
332         /* Duplicate symbol. */
333 //      printf("error: symtab_enter(): `%s' already exists\n", id);
334         return NULL;
335     }
336 //  printf("symtab_enter(): '%s' @@ %.8X\n", id, st);
337     /* Allocate new entry */
338     e = (symtab_entry *)malloc(sizeof(symtab_entry));
339     if (e != NULL) {
340         /* Set its fields */
341         e->id = (char *)malloc(strlen(id)+1);
342         if (e->id != NULL) {
343             strcpy(e->id, id);
344         }
345         e->type = type;
346         e->flags = flags;
347         e->ref_count = 0;
348         e->def = def;
349         /* Zap! */
350         e->struc.size = NULL;
351         e->struc.fields = NULL;
352         e->field.offset = NULL;
353         e->field.size = NULL;
354         e->left = NULL;
355         e->right = NULL;
356         e->parent = NULL;
357         e->symtab = NULL;
358         /* Put it into symbol table */
359         if (st->root == NULL) {
360             st->root = e;   /* This is the root element */
361         }
362         else {
363         /* Insert entry in binary tree */
364             binary_insert(st->root, e);
365         }
366     }
367     /* Return the newly created symbol table entry */
368     return e;
371 /*---------------------------------------------------------------------------*/
374  * Finalizes a single symbol table entry.
375  */
376 static void symtab_entry_finalize(symtab_entry *e)
378     ordered_field_list *list;
379     ordered_field_list *next;
380 //  printf("symtab_finalize(): `%s'\n", e->id);
381     /* Special finalizing */
382     switch (e->type) {
383         case LABEL_SYMBOL:
384         break;
386         case CONSTANT_SYMBOL:
387         astnode_finalize(e->def);
388         break;
390         case MACRO_SYMBOL:
391         astnode_finalize(e->def);
392         break;
394         case STRUC_SYMBOL:
395         case UNION_SYMBOL:
396         case RECORD_SYMBOL:
397         /* Free list of in-order field identifiers */
398         for (list = e->struc.fields; list != NULL; list = next) {
399             next = list->next;
400             free(list);
401         }
402         symtab_finalize(e->symtab);
403         astnode_finalize(e->struc.size);
404         break;
406         case ENUM_SYMBOL:
407         symtab_finalize(e->symtab);
408         break;
410         case VAR_SYMBOL:
411         astnode_finalize(e->def);
412         astnode_finalize(e->field.offset);
413         astnode_finalize(e->field.size);
414         break;
416         case PROC_SYMBOL:
417         break;
419         default:
420         /* Nada */
421         break;
422     }
423     /* Common finalizing */
424     SAFE_FREE(e->id);
425     SAFE_FREE(e);
429  * Finalizes a symbol table entry recursively,
430  * by first finalizing its children before finalizing itself.
431  */
432 static void symtab_entry_finalize_recursive(symtab_entry *e)
434     if (e == NULL) return;
435     symtab_entry_finalize_recursive(e->left);
436     symtab_entry_finalize_recursive(e->right);
437     symtab_entry_finalize(e);
441  * Finalizes a symbol table.
442  * @@param st The symbol table to finalize
443  */
444 void symtab_finalize(symtab *st)
446     if (st == NULL) return;
447     symtab_entry_finalize_recursive(st->root);
448     SAFE_FREE(st);
451 /*---------------------------------------------------------------------------*/
454  * Gets top of symbol table stack.
455  */
456 symtab *symtab_tos()
458     if (stack_index == 0) { return NULL; }
459     return symtab_stack[stack_index-1];
463  * Creates a symbol table and pushes it on the symbol table stack.
464  */
465 symtab *symtab_create()
467     symtab *st = (symtab *)malloc(sizeof(symtab));
468     if (st != NULL) {
469         st->root = NULL;
470         st->parent = symtab_tos();
471         symtab_push(st);
472     }
473     return st;
477  * Pushes a symbol table onto the stack.
478  */
479 void symtab_push(symtab *st)
481     symtab_stack[stack_index++] = st;
485  * Pops a symbol table from the stack.
486  */
487 symtab *symtab_pop()
489     return symtab_stack[--stack_index];
493  * Removes an entry from the symbol table.
494  * The memory associated with the entry is freed.
495  * @@param id Identifier of the entry to remove
496  */
497 void symtab_remove(const char *id)
499     symtab *st = symtab_tos();
500     symtab_entry * e = symtab_lookup(id);
501     if (e == NULL) { return; }  /* Nothing to remove */
502     binary_delete(st, e);
505 /*---------------------------------------------------------------------------*/
508  * Gets the number of entries of the given type.
509  */
510 int symtab_type_count(symbol_type type)
512     symtab *st = symtab_tos();
513     int count = 0;
514     symtab_entry *e = binary_min(st->root);
515     while (e != NULL) {
516         if ((type == ANY_SYMBOL) || (e->type == type)) {
517             count++;
518         }
519         e = binary_succ(e);
520     }
521     return count;
525  * Removes all entries of a given type from the symbol table.
526  * The memory associated with the entries is freed.
527  * @@param type Type of symbols to remove
528  */
529 void symtab_remove_by_type(symbol_type type)
531     int i;
532     /* Declare array to hold identifiers */
533     symbol_ident_list list;
534     /* List the entry identifiers */
535     int count = symtab_list_type(type, &list);
536     /* Remove the entries one by one */
537     for (i=0; i<count; i++) {
538         symtab_remove(list.idents[i]);
539     }
540     /* Finalize the array of identifiers */
541     symtab_list_finalize(&list);
544 /*---------------------------------------------------------------------------*/
546 static void indent(int n)
548     int i;
549     for (i=0; i<n; i++) {
550         printf(" ");
551     }
556  */
557 static void symtab_entry_print_recursive(symtab_entry *e, int level)
559     if (e == NULL) { return; }
560     //indent(level*2);
561     symtab_entry_print_recursive(e->left, level+1);
562     printf("%s\n", e->id);
563     symtab_entry_print_recursive(e->right, level+1);
568  */
569 void symtab_print()
571     symtab *st = symtab_tos();
572     symtab_entry_print_recursive(st->root, 0);
575 /*---------------------------------------------------------------------------*/
578  * Gets the number of entries in the symbol table.
579  */
580 int symtab_size()
582     return symtab_type_count(ANY_SYMBOL);
586  * Lists the entries in a symbol table that are of a certain type.
587  * @@param type The symbol type (*_SYMBOL)
588  * @@param list List which will receive the array of identifiers
589  */
590 int symtab_list_type(symbol_type type, symbol_ident_list *list)
592     symtab *st = symtab_tos();
593     int i = 0;
594     list->size = symtab_type_count(type);
595     list->idents = (char **)malloc(list->size * sizeof(char *) );
596     if (list->idents != NULL) {
597         symtab_entry *e = binary_min(st->root);
598         while (e != NULL) {
599             if ((type == ANY_SYMBOL) || (e->type == type)) {
600                 /* Add to list */
601                 list->idents[i] = (char *)malloc(strlen(e->id)+1);
602                 if (list->idents[i] != NULL) {
603                     strcpy(list->idents[i], e->id);
604                     i++;
605                 }
606             }
607             e = binary_succ(e);
608         }
609     }
610     /* Return the number of entries listed */
611     return i;
615  * Lists all identifiers in the symbol table.
616  */
617 int symtab_list(symbol_ident_list *list)
619     return symtab_list_type(ANY_SYMBOL, list);
623  * Lists all identifiers in the symbol table which has ONE OR MORE of the
624  * given flags set.
625  * If flag is zero it is ignored.
626  */
627 int symtab_list_flag(int flag, symbol_ident_list *list)
629     // TODO
630     return 0;
634  * Lists all identifiers in the symbol table of a given type which has
635  * ONE OR MORE of the given flags set.
636  * If flag is zero it is ignored.
637  */
638 int symtab_list_type_flag(symbol_type type, int flag, symbol_ident_list *list)
640     // TODO
641     return 0;
645  * Finalizes a list of identifiers.
646  */
647 void symtab_list_finalize(symbol_ident_list *list)
649     int i;
650     for (i=0; i<list->size; i++) {
651         SAFE_FREE(list->idents[i]);
652     }
653     SAFE_FREE(list->idents);
660 @progress
662 text
663 @d2 1
664 a2 1
665  * $Id: symtab.c,v 1.5 2007/08/08 22:40:43 khansen Exp khansen $
666 d4 3
667 d73 1
668 d85 1
669 d103 1
670 d119 1
676 @recursive symbol table lookup
678 text
679 @d2 1
680 a2 1
681  * $Id: symtab.c,v 1.4 2007/07/22 13:33:26 khansen Exp khansen $
682 d4 3
683 d225 1
684 a225 1
685 static symtab_entry *lookup(symtab *st, const char *id)
686 d228 1
687 a228 1
688     while (st != NULL) {
689 d232 1
690 a232 1
691     }
692 d241 9
693 a249 1
694     return lookup(symtab_tos(), id);
695 d257 1
696 a257 1
697     return lookup(symtab_stack[0], id);
703 @convert tabs to whitespaces
705 text
706 @d2 1
707 a2 1
708  * $Id: symtab.c,v 1.3 2004/12/14 01:50:05 kenth Exp $
709 d4 3
710 d225 1
711 a225 1
712 //  while (st != NULL) {
713 d228 2
714 a229 2
715 //      st = st->parent;
716 //  }
717 d315 1
718 a315 1
719         
720 d348 1
721 a348 1
722         
723 d400 2
724 a401 2
725         st->parent = NULL; // symtab_tos();
726         symtab_push(st);    /* NB! */
732 @xorcyst 1.3.0
734 text
735 @d2 1
736 a2 1
737  * $Id: symtab.c,v 1.2 2004/12/06 04:52:53 kenth Exp kenth $
738 d4 3
739 d57 5
740 a61 5
741         if (p == NULL) { return NULL; }
742         while (p->left != NULL) {
743                 p = p->left;
744         }
745         return p;
746 d69 5
747 a73 5
748         if (p == NULL) { return NULL; }
749         while (p->right != NULL) {
750                 p = p->right;
751         }
752         return p;
753 d81 9
754 a89 9
755         symtab_entry *e;
756         if (p == NULL) { return NULL; }
757         if (p->right != NULL) { return binary_min(p->right); }
758         e = p->parent;
759         while ((e != NULL) && (p == e->right)) {
760                 p = e;
761                 e = e->parent;
762         }
763         return e;
764 d97 9
765 a105 9
766         symtab_entry *e;
767         if (p == NULL) { return NULL; }
768         if (p->left != NULL) { return binary_max(p->left); }
769         e = p->parent;
770         while ((e != NULL) && (p == e->left)) {
771                 p = e;
772                 e = e->parent;
773         }
774         return e;
775 d116 24
776 a139 24
777         int r = strcmp(p->id, e->id);
778         if (r < 0) {
779                 /* Follow left branch */
780                 if (p->left == NULL) {
781                         p->left = e;    /* Entry inserted successfully */
782                         e->parent = p;
783                 }
784                 else {
785                         binary_insert(p->left, e);   /* Call recursively */
786                 }
787         }
788         else if (r > 0) {
789                 /* Follow right branch */
790                 if (p->right == NULL) {
791                         p->right = e;   /* Entry inserted successfully */
792                         e->parent = p;
793                 }
794                 else {
795                         binary_insert(p->right, e);  /* Call recursively */
796                 }
797         }
798         else {
799                 /* Symbol already present */
800         }
801 d149 52
802 a200 52
803         symtab_entry *y;
804         symtab_entry *x;
805         symtab_entry *p;
806         if ((st == NULL) || (z == NULL)) { return; }
808         if ((z->left == NULL) || (z->right == NULL)) {
809                 y = z;
810         }
811         else {
812                 y = binary_succ(z);
813         }
815         if (y->left != NULL) {
816                 x = y->left;
817         }
818         else {
819                 x = y->right;
820         }
822         p = y->parent;
823         if (x != NULL) {
824                 x->parent = p;
825         }
827         if (p == NULL) {
828                 st->root = x;
829         }
830         else if (y == p->left) {
831                 p->left = x;
832         }
833         else {
834                 p->right = x;
835         }
837         if (y != z) {
838                 symtab_finalize(z->symtab);
839                 SAFE_FREE(z->id);
840                 z->id = (char *)malloc(strlen(y->id)+1);
841                 if (z->id != NULL) {
842                         strcpy(z->id, y->id);
843                 }
844                 z->type = y->type;
845                 z->flags = y->flags;
846                 z->ref_count = y->ref_count;
847                 z->def = y->def;
848                 z->symtab = y->symtab;
849                 z->tag = y->tag;
850         }
852         if (y != NULL) {
853                 symtab_entry_finalize(y);
854         }
855 d210 5
856 a214 5
857         int r;
858         while ((p != NULL) && ((r = strcmp(p->id, id)) != 0)) {
859                 p = (r < 0) ? p->left : p->right;
860         }
861         return p;
862 d221 7
863 a227 7
864         symtab_entry *e;
865 //      while (st != NULL) {
866                 e = binary_search(st->root, id);
867                 if (e != NULL) { return e; }
868 //              st = st->parent;
869 //      }
870         return NULL;
871 d235 1
872 a235 1
873         return lookup(symtab_tos(), id);
874 d243 1
875 a243 1
876         return lookup(symtab_stack[0], id);
877 d255 41
878 a295 41
879         symtab *st = symtab_tos();
880         /* See if this id already exists */
881         symtab_entry *e = symtab_lookup(id);
882         if (e != NULL) {
883                 /* Duplicate symbol. */
884 //              printf("error: symtab_enter(): `%s' already exists\n", id);
885                 return NULL;
886         }
887 //      printf("symtab_enter(): '%s' @@ %.8X\n", id, st);
888         /* Allocate new entry */
889         e = (symtab_entry *)malloc(sizeof(symtab_entry));
890         if (e != NULL) {
891                 /* Set its fields */
892                 e->id = (char *)malloc(strlen(id)+1);
893                 if (e->id != NULL) {
894                         strcpy(e->id, id);
895                 }
896                 e->type = type;
897                 e->flags = flags;
898                 e->ref_count = 0;
899                 e->def = def;
900                 /* Zap! */
901                 e->struc.size = NULL;
902                 e->struc.fields = NULL;
903                 e->field.offset = NULL;
904                 e->field.size = NULL;
905                 e->left = NULL;
906                 e->right = NULL;
907                 e->parent = NULL;
908                 e->symtab = NULL;
909                 /* Put it into symbol table */
910                 if (st->root == NULL) {
911                         st->root = e;   /* This is the root element */
912                 }
913                 else {
914                 /* Insert entry in binary tree */
915                         binary_insert(st->root, e);
916                 }
917         }
918         /* Return the newly created symbol table entry */
919         return e;
920 d305 48
921 a352 48
922         ordered_field_list *list;
923         ordered_field_list *next;
924 //      printf("symtab_finalize(): `%s'\n", e->id);
925         /* Special finalizing */
926         switch (e->type) {
927                 case LABEL_SYMBOL:
928                 break;
929                 
930                 case CONSTANT_SYMBOL:
931                 astnode_finalize(e->def);
932                 break;
934                 case MACRO_SYMBOL:
935                 astnode_finalize(e->def);
936                 break;
938                 case STRUC_SYMBOL:
939                 case UNION_SYMBOL:
940                 case RECORD_SYMBOL:
941                 /* Free list of in-order field identifiers */
942                 for (list = e->struc.fields; list != NULL; list = next) {
943                         next = list->next;
944                         free(list);
945                 }
946                 symtab_finalize(e->symtab);
947                 astnode_finalize(e->struc.size);
948                 break;
950                 case ENUM_SYMBOL:
951                 symtab_finalize(e->symtab);
952                 break;
954                 case VAR_SYMBOL:
955                 astnode_finalize(e->def);
956                 astnode_finalize(e->field.offset);
957                 astnode_finalize(e->field.size);
958                 break;
960                 case PROC_SYMBOL:
961                 break;
962                 
963                 default:
964                 /* Nada */
965                 break;
966         }
967         /* Common finalizing */
968         SAFE_FREE(e->id);
969         SAFE_FREE(e);
970 d361 4
971 a364 4
972         if (e == NULL) return;
973         symtab_entry_finalize_recursive(e->left);
974         symtab_entry_finalize_recursive(e->right);
975         symtab_entry_finalize(e);
976 d373 3
977 a375 3
978         if (st == NULL) return;
979         symtab_entry_finalize_recursive(st->root);
980         SAFE_FREE(st);
981 d385 2
982 a386 2
983         if (stack_index == 0) { return NULL; }
984         return symtab_stack[stack_index-1];
985 d394 7
986 a400 7
987         symtab *st = (symtab *)malloc(sizeof(symtab));
988         if (st != NULL) {
989                 st->root = NULL;
990                 st->parent = NULL; // symtab_tos();
991                 symtab_push(st);        /* NB! */
992         }
993         return st;
994 d408 1
995 a408 1
996         symtab_stack[stack_index++] = st;
997 d416 1
998 a416 1
999         return symtab_stack[--stack_index];
1000 d426 4
1001 a429 4
1002         symtab *st = symtab_tos();
1003         symtab_entry * e = symtab_lookup(id);
1004         if (e == NULL) { return; }      /* Nothing to remove */
1005         binary_delete(st, e);
1006 d439 10
1007 a448 10
1008         symtab *st = symtab_tos();
1009         int count = 0;
1010         symtab_entry *e = binary_min(st->root);
1011         while (e != NULL) {
1012                 if ((type == ANY_SYMBOL) || (e->type == type)) {
1013                         count++;
1014                 }
1015                 e = binary_succ(e);
1016         }
1017         return count;
1018 d458 11
1019 a468 11
1020         int i;
1021         /* Declare array to hold identifiers */
1022         symbol_ident_list list;
1023         /* List the entry identifiers */
1024         int count = symtab_list_type(type, &list);
1025         /* Remove the entries one by one */
1026         for (i=0; i<count; i++) {
1027                 symtab_remove(list.idents[i]);
1028         }
1029         /* Finalize the array of identifiers */
1030         symtab_list_finalize(&list);
1031 d475 4
1032 a478 4
1033         int i;
1034         for (i=0; i<n; i++) {
1035                 printf(" ");
1036         }
1037 d486 5
1038 a490 5
1039         if (e == NULL) { return; }
1040         //indent(level*2);
1041         symtab_entry_print_recursive(e->left, level+1);
1042         printf("%s\n", e->id);
1043         symtab_entry_print_recursive(e->right, level+1);
1044 d498 2
1045 a499 2
1046         symtab *st = symtab_tos();
1047         symtab_entry_print_recursive(st->root, 0);
1048 d509 1
1049 a509 1
1050         return symtab_type_count(ANY_SYMBOL);
1051 d519 20
1052 a538 20
1053         symtab *st = symtab_tos();
1054         int i = 0;
1055         list->size = symtab_type_count(type);
1056         list->idents = (char **)malloc(list->size * sizeof(char *) );
1057         if (list->idents != NULL) {
1058                 symtab_entry *e = binary_min(st->root);
1059                 while (e != NULL) {
1060                         if ((type == ANY_SYMBOL) || (e->type == type)) {
1061                                 /* Add to list */
1062                                 list->idents[i] = (char *)malloc(strlen(e->id)+1);
1063                                 if (list->idents[i] != NULL) {
1064                                         strcpy(list->idents[i], e->id);
1065                                         i++;
1066                                 }
1067                         }
1068                         e = binary_succ(e);
1069                 }
1070         }
1071         /* Return the number of entries listed */
1072         return i;
1073 d546 1
1074 a546 1
1075         return symtab_list_type(ANY_SYMBOL, list);
1076 d556 2
1077 a557 2
1078         // TODO
1079         return 0;
1080 d567 2
1081 a568 2
1082         // TODO
1083         return 0;
1084 d576 5
1085 a580 5
1086         int i;
1087         for (i=0; i<list->size; i++) {
1088                 SAFE_FREE(list->idents[i]);
1089         }
1090         SAFE_FREE(list->idents);
1096 @xorcyst 1.1.0
1098 text
1099 @d2 1
1100 a2 1
1101  * $Id: symtab.c,v 1.1 2004/06/30 07:55:57 kenth Exp $
1102 d4 3
1103 d307 3
1104 d320 1
1105 a329 2
1106                 // TODO: other types: finalize(e->def)
1108 d340 3
1114 @Initial revision
1116 text
1117 @d2 5
1118 a6 2
1119  * $Id$
1120  * $Log$
1121 d39 4
1122 a42 1
1123 static symtab *symtab_stack[1];
1124 d108 2
1125 a109 1
1126 static void binary_insert(symtab_entry *p, symtab_entry *e) {
1127 d178 1
1128 d188 2
1129 a191 1
1130         /* Should call symtab_entry_finalize(y) here */
1131 d193 1
1132 a193 2
1133                 SAFE_FREE(y->id);
1134                 SAFE_FREE(y);
1135 d202 2
1136 a203 1
1137 static symtab_entry *binary_search(symtab_entry *p, const char *id) {
1138 d213 2
1139 a214 4
1141  * Looks up the specified id.
1142  */
1143 symtab_entry *symtab_lookup(const char *id) {
1144 d216 1
1145 a216 3
1146         /* Start looking in the closest namespace */
1147         symtab *st = symtab_tos();
1148         while (st != NULL) {
1149 d219 2
1150 a220 2
1151                 st = st->parent;
1152         }
1153 d225 16
1154 d247 1
1155 a247 1
1156 symtab_entry *symtab_enter(const char *id, symbol_type type, void *def, int flags)
1157 d253 3
1158 a255 3
1159                 printf("'%s' already defined; using previous definition.\n", e->id);
1160                 /* Return the previous definition of id */
1161                 return e;
1162 d257 1
1163 a257 1
1164         //    printf("Entering '%s' @@ %d\n", id, st);
1165 d270 5
1166 d278 1
1167 d299 41
1168 d374 2
1169 a375 1
1170         return symtab_stack[0];
1171 d379 1
1172 a379 1
1173  * Creates a symbol table.
1174 d387 1
1175 a387 1
1176                 symtab_push(st);
1177 d397 1
1178 a397 1
1179         symtab_stack[0] = st;
1180 d405 1
1181 a405 1
1182         return symtab_stack[0];
1183 d461 1
1184 a461 1
1186 d469 1
1187 a469 1
1189 d476 1
1190 a476 1
1191         indent(level*2);
1192 d502 1
1193 a502 1
1194  * Lists the entries in a symbol table.