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

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

lisp: implement LispList::GetLength?.

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