| 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 |
|
/* mtrace, muntrace */ |
| 12 |
|
#include <mcheck.h> |
| 13 |
|
|
| 14 |
#define HASHTBLSIZE 2048 |
#define HASHTBLSIZE 2048 |
| 15 |
|
|
| 60 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
| 61 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
| 62 |
int err; /* Error flag */ |
int err; /* Error flag */ |
|
int non_eval_flag; |
|
| 63 |
char *in_string; /* Input pending to be read */ |
char *in_string; /* Input pending to be read */ |
| 64 |
char *free_string; /* Free this string when all input is |
char *free_string; /* Free this string when all input is |
| 65 |
read from in_string */ |
read from in_string */ |
| 76 |
|
|
| 77 |
env->in_string= NULL; |
env->in_string= NULL; |
| 78 |
env->err= 0; |
env->err= 0; |
|
env->non_eval_flag= 0; |
|
| 79 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
| 80 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
| 81 |
} |
} |
| 102 |
free(item); /* free the stackitem */ |
free(item); /* free the stackitem */ |
| 103 |
item=temp; /* go to next stackitem */ |
item=temp; /* go to next stackitem */ |
| 104 |
} |
} |
|
free(val); /* Free the actual list value */ |
|
| 105 |
break; |
break; |
| 106 |
case integer: |
case integer: |
| 107 |
case func: |
case func: |
| 108 |
|
break; |
| 109 |
case symb: |
case symb: |
| 110 |
|
free(((symbol*)(val->content.ptr))->id); |
| 111 |
|
if(((symbol*)(val->content.ptr))->val!=NULL) |
| 112 |
|
free_val(((symbol*)(val->content.ptr))->val); |
| 113 |
|
free(val->content.ptr); |
| 114 |
break; |
break; |
| 115 |
} |
} |
| 116 |
|
free(val); /* Free the actual value structure */ |
| 117 |
} |
} |
| 118 |
} |
} |
| 119 |
|
|
| 179 |
|
|
| 180 |
new_value->content.val= in_val; |
new_value->content.val= in_val; |
| 181 |
new_value->type= integer; |
new_value->type= integer; |
| 182 |
new_value->refcount=1; |
new_value->refcount= 0; |
| 183 |
|
|
| 184 |
push_val(env, new_value); |
push_val(env, new_value); |
| 185 |
} |
} |
| 192 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
| 193 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
| 194 |
new_value->type= string; |
new_value->type= string; |
| 195 |
new_value->refcount=1; |
new_value->refcount= 0; |
| 196 |
|
|
| 197 |
push_val(env, new_value); |
push_val(env, new_value); |
| 198 |
} |
} |
| 218 |
} |
} |
| 219 |
|
|
| 220 |
extern void mangle(environment *env){ |
extern void mangle(environment *env){ |
|
value *new_value; |
|
| 221 |
char *new_string; |
char *new_string; |
| 222 |
|
|
| 223 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
| 237 |
toss(env); |
toss(env); |
| 238 |
if(env->err) return; |
if(env->err) return; |
| 239 |
|
|
| 240 |
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); |
|
| 241 |
} |
} |
| 242 |
|
|
| 243 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
| 339 |
} |
} |
| 340 |
|
|
| 341 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
| 342 |
void print_h(stackitem *stack_head) |
void print_h(stackitem *stack_head, int noquote) |
| 343 |
{ |
{ |
| 344 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
| 345 |
case integer: |
case integer: |
| 346 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
| 347 |
break; |
break; |
| 348 |
case string: |
case string: |
| 349 |
printf("%s", (char*)stack_head->item->content.ptr); |
if(noquote) |
| 350 |
|
printf("%s", (char*)stack_head->item->content.ptr); |
| 351 |
|
else |
| 352 |
|
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
| 353 |
break; |
break; |
| 354 |
case symb: |
case symb: |
| 355 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
| 362 |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
| 363 |
printf("[ "); |
printf("[ "); |
| 364 |
while(stack_head != NULL) { |
while(stack_head != NULL) { |
| 365 |
print_h(stack_head); |
print_h(stack_head, noquote); |
| 366 |
printf(" "); |
printf(" "); |
| 367 |
stack_head=stack_head->next; |
stack_head=stack_head->next; |
| 368 |
} |
} |
| 377 |
env->err=1; |
env->err=1; |
| 378 |
return; |
return; |
| 379 |
} |
} |
| 380 |
print_h(env->head); |
print_h(env->head, 0); |
| 381 |
|
nl(); |
| 382 |
} |
} |
| 383 |
|
|
| 384 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
| 389 |
toss(env); |
toss(env); |
| 390 |
} |
} |
| 391 |
|
|
| 392 |
|
extern void princ_(environment *env) { |
| 393 |
|
if(env->head==NULL) { |
| 394 |
|
printerr("Too Few Arguments"); |
| 395 |
|
env->err=1; |
| 396 |
|
return; |
| 397 |
|
} |
| 398 |
|
print_h(env->head, 1); |
| 399 |
|
} |
| 400 |
|
|
| 401 |
|
/* Prints the top element of the stack and then discards it. */ |
| 402 |
|
extern void princ(environment *env) |
| 403 |
|
{ |
| 404 |
|
princ_(env); |
| 405 |
|
if(env->err) return; |
| 406 |
|
toss(env); |
| 407 |
|
} |
| 408 |
|
|
| 409 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
| 410 |
void print_st(stackitem *stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
| 411 |
{ |
{ |
| 412 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
| 413 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
| 414 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
| 415 |
print_h(stack_head); |
print_h(stack_head, 0); |
| 416 |
nl(); |
nl(); |
| 417 |
} |
} |
| 418 |
|
|
| 420 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
| 421 |
{ |
{ |
| 422 |
if(env->head == NULL) { |
if(env->head == NULL) { |
| 423 |
|
printf("Stack Empty\n"); |
| 424 |
return; |
return; |
| 425 |
} |
} |
| 426 |
print_st(env->head, 1); |
print_st(env->head, 1); |
|
nl(); |
|
| 427 |
} |
} |
| 428 |
|
|
| 429 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
| 496 |
value* temp_val; |
value* temp_val; |
| 497 |
stackitem* iterator; |
stackitem* iterator; |
| 498 |
|
|
| 499 |
|
eval_start: |
| 500 |
|
|
| 501 |
if(env->head==NULL) { |
if(env->head==NULL) { |
| 502 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 503 |
env->err=1; |
env->err=1; |
| 504 |
return; |
return; |
| 505 |
} |
} |
| 506 |
|
|
|
eval_start: |
|
|
|
|
| 507 |
switch(env->head->item->type) { |
switch(env->head->item->type) { |
| 508 |
/* if it's a symbol */ |
/* if it's a symbol */ |
| 509 |
case symb: |
case symb: |
| 611 |
pack= malloc(sizeof(value)); |
pack= malloc(sizeof(value)); |
| 612 |
pack->type= list; |
pack->type= list; |
| 613 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
| 614 |
pack->refcount= 1; |
pack->refcount= 0; |
| 615 |
|
|
| 616 |
push_val(env, pack); |
push_val(env, pack); |
| 617 |
rev(env); |
rev(env); |
| 740 |
|
|
| 741 |
extern void clear(environment *); |
extern void clear(environment *); |
| 742 |
void forget_sym(symbol **); |
void forget_sym(symbol **); |
| 743 |
|
extern void words(environment *); |
| 744 |
|
|
| 745 |
/* Quit stack. */ |
/* Quit stack. */ |
| 746 |
extern void quit(environment *env) |
extern void quit(environment *env) |
| 748 |
long i; |
long i; |
| 749 |
|
|
| 750 |
clear(env); |
clear(env); |
| 751 |
|
|
| 752 |
|
words(env); |
| 753 |
|
|
| 754 |
if (env->err) return; |
if (env->err) return; |
| 755 |
for(i= 0; i<HASHTBLSIZE; i++) { |
for(i= 0; i<HASHTBLSIZE; i++) { |
| 756 |
while(env->symbols[i]!= NULL) { |
while(env->symbols[i]!= NULL) { |
| 757 |
forget_sym(&(env->symbols[i])); |
forget_sym(&(env->symbols[i])); |
|
env->symbols[i]= NULL; |
|
| 758 |
} |
} |
| 759 |
|
env->symbols[i]= NULL; |
| 760 |
} |
} |
| 761 |
|
|
| 762 |
|
words(env); |
| 763 |
|
|
| 764 |
|
if(env->free_string!=NULL) |
| 765 |
|
free(env->free_string); |
| 766 |
|
|
| 767 |
|
muntrace(); |
| 768 |
|
|
| 769 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
| 770 |
} |
} |
| 771 |
|
|
| 840 |
{ |
{ |
| 841 |
environment myenv; |
environment myenv; |
| 842 |
|
|
| 843 |
|
mtrace(); |
| 844 |
|
|
| 845 |
init_env(&myenv); |
init_env(&myenv); |
| 846 |
|
|
| 847 |
while(1) { |
while(1) { |
| 848 |
if(myenv.in_string==NULL) |
if(myenv.in_string==NULL) { |
| 849 |
|
nl(); |
| 850 |
printstack(&myenv); |
printstack(&myenv); |
| 851 |
|
printf("> "); |
| 852 |
|
} |
| 853 |
read(&myenv); |
read(&myenv); |
| 854 |
if(myenv.err) { |
if(myenv.err) { |
| 855 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
| 1217 |
|
|
| 1218 |
temp_val= malloc(sizeof(value)); |
temp_val= malloc(sizeof(value)); |
| 1219 |
temp_val->content.ptr= env->head; |
temp_val->content.ptr= env->head; |
| 1220 |
temp_val->refcount= 1; |
temp_val->refcount= 0; |
| 1221 |
temp_val->type= list; |
temp_val->type= list; |
| 1222 |
env->head= temp_head; |
env->head= temp_head; |
| 1223 |
push_val(env, temp_val); |
push_val(env, temp_val); |
| 1243 |
|
|
| 1244 |
int itemp, readlength= -1; |
int itemp, readlength= -1; |
| 1245 |
static int depth= 0; |
static int depth= 0; |
| 1246 |
char *rest, *match; |
char *match; |
| 1247 |
size_t inlength; |
size_t inlength; |
| 1248 |
|
|
| 1249 |
if(env->in_string==NULL) { |
if(env->in_string==NULL) { |
| 1250 |
|
if(depth > 0) { |
| 1251 |
|
printf("]> "); |
| 1252 |
|
} |
| 1253 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
| 1254 |
|
|
| 1255 |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
| 1260 |
|
|
| 1261 |
inlength= strlen(env->in_string)+1; |
inlength= strlen(env->in_string)+1; |
| 1262 |
match= malloc(inlength); |
match= malloc(inlength); |
|
rest= malloc(inlength); |
|
| 1263 |
|
|
| 1264 |
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
| 1265 |
&& readlength != -1) { |
&& readlength != -1) { |
| 1287 |
} else { |
} else { |
| 1288 |
free(env->free_string); |
free(env->free_string); |
| 1289 |
env->in_string = env->free_string = NULL; |
env->in_string = env->free_string = NULL; |
|
free(match); |
|
| 1290 |
} |
} |
| 1291 |
if ( env->in_string != NULL) { |
if ( env->in_string != NULL) { |
| 1292 |
env->in_string += readlength; |
env->in_string += readlength; |
| 1293 |
} |
} |
| 1294 |
|
|
| 1295 |
|
free(match); |
| 1296 |
|
|
| 1297 |
if(depth) |
if(depth) |
| 1298 |
return read(env); |
return read(env); |
| 1299 |
} |
} |