-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
201 lines (162 loc) · 3.58 KB
/
map.c
File metadata and controls
201 lines (162 loc) · 3.58 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
#include "cy.h"
static unsigned int map_depth = 0;
static int eval_map_end(struct cy_token *t, struct cy_file *f)
{
if (map_depth > 0) {
t->v.t = CY_V_MAP_END;
return 1;
}
show_token_err(t, "Dangling map terminator");
return -1;
}
int map_add_value(const char *key, struct cy_value *v, struct rb_root *root, bool upsert)
{
struct rb_node *node = root->rb_node, *parent = NULL;
struct rb_node **new = &root->rb_node;
struct cy_map_value *mv;
while (node) {
struct cy_map_value *this;
int r;
parent = *new;
this = rb_entry(node, struct cy_map_value, n);
r = strcmp(this->key, key);
if (r == 0) {
if (!upsert)
return -1;
this->v = *v;
return 0;
}
if (r > 0)
node = node->rb_left, new = &((*new)->rb_left);
else
node = node->rb_right, new = &((*new)->rb_right);
}
mv = malloc(sizeof(*mv));
mv->key = key;
mv->v = *v;
rb_link_and_balance(root, &mv->n, parent, new);
return 1;
}
static int map_key(struct cy_file *f, const char **keyp)
{
int ret;
struct cy_token kt;
ret = cy_next(f, &kt);
if (ret <= 0)
return -1;
/*
* Symbols as keys are accepted as is, w/o derefernece,
* for simplicity and nicety in declaring objects. To
* push dereferenced string as key use + operator.
*/
if (is_symbol_token(&kt)) {
*keyp = kt.v.v_sym;
return 1;
}
if (kt.typ->eval) {
ret = kt.typ->eval(&kt, f);
if (ret <= 0)
return -1;
}
if (kt.v.t & CY_V_TERMINATOR) {
if (kt.v.t == CY_V_MAP_END)
return 0;
else {
show_token_err(&kt, "Unexpected map terminator");
return -1;
}
}
if (kt.v.t != CY_V_STRING) {
show_token_err(&kt, "Expected string as map key, got %s", vtype2s(kt.v.t));
return -1;
}
*keyp = kt.v.v_str;
return 1;
}
static int eval_map(struct cy_token *t, struct cy_file *f)
{
make_map(&t->v);
map_depth++;
while (1) {
int ret;
const char *key;
struct cy_token vt;
ret = map_key(f, &key);
if (ret < 0)
goto er;
if (ret == 0) {
map_depth--;
return 1;
}
if (cy_eval_next(f, &vt) <= 0)
goto er;
if (vt.v.t & CY_V_TERMINATOR)
goto er;
if (map_add_value(key, &vt.v, &t->v.v_map->r, false) < 0)
goto er;
continue;
er:
show_token_err(t, "Error processing map entry");
map_depth--;
return -1;
}
}
static int eval_empty_map(struct cy_token *t, struct cy_file *f)
{
make_map(&t->v);
return 1;
}
struct cy_map_value *find_in_map(struct rb_root *root, const char *key, unsigned klen)
{
struct rb_node *node = root->rb_node;
while (node) {
struct cy_map_value *this;
int r;
this = rb_entry(node, struct cy_map_value, n);
if (klen)
r = strncmp(this->key, key, klen);
else
r = strcmp(this->key, key);
if (r == 0)
return this;
if (r > 0)
node = node->rb_left;
else
node = node->rb_right;
}
return NULL;
}
int dereference_map_elem(struct cy_token *t, struct cy_value *m, struct cy_value *kv)
{
struct cy_map_value *mv;
if (kv->t != CY_V_STRING) {
show_token_err(t, "Nested map resolve is not a string");
return -1;
}
mv = find_in_map(&m->v_map->r, kv->v_str, 0);
if (mv)
t->v = mv->v;
else
t->v.t = CY_V_NOVALUE;
return 1;
}
int cy_map_del(struct cy_value *m, struct cy_value *k)
{
struct rb_root *root = &m->v_map->r;
struct cy_map_value *mv;
mv = find_in_map(root, k->v_str, 0);
if (!mv)
return -1;
rb_erase(&mv->n, root);
return 1;
}
static struct cy_command cmd_map[] = {
{ .name = "[", .t = { .ts = "map start", .eval = eval_map, }, },
{ .name = "]", .t = { .ts = "map end", .eval = eval_map_end, }, },
{ .name = "[]", .t = { .ts = "empty map", .eval = eval_empty_map, }, },
{}
};
void init_map(void)
{
add_commands(cmd_map);
}