furhter improve collision detection by reintroducing time of collision, still more...
[supertux.git] / lib / utils / 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 SUPERTUX_LISPREADER_H
25 #define SUPERTUX_LISPREADER_H
26
27 #include <cstdio>
28 #include <string>
29 #include <vector>
30 #include <exception>
31
32 #include <zlib.h>
33
34 #include "utils/exceptions.h"
35
36 namespace SuperTux {
37
38 #define LISP_STREAM_FILE       1
39 #define LISP_STREAM_STRING     2
40 #define LISP_STREAM_ANY        3
41
42 #define LISP_TYPE_INTERNAL      -3
43 #define LISP_TYPE_PARSE_ERROR   -2
44 #define LISP_TYPE_EOF           -1
45 #define LISP_TYPE_NIL           0
46 #define LISP_TYPE_SYMBOL        1
47 #define LISP_TYPE_INTEGER       2
48 #define LISP_TYPE_STRING        3
49 #define LISP_TYPE_REAL          4
50 #define LISP_TYPE_CONS          5
51 #define LISP_TYPE_PATTERN_CONS  6
52 #define LISP_TYPE_BOOLEAN       7
53 #define LISP_TYPE_PATTERN_VAR   8
54
55 #define LISP_PATTERN_ANY        1
56 #define LISP_PATTERN_SYMBOL     2
57 #define LISP_PATTERN_STRING     3
58 #define LISP_PATTERN_INTEGER    4
59 #define LISP_PATTERN_REAL       5
60 #define LISP_PATTERN_BOOLEAN    6
61 #define LISP_PATTERN_LIST       7
62 #define LISP_PATTERN_OR         8
63
64 typedef struct
65   {
66     int type;
67
68     union
69       {
70         FILE *file;
71         struct
72           {
73             char *buf;
74             int pos;
75           }
76         string;
77         struct
78           {
79             void *data;
80             int (*next_char) (void *data);
81             void (*unget_char) (char c, void *data);
82           }
83         any;
84       } v;
85   }
86 lisp_stream_t;
87
88 typedef struct _lisp_object_t lisp_object_t;
89 struct _lisp_object_t
90   {
91     int type;
92
93     union
94       {
95         struct
96           {
97             struct _lisp_object_t *car;
98             struct _lisp_object_t *cdr;
99           }
100         cons;
101
102         char *string;
103         int integer;
104         float real;
105
106         struct
107           {
108             int type;
109             int index;
110             struct _lisp_object_t *sub;
111           }
112         pattern;
113       } v;
114   };
115
116 lisp_stream_t* lisp_stream_init_file (lisp_stream_t *stream, FILE *file);
117 lisp_stream_t* lisp_stream_init_string (lisp_stream_t *stream, char *buf);
118 lisp_stream_t* lisp_stream_init_any (lisp_stream_t *stream, void *data,
119                                      int (*next_char) (void *data),
120                                      void (*unget_char) (char c, void *data));
121
122 lisp_object_t* lisp_read (lisp_stream_t *in);
123 lisp_object_t* lisp_read_from_file(const std::string& filename);
124 void lisp_free (lisp_object_t *obj);
125
126 lisp_object_t* lisp_read_from_string (const char *buf);
127
128 int lisp_compile_pattern (lisp_object_t **obj, int *num_subs);
129 int lisp_match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars, int num_subs);
130 int lisp_match_string (const char *pattern_string, lisp_object_t *obj, lisp_object_t **vars);
131
132 int lisp_type (lisp_object_t *obj);
133 int lisp_integer (lisp_object_t *obj);
134 float lisp_real (lisp_object_t *obj);
135 char* lisp_symbol (lisp_object_t *obj);
136 char* lisp_string (lisp_object_t *obj);
137 int lisp_boolean (lisp_object_t *obj);
138 lisp_object_t* lisp_car (lisp_object_t *obj);
139 lisp_object_t* lisp_cdr (lisp_object_t *obj);
140
141 lisp_object_t* lisp_cxr (lisp_object_t *obj, const char *x);
142
143 lisp_object_t* lisp_make_integer (int value);
144 lisp_object_t* lisp_make_real (float value);
145 lisp_object_t* lisp_make_symbol (const char *value);
146 lisp_object_t* lisp_make_string (const char *value);
147 lisp_object_t* lisp_make_cons (lisp_object_t *car, lisp_object_t *cdr);
148 lisp_object_t* lisp_make_boolean (int value);
149
150 int lisp_list_length (lisp_object_t *obj);
151 lisp_object_t* lisp_list_nth_cdr (lisp_object_t *obj, int index);
152 lisp_object_t* lisp_list_nth (lisp_object_t *obj, int index);
153
154 void lisp_dump (lisp_object_t *obj, FILE *out);
155
156 #define lisp_nil()           ((lisp_object_t*)0)
157
158 #define lisp_nil_p(obj)      (obj == 0)
159 #define lisp_integer_p(obj)  (lisp_type((obj)) == LISP_TYPE_INTEGER)
160 #define lisp_real_p(obj)     (lisp_type((obj)) == LISP_TYPE_REAL)
161 #define lisp_symbol_p(obj)   (lisp_type((obj)) == LISP_TYPE_SYMBOL)
162 #define lisp_string_p(obj)   (lisp_type((obj)) == LISP_TYPE_STRING)
163 #define lisp_cons_p(obj)     (lisp_type((obj)) == LISP_TYPE_CONS)
164 #define lisp_boolean_p(obj)  (lisp_type((obj)) == LISP_TYPE_BOOLEAN)
165
166 /** */
167 class LispReader
168 {
169 private:
170   lisp_object_t* owner;
171   lisp_object_t* lst;
172
173   lisp_object_t* search_for(const char* name);
174   
175 public:
176   /** cur == ((pos 1 2 3) (id 12 3 4)...) */
177   LispReader(lisp_object_t* l);
178   ~LispReader();
179
180   bool read_int_vector(const char* name, std::vector<int>& vec);
181   bool read_int_vector(const char* name, std::vector<unsigned int>& vec);
182   bool read_char_vector(const char* name, std::vector<char>& vec);
183   bool read_string_vector(const char* name, std::vector<std::string>& vec);
184   bool read_string(const char* name, std::string& str, bool translatable = false);
185   bool read_int(const char* name, int& i);
186   bool read_uint(const char* name, unsigned 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   lisp_object_t* read_lisp(const char* name);
191
192   static LispReader* load(const std::string& filename,
193       const std::string& toplevellist);
194
195   lisp_object_t* get_lisp();
196 };
197
198 } //namespace SuperTux
199
200 #endif /*SUPERTUX_LISPREADER_H*/
201