| 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 (awb@cs.cmu.edu)                    */ | 
| 34 | /*               Date:  December 1999                                    */ | 
| 35 | /*************************************************************************/ | 
| 36 | /*                                                                       */ | 
| 37 | /*  Vals, typed objects                                                  */ | 
| 38 | /*                                                                       */ | 
| 39 | /*************************************************************************/ | 
| 40 | #ifndef _CST_VAL_H__ | 
| 41 | #define _CST_VAL_H__ | 
| 42 |  | 
| 43 | #include "cst_file.h" | 
| 44 | #include "cst_string.h" | 
| 45 | #include "cst_error.h" | 
| 46 | #include "cst_alloc.h" | 
| 47 | #include "cst_val_defs.h" | 
| 48 |  | 
| 49 | #include <stdint.h> | 
| 50 |  | 
| 51 | /* Only CONS can be an even number */ | 
| 52 | #define CST_VAL_TYPE_CONS    0 | 
| 53 | #define CST_VAL_TYPE_INT     1 | 
| 54 | #define CST_VAL_TYPE_FLOAT   3 | 
| 55 | #define CST_VAL_TYPE_STRING  5 | 
| 56 | #define CST_VAL_TYPE_FIRST_FREE 7 | 
| 57 | #define CST_VAL_TYPE_MAX     54 | 
| 58 |  | 
| 59 | typedef struct  cst_val_cons_struct { | 
| 60 |     struct cst_val_struct *car; | 
| 61 |     struct cst_val_struct *cdr; | 
| 62 | }  cst_val_cons; | 
| 63 |  | 
| 64 | typedef struct  cst_val_atom_struct { | 
| 65 | #ifdef WORDS_BIGENDIAN | 
| 66 | #if UINTPTR_MAX > 0xfffffffful | 
| 67 |     int ref_count; | 
| 68 |     int type;  /* order is here important */ | 
| 69 | #else | 
| 70 |     short ref_count; | 
| 71 |     short type;  /* order is here important */ | 
| 72 | #endif | 
| 73 | #else | 
| 74 | #if UINTPTR_MAX > 0xfffffffful | 
| 75 |     int type;  /* order is here important */ | 
| 76 |     int ref_count; | 
| 77 | #else | 
| 78 |     short type;  /* order is here important */ | 
| 79 |     short ref_count; | 
| 80 | #endif | 
| 81 | #endif | 
| 82 |     union  | 
| 83 |     { | 
| 84 | #if UINTPTR_MAX > 0xfffffffful | 
| 85 |         double fval; | 
| 86 |         long long ival; | 
| 87 |         void *vval; | 
| 88 | #else | 
| 89 |         float fval; | 
| 90 |         int ival; | 
| 91 |         void *vval; | 
| 92 | #endif | 
| 93 |     } v; | 
| 94 | } cst_val_atom; | 
| 95 |  | 
| 96 | typedef struct  cst_val_struct { | 
| 97 |     union | 
| 98 |     { | 
| 99 | 	cst_val_cons cc; | 
| 100 | 	cst_val_atom a; | 
| 101 |     } c; | 
| 102 | } cst_val; | 
| 103 |  | 
| 104 | typedef struct cst_val_def_struct { | 
| 105 |     const char *name; | 
| 106 |     void (*delete_function)(void *); | 
| 107 | } cst_val_def; | 
| 108 |  | 
| 109 | /* Constructor functions */ | 
| 110 | cst_val *int_val(int i); | 
| 111 | cst_val *float_val(float f); | 
| 112 | cst_val *string_val(const char *s); | 
| 113 | cst_val *val_new_typed(int type, void *vv); | 
| 114 | cst_val *cons_val(const cst_val *a, const cst_val *b); | 
| 115 |  | 
| 116 | /* Derefence and delete val if no other references */ | 
| 117 | void delete_val(cst_val *val); | 
| 118 | void delete_val_list(cst_val *val); | 
| 119 |  | 
| 120 | /* Accessor functions */ | 
| 121 | int val_int(const cst_val *v); | 
| 122 | float val_float(const cst_val *v); | 
| 123 | const char *val_string(const cst_val *v); | 
| 124 | void *val_void(const cst_val *v); | 
| 125 | void *val_generic(const cst_val *v, int type, const char *stype); | 
| 126 | const cst_val *val_car(const cst_val *v); | 
| 127 | const cst_val *val_cdr(const cst_val *v); | 
| 128 |  | 
| 129 | const cst_val *set_cdr(cst_val *v1, const cst_val *v2); | 
| 130 | const cst_val *set_car(cst_val *v1, const cst_val *v2); | 
| 131 |  | 
| 132 | int cst_val_consp(const cst_val *v); | 
| 133 |  | 
| 134 | /* Unsafe accessor function -- for the brave and foolish */ | 
| 135 | #define CST_VAL_STRING_LVAL(X) ((X)->c.a.v.vval) | 
| 136 | #define CST_VAL_TYPE(X) ((X)->c.a.type) | 
| 137 | #define CST_VAL_INT(X) ((X)->c.a.v.ival) | 
| 138 | #define CST_VAL_FLOAT(X) ((X)->c.a.v.fval) | 
| 139 | #define CST_VAL_STRING(X) ((const char *)(CST_VAL_STRING_LVAL(X))) | 
| 140 | #define CST_VAL_VOID(X) ((X)->c.a.v.vval) | 
| 141 | #define CST_VAL_CAR(X) ((X)->c.cc.car) | 
| 142 | #define CST_VAL_CDR(X) ((X)->c.cc.cdr) | 
| 143 |  | 
| 144 | #define CST_VAL_REFCOUNT(X) ((X)->c.a.ref_count) | 
| 145 |  | 
| 146 | /* Some standard function */ | 
| 147 | int val_equal(const cst_val *a, const cst_val *b); | 
| 148 | int val_less(const cst_val *a, const cst_val *b); | 
| 149 | int val_greater(const cst_val *a, const cst_val *b); | 
| 150 | int val_member(const cst_val *a, const cst_val *b); | 
| 151 | int val_member_string (const char *a, const cst_val *b); | 
| 152 | int val_stringp(const cst_val *a); | 
| 153 | const cst_val *val_assoc_string(const char *v1,const cst_val *al); | 
| 154 |  | 
| 155 | void val_print(cst_file fd,const cst_val *v); | 
| 156 | cst_val *val_readlist_string(const char *str); | 
| 157 |  | 
| 158 | cst_val *val_reverse(cst_val *v); | 
| 159 | cst_val *val_append(cst_val *a,cst_val *b); /* destructive */ | 
| 160 | int val_length(const cst_val *l); | 
| 161 | cst_val *cst_utf8_explode(const cst_string *utf8string); | 
| 162 | cst_string *cst_implode(const cst_val *string_list); | 
| 163 |  | 
| 164 | cst_val *cst_utf8_ord(const cst_val *utf8_char); | 
| 165 | cst_val *cst_utf8_chr(const cst_val *ord); | 
| 166 |  | 
| 167 | int cst_utf8_ord_string(const char *utf8_char); | 
| 168 |  | 
| 169 | /* make sure you know what you are doing before you call these */ | 
| 170 | int val_dec_refcount(const cst_val *b); | 
| 171 | cst_val *val_inc_refcount(const cst_val *b); | 
| 172 |  | 
| 173 | #include "cst_val_const.h" | 
| 174 | extern const cst_val_def cst_val_defs[]; | 
| 175 |  | 
| 176 | /* Generic pointer vals */ | 
| 177 | typedef void cst_userdata; | 
| 178 | CST_VAL_USER_TYPE_DCLS(userdata,cst_userdata) | 
| 179 |  | 
| 180 | #endif | 
| 181 |  |