Debian Codegraph @

src/utils/cst_val.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 1999 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: December 1999 */
35 /*************************************************************************/
36 /* */
37 /* Typed values */
38 /* */
39 /*************************************************************************/
40 #include "cst_math.h"
41 #include "cst_file.h"
42 #include "cst_val.h"
43 #include "cst_string.h"
44 #include "cst_tokenstream.h"
45
46 static cst_val *new_val()
47 {
48 return cst_alloc(struct cst_val_struct,1);
49 }
50
51 cst_val *int_val(int i)
52 {
53 cst_val *v = new_val();
54 CST_VAL_TYPE(v) = CST_VAL_TYPE_INT;
55 CST_VAL_INT(v) = i;
56 return v;
57 }
58
59 cst_val *float_val(float f)
60 {
61 cst_val *v = new_val();
62 CST_VAL_TYPE(v) = CST_VAL_TYPE_FLOAT;
63 CST_VAL_FLOAT(v) = f;
64 return v;
65 }
66
67 cst_val *string_val(const char *s)
68 {
69 cst_val *v = new_val();
70 CST_VAL_TYPE(v) = CST_VAL_TYPE_STRING;
71 /* would be nice to note if this is a deletable string or not */
72 CST_VAL_STRING_LVAL(v) = cst_strdup(s);
73 return v;
74 }
75
76 cst_val *cons_val(const cst_val *a, const cst_val *b)
77 {
78 cst_val *v = new_val();
79 CST_VAL_CAR(v)=((!a || cst_val_consp(a)) ?
80 (cst_val *)(void *)a:val_inc_refcount(a));
81 CST_VAL_CDR(v)=((!b || cst_val_consp(b)) ?
82 (cst_val *)(void *)b:val_inc_refcount(b));
83 return v;
84 }
85
86 cst_val *val_new_typed(int type,void *vv)
87 {
88 cst_val *v = new_val();
89 CST_VAL_TYPE(v) = type;
90 CST_VAL_VOID(v) = vv;
91 return v;
92 }
93
94 void delete_val_list(cst_val *v)
95 {
96 if (v)
97 {
98 if (cst_val_consp(v))
99 {
100 delete_val_list(CST_VAL_CDR(v));
101 cst_free(v);
102 }
103 else
104 delete_val(v);
105 }
106 }
107
108 void delete_val(cst_val *v)
109 {
110 if (v)
111 {
112 if (cst_val_consp(v))
113 {
114 delete_val(CST_VAL_CAR(v));
115 delete_val(CST_VAL_CDR(v));
116 cst_free(v);
117 }
118 else if (val_dec_refcount(v) == 0)
119 {
120 if (CST_VAL_TYPE(v) == CST_VAL_TYPE_STRING)
121 cst_free(CST_VAL_VOID(v));
122 else if (CST_VAL_TYPE(v) >= CST_VAL_TYPE_FIRST_FREE)
123 {
124 if (cst_val_defs[CST_VAL_TYPE(v)/2].delete_function)
125 (cst_val_defs[CST_VAL_TYPE(v)/2].delete_function)
126 (CST_VAL_VOID(v));
127 }
128 cst_free(v);
129 }
130 }
131 }
132
133 /* Accessor functions */
134 int val_int(const cst_val *v)
135 {
136 if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_INT))
137 return CST_VAL_INT(v);
138 else if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_FLOAT))
139 return (int)CST_VAL_FLOAT(v);
140 else if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_STRING))
141 return atoi(CST_VAL_STRING(v));
142 else
143 {
144 cst_errmsg("VAL: tried to access int in %d typed val\n",
145 (v ? CST_VAL_TYPE(v) : -1));
146 cst_error();
147 }
148 return 0;
149 }
150
151 float val_float(const cst_val *v)
152 {
153 if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_INT))
154 return (float)CST_VAL_INT(v);
155 else if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_FLOAT))
156 return CST_VAL_FLOAT(v);
157 else if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_STRING))
158 return cst_atof(CST_VAL_STRING(v));
159 else
160 {
161 cst_errmsg("VAL: tried to access float in %d typed val\n",
162 (v ? CST_VAL_TYPE(v) : -1));
163 cst_error();
164 }
165 return 0;
166 }
167
168 const char *val_string(const cst_val *v)
169 {
170 if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_STRING))
171 return CST_VAL_STRING(v);
172 else
173 {
174 cst_errmsg("VAL: tried to access string in %d typed val\n",
175 (v ? CST_VAL_TYPE(v) : -1));
176 cst_error();
177 }
178 return 0;
179 }
180
181 const cst_val *val_car(const cst_val *v)
182 {
183 if (v && cst_val_consp(v))
184 return CST_VAL_CAR(v);
185 else
186 {
187 cst_errmsg("VAL: tried to access car in %d typed val\n",
188 (v ? CST_VAL_TYPE(v) : -1));
189 cst_error();
190 }
191 return 0;
192 }
193
194 const cst_val *val_cdr(const cst_val *v)
195 {
196 if (v && cst_val_consp(v))
197 return CST_VAL_CDR(v);
198 else
199 {
200 cst_errmsg("VAL: tried to access cdr in %d typed val\n",
201 (v ? CST_VAL_TYPE(v) : -1));
202 cst_error();
203 }
204 return 0;
205 }
206
207 void *val_generic(const cst_val *v, int type, const char *stype)
208 { /* a generic access function that checks the expected type */
209 if (v && CST_VAL_TYPE(v) == type)
210 return CST_VAL_VOID(v);
211 else
212 {
213 cst_errmsg("VAL: tried to access %s in %d type val\n",
214 stype,
215 (v ? CST_VAL_TYPE(v) : -1));
216 cst_error();
217 }
218 return NULL;
219 }
220
221 void *val_void(const cst_val *v)
222 {
223 /* The scary, do anything function, this shouldn't be called by mortals */
224 if ((v == NULL) ||
225 (CST_VAL_TYPE(v) == CST_VAL_TYPE_CONS) ||
226 (CST_VAL_TYPE(v) == CST_VAL_TYPE_INT) ||
227 (CST_VAL_TYPE(v) == CST_VAL_TYPE_FLOAT))
228 {
229 cst_errmsg("VAL: tried to access void in %d typed val\n",
230 (v ? CST_VAL_TYPE(v) : -1));
231 cst_error();
232 return NULL;
233 }
234 else
235 return CST_VAL_VOID(v);
236 }
237
238 int cst_val_consp(const cst_val *v)
239 {
240 /* To keep a val cell down to 8 bytes we identify non-cons cells */
241 /* with non-zero values in the least significant bit of the first */
242 /* address in the cell (this is a standard technique used on Lisp */
243 /* machines) */
244 #if 0
245 void *t;
246 int t1;
247
248 /* Hmm this still isn't right (it can be) but this isn't it */
249 t = CST_VAL_CAR(v);
250 t1 = *(int *)&t;
251
252 if ((t1&0x1) == 0)
253 return TRUE;
254 else
255 return FALSE;
256 #endif
257 const cst_val_atom *t;
258
259 t = (const cst_val_atom *)v;
260 if (t->type % 2 == 0)
261 return TRUE;
262 else
263 return FALSE;
264 }
265
266 const cst_val *set_cdr(cst_val *v1, const cst_val *v2)
267 {
268 /* destructive set cdr, be careful you have a pointer to current cdr */
269
270 if (!cst_val_consp(v1))
271 {
272 cst_errmsg("VAL: tried to set cdr of non-consp cell\n");
273 cst_error();
274 return NULL;
275 }
276 else
277 {
278 if (CST_VAL_CDR(v1))
279 {
280 val_dec_refcount(CST_VAL_CDR(v1));
281 val_inc_refcount(v1);
282 }
283 CST_VAL_CDR(v1) = (cst_val *)v2;
284 }
285 return v1;
286 }
287
288 const cst_val *set_car(cst_val *v1, const cst_val *v2)
289 {
290 /* destructive set car, be careful you have a pointer to current car */
291
292 if (!cst_val_consp(v1))
293 {
294 cst_errmsg("VAL: tried to set car of non-consp cell\n");
295 cst_error();
296 return NULL;
297 }
298 else
299 {
300 val_dec_refcount(CST_VAL_CAR(v1));
301 val_inc_refcount(v1);
302 CST_VAL_CAR(v1) = (cst_val *)v2;
303 }
304 return v1;
305 }
306
307 void val_print(cst_file fd,const cst_val *v)
308 {
309 const cst_val *p;
310
311 if (v == NULL)
312 cst_fprintf(fd,"[null]");
313 else if (CST_VAL_TYPE(v) == CST_VAL_TYPE_INT)
314 cst_fprintf(fd,"%d",val_int(v));
315 else if (CST_VAL_TYPE(v) == CST_VAL_TYPE_FLOAT)
316 cst_fprintf(fd,"%f",val_float(v));
317 else if (CST_VAL_TYPE(v) == CST_VAL_TYPE_STRING)
318 cst_fprintf(fd,"%s",val_string(v));
319 else if (cst_val_consp(v))
320 {
321 cst_fprintf(fd,"(");
322 for (p=v; p; )
323 {
324 val_print(fd,val_car(p));
325 p=val_cdr(p);
326 if (p)
327 cst_fprintf(fd," ");
328 if (p && !cst_val_consp(p)) /* dotted pairs for non-list */
329 {
330 cst_fprintf(fd,". ");
331 val_print(fd,p);
332 break;
333 }
334 }
335 cst_fprintf(fd,")");
336 }
337 else
338 cst_fprintf(fd,"[Val %s 0x%p]",
339 cst_val_defs[CST_VAL_TYPE(v)/2].name,CST_VAL_VOID(v));
340 }
341
342 cst_val *val_reverse(cst_val *l)
343 { /* destructively reverse the list */
344 cst_val *n,*np,*nl;
345 for (nl=0,n=l; n; nl=n,n=np)
346 {
347 np=CST_VAL_CDR(n);
348 CST_VAL_CDR(n) = nl;
349 }
350 return nl;
351 }
352
353 cst_val *val_append(cst_val *l1, cst_val *l2)
354 {
355 /* Destructively add l2 to the end of l1 return l1 */
356 cst_val *t;
357
358 if (l1 == 0)
359 return l2;
360 else
361 {
362 for (t=l1; val_cdr(t); t=CST_VAL_CDR(t));
363 CST_VAL_CDR(t) = l2;
364 return l1;
365 }
366 }
367
368 int val_length(const cst_val *l)
369 {
370 const cst_val *n;
371 int i;
372
373 for (i=0,n=l; n; n=val_cdr(n))
374 i++;
375
376 return i;
377 }
378
379 int val_equal(const cst_val *v1, const cst_val *v2)
380 {
381 if (v1 == v2)
382 return TRUE; /* its eq so its equal */
383 else if (v1 == 0)
384 return FALSE;
385 else if (CST_VAL_TYPE(v1) == CST_VAL_TYPE(v2))
386 {
387 if (cst_val_consp(v1))
388 return ((val_equal(val_car(v1),val_car(v2))) &&
389 (val_equal(val_cdr(v1),val_cdr(v2))));
390 else if (CST_VAL_TYPE(v1) == CST_VAL_TYPE_INT)
391 return (val_int(v1) == val_int(v2));
392 else if (CST_VAL_TYPE(v1) == CST_VAL_TYPE_FLOAT)
393 return (val_float(v1) == val_float(v2));
394 else if (CST_VAL_TYPE(v1) == CST_VAL_TYPE_STRING)
395 return (cst_streq(CST_VAL_STRING(v1),CST_VAL_STRING(v2)));
396 else
397 return CST_VAL_VOID(v1) == CST_VAL_VOID(v2);
398 }
399 else
400 return FALSE;
401 }
402
403 int val_less(const cst_val *v1, const cst_val *v2)
404 {
405 return val_float(v1) < val_float(v2);
406 }
407
408 int val_greater(const cst_val *v1,const cst_val *v2)
409 {
410 return val_float(v1) > val_float(v2);
411 }
412
413 int val_member(const cst_val *v1,const cst_val *l)
414 {
415 const cst_val *i;
416
417 for (i=l; i; i=val_cdr(i))
418 {
419 if (val_equal(val_car(i),v1))
420 return TRUE;
421 }
422 return FALSE;
423 }
424
425 int val_member_string(const char *v1,const cst_val *l)
426 {
427 const cst_val *i;
428
429 for (i=l; i; i=val_cdr(i))
430 {
431 if (cst_streq(v1,val_string(val_car(i))))
432 return TRUE;
433 }
434 return FALSE;
435 }
436
437 cst_val *val_inc_refcount(const cst_val *b)
438 {
439 cst_val *wb;
440
441 /* Well I was lying, they're not really const, but this is the place */
442 /* where breaking const is reasonable */
443 wb = (cst_val *)(void *)b;
444
445 if (CST_VAL_REFCOUNT(wb) == -1)
446 /* or is a cons cell in the text segment, how do I do that ? */
447 return wb;
448 else if (!cst_val_consp(wb)) /* we don't ref count cons cells */
449 CST_VAL_REFCOUNT(wb) += 1;
450 return wb;
451 }
452
453 int val_dec_refcount(const cst_val *b)
454 {
455 cst_val *wb;
456
457 wb = (cst_val *)(void *)b;
458
459 if (CST_VAL_REFCOUNT(wb) == -1)
460 /* or is a cons cell in the text segment, how do I do that ? */
461 return -1;
462 else if (cst_val_consp(wb)) /* we don't ref count cons cells */
463 return 0;
464 else if (CST_VAL_REFCOUNT(wb) == 0)
465 {
466 /* Otherwise, trying to free a val outside an
467 item/relation/etc has rather the opposite effect from what
468 you might have intended... */
469 return 0;
470 }
471 else
472 {
473 CST_VAL_REFCOUNT(wb) -= 1;
474 return CST_VAL_REFCOUNT(wb);
475 }
476 }
477
478 #ifdef _WIN32
479 __inline int utf8_sequence_length(char c0)
480 #else
481 int utf8_sequence_length(char c0)
482 #endif
483 {
484 /* Get the expected length of UTF8 sequence given its most */
485 /* significant byte */
486 return (( 0xE5000000 >> (( c0 >> 3 ) & 0x1E )) & 3 ) + 1;
487 }
488
489 cst_val *cst_utf8_explode(const cst_string *utf8string)
490 {
491 /* Return a list of utf8 characters as strings */
492 cst_val *chars=NULL;
493
494 const unsigned char *str = (const unsigned char*)utf8string;
495 char utf8char[5];
496 char c0;
497 int charlength;
498
499 while ((c0 = *str))
500 {
501 charlength = utf8_sequence_length(c0);
502 cst_snprintf(utf8char, charlength + 1, "%s", str);
503 chars = cons_val(string_val(utf8char),chars);
504 str += charlength;
505 }
506 return val_reverse(chars);
507 }
508
509 static int utf8_ord(const char *utf8_seq)
510 {
511 unsigned int len;
512 int ord;
513
514 unsigned char c0, c1, c2, c3; /* Potential bytes in the UTF8 symbol */
515 c0 = utf8_seq[0];
516 len = utf8_sequence_length(c0);
517
518 /* Make sure the string sequence we received matches with the */
519 /* expected length, and that the expected length is nonzero. */
520 if ( (len == 0) ||
521 (len != strlen(utf8_seq))) {
522 return -1;
523 }
524
525 if (len == 1) {
526 /* ASCII sequence. */
527 return c0;
528 }
529
530 c1 = utf8_seq[1];
531 if (len == 2) {
532 ord = ((c0 & 0x1F) << 6) | (c1 & 0x3F);
533 if (ord < 0x80)
534 return -1;
535 return ord;
536 }
537
538 c2 = utf8_seq[2];
539 if (len == 3) {
540 if ((c2 & 0xC0) != 0x80)
541 return -1;
542 ord = ((c0 & 0x0F) << 12) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);
543 if (ord < 0x800 ||
544 (ord >= 0xD800 && ord <= 0xDFFF))
545 return -1;
546 return ord;
547 }
548
549 c3 = utf8_seq[3];
550 if (len == 4) {
551 if ((c3 & 0xC0) != 0x80)
552 return -1;
553 ord =
554 ((c0 & 0x7) << 18) | ((c1 & 0x3F) << 12) |
555 ((c2 & 0x3F) << 6) | (c3 & 0x3F);
556 if (ord < 0x10000 || ord > 0x10FFFF)
557 return -1;
558 return ord;
559 }
560
561 return -1;
562 }
563
564 cst_val *cst_utf8_ord(const cst_val *utf8_char) {
565 const char *ch=(const char *)val_string(utf8_char);
566 return int_val(utf8_ord(ch));
567 }
568 int cst_utf8_ord_string(const char *utf8_char)
569 {
570 return utf8_ord(utf8_char);
571 }
572
573 static int utf8_chr(int ord, char* utf8char)
574 {
575 unsigned int utf8len;
576 int i = 0;
577
578 if (ord < 0x80) {
579 utf8len = 1;
580 } else if (ord < 0x800) {
581 utf8len = 2;
582 } else if (ord <= 0xFFFF) {
583 utf8len = 3;
584 } else if (ord <= 0x200000) {
585 utf8len = 4;
586 } else {
587 /* Replace invalid character with FFFD */
588 utf8len = 2;
589 ord = 0xFFFD;
590 }
591
592 i = utf8len; /* Index into utf8char */
593 utf8char[i--] = 0;
594
595 switch (utf8len) {
596 /* These fallthrough deliberately */
597 case 6:
598 utf8char[i--] = (ord | 0x80) & 0xBF;
599 ord >>= 6;
600 case 5:
601 utf8char[i--] = (ord | 0x80) & 0xBF;
602 ord >>= 6;
603 case 4:
604 utf8char[i--] = (ord | 0x80) & 0xBF;
605 ord >>= 6;
606 case 3:
607 utf8char[i--] = (ord | 0x80) & 0xBF;
608 ord >>= 6;
609 case 2:
610 utf8char[i--] = (ord | 0x80) & 0xBF;
611 ord >>= 6;
612 case 1:
613 switch (utf8len) {
614 case 0:
615 case 1:
616 utf8char[i--] = ord;
617 break;
618 case 2:
619 utf8char[i--] = ord | 0xC0;
620 break;
621 case 3:
622 utf8char[i--] = ord | 0xE0;
623 break;
624 case 4:
625 utf8char[i--] = ord | 0xF0;
626 }
627 }
628 return utf8len;
629 }
630
631 cst_val *cst_utf8_chr(const cst_val *ord) {
632 char ch[5];
633 int utf8len;
634
635 utf8len = utf8_chr(val_int(ord),ch);
636 if (utf8len == 0) {
637 return 0;
638 }
639
640 return string_val(ch);
641 }
642
643 int val_stringp(const cst_val *v)
644 {
645 if (cst_val_consp(v))
646 return FALSE;
647 else if (CST_VAL_TYPE(v) == CST_VAL_TYPE_STRING)
648 return TRUE;
649 else
650 return FALSE;
651 }
652
653 const cst_val *val_assoc_string(const char *v1,const cst_val *al)
654 {
655 const cst_val *i;
656
657 for (i=al; i; i=val_cdr(i))
658 {
659 if (cst_streq(v1,val_string(val_car(val_car(i)))))
660 return val_car(i);
661 }
662 return NULL;
663 }
664
665 cst_string *cst_implode(const cst_val *sl)
666 {
667 const cst_val *v;
668 int l=0;
669 char *s;
670
671 for (v=sl; v; v=val_cdr(v))
672 {
673 if (val_stringp(val_car(v)))
674 l += cst_strlen(val_string(val_car(v)));
675 }
676
677 s = cst_alloc(cst_string,l+1);
678
679 for (v=sl; v; v=val_cdr(v))
680 {
681 if (val_stringp(val_car(v)))
682 cst_sprintf(s,"%s%s",s,val_string(val_car(v)));
683
684 }
685
686 return s;
687 }
688
689 cst_val *val_readlist_string(const char *str)
690 { /* not fully general but a good start */
691 cst_tokenstream *ts;
692 cst_val *v = NULL;
693 const char *p;
694
695 ts = ts_open_string(str,
696 cst_ts_default_whitespacesymbols,
697 "",
698 "",
699 "");
700
701 while (!ts_eof(ts))
702 {
703 p = ts_get(ts);
704 v = cons_val(string_val(p),v);
705 }
706
707 ts_close(ts);
708
709 return val_reverse(v);
710 }
711
712
713