1 |
/* printf */ |
/* printf, sscanf, fgets, fprintf */ |
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 |
|
|
12 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 65536 |
13 |
|
|
57 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
58 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
59 |
int err; /* Error flag */ |
int err; /* Error flag */ |
60 |
|
int non_eval_flag; |
61 |
} environment; |
} environment; |
62 |
|
|
63 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
67 |
/* Initialize a newly created environment */ |
/* Initialize a newly created environment */ |
68 |
void init_env(environment *env) |
void init_env(environment *env) |
69 |
{ |
{ |
70 |
long i; |
int i; |
71 |
|
|
72 |
env->err=0; |
env->err= 0; |
73 |
|
env->non_eval_flag= 0; |
74 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
75 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
76 |
} |
} |
77 |
|
|
78 |
|
void printerr(const char* in_string) { |
79 |
|
fprintf(stderr, "Err: %s\n", in_string); |
80 |
|
} |
81 |
|
|
82 |
|
/* Throw away a value */ |
83 |
|
void free_val(value *val){ |
84 |
|
stackitem *item, *temp; |
85 |
|
|
86 |
|
val->refcount--; /* Decrease the reference count */ |
87 |
|
if(val->refcount == 0){ |
88 |
|
switch (val->type){ /* and free the contents if necessary */ |
89 |
|
case string: |
90 |
|
free(val->content.ptr); |
91 |
|
break; |
92 |
|
case list: /* lists needs to be freed recursively */ |
93 |
|
item=val->content.ptr; |
94 |
|
while(item != NULL) { /* for all stack items */ |
95 |
|
free_val(item->item); /* free the value */ |
96 |
|
temp=item->next; /* save next ptr */ |
97 |
|
free(item); /* free the stackitem */ |
98 |
|
item=temp; /* go to next stackitem */ |
99 |
|
} |
100 |
|
free(val); /* Free the actual list value */ |
101 |
|
break; |
102 |
|
default: |
103 |
|
break; |
104 |
|
} |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
|
/* Discard the top element of the stack. */ |
109 |
|
extern void toss(environment *env) |
110 |
|
{ |
111 |
|
stackitem *temp= env->head; |
112 |
|
|
113 |
|
if((env->head)==NULL) { |
114 |
|
printerr("Too Few Arguments"); |
115 |
|
env->err=1; |
116 |
|
return; |
117 |
|
} |
118 |
|
|
119 |
|
free_val(env->head->item); /* Free the value */ |
120 |
|
env->head= env->head->next; /* Remove the top stack item */ |
121 |
|
free(temp); /* Free the old top stack item */ |
122 |
|
} |
123 |
|
|
124 |
/* 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. */ |
125 |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
126 |
{ |
{ |
127 |
long i= 0; |
int i= 0; |
128 |
unsigned long out_hash= 0; |
unsigned int out_hash= 0; |
129 |
char key= '\0'; |
char key= '\0'; |
130 |
symbol **position; |
symbol **position; |
131 |
|
|
195 |
push(stack_head, new_item); |
push(stack_head, new_item); |
196 |
} |
} |
197 |
|
|
198 |
|
/* Mangle a symbol name to a valid C identifier name */ |
199 |
|
char *mangle_str(const char *old_string){ |
200 |
|
char validchars[] |
201 |
|
="0123456789abcdef"; |
202 |
|
char *new_string, *current; |
203 |
|
|
204 |
|
new_string=malloc((strlen(old_string)*2)+4); |
205 |
|
strcpy(new_string, "sx_"); /* Stack eXternal */ |
206 |
|
current=new_string+3; |
207 |
|
while(old_string[0] != '\0'){ |
208 |
|
current[0]=validchars[old_string[0]/16]; |
209 |
|
current[1]=validchars[old_string[0]%16]; |
210 |
|
current+=2; |
211 |
|
old_string++; |
212 |
|
} |
213 |
|
current[0]='\0'; |
214 |
|
|
215 |
|
return new_string; /* The caller must free() it */ |
216 |
|
} |
217 |
|
|
218 |
|
extern void mangle(environment *env){ |
219 |
|
value *new_value; |
220 |
|
char *new_string; |
221 |
|
|
222 |
|
if((env->head)==NULL) { |
223 |
|
printerr("Too Few Arguments"); |
224 |
|
env->err=1; |
225 |
|
return; |
226 |
|
} |
227 |
|
|
228 |
|
if(env->head->item->type!=string) { |
229 |
|
printerr("Bad Argument Type"); |
230 |
|
env->err=2; |
231 |
|
return; |
232 |
|
} |
233 |
|
|
234 |
|
new_string= mangle_str((const char *)(env->head->item->content.ptr)); |
235 |
|
|
236 |
|
toss(env); |
237 |
|
if(env->err) return; |
238 |
|
|
239 |
|
new_value= malloc(sizeof(value)); |
240 |
|
new_value->content.ptr= new_string; |
241 |
|
new_value->type= string; |
242 |
|
new_value->refcount=1; |
243 |
|
|
244 |
|
push_val(&(env->head), new_value); |
245 |
|
} |
246 |
|
|
247 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
248 |
void push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
249 |
{ |
{ |
258 |
void *funcptr; /* A function pointer */ |
void *funcptr; /* A function pointer */ |
259 |
|
|
260 |
static void *handle= NULL; /* Dynamic linker handle */ |
static void *handle= NULL; /* Dynamic linker handle */ |
261 |
|
const char *dlerr; /* Dynamic linker error */ |
262 |
|
char *mangled; /* Mangled function name */ |
263 |
|
|
264 |
/* Create a new stack item containing a new value */ |
/* Create a new stack item containing a new value */ |
265 |
new_item= malloc(sizeof(stackitem)); |
new_item= malloc(sizeof(stackitem)); |
292 |
handle= dlopen(NULL, RTLD_LAZY); |
handle= dlopen(NULL, RTLD_LAZY); |
293 |
|
|
294 |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
295 |
if(dlerror()==NULL) { /* If a function was found */ |
dlerr=dlerror(); |
296 |
|
if(dlerr != NULL) { /* If no function was found */ |
297 |
|
mangled=mangle_str(in_string); |
298 |
|
funcptr= dlsym(handle, mangled); /* try mangling it */ |
299 |
|
free(mangled); |
300 |
|
dlerr=dlerror(); |
301 |
|
} |
302 |
|
if(dlerr==NULL) { /* If a function was found */ |
303 |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
304 |
new_fvalue->type=func; /* The new value is a function pointer */ |
new_fvalue->type=func; /* The new value is a function pointer */ |
305 |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
311 |
push(&(env->head), new_item); |
push(&(env->head), new_item); |
312 |
} |
} |
313 |
|
|
|
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); |
|
|
break; |
|
|
case list: /* lists needs to be freed recursively */ |
|
|
item=val->content.ptr; |
|
|
while(item != NULL) { /* for all stack items */ |
|
|
free_val(item->item); /* free the value */ |
|
|
temp=item->next; /* save next ptr */ |
|
|
free(item); /* free the stackitem */ |
|
|
item=temp; /* go to next stackitem */ |
|
|
} |
|
|
free(val); /* Free the actual list value */ |
|
|
break; |
|
|
default: |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
/* Discard the top element of the stack. */ |
|
|
extern void toss(environment *env) |
|
|
{ |
|
|
stackitem *temp= env->head; |
|
|
|
|
|
if((env->head)==NULL) { |
|
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
|
|
|
free_val(env->head->item); /* Free the value */ |
|
|
env->head= env->head->next; /* Remove the top stack item */ |
|
|
free(temp); /* Free the old top stack item */ |
|
|
} |
|
|
|
|
314 |
/* Print newline. */ |
/* Print newline. */ |
315 |
extern void nl() |
extern void nl() |
316 |
{ |
{ |
361 |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
362 |
break; |
break; |
363 |
case symb: |
case symb: |
364 |
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
365 |
break; |
break; |
366 |
case func: |
case func: |
367 |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
410 |
nl(); |
nl(); |
411 |
} |
} |
412 |
|
|
|
|
|
|
|
|
413 |
/* Prints the stack. */ |
/* Prints the stack. */ |
414 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
415 |
{ |
{ |
425 |
{ |
{ |
426 |
stackitem *temp= env->head; |
stackitem *temp= env->head; |
427 |
|
|
428 |
if((env->head)==NULL) { |
if(env->head==NULL || env->head->next==NULL) { |
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
|
|
|
if(env->head->next==NULL) { |
|
429 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
430 |
env->err=1; |
env->err=1; |
431 |
return; |
return; |
436 |
env->head->next= temp; |
env->head->next= temp; |
437 |
} |
} |
438 |
|
|
|
stackitem* copy(stackitem* in_item) |
|
|
{ |
|
|
stackitem *out_item= malloc(sizeof(stackitem)); |
|
|
|
|
|
memcpy(out_item, in_item, sizeof(stackitem)); |
|
|
out_item->next= NULL; |
|
|
|
|
|
return out_item; |
|
|
} |
|
|
|
|
439 |
/* Recall a value from a symbol, if bound */ |
/* Recall a value from a symbol, if bound */ |
440 |
extern void rcl(environment *env) |
extern void rcl(environment *env) |
441 |
{ |
{ |
464 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(&(env->head), val); /* Return its bound value */ |
465 |
} |
} |
466 |
|
|
467 |
|
void stack_read(environment*, char*); |
468 |
|
|
469 |
/* 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 |
470 |
function value, and if it is, toss the symbol and execute the |
function value, and if it is, toss the symbol and execute the |
471 |
function. */ |
function. */ |
472 |
extern void eval(environment *env) |
extern void eval(environment *env) |
473 |
{ |
{ |
474 |
funcp in_func; |
funcp in_func; |
475 |
|
value* temp_val; |
476 |
|
stackitem* iterator; |
477 |
|
char* temp_string; |
478 |
|
|
479 |
if(env->head==NULL) { |
if(env->head==NULL) { |
480 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
481 |
env->err=1; |
env->err=1; |
482 |
return; |
return; |
483 |
} |
} |
484 |
|
|
485 |
/* if it's a symbol */ |
switch(env->head->item->type) { |
486 |
if(env->head->item->type==symb) { |
/* if it's a symbol */ |
487 |
|
case symb: |
488 |
rcl(env); /* get its contents */ |
rcl(env); /* get its contents */ |
489 |
if(env->err) return; |
if(env->err) return; |
490 |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
491 |
eval(env); /* evaluate the value */ |
eval(env); /* evaluate the value */ |
492 |
return; |
return; |
493 |
} |
} |
494 |
} |
break; |
495 |
|
|
496 |
/* If it's a lone function value, run it */ |
/* If it's a lone function value, run it */ |
497 |
if(env->head->item->type==func) { |
case func: |
498 |
in_func= (funcp)(env->head->item->content.ptr); |
in_func= (funcp)(env->head->item->content.ptr); |
499 |
toss(env); |
toss(env); |
500 |
if(env->err) return; |
if(env->err) return; |
501 |
(*in_func)(env); |
(*in_func)(env); |
502 |
|
break; |
503 |
|
|
504 |
|
/* If it's a list */ |
505 |
|
case list: |
506 |
|
temp_val= env->head->item; |
507 |
|
env->head->item->refcount++; |
508 |
|
toss(env); |
509 |
|
if(env->err) return; |
510 |
|
iterator= (stackitem*)temp_val->content.ptr; |
511 |
|
while(iterator!=NULL && iterator->item!=NULL) { |
512 |
|
push_val(&(env->head), iterator->item); |
513 |
|
if(env->head->item->type==symb |
514 |
|
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
515 |
|
toss(env); |
516 |
|
if(env->err) return; |
517 |
|
eval(env); |
518 |
|
if(env->err) return; |
519 |
|
} |
520 |
|
iterator= iterator->next; |
521 |
|
} |
522 |
|
free_val(temp_val); |
523 |
|
break; |
524 |
|
|
525 |
|
/* If it's a string */ |
526 |
|
case string: |
527 |
|
temp_val= env->head->item; |
528 |
|
env->head->item->refcount++; |
529 |
|
toss(env); |
530 |
|
if(env->err) return; |
531 |
|
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
532 |
|
strcpy(temp_string, "[ "); |
533 |
|
strcat(temp_string, (char*)temp_val->content.ptr); |
534 |
|
strcat(temp_string, " ]"); |
535 |
|
stack_read(env, temp_string); |
536 |
|
eval(env); |
537 |
|
if(env->err) return; |
538 |
|
free_val(temp_val); |
539 |
|
free(temp_string); |
540 |
|
break; |
541 |
|
|
542 |
|
default: |
543 |
|
} |
544 |
|
} |
545 |
|
|
546 |
|
/* Reverse (flip) a list */ |
547 |
|
extern void rev(environment *env){ |
548 |
|
stackitem *old_head, *new_head, *item; |
549 |
|
|
550 |
|
if((env->head)==NULL) { |
551 |
|
printerr("Too Few Arguments"); |
552 |
|
env->err=1; |
553 |
|
return; |
554 |
|
} |
555 |
|
|
556 |
|
if(env->head->item->type!=list) { |
557 |
|
printerr("Bad Argument Type"); |
558 |
|
env->err=2; |
559 |
|
return; |
560 |
|
} |
561 |
|
|
562 |
|
old_head=(stackitem *)(env->head->item->content.ptr); |
563 |
|
new_head=NULL; |
564 |
|
while(old_head != NULL){ |
565 |
|
item=old_head; |
566 |
|
old_head=old_head->next; |
567 |
|
item->next=new_head; |
568 |
|
new_head=item; |
569 |
} |
} |
570 |
|
env->head->item->content.ptr=new_head; |
571 |
} |
} |
572 |
|
|
573 |
/* Make a list. */ |
/* Make a list. */ |
610 |
temp->item= pack; |
temp->item= pack; |
611 |
|
|
612 |
push(&(env->head), temp); |
push(&(env->head), temp); |
613 |
|
rev(env); |
614 |
} |
} |
615 |
|
|
616 |
/* Parse input. */ |
/* Parse input. */ |
620 |
int itemp; |
int itemp; |
621 |
size_t inlength= strlen(in_line)+1; |
size_t inlength= strlen(in_line)+1; |
622 |
int convert= 0; |
int convert= 0; |
|
static int non_eval_flag= 0; |
|
623 |
|
|
624 |
temp= malloc(inlength); |
temp= malloc(inlength); |
625 |
rest= malloc(inlength); |
rest= malloc(inlength); |
626 |
|
|
627 |
do { |
do { |
628 |
|
/* If comment */ |
629 |
|
if((convert= sscanf(in_line, "#%[^\n\r]", rest))) { |
630 |
|
free(temp); free(rest); |
631 |
|
return; |
632 |
|
} |
633 |
|
|
634 |
/* If string */ |
/* If string */ |
635 |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
636 |
push_cstring(&(env->head), temp); |
push_cstring(&(env->head), temp); |
655 |
/* If single char */ |
/* If single char */ |
656 |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
657 |
if(*temp==';') { |
if(*temp==';') { |
658 |
if(!non_eval_flag) { |
if(!env->non_eval_flag) { |
659 |
eval(env); /* Evaluate top element */ |
eval(env); /* Evaluate top element */ |
660 |
break; |
break; |
661 |
} |
} |
667 |
if(*temp==']') { |
if(*temp==']') { |
668 |
push_sym(env, "["); |
push_sym(env, "["); |
669 |
pack(env); |
pack(env); |
670 |
if(non_eval_flag!=0) |
if(env->non_eval_flag) |
671 |
non_eval_flag--; |
env->non_eval_flag--; |
672 |
break; |
break; |
673 |
} |
} |
674 |
|
|
675 |
if(*temp=='[') { |
if(*temp=='[') { |
676 |
push_sym(env, "["); |
push_sym(env, "["); |
677 |
non_eval_flag++; |
env->non_eval_flag++; |
678 |
break; |
break; |
679 |
} |
} |
680 |
} |
} |
709 |
return; |
return; |
710 |
} |
} |
711 |
|
|
712 |
|
rev(env); |
713 |
|
|
714 |
|
if(env->err) |
715 |
|
return; |
716 |
|
|
717 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
718 |
new_head= temp= env->head->item->content.ptr; |
new_head= temp= env->head->item->content.ptr; |
719 |
|
|
730 |
|
|
731 |
} |
} |
732 |
|
|
|
/* Reverse a list */ |
|
|
extern void rev(environment *env){ |
|
|
stackitem *old_head, *new_head, *item; |
|
|
|
|
|
if((env->head)==NULL) { |
|
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
|
|
|
if(env->head->item->type!=list) { |
|
|
printerr("Bad Argument Type"); |
|
|
env->err=2; |
|
|
return; |
|
|
} |
|
|
|
|
|
old_head=(stackitem *)(env->head->item->content.ptr); |
|
|
new_head=NULL; |
|
|
while(old_head != NULL){ |
|
|
item=old_head; |
|
|
old_head=old_head->next; |
|
|
item->next=new_head; |
|
|
new_head=item; |
|
|
} |
|
|
env->head->item->content.ptr=new_head; |
|
|
} |
|
|
|
|
733 |
/* Compares two elements by reference. */ |
/* Compares two elements by reference. */ |
734 |
extern void eq(environment *env) |
extern void eq(environment *env) |
735 |
{ |
{ |
896 |
} |
} |
897 |
printf("okidok\n "); |
printf("okidok\n "); |
898 |
} |
} |
899 |
|
quit(&myenv); |
900 |
|
return EXIT_FAILURE; |
901 |
|
} |
902 |
|
|
903 |
exit(EXIT_SUCCESS); |
/* + */ |
904 |
|
extern void sx_2b(environment *env) { |
905 |
|
int a, b; |
906 |
|
size_t len; |
907 |
|
char* new_string; |
908 |
|
value *a_val, *b_val; |
909 |
|
|
910 |
|
if((env->head)==NULL || env->head->next==NULL) { |
911 |
|
printerr("Too Few Arguments"); |
912 |
|
env->err=1; |
913 |
|
return; |
914 |
|
} |
915 |
|
|
916 |
|
if(env->head->item->type==string |
917 |
|
&& env->head->next->item->type==string) { |
918 |
|
a_val= env->head->item; |
919 |
|
b_val= env->head->next->item; |
920 |
|
a_val->refcount++; |
921 |
|
b_val->refcount++; |
922 |
|
toss(env); if(env->err) return; |
923 |
|
toss(env); if(env->err) return; |
924 |
|
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
925 |
|
new_string= malloc(len); |
926 |
|
strcpy(new_string, b_val->content.ptr); |
927 |
|
strcat(new_string, a_val->content.ptr); |
928 |
|
free_val(a_val); free_val(b_val); |
929 |
|
push_cstring(&(env->head), new_string); |
930 |
|
free(new_string); |
931 |
|
return; |
932 |
|
} |
933 |
|
|
934 |
|
if(env->head->item->type!=integer |
935 |
|
|| env->head->next->item->type!=integer) { |
936 |
|
printerr("Bad Argument Type"); |
937 |
|
env->err=2; |
938 |
|
return; |
939 |
|
} |
940 |
|
a=env->head->item->content.val; |
941 |
|
toss(env); |
942 |
|
if(env->err) return; |
943 |
|
b=env->head->item->content.val; |
944 |
|
toss(env); |
945 |
|
if(env->err) return; |
946 |
|
push_int(&(env->head), a+b); |
947 |
} |
} |