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

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

lisp: merge the Lisp and LispGC classes and improve coding style.

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