| 9 |
/* strcmp, strcpy, strlen, strcat, strdup */ |
/* strcmp, strcpy, strlen, strcat, strdup */ |
| 10 |
#include <string.h> |
#include <string.h> |
| 11 |
|
|
| 12 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 2048 |
| 13 |
|
|
| 14 |
/* First, define some types. */ |
/* First, define some types. */ |
| 15 |
|
|
| 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 */ |
| 61 |
int non_eval_flag; |
char *in_string; /* Input pending to be read */ |
| 62 |
|
char *free_string; /* Free this string when all input is |
| 63 |
|
read from in_string */ |
| 64 |
} environment; |
} environment; |
| 65 |
|
|
| 66 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
| 72 |
{ |
{ |
| 73 |
int i; |
int i; |
| 74 |
|
|
| 75 |
|
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 |
|
|
| 160 |
} |
} |
| 161 |
} |
} |
| 162 |
|
|
|
/* Generic push function. */ |
|
|
void push(stackitem** stack_head, stackitem* in_item) |
|
|
{ |
|
|
in_item->next= *stack_head; |
|
|
*stack_head= in_item; |
|
|
} |
|
|
|
|
| 163 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
| 164 |
void push_val(stackitem **stack_head, value *val) |
void push_val(environment *env, value *val) |
| 165 |
{ |
{ |
| 166 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
| 167 |
new_item->item= val; |
new_item->item= val; |
| 168 |
val->refcount++; |
val->refcount++; |
| 169 |
push(stack_head, new_item); |
new_item->next= env->head; |
| 170 |
|
env->head= new_item; |
| 171 |
} |
} |
| 172 |
|
|
| 173 |
/* Push an integer onto the stack. */ |
/* Push an integer onto the stack. */ |
| 174 |
void push_int(stackitem **stack_head, int in_val) |
void push_int(environment *env, int in_val) |
| 175 |
{ |
{ |
| 176 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item= new_value; |
|
| 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(stack_head, new_item); |
push_val(env, new_value); |
| 183 |
} |
} |
| 184 |
|
|
| 185 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
| 186 |
void push_cstring(stackitem **stack_head, const char *in_string) |
void push_cstring(environment *env, const char *in_string) |
| 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.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(stack_head, new_item); |
push_val(env, new_value); |
| 196 |
} |
} |
| 197 |
|
|
| 198 |
/* Mangle a symbol name to a valid C identifier name */ |
/* Mangle a symbol name to a valid C identifier name */ |
| 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->head), new_value); |
|
| 239 |
} |
} |
| 240 |
|
|
| 241 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
| 242 |
void push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
| 243 |
{ |
{ |
|
stackitem *new_item; /* The new stack item */ |
|
|
/* ...which will contain... */ |
|
| 244 |
value *new_value; /* A new symbol value */ |
value *new_value; /* A new symbol value */ |
| 245 |
/* ...which might point to... */ |
/* ...which might point to... */ |
| 246 |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
| 253 |
const char *dlerr; /* Dynamic linker error */ |
const char *dlerr; /* Dynamic linker error */ |
| 254 |
char *mangled; /* Mangled function name */ |
char *mangled; /* Mangled function name */ |
| 255 |
|
|
|
/* Create a new stack item containing a new value */ |
|
|
new_item= malloc(sizeof(stackitem)); |
|
| 256 |
new_value= malloc(sizeof(value)); |
new_value= malloc(sizeof(value)); |
|
new_item->item=new_value; |
|
| 257 |
|
|
| 258 |
/* The new value is a symbol */ |
/* The new value is a symbol */ |
| 259 |
new_value->type= symb; |
new_value->type= symb; |
| 297 |
new_fvalue->refcount= 1; |
new_fvalue->refcount= 1; |
| 298 |
} |
} |
| 299 |
} |
} |
| 300 |
push(&(env->head), new_item); |
push_val(env, new_value); |
| 301 |
} |
} |
| 302 |
|
|
| 303 |
/* Print newline. */ |
/* Print newline. */ |
| 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. */ |
| 482 |
} |
} |
| 483 |
toss(env); /* toss the symbol */ |
toss(env); /* toss the symbol */ |
| 484 |
if(env->err) return; |
if(env->err) return; |
| 485 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(env, val); /* Return its bound value */ |
| 486 |
} |
} |
| 487 |
|
|
|
void stack_read(environment*, char*); |
|
|
|
|
| 488 |
/* 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 |
| 489 |
function value, and if it is, toss the symbol and execute the |
function value, and if it is, toss the symbol and execute the |
| 490 |
function. */ |
function. */ |
| 493 |
funcp in_func; |
funcp in_func; |
| 494 |
value* temp_val; |
value* temp_val; |
| 495 |
stackitem* iterator; |
stackitem* iterator; |
| 496 |
char* temp_string; |
|
| 497 |
|
eval_start: |
| 498 |
|
|
| 499 |
if(env->head==NULL) { |
if(env->head==NULL) { |
| 500 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 508 |
rcl(env); /* get its contents */ |
rcl(env); /* get its contents */ |
| 509 |
if(env->err) return; |
if(env->err) return; |
| 510 |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
| 511 |
return eval(env); /* evaluate the value */ |
goto eval_start; |
| 512 |
} |
} |
| 513 |
return; |
return; |
| 514 |
|
|
| 527 |
if(env->err) return; |
if(env->err) return; |
| 528 |
iterator= (stackitem*)temp_val->content.ptr; |
iterator= (stackitem*)temp_val->content.ptr; |
| 529 |
while(iterator!=NULL) { |
while(iterator!=NULL) { |
| 530 |
push_val(&(env->head), iterator->item); |
push_val(env, iterator->item); |
| 531 |
if(env->head->item->type==symb |
if(env->head->item->type==symb |
| 532 |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
| 533 |
toss(env); |
toss(env); |
| 534 |
if(env->err) return; |
if(env->err) return; |
| 535 |
if(iterator->next == NULL){ |
if(iterator->next == NULL){ |
| 536 |
free_val(temp_val); |
free_val(temp_val); |
| 537 |
return eval(env); |
goto eval_start; |
| 538 |
} |
} |
| 539 |
eval(env); |
eval(env); |
| 540 |
if(env->err) return; |
if(env->err) return; |
| 544 |
free_val(temp_val); |
free_val(temp_val); |
| 545 |
return; |
return; |
| 546 |
|
|
| 547 |
/* If it's a string */ |
default: |
|
case string: |
|
|
temp_val= env->head->item; |
|
|
env->head->item->refcount++; |
|
|
toss(env); |
|
|
if(env->err) return; |
|
|
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
|
|
strcpy(temp_string, "[ "); |
|
|
strcpy(temp_string+2, (char*)temp_val->content.ptr); |
|
|
free_val(temp_val); |
|
|
strcat(temp_string, " ]"); |
|
|
stack_read(env, temp_string); |
|
|
free(temp_string); |
|
|
return eval(env); |
|
|
|
|
|
case integer: |
|
| 548 |
return; |
return; |
| 549 |
} |
} |
| 550 |
} |
} |
| 579 |
/* Make a list. */ |
/* Make a list. */ |
| 580 |
extern void pack(environment *env) |
extern void pack(environment *env) |
| 581 |
{ |
{ |
|
void* delimiter; |
|
| 582 |
stackitem *iterator, *temp; |
stackitem *iterator, *temp; |
| 583 |
value *pack; |
value *pack; |
| 584 |
|
|
|
delimiter= env->head->item->content.ptr; /* Get delimiter */ |
|
|
toss(env); |
|
|
|
|
| 585 |
iterator= env->head; |
iterator= env->head; |
| 586 |
|
|
| 587 |
if(iterator==NULL || iterator->item->content.ptr==delimiter) { |
if(iterator==NULL |
| 588 |
|
|| (iterator->item->type==symb |
| 589 |
|
&& ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) { |
| 590 |
temp= NULL; |
temp= NULL; |
| 591 |
toss(env); |
toss(env); |
| 592 |
} else { |
} else { |
| 593 |
/* Search for first delimiter */ |
/* Search for first delimiter */ |
| 594 |
while(iterator->next!=NULL |
while(iterator->next!=NULL |
| 595 |
&& iterator->next->item->content.ptr!=delimiter) |
&& (iterator->next->item->type!=symb |
| 596 |
|
|| ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) |
| 597 |
iterator= iterator->next; |
iterator= iterator->next; |
| 598 |
|
|
| 599 |
/* Extract list */ |
/* Extract list */ |
| 611 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
| 612 |
pack->refcount= 1; |
pack->refcount= 1; |
| 613 |
|
|
| 614 |
temp= malloc(sizeof(stackitem)); |
push_val(env, pack); |
|
temp->item= pack; |
|
|
|
|
|
push(&(env->head), temp); |
|
| 615 |
rev(env); |
rev(env); |
| 616 |
} |
} |
| 617 |
|
|
|
/* Parse input. */ |
|
|
void stack_read(environment *env, char *in_line) |
|
|
{ |
|
|
char *temp, *rest; |
|
|
int itemp; |
|
|
size_t inlength= strlen(in_line)+1; |
|
|
int convert= 0; |
|
|
|
|
|
temp= malloc(inlength); |
|
|
rest= malloc(inlength); |
|
|
|
|
|
do { |
|
|
/* If comment */ |
|
|
if((convert= sscanf(in_line, "#%[^\n\r]", rest))) { |
|
|
free(temp); free(rest); |
|
|
return; |
|
|
} |
|
|
|
|
|
/* 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(!env->non_eval_flag) { |
|
|
eval(env); /* Evaluate top element */ |
|
|
break; |
|
|
} |
|
|
|
|
|
push_sym(env, ";"); |
|
|
break; |
|
|
} |
|
|
|
|
|
if(*temp==']') { |
|
|
push_sym(env, "["); |
|
|
pack(env); |
|
|
if(env->non_eval_flag) |
|
|
env->non_eval_flag--; |
|
|
break; |
|
|
} |
|
|
|
|
|
if(*temp=='[') { |
|
|
push_sym(env, "["); |
|
|
env->non_eval_flag++; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} while(0); |
|
|
|
|
|
free(temp); |
|
|
|
|
|
if(convert<2) { |
|
|
free(rest); |
|
|
return; |
|
|
} |
|
|
|
|
|
stack_read(env, rest); |
|
|
|
|
|
free(rest); |
|
|
} |
|
|
|
|
| 618 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
| 619 |
extern void expand(environment *env) |
extern void expand(environment *env) |
| 620 |
{ |
{ |
| 671 |
result= (left==right); |
result= (left==right); |
| 672 |
|
|
| 673 |
toss(env); toss(env); |
toss(env); toss(env); |
| 674 |
push_int(&(env->head), result); |
push_int(env, result); |
| 675 |
} |
} |
| 676 |
|
|
| 677 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
| 693 |
|
|
| 694 |
val= env->head->item->content.val; |
val= env->head->item->content.val; |
| 695 |
toss(env); |
toss(env); |
| 696 |
push_int(&(env->head), !val); |
push_int(env, !val); |
| 697 |
} |
} |
| 698 |
|
|
| 699 |
/* 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 |
| 736 |
toss(env); toss(env); |
toss(env); toss(env); |
| 737 |
} |
} |
| 738 |
|
|
| 739 |
|
extern void clear(environment *); |
| 740 |
|
void forget_sym(symbol **); |
| 741 |
|
|
| 742 |
/* Quit stack. */ |
/* Quit stack. */ |
| 743 |
extern void quit(environment *env) |
extern void quit(environment *env) |
| 744 |
{ |
{ |
| 745 |
|
long i; |
| 746 |
|
|
| 747 |
|
clear(env); |
| 748 |
|
if (env->err) return; |
| 749 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
| 750 |
|
while(env->symbols[i]!= NULL) { |
| 751 |
|
forget_sym(&(env->symbols[i])); |
| 752 |
|
} |
| 753 |
|
env->symbols[i]= NULL; |
| 754 |
|
} |
| 755 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
| 756 |
} |
} |
| 757 |
|
|
| 777 |
} |
} |
| 778 |
} |
} |
| 779 |
|
|
| 780 |
|
/* Internal forget function */ |
| 781 |
|
void forget_sym(symbol **hash_entry) { |
| 782 |
|
symbol *temp; |
| 783 |
|
|
| 784 |
|
temp= *hash_entry; |
| 785 |
|
*hash_entry= (*hash_entry)->next; |
| 786 |
|
|
| 787 |
|
if(temp->val!=NULL) { |
| 788 |
|
free_val(temp->val); |
| 789 |
|
} |
| 790 |
|
free(temp->id); |
| 791 |
|
free(temp); |
| 792 |
|
} |
| 793 |
|
|
| 794 |
/* Forgets a symbol (remove it from the hash table) */ |
/* Forgets a symbol (remove it from the hash table) */ |
| 795 |
extern void forget(environment *env) |
extern void forget(environment *env) |
| 796 |
{ |
{ |
| 797 |
char* sym_id; |
char* sym_id; |
| 798 |
stackitem *stack_head= env->head; |
stackitem *stack_head= env->head; |
|
symbol **hash_entry, *temp; |
|
| 799 |
|
|
| 800 |
if(stack_head==NULL) { |
if(stack_head==NULL) { |
| 801 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 812 |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
| 813 |
toss(env); |
toss(env); |
| 814 |
|
|
| 815 |
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); |
|
| 816 |
} |
} |
| 817 |
|
|
| 818 |
/* Returns the current error number to the stack */ |
/* Returns the current error number to the stack */ |
| 819 |
extern void errn(environment *env){ |
extern void errn(environment *env){ |
| 820 |
push_int(&(env->head), env->err); |
push_int(env, env->err); |
| 821 |
} |
} |
| 822 |
|
|
| 823 |
|
extern void read(environment*); |
| 824 |
|
|
| 825 |
int main() |
int main() |
| 826 |
{ |
{ |
| 827 |
environment myenv; |
environment myenv; |
|
char in_string[100]; |
|
| 828 |
|
|
| 829 |
init_env(&myenv); |
init_env(&myenv); |
| 830 |
|
|
| 831 |
printf("okidok\n "); |
while(1) { |
| 832 |
|
if(myenv.in_string==NULL) { |
| 833 |
while(fgets(in_string, 100, stdin) != NULL) { |
nl(); |
| 834 |
stack_read(&myenv, in_string); |
printstack(&myenv); |
| 835 |
|
printf("> "); |
| 836 |
|
} |
| 837 |
|
read(&myenv); |
| 838 |
if(myenv.err) { |
if(myenv.err) { |
| 839 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
| 840 |
myenv.err=0; |
myenv.err=0; |
| 841 |
|
} else if(myenv.head!=NULL |
| 842 |
|
&& myenv.head->item->type==symb |
| 843 |
|
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
| 844 |
|
toss(&myenv); /* No error check in main */ |
| 845 |
|
eval(&myenv); |
| 846 |
} |
} |
|
printf("okidok\n "); |
|
| 847 |
} |
} |
| 848 |
quit(&myenv); |
quit(&myenv); |
| 849 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
| 875 |
strcpy(new_string, b_val->content.ptr); |
strcpy(new_string, b_val->content.ptr); |
| 876 |
strcat(new_string, a_val->content.ptr); |
strcat(new_string, a_val->content.ptr); |
| 877 |
free_val(a_val); free_val(b_val); |
free_val(a_val); free_val(b_val); |
| 878 |
push_cstring(&(env->head), new_string); |
push_cstring(env, new_string); |
| 879 |
free(new_string); |
free(new_string); |
| 880 |
return; |
return; |
| 881 |
} |
} |
| 889 |
a=env->head->item->content.val; |
a=env->head->item->content.val; |
| 890 |
toss(env); |
toss(env); |
| 891 |
if(env->err) return; |
if(env->err) return; |
| 892 |
b=env->head->item->content.val; |
if(env->head->item->refcount == 1) |
| 893 |
|
env->head->item->content.val += a; |
| 894 |
|
else { |
| 895 |
|
b=env->head->item->content.val; |
| 896 |
|
toss(env); |
| 897 |
|
if(env->err) return; |
| 898 |
|
push_int(env, a+b); |
| 899 |
|
} |
| 900 |
|
} |
| 901 |
|
|
| 902 |
|
/* - */ |
| 903 |
|
extern void sx_2d(environment *env) { |
| 904 |
|
int a, b; |
| 905 |
|
|
| 906 |
|
if((env->head)==NULL || env->head->next==NULL) { |
| 907 |
|
printerr("Too Few Arguments"); |
| 908 |
|
env->err=1; |
| 909 |
|
return; |
| 910 |
|
} |
| 911 |
|
|
| 912 |
|
if(env->head->item->type!=integer |
| 913 |
|
|| env->head->next->item->type!=integer) { |
| 914 |
|
printerr("Bad Argument Type"); |
| 915 |
|
env->err=2; |
| 916 |
|
return; |
| 917 |
|
} |
| 918 |
|
a=env->head->item->content.val; |
| 919 |
toss(env); |
toss(env); |
| 920 |
if(env->err) return; |
if(env->err) return; |
| 921 |
push_int(&(env->head), a+b); |
if(env->head->item->refcount == 1) |
| 922 |
|
env->head->item->content.val -= a; |
| 923 |
|
else { |
| 924 |
|
b=env->head->item->content.val; |
| 925 |
|
toss(env); |
| 926 |
|
if(env->err) return; |
| 927 |
|
push_int(env, b-a); |
| 928 |
|
} |
| 929 |
|
} |
| 930 |
|
|
| 931 |
|
/* > */ |
| 932 |
|
extern void sx_3e(environment *env) { |
| 933 |
|
int a, b; |
| 934 |
|
|
| 935 |
|
if((env->head)==NULL || env->head->next==NULL) { |
| 936 |
|
printerr("Too Few Arguments"); |
| 937 |
|
env->err=1; |
| 938 |
|
return; |
| 939 |
|
} |
| 940 |
|
|
| 941 |
|
if(env->head->item->type!=integer |
| 942 |
|
|| env->head->next->item->type!=integer) { |
| 943 |
|
printerr("Bad Argument Type"); |
| 944 |
|
env->err=2; |
| 945 |
|
return; |
| 946 |
|
} |
| 947 |
|
a=env->head->item->content.val; |
| 948 |
|
toss(env); |
| 949 |
|
if(env->err) return; |
| 950 |
|
if(env->head->item->refcount == 1) |
| 951 |
|
env->head->item->content.val = (env->head->item->content.val > a); |
| 952 |
|
else { |
| 953 |
|
b=env->head->item->content.val; |
| 954 |
|
toss(env); |
| 955 |
|
if(env->err) return; |
| 956 |
|
push_int(env, b>a); |
| 957 |
|
} |
| 958 |
} |
} |
| 959 |
|
|
| 960 |
/* Return copy of a value */ |
/* Return copy of a value */ |
| 1009 |
env->err=1; |
env->err=1; |
| 1010 |
return; |
return; |
| 1011 |
} |
} |
| 1012 |
push_val(&(env->head), copy_val(env->head->item)); |
push_val(env, copy_val(env->head->item)); |
| 1013 |
} |
} |
| 1014 |
|
|
| 1015 |
/* "if", If-Then */ |
/* "if", If-Then */ |
| 1083 |
extern void sx_7768696c65(environment *env) { |
extern void sx_7768696c65(environment *env) { |
| 1084 |
|
|
| 1085 |
int truth; |
int truth; |
| 1086 |
|
value *loop, *test; |
| 1087 |
|
|
| 1088 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
| 1089 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 1091 |
return; |
return; |
| 1092 |
} |
} |
| 1093 |
|
|
| 1094 |
|
loop= env->head->item; |
| 1095 |
|
loop->refcount++; |
| 1096 |
|
toss(env); if(env->err) return; |
| 1097 |
|
|
| 1098 |
|
test= env->head->item; |
| 1099 |
|
test->refcount++; |
| 1100 |
|
toss(env); if(env->err) return; |
| 1101 |
|
|
| 1102 |
do { |
do { |
| 1103 |
swap(env); if(env->err) return; |
push_val(env, test); |
| 1104 |
dup(env); if(env->err) return; |
eval(env); |
|
eval(env); if(env->err) return; |
|
| 1105 |
|
|
| 1106 |
if(env->head->item->type != integer) { |
if(env->head->item->type != integer) { |
| 1107 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
| 1110 |
} |
} |
| 1111 |
|
|
| 1112 |
truth= env->head->item->content.val; |
truth= env->head->item->content.val; |
|
|
|
| 1113 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
|
swap(env); if(env->err) return; |
|
| 1114 |
|
|
| 1115 |
if(truth) { |
if(truth) { |
| 1116 |
dup(env); |
push_val(env, loop); |
| 1117 |
eval(env); |
eval(env); |
| 1118 |
} else { |
} else { |
| 1119 |
toss(env); |
toss(env); |
|
toss(env); |
|
| 1120 |
} |
} |
| 1121 |
|
|
| 1122 |
} while(truth); |
} while(truth); |
| 1123 |
|
|
| 1124 |
|
free_val(test); |
| 1125 |
|
free_val(loop); |
| 1126 |
|
} |
| 1127 |
|
|
| 1128 |
|
/* For-loop */ |
| 1129 |
|
extern void sx_666f72(environment *env) { |
| 1130 |
|
|
| 1131 |
|
value *loop, *foo; |
| 1132 |
|
stackitem *iterator; |
| 1133 |
|
|
| 1134 |
|
if((env->head)==NULL || env->head->next==NULL) { |
| 1135 |
|
printerr("Too Few Arguments"); |
| 1136 |
|
env->err=1; |
| 1137 |
|
return; |
| 1138 |
|
} |
| 1139 |
|
|
| 1140 |
|
if(env->head->next->item->type != list) { |
| 1141 |
|
printerr("Bad Argument Type"); |
| 1142 |
|
env->err=2; |
| 1143 |
|
return; |
| 1144 |
|
} |
| 1145 |
|
|
| 1146 |
|
loop= env->head->item; |
| 1147 |
|
loop->refcount++; |
| 1148 |
|
toss(env); if(env->err) return; |
| 1149 |
|
|
| 1150 |
|
foo= env->head->item; |
| 1151 |
|
foo->refcount++; |
| 1152 |
|
toss(env); if(env->err) return; |
| 1153 |
|
|
| 1154 |
|
iterator= foo->content.ptr; |
| 1155 |
|
|
| 1156 |
|
while(iterator!=NULL) { |
| 1157 |
|
push_val(env, iterator->item); |
| 1158 |
|
push_val(env, loop); |
| 1159 |
|
eval(env); if(env->err) return; |
| 1160 |
|
iterator= iterator->next; |
| 1161 |
|
} |
| 1162 |
|
|
| 1163 |
|
free_val(loop); |
| 1164 |
|
free_val(foo); |
| 1165 |
|
} |
| 1166 |
|
|
| 1167 |
|
/* 'to' */ |
| 1168 |
|
extern void to(environment *env) { |
| 1169 |
|
int i, start, ending; |
| 1170 |
|
stackitem *temp_head; |
| 1171 |
|
value *temp_val; |
| 1172 |
|
|
| 1173 |
|
if((env->head)==NULL || env->head->next==NULL) { |
| 1174 |
|
printerr("Too Few Arguments"); |
| 1175 |
|
env->err=1; |
| 1176 |
|
return; |
| 1177 |
|
} |
| 1178 |
|
|
| 1179 |
|
if(env->head->item->type!=integer |
| 1180 |
|
|| env->head->next->item->type!=integer) { |
| 1181 |
|
printerr("Bad Argument Type"); |
| 1182 |
|
env->err=2; |
| 1183 |
|
return; |
| 1184 |
|
} |
| 1185 |
|
|
| 1186 |
|
ending= env->head->item->content.val; |
| 1187 |
|
toss(env); if(env->err) return; |
| 1188 |
|
start= env->head->item->content.val; |
| 1189 |
|
toss(env); if(env->err) return; |
| 1190 |
|
|
| 1191 |
|
temp_head= env->head; |
| 1192 |
|
env->head= NULL; |
| 1193 |
|
|
| 1194 |
|
if(ending>=start) { |
| 1195 |
|
for(i= ending; i>=start; i--) |
| 1196 |
|
push_int(env, i); |
| 1197 |
|
} else { |
| 1198 |
|
for(i= ending; i<=start; i++) |
| 1199 |
|
push_int(env, i); |
| 1200 |
|
} |
| 1201 |
|
|
| 1202 |
|
temp_val= malloc(sizeof(value)); |
| 1203 |
|
temp_val->content.ptr= env->head; |
| 1204 |
|
temp_val->refcount= 1; |
| 1205 |
|
temp_val->type= list; |
| 1206 |
|
env->head= temp_head; |
| 1207 |
|
push_val(env, temp_val); |
| 1208 |
|
} |
| 1209 |
|
|
| 1210 |
|
/* Read a string */ |
| 1211 |
|
extern void readline(environment *env) { |
| 1212 |
|
char in_string[101]; |
| 1213 |
|
|
| 1214 |
|
fgets(in_string, 100, stdin); |
| 1215 |
|
push_cstring(env, in_string); |
| 1216 |
|
} |
| 1217 |
|
|
| 1218 |
|
/* Read a value and place on stack */ |
| 1219 |
|
extern void read(environment *env) { |
| 1220 |
|
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
| 1221 |
|
const char strform[]= "\"%[^\"]\"%n"; |
| 1222 |
|
const char intform[]= "%i%n"; |
| 1223 |
|
const char blankform[]= "%*[ \t]%n"; |
| 1224 |
|
const char ebrackform[]= "%*1[]]%n"; |
| 1225 |
|
const char semicform[]= "%*1[;]%n"; |
| 1226 |
|
const char bbrackform[]= "%*1[[]%n"; |
| 1227 |
|
|
| 1228 |
|
int itemp, readlength= -1; |
| 1229 |
|
static int depth= 0; |
| 1230 |
|
char *rest, *match; |
| 1231 |
|
size_t inlength; |
| 1232 |
|
|
| 1233 |
|
if(env->in_string==NULL) { |
| 1234 |
|
if(depth > 0) { |
| 1235 |
|
printf("]> "); |
| 1236 |
|
} |
| 1237 |
|
readline(env); if(env->err) return; |
| 1238 |
|
|
| 1239 |
|
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
| 1240 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
| 1241 |
|
strcpy(env->in_string, env->head->item->content.ptr); |
| 1242 |
|
toss(env); if(env->err) return; |
| 1243 |
|
} |
| 1244 |
|
|
| 1245 |
|
inlength= strlen(env->in_string)+1; |
| 1246 |
|
match= malloc(inlength); |
| 1247 |
|
rest= malloc(inlength); |
| 1248 |
|
|
| 1249 |
|
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
| 1250 |
|
&& readlength != -1) { |
| 1251 |
|
; |
| 1252 |
|
} else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF |
| 1253 |
|
&& readlength != -1) { |
| 1254 |
|
push_int(env, itemp); |
| 1255 |
|
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
| 1256 |
|
&& readlength != -1) { |
| 1257 |
|
push_cstring(env, match); |
| 1258 |
|
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
| 1259 |
|
&& readlength != -1) { |
| 1260 |
|
push_sym(env, match); |
| 1261 |
|
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
| 1262 |
|
&& readlength != -1) { |
| 1263 |
|
pack(env); if(env->err) return; |
| 1264 |
|
if(depth != 0) depth--; |
| 1265 |
|
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
| 1266 |
|
&& readlength != -1) { |
| 1267 |
|
push_sym(env, ";"); |
| 1268 |
|
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
| 1269 |
|
&& readlength != -1) { |
| 1270 |
|
push_sym(env, "["); |
| 1271 |
|
depth++; |
| 1272 |
|
} else { |
| 1273 |
|
free(env->free_string); |
| 1274 |
|
env->in_string = env->free_string = NULL; |
| 1275 |
|
free(match); |
| 1276 |
|
} |
| 1277 |
|
if ( env->in_string != NULL) { |
| 1278 |
|
env->in_string += readlength; |
| 1279 |
|
} |
| 1280 |
|
|
| 1281 |
|
if(depth) |
| 1282 |
|
return read(env); |
| 1283 |
} |
} |