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