-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatom.c
176 lines (146 loc) · 3.66 KB
/
atom.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
/* Symbol table.
*
* Braindead -- simplest hash table possible.
*/
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "atom.h"
static const struct str **string_of_atom;
static atom symbol_counter;
typedef uint32_t hash;
static size_t allocated, n_entries;
static atom *table;
__attribute__((constructor))
static void init(void)
{
allocated = 128;
n_entries = 0;
table = calloc(allocated, sizeof(*table));
assert(table);
string_of_atom = malloc((1+allocated) * sizeof(*string_of_atom));
assert(string_of_atom);
}
__attribute__((destructor))
static void shutdown(void)
{
free(table);
for (unsigned i = 1; i <= n_entries; ++i)
str_free((struct str **)&string_of_atom[i]);
free(string_of_atom);
}
static hash hash_of_string(const struct str *s)
{
hash v = 0;
for (size_t i = 0; i < s->len; ++i)
v += s->data[i] * 31;
return v;
}
static void grow(void)
{
size_t original_size = allocated;
allocated <<= 1;
atom *new = calloc(allocated, sizeof(*table));
for (size_t i = 0; i < original_size; ++i) {
atom sym = table[i];
if (sym) {
assert(sym <= symbol_counter);
hash h = hash_of_string(string_of_atom[sym]) % allocated, o = h;
while (new[h] && ++h != o)
h %= allocated;
assert(!new[h]);
new[h] = sym;
}
}
free(table);
table = new;
typeof(string_of_atom) tmp =
realloc(string_of_atom, (1+allocated) * sizeof(*string_of_atom));
assert(tmp != NULL);
string_of_atom = tmp;
assert(string_of_atom);
}
static void insert(const struct str *s, hash h, atom sym)
{
if (n_entries >= allocated)
grow();
while (table[h % allocated])
++h;
table[h % allocated] = sym;
++n_entries;
string_of_atom[sym] = s;
}
static atom lookup(const struct str *s, hash h_orig)
{
atom sym = 0;
h_orig %= allocated;
hash h = h_orig;
while ((sym = table[h])) {
if (str_eq(s, string_of_atom[sym]))
return sym;
h = (h+1) % allocated;
if (h == h_orig)
return 0;
}
return 0;
}
atom intern(const struct str *name)
{
hash h = hash_of_string(name);
atom sym = lookup(name, h);
if (sym != 0) {
str_free((struct str **)&name);
return sym;
}
sym = ++symbol_counter;
insert(name, h, sym);
return sym;
}
atom intern_cstr(const char *name)
{
return intern(str_dup_cstr(name));
}
const struct str *symbol_name(atom sym)
{
if (sym < 1 || sym > symbol_counter)
return NULL;
return string_of_atom[sym];
}
static bool atom_must_be_quoted_p(const struct str *name)
{
if (name->len == 0)
return true;
/* [a-z@][0-9a-zA-Z_@] */
if (name->data[0] != '@' &&
!(islower(name->data[0]) &&
isalpha(name->data[0])))
return true;
for (unsigned i = 1; i < name->len; ++i)
if (name->data[i] != '@' &&
name->data[i] != '_' &&
!isalpha(name->data[i]))
return true;
return false;
}
static void pretty_print_quoted_atom(FILE *out, const struct str *name)
{
fputc('\'', out);
for (unsigned i = 0; i < name->len; ++i) {
if (isgraph(name->data[i]))
fputc(name->data[i], out);
else
fprintf(out, "\\x%02x", (uint8_t)name->data[i]);
}
fputc('\'', out);
}
void pretty_print_atom(FILE *out, atom a)
{
const struct str *name = symbol_name(a);
if (!name)
fputs("(null)", out);
else if (atom_must_be_quoted_p(name))
pretty_print_quoted_atom(out, name);
else
str_print(out, name);
}