merged lispreader with Ingo Ruhnke's Construo LispReader and LispWriter wrappers.
[supertux.git] / src / lispreader.h
1 /* $Id$ */
2 /*
3  * lispreader.h
4  *
5  * Copyright (C) 1998-2000 Mark Probst
6  * Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifndef __LISPREADER_H__
25 #define __LISPREADER_H__
26
27 #include <stdio.h>
28 #include <vector>
29
30 #define LISP_STREAM_FILE       1
31 #define LISP_STREAM_STRING     2
32 #define LISP_STREAM_ANY        3
33
34 #define LISP_TYPE_INTERNAL      -3
35 #define LISP_TYPE_PARSE_ERROR   -2
36 #define LISP_TYPE_EOF           -1
37 #define LISP_TYPE_NIL           0
38 #define LISP_TYPE_SYMBOL        1
39 #define LISP_TYPE_INTEGER       2
40 #define LISP_TYPE_STRING        3
41 #define LISP_TYPE_REAL          4
42 #define LISP_TYPE_CONS          5
43 #define LISP_TYPE_PATTERN_CONS  6
44 #define LISP_TYPE_BOOLEAN       7
45 #define LISP_TYPE_PATTERN_VAR   8
46
47 #define LISP_PATTERN_ANY        1
48 #define LISP_PATTERN_SYMBOL     2
49 #define LISP_PATTERN_STRING     3
50 #define LISP_PATTERN_INTEGER    4
51 #define LISP_PATTERN_REAL       5
52 #define LISP_PATTERN_BOOLEAN    6
53 #define LISP_PATTERN_LIST       7
54 #define LISP_PATTERN_OR         8
55
56 typedef struct
57   {
58     int type;
59
60     union
61       {
62         FILE *file;
63         struct
64           {
65             char *buf;
66             int pos;
67           }
68         string;
69         struct
70           {
71             void *data;
72             int (*next_char) (void *data);
73             void (*unget_char) (char c, void *data);
74           }
75         any;
76       } v;
77   }
78 lisp_stream_t;
79
80 typedef struct _lisp_object_t lisp_object_t;
81 struct _lisp_object_t
82   {
83     int type;
84
85     union
86       {
87         struct
88           {
89             struct _lisp_object_t *car;
90             struct _lisp_object_t *cdr;
91           }
92         cons;
93
94         char *string;
95         int integer;
96         float real;
97
98         struct
99           {
100             int type;
101             int index;
102             struct _lisp_object_t *sub;
103           }
104         pattern;
105       } v;
106   };
107
108 lisp_stream_t* lisp_stream_init_file (lisp_stream_t *stream, FILE *file);
109 lisp_stream_t* lisp_stream_init_string (lisp_stream_t *stream, char *buf);
110 lisp_stream_t* lisp_stream_init_any (lisp_stream_t *stream, void *data,
111                                      int (*next_char) (void *data),
112                                      void (*unget_char) (char c, void *data));
113
114 lisp_object_t* lisp_read (lisp_stream_t *in);
115 void lisp_free (lisp_object_t *obj);
116
117 lisp_object_t* lisp_read_from_string (const char *buf);
118
119 int lisp_compile_pattern (lisp_object_t **obj, int *num_subs);
120 int lisp_match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars, int num_subs);
121 int lisp_match_string (const char *pattern_string, lisp_object_t *obj, lisp_object_t **vars);
122
123 int lisp_type (lisp_object_t *obj);
124 int lisp_integer (lisp_object_t *obj);
125 float lisp_real (lisp_object_t *obj);
126 char* lisp_symbol (lisp_object_t *obj);
127 char* lisp_string (lisp_object_t *obj);
128 int lisp_boolean (lisp_object_t *obj);
129 lisp_object_t* lisp_car (lisp_object_t *obj);
130 lisp_object_t* lisp_cdr (lisp_object_t *obj);
131
132 lisp_object_t* lisp_cxr (lisp_object_t *obj, const char *x);
133
134 lisp_object_t* lisp_make_integer (int value);
135 lisp_object_t* lisp_make_real (float value);
136 lisp_object_t* lisp_make_symbol (const char *value);
137 lisp_object_t* lisp_make_string (const char *value);
138 lisp_object_t* lisp_make_cons (lisp_object_t *car, lisp_object_t *cdr);
139 lisp_object_t* lisp_make_boolean (int value);
140
141 int lisp_list_length (lisp_object_t *obj);
142 lisp_object_t* lisp_list_nth_cdr (lisp_object_t *obj, int index);
143 lisp_object_t* lisp_list_nth (lisp_object_t *obj, int index);
144
145 void lisp_dump (lisp_object_t *obj, FILE *out);
146
147 #define lisp_nil()           ((lisp_object_t*)0)
148
149 #define lisp_nil_p(obj)      (obj == 0)
150 #define lisp_integer_p(obj)  (lisp_type((obj)) == LISP_TYPE_INTEGER)
151 #define lisp_real_p(obj)     (lisp_type((obj)) == LISP_TYPE_REAL)
152 #define lisp_symbol_p(obj)   (lisp_type((obj)) == LISP_TYPE_SYMBOL)
153 #define lisp_string_p(obj)   (lisp_type((obj)) == LISP_TYPE_STRING)
154 #define lisp_cons_p(obj)     (lisp_type((obj)) == LISP_TYPE_CONS)
155 #define lisp_boolean_p(obj)  (lisp_type((obj)) == LISP_TYPE_BOOLEAN)
156
157 /** */
158 class LispReader
159   {
160   private:
161     lisp_object_t* lst;
162
163     lisp_object_t* search_for(const char* name);
164   public:
165     /** cur == ((pos 1 2 3) (id 12 3 4)...) */
166     LispReader (lisp_object_t* l);
167
168     bool read_int (const char* name, int* i);
169     bool read_float (const char* name, float* f);
170     bool read_bool (const char* name, bool* b);
171   };
172
173 /** */
174 class LispWriter
175   {
176   private:
177     std::vector<lisp_object_t*> lisp_objs;
178
179     void append (lisp_object_t* obj);
180     lisp_object_t* make_list3 (lisp_object_t*, lisp_object_t*, lisp_object_t*);
181     lisp_object_t* make_list2 (lisp_object_t*, lisp_object_t*);
182   public:
183     LispWriter (const char* name);
184     void write_float (const char* name, float f);
185     void write_int (const char* name, int i);
186     void write_boolean (const char* name, bool b);
187     void write_string (const char* name, const char* str);
188     void write_symbol (const char* name, const char* symname);
189     void write_lisp_obj(const char* name, lisp_object_t* lst);
190
191     /** caller is responible to free the returned lisp_object_t */
192     lisp_object_t* create_lisp ();
193   };
194
195 #endif