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