| 58 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
| 59 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
| 60 |
int err; /* Error flag */ |
int err; /* Error flag */ |
|
int non_eval_flag; |
|
| 61 |
char *in_string; /* Input pending to be read */ |
char *in_string; /* Input pending to be read */ |
| 62 |
char *free_string; /* Free this string when all input is |
char *free_string; /* Free this string when all input is |
| 63 |
read from in_string */ |
read from in_string */ |
| 74 |
|
|
| 75 |
env->in_string= NULL; |
env->in_string= NULL; |
| 76 |
env->err= 0; |
env->err= 0; |
|
env->non_eval_flag= 0; |
|
| 77 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
| 78 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
| 79 |
} |
} |
| 100 |
free(item); /* free the stackitem */ |
free(item); /* free the stackitem */ |
| 101 |
item=temp; /* go to next stackitem */ |
item=temp; /* go to next stackitem */ |
| 102 |
} |
} |
|
free(val); /* Free the actual list value */ |
|
| 103 |
break; |
break; |
| 104 |
case integer: |
case integer: |
| 105 |
case func: |
case func: |
| 106 |
|
break; |
| 107 |
case symb: |
case symb: |
| 108 |
|
free(((symbol*)(val->content.ptr))->id); |
| 109 |
|
if(((symbol*)(val->content.ptr))->val!=NULL) |
| 110 |
|
free_val(((symbol*)(val->content.ptr))->val); |
| 111 |
|
free(val->content.ptr); |
| 112 |
break; |
break; |
| 113 |
} |
} |
| 114 |
|
free(val); /* Free the actual list value */ |
| 115 |
} |
} |
| 116 |
} |
} |
| 117 |
|
|
| 177 |
|
|
| 178 |
new_value->content.val= in_val; |
new_value->content.val= in_val; |
| 179 |
new_value->type= integer; |
new_value->type= integer; |
| 180 |
new_value->refcount=1; |
new_value->refcount= 1; |
| 181 |
|
|
| 182 |
push_val(env, new_value); |
push_val(env, new_value); |
| 183 |
} |
} |
| 190 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
| 191 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
| 192 |
new_value->type= string; |
new_value->type= string; |
| 193 |
new_value->refcount=1; |
new_value->refcount= 1; |
| 194 |
|
|
| 195 |
push_val(env, new_value); |
push_val(env, new_value); |
| 196 |
} |
} |
| 216 |
} |
} |
| 217 |
|
|
| 218 |
extern void mangle(environment *env){ |
extern void mangle(environment *env){ |
|
value *new_value; |
|
| 219 |
char *new_string; |
char *new_string; |
| 220 |
|
|
| 221 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
| 235 |
toss(env); |
toss(env); |
| 236 |
if(env->err) return; |
if(env->err) return; |
| 237 |
|
|
| 238 |
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); |
|
| 239 |
} |
} |
| 240 |
|
|
| 241 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
| 337 |
} |
} |
| 338 |
|
|
| 339 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
| 340 |
void print_h(stackitem *stack_head) |
void print_h(stackitem *stack_head, int noquote) |
| 341 |
{ |
{ |
| 342 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
| 343 |
case integer: |
case integer: |
| 344 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
| 345 |
break; |
break; |
| 346 |
case string: |
case string: |
| 347 |
printf("%s", (char*)stack_head->item->content.ptr); |
if(noquote) |
| 348 |
|
printf("%s", (char*)stack_head->item->content.ptr); |
| 349 |
|
else |
| 350 |
|
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
| 351 |
break; |
break; |
| 352 |
case symb: |
case symb: |
| 353 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
| 360 |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
| 361 |
printf("[ "); |
printf("[ "); |
| 362 |
while(stack_head != NULL) { |
while(stack_head != NULL) { |
| 363 |
print_h(stack_head); |
print_h(stack_head, noquote); |
| 364 |
printf(" "); |
printf(" "); |
| 365 |
stack_head=stack_head->next; |
stack_head=stack_head->next; |
| 366 |
} |
} |
| 375 |
env->err=1; |
env->err=1; |
| 376 |
return; |
return; |
| 377 |
} |
} |
| 378 |
print_h(env->head); |
print_h(env->head, 0); |
| 379 |
|
nl(); |
| 380 |
} |
} |
| 381 |
|
|
| 382 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
| 387 |
toss(env); |
toss(env); |
| 388 |
} |
} |
| 389 |
|
|
| 390 |
|
extern void princ_(environment *env) { |
| 391 |
|
if(env->head==NULL) { |
| 392 |
|
printerr("Too Few Arguments"); |
| 393 |
|
env->err=1; |
| 394 |
|
return; |
| 395 |
|
} |
| 396 |
|
print_h(env->head, 1); |
| 397 |
|
} |
| 398 |
|
|
| 399 |
|
/* Prints the top element of the stack and then discards it. */ |
| 400 |
|
extern void princ(environment *env) |
| 401 |
|
{ |
| 402 |
|
princ_(env); |
| 403 |
|
if(env->err) return; |
| 404 |
|
toss(env); |
| 405 |
|
} |
| 406 |
|
|
| 407 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
| 408 |
void print_st(stackitem *stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
| 409 |
{ |
{ |
| 410 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
| 411 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
| 412 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
| 413 |
print_h(stack_head); |
print_h(stack_head, 0); |
| 414 |
nl(); |
nl(); |
| 415 |
} |
} |
| 416 |
|
|
| 418 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
| 419 |
{ |
{ |
| 420 |
if(env->head == NULL) { |
if(env->head == NULL) { |
| 421 |
|
printf("Stack Empty\n"); |
| 422 |
return; |
return; |
| 423 |
} |
} |
| 424 |
print_st(env->head, 1); |
print_st(env->head, 1); |
|
nl(); |
|
| 425 |
} |
} |
| 426 |
|
|
| 427 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
| 494 |
value* temp_val; |
value* temp_val; |
| 495 |
stackitem* iterator; |
stackitem* iterator; |
| 496 |
|
|
| 497 |
|
eval_start: |
| 498 |
|
|
| 499 |
if(env->head==NULL) { |
if(env->head==NULL) { |
| 500 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 501 |
env->err=1; |
env->err=1; |
| 502 |
return; |
return; |
| 503 |
} |
} |
| 504 |
|
|
|
eval_start: |
|
|
|
|
| 505 |
switch(env->head->item->type) { |
switch(env->head->item->type) { |
| 506 |
/* if it's a symbol */ |
/* if it's a symbol */ |
| 507 |
case symb: |
case symb: |
| 747 |
clear(env); |
clear(env); |
| 748 |
if (env->err) return; |
if (env->err) return; |
| 749 |
for(i= 0; i<HASHTBLSIZE; i++) { |
for(i= 0; i<HASHTBLSIZE; i++) { |
| 750 |
if (env->symbols[i]!= NULL) { |
while(env->symbols[i]!= NULL) { |
| 751 |
forget_sym(&(env->symbols[i])); |
forget_sym(&(env->symbols[i])); |
|
env->symbols[i]= NULL; |
|
| 752 |
} |
} |
| 753 |
|
env->symbols[i]= NULL; |
| 754 |
} |
} |
| 755 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
| 756 |
} |
} |
| 829 |
init_env(&myenv); |
init_env(&myenv); |
| 830 |
|
|
| 831 |
while(1) { |
while(1) { |
| 832 |
if(myenv.in_string==NULL) |
if(myenv.in_string==NULL) { |
| 833 |
|
nl(); |
| 834 |
printstack(&myenv); |
printstack(&myenv); |
| 835 |
|
printf("> "); |
| 836 |
|
} |
| 837 |
read(&myenv); |
read(&myenv); |
| 838 |
if(myenv.err) { |
if(myenv.err) { |
| 839 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
| 1231 |
size_t inlength; |
size_t inlength; |
| 1232 |
|
|
| 1233 |
if(env->in_string==NULL) { |
if(env->in_string==NULL) { |
| 1234 |
|
if(depth > 0) { |
| 1235 |
|
printf("]> "); |
| 1236 |
|
} |
| 1237 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
| 1238 |
|
|
| 1239 |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |