1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This software was released into the Public Domain. As with most public |
---|
7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
8 | * Jonathan Clark, or by Sam Hocevar. |
---|
9 | */ |
---|
10 | |
---|
11 | #ifndef __LISP_HPP_ |
---|
12 | #define __LISP_HPP_ |
---|
13 | |
---|
14 | #include <cstdlib> |
---|
15 | #include <stdint.h> |
---|
16 | |
---|
17 | #ifdef L_PROFILE |
---|
18 | #include "timing.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #define Cell void |
---|
22 | #define MAX_LISP_TOKEN_LEN 200 |
---|
23 | |
---|
24 | #define FIXED_TRIG_SIZE 360 // 360 degrees stored in table |
---|
25 | extern int32_t sin_table[FIXED_TRIG_SIZE]; // this should be filled in by external module |
---|
26 | #define TBS 1662 // atan table granularity |
---|
27 | extern uint16_t atan_table[TBS]; |
---|
28 | #define NILP(x) ((x)==NULL) |
---|
29 | #define DEFINEDP(x) ((x)!=l_undefined) |
---|
30 | class bFILE; |
---|
31 | extern bFILE *current_print_file; |
---|
32 | |
---|
33 | enum |
---|
34 | { |
---|
35 | L_BAD_CELL, // error catching type |
---|
36 | L_CONS_CELL, |
---|
37 | L_NUMBER, |
---|
38 | L_SYMBOL, |
---|
39 | L_SYS_FUNCTION, |
---|
40 | L_USER_FUNCTION, |
---|
41 | L_STRING, |
---|
42 | L_CHARACTER, |
---|
43 | L_C_FUNCTION, |
---|
44 | L_C_BOOL, |
---|
45 | L_L_FUNCTION, |
---|
46 | L_POINTER, |
---|
47 | L_OBJECT_VAR, |
---|
48 | L_1D_ARRAY, |
---|
49 | L_FIXED_POINT, |
---|
50 | L_COLLECTED_OBJECT, |
---|
51 | }; |
---|
52 | |
---|
53 | // FIXME: switch this to uint8_t one day? it still breaks stuff |
---|
54 | typedef uint8_t ltype; |
---|
55 | |
---|
56 | struct LSpace |
---|
57 | { |
---|
58 | size_t GetFree(); |
---|
59 | void *Alloc(size_t size); |
---|
60 | |
---|
61 | void *Mark(); |
---|
62 | void Restore(void *val); |
---|
63 | void Clear(); |
---|
64 | |
---|
65 | static LSpace Tmp, Perm, Gc; |
---|
66 | static LSpace *Current; |
---|
67 | |
---|
68 | uint8_t *m_data; |
---|
69 | uint8_t *m_free; |
---|
70 | char const *m_name; |
---|
71 | size_t m_size; |
---|
72 | }; |
---|
73 | |
---|
74 | struct LObject |
---|
75 | { |
---|
76 | /* Factories */ |
---|
77 | static LObject *Compile(char const *&s); |
---|
78 | |
---|
79 | /* Methods */ |
---|
80 | LObject *Eval(); |
---|
81 | void Print(); |
---|
82 | |
---|
83 | /* Members */ |
---|
84 | ltype m_type; |
---|
85 | }; |
---|
86 | |
---|
87 | struct LObjectVar : LObject |
---|
88 | { |
---|
89 | /* Factories */ |
---|
90 | static LObjectVar *Create(int index); |
---|
91 | |
---|
92 | /* Members */ |
---|
93 | int m_index; |
---|
94 | }; |
---|
95 | |
---|
96 | struct LList : LObject |
---|
97 | { |
---|
98 | /* Factories */ |
---|
99 | static LList *Create(); |
---|
100 | |
---|
101 | /* Methods */ |
---|
102 | size_t GetLength(); |
---|
103 | LList *Assoc(LObject *item); |
---|
104 | |
---|
105 | /* Members */ |
---|
106 | LObject *m_cdr, *m_car; |
---|
107 | }; |
---|
108 | |
---|
109 | struct LNumber : LObject |
---|
110 | { |
---|
111 | /* Factories */ |
---|
112 | static LNumber *Create(long num); |
---|
113 | |
---|
114 | /* Members */ |
---|
115 | long m_num; |
---|
116 | }; |
---|
117 | |
---|
118 | struct LRedirect : LObject |
---|
119 | { |
---|
120 | /* Members */ |
---|
121 | LObject *m_ref; |
---|
122 | }; |
---|
123 | |
---|
124 | struct LString : LObject |
---|
125 | { |
---|
126 | /* Factories */ |
---|
127 | static LString *Create(char const *string); |
---|
128 | static LString *Create(char const *string, int length); |
---|
129 | static LString *Create(int length); |
---|
130 | |
---|
131 | /* Methods */ |
---|
132 | char *GetString(); |
---|
133 | |
---|
134 | /* Members */ |
---|
135 | private: |
---|
136 | char m_str[1]; /* Can be allocated much larger than 1 */ |
---|
137 | }; |
---|
138 | |
---|
139 | struct LSymbol : LObject |
---|
140 | { |
---|
141 | /* Factories */ |
---|
142 | static LSymbol *Find(char const *name); |
---|
143 | static LSymbol *FindOrCreate(char const *name); |
---|
144 | |
---|
145 | /* Methods */ |
---|
146 | LObject *EvalFunction(void *arg_list); |
---|
147 | LObject *EvalUserFunction(LList *arg_list); |
---|
148 | |
---|
149 | LString *GetName(); |
---|
150 | LObject *GetFunction(); |
---|
151 | LObject *GetValue(); |
---|
152 | |
---|
153 | void SetFunction(LObject *fun); |
---|
154 | void SetValue(LObject *value); |
---|
155 | void SetNumber(long num); |
---|
156 | |
---|
157 | /* Members */ |
---|
158 | #ifdef L_PROFILE |
---|
159 | float time_taken; |
---|
160 | #endif |
---|
161 | LObject *m_value; |
---|
162 | LObject *m_function; |
---|
163 | LString *m_name; |
---|
164 | LSymbol *m_left, *m_right; // tree structure |
---|
165 | |
---|
166 | /* Static members */ |
---|
167 | static LSymbol *root; |
---|
168 | static size_t count; |
---|
169 | }; |
---|
170 | |
---|
171 | struct LSysFunction : LObject |
---|
172 | { |
---|
173 | /* Methods */ |
---|
174 | LObject *EvalFunction(LList *arg_list); |
---|
175 | |
---|
176 | /* Members */ |
---|
177 | short min_args, max_args; |
---|
178 | short fun_number; |
---|
179 | }; |
---|
180 | |
---|
181 | struct LUserFunction : LObject |
---|
182 | { |
---|
183 | LList *arg_list, *block_list; |
---|
184 | }; |
---|
185 | |
---|
186 | struct LArray : LObject |
---|
187 | { |
---|
188 | /* Factories */ |
---|
189 | static LArray *Create(size_t len, void *rest); |
---|
190 | |
---|
191 | /* Methods */ |
---|
192 | inline LObject **GetData() { return m_data; } |
---|
193 | LObject *Get(int x); |
---|
194 | |
---|
195 | /* Members */ |
---|
196 | size_t m_len; |
---|
197 | |
---|
198 | private: |
---|
199 | LObject *m_data[1]; /* Can be allocated much larger than 1 */ |
---|
200 | }; |
---|
201 | |
---|
202 | struct LChar : LObject |
---|
203 | { |
---|
204 | /* Factories */ |
---|
205 | static LChar *Create(uint16_t ch); |
---|
206 | |
---|
207 | /* Methods */ |
---|
208 | uint16_t GetValue(); |
---|
209 | |
---|
210 | /* Members */ |
---|
211 | uint16_t m_ch; |
---|
212 | }; |
---|
213 | |
---|
214 | struct LPointer : LObject |
---|
215 | { |
---|
216 | /* Factories */ |
---|
217 | static LPointer *Create(void *addr); |
---|
218 | |
---|
219 | /* Members */ |
---|
220 | void *m_addr; |
---|
221 | }; |
---|
222 | |
---|
223 | struct LFixedPoint : LObject |
---|
224 | { |
---|
225 | /* Factories */ |
---|
226 | static LFixedPoint *Create(int32_t x); |
---|
227 | |
---|
228 | /* Members */ |
---|
229 | int32_t m_fixed; |
---|
230 | }; |
---|
231 | |
---|
232 | class Lisp |
---|
233 | { |
---|
234 | public: |
---|
235 | static void Init(); |
---|
236 | static void Uninit(); |
---|
237 | |
---|
238 | static void InitConstants(); |
---|
239 | |
---|
240 | // Collect temporary or permanent spaces |
---|
241 | static void CollectSpace(LSpace *which_space, int grow); |
---|
242 | |
---|
243 | private: |
---|
244 | static LArray *CollectArray(LArray *x); |
---|
245 | static LList *CollectList(LList *x); |
---|
246 | static LObject *CollectObject(LObject *x); |
---|
247 | static void CollectSymbols(LSymbol *root); |
---|
248 | static void CollectStacks(); |
---|
249 | }; |
---|
250 | |
---|
251 | static inline LObject *&CAR(void *x) { return ((LList *)x)->m_car; } |
---|
252 | static inline LObject *&CDR(void *x) { return ((LList *)x)->m_cdr; } |
---|
253 | static inline ltype item_type(void *x) { if (x) return *(ltype *)x; return L_CONS_CELL; } |
---|
254 | |
---|
255 | void perm_space(); |
---|
256 | void tmp_space(); |
---|
257 | void *lpointer_value(void *lpointer); |
---|
258 | int32_t lnumber_value(void *lnumber); |
---|
259 | long lfixed_point_value(void *c); |
---|
260 | void *lisp_atom(void *i); |
---|
261 | LObject *lcdr(void *c); |
---|
262 | LObject *lcar(void *c); |
---|
263 | void *lisp_eq(void *n1, void *n2); |
---|
264 | void *lisp_equal(void *n1, void *n2); |
---|
265 | void *eval_block(void *list); |
---|
266 | void resize_tmp(size_t new_size); |
---|
267 | void resize_perm(size_t new_size); |
---|
268 | |
---|
269 | void push_onto_list(void *object, void *&list); |
---|
270 | LSymbol *add_c_object(void *symbol, int index); |
---|
271 | LSymbol *add_c_function(char const *name, short min_args, short max_args, short number); |
---|
272 | LSymbol *add_c_bool_fun(char const *name, short min_args, short max_args, short number); |
---|
273 | LSymbol *add_lisp_function(char const *name, short min_args, short max_args, short number); |
---|
274 | int read_ltoken(char *&s, char *buffer); |
---|
275 | void print_trace_stack(int max_levels); |
---|
276 | |
---|
277 | |
---|
278 | LSysFunction *new_lisp_sys_function(int min_args, int max_args, int fun_number); |
---|
279 | LSysFunction *new_lisp_c_function(int min_args, int max_args, int fun_number); |
---|
280 | LSysFunction *new_lisp_c_bool(int min_args, int max_args, int fun_number); |
---|
281 | |
---|
282 | LUserFunction *new_lisp_user_function(LList *arg_list, LList *block_list); |
---|
283 | |
---|
284 | LSysFunction *new_user_lisp_function(int min_args, int max_args, int fun_number); |
---|
285 | |
---|
286 | int end_of_program(char *s); |
---|
287 | |
---|
288 | void *nth(int num, void *list); |
---|
289 | int32_t lisp_atan2(int32_t dy, int32_t dx); |
---|
290 | int32_t lisp_sin(int32_t x); |
---|
291 | int32_t lisp_cos(int32_t x); |
---|
292 | |
---|
293 | extern "C" { |
---|
294 | void lbreak(const char *format, ...); |
---|
295 | } ; |
---|
296 | |
---|
297 | extern void clisp_init(); // external initalizer call by lisp_init() |
---|
298 | extern long c_caller(long number, void *arg); // exten c function switches on number |
---|
299 | extern void *l_caller(long number, void *arg); // exten lisp function switches on number |
---|
300 | |
---|
301 | extern void *l_obj_get(long number); // exten lisp function switches on number |
---|
302 | extern void l_obj_set(long number, void *arg); // exten lisp function switches on number |
---|
303 | extern void l_obj_print(long number); // exten lisp function switches on number |
---|
304 | |
---|
305 | // FIXME: get rid of this later |
---|
306 | static inline LObject *symbol_value(void *sym) { return ((LSymbol *)sym)->GetValue(); } |
---|
307 | static inline char *lstring_value(void *str) { return ((LString *)str)->GetString(); } |
---|
308 | |
---|
309 | #include "lisp_opt.h" |
---|
310 | |
---|
311 | #endif |
---|