-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsexpr.c
526 lines (426 loc) · 9.67 KB
/
sexpr.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
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
/*
* Copyright (c) 2015-2019 Josef 'Jeff' Sipek <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <jeffpc/sexpr.h>
#include <jeffpc/mem.h>
#include "sexpr_impl.h"
struct val *sexpr_parse(const char *str, size_t len)
{
struct sexpr_parser_state x;
int ret;
x.input = str;
x.len = len;
x.pos = 0;
sexpr_reader_lex_init(&x.scanner);
sexpr_reader_set_extra(&x, x.scanner);
ret = sexpr_reader_parse(&x);
sexpr_reader_lex_destroy(x.scanner);
return ret ? ERR_PTR(-EINVAL) : x.output;
}
/*
* Convert a C array of vals into a sexpr list. E.g.,
*
* vals = { A, B, C }, nvals = 3
*
* turns into:
*
* '(A . (B . (C . ())))
*
* which is the same as:
*
* '(A B C)
*/
struct val *sexpr_array_to_list(struct val **vals, size_t nvals)
{
struct val *last = NULL;
struct val *tmp;
if (!nvals)
return val_empty_cons();
for (; nvals > 0; nvals--, last = tmp)
tmp = VAL_ALLOC_CONS(vals[nvals - 1], last);
return last;
}
/*
* Just like sexpr_array_to_list, but obtains value from args instead of an
* array.
*/
struct val *sexpr_args_to_list(size_t nvals, ...)
{
const size_t n = nvals;
struct val *arr[n];
va_list args;
size_t i;
va_start(args, nvals);
for (i = 0; i < n; i++)
arr[i] = va_arg(args, struct val *);
va_end(args);
return sexpr_array_to_list(arr, n);
}
/*
* Convert a sexpr list into a C array of vals. E.g.,
*
* '(A B C)
*
* turns into:
*
* array = { A, B, C }, nvals = 3
*
* We fill in the passed in array with at most alen elements. The number of
* filled in elements is returned to the caller.
*/
ssize_t sexpr_list_to_array(struct val *list, struct val **array, size_t alen)
{
struct val *tmp;
size_t nvals = 0;
ssize_t ret;
for (tmp = list;
!sexpr_is_null(tmp) && (alen > nvals);
tmp = tmp->cons.tail, nvals++) {
if (tmp->type != VT_CONS) {
ret = -EINVAL;
goto err;
}
array[nvals] = val_getref(tmp->cons.head);
}
if ((alen == nvals) && !sexpr_is_null(tmp)) {
ret = -ENOMEM;
goto err;
}
return nvals;
err:
while (nvals)
val_putref(array[--nvals]);
return ret;
}
/*
* Convert a sexpr list into a C array of vals. E.g.,
*
* '(A B C)
*
* turns into a VT_ARRAY containing:
*
* { A, B, C }
*/
struct val *sexpr_list_to_val_array(struct val *list)
{
struct val **arr;
ssize_t len;
int ret;
len = sexpr_length(val_getref(list));
if (len < 0) {
val_putref(list);
return ERR_PTR(-EINVAL);
}
arr = mem_reallocarray(NULL, len, sizeof(struct val *));
if (!arr) {
val_putref(list);
return ERR_PTR(-ENOMEM);
}
ret = sexpr_list_to_array(list, arr, len);
if (ret != len) {
free(arr);
return ERR_PTR(-EINVAL);
}
return val_alloc_array(arr, len);
}
struct val *sexpr_car(struct val *lv)
{
struct val *ret;
if (!lv)
return NULL;
if (lv->type == VT_CONS)
ret = val_getref(lv->cons.head);
else
ret = NULL;
val_putref(lv);
return ret;
}
struct val *sexpr_cdr(struct val *lv)
{
struct val *ret;
if (!lv)
return NULL;
if (lv->type == VT_CONS)
ret = val_getref(lv->cons.tail);
else
ret = NULL;
val_putref(lv);
return ret;
}
ssize_t sexpr_length(struct val *lv)
{
ssize_t len;
len = 0;
while (!sexpr_is_null(lv)) {
if (lv->type != VT_CONS) {
/* not a list */
val_putref(lv);
return -1;
}
len++;
lv = sexpr_cdr(lv);
}
val_putref(lv);
return len;
}
struct val *sexpr_nth(struct val *lv, uint64_t n)
{
while (n-- && lv) {
struct val *tmp;
if (lv->type == VT_CONS) {
/*
* If this is not the one we want, follow the tail.
* Otherwise, grab the head.
*/
if (n)
tmp = val_getref(lv->cons.tail);
else
tmp = val_getref(lv->cons.head);
} else {
tmp = NULL;
}
val_putref(lv);
lv = tmp;
}
return lv;
}
/*
* Given a list, lookup a certain name.
*
* The input list looks like:
* '((a . b) (c . d))
* which really is:
* '((a . b) . ((c . d) . ()))
*
* So, to check it, we examine the car of the list, if that's not the right
* key, we recurse on cdr of the list.
*/
struct val *sexpr_assoc(struct val *lv, const char *name)
{
struct val *head;
struct val *tail;
/* empty list */
if (!lv)
return NULL;
/* not a list */
if (lv->type != VT_CONS)
return NULL;
head = lv->cons.head;
tail = lv->cons.tail;
/*
* check the head of current cons cell: '(head . tail)
* (1) must be non-null
* (2) must be a cons cell, i.e., head == '(a . b)
* (3) (car head) must be a string or symbol
* (4) (car head) must be equal to the value passed in
*/
if (head && (head->type == VT_CONS) &&
head->cons.head &&
((head->cons.head->type == VT_STR) ||
(head->cons.head->type == VT_SYM)) &&
!strcmp(val_cstr(head->cons.head), name))
return val_getref(head);
return sexpr_assoc(tail, name);
}
bool sexpr_equal(struct val *lhs, struct val *rhs)
{
bool ret;
/* if they are the same object, they are equal - even if NULL */
if (lhs == rhs) {
ret = true;
goto out;
}
/* if one is NULL, they are unequal */
if (!lhs || !rhs) {
/* ... unless we're comparing a NULL with a '() */
if ((!lhs && ((rhs->type != VT_CONS) ||
rhs->cons.head ||
rhs->cons.tail)) ||
(!rhs && ((lhs->type != VT_CONS) ||
lhs->cons.head ||
lhs->cons.tail))) {
ret = false;
goto out;
}
ret = true;
goto out;
}
/*
* At this point, we have two non-NULL values.
*/
/* different type -> unequal */
if (lhs->type != rhs->type) {
ret = false;
goto out;
}
switch (lhs->type) {
case VT_NULL:
ret = true;
goto out;
case VT_INT:
case VT_CHAR:
ret = (lhs->i == rhs->i);
goto out;
case VT_STR:
case VT_SYM:
ret = str_cmp(val_cast_to_str(lhs),
val_cast_to_str(rhs)) == 0;
goto out;
case VT_BOOL:
ret = (lhs->b == rhs->b);
goto out;
case VT_BLOB:
ret = (lhs->blob.size == rhs->blob.size);
if (!ret)
goto out;
ret = (lhs->blob.ptr == rhs->blob.ptr);
if (ret)
goto out;
ret = (memcmp(lhs->blob.ptr, rhs->blob.ptr,
lhs->blob.size) == 0);
goto out;
case VT_CONS:
ret = sexpr_equal(val_getref(lhs->cons.head),
val_getref(rhs->cons.head)) &&
sexpr_equal(val_getref(lhs->cons.tail),
val_getref(rhs->cons.tail));
goto out;
case VT_ARRAY: {
size_t i;
ret = (lhs->array.nelem == rhs->array.nelem);
if (!ret)
goto out;
for (i = 0; i < lhs->array.nelem; i++) {
ret = sexpr_equal(val_getref(lhs->_set_array.vals[i]),
val_getref(rhs->_set_array.vals[i]));
if (!ret)
break;
}
goto out;
}
case VT_NVL: {
struct rb_tree *ltree = &lhs->_set_nvl.values;
struct rb_tree *rtree = &rhs->_set_nvl.values;
struct nvpair *lcur;
struct nvpair *rcur;
lcur = rb_first(ltree);
rcur = rb_first(rtree);
while (lcur && rcur) {
ret = (str_cmp(lcur->name, rcur->name) == 0);
if (!ret)
goto out;
ret = sexpr_equal(val_getref(lcur->value),
val_getref(rcur->value));
if (!ret)
goto out;
lcur = rb_next(ltree, lcur);
rcur = rb_next(rtree, rcur);
}
/* if both sides reached the end, then they are equal */
ret = !lcur && !rcur;
goto out;
}
}
panic("unknown struct val type %u", lhs->type);
out:
val_putref(lhs);
val_putref(rhs);
return ret;
}
struct val *sexpr_alist_lookup_val(struct val *lv, const char *name)
{
if (!lv || !name)
return NULL;
return sexpr_cdr(sexpr_assoc(lv, name));
}
struct str *sexpr_alist_lookup_str(struct val *lv, const char *name)
{
struct str *ret;
struct val *v;
if (!lv || !name)
return NULL;
v = sexpr_cdr(sexpr_assoc(lv, name));
if (!v || (v->type != VT_STR))
ret = NULL;
else
ret = val_cast_to_str(val_getref(v));
val_putref(v);
return ret;
}
uint64_t sexpr_alist_lookup_int(struct val *lv, const char *name, bool *found)
{
struct val *v;
uint64_t ret;
bool ok;
if (!lv || !name) {
if (found)
*found = false;
return 0;
}
v = sexpr_cdr(sexpr_assoc(lv, name));
ok = v && (v->type == VT_INT);
if (!ok)
ret = 0;
else
ret = v->i;
val_putref(v);
if (found)
*found = ok;
return ret;
}
bool sexpr_alist_lookup_bool(struct val *lv, const char *name, bool def,
bool *found)
{
struct val *v;
bool ret;
bool ok;
if (!lv || !name) {
if (found)
*found = false;
return def;
}
v = sexpr_cdr(sexpr_assoc(lv, name));
ok = v && (v->type == VT_BOOL);
if (!ok)
ret = def;
else
ret = v->b;
val_putref(v);
if (found)
*found = ok;
return ret;
}
struct val *sexpr_alist_lookup_list(struct val *lv, const char *name)
{
struct val *ret;
struct val *v;
if (!lv || !name)
return NULL;
v = sexpr_cdr(sexpr_assoc(lv, name));
if (!v || (v->type != VT_CONS))
ret = NULL;
else
ret = val_getref(v);
val_putref(v);
return ret;
}