1 |
|
2 |
#define HASHTBLSIZE 2048 |
3 |
|
4 |
/* First, define some types. */ |
5 |
|
6 |
/* A value of some type */ |
7 |
typedef struct { |
8 |
enum { |
9 |
integer, |
10 |
string, |
11 |
func, /* Function pointer */ |
12 |
symb, |
13 |
list |
14 |
} type:3; /* Type of stack element */ |
15 |
|
16 |
int gc_garb:1; |
17 |
|
18 |
union { |
19 |
void *ptr; /* Pointer to the content */ |
20 |
int val; /* ...or an integer */ |
21 |
} content; /* Stores a pointer or an integer */ |
22 |
|
23 |
} value; |
24 |
|
25 |
/* A symbol with a name and possible value */ |
26 |
/* (These do not need reference counters, they are kept unique by |
27 |
hashing.) */ |
28 |
typedef struct symbol_struct { |
29 |
char *id; /* Symbol name */ |
30 |
value *val; /* The value (if any) bound to it */ |
31 |
struct symbol_struct *next; /* In case of hashing conflicts, a */ |
32 |
} symbol; /* symbol is a kind of stack item. */ |
33 |
|
34 |
/* A type for a hash table for symbols */ |
35 |
typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
36 |
|
37 |
/* An item (value) on a stack */ |
38 |
typedef struct stackitem_struct |
39 |
{ |
40 |
value *item; /* The value on the stack */ |
41 |
/* (This is never NULL) */ |
42 |
struct stackitem_struct *next; /* Next item */ |
43 |
} stackitem; |
44 |
|
45 |
/* An environment; gives access to the stack and a hash table of |
46 |
defined symbols */ |
47 |
typedef struct { |
48 |
stackitem *gc_ref; |
49 |
stackitem *gc_protect; |
50 |
int gc_limit, gc_count; |
51 |
|
52 |
stackitem *head; /* Head of the stack */ |
53 |
hashtbl symbols; /* Hash table of all variable bindings */ |
54 |
int err; /* Error flag */ |
55 |
char *in_string; /* Input pending to be read */ |
56 |
char *free_string; /* Free this string when all input is |
57 |
read from in_string */ |
58 |
FILE *inputstream; /* stdin or a file, most likely */ |
59 |
int interactive; /* print prompts, stack, etc */ |
60 |
} environment; |
61 |
|
62 |
/* A type for pointers to external functions */ |
63 |
typedef void (*funcp)(environment*); /* funcp is a pointer to a void |
64 |
function (environment *) */ |
65 |
|
66 |
void init_env(environment*); |
67 |
void printerr(const char*); |
68 |
extern void toss(environment*); |
69 |
symbol **hash(hashtbl, const char*); |
70 |
value* new_val(environment*); |
71 |
void gc_mark(value*); |
72 |
extern void gc_init(environment*); |
73 |
void push_val(environment*, value*); |
74 |
void push_int(environment*, int); |
75 |
void push_cstring(environment*, const char*); |
76 |
char *mangle_str(const char*); |
77 |
extern void mangle(environment*); |
78 |
void push_sym(environment*, const char*); |
79 |
extern void nl(); |
80 |
extern void type(environment*); |
81 |
void print_h(stackitem*, int); |
82 |
extern void print_(environment*); |
83 |
extern void print(environment*); |
84 |
extern void princ_(environment*); |
85 |
extern void princ(environment*); |
86 |
void print_st(stackitem*, long); |
87 |
extern void printstack(environment*); |
88 |
extern void swap(environment*); |
89 |
extern void rot(environment*); |
90 |
extern void rcl(environment*); |
91 |
extern void eval(environment*); |
92 |
extern void rev(environment*); |
93 |
extern void pack(environment*); |
94 |
extern void expand(environment*); |
95 |
extern void eq(environment*); |
96 |
extern void not(environment*); |
97 |
extern void neq(environment*); |
98 |
extern void def(environment*); |
99 |
extern void quit(environment*); |
100 |
extern void clear(environment*); |
101 |
extern void words(environment*); |
102 |
void forget_sym(symbol**); |
103 |
extern void forget(environment*); |
104 |
extern void errn(environment*); |
105 |
extern void sx_2b(environment*); |
106 |
extern void sx_2d(environment*); |
107 |
extern void sx_3e(environment*); |
108 |
value *copy_val(environment*, value*); |
109 |
extern void sx_647570(environment*); |
110 |
extern void sx_6966(environment*); |
111 |
extern void ifelse(environment*); |
112 |
extern void sx_7768696c65(environment*); |
113 |
extern void sx_666f72(environment*); |
114 |
extern void to(environment*); |
115 |
extern void readline(environment*); |
116 |
extern void sx_72656164(environment*); |
117 |
extern void foreach(environment*); |
118 |
void protect(environment*, value*); |
119 |
void unprotect(environment*); |