1 /* $Id: lispreader.hpp 152 2007-06-07 09:35:21Z grumbel $ */
2 /*
3  * lispreader.h
4  *
5  * Copyright (C) 1998-2000 Mark Probst
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
23 #ifndef __LISPREADER_H__
24 #define __LISPREADER_H__
25 
26 #include <stdio.h>
27 
28 #define LISP_STREAM_FILE       1
29 #define LISP_STREAM_STRING     2
30 #define LISP_STREAM_ANY        3
31 
32 #define LISP_TYPE_INTERNAL      -3
33 #define LISP_TYPE_PARSE_ERROR   -2
34 #define LISP_TYPE_EOF           -1
35 #define LISP_TYPE_NIL           0
36 #define LISP_TYPE_SYMBOL        1
37 #define LISP_TYPE_INTEGER       2
38 #define LISP_TYPE_STRING        3
39 #define LISP_TYPE_REAL          4
40 #define LISP_TYPE_CONS          5
41 #define LISP_TYPE_PATTERN_CONS  6
42 #define LISP_TYPE_BOOLEAN       7
43 #define LISP_TYPE_PATTERN_VAR   8
44 
45 #define LISP_PATTERN_ANY        1
46 #define LISP_PATTERN_SYMBOL     2
47 #define LISP_PATTERN_STRING     3
48 #define LISP_PATTERN_INTEGER    4
49 #define LISP_PATTERN_REAL       5
50 #define LISP_PATTERN_BOOLEAN    6
51 #define LISP_PATTERN_LIST       7
52 #define LISP_PATTERN_OR         8
53 
54 typedef struct
55 {
56     int type;
57 
58     union
59     {
60 	FILE *file;
61 	struct
62 	{
63 	    char *buf;
64 	    int pos;
65 	} string;
66         struct
67 	{
68 	    void *data;
69 	    int (*next_char) (void *data);
70 	    void (*unget_char) (char c, void *data);
71 	} any;
72     } v;
73 } lisp_stream_t;
74 
75 typedef struct _lisp_object_t lisp_object_t;
76 struct _lisp_object_t
77 {
78     int type;
79 
80     union
81     {
82 	struct
83 	{
84 	    struct _lisp_object_t *car;
85 	    struct _lisp_object_t *cdr;
86 	} cons;
87 
88 	char *string;
89 	int integer;
90 	float real;
91 
92 	struct
93 	{
94 	    int type;
95 	    int index;
96 	    struct _lisp_object_t *sub;
97 	} pattern;
98     } v;
99 };
100 
101 lisp_stream_t* lisp_stream_init_file (lisp_stream_t *stream, FILE *file);
102 lisp_stream_t* lisp_stream_init_string (lisp_stream_t *stream, char *buf);
103 lisp_stream_t* lisp_stream_init_any (lisp_stream_t *stream, void *data,
104 				     int (*next_char) (void *data),
105 				     void (*unget_char) (char c, void *data));
106 
107 lisp_object_t* lisp_read (lisp_stream_t *in);
108 void lisp_free (lisp_object_t *obj);
109 
110 lisp_object_t* lisp_read_from_string (const char *buf);
111 
112 int lisp_compile_pattern (lisp_object_t **obj, int *num_subs);
113 int lisp_match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars, int num_subs);
114 int lisp_match_string (const char *pattern_string, lisp_object_t *obj, lisp_object_t **vars);
115 
116 int lisp_type (lisp_object_t *obj);
117 int lisp_integer (lisp_object_t *obj);
118 float lisp_real (lisp_object_t *obj);
119 char* lisp_symbol (lisp_object_t *obj);
120 char* lisp_string (lisp_object_t *obj);
121 int lisp_boolean (lisp_object_t *obj);
122 lisp_object_t* lisp_car (lisp_object_t *obj);
123 lisp_object_t* lisp_cdr (lisp_object_t *obj);
124 
125 lisp_object_t* lisp_cxr (lisp_object_t *obj, const char *x);
126 
127 lisp_object_t* lisp_make_integer (int value);
128 lisp_object_t* lisp_make_real (float value);
129 lisp_object_t* lisp_make_symbol (const char *value);
130 lisp_object_t* lisp_make_string (const char *value);
131 lisp_object_t* lisp_make_cons (lisp_object_t *car, lisp_object_t *cdr);
132 lisp_object_t* lisp_make_boolean (int value);
133 
134 int lisp_list_length (lisp_object_t *obj);
135 lisp_object_t* lisp_list_nth_cdr (lisp_object_t *obj, int index);
136 lisp_object_t* lisp_list_nth (lisp_object_t *obj, int index);
137 
138 void lisp_dump (lisp_object_t *obj, FILE *out);
139 
140 #define lisp_nil()           ((lisp_object_t*)0)
141 
142 #define lisp_nil_p(obj)      (obj == 0)
143 #define lisp_integer_p(obj)  (lisp_type((obj)) == LISP_TYPE_INTEGER)
144 #define lisp_real_p(obj)     (lisp_type((obj)) == LISP_TYPE_REAL)
145 #define lisp_symbol_p(obj)   (lisp_type((obj)) == LISP_TYPE_SYMBOL)
146 #define lisp_string_p(obj)   (lisp_type((obj)) == LISP_TYPE_STRING)
147 #define lisp_cons_p(obj)     (lisp_type((obj)) == LISP_TYPE_CONS)
148 #define lisp_boolean_p(obj)  (lisp_type((obj)) == LISP_TYPE_BOOLEAN)
149 
150 #endif
151