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

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

lisp: minor coding style fixes.

  • Property svn:keywords set to Id
File size: 7.0 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
33enum
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
54typedef uint8_t ltype;
55
56struct 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
74struct 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
87struct LObjectVar : LObject
88{
89    /* Factories */
90    static LObjectVar *Create(int index);
91
92    /* Members */
93    int m_index;
94};
95
96struct 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
109struct LNumber : LObject
110{
111    /* Factories */
112    static LNumber *Create(long num);
113
114    /* Members */
115    long m_num;
116};
117
118struct LRedirect : LObject
119{
120    /* Members */
121    LObject *m_ref;
122};
123
124struct 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 */
135private:
136    char m_str[1]; /* Can be allocated much larger than 1 */
137};
138
139struct 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
171struct 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
181struct LUserFunction : LObject
182{
183    LList *arg_list, *block_list;
184};
185
186struct 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
198private:
199    LObject *m_data[1]; /* Can be allocated much larger than 1 */
200};
201
202struct 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
214struct LPointer : LObject
215{
216    /* Factories */
217    static LPointer *Create(void *addr);
218
219    /* Members */
220    void *m_addr;
221};
222
223struct LFixedPoint : LObject
224{
225    /* Factories */
226    static LFixedPoint *Create(int32_t x);
227
228    /* Members */
229    int32_t m_fixed;
230};
231
232class Lisp
233{
234public:
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
243private:
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
251static inline LObject *&CAR(void *x) { return ((LList *)x)->m_car; }
252static inline LObject *&CDR(void *x) { return ((LList *)x)->m_cdr; }
253static inline ltype item_type(void *x) { if (x) return *(ltype *)x; return L_CONS_CELL; }
254
255void perm_space();
256void tmp_space();
257void *lpointer_value(void *lpointer);
258int32_t lnumber_value(void *lnumber);
259long lfixed_point_value(void *c);
260void *lisp_atom(void *i);
261LObject *lcdr(void *c);
262LObject *lcar(void *c);
263void *lisp_eq(void *n1, void *n2);
264void *lisp_equal(void *n1, void *n2);
265void *eval_block(void *list);
266void resize_tmp(size_t new_size);
267void resize_perm(size_t new_size);
268
269void push_onto_list(void *object, void *&list);
270LSymbol *add_c_object(void *symbol, int index);
271LSymbol *add_c_function(char const *name, short min_args, short max_args, short number);
272LSymbol *add_c_bool_fun(char const *name, short min_args, short max_args, short number);
273LSymbol *add_lisp_function(char const *name, short min_args, short max_args, short number);
274int read_ltoken(char *&s, char *buffer);
275void print_trace_stack(int max_levels);
276
277
278LSysFunction *new_lisp_sys_function(int min_args, int max_args, int fun_number);
279LSysFunction *new_lisp_c_function(int min_args, int max_args, int fun_number);
280LSysFunction *new_lisp_c_bool(int min_args, int max_args, int fun_number);
281
282LUserFunction *new_lisp_user_function(LList *arg_list, LList *block_list);
283
284LSysFunction *new_user_lisp_function(int min_args, int max_args, int fun_number);
285
286int end_of_program(char *s);
287
288void *nth(int num, void *list);
289int32_t lisp_atan2(int32_t dy, int32_t dx);
290int32_t lisp_sin(int32_t x);
291int32_t lisp_cos(int32_t x);
292
293extern "C" {
294void lbreak(const char *format, ...);
295} ;
296
297extern void clisp_init();                      // external initalizer call by lisp_init()
298extern long c_caller(long number, void *arg);  // exten c function switches on number
299extern void *l_caller(long number, void *arg);  // exten lisp function switches on number
300
301extern void *l_obj_get(long number);  // exten lisp function switches on number
302extern void l_obj_set(long number, void *arg);  // exten lisp function switches on number
303extern void l_obj_print(long number);  // exten lisp function switches on number
304
305// FIXME: get rid of this later
306static inline void *symbol_value(void *sym) { return ((LSymbol *)sym)->GetValue(); }
307static inline char *lstring_value(void *str) { return ((LString *)str)->GetString(); }
308
309#include "lisp_opt.h"
310
311#endif
Note: See TracBrowser for help on using the repository browser.