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
59typedef struct cst_val_cons_struct {
60 struct cst_val_struct *car;
61 struct cst_val_struct *cdr;
62} cst_val_cons;
63
64typedef 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
96typedef struct cst_val_struct {
97 union
98 {
99 cst_val_cons cc;
100 cst_val_atom a;
101 } c;
102} cst_val;
103
104typedef struct cst_val_def_struct {
105 const char *name;
106 void (*delete_function)(void *);
107} cst_val_def;
108
109/* Constructor functions */
110cst_val *int_val(int i);
111cst_val *float_val(float f);
112cst_val *string_val(const char *s);
113cst_val *val_new_typed(int type, void *vv);
114cst_val *cons_val(const cst_val *a, const cst_val *b);
115
116/* Derefence and delete val if no other references */
117void delete_val(cst_val *val);
118void delete_val_list(cst_val *val);
119
120/* Accessor functions */
121int val_int(const cst_val *v);
122float val_float(const cst_val *v);
123const char *val_string(const cst_val *v);
124void *val_void(const cst_val *v);
125void *val_generic(const cst_val *v, int type, const char *stype);
126const cst_val *val_car(const cst_val *v);
127const cst_val *val_cdr(const cst_val *v);
128
129const cst_val *set_cdr(cst_val *v1, const cst_val *v2);
130const cst_val *set_car(cst_val *v1, const cst_val *v2);
131
132int 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 */
147int val_equal(const cst_val *a, const cst_val *b);
148int val_less(const cst_val *a, const cst_val *b);
149int val_greater(const cst_val *a, const cst_val *b);
150int val_member(const cst_val *a, const cst_val *b);
151int val_member_string (const char *a, const cst_val *b);
152int val_stringp(const cst_val *a);
153const cst_val *val_assoc_string(const char *v1,const cst_val *al);
154
155void val_print(cst_file fd,const cst_val *v);
156cst_val *val_readlist_string(const char *str);
157
158cst_val *val_reverse(cst_val *v);
159cst_val *val_append(cst_val *a,cst_val *b); /* destructive */
160int val_length(const cst_val *l);
161cst_val *cst_utf8_explode(const cst_string *utf8string);
162cst_string *cst_implode(const cst_val *string_list);
163
164cst_val *cst_utf8_ord(const cst_val *utf8_char);
165cst_val *cst_utf8_chr(const cst_val *ord);
166
167int cst_utf8_ord_string(const char *utf8_char);
168
169/* make sure you know what you are doing before you call these */
170int val_dec_refcount(const cst_val *b);
171cst_val *val_inc_refcount(const cst_val *b);
172
173#include "cst_val_const.h"
174extern const cst_val_def cst_val_defs[];
175
176/* Generic pointer vals */
177typedef void cst_userdata;
178CST_VAL_USER_TYPE_DCLS(userdata,cst_userdata)
179
180#endif
181

source code of include/flite/cst_val.h