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
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
[555]7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
[56]9 */
10
[2]11#ifndef __LISP_HPP_
12#define __LISP_HPP_
13
[490]14#include <cstdlib>
[11]15#include <stdint.h>
16
[2]17#ifdef L_PROFILE
[481]18#include "timing.h"
[2]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
[43]25extern int32_t sin_table[FIXED_TRIG_SIZE];   // this should be filled in by external module
[2]26#define TBS 1662                          // atan table granularity
[43]27extern uint16_t atan_table[TBS];
[639]28#define NILP(x) ((x)==NULL)
29#define DEFINEDP(x) ((x)!=l_undefined)
[2]30class bFILE;
31extern bFILE *current_print_file;
32
[639]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};
[2]52
[639]53// FIXME: switch this to uint8_t one day? it still breaks stuff
54typedef uint8_t ltype;
[2]55
[635]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
[492]74struct LObject
[124]75{
[496]76    /* Factories */
77    static LObject *Compile(char const *&s);
78
79    /* Methods */
80    LObject *Eval();
[493]81    void Print();
82
[496]83    /* Members */
[636]84    ltype m_type;
[482]85};
[2]86
[492]87struct LObjectVar : LObject
[2]88{
[496]89    /* Factories */
[493]90    static LObjectVar *Create(int index);
91
[496]92    /* Members */
[636]93    int m_index;
[482]94};
[2]95
[492]96struct LList : LObject
[2]97{
[496]98    /* Factories */
[492]99    static LList *Create();
[491]100
[496]101    /* Methods */
[490]102    size_t GetLength();
[638]103    LList *Assoc(LObject *item);
[490]104
[496]105    /* Members */
[636]106    LObject *m_cdr, *m_car;
[482]107};
[2]108
[492]109struct LNumber : LObject
[2]110{
[496]111    /* Factories */
[492]112    static LNumber *Create(long num);
[486]113
[496]114    /* Members */
[636]115    long m_num;
[482]116};
[2]117
[492]118struct LRedirect : LObject
[2]119{
[496]120    /* Members */
[636]121    LObject *m_ref;
[482]122};
123
[492]124struct LString : LObject
[484]125{
[496]126    /* Factories */
[492]127    static LString *Create(char const *string);
128    static LString *Create(char const *string, int length);
129    static LString *Create(int length);
[488]130
[496]131    /* Methods */
[488]132    char *GetString();
133
[496]134    /* Members */
[493]135private:
[636]136    char m_str[1]; /* Can be allocated much larger than 1 */
[484]137};
138
[492]139struct LSymbol : LObject
[482]140{
[493]141    /* Factories */
[492]142    static LSymbol *Find(char const *name);
143    static LSymbol *FindOrCreate(char const *name);
[484]144
[493]145    /* Methods */
[497]146    LObject *EvalFunction(void *arg_list);
[499]147    LObject *EvalUserFunction(LList *arg_list);
[497]148
[492]149    LString *GetName();
150    LObject *GetFunction();
151    LObject *GetValue();
[484]152
[492]153    void SetFunction(LObject *fun);
154    void SetValue(LObject *value);
[485]155    void SetNumber(long num);
[484]156
[493]157    /* Members */
[2]158#ifdef L_PROFILE
[482]159    float time_taken;
[2]160#endif
[636]161    LObject *m_value;
162    LObject *m_function;
163    LString *m_name;
164    LSymbol *m_left, *m_right; // tree structure
[493]165
166    /* Static members */
167    static LSymbol *root;
168    static size_t count;
[482]169};
[2]170
[492]171struct LSysFunction : LObject
[2]172{
[498]173    /* Methods */
174    LObject *EvalFunction(LList *arg_list);
175
176    /* Members */
[482]177    short min_args, max_args;
178    short fun_number;
179};
[2]180
[492]181struct LUserFunction : LObject
[2]182{
[501]183    LList *arg_list, *block_list;
[482]184};
[2]185
[492]186struct LArray : LObject
[2]187{
[493]188    /* Factories */
189    static LArray *Create(size_t len, void *rest);
[483]190
[493]191    /* Methods */
[636]192    inline LObject **GetData() { return m_data; }
[493]193    LObject *Get(int x);
[483]194
[493]195    /* Members */
[636]196    size_t m_len;
[483]197
198private:
[636]199    LObject *m_data[1]; /* Can be allocated much larger than 1 */
[482]200};
[2]201
[492]202struct LChar : LObject
[2]203{
[496]204    /* Factories */
[493]205    static LChar *Create(uint16_t ch);
206
[636]207    /* Methods */
208    uint16_t GetValue();
209
[496]210    /* Members */
[636]211    uint16_t m_ch;
[482]212};
[2]213
[492]214struct LPointer : LObject
[2]215{
[496]216    /* Factories */
[493]217    static LPointer *Create(void *addr);
218
[496]219    /* Members */
[636]220    void *m_addr;
[482]221};
[2]222
[492]223struct LFixedPoint : LObject
[2]224{
[496]225    /* Factories */
[493]226    static LFixedPoint *Create(int32_t x);
227
[496]228    /* Members */
[636]229    int32_t m_fixed;
[482]230};
[2]231
[636]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; }
[489]253static inline ltype item_type(void *x) { if (x) return *(ltype *)x; return L_CONS_CELL; }
[2]254
255void perm_space();
256void tmp_space();
257void *lpointer_value(void *lpointer);
[16]258int32_t lnumber_value(void *lnumber);
[2]259long lfixed_point_value(void *c);
260void *lisp_atom(void *i);
[496]261LObject *lcdr(void *c);
262LObject *lcar(void *c);
[2]263void *lisp_eq(void *n1, void *n2);
264void *lisp_equal(void *n1, void *n2);
265void *eval_block(void *list);
[504]266void resize_tmp(size_t new_size);
267void resize_perm(size_t new_size);
[2]268
269void push_onto_list(void *object, void *&list);
[493]270LSymbol *add_c_object(void *symbol, int index);
[492]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);
[2]274int read_ltoken(char *&s, char *buffer);
275void print_trace_stack(int max_levels);
276
277
[492]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);
[2]281
[501]282LUserFunction *new_lisp_user_function(LList *arg_list, LList *block_list);
[2]283
[492]284LSysFunction *new_user_lisp_function(int min_args, int max_args, int fun_number);
[2]285
286int end_of_program(char *s);
287
288void *nth(int num, void *list);
[43]289int32_t lisp_atan2(int32_t dy, int32_t dx);
290int32_t lisp_sin(int32_t x);
291int32_t lisp_cos(int32_t x);
[2]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
[484]305// FIXME: get rid of this later
[492]306static inline void *symbol_value(void *sym) { return ((LSymbol *)sym)->GetValue(); }
307static inline char *lstring_value(void *str) { return ((LString *)str)->GetString(); }
[2]308
[489]309#include "lisp_opt.h"
[2]310
311#endif
Note: See TracBrowser for help on using the repository browser.