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 |
#define CAR(X) ((X)->content.c->car) |
26 |
#define CDR(X) ((X)->content.c->cdr) |
27 |
|
28 |
/* printf, sscanf, fgets, fprintf, fopen, perror */ |
29 |
#include <stdio.h> |
30 |
/* exit, EXIT_SUCCESS, malloc, free */ |
31 |
#include <stdlib.h> |
32 |
/* NULL */ |
33 |
#include <stddef.h> |
34 |
/* dlopen, dlsym, dlerror */ |
35 |
#include <dlfcn.h> |
36 |
/* strcmp, strcpy, strlen, strcat, strdup */ |
37 |
#include <string.h> |
38 |
/* getopt, STDIN_FILENO, STDOUT_FILENO, usleep */ |
39 |
#include <unistd.h> |
40 |
/* EX_NOINPUT, EX_USAGE */ |
41 |
#include <sysexits.h> |
42 |
/* assert */ |
43 |
#include <assert.h> |
44 |
/* waitpid */ |
45 |
#include <sys/wait.h> |
46 |
/* va_list, va_start, va_arg, va_end */ |
47 |
#include <stdarg.h> |
48 |
/* time */ |
49 |
#include <time.h> |
50 |
|
51 |
#ifdef __linux__ |
52 |
/* mtrace, muntrace */ |
53 |
#include <mcheck.h> |
54 |
/* ioctl */ |
55 |
#include <sys/ioctl.h> |
56 |
/* KDMKTONE */ |
57 |
#include <linux/kd.h> |
58 |
#endif /* __linux__ */ |
59 |
|
60 |
|
61 |
|
62 |
/* First, define some types. */ |
63 |
|
64 |
struct cons_struct; |
65 |
struct symbol_struct; |
66 |
struct environment_struct; |
67 |
|
68 |
/* A type for pointers to external functions */ |
69 |
typedef void (*funcp)(struct environment_struct*); |
70 |
/* funcp is a pointer to a void function (environment *) */ |
71 |
|
72 |
enum type_enum { |
73 |
unknown, |
74 |
empty, /* The empty list */ |
75 |
integer, |
76 |
tfloat, |
77 |
string, |
78 |
func, /* Function pointer */ |
79 |
symb, /* Symbol */ |
80 |
tcons, /* A pair of two values */ |
81 |
port /* An I/O port */ |
82 |
}; /* Type of stack element */ |
83 |
|
84 |
|
85 |
/* A value of some type */ |
86 |
typedef struct { |
87 |
enum type_enum type:4; |
88 |
|
89 |
union { |
90 |
struct { |
91 |
unsigned int mark:1; /* Used internally in the GC */ |
92 |
unsigned int protect:1; /* Protect from GC */ |
93 |
} flag; |
94 |
unsigned int no_gc:2; /* Both flags as one integer */ |
95 |
} gc; /* Garbage collector stuff */ |
96 |
|
97 |
union { |
98 |
void *ptr; /* Pointer to the content */ |
99 |
struct cons_struct *c; /* ...or a pointer to a cons cell */ |
100 |
struct symbol_struct *sym; /* ...or a pointer to a symbol */ |
101 |
FILE *p; /* ...or an I/O stream */ |
102 |
int i; /* ...or an integer */ |
103 |
float f; /* ...or a floating point number */ |
104 |
funcp func; /* ...or a function pointer */ |
105 |
char *string; /* ...or a string */ |
106 |
} content; /* Stores a pointer or an integer */ |
107 |
|
108 |
} value; |
109 |
|
110 |
/* An item (value) on a stack */ |
111 |
typedef struct stackitem_struct |
112 |
{ |
113 |
value *item; /* The value on the stack */ |
114 |
/* (This is never NULL) */ |
115 |
struct stackitem_struct *next; /* Next item */ |
116 |
} stackitem; |
117 |
|
118 |
typedef struct cons_struct { /* A pair of two values */ |
119 |
value *car; |
120 |
value *cdr; |
121 |
} pair; |
122 |
|
123 |
/* A symbol with a name and possible value */ |
124 |
/* (These do not need reference counters, they are kept unique by |
125 |
hashing.) */ |
126 |
typedef struct symbol_struct { |
127 |
char *id; /* Symbol name */ |
128 |
value *val; /* The value (if any) bound to it */ |
129 |
struct symbol_struct *next; /* In case of hashing conflicts, a */ |
130 |
} symbol; /* symbol is a kind of stack item. */ |
131 |
|
132 |
/* A type for a hash table for symbols */ |
133 |
typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
134 |
|
135 |
/* An environment; gives access to the stack and a hash table of |
136 |
defined symbols */ |
137 |
typedef struct { |
138 |
value *head; /* Head of the stack */ |
139 |
hashtbl symbols; /* Hash table of all variable bindings */ |
140 |
int err; /* Error flag */ |
141 |
char *errsymb; |
142 |
char *in_string; /* Input pending to be read */ |
143 |
char *free_string; /* Free this string when all input is |
144 |
read from in_string */ |
145 |
FILE *inputstream; /* stdin or a file, most likely */ |
146 |
int interactive; /* print prompts, stack, etc */ |
147 |
|
148 |
/* Garbage Collector stuff*/ |
149 |
stackitem *gc_ref; /* Stack of all allocated values */ |
150 |
int gc_limit; /* Run GC when this much is allocated */ |
151 |
int gc_count; /* Amount currently allocated */ |
152 |
} environment; |
153 |
|
154 |
|
155 |
void init_env(environment*); |
156 |
void printerr(environment*); |
157 |
extern void toss(environment*); |
158 |
symbol **hash(hashtbl, const char*); |
159 |
value* new_val(environment*); |
160 |
void gc_mark(value*); |
161 |
void gc_maybe(environment*); |
162 |
extern void gc_init(environment*); |
163 |
void protect(value*); |
164 |
void unprotect(value*); |
165 |
void push_val(environment*, value*); |
166 |
void push_int(environment*, int); |
167 |
void push_float(environment*, float); |
168 |
void push_cstring(environment*, const char*); |
169 |
char *mangle_str(const char*); |
170 |
extern void mangle(environment*); |
171 |
void push_sym(environment*, const char*); |
172 |
extern void nl(environment*); |
173 |
extern void nlport(environment*); |
174 |
extern void type(environment*); |
175 |
void print_val(environment *, value*, int, stackitem*, FILE*); |
176 |
extern void print_(environment*); |
177 |
extern void print(environment*); |
178 |
extern void princ_(environment*); |
179 |
extern void princ(environment*); |
180 |
extern void printport_(environment*); |
181 |
extern void printport(environment*); |
182 |
extern void princport_(environment*); |
183 |
extern void princport(environment*); |
184 |
void print_st(environment*, value*, long); |
185 |
extern void printstack(environment*); |
186 |
extern void swap(environment*); |
187 |
extern void rot(environment*); |
188 |
extern void rcl(environment*); |
189 |
extern void eval(environment*); |
190 |
extern void rev(environment*); |
191 |
extern void pack(environment*); |
192 |
extern void expand(environment*); |
193 |
extern void eq(environment*); |
194 |
extern void not(environment*); |
195 |
extern void neq(environment*); |
196 |
extern void def(environment*); |
197 |
extern void quit(environment*); |
198 |
extern void clear(environment*); |
199 |
extern void words(environment*); |
200 |
void forget_sym(symbol**); |
201 |
extern void forget(environment*); |
202 |
extern void errn(environment*); |
203 |
extern void sx_2b(environment*); |
204 |
extern void sx_2d(environment*); |
205 |
extern void sx_3e(environment*); |
206 |
extern void sx_3c(environment*); |
207 |
extern void sx_3c3d(environment*); |
208 |
extern void sx_3e3d(environment*); |
209 |
value *copy_val(environment*, value*); |
210 |
extern void sx_647570(environment*); |
211 |
extern void sx_6966(environment*); |
212 |
extern void ifelse(environment*); |
213 |
extern void sx_656c7365(environment*); |
214 |
extern void then(environment*); |
215 |
extern void sx_7768696c65(environment*); |
216 |
extern void sx_666f72(environment*); |
217 |
extern void foreach(environment*); |
218 |
extern void to(environment*); |
219 |
extern void readline(environment*); |
220 |
extern void readlineport(environment*); |
221 |
void readlinestream(environment*, FILE*); |
222 |
extern void sx_72656164(environment*); |
223 |
extern void readport(environment*); |
224 |
void readstream(environment*, FILE*); |
225 |
extern void beep(environment*); |
226 |
extern void sx_77616974(environment*); |
227 |
extern void copying(environment*); |
228 |
extern void warranty(environment*); |
229 |
extern void sx_2a(environment*); |
230 |
extern void sx_2f(environment*); |
231 |
extern void mod(environment*); |
232 |
extern void sx_646976(environment*); |
233 |
extern void setcar(environment*); |
234 |
extern void setcdr(environment*); |
235 |
extern void car(environment*); |
236 |
extern void cdr(environment*); |
237 |
extern void cons(environment*); |
238 |
extern void assq(environment*); |
239 |
void assocgen(environment*, funcp); |
240 |
extern void sx_646f(environment *); |
241 |
extern void sx_6f70656e(environment*); |
242 |
extern void sx_636c6f7365(environment*); |
243 |
int check_args(environment*, int, ...); |