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