- fixed vscroll a bit
[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 <zlib.h>
29 #include <string>
30 #include <vector>
31 #include <exception>
32 #include "exceptions.h"
33
34 #define LISP_STREAM_FILE       1
35 #define LISP_STREAM_STRING     2
36 #define LISP_STREAM_ANY        3
37
38 #define LISP_TYPE_INTERNAL      -3
39 #define LISP_TYPE_PARSE_ERROR   -2
40 #define LISP_TYPE_EOF           -1
41 #define LISP_TYPE_NIL           0
42 #define LISP_TYPE_SYMBOL        1
43 #define LISP_TYPE_INTEGER       2
44 #define LISP_TYPE_STRING        3
45 #define LISP_TYPE_REAL          4
46 #define LISP_TYPE_CONS          5
47 #define LISP_TYPE_PATTERN_CONS  6
48 #define LISP_TYPE_BOOLEAN       7
49 #define LISP_TYPE_PATTERN_VAR   8
50
51 #define LISP_PATTERN_ANY        1
52 #define LISP_PATTERN_SYMBOL     2
53 #define LISP_PATTERN_STRING     3
54 #define LISP_PATTERN_INTEGER    4
55 #define LISP_PATTERN_REAL       5
56 #define LISP_PATTERN_BOOLEAN    6
57 #define LISP_PATTERN_LIST       7
58 #define LISP_PATTERN_OR         8
59
60 // Exception
61 class LispReaderException : public SuperTuxException
62 {
63   public:
64     LispReaderException(const char* _message = "lispreader error", const char* _file = "", const unsigned int _line = 0)
65       : SuperTuxException(_message, _file, _line) { };
66 };
67
68 typedef struct
69   {
70     int type;
71
72     union
73       {
74         FILE *file;
75         struct
76           {
77             char *buf;
78             int pos;
79           }
80         string;
81         struct
82           {
83             void *data;
84             int (*next_char) (void *data);
85             void (*unget_char) (char c, void *data);
86           }
87         any;
88       } v;
89   }
90 lisp_stream_t;
91
92 typedef struct _lisp_object_t lisp_object_t;
93 struct _lisp_object_t
94   {
95     int type;
96
97     union
98       {
99         struct
100           {
101             struct _lisp_object_t *car;
102             struct _lisp_object_t *cdr;
103           }
104         cons;
105
106         char *string;
107         int integer;
108         float real;
109
110         struct
111           {
112             int type;
113             int index;
114             struct _lisp_object_t *sub;
115           }
116         pattern;
117       } v;
118   };
119
120 lisp_stream_t* lisp_stream_init_gzfile (lisp_stream_t *stream, gzFile file);
121 lisp_stream_t* lisp_stream_init_file (lisp_stream_t *stream, FILE *file);
122 lisp_stream_t* lisp_stream_init_string (lisp_stream_t *stream, char *buf);
123 lisp_stream_t* lisp_stream_init_any (lisp_stream_t *stream, void *data,
124                                      int (*next_char) (void *data),
125                                      void (*unget_char) (char c, void *data));
126
127 lisp_object_t* lisp_read (lisp_stream_t *in);
128 lisp_object_t* lisp_read_from_file(const std::string& filename);
129 void lisp_free (lisp_object_t *obj);
130
131 lisp_object_t* lisp_read_from_string (const char *buf);
132
133 int lisp_compile_pattern (lisp_object_t **obj, int *num_subs);
134 int lisp_match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars, int num_subs);
135 int lisp_match_string (const char *pattern_string, lisp_object_t *obj, lisp_object_t **vars);
136
137 int lisp_type (lisp_object_t *obj);
138 int lisp_integer (lisp_object_t *obj);
139 float lisp_real (lisp_object_t *obj);
140 char* lisp_symbol (lisp_object_t *obj);
141 char* lisp_string (lisp_object_t *obj);
142 int lisp_boolean (lisp_object_t *obj);
143 lisp_object_t* lisp_car (lisp_object_t *obj);
144 lisp_object_t* lisp_cdr (lisp_object_t *obj);
145
146 lisp_object_t* lisp_cxr (lisp_object_t *obj, const char *x);
147
148 lisp_object_t* lisp_make_integer (int value);
149 lisp_object_t* lisp_make_real (float value);
150 lisp_object_t* lisp_make_symbol (const char *value);
151 lisp_object_t* lisp_make_string (const char *value);
152 lisp_object_t* lisp_make_cons (lisp_object_t *car, lisp_object_t *cdr);
153 lisp_object_t* lisp_make_boolean (int value);
154
155 int lisp_list_length (lisp_object_t *obj);
156 lisp_object_t* lisp_list_nth_cdr (lisp_object_t *obj, int index);
157 lisp_object_t* lisp_list_nth (lisp_object_t *obj, int index);
158
159 void lisp_dump (lisp_object_t *obj, FILE *out);
160
161 #define lisp_nil()           ((lisp_object_t*)0)
162
163 #define lisp_nil_p(obj)      (obj == 0)
164 #define lisp_integer_p(obj)  (lisp_type((obj)) == LISP_TYPE_INTEGER)
165 #define lisp_real_p(obj)     (lisp_type((obj)) == LISP_TYPE_REAL)
166 #define lisp_symbol_p(obj)   (lisp_type((obj)) == LISP_TYPE_SYMBOL)
167 #define lisp_string_p(obj)   (lisp_type((obj)) == LISP_TYPE_STRING)
168 #define lisp_cons_p(obj)     (lisp_type((obj)) == LISP_TYPE_CONS)
169 #define lisp_boolean_p(obj)  (lisp_type((obj)) == LISP_TYPE_BOOLEAN)
170
171 /** */
172 class LispReader
173   {
174   private:
175     lisp_object_t* lst;
176
177     lisp_object_t* search_for(const char* name);
178   public:
179     /** cur == ((pos 1 2 3) (id 12 3 4)...) */
180     LispReader (lisp_object_t* l);
181
182     bool read_int_vector (const char* name, std::vector<int>* vec);
183     bool read_char_vector (const char* name, std::vector<char>* vec);
184     bool read_string_vector (const char* name, std::vector<std::string>* vec);
185     bool read_string (const char* name, std::string* str);
186     bool read_int (const char* name, int* i);
187     bool read_float (const char* name, float* f);
188     bool read_bool (const char* name, bool* b);
189     bool read_lisp (const char* name, lisp_object_t** b);
190   };
191
192 /** */
193 class LispWriter
194   {
195   private:
196     std::vector<lisp_object_t*> lisp_objs;
197
198     void append (lisp_object_t* obj);
199     lisp_object_t* make_list3 (lisp_object_t*, lisp_object_t*, lisp_object_t*);
200     lisp_object_t* make_list2 (lisp_object_t*, lisp_object_t*);
201   public:
202     LispWriter (const char* name);
203     void write_float (const char* name, float f);
204     void write_int (const char* name, int i);
205     void write_boolean (const char* name, bool b);
206     void write_string (const char* name, const char* str);
207     void write_symbol (const char* name, const char* symname);
208     void write_lisp_obj(const char* name, lisp_object_t* lst);
209
210     /** caller is responible to free the returned lisp_object_t */
211     lisp_object_t* create_lisp ();
212   };
213
214 #endif