28 |
| Create a new symbol, named TAG. | |
| Create a new symbol, named TAG. | |
29 |
`---------------------------------*/ |
`---------------------------------*/ |
30 |
|
|
31 |
static bucket * |
static symbol_t * |
32 |
bucket_new (const char *tag) |
symbol_new (const char *tag) |
33 |
{ |
{ |
34 |
bucket *res = XMALLOC (bucket, 1); |
symbol_t *res = XMALLOC (symbol_t, 1); |
35 |
|
|
36 |
res->tag = xstrdup (tag); |
res->tag = xstrdup (tag); |
37 |
res->type_name = NULL; |
res->type_name = NULL; |
56 |
`------------*/ |
`------------*/ |
57 |
|
|
58 |
static void |
static void |
59 |
bucket_free (bucket *this) |
symbol_free (symbol_t *this) |
60 |
{ |
{ |
61 |
#if 0 |
#if 0 |
62 |
/* This causes crashes because one string can appear more |
/* This causes crashes because one string can appear more |
70 |
|
|
71 |
|
|
72 |
/*----------------------. |
/*----------------------. |
73 |
| A bucket hash table. | |
| A symbol_t hash table. | |
74 |
`----------------------*/ |
`----------------------*/ |
75 |
|
|
76 |
/* Initial capacity of buckets hash table. */ |
/* Initial capacity of symbols hash table. */ |
77 |
#define HT_INITIAL_CAPACITY 257 |
#define HT_INITIAL_CAPACITY 257 |
78 |
|
|
79 |
static struct hash_table *bucket_table = NULL; |
static struct hash_table *symbol_table = NULL; |
80 |
|
|
81 |
static bool |
static bool |
82 |
hash_compare_bucket (const bucket *m1, const bucket *m2) |
hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2) |
83 |
{ |
{ |
84 |
return strcmp (m1->tag, m2->tag) ? FALSE : TRUE; |
return strcmp (m1->tag, m2->tag) ? FALSE : TRUE; |
85 |
} |
} |
86 |
|
|
87 |
static unsigned int |
static unsigned int |
88 |
hash_bucket (const bucket *m, unsigned int tablesize) |
hash_symbol_t (const symbol_t *m, unsigned int tablesize) |
89 |
{ |
{ |
90 |
return hash_string (m->tag, tablesize); |
return hash_string (m->tag, tablesize); |
91 |
} |
} |
92 |
|
|
93 |
|
|
94 |
/*-------------------------------. |
/*-------------------------------. |
95 |
| Create the bucket hash table. | |
| Create the symbol_t hash table. | |
96 |
`-------------------------------*/ |
`-------------------------------*/ |
97 |
|
|
98 |
void |
void |
99 |
buckets_new (void) |
symbols_new (void) |
100 |
{ |
{ |
101 |
bucket_table = hash_initialize (HT_INITIAL_CAPACITY, |
symbol_table = hash_initialize (HT_INITIAL_CAPACITY, |
102 |
NULL, |
NULL, |
103 |
(Hash_hasher) hash_bucket, |
(Hash_hasher) hash_symbol_t, |
104 |
(Hash_comparator) hash_compare_bucket, |
(Hash_comparator) hash_compare_symbol_t, |
105 |
(Hash_data_freer) bucket_free); |
(Hash_data_freer) symbol_free); |
106 |
} |
} |
107 |
|
|
108 |
|
|
111 |
| yet, create it. | |
| yet, create it. | |
112 |
`----------------------------------------------------------------*/ |
`----------------------------------------------------------------*/ |
113 |
|
|
114 |
bucket * |
symbol_t * |
115 |
getsym (const char *key) |
getsym (const char *key) |
116 |
{ |
{ |
117 |
bucket probe; |
symbol_t probe; |
118 |
bucket *entry; |
symbol_t *entry; |
119 |
|
|
120 |
(const char *) probe.tag = key; |
(const char *) probe.tag = key; |
121 |
entry = hash_lookup (bucket_table, &probe); |
entry = hash_lookup (symbol_table, &probe); |
122 |
|
|
123 |
if (!entry) |
if (!entry) |
124 |
{ |
{ |
125 |
/* First insertion in the hash. */ |
/* First insertion in the hash. */ |
126 |
entry = bucket_new (key); |
entry = symbol_new (key); |
127 |
hash_insert (bucket_table, entry); |
hash_insert (symbol_table, entry); |
128 |
} |
} |
129 |
return entry; |
return entry; |
130 |
} |
} |
131 |
|
|
132 |
|
|
133 |
/*-------------------. |
/*-------------------. |
134 |
| Free the buckets. | |
| Free the symbols. | |
135 |
`-------------------*/ |
`-------------------*/ |
136 |
|
|
137 |
void |
void |
138 |
buckets_free (void) |
symbols_free (void) |
139 |
{ |
{ |
140 |
hash_free (bucket_table); |
hash_free (symbol_table); |
141 |
} |
} |
142 |
|
|
143 |
|
|
144 |
/*---------------------------------------------------------------. |
/*---------------------------------------------------------------. |
145 |
| Look for undefined buckets, report an error, and consider them | |
| Look for undefined symbols, report an error, and consider them | |
146 |
| terminals. | |
| terminals. | |
147 |
`---------------------------------------------------------------*/ |
`---------------------------------------------------------------*/ |
148 |
|
|
149 |
void |
void |
150 |
buckets_do (bucket_processor processor, void *processor_data) |
symbols_do (symbol_processor processor, void *processor_data) |
151 |
{ |
{ |
152 |
hash_do_for_each (bucket_table, |
hash_do_for_each (symbol_table, |
153 |
(Hash_processor) processor, |
(Hash_processor) processor, |
154 |
processor_data); |
processor_data); |
155 |
} |
} |