-
Notifications
You must be signed in to change notification settings - Fork 0
/
wannabe-lisp.h
132 lines (115 loc) · 3.36 KB
/
wannabe-lisp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef WBLISP_H
#define WBLISP_H
#include <setjmp.h>
/* constants */
#define INITIAL_ENV_ALLOC 8
#define ALLOC_EXPAND 16
#define SYMBOL_NAME_MAXLEN 32
#define ERROR_TEXT_BUFSIZ 1024 * 2
#define LINEBUFSIZ 1024
#define LOGICAL_LINE_BUFSIZ 1024 * 512
#define DEBUGLOG_BUFSIZ 4096
/* REPL stuff, see main.c */
extern int interactive;
extern jmp_buf repl_jmp;
/* REPL logging */
extern int save_mode;
extern FILE *save_file;
/*
* Types of objects. used by field `type'
* of `list_t'
*/
enum {
SYMBOL = 0,
NUMBER,
LIST,
CONS,
CLOSURE,
BOOL
};
/*
* Structure returned by lookup()
* routine in file environment.c
*
* Not sure why this isn't simply
* replaced by the pointer e + i
* aka &e[i]
*/
typedef struct env_ref {
struct env* e;
int i;
} env_ref_t;
/*
* Environment data structure
*/
typedef struct env {
int count; /* number of symbols */
int alloc; /* allocated space for symbols */
char **sym; /* symbol names */
void **ptr; /* symbol bound-object pointers */
struct env *father; /* father environment */
} env_t;
/*
* The global environment
* (must be defined below env_t definition,
* at least with my compiler ...
*/
extern env_t *global;
/*
* List data structure
*/
typedef struct list {
char* head; /* used for symbol names */
int type; /* type; see enum at top of file */
int val; /* used for bools (?) and ints */
env_t *closure; /* closure environment (for procedures) */
int cc; /* child count (for lists/conses) */
int ca; /* child allocation */
struct list **c; /* array of children */
} list_t;
extern list_t* eval(list_t *l, env_t *env);
extern void add_child(list_t *parent, list_t* child);
extern void printout(list_t *l, char *s);
extern list_t *new_list(void);
extern list_t* do_prim_op(char *name, list_t *args);
extern env_t* new_env(void);
extern list_t* apply(list_t *proc, list_t *args);
extern env_ref_t lookup(env_t *e, char *sym);
extern void evlist(list_t* l, env_t *env);
extern list_t* makebool(int cbool);
extern void env_add(env_t *e, char *sym, void *p);
extern void install_primitives(env_t *env);
extern char* build(list_t* l, char *expr);
extern int isnum(char c);
extern list_t* mksym(char *s);
extern void *c_malloc(long size);
extern void *c_realloc(void *ptr, long size);
extern void strip_nl(char *s);
extern list_t* makelist(list_t* argl);
extern void marksweep(env_t *e);
extern void add_ptr(void *p);
extern void gc();
extern void do_mark(void* p, int m);
extern void gc_selfdestroy();
extern void env_set(env_t *e, char *sym, void *p);
extern list_t* eval_apply_tco(int oper, list_t *a_l, env_t *a_env, list_t *a_proc, list_t *a_args);
#define call_eval(l, e) eval_apply_tco(0, l, e, NULL, NULL)
#define call_apply(p, a) eval_apply_tco(1, NULL, NULL, p, a)
extern list_t *copy_list(list_t *l);
extern int code_error();
extern int do_read_file(char *buf, FILE *f, int silent);
extern int check_comment(char *s);
extern void error_msg(char *s);
extern void fatal_error_msg(char *s);
extern void load_code_from_file(char *fil);
extern list_t* cons2list(list_t *);
extern void stacktracer_reset();
extern void stacktracer_init();
extern void stacktracer_destroy();
extern void stacktracer_push(char *s);
extern void stacktracer_show(char *s);
extern void stacktracer_prebarf();
extern void stacktracer_barf();
extern void stacktracer_push_sym(char *symb, char *prnt);
extern void* better_realloc(void *ptr, long size);
#endif