1 |
/* printf, sscanf, fgets, fprintf */ |
/* -*- coding: utf-8; -*- */ |
2 |
#include <stdio.h> |
/* |
3 |
/* exit, EXIT_SUCCESS, malloc, free */ |
stack - an interactive interpreter for a stack-based language |
4 |
#include <stdlib.h> |
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn |
5 |
/* NULL */ |
|
6 |
#include <stddef.h> |
This program is free software; you can redistribute it and/or modify |
7 |
/* dlopen, dlsym, dlerror */ |
it under the terms of the GNU General Public License as published by |
8 |
#include <dlfcn.h> |
the Free Software Foundation; either version 2 of the License, or |
9 |
/* strcmp, strcpy, strlen, strcat, strdup */ |
(at your option) any later version. |
10 |
#include <string.h> |
|
11 |
|
This program is distributed in the hope that it will be useful, |
12 |
#define HASHTBLSIZE 65536 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
/* First, define some types. */ |
GNU General Public License for more details. |
15 |
|
|
16 |
/* A value of some type */ |
You should have received a copy of the GNU General Public License |
17 |
typedef struct { |
along with this program; if not, write to the Free Software |
18 |
enum { |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
integer, |
|
20 |
string, |
Authors: Mats Alritzson <masse@fukt.bth.se> |
21 |
func, /* Function pointer */ |
Teddy Hogeborn <teddy@fukt.bth.se> |
22 |
symb, |
*/ |
23 |
list |
|
24 |
} type; /* Type of stack element */ |
#include "stack.h" |
25 |
|
|
26 |
union { |
const char* start_message= "Stack version $Revision$\n\ |
27 |
void *ptr; /* Pointer to the content */ |
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn\n\ |
28 |
int val; /* ...or an integer */ |
Stack comes with ABSOLUTELY NO WARRANTY; for details type 'warranty;'.\n\ |
29 |
} content; /* Stores a pointer or an integer */ |
This is free software, and you are welcome to redistribute it\n\ |
30 |
|
under certain conditions; type 'copying;' for details.\n"; |
31 |
int refcount; /* Reference counter */ |
|
|
|
|
|
} value; |
|
|
|
|
|
/* A symbol with a name and possible value */ |
|
|
/* (These do not need reference counters, they are kept unique by |
|
|
hashing.) */ |
|
|
typedef struct symbol_struct { |
|
|
char *id; /* Symbol name */ |
|
|
value *val; /* The value (if any) bound to it */ |
|
|
struct symbol_struct *next; /* In case of hashing conflicts, a */ |
|
|
} symbol; /* symbol is a kind of stack item. */ |
|
|
|
|
|
/* A type for a hash table for symbols */ |
|
|
typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
|
|
|
|
|
/* An item (value) on a stack */ |
|
|
typedef struct stackitem_struct |
|
|
{ |
|
|
value *item; /* The value on the stack */ |
|
|
struct stackitem_struct *next; /* Next item */ |
|
|
} stackitem; |
|
|
|
|
|
/* An environment; gives access to the stack and a hash table of |
|
|
defined symbols */ |
|
|
typedef struct { |
|
|
stackitem *head; /* Head of the stack */ |
|
|
hashtbl symbols; /* Hash table of all variable bindings */ |
|
|
int err; /* Error flag */ |
|
|
int non_eval_flag; |
|
|
} environment; |
|
|
|
|
|
/* A type for pointers to external functions */ |
|
|
typedef void (*funcp)(environment *); /* funcp is a pointer to a void |
|
|
function (environment *) */ |
|
32 |
|
|
33 |
/* Initialize a newly created environment */ |
/* Initialize a newly created environment */ |
34 |
void init_env(environment *env) |
void init_env(environment *env) |
35 |
{ |
{ |
36 |
int i; |
int i; |
37 |
|
|
38 |
env->err= 0; |
env->gc_limit= 400000; |
39 |
env->non_eval_flag= 0; |
env->gc_count= 0; |
40 |
|
env->gc_ref= NULL; |
41 |
|
|
42 |
|
env->head= new_val(env); |
43 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
44 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
45 |
|
env->err= 0; |
46 |
|
env->in_string= NULL; |
47 |
|
env->free_string= NULL; |
48 |
|
env->inputstream= stdin; |
49 |
|
env->interactive= 1; |
50 |
} |
} |
51 |
|
|
|
void printerr(const char* in_string) { |
|
|
fprintf(stderr, "Err: %s\n", in_string); |
|
|
} |
|
52 |
|
|
53 |
/* Throw away a value */ |
void printerr(const char* in_string) |
|
void free_val(value *val){ |
|
|
stackitem *item, *temp; |
|
|
|
|
|
val->refcount--; /* Decrease the reference count */ |
|
|
if(val->refcount == 0){ |
|
|
switch (val->type){ /* and free the contents if necessary */ |
|
|
case string: |
|
|
free(val->content.ptr); |
|
|
break; |
|
|
case list: /* lists needs to be freed recursively */ |
|
|
item=val->content.ptr; |
|
|
while(item != NULL) { /* for all stack items */ |
|
|
free_val(item->item); /* free the value */ |
|
|
temp=item->next; /* save next ptr */ |
|
|
free(item); /* free the stackitem */ |
|
|
item=temp; /* go to next stackitem */ |
|
|
} |
|
|
free(val); /* Free the actual list value */ |
|
|
break; |
|
|
default: |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
/* Discard the top element of the stack. */ |
|
|
extern void toss(environment *env) |
|
54 |
{ |
{ |
55 |
stackitem *temp= env->head; |
fprintf(stderr, "Err: %s\n", in_string); |
|
|
|
|
if((env->head)==NULL) { |
|
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
|
|
|
free_val(env->head->item); /* Free the value */ |
|
|
env->head= env->head->next; /* Remove the top stack item */ |
|
|
free(temp); /* Free the old top stack item */ |
|
56 |
} |
} |
57 |
|
|
58 |
|
|
59 |
/* Returns a pointer to a pointer to an element in the hash table. */ |
/* Returns a pointer to a pointer to an element in the hash table. */ |
60 |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
61 |
{ |
{ |
85 |
} |
} |
86 |
} |
} |
87 |
|
|
88 |
/* Generic push function. */ |
|
89 |
void push(stackitem** stack_head, stackitem* in_item) |
/* Create new value */ |
90 |
|
value* new_val(environment *env) |
91 |
|
{ |
92 |
|
value *nval= malloc(sizeof(value)); |
93 |
|
stackitem *nitem= malloc(sizeof(stackitem)); |
94 |
|
|
95 |
|
assert(nval != NULL); |
96 |
|
assert(nitem != NULL); |
97 |
|
|
98 |
|
nval->content.ptr= NULL; |
99 |
|
nval->type= empty; |
100 |
|
|
101 |
|
nitem->item= nval; |
102 |
|
nitem->next= env->gc_ref; |
103 |
|
|
104 |
|
env->gc_ref= nitem; |
105 |
|
|
106 |
|
env->gc_count += sizeof(value); |
107 |
|
nval->gc.flag.mark= 0; |
108 |
|
nval->gc.flag.protect= 0; |
109 |
|
|
110 |
|
return nval; |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
/* Mark values recursively. |
115 |
|
Marked values are not collected by the GC. */ |
116 |
|
inline void gc_mark(value *val) |
117 |
{ |
{ |
118 |
in_item->next= *stack_head; |
if(val==NULL || val->gc.flag.mark) |
119 |
*stack_head= in_item; |
return; |
120 |
|
|
121 |
|
val->gc.flag.mark= 1; |
122 |
|
|
123 |
|
if(val->type==tcons) { |
124 |
|
gc_mark(CAR(val)); |
125 |
|
gc_mark(CDR(val)); |
126 |
|
} |
127 |
} |
} |
128 |
|
|
129 |
|
|
130 |
|
/* Start GC */ |
131 |
|
extern void gc_init(environment *env) |
132 |
|
{ |
133 |
|
stackitem *new_head= NULL, *titem; |
134 |
|
symbol *tsymb; |
135 |
|
int i; |
136 |
|
|
137 |
|
if(env->interactive) |
138 |
|
printf("Garbage collecting."); |
139 |
|
|
140 |
|
/* Mark values on stack */ |
141 |
|
gc_mark(env->head); |
142 |
|
|
143 |
|
if(env->interactive) |
144 |
|
printf("."); |
145 |
|
|
146 |
|
/* Mark values in hashtable */ |
147 |
|
for(i= 0; i<HASHTBLSIZE; i++) |
148 |
|
for(tsymb= env->symbols[i]; tsymb!=NULL; tsymb= tsymb->next) |
149 |
|
if (tsymb->val != NULL) |
150 |
|
gc_mark(tsymb->val); |
151 |
|
|
152 |
|
if(env->interactive) |
153 |
|
printf("."); |
154 |
|
|
155 |
|
env->gc_count= 0; |
156 |
|
|
157 |
|
while(env->gc_ref!=NULL) { /* Sweep unused values */ |
158 |
|
if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */ |
159 |
|
|
160 |
|
/* Remove content */ |
161 |
|
switch(env->gc_ref->item->type){ |
162 |
|
case string: |
163 |
|
free(env->gc_ref->item->content.string); |
164 |
|
break; |
165 |
|
case tcons: |
166 |
|
free(env->gc_ref->item->content.c); |
167 |
|
break; |
168 |
|
case port: |
169 |
|
case empty: |
170 |
|
case integer: |
171 |
|
case tfloat: |
172 |
|
case func: |
173 |
|
case symb: |
174 |
|
/* Symbol strings are freed when walking the hash table */ |
175 |
|
break; |
176 |
|
} |
177 |
|
|
178 |
|
free(env->gc_ref->item); /* Remove from gc_ref */ |
179 |
|
titem= env->gc_ref->next; |
180 |
|
free(env->gc_ref); /* Remove value */ |
181 |
|
env->gc_ref= titem; |
182 |
|
continue; |
183 |
|
} |
184 |
|
|
185 |
|
#ifdef DEBUG |
186 |
|
printf("Kept value (%p)", env->gc_ref->item); |
187 |
|
if(env->gc_ref->item->gc.flag.mark) |
188 |
|
printf(" (marked)"); |
189 |
|
if(env->gc_ref->item->gc.flag.protect) |
190 |
|
printf(" (protected)"); |
191 |
|
switch(env->gc_ref->item->type){ |
192 |
|
case integer: |
193 |
|
printf(" integer: %d", env->gc_ref->item->content.i); |
194 |
|
break; |
195 |
|
case func: |
196 |
|
printf(" func: %p", env->gc_ref->item->content.func); |
197 |
|
break; |
198 |
|
case symb: |
199 |
|
printf(" symb: %s", env->gc_ref->item->content.sym->id); |
200 |
|
break; |
201 |
|
case tcons: |
202 |
|
printf(" tcons: %p\t%p", CAR(env->gc_ref->item), |
203 |
|
CDR(env->gc_ref->item)); |
204 |
|
break; |
205 |
|
default: |
206 |
|
printf(" <unknown %d>", (env->gc_ref->item->type)); |
207 |
|
} |
208 |
|
printf("\n"); |
209 |
|
#endif /* DEBUG */ |
210 |
|
|
211 |
|
/* Keep values */ |
212 |
|
env->gc_count += sizeof(value); |
213 |
|
if(env->gc_ref->item->type==string) |
214 |
|
env->gc_count += strlen(env->gc_ref->item->content.string)+1; |
215 |
|
|
216 |
|
titem= env->gc_ref->next; |
217 |
|
env->gc_ref->next= new_head; |
218 |
|
new_head= env->gc_ref; |
219 |
|
new_head->item->gc.flag.mark= 0; |
220 |
|
env->gc_ref= titem; |
221 |
|
} |
222 |
|
|
223 |
|
if (env->gc_limit < env->gc_count*2) |
224 |
|
env->gc_limit= env->gc_count*2; |
225 |
|
|
226 |
|
env->gc_ref= new_head; |
227 |
|
|
228 |
|
if(env->interactive) |
229 |
|
printf("done (%d bytes still allocated)\n", env->gc_count); |
230 |
|
|
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
inline void gc_maybe(environment *env) |
235 |
|
{ |
236 |
|
if(env->gc_count < env->gc_limit) |
237 |
|
return; |
238 |
|
else |
239 |
|
return gc_init(env); |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
/* Protect values from GC */ |
244 |
|
void protect(value *val) |
245 |
|
{ |
246 |
|
if(val==NULL || val->gc.flag.protect) |
247 |
|
return; |
248 |
|
|
249 |
|
val->gc.flag.protect= 1; |
250 |
|
|
251 |
|
if(val->type==tcons) { |
252 |
|
protect(CAR(val)); |
253 |
|
protect(CDR(val)); |
254 |
|
} |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
|
/* Unprotect values from GC */ |
259 |
|
void unprotect(value *val) |
260 |
|
{ |
261 |
|
if(val==NULL || !(val->gc.flag.protect)) |
262 |
|
return; |
263 |
|
|
264 |
|
val->gc.flag.protect= 0; |
265 |
|
|
266 |
|
if(val->type==tcons) { |
267 |
|
unprotect(CAR(val)); |
268 |
|
unprotect(CDR(val)); |
269 |
|
} |
270 |
|
} |
271 |
|
|
272 |
|
|
273 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
274 |
void push_val(stackitem **stack_head, value *val) |
void push_val(environment *env, value *val) |
275 |
{ |
{ |
276 |
stackitem *new_item= malloc(sizeof(stackitem)); |
value *new_value= new_val(env); |
277 |
new_item->item= val; |
|
278 |
val->refcount++; |
new_value->content.c= malloc(sizeof(pair)); |
279 |
push(stack_head, new_item); |
assert(new_value->content.c!=NULL); |
280 |
|
env->gc_count += sizeof(pair); |
281 |
|
new_value->type= tcons; |
282 |
|
CAR(new_value)= val; |
283 |
|
CDR(new_value)= env->head; |
284 |
|
env->head= new_value; |
285 |
} |
} |
286 |
|
|
287 |
/* Push an integer onto the stack. */ |
|
288 |
void push_int(stackitem **stack_head, int in_val) |
/* Push an integer onto the stack */ |
289 |
|
void push_int(environment *env, int in_val) |
290 |
{ |
{ |
291 |
value *new_value= malloc(sizeof(value)); |
value *new_value= new_val(env); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item= new_value; |
|
292 |
|
|
293 |
new_value->content.val= in_val; |
new_value->content.i= in_val; |
294 |
new_value->type= integer; |
new_value->type= integer; |
|
new_value->refcount=1; |
|
295 |
|
|
296 |
push(stack_head, new_item); |
push_val(env, new_value); |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
/* Push a floating point number onto the stack */ |
301 |
|
void push_float(environment *env, float in_val) |
302 |
|
{ |
303 |
|
value *new_value= new_val(env); |
304 |
|
|
305 |
|
new_value->content.f= in_val; |
306 |
|
new_value->type= tfloat; |
307 |
|
|
308 |
|
push_val(env, new_value); |
309 |
} |
} |
310 |
|
|
311 |
|
|
312 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
313 |
void push_cstring(stackitem **stack_head, const char *in_string) |
void push_cstring(environment *env, const char *in_string) |
314 |
{ |
{ |
315 |
value *new_value= malloc(sizeof(value)); |
value *new_value= new_val(env); |
316 |
stackitem *new_item= malloc(sizeof(stackitem)); |
int length= strlen(in_string)+1; |
|
new_item->item=new_value; |
|
317 |
|
|
318 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.string= malloc(length); |
319 |
strcpy(new_value->content.ptr, in_string); |
assert(new_value != NULL); |
320 |
|
env->gc_count += length; |
321 |
|
strcpy(new_value->content.string, in_string); |
322 |
new_value->type= string; |
new_value->type= string; |
|
new_value->refcount=1; |
|
323 |
|
|
324 |
push(stack_head, new_item); |
push_val(env, new_value); |
325 |
} |
} |
326 |
|
|
327 |
|
|
328 |
/* Mangle a symbol name to a valid C identifier name */ |
/* Mangle a symbol name to a valid C identifier name */ |
329 |
char *mangle_str(const char *old_string){ |
char *mangle_str(const char *old_string) |
330 |
char validchars[] |
{ |
331 |
="0123456789abcdef"; |
char validchars[]= "0123456789abcdef"; |
332 |
char *new_string, *current; |
char *new_string, *current; |
333 |
|
|
334 |
new_string=malloc((strlen(old_string)*2)+4); |
new_string= malloc((strlen(old_string)*2)+4); |
335 |
|
assert(new_string != NULL); |
336 |
strcpy(new_string, "sx_"); /* Stack eXternal */ |
strcpy(new_string, "sx_"); /* Stack eXternal */ |
337 |
current=new_string+3; |
current= new_string+3; |
338 |
|
|
339 |
while(old_string[0] != '\0'){ |
while(old_string[0] != '\0'){ |
340 |
current[0]=validchars[(unsigned char)(old_string[0])/16]; |
current[0]= validchars[(unsigned char)(old_string[0])/16]; |
341 |
current[1]=validchars[(unsigned char)(old_string[0])%16]; |
current[1]= validchars[(unsigned char)(old_string[0])%16]; |
342 |
current+=2; |
current+= 2; |
343 |
old_string++; |
old_string++; |
344 |
} |
} |
345 |
current[0]='\0'; |
current[0]= '\0'; |
346 |
|
|
347 |
return new_string; /* The caller must free() it */ |
return new_string; /* The caller must free() it */ |
348 |
} |
} |
349 |
|
|
|
extern void mangle(environment *env){ |
|
|
value *new_value; |
|
|
char *new_string; |
|
|
|
|
|
if((env->head)==NULL) { |
|
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
|
|
|
if(env->head->item->type!=string) { |
|
|
printerr("Bad Argument Type"); |
|
|
env->err=2; |
|
|
return; |
|
|
} |
|
|
|
|
|
new_string= mangle_str((const char *)(env->head->item->content.ptr)); |
|
|
|
|
|
toss(env); |
|
|
if(env->err) return; |
|
|
|
|
|
new_value= malloc(sizeof(value)); |
|
|
new_value->content.ptr= new_string; |
|
|
new_value->type= string; |
|
|
new_value->refcount=1; |
|
|
|
|
|
push_val(&(env->head), new_value); |
|
|
} |
|
350 |
|
|
351 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
352 |
void push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
353 |
{ |
{ |
|
stackitem *new_item; /* The new stack item */ |
|
|
/* ...which will contain... */ |
|
354 |
value *new_value; /* A new symbol value */ |
value *new_value; /* A new symbol value */ |
355 |
/* ...which might point to... */ |
/* ...which might point to... */ |
356 |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
363 |
const char *dlerr; /* Dynamic linker error */ |
const char *dlerr; /* Dynamic linker error */ |
364 |
char *mangled; /* Mangled function name */ |
char *mangled; /* Mangled function name */ |
365 |
|
|
366 |
/* Create a new stack item containing a new value */ |
new_value= new_val(env); |
367 |
new_item= malloc(sizeof(stackitem)); |
protect(new_value); |
368 |
new_value= malloc(sizeof(value)); |
new_fvalue= new_val(env); |
369 |
new_item->item=new_value; |
protect(new_fvalue); |
370 |
|
|
371 |
/* The new value is a symbol */ |
/* The new value is a symbol */ |
372 |
new_value->type= symb; |
new_value->type= symb; |
|
new_value->refcount= 1; |
|
373 |
|
|
374 |
/* Look up the symbol name in the hash table */ |
/* Look up the symbol name in the hash table */ |
375 |
new_symbol= hash(env->symbols, in_string); |
new_symbol= hash(env->symbols, in_string); |
376 |
new_value->content.ptr= *new_symbol; |
new_value->content.sym= *new_symbol; |
377 |
|
|
378 |
if(*new_symbol==NULL) { /* If symbol was undefined */ |
if(*new_symbol==NULL) { /* If symbol was undefined */ |
379 |
|
|
380 |
/* Create a new symbol */ |
/* Create a new symbol */ |
381 |
(*new_symbol)= malloc(sizeof(symbol)); |
(*new_symbol)= malloc(sizeof(symbol)); |
382 |
|
assert((*new_symbol) != NULL); |
383 |
(*new_symbol)->val= NULL; /* undefined value */ |
(*new_symbol)->val= NULL; /* undefined value */ |
384 |
(*new_symbol)->next= NULL; |
(*new_symbol)->next= NULL; |
385 |
(*new_symbol)->id= malloc(strlen(in_string)+1); |
(*new_symbol)->id= malloc(strlen(in_string)+1); |
386 |
|
assert((*new_symbol)->id != NULL); |
387 |
strcpy((*new_symbol)->id, in_string); |
strcpy((*new_symbol)->id, in_string); |
388 |
|
|
389 |
/* Intern the new symbol in the hash table */ |
/* Intern the new symbol in the hash table */ |
390 |
new_value->content.ptr= *new_symbol; |
new_value->content.sym= *new_symbol; |
391 |
|
|
392 |
/* Try to load the symbol name as an external function, to see if |
/* Try to load the symbol name as an external function, to see if |
393 |
we should bind the symbol to a new function pointer value */ |
we should bind the symbol to a new function pointer value */ |
394 |
if(handle==NULL) /* If no handle */ |
if(handle==NULL) /* If no handle */ |
395 |
handle= dlopen(NULL, RTLD_LAZY); |
handle= dlopen(NULL, RTLD_LAZY); |
396 |
|
|
397 |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
mangled= mangle_str(in_string); /* mangle the name */ |
398 |
dlerr=dlerror(); |
funcptr= dlsym(handle, mangled); /* and try to find it */ |
399 |
|
|
400 |
|
dlerr= dlerror(); |
401 |
if(dlerr != NULL) { /* If no function was found */ |
if(dlerr != NULL) { /* If no function was found */ |
402 |
mangled=mangle_str(in_string); |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
403 |
funcptr= dlsym(handle, mangled); /* try mangling it */ |
dlerr= dlerror(); |
|
free(mangled); |
|
|
dlerr=dlerror(); |
|
404 |
} |
} |
405 |
|
|
406 |
if(dlerr==NULL) { /* If a function was found */ |
if(dlerr==NULL) { /* If a function was found */ |
407 |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
new_fvalue->type= func; /* The new value is a function pointer */ |
408 |
new_fvalue->type=func; /* The new value is a function pointer */ |
new_fvalue->content.func= funcptr; /* Store function pointer */ |
|
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
|
409 |
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
410 |
function value */ |
function value */ |
|
new_fvalue->refcount= 1; |
|
411 |
} |
} |
412 |
|
|
413 |
|
free(mangled); |
414 |
} |
} |
|
push(&(env->head), new_item); |
|
|
} |
|
415 |
|
|
416 |
/* Print newline. */ |
push_val(env, new_value); |
417 |
extern void nl() |
unprotect(new_value); unprotect(new_fvalue); |
|
{ |
|
|
printf("\n"); |
|
418 |
} |
} |
419 |
|
|
|
/* Gets the type of a value */ |
|
|
extern void type(environment *env){ |
|
|
int typenum; |
|
420 |
|
|
421 |
if((env->head)==NULL) { |
/* Print a value */ |
422 |
printerr("Too Few Arguments"); |
void print_val(environment *env, value *val, int noquote, stackitem *stack, |
423 |
env->err=1; |
FILE *stream) |
|
return; |
|
|
} |
|
|
typenum=env->head->item->type; |
|
|
toss(env); |
|
|
switch(typenum){ |
|
|
case integer: |
|
|
push_sym(env, "integer"); |
|
|
break; |
|
|
case string: |
|
|
push_sym(env, "string"); |
|
|
break; |
|
|
case symb: |
|
|
push_sym(env, "symbol"); |
|
|
break; |
|
|
case func: |
|
|
push_sym(env, "function"); |
|
|
break; |
|
|
case list: |
|
|
push_sym(env, "list"); |
|
|
break; |
|
|
default: |
|
|
push_sym(env, "unknown"); |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
/* Prints the top element of the stack. */ |
|
|
void print_h(stackitem *stack_head) |
|
424 |
{ |
{ |
425 |
switch(stack_head->item->type) { |
stackitem *titem, *tstack; |
426 |
|
int depth; |
427 |
|
|
428 |
|
switch(val->type) { |
429 |
|
case empty: |
430 |
|
if(fprintf(stream, "[]") < 0){ |
431 |
|
perror("print_val"); |
432 |
|
env->err= 5; |
433 |
|
return; |
434 |
|
} |
435 |
|
break; |
436 |
case integer: |
case integer: |
437 |
printf("%d", stack_head->item->content.val); |
if(fprintf(stream, "%d", val->content.i) < 0){ |
438 |
|
perror("print_val"); |
439 |
|
env->err= 5; |
440 |
|
return; |
441 |
|
} |
442 |
|
break; |
443 |
|
case tfloat: |
444 |
|
if(fprintf(stream, "%f", val->content.f) < 0){ |
445 |
|
perror("print_val"); |
446 |
|
env->err= 5; |
447 |
|
return; |
448 |
|
} |
449 |
break; |
break; |
450 |
case string: |
case string: |
451 |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
if(noquote){ |
452 |
|
if(fprintf(stream, "%s", val->content.string) < 0){ |
453 |
|
perror("print_val"); |
454 |
|
env->err= 5; |
455 |
|
return; |
456 |
|
} |
457 |
|
} else { /* quote */ |
458 |
|
if(fprintf(stream, "\"%s\"", val->content.string) < 0){ |
459 |
|
perror("print_val"); |
460 |
|
env->err= 5; |
461 |
|
return; |
462 |
|
} |
463 |
|
} |
464 |
break; |
break; |
465 |
case symb: |
case symb: |
466 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
if(fprintf(stream, "%s", val->content.sym->id) < 0){ |
467 |
|
perror("print_val"); |
468 |
|
env->err= 5; |
469 |
|
return; |
470 |
|
} |
471 |
break; |
break; |
472 |
case func: |
case func: |
473 |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
if(fprintf(stream, "#<function %p>", val->content.func) < 0){ |
474 |
break; |
perror("print_val"); |
475 |
case list: |
env->err= 5; |
476 |
/* A list is just a stack, so make stack_head point to it */ |
return; |
|
stack_head=(stackitem *)(stack_head->item->content.ptr); |
|
|
printf("[ "); |
|
|
while(stack_head != NULL) { |
|
|
print_h(stack_head); |
|
|
printf(" "); |
|
|
stack_head=stack_head->next; |
|
477 |
} |
} |
|
printf("]"); |
|
478 |
break; |
break; |
479 |
default: |
case port: |
480 |
printf("#<unknown %p>", (stack_head->item->content.ptr)); |
if(fprintf(stream, "#<port %p>", val->content.p) < 0){ |
481 |
|
perror("print_val"); |
482 |
|
env->err= 5; |
483 |
|
return; |
484 |
|
} |
485 |
break; |
break; |
486 |
} |
case tcons: |
487 |
} |
if(fprintf(stream, "[ ") < 0){ |
488 |
|
perror("print_val"); |
489 |
|
env->err= 5; |
490 |
|
return; |
491 |
|
} |
492 |
|
tstack= stack; |
493 |
|
|
494 |
extern void print_(environment *env) { |
do { |
495 |
if(env->head==NULL) { |
titem=malloc(sizeof(stackitem)); |
496 |
printerr("Too Few Arguments"); |
assert(titem != NULL); |
497 |
env->err=1; |
titem->item=val; |
498 |
return; |
titem->next=tstack; |
499 |
} |
tstack=titem; /* Put it on the stack */ |
500 |
print_h(env->head); |
/* Search a stack of values being printed to see if we are already |
501 |
} |
printing this value */ |
502 |
|
titem=tstack; |
503 |
|
depth=0; |
504 |
|
|
505 |
|
while(titem != NULL && titem->item != CAR(val)){ |
506 |
|
titem=titem->next; |
507 |
|
depth++; |
508 |
|
} |
509 |
|
|
510 |
/* Prints the top element of the stack and then discards it. */ |
if(titem != NULL){ /* If we found it on the stack, */ |
511 |
extern void print(environment *env) |
if(fprintf(stream, "#%d#", depth) < 0){ /* print a depth reference */ |
512 |
{ |
perror("print_val"); |
513 |
print_(env); |
env->err= 5; |
514 |
if(env->err) return; |
free(titem); |
515 |
toss(env); |
return; |
516 |
} |
} |
517 |
|
} else { |
518 |
|
print_val(env, CAR(val), noquote, tstack, stream); |
519 |
|
} |
520 |
|
|
521 |
/* Only to be called by function printstack. */ |
val= CDR(val); |
522 |
void print_st(stackitem *stack_head, long counter) |
switch(val->type){ |
523 |
{ |
case empty: |
524 |
if(stack_head->next != NULL) |
break; |
525 |
print_st(stack_head->next, counter+1); |
case tcons: |
526 |
printf("%ld: ", counter); |
/* Search a stack of values being printed to see if we are already |
527 |
print_h(stack_head); |
printing this value */ |
528 |
nl(); |
titem=tstack; |
529 |
} |
depth=0; |
530 |
|
|
531 |
|
while(titem != NULL && titem->item != val){ |
532 |
|
titem=titem->next; |
533 |
|
depth++; |
534 |
|
} |
535 |
|
if(titem != NULL){ /* If we found it on the stack, */ |
536 |
|
if(fprintf(stream, " . #%d#", depth) < 0){ /* print a depth reference */ |
537 |
|
perror("print_val"); |
538 |
|
env->err= 5; |
539 |
|
goto printval_end; |
540 |
|
} |
541 |
|
} else { |
542 |
|
if(fprintf(stream, " ") < 0){ |
543 |
|
perror("print_val"); |
544 |
|
env->err= 5; |
545 |
|
goto printval_end; |
546 |
|
} |
547 |
|
} |
548 |
|
break; |
549 |
|
default: |
550 |
|
if(fprintf(stream, " . ") < 0){ /* Improper list */ |
551 |
|
perror("print_val"); |
552 |
|
env->err= 5; |
553 |
|
goto printval_end; |
554 |
|
} |
555 |
|
print_val(env, val, noquote, tstack, stream); |
556 |
|
} |
557 |
|
} while(val->type == tcons && titem == NULL); |
558 |
|
|
559 |
/* Prints the stack. */ |
printval_end: |
560 |
extern void printstack(environment *env) |
|
561 |
{ |
titem=tstack; |
562 |
if(env->head == NULL) { |
while(titem != stack){ |
563 |
return; |
tstack=titem->next; |
564 |
|
free(titem); |
565 |
|
titem=tstack; |
566 |
|
} |
567 |
|
|
568 |
|
if(! (env->err)){ |
569 |
|
if(fprintf(stream, " ]") < 0){ |
570 |
|
perror("print_val"); |
571 |
|
env->err= 5; |
572 |
|
} |
573 |
|
} |
574 |
|
break; |
575 |
} |
} |
|
print_st(env->head, 1); |
|
|
nl(); |
|
576 |
} |
} |
577 |
|
|
578 |
|
|
579 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
580 |
extern void swap(environment *env) |
extern void swap(environment *env) |
581 |
{ |
{ |
582 |
stackitem *temp= env->head; |
value *temp= env->head; |
583 |
|
|
584 |
if(env->head==NULL || env->head->next==NULL) { |
if(env->head->type == empty || CDR(env->head)->type == empty) { |
585 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
586 |
env->err=1; |
env->err=1; |
587 |
return; |
return; |
588 |
} |
} |
589 |
|
|
590 |
env->head= env->head->next; |
env->head= CDR(env->head); |
591 |
temp->next= env->head->next; |
CDR(temp)= CDR(env->head); |
592 |
env->head->next= temp; |
CDR(env->head)= temp; |
593 |
} |
} |
594 |
|
|
595 |
|
|
596 |
/* Recall a value from a symbol, if bound */ |
/* Recall a value from a symbol, if bound */ |
597 |
extern void rcl(environment *env) |
extern void rcl(environment *env) |
598 |
{ |
{ |
599 |
value *val; |
value *val; |
600 |
|
|
601 |
if(env->head == NULL) { |
if(env->head->type==empty) { |
602 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
603 |
env->err=1; |
env->err= 1; |
604 |
return; |
return; |
605 |
} |
} |
606 |
|
|
607 |
if(env->head->item->type!=symb) { |
if(CAR(env->head)->type!=symb) { |
608 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
609 |
env->err=2; |
env->err= 2; |
610 |
return; |
return; |
611 |
} |
} |
612 |
|
|
613 |
val=((symbol *)(env->head->item->content.ptr))->val; |
val= CAR(env->head)->content.sym->val; |
614 |
if(val == NULL){ |
if(val == NULL){ |
615 |
printerr("Unbound Variable"); |
printerr("Unbound Variable"); |
616 |
env->err=3; |
env->err= 3; |
617 |
return; |
return; |
618 |
} |
} |
619 |
toss(env); /* toss the symbol */ |
push_val(env, val); /* Return the symbol's bound value */ |
620 |
|
swap(env); |
621 |
if(env->err) return; |
if(env->err) return; |
622 |
push_val(&(env->head), val); /* Return its bound value */ |
env->head= CDR(env->head); |
623 |
} |
} |
624 |
|
|
|
void stack_read(environment*, char*); |
|
625 |
|
|
626 |
/* If the top element is a symbol, determine if it's bound to a |
/* If the top element is a symbol, determine if it's bound to a |
627 |
function value, and if it is, toss the symbol and execute the |
function value, and if it is, toss the symbol and execute the |
630 |
{ |
{ |
631 |
funcp in_func; |
funcp in_func; |
632 |
value* temp_val; |
value* temp_val; |
633 |
stackitem* iterator; |
value* iterator; |
634 |
char* temp_string; |
|
635 |
|
eval_start: |
636 |
|
|
637 |
|
gc_maybe(env); |
638 |
|
|
639 |
if(env->head==NULL) { |
if(env->head->type==empty) { |
640 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
641 |
env->err=1; |
env->err= 1; |
642 |
return; |
return; |
643 |
} |
} |
644 |
|
|
645 |
switch(env->head->item->type) { |
switch(CAR(env->head)->type) { |
646 |
/* if it's a symbol */ |
/* if it's a symbol */ |
647 |
case symb: |
case symb: |
648 |
rcl(env); /* get its contents */ |
rcl(env); /* get its contents */ |
649 |
if(env->err) return; |
if(env->err) return; |
650 |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
if(CAR(env->head)->type!=symb){ /* don't recurse symbols */ |
651 |
eval(env); /* evaluate the value */ |
goto eval_start; |
|
return; |
|
652 |
} |
} |
653 |
break; |
return; |
654 |
|
|
655 |
/* If it's a lone function value, run it */ |
/* If it's a lone function value, run it */ |
656 |
case func: |
case func: |
657 |
in_func= (funcp)(env->head->item->content.ptr); |
in_func= CAR(env->head)->content.func; |
658 |
toss(env); |
env->head= CDR(env->head); |
659 |
if(env->err) return; |
return in_func(env); |
|
(*in_func)(env); |
|
|
break; |
|
660 |
|
|
661 |
/* If it's a list */ |
/* If it's a list */ |
662 |
case list: |
case tcons: |
663 |
temp_val= env->head->item; |
temp_val= CAR(env->head); |
664 |
env->head->item->refcount++; |
protect(temp_val); |
665 |
toss(env); |
|
666 |
if(env->err) return; |
env->head= CDR(env->head); |
667 |
iterator= (stackitem*)temp_val->content.ptr; |
iterator= temp_val; |
668 |
while(iterator!=NULL && iterator->item!=NULL) { |
|
669 |
push_val(&(env->head), iterator->item); |
while(iterator->type != empty) { |
670 |
if(env->head->item->type==symb |
push_val(env, CAR(iterator)); |
671 |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
|
672 |
toss(env); |
if(CAR(env->head)->type==symb |
673 |
if(env->err) return; |
&& CAR(env->head)->content.sym->id[0]==';') { |
674 |
|
env->head= CDR(env->head); |
675 |
|
|
676 |
|
if(CDR(iterator)->type == empty){ |
677 |
|
goto eval_start; |
678 |
|
} |
679 |
eval(env); |
eval(env); |
680 |
if(env->err) return; |
if(env->err) return; |
681 |
} |
} |
682 |
iterator= iterator->next; |
if (CDR(iterator)->type == empty || CDR(iterator)->type == tcons) |
683 |
|
iterator= CDR(iterator); |
684 |
|
else { |
685 |
|
printerr("Bad Argument Type"); /* Improper list */ |
686 |
|
env->err= 2; |
687 |
|
return; |
688 |
|
} |
689 |
} |
} |
690 |
free_val(temp_val); |
unprotect(temp_val); |
|
break; |
|
|
|
|
|
/* If it's a string */ |
|
|
case string: |
|
|
temp_val= env->head->item; |
|
|
env->head->item->refcount++; |
|
|
toss(env); |
|
|
if(env->err) return; |
|
|
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
|
|
strcpy(temp_string, "[ "); |
|
|
strcat(temp_string, (char*)temp_val->content.ptr); |
|
|
strcat(temp_string, " ]"); |
|
|
stack_read(env, temp_string); |
|
|
eval(env); |
|
|
if(env->err) return; |
|
|
free_val(temp_val); |
|
|
free(temp_string); |
|
|
break; |
|
|
|
|
|
default: |
|
|
} |
|
|
} |
|
|
|
|
|
/* Reverse (flip) a list */ |
|
|
extern void rev(environment *env){ |
|
|
stackitem *old_head, *new_head, *item; |
|
|
|
|
|
if((env->head)==NULL) { |
|
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
691 |
return; |
return; |
|
} |
|
692 |
|
|
693 |
if(env->head->item->type!=list) { |
case empty: |
694 |
printerr("Bad Argument Type"); |
env->head= CDR(env->head); |
695 |
env->err=2; |
case integer: |
696 |
|
case tfloat: |
697 |
|
case string: |
698 |
|
case port: |
699 |
return; |
return; |
700 |
} |
} |
|
|
|
|
old_head=(stackitem *)(env->head->item->content.ptr); |
|
|
new_head=NULL; |
|
|
while(old_head != NULL){ |
|
|
item=old_head; |
|
|
old_head=old_head->next; |
|
|
item->next=new_head; |
|
|
new_head=item; |
|
|
} |
|
|
env->head->item->content.ptr=new_head; |
|
701 |
} |
} |
702 |
|
|
|
/* Make a list. */ |
|
|
extern void pack(environment *env) |
|
|
{ |
|
|
void* delimiter; |
|
|
stackitem *iterator, *temp; |
|
|
value *pack; |
|
|
|
|
|
delimiter= env->head->item->content.ptr; /* Get delimiter */ |
|
|
toss(env); |
|
703 |
|
|
704 |
iterator= env->head; |
/* Internal forget function */ |
705 |
|
void forget_sym(symbol **hash_entry) |
706 |
|
{ |
707 |
|
symbol *temp; |
708 |
|
|
709 |
if(iterator==NULL || iterator->item->content.ptr==delimiter) { |
temp= *hash_entry; |
710 |
temp= NULL; |
*hash_entry= (*hash_entry)->next; |
711 |
toss(env); |
|
712 |
} else { |
free(temp->id); |
713 |
/* Search for first delimiter */ |
free(temp); |
714 |
while(iterator->next!=NULL |
} |
|
&& iterator->next->item->content.ptr!=delimiter) |
|
|
iterator= iterator->next; |
|
|
|
|
|
/* Extract list */ |
|
|
temp= env->head; |
|
|
env->head= iterator->next; |
|
|
iterator->next= NULL; |
|
|
|
|
|
if(env->head!=NULL) |
|
|
toss(env); |
|
|
} |
|
715 |
|
|
|
/* Push list */ |
|
|
pack= malloc(sizeof(value)); |
|
|
pack->type= list; |
|
|
pack->content.ptr= temp; |
|
|
pack->refcount= 1; |
|
716 |
|
|
717 |
temp= malloc(sizeof(stackitem)); |
int main(int argc, char **argv) |
718 |
temp->item= pack; |
{ |
719 |
|
environment myenv; |
720 |
|
int c; /* getopt option character */ |
721 |
|
|
722 |
push(&(env->head), temp); |
#ifdef __linux__ |
723 |
rev(env); |
mtrace(); |
724 |
} |
#endif |
725 |
|
|
726 |
/* Parse input. */ |
init_env(&myenv); |
|
void stack_read(environment *env, char *in_line) |
|
|
{ |
|
|
char *temp, *rest; |
|
|
int itemp; |
|
|
size_t inlength= strlen(in_line)+1; |
|
|
int convert= 0; |
|
|
|
|
|
temp= malloc(inlength); |
|
|
rest= malloc(inlength); |
|
|
|
|
|
do { |
|
|
/* If comment */ |
|
|
if((convert= sscanf(in_line, "#%[^\n\r]", rest))) { |
|
|
free(temp); free(rest); |
|
|
return; |
|
|
} |
|
727 |
|
|
728 |
/* If string */ |
myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO); |
|
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
|
|
push_cstring(&(env->head), temp); |
|
|
break; |
|
|
} |
|
|
/* If integer */ |
|
|
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
|
|
push_int(&(env->head), itemp); |
|
|
break; |
|
|
} |
|
|
/* Escape ';' with '\' */ |
|
|
if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { |
|
|
temp[1]= '\0'; |
|
|
push_sym(env, temp); |
|
|
break; |
|
|
} |
|
|
/* If symbol */ |
|
|
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
|
|
push_sym(env, temp); |
|
|
break; |
|
|
} |
|
|
/* If single char */ |
|
|
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
|
|
if(*temp==';') { |
|
|
if(!env->non_eval_flag) { |
|
|
eval(env); /* Evaluate top element */ |
|
|
break; |
|
|
} |
|
|
|
|
|
push_sym(env, ";"); |
|
|
break; |
|
|
} |
|
729 |
|
|
730 |
if(*temp==']') { |
while ((c = getopt (argc, argv, "i")) != -1) |
731 |
push_sym(env, "["); |
switch (c) |
732 |
pack(env); |
{ |
733 |
if(env->non_eval_flag) |
case 'i': |
734 |
env->non_eval_flag--; |
myenv.interactive = 1; |
735 |
break; |
break; |
736 |
|
case '?': |
737 |
|
fprintf (stderr, |
738 |
|
"Unknown option character '\\x%x'.\n", |
739 |
|
optopt); |
740 |
|
return EX_USAGE; |
741 |
|
default: |
742 |
|
abort (); |
743 |
} |
} |
744 |
|
|
745 |
|
if (optind < argc) { |
746 |
|
myenv.interactive = 0; |
747 |
|
myenv.inputstream= fopen(argv[optind], "r"); |
748 |
|
if(myenv.inputstream== NULL) { |
749 |
|
perror(argv[0]); |
750 |
|
exit (EX_NOINPUT); |
751 |
|
} |
752 |
|
} |
753 |
|
|
754 |
if(*temp=='[') { |
if(myenv.interactive) |
755 |
push_sym(env, "["); |
printf(start_message); |
756 |
env->non_eval_flag++; |
|
757 |
break; |
while(1) { |
758 |
|
if(myenv.in_string==NULL) { |
759 |
|
if (myenv.interactive) { |
760 |
|
if(myenv.err) { |
761 |
|
printf("(error %d)\n", myenv.err); |
762 |
|
myenv.err= 0; |
763 |
|
} |
764 |
|
printf("\n"); |
765 |
|
printstack(&myenv); |
766 |
|
printf("> "); |
767 |
} |
} |
768 |
|
myenv.err=0; |
769 |
|
} |
770 |
|
readstream(&myenv, myenv.inputstream); |
771 |
|
if (myenv.err) { /* EOF or other error */ |
772 |
|
myenv.err=0; |
773 |
|
quit(&myenv); |
774 |
|
} else if(myenv.head->type!=empty |
775 |
|
&& CAR(myenv.head)->type==symb |
776 |
|
&& CAR(myenv.head)->content.sym->id[0] == ';') { |
777 |
|
if(myenv.head->type != empty) |
778 |
|
myenv.head= CDR(myenv.head); |
779 |
|
eval(&myenv); |
780 |
|
} else { |
781 |
|
gc_maybe(&myenv); |
782 |
} |
} |
|
} while(0); |
|
|
|
|
|
free(temp); |
|
|
|
|
|
if(convert<2) { |
|
|
free(rest); |
|
|
return; |
|
783 |
} |
} |
784 |
|
quit(&myenv); |
785 |
stack_read(env, rest); |
return EXIT_FAILURE; |
|
|
|
|
free(rest); |
|
786 |
} |
} |
787 |
|
|
|
/* Relocate elements of the list on the stack. */ |
|
|
extern void expand(environment *env) |
|
|
{ |
|
|
stackitem *temp, *new_head; |
|
|
|
|
|
/* Is top element a list? */ |
|
|
if(env->head==NULL) { |
|
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
if(env->head->item->type!=list) { |
|
|
printerr("Bad Argument Type"); |
|
|
env->err=2; |
|
|
return; |
|
|
} |
|
788 |
|
|
789 |
rev(env); |
/* Return copy of a value */ |
790 |
|
value *copy_val(environment *env, value *old_value) |
791 |
|
{ |
792 |
|
value *new_value; |
793 |
|
|
794 |
if(env->err) |
if(old_value==NULL) |
795 |
return; |
return NULL; |
796 |
|
|
797 |
/* The first list element is the new stack head */ |
new_value= new_val(env); |
798 |
new_head= temp= env->head->item->content.ptr; |
new_value->type= old_value->type; |
799 |
|
|
800 |
env->head->item->refcount++; |
switch(old_value->type){ |
801 |
toss(env); |
case tfloat: |
802 |
|
case integer: |
803 |
|
case func: |
804 |
|
case symb: |
805 |
|
case empty: |
806 |
|
case port: |
807 |
|
new_value->content= old_value->content; |
808 |
|
break; |
809 |
|
case string: |
810 |
|
new_value->content.string= strdup(old_value->content.string); |
811 |
|
break; |
812 |
|
case tcons: |
813 |
|
|
814 |
/* Find the end of the list */ |
new_value->content.c= malloc(sizeof(pair)); |
815 |
while(temp->next!=NULL) |
assert(new_value->content.c!=NULL); |
816 |
temp= temp->next; |
env->gc_count += sizeof(pair); |
817 |
|
|
818 |
/* Connect the tail of the list with the old stack head */ |
CAR(new_value)= copy_val(env, CAR(old_value)); /* recurse */ |
819 |
temp->next= env->head; |
CDR(new_value)= copy_val(env, CDR(old_value)); /* recurse */ |
820 |
env->head= new_head; /* ...and voila! */ |
break; |
821 |
|
} |
822 |
|
|
823 |
|
return new_value; |
824 |
} |
} |
825 |
|
|
826 |
/* Compares two elements by reference. */ |
|
827 |
extern void eq(environment *env) |
/* read a line from a stream; used by readline */ |
828 |
|
void readlinestream(environment *env, FILE *stream) |
829 |
{ |
{ |
830 |
void *left, *right; |
char in_string[101]; |
|
int result; |
|
831 |
|
|
832 |
if((env->head)==NULL || env->head->next==NULL) { |
if(fgets(in_string, 100, stream)==NULL) { |
833 |
printerr("Too Few Arguments"); |
push_cstring(env, ""); |
834 |
env->err=1; |
if (! feof(stream)){ |
835 |
return; |
perror("readline"); |
836 |
|
env->err= 5; |
837 |
|
} |
838 |
|
} else { |
839 |
|
push_cstring(env, in_string); |
840 |
} |
} |
|
|
|
|
left= env->head->item->content.ptr; |
|
|
swap(env); |
|
|
right= env->head->item->content.ptr; |
|
|
result= (left==right); |
|
|
|
|
|
toss(env); toss(env); |
|
|
push_int(&(env->head), result); |
|
841 |
} |
} |
842 |
|
|
843 |
/* Negates the top element on the stack. */ |
|
844 |
extern void not(environment *env) |
/* Reverse (flip) a list */ |
845 |
|
extern void rev(environment *env) |
846 |
{ |
{ |
847 |
int val; |
value *old_head, *new_head, *item; |
848 |
|
|
849 |
if((env->head)==NULL) { |
if(env->head->type==empty) { |
850 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
851 |
env->err=1; |
env->err= 1; |
852 |
return; |
return; |
853 |
} |
} |
854 |
|
|
855 |
if(env->head->item->type!=integer) { |
if(CAR(env->head)->type==empty) |
856 |
|
return; /* Don't reverse an empty list */ |
857 |
|
|
858 |
|
if(CAR(env->head)->type!=tcons) { |
859 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
860 |
env->err=2; |
env->err= 2; |
861 |
return; |
return; |
862 |
} |
} |
863 |
|
|
864 |
val= env->head->item->content.val; |
old_head= CAR(env->head); |
865 |
toss(env); |
new_head= new_val(env); |
866 |
push_int(&(env->head), !val); |
while(old_head->type != empty) { |
867 |
|
item= old_head; |
868 |
|
old_head= CDR(old_head); |
869 |
|
CDR(item)= new_head; |
870 |
|
new_head= item; |
871 |
|
} |
872 |
|
CAR(env->head)= new_head; |
873 |
} |
} |
874 |
|
|
|
/* Compares the two top elements on the stack and return 0 if they're the |
|
|
same. */ |
|
|
extern void neq(environment *env) |
|
|
{ |
|
|
eq(env); |
|
|
not(env); |
|
|
} |
|
875 |
|
|
876 |
/* Give a symbol some content. */ |
/* Make a list. */ |
877 |
extern void def(environment *env) |
extern void pack(environment *env) |
878 |
{ |
{ |
879 |
symbol *sym; |
value *iterator, *temp, *ending; |
880 |
|
|
881 |
/* Needs two values on the stack, the top one must be a symbol */ |
ending=new_val(env); |
|
if(env->head==NULL || env->head->next==NULL) { |
|
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
|
|
|
if(env->head->item->type!=symb) { |
|
|
printerr("Bad Argument Type"); |
|
|
env->err=2; |
|
|
return; |
|
|
} |
|
882 |
|
|
883 |
/* long names are a pain */ |
iterator= env->head; |
884 |
sym=env->head->item->content.ptr; |
if(iterator->type == empty |
885 |
|
|| (CAR(iterator)->type==symb |
886 |
/* if the symbol was bound to something else, throw it away */ |
&& CAR(iterator)->content.sym->id[0]=='[')) { |
887 |
if(sym->val != NULL) |
temp= ending; |
888 |
free_val(sym->val); |
if(env->head->type != empty) |
889 |
|
env->head= CDR(env->head); |
890 |
|
} else { |
891 |
|
/* Search for first delimiter */ |
892 |
|
while(CDR(iterator)->type != empty |
893 |
|
&& (CAR(CDR(iterator))->type!=symb |
894 |
|
|| CAR(CDR(iterator))->content.sym->id[0]!='[')) |
895 |
|
iterator= CDR(iterator); |
896 |
|
|
897 |
|
/* Extract list */ |
898 |
|
temp= env->head; |
899 |
|
env->head= CDR(iterator); |
900 |
|
CDR(iterator)= ending; |
901 |
|
|
902 |
/* Bind the symbol to the value */ |
if(env->head->type != empty) |
903 |
sym->val= env->head->next->item; |
env->head= CDR(env->head); |
904 |
sym->val->refcount++; /* Increase the reference counter */ |
} |
905 |
|
|
906 |
toss(env); toss(env); |
/* Push list */ |
|
} |
|
907 |
|
|
908 |
/* Quit stack. */ |
push_val(env, temp); |
909 |
extern void quit(environment *env) |
rev(env); |
|
{ |
|
|
exit(EXIT_SUCCESS); |
|
910 |
} |
} |
911 |
|
|
|
/* Clear stack */ |
|
|
extern void clear(environment *env) |
|
|
{ |
|
|
while(env->head!=NULL) |
|
|
toss(env); |
|
|
} |
|
912 |
|
|
913 |
/* List all defined words */ |
/* read from a stream; used by "read" and "readport" */ |
914 |
extern void words(environment *env) |
void readstream(environment *env, FILE *stream) |
915 |
{ |
{ |
916 |
symbol *temp; |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
917 |
int i; |
const char strform[]= "\"%[^\"]\"%n"; |
918 |
|
const char intform[]= "%i%n"; |
919 |
for(i= 0; i<HASHTBLSIZE; i++) { |
const char fltform[]= "%f%n"; |
920 |
temp= env->symbols[i]; |
const char blankform[]= "%*[ \t]%n"; |
921 |
while(temp!=NULL) { |
const char ebrackform[]= "]%n"; |
922 |
printf("%s\n", temp->id); |
const char semicform[]= ";%n"; |
923 |
temp= temp->next; |
const char bbrackform[]= "[%n"; |
924 |
|
|
925 |
|
int itemp, readlength= -1; |
926 |
|
int count= -1; |
927 |
|
float ftemp; |
928 |
|
static int depth= 0; |
929 |
|
char *match; |
930 |
|
size_t inlength; |
931 |
|
|
932 |
|
if(env->in_string==NULL) { |
933 |
|
if(depth > 0 && env->interactive) { |
934 |
|
printf("]> "); |
935 |
} |
} |
936 |
} |
readlinestream(env, env->inputstream); |
937 |
} |
if(env->err) return; |
|
|
|
|
/* Forgets a symbol (remove it from the hash table) */ |
|
|
extern void forget(environment *env) |
|
|
{ |
|
|
char* sym_id; |
|
|
stackitem *stack_head= env->head; |
|
|
symbol **hash_entry, *temp; |
|
938 |
|
|
939 |
if(stack_head==NULL) { |
if((CAR(env->head)->content.string)[0]=='\0'){ |
940 |
printerr("Too Few Arguments"); |
env->err= 4; /* "" means EOF */ |
941 |
env->err=1; |
return; |
942 |
return; |
} |
943 |
|
|
944 |
|
env->in_string= malloc(strlen(CAR(env->head)->content.string)+1); |
945 |
|
assert(env->in_string != NULL); |
946 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
947 |
|
strcpy(env->in_string, CAR(env->head)->content.string); |
948 |
|
env->head= CDR(env->head); |
949 |
} |
} |
950 |
|
|
951 |
if(stack_head->item->type!=symb) { |
inlength= strlen(env->in_string)+1; |
952 |
printerr("Bad Argument Type"); |
match= malloc(inlength); |
953 |
env->err=2; |
assert(match != NULL); |
954 |
return; |
|
955 |
|
if(sscanf(env->in_string, blankform, &readlength) != EOF |
956 |
|
&& readlength != -1) { |
957 |
|
; |
958 |
|
} else if(sscanf(env->in_string, fltform, &ftemp, &readlength) != EOF |
959 |
|
&& readlength != -1) { |
960 |
|
if(sscanf(env->in_string, intform, &itemp, &count) != EOF |
961 |
|
&& count==readlength) { |
962 |
|
push_int(env, itemp); |
963 |
|
} else { |
964 |
|
push_float(env, ftemp); |
965 |
|
} |
966 |
|
} else if(sscanf(env->in_string, "\"\"%n", &readlength) != EOF |
967 |
|
&& readlength != -1) { |
968 |
|
push_cstring(env, ""); |
969 |
|
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
970 |
|
&& readlength != -1) { |
971 |
|
push_cstring(env, match); |
972 |
|
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
973 |
|
&& readlength != -1) { |
974 |
|
push_sym(env, match); |
975 |
|
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
976 |
|
&& readlength != -1) { |
977 |
|
pack(env); if(env->err) return; |
978 |
|
if(depth != 0) depth--; |
979 |
|
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
980 |
|
&& readlength != -1) { |
981 |
|
push_sym(env, ";"); |
982 |
|
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
983 |
|
&& readlength != -1) { |
984 |
|
push_sym(env, "["); |
985 |
|
depth++; |
986 |
|
} else { |
987 |
|
free(env->free_string); |
988 |
|
env->in_string = env->free_string = NULL; |
989 |
} |
} |
990 |
|
if (env->in_string != NULL) { |
991 |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
env->in_string += readlength; |
|
toss(env); |
|
|
|
|
|
hash_entry= hash(env->symbols, sym_id); |
|
|
temp= *hash_entry; |
|
|
*hash_entry= (*hash_entry)->next; |
|
|
|
|
|
if(temp->val!=NULL) { |
|
|
free_val(temp->val); |
|
992 |
} |
} |
|
free(temp->id); |
|
|
free(temp); |
|
|
} |
|
|
|
|
|
/* Returns the current error number to the stack */ |
|
|
extern void errn(environment *env){ |
|
|
push_int(&(env->head), env->err); |
|
|
} |
|
993 |
|
|
994 |
int main() |
free(match); |
|
{ |
|
|
environment myenv; |
|
|
char in_string[100]; |
|
995 |
|
|
996 |
init_env(&myenv); |
if(depth) |
997 |
|
return readstream(env, env->inputstream); |
|
printf("okidok\n "); |
|
|
|
|
|
while(fgets(in_string, 100, stdin) != NULL) { |
|
|
stack_read(&myenv, in_string); |
|
|
if(myenv.err) { |
|
|
printf("(error %d) ", myenv.err); |
|
|
myenv.err=0; |
|
|
} |
|
|
printf("okidok\n "); |
|
|
} |
|
|
quit(&myenv); |
|
|
return EXIT_FAILURE; |
|
|
} |
|
|
|
|
|
/* + */ |
|
|
extern void sx_2b(environment *env) { |
|
|
int a, b; |
|
|
size_t len; |
|
|
char* new_string; |
|
|
value *a_val, *b_val; |
|
|
|
|
|
if((env->head)==NULL || env->head->next==NULL) { |
|
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
|
|
|
if(env->head->item->type==string |
|
|
&& env->head->next->item->type==string) { |
|
|
a_val= env->head->item; |
|
|
b_val= env->head->next->item; |
|
|
a_val->refcount++; |
|
|
b_val->refcount++; |
|
|
toss(env); if(env->err) return; |
|
|
toss(env); if(env->err) return; |
|
|
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
|
|
new_string= malloc(len); |
|
|
strcpy(new_string, b_val->content.ptr); |
|
|
strcat(new_string, a_val->content.ptr); |
|
|
free_val(a_val); free_val(b_val); |
|
|
push_cstring(&(env->head), new_string); |
|
|
free(new_string); |
|
|
return; |
|
|
} |
|
|
|
|
|
if(env->head->item->type!=integer |
|
|
|| env->head->next->item->type!=integer) { |
|
|
printerr("Bad Argument Type"); |
|
|
env->err=2; |
|
|
return; |
|
|
} |
|
|
a=env->head->item->content.val; |
|
|
toss(env); |
|
|
if(env->err) return; |
|
|
b=env->head->item->content.val; |
|
|
toss(env); |
|
|
if(env->err) return; |
|
|
push_int(&(env->head), a+b); |
|
998 |
} |
} |