source: abuse/trunk/src/lisp/lisp.h @ 635

Last change on this file since 635 was 635, checked in by Sam Hocevar, 11 years ago

lisp: refactor Lisp spaces so that they are real objects, and get rid
of the unused USER_SPACE.

  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
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
25extern int32_t sin_table[FIXED_TRIG_SIZE];   // this should be filled in by external module
26#define TBS 1662                          // atan table granularity
27extern uint16_t atan_table[TBS];
28#define NILP(x) (x==NULL)
29#define DEFINEDP(x) (x!=l_undefined)
30class bFILE;
31extern bFILE *current_print_file;
32
33
34enum { L_BAD_CELL,   // error catching type
35       L_CONS_CELL, L_NUMBER, L_SYMBOL,     L_SYS_FUNCTION, L_USER_FUNCTION,
36       L_STRING, L_CHARACTER, L_C_FUNCTION, L_C_BOOL,       L_L_FUNCTION, L_POINTER,
37       L_OBJECT_VAR, L_1D_ARRAY,
38       L_FIXED_POINT, L_COLLECTED_OBJECT };
39
40typedef uint32_t ltype;    // make sure structures aren't packed differently on various compiler
41                       // and sure that word, etc are word aligned
42
43class Lisp
44{
45public:
46    static void Init();
47    static void Uninit();
48};
49
50struct LSpace
51{
52    size_t GetFree();
53    void *Alloc(size_t size);
54
55    void *Mark();
56    void Restore(void *val);
57    void Clear();
58
59    static LSpace Tmp, Perm, Gc;
60    static LSpace *Current;
61
62    uint8_t *m_data;
63    uint8_t *m_free;
64    char const *m_name;
65    size_t m_size;
66};
67
68struct LObject
69{
70    /* Factories */
71    static LObject *Compile(char const *&s);
72
73    /* Methods */
74    LObject *Eval();
75    void Print();
76
77    /* Members */
78    ltype type;
79};
80
81struct LObjectVar : LObject
82{
83    /* Factories */
84    static LObjectVar *Create(int index);
85
86    /* Members */
87    int index;
88};
89
90struct LList : LObject
91{
92    /* Factories */
93    static LList *Create();
94
95    /* Methods */
96    size_t GetLength();
97
98    /* Members */
99    LObject *cdr, *car;
100};
101
102struct LNumber : LObject
103{
104    /* Factories */
105    static LNumber *Create(long num);
106
107    /* Members */
108    long num;
109};
110
111struct LRedirect : LObject
112{
113    /* Members */
114    LObject *ref;
115};
116
117struct LString : LObject
118{
119    /* Factories */
120    static LString *Create(char const *string);
121    static LString *Create(char const *string, int length);
122    static LString *Create(int length);
123
124    /* Methods */
125    char *GetString();
126
127    /* Members */
128private:
129    char str[1]; /* Can be allocated much larger than 1 */
130};
131
132struct LSymbol : LObject
133{
134    /* Factories */
135    static LSymbol *Find(char const *name);
136    static LSymbol *FindOrCreate(char const *name);
137
138    /* Methods */
139    LObject *EvalFunction(void *arg_list);
140    LObject *EvalUserFunction(LList *arg_list);
141
142    LString *GetName();
143    LObject *GetFunction();
144    LObject *GetValue();
145
146    void SetFunction(LObject *fun);
147    void SetValue(LObject *value);
148    void SetNumber(long num);
149
150    /* Members */
151#ifdef L_PROFILE
152    float time_taken;
153#endif
154    LObject *value;
155    LObject *function;
156    LString *name;
157    LSymbol *left, *right; // tree structure
158
159    /* Static members */
160    static LSymbol *root;
161    static size_t count;
162};
163
164struct LSysFunction : LObject
165{
166    /* Methods */
167    LObject *EvalFunction(LList *arg_list);
168
169    /* Members */
170    short min_args, max_args;
171    short fun_number;
172};
173
174struct LUserFunction : LObject
175{
176    LList *arg_list, *block_list;
177};
178
179struct LArray : LObject
180{
181    /* Factories */
182    static LArray *Create(size_t len, void *rest);
183
184    /* Methods */
185    inline LObject **GetData() { return data; }
186    LObject *Get(int x);
187
188    /* Members */
189    size_t len;
190
191private:
192    LObject *data[1]; /* Can be allocated much larger than 1 */
193};
194
195struct LChar : LObject
196{
197    /* Factories */
198    static LChar *Create(uint16_t ch);
199
200    /* Members */
201    uint16_t ch;
202};
203
204struct LPointer : LObject
205{
206    /* Factories */
207    static LPointer *Create(void *addr);
208
209    /* Members */
210    void *addr;
211};
212
213struct LFixedPoint : LObject
214{
215    /* Factories */
216    static LFixedPoint *Create(int32_t x);
217
218    /* Members */
219    int32_t x;
220};
221
222static inline LObject *&CAR(void *x) { return ((LList *)x)->car; }
223static inline LObject *&CDR(void *x) { return ((LList *)x)->cdr; }
224static inline ltype item_type(void *x) { if (x) return *(ltype *)x; return L_CONS_CELL; }
225
226void perm_space();
227void tmp_space();
228void *lpointer_value(void *lpointer);
229int32_t lnumber_value(void *lnumber);
230unsigned short lcharacter_value(void *c);
231long lfixed_point_value(void *c);
232void *lisp_atom(void *i);
233LObject *lcdr(void *c);
234LObject *lcar(void *c);
235void *lisp_eq(void *n1, void *n2);
236void *lisp_equal(void *n1, void *n2);
237void *eval_block(void *list);
238void *assoc(void *item, void *list);
239void resize_tmp(size_t new_size);
240void resize_perm(size_t new_size);
241
242void push_onto_list(void *object, void *&list);
243LSymbol *add_c_object(void *symbol, int index);
244LSymbol *add_c_function(char const *name, short min_args, short max_args, short number);
245LSymbol *add_c_bool_fun(char const *name, short min_args, short max_args, short number);
246LSymbol *add_lisp_function(char const *name, short min_args, short max_args, short number);
247int read_ltoken(char *&s, char *buffer);
248void print_trace_stack(int max_levels);
249
250
251LSysFunction *new_lisp_sys_function(int min_args, int max_args, int fun_number);
252LSysFunction *new_lisp_c_function(int min_args, int max_args, int fun_number);
253LSysFunction *new_lisp_c_bool(int min_args, int max_args, int fun_number);
254
255LUserFunction *new_lisp_user_function(LList *arg_list, LList *block_list);
256
257LSysFunction *new_user_lisp_function(int min_args, int max_args, int fun_number);
258
259int end_of_program(char *s);
260void lisp_init();
261void lisp_uninit();
262
263void *nth(int num, void *list);
264int32_t lisp_atan2(int32_t dy, int32_t dx);
265int32_t lisp_sin(int32_t x);
266int32_t lisp_cos(int32_t x);
267void restore_heap(void *val, int heap);
268void *mark_heap(int heap);
269
270extern "C" {
271void lbreak(const char *format, ...);
272} ;
273
274extern void clisp_init();                      // external initalizer call by lisp_init()
275extern long c_caller(long number, void *arg);  // exten c function switches on number
276extern void *l_caller(long number, void *arg);  // exten lisp function switches on number
277
278extern void *l_obj_get(long number);  // exten lisp function switches on number
279extern void l_obj_set(long number, void *arg);  // exten lisp function switches on number
280extern void l_obj_print(long number);  // exten lisp function switches on number
281
282// FIXME: get rid of this later
283static inline void *symbol_value(void *sym) { return ((LSymbol *)sym)->GetValue(); }
284static inline char *lstring_value(void *str) { return ((LString *)str)->GetString(); }
285
286#include "lisp_opt.h"
287
288#endif
Note: See TracBrowser for help on using the repository browser.