1 |
/* printf, sscanf, fgets, fprintf */ |
/* printf, sscanf, fgets, fprintf, fopen, perror */ |
2 |
#include <stdio.h> |
#include <stdio.h> |
3 |
/* exit, EXIT_SUCCESS, malloc, free */ |
/* exit, EXIT_SUCCESS, malloc, free */ |
4 |
#include <stdlib.h> |
#include <stdlib.h> |
8 |
#include <dlfcn.h> |
#include <dlfcn.h> |
9 |
/* strcmp, strcpy, strlen, strcat, strdup */ |
/* strcmp, strcpy, strlen, strcat, strdup */ |
10 |
#include <string.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 2048 |
#define HASHTBLSIZE 2048 |
19 |
|
|
34 |
int val; /* ...or an integer */ |
int val; /* ...or an integer */ |
35 |
} content; /* Stores a pointer or an integer */ |
} content; /* Stores a pointer or an integer */ |
36 |
|
|
37 |
int refcount; /* Reference counter */ |
int gc_garb; |
38 |
|
|
39 |
} value; |
} value; |
40 |
|
|
61 |
/* An environment; gives access to the stack and a hash table of |
/* An environment; gives access to the stack and a hash table of |
62 |
defined symbols */ |
defined symbols */ |
63 |
typedef struct { |
typedef struct { |
64 |
|
stackitem *gc_ref; |
65 |
|
int gc_limit, gc_count; |
66 |
|
|
67 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
68 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
69 |
int err; /* Error flag */ |
int err; /* Error flag */ |
|
int non_eval_flag; |
|
70 |
char *in_string; /* Input pending to be read */ |
char *in_string; /* Input pending to be read */ |
71 |
|
char *free_string; /* Free this string when all input is |
72 |
|
read from in_string */ |
73 |
|
FILE *inputstream; /* stdin or a file, most likely */ |
74 |
|
int interactive; /* print prompts, stack, etc */ |
75 |
} environment; |
} environment; |
76 |
|
|
77 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
83 |
{ |
{ |
84 |
int i; |
int i; |
85 |
|
|
86 |
env->in_string= NULL; |
env->gc_limit= 20; |
87 |
env->err= 0; |
env->gc_count= 0; |
88 |
env->non_eval_flag= 0; |
|
89 |
|
env->head= NULL; |
90 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
91 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
92 |
|
env->err= 0; |
93 |
|
env->in_string= NULL; |
94 |
|
env->free_string= NULL; |
95 |
|
env->inputstream= stdin; |
96 |
|
env->interactive= 1; |
97 |
} |
} |
98 |
|
|
99 |
void printerr(const char* in_string) { |
void printerr(const char* in_string) { |
100 |
fprintf(stderr, "Err: %s\n", in_string); |
fprintf(stderr, "Err: %s\n", in_string); |
101 |
} |
} |
102 |
|
|
|
/* 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); |
|
|
break; |
|
|
case list: /* lists needs to be freed recursively */ |
|
|
item=val->content.ptr; |
|
|
while(item != NULL) { /* for all stack items */ |
|
|
free_val(item->item); /* free the value */ |
|
|
temp=item->next; /* save next ptr */ |
|
|
free(item); /* free the stackitem */ |
|
|
item=temp; /* go to next stackitem */ |
|
|
} |
|
|
free(val); /* Free the actual list value */ |
|
|
break; |
|
|
case integer: |
|
|
case func: |
|
|
case symb: |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
103 |
/* Discard the top element of the stack. */ |
/* Discard the top element of the stack. */ |
104 |
extern void toss(environment *env) |
extern void toss(environment *env) |
105 |
{ |
{ |
111 |
return; |
return; |
112 |
} |
} |
113 |
|
|
|
free_val(env->head->item); /* Free the value */ |
|
114 |
env->head= env->head->next; /* Remove the top stack item */ |
env->head= env->head->next; /* Remove the top stack item */ |
115 |
free(temp); /* Free the old top stack item */ |
free(temp); /* Free the old top stack item */ |
116 |
} |
} |
144 |
} |
} |
145 |
} |
} |
146 |
|
|
147 |
/* Generic push function. */ |
extern void gc_init(environment*); |
148 |
void push(environment *env, stackitem* in_item) |
|
149 |
{ |
value* new_val(environment *env) { |
150 |
in_item->next= env->head; |
value *nval= malloc(sizeof(value)); |
151 |
env->head= in_item; |
stackitem *nitem= malloc(sizeof(stackitem)); |
152 |
|
|
153 |
|
if(env->gc_count >= env->gc_limit) |
154 |
|
gc_init(env); |
155 |
|
|
156 |
|
nval->content.ptr= NULL; |
157 |
|
|
158 |
|
nitem->item= nval; |
159 |
|
nitem->next= env->gc_ref; |
160 |
|
env->gc_ref= nitem; |
161 |
|
|
162 |
|
env->gc_count++; |
163 |
|
|
164 |
|
return nval; |
165 |
|
} |
166 |
|
|
167 |
|
void gc_mark(value *val) { |
168 |
|
stackitem *iterator; |
169 |
|
|
170 |
|
if(val==NULL || val->gc_garb==0) |
171 |
|
return; |
172 |
|
|
173 |
|
val->gc_garb= 0; |
174 |
|
|
175 |
|
if(val->type==list) { |
176 |
|
iterator= val->content.ptr; |
177 |
|
|
178 |
|
while(iterator!=NULL) { |
179 |
|
gc_mark(iterator->item); |
180 |
|
iterator= iterator->next; |
181 |
|
} |
182 |
|
} |
183 |
|
} |
184 |
|
|
185 |
|
extern void gc_init(environment *env) { |
186 |
|
stackitem *new_head= NULL, *titem, *iterator= env->gc_ref; |
187 |
|
symbol *tsymb; |
188 |
|
int i; |
189 |
|
|
190 |
|
while(iterator!=NULL) { |
191 |
|
iterator->item->gc_garb= 1; |
192 |
|
iterator= iterator->next; |
193 |
|
} |
194 |
|
|
195 |
|
/* Mark */ |
196 |
|
iterator= env->head; |
197 |
|
while(iterator!=NULL) { |
198 |
|
gc_mark(iterator->item); |
199 |
|
iterator= iterator->next; |
200 |
|
} |
201 |
|
|
202 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
203 |
|
tsymb= env->symbols[i]; |
204 |
|
while(tsymb!=NULL) { |
205 |
|
gc_mark(tsymb->val); |
206 |
|
tsymb= tsymb->next; |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
|
env->gc_count= 0; |
211 |
|
|
212 |
|
/* Sweep */ |
213 |
|
while(env->gc_ref!=NULL) { |
214 |
|
if(env->gc_ref->item->gc_garb) { |
215 |
|
switch(env->gc_ref->item->type) { |
216 |
|
case string: |
217 |
|
free(env->gc_ref->item->content.ptr); |
218 |
|
break; |
219 |
|
case integer: |
220 |
|
break; |
221 |
|
case list: |
222 |
|
while(env->gc_ref->item->content.ptr!=NULL) { |
223 |
|
titem= env->gc_ref->item->content.ptr; |
224 |
|
env->gc_ref->item->content.ptr= titem->next; |
225 |
|
free(titem); |
226 |
|
} |
227 |
|
break; |
228 |
|
default: |
229 |
|
break; |
230 |
|
} |
231 |
|
free(env->gc_ref->item); |
232 |
|
titem= env->gc_ref->next; |
233 |
|
free(env->gc_ref); |
234 |
|
env->gc_ref= titem; |
235 |
|
} else { |
236 |
|
titem= env->gc_ref->next; |
237 |
|
env->gc_ref->next= new_head; |
238 |
|
new_head= env->gc_ref; |
239 |
|
env->gc_ref= titem; |
240 |
|
env->gc_count++; |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
env->gc_limit= env->gc_count+20; |
245 |
|
env->gc_ref= new_head; |
246 |
} |
} |
247 |
|
|
248 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
250 |
{ |
{ |
251 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
252 |
new_item->item= val; |
new_item->item= val; |
253 |
val->refcount++; |
new_item->next= env->head; |
254 |
push(env, new_item); |
env->head= new_item; |
255 |
} |
} |
256 |
|
|
257 |
/* Push an integer onto the stack. */ |
/* Push an integer onto the stack. */ |
258 |
void push_int(environment *env, int in_val) |
void push_int(environment *env, int in_val) |
259 |
{ |
{ |
260 |
value *new_value= malloc(sizeof(value)); |
value *new_value= new_val(env); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item= new_value; |
|
261 |
|
|
262 |
new_value->content.val= in_val; |
new_value->content.val= in_val; |
263 |
new_value->type= integer; |
new_value->type= integer; |
|
new_value->refcount=1; |
|
264 |
|
|
265 |
push(env, new_item); |
push_val(env, new_value); |
266 |
} |
} |
267 |
|
|
268 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
269 |
void push_cstring(environment *env, const char *in_string) |
void push_cstring(environment *env, const char *in_string) |
270 |
{ |
{ |
271 |
value *new_value= malloc(sizeof(value)); |
value *new_value= new_val(env); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item=new_value; |
|
272 |
|
|
273 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
274 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
275 |
new_value->type= string; |
new_value->type= string; |
|
new_value->refcount=1; |
|
276 |
|
|
277 |
push(env, new_item); |
push_val(env, new_value); |
278 |
} |
} |
279 |
|
|
280 |
/* Mangle a symbol name to a valid C identifier name */ |
/* Mangle a symbol name to a valid C identifier name */ |
298 |
} |
} |
299 |
|
|
300 |
extern void mangle(environment *env){ |
extern void mangle(environment *env){ |
|
value *new_value; |
|
301 |
char *new_string; |
char *new_string; |
302 |
|
|
303 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
317 |
toss(env); |
toss(env); |
318 |
if(env->err) return; |
if(env->err) return; |
319 |
|
|
320 |
new_value= malloc(sizeof(value)); |
push_cstring(env, new_string); |
|
new_value->content.ptr= new_string; |
|
|
new_value->type= string; |
|
|
new_value->refcount=1; |
|
|
|
|
|
push_val(env, new_value); |
|
321 |
} |
} |
322 |
|
|
323 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
324 |
void push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
325 |
{ |
{ |
|
stackitem *new_item; /* The new stack item */ |
|
|
/* ...which will contain... */ |
|
326 |
value *new_value; /* A new symbol value */ |
value *new_value; /* A new symbol value */ |
327 |
/* ...which might point to... */ |
/* ...which might point to... */ |
328 |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
335 |
const char *dlerr; /* Dynamic linker error */ |
const char *dlerr; /* Dynamic linker error */ |
336 |
char *mangled; /* Mangled function name */ |
char *mangled; /* Mangled function name */ |
337 |
|
|
338 |
/* 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; |
|
339 |
|
|
340 |
/* The new value is a symbol */ |
/* The new value is a symbol */ |
341 |
new_value->type= symb; |
new_value->type= symb; |
|
new_value->refcount= 1; |
|
342 |
|
|
343 |
/* Look up the symbol name in the hash table */ |
/* Look up the symbol name in the hash table */ |
344 |
new_symbol= hash(env->symbols, in_string); |
new_symbol= hash(env->symbols, in_string); |
361 |
if(handle==NULL) /* If no handle */ |
if(handle==NULL) /* If no handle */ |
362 |
handle= dlopen(NULL, RTLD_LAZY); |
handle= dlopen(NULL, RTLD_LAZY); |
363 |
|
|
364 |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
mangled=mangle_str(in_string); /* mangle the name */ |
365 |
|
funcptr= dlsym(handle, mangled); /* and try to find it */ |
366 |
|
free(mangled); |
367 |
dlerr=dlerror(); |
dlerr=dlerror(); |
368 |
if(dlerr != NULL) { /* If no function was found */ |
if(dlerr != NULL) { /* If no function was found */ |
369 |
mangled=mangle_str(in_string); |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
|
funcptr= dlsym(handle, mangled); /* try mangling it */ |
|
|
free(mangled); |
|
370 |
dlerr=dlerror(); |
dlerr=dlerror(); |
371 |
} |
} |
372 |
if(dlerr==NULL) { /* If a function was found */ |
if(dlerr==NULL) { /* If a function was found */ |
373 |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
new_fvalue= new_val(env); /* Create a new value */ |
374 |
new_fvalue->type=func; /* The new value is a function pointer */ |
new_fvalue->type=func; /* The new value is a function pointer */ |
375 |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
376 |
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
377 |
function value */ |
function value */ |
|
new_fvalue->refcount= 1; |
|
378 |
} |
} |
379 |
} |
} |
380 |
push(env, new_item); |
push_val(env, new_value); |
381 |
} |
} |
382 |
|
|
383 |
/* Print newline. */ |
/* Print newline. */ |
417 |
} |
} |
418 |
|
|
419 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
420 |
void print_h(stackitem *stack_head) |
void print_h(stackitem *stack_head, int noquote) |
421 |
{ |
{ |
422 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
423 |
case integer: |
case integer: |
424 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
425 |
break; |
break; |
426 |
case string: |
case string: |
427 |
printf("%s", (char*)stack_head->item->content.ptr); |
if(noquote) |
428 |
|
printf("%s", (char*)stack_head->item->content.ptr); |
429 |
|
else |
430 |
|
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
431 |
break; |
break; |
432 |
case symb: |
case symb: |
433 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
440 |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
441 |
printf("[ "); |
printf("[ "); |
442 |
while(stack_head != NULL) { |
while(stack_head != NULL) { |
443 |
print_h(stack_head); |
print_h(stack_head, noquote); |
444 |
printf(" "); |
printf(" "); |
445 |
stack_head=stack_head->next; |
stack_head=stack_head->next; |
446 |
} |
} |
455 |
env->err=1; |
env->err=1; |
456 |
return; |
return; |
457 |
} |
} |
458 |
print_h(env->head); |
print_h(env->head, 0); |
459 |
|
nl(); |
460 |
} |
} |
461 |
|
|
462 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
467 |
toss(env); |
toss(env); |
468 |
} |
} |
469 |
|
|
470 |
|
extern void princ_(environment *env) { |
471 |
|
if(env->head==NULL) { |
472 |
|
printerr("Too Few Arguments"); |
473 |
|
env->err=1; |
474 |
|
return; |
475 |
|
} |
476 |
|
print_h(env->head, 1); |
477 |
|
} |
478 |
|
|
479 |
|
/* Prints the top element of the stack and then discards it. */ |
480 |
|
extern void princ(environment *env) |
481 |
|
{ |
482 |
|
princ_(env); |
483 |
|
if(env->err) return; |
484 |
|
toss(env); |
485 |
|
} |
486 |
|
|
487 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
488 |
void print_st(stackitem *stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
489 |
{ |
{ |
490 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
491 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
492 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
493 |
print_h(stack_head); |
print_h(stack_head, 0); |
494 |
nl(); |
nl(); |
495 |
} |
} |
496 |
|
|
498 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
499 |
{ |
{ |
500 |
if(env->head == NULL) { |
if(env->head == NULL) { |
501 |
|
printf("Stack Empty\n"); |
502 |
return; |
return; |
503 |
} |
} |
504 |
print_st(env->head, 1); |
print_st(env->head, 1); |
|
nl(); |
|
505 |
} |
} |
506 |
|
|
507 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
574 |
value* temp_val; |
value* temp_val; |
575 |
stackitem* iterator; |
stackitem* iterator; |
576 |
|
|
577 |
|
eval_start: |
578 |
|
|
579 |
if(env->head==NULL) { |
if(env->head==NULL) { |
580 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
581 |
env->err=1; |
env->err=1; |
582 |
return; |
return; |
583 |
} |
} |
584 |
|
|
|
eval_start: |
|
|
|
|
585 |
switch(env->head->item->type) { |
switch(env->head->item->type) { |
586 |
/* if it's a symbol */ |
/* if it's a symbol */ |
587 |
case symb: |
case symb: |
602 |
/* If it's a list */ |
/* If it's a list */ |
603 |
case list: |
case list: |
604 |
temp_val= env->head->item; |
temp_val= env->head->item; |
|
env->head->item->refcount++; |
|
605 |
toss(env); |
toss(env); |
606 |
if(env->err) return; |
if(env->err) return; |
607 |
iterator= (stackitem*)temp_val->content.ptr; |
iterator= (stackitem*)temp_val->content.ptr; |
612 |
toss(env); |
toss(env); |
613 |
if(env->err) return; |
if(env->err) return; |
614 |
if(iterator->next == NULL){ |
if(iterator->next == NULL){ |
|
free_val(temp_val); |
|
615 |
goto eval_start; |
goto eval_start; |
616 |
} |
} |
617 |
eval(env); |
eval(env); |
619 |
} |
} |
620 |
iterator= iterator->next; |
iterator= iterator->next; |
621 |
} |
} |
|
free_val(temp_val); |
|
622 |
return; |
return; |
623 |
|
|
624 |
default: |
default: |
683 |
} |
} |
684 |
|
|
685 |
/* Push list */ |
/* Push list */ |
686 |
pack= malloc(sizeof(value)); |
pack= new_val(env); |
687 |
pack->type= list; |
pack->type= list; |
688 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
|
pack->refcount= 1; |
|
689 |
|
|
690 |
push_val(env, pack); |
push_val(env, pack); |
691 |
rev(env); |
rev(env); |
716 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
717 |
new_head= temp= env->head->item->content.ptr; |
new_head= temp= env->head->item->content.ptr; |
718 |
|
|
|
env->head->item->refcount++; |
|
719 |
toss(env); |
toss(env); |
720 |
|
|
721 |
/* Find the end of the list */ |
/* Find the end of the list */ |
801 |
sym=env->head->item->content.ptr; |
sym=env->head->item->content.ptr; |
802 |
|
|
803 |
/* if the symbol was bound to something else, throw it away */ |
/* if the symbol was bound to something else, throw it away */ |
|
if(sym->val != NULL) |
|
|
free_val(sym->val); |
|
804 |
|
|
805 |
/* Bind the symbol to the value */ |
/* Bind the symbol to the value */ |
806 |
sym->val= env->head->next->item; |
sym->val= env->head->next->item; |
|
sym->val->refcount++; /* Increase the reference counter */ |
|
807 |
|
|
808 |
toss(env); toss(env); |
toss(env); toss(env); |
809 |
} |
} |
810 |
|
|
811 |
|
extern void clear(environment *); |
812 |
|
void forget_sym(symbol **); |
813 |
|
|
814 |
/* Quit stack. */ |
/* Quit stack. */ |
815 |
extern void quit(environment *env) |
extern void quit(environment *env) |
816 |
{ |
{ |
817 |
|
long i; |
818 |
|
|
819 |
|
clear(env); |
820 |
|
|
821 |
|
if (env->err) return; |
822 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
823 |
|
while(env->symbols[i]!= NULL) { |
824 |
|
forget_sym(&(env->symbols[i])); |
825 |
|
} |
826 |
|
env->symbols[i]= NULL; |
827 |
|
} |
828 |
|
|
829 |
|
gc_init(env); |
830 |
|
|
831 |
|
if(env->free_string!=NULL) |
832 |
|
free(env->free_string); |
833 |
|
|
834 |
|
muntrace(); |
835 |
|
|
836 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
837 |
} |
} |
838 |
|
|
858 |
} |
} |
859 |
} |
} |
860 |
|
|
861 |
|
/* Internal forget function */ |
862 |
|
void forget_sym(symbol **hash_entry) { |
863 |
|
symbol *temp; |
864 |
|
|
865 |
|
temp= *hash_entry; |
866 |
|
*hash_entry= (*hash_entry)->next; |
867 |
|
|
868 |
|
free(temp->id); |
869 |
|
free(temp); |
870 |
|
} |
871 |
|
|
872 |
/* Forgets a symbol (remove it from the hash table) */ |
/* Forgets a symbol (remove it from the hash table) */ |
873 |
extern void forget(environment *env) |
extern void forget(environment *env) |
874 |
{ |
{ |
875 |
char* sym_id; |
char* sym_id; |
876 |
stackitem *stack_head= env->head; |
stackitem *stack_head= env->head; |
|
symbol **hash_entry, *temp; |
|
877 |
|
|
878 |
if(stack_head==NULL) { |
if(stack_head==NULL) { |
879 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
890 |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
891 |
toss(env); |
toss(env); |
892 |
|
|
893 |
hash_entry= hash(env->symbols, sym_id); |
return forget_sym(hash(env->symbols, sym_id)); |
|
temp= *hash_entry; |
|
|
*hash_entry= (*hash_entry)->next; |
|
|
|
|
|
if(temp->val!=NULL) { |
|
|
free_val(temp->val); |
|
|
} |
|
|
free(temp->id); |
|
|
free(temp); |
|
894 |
} |
} |
895 |
|
|
896 |
/* Returns the current error number to the stack */ |
/* Returns the current error number to the stack */ |
898 |
push_int(env, env->err); |
push_int(env, env->err); |
899 |
} |
} |
900 |
|
|
901 |
extern void read(environment*); |
extern void sx_72656164(environment*); |
902 |
|
|
903 |
int main() |
int main(int argc, char **argv) |
904 |
{ |
{ |
905 |
environment myenv; |
environment myenv; |
906 |
|
|
907 |
|
int c; /* getopt option character */ |
908 |
|
|
909 |
|
mtrace(); |
910 |
|
|
911 |
init_env(&myenv); |
init_env(&myenv); |
912 |
|
|
913 |
|
myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO); |
914 |
|
|
915 |
|
while ((c = getopt (argc, argv, "i")) != -1) |
916 |
|
switch (c) |
917 |
|
{ |
918 |
|
case 'i': |
919 |
|
myenv.interactive = 1; |
920 |
|
break; |
921 |
|
case '?': |
922 |
|
fprintf (stderr, |
923 |
|
"Unknown option character `\\x%x'.\n", |
924 |
|
optopt); |
925 |
|
return EX_USAGE; |
926 |
|
default: |
927 |
|
abort (); |
928 |
|
} |
929 |
|
|
930 |
|
if (optind < argc) { |
931 |
|
myenv.interactive = 0; |
932 |
|
myenv.inputstream= fopen(argv[optind], "r"); |
933 |
|
if(myenv.inputstream== NULL) { |
934 |
|
perror(argv[0]); |
935 |
|
exit (EX_NOINPUT); |
936 |
|
} |
937 |
|
} |
938 |
|
|
939 |
while(1) { |
while(1) { |
940 |
if(myenv.in_string==NULL) |
if(myenv.in_string==NULL) { |
941 |
printstack(&myenv); |
if (myenv.interactive) { |
942 |
read(&myenv); |
if(myenv.err) { |
943 |
if(myenv.err) { |
printf("(error %d)\n", myenv.err); |
944 |
printf("(error %d) ", myenv.err); |
} |
945 |
|
nl(); |
946 |
|
printstack(&myenv); |
947 |
|
printf("> "); |
948 |
|
} |
949 |
myenv.err=0; |
myenv.err=0; |
950 |
|
} |
951 |
|
sx_72656164(&myenv); |
952 |
|
if (myenv.err==4) { |
953 |
|
return EX_NOINPUT; |
954 |
} else if(myenv.head!=NULL |
} else if(myenv.head!=NULL |
955 |
&& myenv.head->item->type==symb |
&& myenv.head->item->type==symb |
956 |
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
962 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
963 |
} |
} |
964 |
|
|
965 |
/* + */ |
/* "+" */ |
966 |
extern void sx_2b(environment *env) { |
extern void sx_2b(environment *env) { |
967 |
int a, b; |
int a, b; |
968 |
size_t len; |
size_t len; |
979 |
&& env->head->next->item->type==string) { |
&& env->head->next->item->type==string) { |
980 |
a_val= env->head->item; |
a_val= env->head->item; |
981 |
b_val= env->head->next->item; |
b_val= env->head->next->item; |
|
a_val->refcount++; |
|
|
b_val->refcount++; |
|
982 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
983 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
984 |
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
985 |
new_string= malloc(len); |
new_string= malloc(len); |
986 |
strcpy(new_string, b_val->content.ptr); |
strcpy(new_string, b_val->content.ptr); |
987 |
strcat(new_string, a_val->content.ptr); |
strcat(new_string, a_val->content.ptr); |
|
free_val(a_val); free_val(b_val); |
|
988 |
push_cstring(env, new_string); |
push_cstring(env, new_string); |
989 |
free(new_string); |
free(new_string); |
990 |
return; |
return; |
997 |
return; |
return; |
998 |
} |
} |
999 |
a=env->head->item->content.val; |
a=env->head->item->content.val; |
1000 |
toss(env); |
toss(env); if(env->err) return; |
1001 |
if(env->err) return; |
|
1002 |
if(env->head->item->refcount == 1) |
b=env->head->item->content.val; |
1003 |
env->head->item->content.val += a; |
toss(env); if(env->err) return; |
1004 |
else { |
push_int(env, a+b); |
|
b=env->head->item->content.val; |
|
|
toss(env); |
|
|
if(env->err) return; |
|
|
push_int(env, a+b); |
|
|
} |
|
1005 |
} |
} |
1006 |
|
|
1007 |
/* - */ |
/* "-" */ |
1008 |
extern void sx_2d(environment *env) { |
extern void sx_2d(environment *env) { |
1009 |
int a, b; |
int a, b; |
1010 |
|
|
1021 |
return; |
return; |
1022 |
} |
} |
1023 |
a=env->head->item->content.val; |
a=env->head->item->content.val; |
1024 |
toss(env); |
toss(env); if(env->err) return; |
1025 |
if(env->err) return; |
b=env->head->item->content.val; |
1026 |
if(env->head->item->refcount == 1) |
toss(env); if(env->err) return; |
1027 |
env->head->item->content.val -= a; |
push_int(env, b-a); |
|
else { |
|
|
b=env->head->item->content.val; |
|
|
toss(env); |
|
|
if(env->err) return; |
|
|
push_int(env, b-a); |
|
|
} |
|
1028 |
} |
} |
1029 |
|
|
1030 |
/* > */ |
/* ">" */ |
1031 |
extern void sx_3e(environment *env) { |
extern void sx_3e(environment *env) { |
1032 |
int a, b; |
int a, b; |
1033 |
|
|
1044 |
return; |
return; |
1045 |
} |
} |
1046 |
a=env->head->item->content.val; |
a=env->head->item->content.val; |
1047 |
toss(env); |
toss(env); if(env->err) return; |
1048 |
if(env->err) return; |
b=env->head->item->content.val; |
1049 |
if(env->head->item->refcount == 1) |
toss(env); if(env->err) return; |
1050 |
env->head->item->content.val = (env->head->item->content.val > a); |
push_int(env, b>a); |
|
else { |
|
|
b=env->head->item->content.val; |
|
|
toss(env); |
|
|
if(env->err) return; |
|
|
push_int(env, b>a); |
|
|
} |
|
1051 |
} |
} |
1052 |
|
|
1053 |
/* Return copy of a value */ |
/* Return copy of a value */ |
1054 |
value *copy_val(value *old_value){ |
value *copy_val(environment *env, value *old_value){ |
1055 |
stackitem *old_item, *new_item, *prev_item; |
stackitem *old_item, *new_item, *prev_item; |
1056 |
|
|
1057 |
value *new_value=malloc(sizeof(value)); |
value *new_value=new_val(env); |
1058 |
|
|
1059 |
new_value->type=old_value->type; |
new_value->type=old_value->type; |
1060 |
new_value->refcount=0; /* This is increased if/when this |
|
|
value is referenced somewhere, like |
|
|
in a stack item or a variable */ |
|
1061 |
switch(old_value->type){ |
switch(old_value->type){ |
1062 |
case integer: |
case integer: |
1063 |
new_value->content.val=old_value->content.val; |
new_value->content.val=old_value->content.val; |
1078 |
|
|
1079 |
while(old_item != NULL) { /* While list is not empty */ |
while(old_item != NULL) { /* While list is not empty */ |
1080 |
new_item= malloc(sizeof(stackitem)); |
new_item= malloc(sizeof(stackitem)); |
1081 |
new_item->item=copy_val(old_item->item); /* recurse */ |
new_item->item=copy_val(env, old_item->item); /* recurse */ |
1082 |
new_item->next=NULL; |
new_item->next=NULL; |
1083 |
if(prev_item != NULL) /* If this wasn't the first item */ |
if(prev_item != NULL) /* If this wasn't the first item */ |
1084 |
prev_item->next=new_item; /* point the previous item to the |
prev_item->next=new_item; /* point the previous item to the |
1093 |
return new_value; |
return new_value; |
1094 |
} |
} |
1095 |
|
|
1096 |
/* duplicates an item on the stack */ |
/* "dup"; duplicates an item on the stack */ |
1097 |
extern void dup(environment *env) { |
extern void sx_647570(environment *env) { |
1098 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
1099 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1100 |
env->err=1; |
env->err=1; |
1101 |
return; |
return; |
1102 |
} |
} |
1103 |
push_val(env, copy_val(env->head->item)); |
push_val(env, copy_val(env, env->head->item)); |
1104 |
} |
} |
1105 |
|
|
1106 |
/* "if", If-Then */ |
/* "if", If-Then */ |
1170 |
eval(env); |
eval(env); |
1171 |
} |
} |
1172 |
|
|
1173 |
/* while */ |
/* "while" */ |
1174 |
extern void sx_7768696c65(environment *env) { |
extern void sx_7768696c65(environment *env) { |
1175 |
|
|
1176 |
int truth; |
int truth; |
1183 |
} |
} |
1184 |
|
|
1185 |
loop= env->head->item; |
loop= env->head->item; |
|
loop->refcount++; |
|
1186 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1187 |
|
|
1188 |
test= env->head->item; |
test= env->head->item; |
|
test->refcount++; |
|
1189 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1190 |
|
|
1191 |
do { |
do { |
1209 |
} |
} |
1210 |
|
|
1211 |
} while(truth); |
} while(truth); |
|
|
|
|
free_val(test); |
|
|
free_val(loop); |
|
1212 |
} |
} |
1213 |
|
|
1214 |
/* For-loop */ |
/* "for"; For-loop */ |
1215 |
extern void sx_666f72(environment *env) { |
extern void sx_666f72(environment *env) { |
1216 |
|
|
1217 |
value *loop, *foo; |
value *loop, *foo; |
1230 |
} |
} |
1231 |
|
|
1232 |
loop= env->head->item; |
loop= env->head->item; |
|
loop->refcount++; |
|
1233 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1234 |
|
|
1235 |
foo= env->head->item; |
foo= env->head->item; |
|
foo->refcount++; |
|
1236 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1237 |
|
|
1238 |
iterator= foo->content.ptr; |
iterator= foo->content.ptr; |
1243 |
eval(env); if(env->err) return; |
eval(env); if(env->err) return; |
1244 |
iterator= iterator->next; |
iterator= iterator->next; |
1245 |
} |
} |
|
|
|
|
free_val(loop); |
|
|
free_val(foo); |
|
1246 |
} |
} |
1247 |
|
|
1248 |
/* 'to' */ |
/* "to" */ |
1249 |
extern void to(environment *env) { |
extern void to(environment *env) { |
1250 |
int i, start, ending; |
int i, start, ending; |
1251 |
stackitem *temp_head; |
stackitem *temp_head; |
1280 |
push_int(env, i); |
push_int(env, i); |
1281 |
} |
} |
1282 |
|
|
1283 |
temp_val= malloc(sizeof(value)); |
temp_val= new_val(env); |
1284 |
temp_val->content.ptr= env->head; |
temp_val->content.ptr= env->head; |
|
temp_val->refcount= 1; |
|
1285 |
temp_val->type= list; |
temp_val->type= list; |
1286 |
env->head= temp_head; |
env->head= temp_head; |
1287 |
push_val(env, temp_val); |
push_val(env, temp_val); |
1291 |
extern void readline(environment *env) { |
extern void readline(environment *env) { |
1292 |
char in_string[101]; |
char in_string[101]; |
1293 |
|
|
1294 |
fgets(in_string, 100, stdin); |
if(fgets(in_string, 100, env->inputstream)==NULL) |
1295 |
push_cstring(env, in_string); |
push_cstring(env, ""); |
1296 |
|
else |
1297 |
|
push_cstring(env, in_string); |
1298 |
} |
} |
1299 |
|
|
1300 |
/* Read a value and place on stack */ |
/* "read"; Read a value and place on stack */ |
1301 |
extern void read(environment *env) { |
extern void sx_72656164(environment *env) { |
1302 |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%[\001-\377]"; |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
1303 |
const char strform[]= "\"%[^\"]\"%[\001-\377]"; |
const char strform[]= "\"%[^\"]\"%n"; |
1304 |
const char intform[]= "%i%[\001-\377]"; |
const char intform[]= "%i%n"; |
1305 |
const char blankform[]= "%*[ \t]%[\001-\377]"; |
const char blankform[]= "%*[ \t]%n"; |
1306 |
const char ebrackform[]= "%*1[]]%[\001-\377]"; |
const char ebrackform[]= "%*1[]]%n"; |
1307 |
const char semicform[]= "%*1[;]%[\001-\377]"; |
const char semicform[]= "%*1[;]%n"; |
1308 |
const char bbrackform[]= "%*1[[]%[\001-\377]"; |
const char bbrackform[]= "%*1[[]%n"; |
1309 |
|
|
1310 |
int itemp; |
int itemp, readlength= -1; |
1311 |
static int depth= 0; |
static int depth= 0; |
1312 |
char *rest, *match; |
char *match; |
1313 |
size_t inlength; |
size_t inlength; |
1314 |
|
|
1315 |
if(env->in_string==NULL) { |
if(env->in_string==NULL) { |
1316 |
|
if(depth > 0 && env->interactive) { |
1317 |
|
printf("]> "); |
1318 |
|
} |
1319 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
1320 |
|
|
1321 |
|
if(((char *)(env->head->item->content.ptr))[0]=='\0'){ |
1322 |
|
env->err= 4; /* "" means EOF */ |
1323 |
|
return; |
1324 |
|
} |
1325 |
|
|
1326 |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
1327 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
1328 |
strcpy(env->in_string, env->head->item->content.ptr); |
strcpy(env->in_string, env->head->item->content.ptr); |
1329 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1330 |
} |
} |
1331 |
|
|
1332 |
inlength= strlen(env->in_string)+1; |
inlength= strlen(env->in_string)+1; |
1333 |
match= malloc(inlength); |
match= malloc(inlength); |
|
rest= malloc(inlength); |
|
1334 |
|
|
1335 |
if(sscanf(env->in_string, blankform, rest)) { |
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
1336 |
|
&& readlength != -1) { |
1337 |
; |
; |
1338 |
} else if(sscanf(env->in_string, intform, &itemp, rest) > 0) { |
} else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF |
1339 |
|
&& readlength != -1) { |
1340 |
push_int(env, itemp); |
push_int(env, itemp); |
1341 |
} else if(sscanf(env->in_string, strform, match, rest) > 0) { |
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
1342 |
|
&& readlength != -1) { |
1343 |
push_cstring(env, match); |
push_cstring(env, match); |
1344 |
} else if(sscanf(env->in_string, symbform, match, rest) > 0) { |
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
1345 |
|
&& readlength != -1) { |
1346 |
push_sym(env, match); |
push_sym(env, match); |
1347 |
} else if(sscanf(env->in_string, ebrackform, rest) > 0) { |
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
1348 |
|
&& readlength != -1) { |
1349 |
pack(env); if(env->err) return; |
pack(env); if(env->err) return; |
1350 |
if(depth!=0) depth--; |
if(depth != 0) depth--; |
1351 |
} else if(sscanf(env->in_string, semicform, rest) > 0) { |
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
1352 |
|
&& readlength != -1) { |
1353 |
push_sym(env, ";"); |
push_sym(env, ";"); |
1354 |
} else if(sscanf(env->in_string, bbrackform, rest) > 0) { |
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
1355 |
|
&& readlength != -1) { |
1356 |
push_sym(env, "["); |
push_sym(env, "["); |
1357 |
depth++; |
depth++; |
1358 |
} else { |
} else { |
1359 |
free(rest); |
free(env->free_string); |
1360 |
rest= NULL; |
env->in_string = env->free_string = NULL; |
1361 |
|
} |
1362 |
|
if ( env->in_string != NULL) { |
1363 |
|
env->in_string += readlength; |
1364 |
} |
} |
|
|
|
|
free(env->in_string); |
|
|
free(match); |
|
1365 |
|
|
1366 |
env->in_string= rest; |
free(match); |
1367 |
|
|
1368 |
if(depth) |
if(depth) |
1369 |
return read(env); |
return sx_72656164(env); |
1370 |
} |
} |