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

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

lisp: miscellaneous work on type safety.

  • 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 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#ifndef __LISP_HPP_
11#define __LISP_HPP_
12
13#include <cstdlib>
14#include <stdint.h>
15
16#ifdef L_PROFILE
17#include "timing.h"
18#endif
19
20#define Cell void
21#define MAX_LISP_TOKEN_LEN 200
22enum { PERM_SPACE,
23       TMP_SPACE,
24       USER_SPACE,
25       GC_SPACE };
26
27#define FIXED_TRIG_SIZE 360               // 360 degrees stored in table
28extern int32_t sin_table[FIXED_TRIG_SIZE];   // this should be filled in by external module
29#define TBS 1662                          // atan table granularity
30extern uint16_t atan_table[TBS];
31#define NILP(x) (x==NULL)
32#define DEFINEDP(x) (x!=l_undefined)
33class bFILE;
34extern int current_space;
35extern bFILE *current_print_file;
36
37
38enum { L_BAD_CELL,   // error catching type
39       L_CONS_CELL, L_NUMBER, L_SYMBOL,     L_SYS_FUNCTION, L_USER_FUNCTION,
40       L_STRING, L_CHARACTER, L_C_FUNCTION, L_C_BOOL,       L_L_FUNCTION, L_POINTER,
41       L_OBJECT_VAR, L_1D_ARRAY,
42       L_FIXED_POINT, L_COLLECTED_OBJECT };
43
44typedef uint32_t ltype;    // make sure structures aren't packed differently on various compiler
45                       // and sure that word, etc are word aligned
46
47struct LispObject
48{
49    ltype type;
50};
51
52struct LispObjectVar : LispObject
53{
54    long number;
55};
56
57struct LispList : LispObject
58{
59    static LispList *Create();
60
61    size_t GetLength();
62
63    LispObject *cdr, *car;
64};
65
66struct LispNumber : LispObject
67{
68    static LispNumber *Create(long num);
69
70    long num;
71};
72
73struct LispRedirect : LispObject
74{
75    LispObject *new_reference;
76};
77
78struct LispString : LispObject
79{
80    static LispString *Create(char const *string);
81    static LispString *Create(char const *string, int length);
82    static LispString *Create(int length);
83
84    char *GetString();
85
86    char str[1];
87};
88
89struct LispSymbol : LispObject
90{
91    static LispSymbol *Find(char const *name);
92    static LispSymbol *FindOrCreate(char const *name);
93
94    LispString *GetName();
95    LispObject *GetFunction();
96    LispObject *GetValue();
97
98    void SetFunction(LispObject *fun);
99    void SetValue(LispObject *value);
100    void SetNumber(long num);
101
102#ifdef L_PROFILE
103    float time_taken;
104#endif
105    LispObject *value;
106    LispObject *function;
107    LispString *name;
108    LispSymbol *left, *right; // tree structure
109};
110
111struct LispSysFunction : LispObject
112{
113    short min_args, max_args;
114    short fun_number;
115};
116
117struct LispUserFunction : LispObject
118{
119#ifndef NO_LIBS
120    intptr_t alist, blist;      // id for cached blocks
121#else
122    void *arg_list, *block_list;
123#endif
124};
125
126struct LispArray : LispObject
127{
128    static LispArray *Create(int size, void *rest);
129
130    inline LispObject **GetData() { return data; }
131    LispObject *Get(long x);
132
133    unsigned short size;
134    // size * sizeof (void *) follows1
135
136private:
137    LispObject *data[1];
138};
139
140struct LispChar : LispObject
141{
142    int16_t pad;
143    uint16_t ch;
144};
145
146struct LispPointer : LispObject
147{
148    void *addr;
149};
150
151
152struct LispFixedPoint : LispObject
153{
154    int32_t x;
155};
156
157static inline LispObject *&CAR(void *x) { return ((LispList *)x)->car; }
158static inline LispObject *&CDR(void *x) { return ((LispList *)x)->cdr; }
159static inline ltype item_type(void *x) { if (x) return *(ltype *)x; return L_CONS_CELL; }
160
161void perm_space();
162void tmp_space();
163void use_user_space(void *addr, long size);
164void *lpointer_value(void *lpointer);
165int32_t lnumber_value(void *lnumber);
166unsigned short lcharacter_value(void *c);
167long lfixed_point_value(void *c);
168void *lisp_atom(void *i);
169void *lcdr(void *c);
170void *lcar(void *c);
171void *lisp_eq(void *n1, void *n2);
172void *lisp_equal(void *n1, void *n2);
173void lprint(void *i);
174void *eval(void *prog);
175void *eval_block(void *list);
176void *eval_function(LispSymbol *sym, void *arg_list);
177void *eval_user_fun(LispSymbol *sym, void *arg_list);
178void *compile(char const *&s);
179void *assoc(void *item, void *list);
180void resize_tmp(int new_size);
181void resize_perm(int new_size);
182
183void push_onto_list(void *object, void *&list);
184LispSymbol *add_c_object(void *symbol, int16_t number);
185LispSymbol *add_c_function(char const *name, short min_args, short max_args, short number);
186LispSymbol *add_c_bool_fun(char const *name, short min_args, short max_args, short number);
187LispSymbol *add_lisp_function(char const *name, short min_args, short max_args, short number);
188int read_ltoken(char *&s, char *buffer);
189void print_trace_stack(int max_levels);
190
191
192LispPointer *new_lisp_pointer(void *addr);
193LispChar *new_lisp_character(uint16_t ch);
194LispFixedPoint *new_lisp_fixed_point(int32_t x);
195LispObjectVar *new_lisp_object_var(int16_t number);
196LispSysFunction *new_lisp_sys_function(int min_args, int max_args, int fun_number);
197LispSysFunction *new_lisp_c_function(int min_args, int max_args, int fun_number);
198LispSysFunction *new_lisp_c_bool(int min_args, int max_args, int fun_number);
199
200#ifdef NO_LIBS
201LispUserFunction *new_lisp_user_function(void *arg_list, void *block_list);
202#else
203LispUserFunction *new_lisp_user_function(intptr_t arg_list, intptr_t block_list);
204#endif
205
206LispSysFunction *new_user_lisp_function(int min_args, int max_args, int fun_number);
207
208int end_of_program(char *s);
209void clear_tmp();
210void lisp_init(long perm_size, long tmp_size);
211void lisp_uninit();
212extern LispSymbol *lsym_root;
213
214extern uint8_t *space[4], *free_space[4];
215extern int space_size[4];
216void *nth(int num, void *list);
217int32_t lisp_atan2(int32_t dy, int32_t dx);
218int32_t lisp_sin(int32_t x);
219int32_t lisp_cos(int32_t x);
220void restore_heap(void *val, int heap);
221void *mark_heap(int heap);
222
223extern "C" {
224void lbreak(const char *format, ...);
225} ;
226
227extern void clisp_init();                      // external initalizer call by lisp_init()
228extern long c_caller(long number, void *arg);  // exten c function switches on number
229extern void *l_caller(long number, void *arg);  // exten lisp function switches on number
230
231extern void *l_obj_get(long number);  // exten lisp function switches on number
232extern void l_obj_set(long number, void *arg);  // exten lisp function switches on number
233extern void l_obj_print(long number);  // exten lisp function switches on number
234
235// FIXME: get rid of this later
236static inline void *symbol_value(void *sym) { return ((LispSymbol *)sym)->GetValue(); }
237static inline char *lstring_value(void *str) { return ((LispString *)str)->GetString(); }
238
239#include "lisp_opt.h"
240
241#endif
Note: See TracBrowser for help on using the repository browser.