ia64/xen-unstable

view tools/xfrd/sxpr.h @ 2422:2274a0386cc9

bitkeeper revision 1.1159.69.5 (4138e882jA1YaR_OfTfNHe_uT4PDIg)

trivial
author iap10@labyrinth.cl.cam.ac.uk
date Fri Sep 03 21:56:18 2004 +0000 (2004-09-03)
parents 0e23f01219c6
children
line source
1 /*
2 *
3 * This library is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU Lesser General Public License as
5 * published by the Free Software Foundation; either version 2.1 of the
6 * License, or (at your option) any later version. This library is
7 * distributed in the hope that it will be useful, but WITHOUT ANY
8 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
9 * FITNESS FOR A PARTICULAR PURPOSE.
10 * See the GNU Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this library; if not, write to the Free Software Foundation,
14 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16 #ifndef _XUTIL_SXPR_H_
17 #define _XUTIL_SXPR_H_
19 #include <stdint.h>
21 #include "hash_table.h"
22 #include "iostream.h"
23 #include "allocate.h"
25 /** @file
26 * Definitions for rules and sxprs.
27 */
29 #ifndef NULL
30 #define NULL 0
31 #endif
33 #ifndef TRUE
34 #define TRUE 1
35 #endif
37 #ifndef FALSE
38 #define FALSE 0
39 #endif
41 /** Sxpr type. */
42 typedef int16_t TypeCode;
44 /** A typed sxpr handle.*/
45 typedef struct Sxpr {
46 /** Sxpr type. */
47 TypeCode type;
48 union {
49 /** Sxpr value. */
50 unsigned long ul;
51 /** Pointer. */
52 void *ptr;
53 } v;
54 } Sxpr;
56 /** Sxpr type to indicate out of memory. */
57 #define T_NOMEM ((TypeCode)-1)
58 /** The 'unspecified' sxpr. */
59 #define T_NONE ((TypeCode)0)
60 /** The empty list. */
61 #define T_NULL ((TypeCode)1)
62 /** Unsigned integer. */
63 #define T_UINT ((TypeCode)2)
64 /** A string. */
65 #define T_STRING ((TypeCode)3)
66 /** An atom. */
67 #define T_ATOM ((TypeCode)4)
68 /** A boolean. */
69 #define T_BOOL ((TypeCode)5)
71 /** A cons (pair or list). */
72 #define T_CONS ((TypeCode)10)
74 /** An error. */
75 #define T_ERR ((TypeCode)40)
77 /** An atom. */
78 typedef struct ObjAtom {
79 Sxpr name;
80 Hashcode hashcode;
81 int interned;
82 } ObjAtom;
84 /** A cons (pair). */
85 typedef struct ObjCons {
86 Sxpr car;
87 Sxpr cdr;
88 } ObjCons;
90 /** A vector. */
91 typedef struct ObjVector {
92 int n;
93 Sxpr data[0];
94 } ObjVector;
96 /** Flags for sxpr printing. */
97 enum PrintFlags {
98 PRINT_RAW = 0x001,
99 PRINT_TYPE = 0x002,
100 PRINT_PRETTY = 0x004,
101 PRINT_NUM = 0x008,
102 };
104 /** An integer sxpr.
105 *
106 * @param ty type
107 * @param val integer value
108 */
109 #define OBJI(ty, val) (Sxpr){ type: (ty), v: { ul: (val) }}
111 /** A pointer sxpr.
112 * If the pointer is non-null, returns an sxpr containing it.
113 * If the pointer is null, returns ONOMEM.
114 *
115 * @param ty type
116 * @param val pointer
117 */
118 #define OBJP(ty, val) ((val) ? (Sxpr){ type: (ty), v: { ptr: (val) }} : ONOMEM)
120 /** Make an integer sxpr containing a pointer.
121 *
122 * @param val pointer
123 */
124 #define PTR(val) OBJP(T_UINT, (void*)(val))
126 /** Make an integer sxpr.
127 * @param x value
128 */
129 #define OINT(x) OBJI(T_UINT, x)
131 /** Make an error sxpr.
132 *
133 * @param x value
134 */
135 #define OERR(x) OBJI(T_ERR, x)
137 /** Out of memory constant. */
138 #define ONOMEM OBJI(T_NOMEM, 0)
140 /** The `unspecified' constant. */
141 #define ONONE OBJI(T_NONE, 0)
143 /** Empty list constant. */
144 #define ONULL OBJI(T_NULL, 0)
146 /** False constant. */
147 #define OFALSE OBJI(T_BOOL, 0)
149 /** True constant. */
150 #define OTRUE OBJI(T_BOOL, 1)
152 /* Recognizers for the various sxpr types. */
153 #define ATOMP(obj) has_type(obj, T_ATOM)
154 #define BOOLP(obj) has_type(obj, T_BOOL)
155 #define CONSP(obj) has_type(obj, T_CONS)
156 #define ERRP(obj) has_type(obj, T_ERR)
157 #define INTP(obj) has_type(obj, T_UINT)
158 #define NOMEMP(obj) has_type(obj, T_NOMEM)
159 #define NONEP(obj) has_type(obj, T_NONE)
160 #define NULLP(obj) has_type(obj, T_NULL)
161 #define STRINGP(obj) has_type(obj, T_STRING)
163 #define TRUEP(obj) get_ul(obj)
165 /** Convert an sxpr to an unsigned integer. */
166 #define OBJ_UINT(x) get_ul(x)
167 /** Convert an sxpr to an integer. */
168 #define OBJ_INT(x) (int)get_ul(x)
170 /* Conversions of sxprs to their values.
171 * No checking is done.
172 */
173 #define OBJ_STRING(x) ((char*)get_ptr(x))
174 #define OBJ_CONS(x) ((ObjCons*)get_ptr(x))
175 #define OBJ_ATOM(x) ((ObjAtom*)get_ptr(x))
176 #define OBJ_SET(x) ((ObjSet*)get_ptr(x))
177 #define CAR(x) (OBJ_CONS(x)->car)
178 #define CDR(x) (OBJ_CONS(x)->cdr)
180 #define CAAR(x) (CAR(CAR(x)))
181 #define CADR(x) (CAR(CDR(x)))
182 #define CDAR(x) (CDR(CAR(x)))
183 #define CDDR(x) (CDR(CDR(x)))
185 /** Get the integer value from an sxpr.
186 *
187 * @param obj sxpr
188 * @return value
189 */
190 static inline unsigned long get_ul(Sxpr obj){
191 return obj.v.ul;
192 }
194 /** Get the pointer value from an sxpr.
195 *
196 * @param obj sxpr
197 * @return value
198 */
199 static inline void * get_ptr(Sxpr obj){
200 return obj.v.ptr;
201 }
203 /** Create an sxpr containing a pointer.
204 *
205 * @param type typecode
206 * @param val pointer
207 * @return sxpr
208 */
209 static inline Sxpr obj_ptr(TypeCode type, void *val){
210 return (Sxpr){ type: type, v: { ptr: val } };
211 }
213 /** Create an sxpr containing an integer.
214 *
215 * @param type typecode
216 * @param val integer
217 * @return sxpr
218 */
219 static inline Sxpr obj_ul(TypeCode type, unsigned long val){
220 return (Sxpr){ type: type, v: { ul: val } };
221 }
223 /** Get the type of an sxpr.
224 *
225 * @param obj sxpr
226 * @return type
227 */
228 static inline TypeCode get_type(Sxpr obj){
229 return obj.type;
230 }
232 /** Check the type of an sxpr.
233 *
234 * @param obj sxpr
235 * @param type to check
236 * @return 1 if has the type, 0 otherwise
237 */
238 static inline int has_type(Sxpr obj, TypeCode type){
239 return get_type(obj) == type;
240 }
242 /** Compare sxprs for literal equality of type and value.
243 *
244 * @param x sxpr to compare
245 * @param y sxpr to compare
246 * @return 1 if equal, 0 otherwise
247 */
248 static inline int eq(Sxpr x, Sxpr y){
249 return ((get_type(x) == get_type(y)) && (get_ul(x) == get_ul(y)));
250 }
252 /** Checked version of CAR
253 *
254 * @param x sxpr
255 * @return CAR if a cons, x otherwise
256 */
257 static inline Sxpr car(Sxpr x){
258 return (CONSP(x) ? CAR(x) : x);
259 }
261 /** Checked version of CDR.
262 *
263 * @param x sxpr
264 * @return CDR if a cons, null otherwise
265 */
266 static inline Sxpr cdr(Sxpr x){
267 return (CONSP(x) ? CDR(x) : ONULL);
268 }
270 /** Allocate some memory and return an sxpr containing it.
271 * Returns ONOMEM if allocation failed.
272 *
273 * @param n number of bytes to allocate
274 * @param ty typecode
275 * @return sxpr
276 */
277 static inline Sxpr halloc(size_t n, TypeCode ty){
278 return OBJP(ty, allocate(n));
279 }
281 /** Allocate an sxpr containing a pointer to the given type.
282 *
283 * @param ty type (uses sizeof to determine how many bytes to allocate)
284 * @param code typecode
285 * @return sxpr, ONOMEM if allocation failed
286 */
287 #define HALLOC(ty, code) halloc(sizeof(ty), code)
289 typedef int ObjPrintFn(IOStream *io, Sxpr obj, unsigned flags);
290 typedef int ObjEqualFn(Sxpr obj, Sxpr other);
291 typedef void ObjFreeFn(Sxpr obj);
293 /** An sxpr type definition. */
294 typedef struct SxprType {
295 TypeCode type;
296 char *name;
297 int pointer;
298 ObjPrintFn *print;
299 ObjEqualFn *equal;
300 ObjFreeFn *free;
301 } SxprType;
304 extern SxprType *get_sxpr_type(int ty);
306 /** Free the pointer in an sxpr.
307 *
308 * @param x sxpr containing a pointer
309 */
310 static inline void hfree(Sxpr x){
311 deallocate(get_ptr(x));
312 }
314 extern int objprint(IOStream *io, Sxpr x, unsigned flags);
315 extern int objequal(Sxpr x, Sxpr y);
316 extern void objfree(Sxpr x);
318 extern void cons_free_cells(Sxpr obj);
319 extern Sxpr intern(char *s);
321 extern Sxpr assoc(Sxpr k, Sxpr l);
322 extern Sxpr assocq(Sxpr k, Sxpr l);
323 extern Sxpr acons(Sxpr k, Sxpr v, Sxpr l);
324 extern Sxpr nrev(Sxpr l);
325 extern Sxpr cons_member(Sxpr l, Sxpr x);
326 extern Sxpr cons_member_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v);
327 extern int cons_subset(Sxpr s, Sxpr t);
328 extern int cons_set_equal(Sxpr s, Sxpr t);
330 #ifdef USE_GC
331 extern Sxpr cons_remove(Sxpr l, Sxpr x);
332 extern Sxpr cons_remove_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v);
333 #endif
335 extern Sxpr atom_new(char *name);
336 extern char * atom_name(Sxpr obj);
338 extern Sxpr string_new(char *s);
339 extern char * string_string(Sxpr obj);
340 extern int string_length(Sxpr obj);
342 extern Sxpr cons_new(Sxpr car, Sxpr cdr);
343 extern int cons_push(Sxpr *list, Sxpr elt);
344 extern int cons_length(Sxpr obj);
346 Sxpr sxpr_name(Sxpr obj);
347 int sxpr_is(Sxpr obj, char *s);
348 int sxpr_elementp(Sxpr obj, Sxpr name);
349 Sxpr sxpr_attributes(Sxpr obj);
350 Sxpr sxpr_attribute(Sxpr obj, Sxpr key, Sxpr def);
351 Sxpr sxpr_children(Sxpr obj);
352 Sxpr sxpr_child(Sxpr obj, Sxpr name, Sxpr def);
353 Sxpr sxpr_childN(Sxpr obj, int n, Sxpr def);
354 Sxpr sxpr_child0(Sxpr obj, Sxpr def);
355 Sxpr sxpr_child_value(Sxpr obj, Sxpr name, Sxpr def);
357 /** Create a new atom.
358 *
359 * @param s atom name
360 * @return new atom
361 */
362 static inline Sxpr mkatom(char *s){
363 return atom_new(s);
364 }
366 /** Create a new string sxpr.
367 *
368 * @param s string bytes (copied)
369 * @return new string
370 */
371 static inline Sxpr mkstring(char *s){
372 return string_new(s);
373 }
375 /** Create an integer sxpr.
376 *
377 * @param i value
378 * @return sxpr
379 */
380 static inline Sxpr mkint(int i){
381 return OBJI(T_UINT, i);
382 }
384 /** Create a boolean sxpr.
385 *
386 * @param b value
387 * @return sxpr
388 */
389 static inline Sxpr mkbool(int b){
390 return OBJI(T_BOOL, (b ? 1 : 0));
391 }
393 /* Constants used in parsing and printing. */
394 #define k_list_open "("
395 #define c_list_open '('
396 #define k_list_close ")"
397 #define c_list_close ')'
398 #define k_true "true"
399 #define k_false "false"
401 #define c_var '$'
402 #define c_escape '\\'
403 #define c_single_quote '\''
404 #define c_double_quote '"'
405 #define c_string_open c_double_quote
406 #define c_string_close c_double_quote
407 #define c_data_open '['
408 #define c_data_close ']'
409 #define c_binary '*'
410 #define c_eval '!'
411 #define c_concat_open '{'
412 #define c_concat_close '}'
414 #endif /* ! _XUTIL_SXPR_H_ */