1 |
/* |
2 |
stack - an interactive interpreter for a stack-based language |
3 |
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License |
16 |
along with this program; if not, write to the Free Software |
17 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
|
19 |
Authors: Mats Alritzson <masse@fukt.bth.se> |
20 |
Teddy Hogeborn <teddy@fukt.bth.se> |
21 |
*/ |
22 |
|
23 |
#define HASHTBLSIZE 2048 |
24 |
|
25 |
/* First, define some types. */ |
26 |
|
27 |
struct cons_struct; |
28 |
struct symbol_struct; |
29 |
|
30 |
/* A value of some type */ |
31 |
typedef struct { |
32 |
enum { |
33 |
empty, /* The empty list */ |
34 |
integer, |
35 |
tfloat, |
36 |
string, |
37 |
func, /* Function pointer */ |
38 |
symb, /* Symbol */ |
39 |
tcons, /* A pair of two values */ |
40 |
port /* An I/O port */ |
41 |
} type:4; /* Type of stack element */ |
42 |
|
43 |
union { |
44 |
struct { |
45 |
unsigned int mark:1; /* Used internally in the GC */ |
46 |
unsigned int protect:1; /* Protect from GC */ |
47 |
} flag; |
48 |
unsigned int no_gc:2; /* Both flags as one integer */ |
49 |
} gc; /* Garbage collector stuff */ |
50 |
|
51 |
union { |
52 |
void *ptr; /* Pointer to the content */ |
53 |
struct cons_struct *c; /* ...or a pointer to a cons cell */ |
54 |
struct symbol_struct *sym; /* ...or a pointer to a symbol */ |
55 |
FILE *p; /* ...or an I/O stream */ |
56 |
int i; /* ...or an integer */ |
57 |
float f; /* ...or a floating point number */ |
58 |
} content; /* Stores a pointer or an integer */ |
59 |
|
60 |
} value; |
61 |
|
62 |
/* An item (value) on a stack */ |
63 |
typedef struct stackitem_struct |
64 |
{ |
65 |
value *item; /* The value on the stack */ |
66 |
/* (This is never NULL) */ |
67 |
struct stackitem_struct *next; /* Next item */ |
68 |
} stackitem; |
69 |
|
70 |
typedef struct cons_struct { /* A pair of two values */ |
71 |
value *car; |
72 |
value *cdr; |
73 |
} pair; |
74 |
|
75 |
/* A symbol with a name and possible value */ |
76 |
/* (These do not need reference counters, they are kept unique by |
77 |
hashing.) */ |
78 |
typedef struct symbol_struct { |
79 |
char *id; /* Symbol name */ |
80 |
value *val; /* The value (if any) bound to it */ |
81 |
struct symbol_struct *next; /* In case of hashing conflicts, a */ |
82 |
} symbol; /* symbol is a kind of stack item. */ |
83 |
|
84 |
/* A type for a hash table for symbols */ |
85 |
typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
86 |
|
87 |
/* An environment; gives access to the stack and a hash table of |
88 |
defined symbols */ |
89 |
typedef struct { |
90 |
value *head; /* Head of the stack */ |
91 |
hashtbl symbols; /* Hash table of all variable bindings */ |
92 |
int err; /* Error flag */ |
93 |
char *in_string; /* Input pending to be read */ |
94 |
char *free_string; /* Free this string when all input is |
95 |
read from in_string */ |
96 |
FILE *inputstream; /* stdin or a file, most likely */ |
97 |
int interactive; /* print prompts, stack, etc */ |
98 |
|
99 |
/* Garbage Collector stuff*/ |
100 |
stackitem *gc_ref; /* Stack of all allocated values */ |
101 |
int gc_limit; /* Run GC when this much is allocated */ |
102 |
int gc_count; /* Amount currently allocated */ |
103 |
} environment; |
104 |
|
105 |
/* A type for pointers to external functions */ |
106 |
typedef void (*funcp)(environment*); /* funcp is a pointer to a void |
107 |
function (environment *) */ |
108 |
|
109 |
void init_env(environment*); |
110 |
void printerr(const char*); |
111 |
extern void toss(environment*); |
112 |
symbol **hash(hashtbl, const char*); |
113 |
value* new_val(environment*); |
114 |
void gc_mark(value*); |
115 |
void gc_maybe(environment*); |
116 |
extern void gc_init(environment*); |
117 |
void protect(value*); |
118 |
void unprotect(value*); |
119 |
void push_val(environment*, value*); |
120 |
void push_int(environment*, int); |
121 |
void push_float(environment*, float); |
122 |
void push_cstring(environment*, const char*); |
123 |
char *mangle_str(const char*); |
124 |
extern void mangle(environment*); |
125 |
void push_sym(environment*, const char*); |
126 |
extern void nl(environment*); |
127 |
extern void nlport(environment*); |
128 |
extern void type(environment*); |
129 |
void print_val(environment *, value*, int, stackitem*, FILE*); |
130 |
extern void print_(environment*); |
131 |
extern void print(environment*); |
132 |
extern void princ_(environment*); |
133 |
extern void princ(environment*); |
134 |
extern void printport_(environment*); |
135 |
extern void printport(environment*); |
136 |
extern void princport_(environment*); |
137 |
extern void princport(environment*); |
138 |
void print_st(environment*, value*, long); |
139 |
extern void printstack(environment*); |
140 |
extern void swap(environment*); |
141 |
extern void rot(environment*); |
142 |
extern void rcl(environment*); |
143 |
extern void eval(environment*); |
144 |
extern void rev(environment*); |
145 |
extern void pack(environment*); |
146 |
extern void expand(environment*); |
147 |
extern void eq(environment*); |
148 |
extern void not(environment*); |
149 |
extern void neq(environment*); |
150 |
extern void def(environment*); |
151 |
extern void quit(environment*); |
152 |
extern void clear(environment*); |
153 |
extern void words(environment*); |
154 |
void forget_sym(symbol**); |
155 |
extern void forget(environment*); |
156 |
extern void errn(environment*); |
157 |
extern void sx_2b(environment*); |
158 |
extern void sx_2d(environment*); |
159 |
extern void sx_3e(environment*); |
160 |
extern void sx_3c(environment*); |
161 |
extern void sx_3c3d(environment*); |
162 |
extern void sx_3e3d(environment*); |
163 |
value *copy_val(environment*, value*); |
164 |
extern void sx_647570(environment*); |
165 |
extern void sx_6966(environment*); |
166 |
extern void ifelse(environment*); |
167 |
extern void sx_656c7365(environment*); |
168 |
extern void then(environment*); |
169 |
extern void sx_7768696c65(environment*); |
170 |
extern void sx_666f72(environment*); |
171 |
extern void foreach(environment*); |
172 |
extern void to(environment*); |
173 |
extern void readline(environment*); |
174 |
extern void readlineport(environment*); |
175 |
void readlinestream(environment*, FILE*); |
176 |
extern void sx_72656164(environment*); |
177 |
extern void readport(environment*); |
178 |
void readstream(environment*, FILE*); |
179 |
extern void beep(environment*); |
180 |
extern void sx_77616974(environment*); |
181 |
extern void copying(environment*); |
182 |
extern void warranty(environment*); |
183 |
extern void sx_2a(environment*); |
184 |
extern void sx_2f(environment*); |
185 |
extern void mod(environment*); |
186 |
extern void sx_646976(environment*); |
187 |
extern void setcar(environment*); |
188 |
extern void setcdr(environment*); |
189 |
extern void car(environment*); |
190 |
extern void cdr(environment*); |
191 |
extern void cons(environment*); |
192 |
extern void assq(environment*); |
193 |
void assocgen(environment*, funcp); |
194 |
extern void sx_646f(environment *); |
195 |
extern void sx_6f70656e(environment*); |
196 |
extern void sx_636c6f7365(environment*); |