1 |
/* printf */ |
/* printf, sscanf, fgets, fprintf, fopen, perror */ |
2 |
#include <stdio.h> |
#include <stdio.h> |
3 |
/* EXIT_SUCCESS */ |
/* exit, EXIT_SUCCESS, malloc, free */ |
4 |
#include <stdlib.h> |
#include <stdlib.h> |
5 |
/* NULL */ |
/* NULL */ |
6 |
#include <stddef.h> |
#include <stddef.h> |
7 |
/* dlopen, dlsym, dlerror */ |
/* dlopen, dlsym, dlerror */ |
8 |
#include <dlfcn.h> |
#include <dlfcn.h> |
9 |
/* assert */ |
/* strcmp, strcpy, strlen, strcat, strdup */ |
10 |
#include <assert.h> |
#include <string.h> |
11 |
|
/* getopt, STDIN_FILENO, STDOUT_FILENO */ |
12 |
|
#include <unistd.h> |
13 |
|
/* EX_NOINPUT, EX_USAGE */ |
14 |
|
#include <sysexits.h> |
15 |
|
/* mtrace, muntrace */ |
16 |
|
#include <mcheck.h> |
17 |
|
|
18 |
#define HASHTBLSIZE 65536 |
#include "stack.h" |
|
|
|
|
/* First, define some types. */ |
|
|
|
|
|
/* A value of some type */ |
|
|
typedef struct { |
|
|
enum { |
|
|
integer, |
|
|
string, |
|
|
ref, /* Reference (to an element in the |
|
|
hash table) */ |
|
|
func, /* Function pointer */ |
|
|
symb, |
|
|
list |
|
|
} type; /* Type of stack element */ |
|
|
|
|
|
union { |
|
|
void *ptr; /* Pointer to the content */ |
|
|
int val; /* ...or an integer */ |
|
|
} content; /* Stores a pointer or an integer */ |
|
|
|
|
|
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 */ |
|
|
} environment; |
|
|
|
|
|
/* A type for pointers to external functions */ |
|
|
typedef void (*funcp)(environment *); /* funcp is a pointer to a void |
|
|
function (environment *) */ |
|
19 |
|
|
20 |
/* Initialize a newly created environment */ |
/* Initialize a newly created environment */ |
21 |
void init_env(environment *env) |
void init_env(environment *env) |
22 |
{ |
{ |
23 |
long i; |
int i; |
24 |
|
|
25 |
|
env->gc_limit= 20; |
26 |
|
env->gc_count= 0; |
27 |
|
env->gc_ref= NULL; |
28 |
|
env->gc_protect= NULL; |
29 |
|
|
30 |
|
env->head= NULL; |
31 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
32 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
33 |
|
env->err= 0; |
34 |
|
env->in_string= NULL; |
35 |
|
env->free_string= NULL; |
36 |
|
env->inputstream= stdin; |
37 |
|
env->interactive= 1; |
38 |
|
} |
39 |
|
|
40 |
|
void printerr(const char* in_string) { |
41 |
|
fprintf(stderr, "Err: %s\n", in_string); |
42 |
|
} |
43 |
|
|
44 |
|
/* Discard the top element of the stack. */ |
45 |
|
extern void toss(environment *env) |
46 |
|
{ |
47 |
|
stackitem *temp= env->head; |
48 |
|
|
49 |
|
if((env->head)==NULL) { |
50 |
|
printerr("Too Few Arguments"); |
51 |
|
env->err= 1; |
52 |
|
return; |
53 |
|
} |
54 |
|
|
55 |
|
env->head= env->head->next; /* Remove the top stack item */ |
56 |
|
free(temp); /* Free the old top stack item */ |
57 |
|
|
58 |
|
gc_init(env); |
59 |
} |
} |
60 |
|
|
61 |
/* 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. */ |
62 |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
63 |
{ |
{ |
64 |
long i= 0; |
int i= 0; |
65 |
unsigned long out_hash= 0; |
unsigned int out_hash= 0; |
66 |
char key= '\0'; |
char key= '\0'; |
67 |
symbol **position; |
symbol **position; |
68 |
|
|
87 |
} |
} |
88 |
} |
} |
89 |
|
|
90 |
/* Generic push function. */ |
value* new_val(environment *env) { |
91 |
int push(stackitem** stack_head, stackitem* in_item) |
value *nval= malloc(sizeof(value)); |
92 |
|
stackitem *nitem= malloc(sizeof(stackitem)); |
93 |
|
|
94 |
|
nval->content.ptr= NULL; |
95 |
|
protect(env, nval); |
96 |
|
|
97 |
|
gc_init(env); |
98 |
|
|
99 |
|
nitem->item= nval; |
100 |
|
nitem->next= env->gc_ref; |
101 |
|
env->gc_ref= nitem; |
102 |
|
|
103 |
|
env->gc_count++; |
104 |
|
unprotect(env); |
105 |
|
|
106 |
|
return nval; |
107 |
|
} |
108 |
|
|
109 |
|
void gc_mark(value *val) { |
110 |
|
stackitem *iterator; |
111 |
|
|
112 |
|
if(val==NULL || val->gc_garb==0) |
113 |
|
return; |
114 |
|
|
115 |
|
val->gc_garb= 0; |
116 |
|
|
117 |
|
if(val->type==list) { |
118 |
|
iterator= val->content.ptr; |
119 |
|
|
120 |
|
while(iterator!=NULL) { |
121 |
|
gc_mark(iterator->item); |
122 |
|
iterator= iterator->next; |
123 |
|
} |
124 |
|
} |
125 |
|
} |
126 |
|
|
127 |
|
extern void gc_init(environment *env) { |
128 |
|
stackitem *new_head= NULL, *titem, *iterator= env->gc_ref; |
129 |
|
symbol *tsymb; |
130 |
|
int i; |
131 |
|
|
132 |
|
if(env->gc_count < env->gc_limit) |
133 |
|
return; |
134 |
|
|
135 |
|
while(iterator!=NULL) { |
136 |
|
iterator->item->gc_garb= 1; |
137 |
|
iterator= iterator->next; |
138 |
|
} |
139 |
|
|
140 |
|
/* Mark */ |
141 |
|
iterator= env->gc_protect; |
142 |
|
while(iterator!=NULL) { |
143 |
|
gc_mark(iterator->item); |
144 |
|
iterator= iterator->next; |
145 |
|
} |
146 |
|
|
147 |
|
iterator= env->head; |
148 |
|
while(iterator!=NULL) { |
149 |
|
gc_mark(iterator->item); |
150 |
|
iterator= iterator->next; |
151 |
|
} |
152 |
|
|
153 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
154 |
|
tsymb= env->symbols[i]; |
155 |
|
while(tsymb!=NULL) { |
156 |
|
gc_mark(tsymb->val); |
157 |
|
tsymb= tsymb->next; |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
env->gc_count= 0; |
162 |
|
|
163 |
|
/* Sweep */ |
164 |
|
while(env->gc_ref!=NULL) { |
165 |
|
|
166 |
|
if(env->gc_ref->item->gc_garb) { |
167 |
|
switch(env->gc_ref->item->type) { |
168 |
|
case string: |
169 |
|
free(env->gc_ref->item->content.ptr); |
170 |
|
break; |
171 |
|
case integer: |
172 |
|
break; |
173 |
|
case list: |
174 |
|
while(env->gc_ref->item->content.ptr!=NULL) { |
175 |
|
titem= env->gc_ref->item->content.ptr; |
176 |
|
env->gc_ref->item->content.ptr= titem->next; |
177 |
|
free(titem); |
178 |
|
} |
179 |
|
break; |
180 |
|
default: |
181 |
|
break; |
182 |
|
} |
183 |
|
free(env->gc_ref->item); |
184 |
|
titem= env->gc_ref->next; |
185 |
|
free(env->gc_ref); |
186 |
|
env->gc_ref= titem; |
187 |
|
} else { |
188 |
|
titem= env->gc_ref->next; |
189 |
|
env->gc_ref->next= new_head; |
190 |
|
new_head= env->gc_ref; |
191 |
|
env->gc_ref= titem; |
192 |
|
env->gc_count++; |
193 |
|
} |
194 |
|
} |
195 |
|
|
196 |
|
env->gc_limit= env->gc_count*2; |
197 |
|
env->gc_ref= new_head; |
198 |
|
} |
199 |
|
|
200 |
|
void protect(environment *env, value *val) |
201 |
{ |
{ |
202 |
in_item->next= *stack_head; |
stackitem *new_item= malloc(sizeof(stackitem)); |
203 |
*stack_head= in_item; |
new_item->item= val; |
204 |
return 1; |
new_item->next= env->gc_protect; |
205 |
|
env->gc_protect= new_item; |
206 |
} |
} |
207 |
|
|
208 |
/* Push an integer onto the stack. */ |
void unprotect(environment *env) |
209 |
int push_val(stackitem **stack_head, int in_val) |
{ |
210 |
|
stackitem *temp= env->gc_protect; |
211 |
|
env->gc_protect= env->gc_protect->next; |
212 |
|
free(temp); |
213 |
|
} |
214 |
|
|
215 |
|
/* Push a value onto the stack */ |
216 |
|
void push_val(environment *env, value *val) |
217 |
{ |
{ |
|
value *new_value= malloc(sizeof(value)); |
|
218 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
219 |
new_item->item= new_value; |
new_item->item= val; |
220 |
|
new_item->next= env->head; |
221 |
|
env->head= new_item; |
222 |
|
} |
223 |
|
|
224 |
|
/* Push an integer onto the stack. */ |
225 |
|
void push_int(environment *env, int in_val) |
226 |
|
{ |
227 |
|
value *new_value= new_val(env); |
228 |
|
|
229 |
new_value->content.val= in_val; |
new_value->content.val= in_val; |
230 |
new_value->type= integer; |
new_value->type= integer; |
|
new_value->refcount=1; |
|
231 |
|
|
232 |
push(stack_head, new_item); |
push_val(env, new_value); |
|
return 1; |
|
233 |
} |
} |
234 |
|
|
235 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
236 |
int push_cstring(stackitem **stack_head, const char *in_string) |
void push_cstring(environment *env, const char *in_string) |
237 |
{ |
{ |
238 |
value *new_value= malloc(sizeof(value)); |
value *new_value= new_val(env); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item=new_value; |
|
239 |
|
|
240 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
241 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
242 |
new_value->type= string; |
new_value->type= string; |
|
new_value->refcount=1; |
|
243 |
|
|
244 |
push(stack_head, new_item); |
push_val(env, new_value); |
245 |
return 1; |
} |
246 |
|
|
247 |
|
/* Mangle a symbol name to a valid C identifier name */ |
248 |
|
char *mangle_str(const char *old_string){ |
249 |
|
char validchars[]= "0123456789abcdef"; |
250 |
|
char *new_string, *current; |
251 |
|
|
252 |
|
new_string= malloc((strlen(old_string)*2)+4); |
253 |
|
strcpy(new_string, "sx_"); /* Stack eXternal */ |
254 |
|
current= new_string+3; |
255 |
|
while(old_string[0] != '\0'){ |
256 |
|
current[0]= validchars[(unsigned char)(old_string[0])/16]; |
257 |
|
current[1]= validchars[(unsigned char)(old_string[0])%16]; |
258 |
|
current+= 2; |
259 |
|
old_string++; |
260 |
|
} |
261 |
|
current[0]= '\0'; |
262 |
|
|
263 |
|
return new_string; /* The caller must free() it */ |
264 |
|
} |
265 |
|
|
266 |
|
extern void mangle(environment *env){ |
267 |
|
char *new_string; |
268 |
|
|
269 |
|
if((env->head)==NULL) { |
270 |
|
printerr("Too Few Arguments"); |
271 |
|
env->err= 1; |
272 |
|
return; |
273 |
|
} |
274 |
|
|
275 |
|
if(env->head->item->type!=string) { |
276 |
|
printerr("Bad Argument Type"); |
277 |
|
env->err= 2; |
278 |
|
return; |
279 |
|
} |
280 |
|
|
281 |
|
new_string= mangle_str((const char *)(env->head->item->content.ptr)); |
282 |
|
|
283 |
|
toss(env); |
284 |
|
if(env->err) return; |
285 |
|
|
286 |
|
push_cstring(env, new_string); |
287 |
} |
} |
288 |
|
|
289 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
290 |
int push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
291 |
{ |
{ |
|
stackitem *new_item; /* The new stack item */ |
|
|
/* ...which will contain... */ |
|
292 |
value *new_value; /* A new symbol value */ |
value *new_value; /* A new symbol value */ |
293 |
/* ...which might point to... */ |
/* ...which might point to... */ |
294 |
symbol *new_symbol; /* (if needed) A new actual symbol */ |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
295 |
/* ...which, if possible, will be bound to... */ |
/* ...which, if possible, will be bound to... */ |
296 |
value *new_fvalue; /* (if needed) A new function value */ |
value *new_fvalue; /* (if needed) A new function value */ |
297 |
/* ...which will point to... */ |
/* ...which will point to... */ |
298 |
void *funcptr; /* A function pointer */ |
void *funcptr; /* A function pointer */ |
299 |
|
|
300 |
static void *handle= NULL; /* Dynamic linker handle */ |
static void *handle= NULL; /* Dynamic linker handle */ |
301 |
|
const char *dlerr; /* Dynamic linker error */ |
302 |
|
char *mangled; /* Mangled function name */ |
303 |
|
|
304 |
/* Create a new stack item containing a new value */ |
new_value= new_val(env); |
|
new_item= malloc(sizeof(stackitem)); |
|
|
new_value= malloc(sizeof(value)); |
|
|
new_item->item=new_value; |
|
305 |
|
|
306 |
/* The new value is a symbol */ |
/* The new value is a symbol */ |
307 |
new_value->type= symb; |
new_value->type= symb; |
|
new_value->refcount= 1; |
|
308 |
|
|
309 |
/* Look up the symbol name in the hash table */ |
/* Look up the symbol name in the hash table */ |
310 |
new_value->content.ptr= *hash(env->symbols, in_string); |
new_symbol= hash(env->symbols, in_string); |
311 |
|
new_value->content.ptr= *new_symbol; |
312 |
|
|
313 |
if(new_value->content.ptr==NULL) { /* If symbol was undefined */ |
if(*new_symbol==NULL) { /* If symbol was undefined */ |
314 |
|
|
315 |
/* Create a new symbol */ |
/* Create a new symbol */ |
316 |
new_symbol= malloc(sizeof(symbol)); |
(*new_symbol)= malloc(sizeof(symbol)); |
317 |
new_symbol->val= NULL; /* undefined value */ |
(*new_symbol)->val= NULL; /* undefined value */ |
318 |
new_symbol->next= NULL; |
(*new_symbol)->next= NULL; |
319 |
new_symbol->id= malloc(strlen(in_string)+1); |
(*new_symbol)->id= malloc(strlen(in_string)+1); |
320 |
strcpy(new_symbol->id, in_string); |
strcpy((*new_symbol)->id, in_string); |
321 |
|
|
322 |
/* Intern the new symbol in the hash table */ |
/* Intern the new symbol in the hash table */ |
323 |
new_value->content.ptr= new_symbol; |
new_value->content.ptr= *new_symbol; |
324 |
|
|
325 |
/* 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 |
326 |
we should bind the symbol to a new function pointer value */ |
we should bind the symbol to a new function pointer value */ |
327 |
if(handle==NULL) /* If no handle */ |
if(handle==NULL) /* If no handle */ |
328 |
handle= dlopen(NULL, RTLD_LAZY); |
handle= dlopen(NULL, RTLD_LAZY); |
329 |
|
|
330 |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
mangled= mangle_str(in_string); /* mangle the name */ |
331 |
if(dlerror()==NULL) { /* If a function was found */ |
funcptr= dlsym(handle, mangled); /* and try to find it */ |
332 |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
free(mangled); |
333 |
new_fvalue->type=func; /* The new value is a function pointer */ |
dlerr= dlerror(); |
334 |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
if(dlerr != NULL) { /* If no function was found */ |
335 |
new_symbol->val= new_fvalue; /* Bind the symbol to the new |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
336 |
function value */ |
dlerr= dlerror(); |
|
new_fvalue->refcount= 1; |
|
337 |
} |
} |
338 |
} |
if(dlerr==NULL) { /* If a function was found */ |
339 |
push(&(env->head), new_item); |
new_fvalue= new_val(env); /* Create a new value */ |
340 |
return 1; |
new_fvalue->type= func; /* The new value is a function pointer */ |
341 |
} |
new_fvalue->content.ptr= funcptr; /* Store function pointer */ |
342 |
|
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
343 |
void printerr(const char* in_string) { |
function value */ |
|
fprintf(stderr, "Err: %s\n", in_string); |
|
|
} |
|
|
|
|
|
/* Throw away a value */ |
|
|
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); |
|
|
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; |
|
344 |
} |
} |
345 |
} |
} |
346 |
} |
push_val(env, new_value); |
|
|
|
|
/* Discard the top element of the stack. */ |
|
|
extern void toss(environment *env) |
|
|
{ |
|
|
stackitem *temp= env->head; |
|
|
|
|
|
if((env->head)==NULL) { |
|
|
printerr("Stack empty"); |
|
|
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 */ |
|
347 |
} |
} |
348 |
|
|
349 |
/* Print newline. */ |
/* Print newline. */ |
352 |
printf("\n"); |
printf("\n"); |
353 |
} |
} |
354 |
|
|
355 |
/* Prints the top element of the stack. */ |
/* Gets the type of a value */ |
356 |
void print_h(stackitem *stack_head) |
extern void type(environment *env){ |
357 |
{ |
int typenum; |
358 |
|
|
359 |
if(stack_head==NULL) { |
if((env->head)==NULL) { |
360 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
361 |
|
env->err=1; |
362 |
return; |
return; |
363 |
} |
} |
364 |
|
typenum=env->head->item->type; |
365 |
|
toss(env); |
366 |
|
switch(typenum){ |
367 |
|
case integer: |
368 |
|
push_sym(env, "integer"); |
369 |
|
break; |
370 |
|
case string: |
371 |
|
push_sym(env, "string"); |
372 |
|
break; |
373 |
|
case symb: |
374 |
|
push_sym(env, "symbol"); |
375 |
|
break; |
376 |
|
case func: |
377 |
|
push_sym(env, "function"); |
378 |
|
break; |
379 |
|
case list: |
380 |
|
push_sym(env, "list"); |
381 |
|
break; |
382 |
|
} |
383 |
|
} |
384 |
|
|
385 |
|
/* Prints the top element of the stack. */ |
386 |
|
void print_h(stackitem *stack_head, int noquote) |
387 |
|
{ |
388 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
389 |
case integer: |
case integer: |
390 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
391 |
break; |
break; |
392 |
case string: |
case string: |
393 |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
if(noquote) |
394 |
|
printf("%s", (char*)stack_head->item->content.ptr); |
395 |
|
else |
396 |
|
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
397 |
break; |
break; |
398 |
case symb: |
case symb: |
399 |
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
400 |
break; |
break; |
401 |
default: |
case func: |
402 |
printf("%p", (funcp)(stack_head->item->content.ptr)); |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
403 |
|
break; |
404 |
|
case list: |
405 |
|
/* A list is just a stack, so make stack_head point to it */ |
406 |
|
stack_head=(stackitem *)(stack_head->item->content.ptr); |
407 |
|
printf("[ "); |
408 |
|
while(stack_head != NULL) { |
409 |
|
print_h(stack_head, noquote); |
410 |
|
printf(" "); |
411 |
|
stack_head=stack_head->next; |
412 |
|
} |
413 |
|
printf("]"); |
414 |
break; |
break; |
415 |
} |
} |
416 |
} |
} |
417 |
|
|
418 |
extern void print_(environment *env) { |
extern void print_(environment *env) { |
419 |
print_h(env->head); |
if(env->head==NULL) { |
420 |
|
printerr("Too Few Arguments"); |
421 |
|
env->err=1; |
422 |
|
return; |
423 |
|
} |
424 |
|
print_h(env->head, 0); |
425 |
|
nl(); |
426 |
} |
} |
427 |
|
|
428 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
429 |
extern void print(environment *env) |
extern void print(environment *env) |
430 |
{ |
{ |
431 |
print_(env); |
print_(env); |
432 |
|
if(env->err) return; |
433 |
|
toss(env); |
434 |
|
} |
435 |
|
|
436 |
|
extern void princ_(environment *env) { |
437 |
|
if(env->head==NULL) { |
438 |
|
printerr("Too Few Arguments"); |
439 |
|
env->err=1; |
440 |
|
return; |
441 |
|
} |
442 |
|
print_h(env->head, 1); |
443 |
|
} |
444 |
|
|
445 |
|
/* Prints the top element of the stack and then discards it. */ |
446 |
|
extern void princ(environment *env) |
447 |
|
{ |
448 |
|
princ_(env); |
449 |
|
if(env->err) return; |
450 |
toss(env); |
toss(env); |
451 |
} |
} |
452 |
|
|
456 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
457 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
458 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
459 |
print_h(stack_head); |
print_h(stack_head, 0); |
460 |
nl(); |
nl(); |
461 |
} |
} |
462 |
|
|
|
|
|
|
|
|
463 |
/* Prints the stack. */ |
/* Prints the stack. */ |
464 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
465 |
{ |
{ |
466 |
if(env->head != NULL) { |
if(env->head == NULL) { |
467 |
print_st(env->head, 1); |
printf("Stack Empty\n"); |
468 |
nl(); |
return; |
|
} else { |
|
|
printerr("Stack empty"); |
|
469 |
} |
} |
470 |
|
print_st(env->head, 1); |
471 |
} |
} |
472 |
|
|
473 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
475 |
{ |
{ |
476 |
stackitem *temp= env->head; |
stackitem *temp= env->head; |
477 |
|
|
478 |
if((env->head)==NULL) { |
if(env->head==NULL || env->head->next==NULL) { |
479 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
480 |
return; |
env->err=1; |
|
} |
|
|
|
|
|
if(env->head->next==NULL) { |
|
|
printerr("Not enough arguments"); |
|
481 |
return; |
return; |
482 |
} |
} |
483 |
|
|
486 |
env->head->next= temp; |
env->head->next= temp; |
487 |
} |
} |
488 |
|
|
489 |
stackitem* copy(stackitem* in_item) |
/* Rotate the first three elements on the stack. */ |
490 |
|
extern void rot(environment *env) |
491 |
{ |
{ |
492 |
stackitem *out_item= malloc(sizeof(stackitem)); |
stackitem *temp= env->head; |
493 |
|
|
494 |
memcpy(out_item, in_item, sizeof(stackitem)); |
if(env->head==NULL || env->head->next==NULL |
495 |
out_item->next= NULL; |
|| env->head->next->next==NULL) { |
496 |
|
printerr("Too Few Arguments"); |
497 |
|
env->err=1; |
498 |
|
return; |
499 |
|
} |
500 |
|
|
501 |
return out_item; |
env->head= env->head->next->next; |
502 |
|
temp->next->next= env->head->next; |
503 |
|
env->head->next= temp; |
504 |
} |
} |
505 |
|
|
506 |
|
/* Recall a value from a symbol, if bound */ |
507 |
|
extern void rcl(environment *env) |
508 |
|
{ |
509 |
|
value *val; |
510 |
|
|
511 |
|
if(env->head == NULL) { |
512 |
|
printerr("Too Few Arguments"); |
513 |
|
env->err=1; |
514 |
|
return; |
515 |
|
} |
516 |
|
|
517 |
|
if(env->head->item->type!=symb) { |
518 |
|
printerr("Bad Argument Type"); |
519 |
|
env->err=2; |
520 |
|
return; |
521 |
|
} |
522 |
|
|
523 |
/* If the top element is a reference, determine if it's a reference to a |
val=((symbol *)(env->head->item->content.ptr))->val; |
524 |
function, and if it is, toss the reference and execute the function. */ |
if(val == NULL){ |
525 |
|
printerr("Unbound Variable"); |
526 |
|
env->err=3; |
527 |
|
return; |
528 |
|
} |
529 |
|
protect(env, val); |
530 |
|
toss(env); /* toss the symbol */ |
531 |
|
if(env->err) return; |
532 |
|
push_val(env, val); /* Return its bound value */ |
533 |
|
unprotect(env); |
534 |
|
} |
535 |
|
|
536 |
|
/* If the top element is a symbol, determine if it's bound to a |
537 |
|
function value, and if it is, toss the symbol and execute the |
538 |
|
function. */ |
539 |
extern void eval(environment *env) |
extern void eval(environment *env) |
540 |
{ |
{ |
541 |
funcp in_func; |
funcp in_func; |
542 |
stackitem* temp= env->head; |
value* temp_val; |
543 |
|
stackitem* iterator; |
544 |
|
|
545 |
|
eval_start: |
546 |
|
|
547 |
if(temp==NULL) { |
if(env->head==NULL) { |
548 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
549 |
|
env->err=1; |
550 |
return; |
return; |
551 |
} |
} |
552 |
|
|
553 |
if(temp->item->type==symb |
switch(env->head->item->type) { |
554 |
&& ((symbol *)(temp->item->content.ptr))->val != NULL |
/* if it's a symbol */ |
555 |
&& ((symbol *)(temp->item->content.ptr))->val->type == func) { |
case symb: |
556 |
in_func= (funcp)(((symbol *)(temp->item->content.ptr))->val->content.ptr); |
rcl(env); /* get its contents */ |
557 |
|
if(env->err) return; |
558 |
|
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
559 |
|
goto eval_start; |
560 |
|
} |
561 |
|
return; |
562 |
|
|
563 |
|
/* If it's a lone function value, run it */ |
564 |
|
case func: |
565 |
|
in_func= (funcp)(env->head->item->content.ptr); |
566 |
|
toss(env); |
567 |
|
if(env->err) return; |
568 |
|
return in_func(env); |
569 |
|
|
570 |
|
/* If it's a list */ |
571 |
|
case list: |
572 |
|
temp_val= env->head->item; |
573 |
|
protect(env, temp_val); |
574 |
toss(env); |
toss(env); |
575 |
(*in_func)(env); |
if(env->err) return; |
576 |
|
iterator= (stackitem*)temp_val->content.ptr; |
577 |
|
unprotect(env); |
578 |
|
|
579 |
|
while(iterator!=NULL) { |
580 |
|
push_val(env, iterator->item); |
581 |
|
|
582 |
|
if(env->head->item->type==symb |
583 |
|
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
584 |
|
toss(env); |
585 |
|
if(env->err) return; |
586 |
|
|
587 |
|
if(iterator->next == NULL){ |
588 |
|
goto eval_start; |
589 |
|
} |
590 |
|
eval(env); |
591 |
|
if(env->err) return; |
592 |
|
} |
593 |
|
iterator= iterator->next; |
594 |
|
} |
595 |
|
return; |
596 |
|
|
597 |
|
default: |
598 |
return; |
return; |
599 |
} |
} |
600 |
|
} |
601 |
|
|
602 |
if(temp->item->type==func) { |
/* Reverse (flip) a list */ |
603 |
in_func= (funcp)(temp->item->content.ptr); |
extern void rev(environment *env){ |
604 |
toss(env); |
stackitem *old_head, *new_head, *item; |
605 |
(*in_func)(env); |
|
606 |
|
if((env->head)==NULL) { |
607 |
|
printerr("Too Few Arguments"); |
608 |
|
env->err= 1; |
609 |
return; |
return; |
610 |
} |
} |
611 |
|
|
612 |
push(&(env->head), copy(temp)); |
if(env->head->item->type!=list) { |
613 |
swap(env); |
printerr("Bad Argument Type"); |
614 |
toss(env); |
env->err= 2; |
615 |
|
return; |
616 |
|
} |
617 |
|
|
618 |
|
old_head= (stackitem *)(env->head->item->content.ptr); |
619 |
|
new_head= NULL; |
620 |
|
while(old_head != NULL){ |
621 |
|
item= old_head; |
622 |
|
old_head= old_head->next; |
623 |
|
item->next= new_head; |
624 |
|
new_head= item; |
625 |
|
} |
626 |
|
env->head->item->content.ptr= new_head; |
627 |
} |
} |
628 |
|
|
629 |
/* Make a list. */ |
/* Make a list. */ |
630 |
extern void pack(environment *env) |
extern void pack(environment *env) |
631 |
{ |
{ |
|
void* delimiter; |
|
632 |
stackitem *iterator, *temp; |
stackitem *iterator, *temp; |
633 |
value *pack; |
value *pack; |
634 |
|
|
|
delimiter= env->head->item->content.ptr; /* Get delimiter */ |
|
|
toss(env); |
|
|
|
|
635 |
iterator= env->head; |
iterator= env->head; |
636 |
|
|
637 |
if(iterator==NULL || iterator->item->content.ptr==delimiter) { |
if(iterator==NULL |
638 |
|
|| (iterator->item->type==symb |
639 |
|
&& ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) { |
640 |
temp= NULL; |
temp= NULL; |
641 |
toss(env); |
toss(env); |
642 |
} else { |
} else { |
643 |
/* Search for first delimiter */ |
/* Search for first delimiter */ |
644 |
while(iterator->next!=NULL |
while(iterator->next!=NULL |
645 |
&& iterator->next->item->content.ptr!=delimiter) |
&& (iterator->next->item->type!=symb |
646 |
|
|| ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) |
647 |
iterator= iterator->next; |
iterator= iterator->next; |
648 |
|
|
649 |
/* Extract list */ |
/* Extract list */ |
656 |
} |
} |
657 |
|
|
658 |
/* Push list */ |
/* Push list */ |
659 |
pack= malloc(sizeof(value)); |
pack= new_val(env); |
660 |
pack->type= list; |
pack->type= list; |
661 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
|
pack->refcount= 1; |
|
662 |
|
|
663 |
temp= malloc(sizeof(stackitem)); |
push_val(env, pack); |
664 |
temp->item= pack; |
rev(env); |
|
|
|
|
push(&(env->head), temp); |
|
|
} |
|
|
|
|
|
/* Parse input. */ |
|
|
int stack_read(environment *env, char *in_line) |
|
|
{ |
|
|
char *temp, *rest; |
|
|
int itemp; |
|
|
size_t inlength= strlen(in_line)+1; |
|
|
int convert= 0; |
|
|
static int non_eval_flag= 0; |
|
|
|
|
|
temp= malloc(inlength); |
|
|
rest= malloc(inlength); |
|
|
|
|
|
do { |
|
|
/* If string */ |
|
|
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_val(&(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(!non_eval_flag) { |
|
|
eval(env); /* Evaluate top element */ |
|
|
break; |
|
|
} |
|
|
|
|
|
push_sym(env, ";"); |
|
|
break; |
|
|
} |
|
|
|
|
|
if(*temp==']') { |
|
|
push_sym(env, "["); |
|
|
pack(env); |
|
|
if(non_eval_flag!=0) |
|
|
non_eval_flag--; |
|
|
break; |
|
|
} |
|
|
|
|
|
if(*temp=='[') { |
|
|
push_sym(env, "["); |
|
|
non_eval_flag++; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} while(0); |
|
|
|
|
|
|
|
|
free(temp); |
|
|
|
|
|
if(convert<2) { |
|
|
free(rest); |
|
|
return 0; |
|
|
} |
|
|
|
|
|
stack_read(env, rest); |
|
|
|
|
|
free(rest); |
|
|
return 1; |
|
665 |
} |
} |
666 |
|
|
667 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
670 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
671 |
|
|
672 |
/* Is top element a list? */ |
/* Is top element a list? */ |
673 |
if(env->head==NULL || env->head->item->type!=list) { |
if(env->head==NULL) { |
674 |
printerr("Stack empty or not a list"); |
printerr("Too Few Arguments"); |
675 |
|
env->err= 1; |
676 |
|
return; |
677 |
|
} |
678 |
|
if(env->head->item->type!=list) { |
679 |
|
printerr("Bad Argument Type"); |
680 |
|
env->err= 2; |
681 |
return; |
return; |
682 |
} |
} |
683 |
|
|
684 |
|
rev(env); |
685 |
|
|
686 |
|
if(env->err) |
687 |
|
return; |
688 |
|
|
689 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
690 |
new_head= temp= env->head->item->content.ptr; |
new_head= temp= env->head->item->content.ptr; |
691 |
|
|
|
env->head->item->refcount++; |
|
692 |
toss(env); |
toss(env); |
693 |
|
|
694 |
/* Find the end of the list */ |
/* Find the end of the list */ |
708 |
int result; |
int result; |
709 |
|
|
710 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
711 |
printerr("Not enough elements to compare"); |
printerr("Too Few Arguments"); |
712 |
|
env->err= 1; |
713 |
return; |
return; |
714 |
} |
} |
715 |
|
|
719 |
result= (left==right); |
result= (left==right); |
720 |
|
|
721 |
toss(env); toss(env); |
toss(env); toss(env); |
722 |
push_val(&(env->head), result); |
push_int(env, result); |
723 |
} |
} |
724 |
|
|
725 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
727 |
{ |
{ |
728 |
int val; |
int val; |
729 |
|
|
730 |
if((env->head)==NULL || env->head->item->type!=integer) { |
if((env->head)==NULL) { |
731 |
printerr("Stack empty or element is not a integer"); |
printerr("Too Few Arguments"); |
732 |
|
env->err= 1; |
733 |
|
return; |
734 |
|
} |
735 |
|
|
736 |
|
if(env->head->item->type!=integer) { |
737 |
|
printerr("Bad Argument Type"); |
738 |
|
env->err= 2; |
739 |
return; |
return; |
740 |
} |
} |
741 |
|
|
742 |
val= env->head->item->content.val; |
val= env->head->item->content.val; |
743 |
toss(env); |
toss(env); |
744 |
push_val(&(env->head), !val); |
push_int(env, !val); |
745 |
} |
} |
746 |
|
|
747 |
/* Compares the two top elements on the stack and return 0 if they're the |
/* Compares the two top elements on the stack and return 0 if they're the |
758 |
symbol *sym; |
symbol *sym; |
759 |
|
|
760 |
/* Needs two values on the stack, the top one must be a symbol */ |
/* Needs two values on the stack, the top one must be a symbol */ |
761 |
if(env->head==NULL || env->head->next==NULL |
if(env->head==NULL || env->head->next==NULL) { |
762 |
|| env->head->item->type!=symb) { |
printerr("Too Few Arguments"); |
763 |
printerr("Define what?"); |
env->err= 1; |
764 |
return; |
return; |
765 |
} |
} |
766 |
|
|
767 |
/* long names are a pain */ |
if(env->head->item->type!=symb) { |
768 |
sym=env->head->item->content.ptr; |
printerr("Bad Argument Type"); |
769 |
|
env->err= 2; |
770 |
|
return; |
771 |
|
} |
772 |
|
|
773 |
/* if the symbol was bound to something else, throw it away */ |
/* long names are a pain */ |
774 |
if(sym->val != NULL) |
sym= env->head->item->content.ptr; |
|
free_val(sym->val); |
|
775 |
|
|
776 |
/* Bind the symbol to the value */ |
/* Bind the symbol to the value */ |
777 |
sym->val= env->head->next->item; |
sym->val= env->head->next->item; |
|
sym->val->refcount++; /* Increase the reference counter */ |
|
778 |
|
|
779 |
toss(env); toss(env); |
toss(env); toss(env); |
780 |
} |
} |
782 |
/* Quit stack. */ |
/* Quit stack. */ |
783 |
extern void quit(environment *env) |
extern void quit(environment *env) |
784 |
{ |
{ |
785 |
|
long i; |
786 |
|
|
787 |
|
clear(env); |
788 |
|
|
789 |
|
if (env->err) return; |
790 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
791 |
|
while(env->symbols[i]!= NULL) { |
792 |
|
forget_sym(&(env->symbols[i])); |
793 |
|
} |
794 |
|
env->symbols[i]= NULL; |
795 |
|
} |
796 |
|
|
797 |
|
env->gc_limit= 0; |
798 |
|
gc_init(env); |
799 |
|
|
800 |
|
if(env->free_string!=NULL) |
801 |
|
free(env->free_string); |
802 |
|
|
803 |
|
muntrace(); |
804 |
|
|
805 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
806 |
} |
} |
807 |
|
|
812 |
toss(env); |
toss(env); |
813 |
} |
} |
814 |
|
|
815 |
int main() |
/* List all defined words */ |
816 |
|
extern void words(environment *env) |
817 |
|
{ |
818 |
|
symbol *temp; |
819 |
|
int i; |
820 |
|
|
821 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
822 |
|
temp= env->symbols[i]; |
823 |
|
while(temp!=NULL) { |
824 |
|
printf("%s\n", temp->id); |
825 |
|
temp= temp->next; |
826 |
|
} |
827 |
|
} |
828 |
|
} |
829 |
|
|
830 |
|
/* Internal forget function */ |
831 |
|
void forget_sym(symbol **hash_entry) { |
832 |
|
symbol *temp; |
833 |
|
|
834 |
|
temp= *hash_entry; |
835 |
|
*hash_entry= (*hash_entry)->next; |
836 |
|
|
837 |
|
free(temp->id); |
838 |
|
free(temp); |
839 |
|
} |
840 |
|
|
841 |
|
/* Forgets a symbol (remove it from the hash table) */ |
842 |
|
extern void forget(environment *env) |
843 |
|
{ |
844 |
|
char* sym_id; |
845 |
|
stackitem *stack_head= env->head; |
846 |
|
|
847 |
|
if(stack_head==NULL) { |
848 |
|
printerr("Too Few Arguments"); |
849 |
|
env->err=1; |
850 |
|
return; |
851 |
|
} |
852 |
|
|
853 |
|
if(stack_head->item->type!=symb) { |
854 |
|
printerr("Bad Argument Type"); |
855 |
|
env->err=2; |
856 |
|
return; |
857 |
|
} |
858 |
|
|
859 |
|
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
860 |
|
toss(env); |
861 |
|
|
862 |
|
return forget_sym(hash(env->symbols, sym_id)); |
863 |
|
} |
864 |
|
|
865 |
|
/* Returns the current error number to the stack */ |
866 |
|
extern void errn(environment *env){ |
867 |
|
push_int(env, env->err); |
868 |
|
} |
869 |
|
|
870 |
|
int main(int argc, char **argv) |
871 |
{ |
{ |
872 |
environment myenv; |
environment myenv; |
873 |
char in_string[100]; |
|
874 |
|
int c; /* getopt option character */ |
875 |
|
|
876 |
|
mtrace(); |
877 |
|
|
878 |
init_env(&myenv); |
init_env(&myenv); |
879 |
|
|
880 |
printf("okidok\n "); |
myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO); |
881 |
|
|
882 |
while(fgets(in_string, 100, stdin) != NULL) { |
while ((c = getopt (argc, argv, "i")) != -1) |
883 |
stack_read(&myenv, in_string); |
switch (c) |
884 |
printf("okidok\n "); |
{ |
885 |
|
case 'i': |
886 |
|
myenv.interactive = 1; |
887 |
|
break; |
888 |
|
case '?': |
889 |
|
fprintf (stderr, |
890 |
|
"Unknown option character `\\x%x'.\n", |
891 |
|
optopt); |
892 |
|
return EX_USAGE; |
893 |
|
default: |
894 |
|
abort (); |
895 |
|
} |
896 |
|
|
897 |
|
if (optind < argc) { |
898 |
|
myenv.interactive = 0; |
899 |
|
myenv.inputstream= fopen(argv[optind], "r"); |
900 |
|
if(myenv.inputstream== NULL) { |
901 |
|
perror(argv[0]); |
902 |
|
exit (EX_NOINPUT); |
903 |
|
} |
904 |
} |
} |
905 |
|
|
906 |
exit(EXIT_SUCCESS); |
while(1) { |
907 |
|
if(myenv.in_string==NULL) { |
908 |
|
if (myenv.interactive) { |
909 |
|
if(myenv.err) { |
910 |
|
printf("(error %d)\n", myenv.err); |
911 |
|
} |
912 |
|
nl(); |
913 |
|
printstack(&myenv); |
914 |
|
printf("> "); |
915 |
|
} |
916 |
|
myenv.err=0; |
917 |
|
} |
918 |
|
sx_72656164(&myenv); |
919 |
|
if (myenv.err==4) { |
920 |
|
return EX_NOINPUT; |
921 |
|
} else if(myenv.head!=NULL |
922 |
|
&& myenv.head->item->type==symb |
923 |
|
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
924 |
|
toss(&myenv); /* No error check in main */ |
925 |
|
eval(&myenv); |
926 |
|
} |
927 |
|
gc_init(&myenv); |
928 |
|
} |
929 |
|
quit(&myenv); |
930 |
|
return EXIT_FAILURE; |
931 |
|
} |
932 |
|
|
933 |
|
/* "+" */ |
934 |
|
extern void sx_2b(environment *env) { |
935 |
|
int a, b; |
936 |
|
size_t len; |
937 |
|
char* new_string; |
938 |
|
value *a_val, *b_val; |
939 |
|
|
940 |
|
if((env->head)==NULL || env->head->next==NULL) { |
941 |
|
printerr("Too Few Arguments"); |
942 |
|
env->err= 1; |
943 |
|
return; |
944 |
|
} |
945 |
|
|
946 |
|
if(env->head->item->type==string |
947 |
|
&& env->head->next->item->type==string) { |
948 |
|
a_val= env->head->item; |
949 |
|
b_val= env->head->next->item; |
950 |
|
protect(env, a_val); protect(env, b_val); |
951 |
|
toss(env); if(env->err) return; |
952 |
|
toss(env); if(env->err) return; |
953 |
|
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
954 |
|
new_string= malloc(len); |
955 |
|
strcpy(new_string, b_val->content.ptr); |
956 |
|
strcat(new_string, a_val->content.ptr); |
957 |
|
push_cstring(env, new_string); |
958 |
|
unprotect(env); unprotect(env); |
959 |
|
free(new_string); |
960 |
|
return; |
961 |
|
} |
962 |
|
|
963 |
|
if(env->head->item->type!=integer |
964 |
|
|| env->head->next->item->type!=integer) { |
965 |
|
printerr("Bad Argument Type"); |
966 |
|
env->err=2; |
967 |
|
return; |
968 |
|
} |
969 |
|
a= env->head->item->content.val; |
970 |
|
toss(env); if(env->err) return; |
971 |
|
|
972 |
|
b= env->head->item->content.val; |
973 |
|
toss(env); if(env->err) return; |
974 |
|
push_int(env, a+b); |
975 |
|
} |
976 |
|
|
977 |
|
/* "-" */ |
978 |
|
extern void sx_2d(environment *env) { |
979 |
|
int a, b; |
980 |
|
|
981 |
|
if((env->head)==NULL || env->head->next==NULL) { |
982 |
|
printerr("Too Few Arguments"); |
983 |
|
env->err=1; |
984 |
|
return; |
985 |
|
} |
986 |
|
|
987 |
|
if(env->head->item->type!=integer |
988 |
|
|| env->head->next->item->type!=integer) { |
989 |
|
printerr("Bad Argument Type"); |
990 |
|
env->err=2; |
991 |
|
return; |
992 |
|
} |
993 |
|
|
994 |
|
a=env->head->item->content.val; |
995 |
|
toss(env); if(env->err) return; |
996 |
|
b=env->head->item->content.val; |
997 |
|
toss(env); if(env->err) return; |
998 |
|
push_int(env, b-a); |
999 |
|
} |
1000 |
|
|
1001 |
|
/* ">" */ |
1002 |
|
extern void sx_3e(environment *env) { |
1003 |
|
int a, b; |
1004 |
|
|
1005 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1006 |
|
printerr("Too Few Arguments"); |
1007 |
|
env->err=1; |
1008 |
|
return; |
1009 |
|
} |
1010 |
|
|
1011 |
|
if(env->head->item->type!=integer |
1012 |
|
|| env->head->next->item->type!=integer) { |
1013 |
|
printerr("Bad Argument Type"); |
1014 |
|
env->err=2; |
1015 |
|
return; |
1016 |
|
} |
1017 |
|
|
1018 |
|
a=env->head->item->content.val; |
1019 |
|
toss(env); if(env->err) return; |
1020 |
|
b=env->head->item->content.val; |
1021 |
|
toss(env); if(env->err) return; |
1022 |
|
push_int(env, b>a); |
1023 |
|
} |
1024 |
|
|
1025 |
|
/* Return copy of a value */ |
1026 |
|
value *copy_val(environment *env, value *old_value){ |
1027 |
|
stackitem *old_item, *new_item, *prev_item; |
1028 |
|
|
1029 |
|
value *new_value= new_val(env); |
1030 |
|
|
1031 |
|
protect(env, old_value); |
1032 |
|
new_value->type= old_value->type; |
1033 |
|
|
1034 |
|
switch(old_value->type){ |
1035 |
|
case integer: |
1036 |
|
new_value->content.val= old_value->content.val; |
1037 |
|
break; |
1038 |
|
case string: |
1039 |
|
(char *)(new_value->content.ptr)= |
1040 |
|
strdup((char *)(old_value->content.ptr)); |
1041 |
|
break; |
1042 |
|
case func: |
1043 |
|
case symb: |
1044 |
|
new_value->content.ptr= old_value->content.ptr; |
1045 |
|
break; |
1046 |
|
case list: |
1047 |
|
new_value->content.ptr= NULL; |
1048 |
|
|
1049 |
|
prev_item= NULL; |
1050 |
|
old_item= (stackitem*)(old_value->content.ptr); |
1051 |
|
|
1052 |
|
while(old_item != NULL) { /* While list is not empty */ |
1053 |
|
new_item= malloc(sizeof(stackitem)); |
1054 |
|
new_item->item= copy_val(env, old_item->item); /* recurse */ |
1055 |
|
new_item->next= NULL; |
1056 |
|
if(prev_item != NULL) /* If this wasn't the first item */ |
1057 |
|
prev_item->next= new_item; /* point the previous item to the |
1058 |
|
new item */ |
1059 |
|
else |
1060 |
|
new_value->content.ptr= new_item; |
1061 |
|
old_item= old_item->next; |
1062 |
|
prev_item= new_item; |
1063 |
|
} |
1064 |
|
break; |
1065 |
|
} |
1066 |
|
|
1067 |
|
unprotect(env); |
1068 |
|
|
1069 |
|
return new_value; |
1070 |
|
} |
1071 |
|
|
1072 |
|
/* "dup"; duplicates an item on the stack */ |
1073 |
|
extern void sx_647570(environment *env) { |
1074 |
|
if((env->head)==NULL) { |
1075 |
|
printerr("Too Few Arguments"); |
1076 |
|
env->err= 1; |
1077 |
|
return; |
1078 |
|
} |
1079 |
|
push_val(env, copy_val(env, env->head->item)); |
1080 |
|
} |
1081 |
|
|
1082 |
|
/* "if", If-Then */ |
1083 |
|
extern void sx_6966(environment *env) { |
1084 |
|
|
1085 |
|
int truth; |
1086 |
|
|
1087 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1088 |
|
printerr("Too Few Arguments"); |
1089 |
|
env->err= 1; |
1090 |
|
return; |
1091 |
|
} |
1092 |
|
|
1093 |
|
if(env->head->next->item->type != integer) { |
1094 |
|
printerr("Bad Argument Type"); |
1095 |
|
env->err=2; |
1096 |
|
return; |
1097 |
|
} |
1098 |
|
|
1099 |
|
swap(env); |
1100 |
|
if(env->err) return; |
1101 |
|
|
1102 |
|
truth=env->head->item->content.val; |
1103 |
|
|
1104 |
|
toss(env); |
1105 |
|
if(env->err) return; |
1106 |
|
|
1107 |
|
if(truth) |
1108 |
|
eval(env); |
1109 |
|
else |
1110 |
|
toss(env); |
1111 |
|
} |
1112 |
|
|
1113 |
|
/* If-Then-Else */ |
1114 |
|
extern void ifelse(environment *env) { |
1115 |
|
|
1116 |
|
int truth; |
1117 |
|
|
1118 |
|
if((env->head)==NULL || env->head->next==NULL |
1119 |
|
|| env->head->next->next==NULL) { |
1120 |
|
printerr("Too Few Arguments"); |
1121 |
|
env->err=1; |
1122 |
|
return; |
1123 |
|
} |
1124 |
|
|
1125 |
|
if(env->head->next->next->item->type != integer) { |
1126 |
|
printerr("Bad Argument Type"); |
1127 |
|
env->err=2; |
1128 |
|
return; |
1129 |
|
} |
1130 |
|
|
1131 |
|
rot(env); |
1132 |
|
if(env->err) return; |
1133 |
|
|
1134 |
|
truth=env->head->item->content.val; |
1135 |
|
|
1136 |
|
toss(env); |
1137 |
|
if(env->err) return; |
1138 |
|
|
1139 |
|
if(!truth) |
1140 |
|
swap(env); |
1141 |
|
if(env->err) return; |
1142 |
|
|
1143 |
|
toss(env); |
1144 |
|
if(env->err) return; |
1145 |
|
|
1146 |
|
eval(env); |
1147 |
|
} |
1148 |
|
|
1149 |
|
/* "while" */ |
1150 |
|
extern void sx_7768696c65(environment *env) { |
1151 |
|
|
1152 |
|
int truth; |
1153 |
|
value *loop, *test; |
1154 |
|
|
1155 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1156 |
|
printerr("Too Few Arguments"); |
1157 |
|
env->err=1; |
1158 |
|
return; |
1159 |
|
} |
1160 |
|
|
1161 |
|
loop= env->head->item; |
1162 |
|
protect(env, loop); |
1163 |
|
toss(env); if(env->err) return; |
1164 |
|
|
1165 |
|
test= env->head->item; |
1166 |
|
protect(env, test); |
1167 |
|
toss(env); if(env->err) return; |
1168 |
|
|
1169 |
|
do { |
1170 |
|
push_val(env, test); |
1171 |
|
eval(env); |
1172 |
|
|
1173 |
|
if(env->head->item->type != integer) { |
1174 |
|
printerr("Bad Argument Type"); |
1175 |
|
env->err= 2; |
1176 |
|
return; |
1177 |
|
} |
1178 |
|
|
1179 |
|
truth= env->head->item->content.val; |
1180 |
|
toss(env); if(env->err) return; |
1181 |
|
|
1182 |
|
if(truth) { |
1183 |
|
push_val(env, loop); |
1184 |
|
eval(env); |
1185 |
|
} else { |
1186 |
|
toss(env); |
1187 |
|
} |
1188 |
|
|
1189 |
|
} while(truth); |
1190 |
|
|
1191 |
|
unprotect(env); unprotect(env); |
1192 |
|
} |
1193 |
|
|
1194 |
|
|
1195 |
|
/* "for"; for-loop */ |
1196 |
|
extern void sx_666f72(environment *env) { |
1197 |
|
value *loop; |
1198 |
|
int foo1, foo2; |
1199 |
|
|
1200 |
|
if(env->head==NULL || env->head->next==NULL |
1201 |
|
|| env->head->next->next==NULL) { |
1202 |
|
printerr("Too Few Arguments"); |
1203 |
|
env->err= 1; |
1204 |
|
return; |
1205 |
|
} |
1206 |
|
|
1207 |
|
if(env->head->next->item->type!=integer |
1208 |
|
|| env->head->next->next->item->type!=integer) { |
1209 |
|
printerr("Bad Argument Type"); |
1210 |
|
env->err= 2; |
1211 |
|
return; |
1212 |
|
} |
1213 |
|
|
1214 |
|
loop= env->head->item; |
1215 |
|
protect(env, loop); |
1216 |
|
toss(env); if(env->err) return; |
1217 |
|
|
1218 |
|
foo2= env->head->item->content.val; |
1219 |
|
toss(env); if(env->err) return; |
1220 |
|
|
1221 |
|
foo1= env->head->item->content.val; |
1222 |
|
toss(env); if(env->err) return; |
1223 |
|
|
1224 |
|
if(foo1<=foo2) { |
1225 |
|
while(foo1<=foo2) { |
1226 |
|
push_int(env, foo1); |
1227 |
|
push_val(env, loop); |
1228 |
|
eval(env); if(env->err) return; |
1229 |
|
foo1++; |
1230 |
|
} |
1231 |
|
} else { |
1232 |
|
while(foo1>=foo2) { |
1233 |
|
push_int(env, foo1); |
1234 |
|
push_val(env, loop); |
1235 |
|
eval(env); if(env->err) return; |
1236 |
|
foo1--; |
1237 |
|
} |
1238 |
|
} |
1239 |
|
unprotect(env); |
1240 |
|
} |
1241 |
|
|
1242 |
|
/* Variant of for-loop */ |
1243 |
|
extern void foreach(environment *env) { |
1244 |
|
|
1245 |
|
value *loop, *foo; |
1246 |
|
stackitem *iterator; |
1247 |
|
|
1248 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1249 |
|
printerr("Too Few Arguments"); |
1250 |
|
env->err= 1; |
1251 |
|
return; |
1252 |
|
} |
1253 |
|
|
1254 |
|
if(env->head->next->item->type != list) { |
1255 |
|
printerr("Bad Argument Type"); |
1256 |
|
env->err= 2; |
1257 |
|
return; |
1258 |
|
} |
1259 |
|
|
1260 |
|
loop= env->head->item; |
1261 |
|
protect(env, loop); |
1262 |
|
toss(env); if(env->err) return; |
1263 |
|
|
1264 |
|
foo= env->head->item; |
1265 |
|
protect(env, foo); |
1266 |
|
toss(env); if(env->err) return; |
1267 |
|
|
1268 |
|
iterator= foo->content.ptr; |
1269 |
|
|
1270 |
|
while(iterator!=NULL) { |
1271 |
|
push_val(env, iterator->item); |
1272 |
|
push_val(env, loop); |
1273 |
|
eval(env); if(env->err) return; |
1274 |
|
iterator= iterator->next; |
1275 |
|
} |
1276 |
|
unprotect(env); unprotect(env); |
1277 |
|
} |
1278 |
|
|
1279 |
|
/* "to" */ |
1280 |
|
extern void to(environment *env) { |
1281 |
|
int i, start, ending; |
1282 |
|
stackitem *temp_head; |
1283 |
|
value *temp_val; |
1284 |
|
|
1285 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1286 |
|
printerr("Too Few Arguments"); |
1287 |
|
env->err=1; |
1288 |
|
return; |
1289 |
|
} |
1290 |
|
|
1291 |
|
if(env->head->item->type!=integer |
1292 |
|
|| env->head->next->item->type!=integer) { |
1293 |
|
printerr("Bad Argument Type"); |
1294 |
|
env->err=2; |
1295 |
|
return; |
1296 |
|
} |
1297 |
|
|
1298 |
|
ending= env->head->item->content.val; |
1299 |
|
toss(env); if(env->err) return; |
1300 |
|
start= env->head->item->content.val; |
1301 |
|
toss(env); if(env->err) return; |
1302 |
|
|
1303 |
|
temp_head= env->head; |
1304 |
|
env->head= NULL; |
1305 |
|
|
1306 |
|
if(ending>=start) { |
1307 |
|
for(i= ending; i>=start; i--) |
1308 |
|
push_int(env, i); |
1309 |
|
} else { |
1310 |
|
for(i= ending; i<=start; i++) |
1311 |
|
push_int(env, i); |
1312 |
|
} |
1313 |
|
|
1314 |
|
temp_val= new_val(env); |
1315 |
|
temp_val->content.ptr= env->head; |
1316 |
|
temp_val->type= list; |
1317 |
|
env->head= temp_head; |
1318 |
|
push_val(env, temp_val); |
1319 |
|
} |
1320 |
|
|
1321 |
|
/* Read a string */ |
1322 |
|
extern void readline(environment *env) { |
1323 |
|
char in_string[101]; |
1324 |
|
|
1325 |
|
if(fgets(in_string, 100, env->inputstream)==NULL) |
1326 |
|
push_cstring(env, ""); |
1327 |
|
else |
1328 |
|
push_cstring(env, in_string); |
1329 |
|
} |
1330 |
|
|
1331 |
|
/* "read"; Read a value and place on stack */ |
1332 |
|
extern void sx_72656164(environment *env) { |
1333 |
|
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
1334 |
|
const char strform[]= "\"%[^\"]\"%n"; |
1335 |
|
const char intform[]= "%i%n"; |
1336 |
|
const char blankform[]= "%*[ \t]%n"; |
1337 |
|
const char ebrackform[]= "]%n"; |
1338 |
|
const char semicform[]= ";%n"; |
1339 |
|
const char bbrackform[]= "[%n"; |
1340 |
|
|
1341 |
|
int itemp, readlength= -1; |
1342 |
|
static int depth= 0; |
1343 |
|
char *match; |
1344 |
|
size_t inlength; |
1345 |
|
|
1346 |
|
if(env->in_string==NULL) { |
1347 |
|
if(depth > 0 && env->interactive) { |
1348 |
|
printf("]> "); |
1349 |
|
} |
1350 |
|
readline(env); if(env->err) return; |
1351 |
|
|
1352 |
|
if(((char *)(env->head->item->content.ptr))[0]=='\0'){ |
1353 |
|
env->err= 4; /* "" means EOF */ |
1354 |
|
return; |
1355 |
|
} |
1356 |
|
|
1357 |
|
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
1358 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
1359 |
|
strcpy(env->in_string, env->head->item->content.ptr); |
1360 |
|
toss(env); if(env->err) return; |
1361 |
|
} |
1362 |
|
|
1363 |
|
inlength= strlen(env->in_string)+1; |
1364 |
|
match= malloc(inlength); |
1365 |
|
|
1366 |
|
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
1367 |
|
&& readlength != -1) { |
1368 |
|
; |
1369 |
|
} else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF |
1370 |
|
&& readlength != -1) { |
1371 |
|
push_int(env, itemp); |
1372 |
|
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
1373 |
|
&& readlength != -1) { |
1374 |
|
push_cstring(env, match); |
1375 |
|
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
1376 |
|
&& readlength != -1) { |
1377 |
|
push_sym(env, match); |
1378 |
|
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
1379 |
|
&& readlength != -1) { |
1380 |
|
pack(env); if(env->err) return; |
1381 |
|
if(depth != 0) depth--; |
1382 |
|
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
1383 |
|
&& readlength != -1) { |
1384 |
|
push_sym(env, ";"); |
1385 |
|
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
1386 |
|
&& readlength != -1) { |
1387 |
|
push_sym(env, "["); |
1388 |
|
depth++; |
1389 |
|
} else { |
1390 |
|
free(env->free_string); |
1391 |
|
env->in_string = env->free_string = NULL; |
1392 |
|
} |
1393 |
|
if ( env->in_string != NULL) { |
1394 |
|
env->in_string += readlength; |
1395 |
|
} |
1396 |
|
|
1397 |
|
free(match); |
1398 |
|
|
1399 |
|
if(depth) |
1400 |
|
return sx_72656164(env); |
1401 |
} |
} |