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

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

style: remove trailing spaces, fix copyright statements.

  • Property svn:keywords set to Id
File size: 6.4 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    void Print();
51
52    ltype type;
53};
54
55struct LObjectVar : LObject
56{
57    static LObjectVar *Create(int index);
58
59    int index;
60};
61
62struct LList : LObject
63{
64    static LList *Create();
65
66    size_t GetLength();
67
68    LObject *cdr, *car;
69};
70
71struct LNumber : LObject
72{
73    static LNumber *Create(long num);
74
75    long num;
76};
77
78struct LRedirect : LObject
79{
80    LObject *ref;
81};
82
83struct LString : LObject
84{
85    static LString *Create(char const *string);
86    static LString *Create(char const *string, int length);
87    static LString *Create(int length);
88
89    char *GetString();
90
91private:
92    char str[1]; /* Can be allocated much larger than 1 */
93};
94
95struct LSymbol : LObject
96{
97    /* Factories */
98    static LSymbol *Find(char const *name);
99    static LSymbol *FindOrCreate(char const *name);
100
101    /* Methods */
102    LString *GetName();
103    LObject *GetFunction();
104    LObject *GetValue();
105
106    void SetFunction(LObject *fun);
107    void SetValue(LObject *value);
108    void SetNumber(long num);
109
110    /* Members */
111#ifdef L_PROFILE
112    float time_taken;
113#endif
114    LObject *value;
115    LObject *function;
116    LString *name;
117    LSymbol *left, *right; // tree structure
118
119    /* Static members */
120    static LSymbol *root;
121    static size_t count;
122};
123
124struct LSysFunction : LObject
125{
126    short min_args, max_args;
127    short fun_number;
128};
129
130struct LUserFunction : LObject
131{
132#ifndef NO_LIBS
133    intptr_t alist, blist;      // id for cached blocks
134#else
135    void *arg_list, *block_list;
136#endif
137};
138
139struct LArray : LObject
140{
141    /* Factories */
142    static LArray *Create(size_t len, void *rest);
143
144    /* Methods */
145    inline LObject **GetData() { return data; }
146    LObject *Get(int x);
147
148    /* Members */
149    size_t len;
150
151private:
152    LObject *data[1]; /* Can be allocated much larger than 1 */
153};
154
155struct LChar : LObject
156{
157    static LChar *Create(uint16_t ch);
158
159    uint16_t ch;
160};
161
162struct LPointer : LObject
163{
164    static LPointer *Create(void *addr);
165
166    void *addr;
167};
168
169struct LFixedPoint : LObject
170{
171    static LFixedPoint *Create(int32_t x);
172
173    int32_t x;
174};
175
176static inline LObject *&CAR(void *x) { return ((LList *)x)->car; }
177static inline LObject *&CDR(void *x) { return ((LList *)x)->cdr; }
178static inline ltype item_type(void *x) { if (x) return *(ltype *)x; return L_CONS_CELL; }
179
180void perm_space();
181void tmp_space();
182void use_user_space(void *addr, long size);
183void *lpointer_value(void *lpointer);
184int32_t lnumber_value(void *lnumber);
185unsigned short lcharacter_value(void *c);
186long lfixed_point_value(void *c);
187void *lisp_atom(void *i);
188void *lcdr(void *c);
189void *lcar(void *c);
190void *lisp_eq(void *n1, void *n2);
191void *lisp_equal(void *n1, void *n2);
192void *eval(void *prog);
193void *eval_block(void *list);
194void *eval_function(LSymbol *sym, void *arg_list);
195void *eval_user_fun(LSymbol *sym, void *arg_list);
196void *compile(char const *&s);
197void *assoc(void *item, void *list);
198void resize_tmp(int new_size);
199void resize_perm(int new_size);
200
201void push_onto_list(void *object, void *&list);
202LSymbol *add_c_object(void *symbol, int index);
203LSymbol *add_c_function(char const *name, short min_args, short max_args, short number);
204LSymbol *add_c_bool_fun(char const *name, short min_args, short max_args, short number);
205LSymbol *add_lisp_function(char const *name, short min_args, short max_args, short number);
206int read_ltoken(char *&s, char *buffer);
207void print_trace_stack(int max_levels);
208
209
210LSysFunction *new_lisp_sys_function(int min_args, int max_args, int fun_number);
211LSysFunction *new_lisp_c_function(int min_args, int max_args, int fun_number);
212LSysFunction *new_lisp_c_bool(int min_args, int max_args, int fun_number);
213
214#ifdef NO_LIBS
215LUserFunction *new_lisp_user_function(void *arg_list, void *block_list);
216#else
217LUserFunction *new_lisp_user_function(intptr_t arg_list, intptr_t block_list);
218#endif
219
220LSysFunction *new_user_lisp_function(int min_args, int max_args, int fun_number);
221
222int end_of_program(char *s);
223void clear_tmp();
224void lisp_init(long perm_size, long tmp_size);
225void lisp_uninit();
226
227extern uint8_t *space[4], *free_space[4];
228extern int space_size[4];
229void *nth(int num, void *list);
230int32_t lisp_atan2(int32_t dy, int32_t dx);
231int32_t lisp_sin(int32_t x);
232int32_t lisp_cos(int32_t x);
233void restore_heap(void *val, int heap);
234void *mark_heap(int heap);
235
236extern "C" {
237void lbreak(const char *format, ...);
238} ;
239
240extern void clisp_init();                      // external initalizer call by lisp_init()
241extern long c_caller(long number, void *arg);  // exten c function switches on number
242extern void *l_caller(long number, void *arg);  // exten lisp function switches on number
243
244extern void *l_obj_get(long number);  // exten lisp function switches on number
245extern void l_obj_set(long number, void *arg);  // exten lisp function switches on number
246extern void l_obj_print(long number);  // exten lisp function switches on number
247
248// FIXME: get rid of this later
249static inline void *symbol_value(void *sym) { return ((LSymbol *)sym)->GetValue(); }
250static inline char *lstring_value(void *str) { return ((LString *)str)->GetString(); }
251
252#include "lisp_opt.h"
253
254#endif
Note: See TracBrowser for help on using the repository browser.