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

Last change on this file since 497 was 497, checked in by Sam Hocevar, 10 years ago

lisp: implement LSymbol::EvalFunction? and ensure all local pointers are
protected against collection.

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