| 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 |
|
|
| 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; |
int non_eval_flag; |
| 62 |
|
char *in_string; /* Input pending to be read */ |
| 63 |
|
char *free_string; /* Free this string when all input is |
| 64 |
|
read from in_string */ |
| 65 |
} environment; |
} environment; |
| 66 |
|
|
| 67 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
| 73 |
{ |
{ |
| 74 |
int i; |
int i; |
| 75 |
|
|
| 76 |
|
env->in_string= NULL; |
| 77 |
env->err= 0; |
env->err= 0; |
| 78 |
env->non_eval_flag= 0; |
env->non_eval_flag= 0; |
| 79 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
| 157 |
} |
} |
| 158 |
} |
} |
| 159 |
|
|
|
/* Generic push function. */ |
|
|
void push(stackitem** stack_head, stackitem* in_item) |
|
|
{ |
|
|
in_item->next= *stack_head; |
|
|
*stack_head= in_item; |
|
|
} |
|
|
|
|
| 160 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
| 161 |
void push_val(stackitem **stack_head, value *val) |
void push_val(environment *env, value *val) |
| 162 |
{ |
{ |
| 163 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
| 164 |
new_item->item= val; |
new_item->item= val; |
| 165 |
val->refcount++; |
val->refcount++; |
| 166 |
push(stack_head, new_item); |
new_item->next= env->head; |
| 167 |
|
env->head= new_item; |
| 168 |
} |
} |
| 169 |
|
|
| 170 |
/* Push an integer onto the stack. */ |
/* Push an integer onto the stack. */ |
| 171 |
void push_int(stackitem **stack_head, int in_val) |
void push_int(environment *env, int in_val) |
| 172 |
{ |
{ |
| 173 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item= new_value; |
|
| 174 |
|
|
| 175 |
new_value->content.val= in_val; |
new_value->content.val= in_val; |
| 176 |
new_value->type= integer; |
new_value->type= integer; |
| 177 |
new_value->refcount=1; |
new_value->refcount=1; |
| 178 |
|
|
| 179 |
push(stack_head, new_item); |
push_val(env, new_value); |
| 180 |
} |
} |
| 181 |
|
|
| 182 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
| 183 |
void push_cstring(stackitem **stack_head, const char *in_string) |
void push_cstring(environment *env, const char *in_string) |
| 184 |
{ |
{ |
| 185 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item=new_value; |
|
| 186 |
|
|
| 187 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
| 188 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
| 189 |
new_value->type= string; |
new_value->type= string; |
| 190 |
new_value->refcount=1; |
new_value->refcount=1; |
| 191 |
|
|
| 192 |
push(stack_head, new_item); |
push_val(env, new_value); |
| 193 |
} |
} |
| 194 |
|
|
| 195 |
/* Mangle a symbol name to a valid C identifier name */ |
/* Mangle a symbol name to a valid C identifier name */ |
| 238 |
new_value->type= string; |
new_value->type= string; |
| 239 |
new_value->refcount=1; |
new_value->refcount=1; |
| 240 |
|
|
| 241 |
push_val(&(env->head), new_value); |
push_val(env, new_value); |
| 242 |
} |
} |
| 243 |
|
|
| 244 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
| 245 |
void push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
| 246 |
{ |
{ |
|
stackitem *new_item; /* The new stack item */ |
|
|
/* ...which will contain... */ |
|
| 247 |
value *new_value; /* A new symbol value */ |
value *new_value; /* A new symbol value */ |
| 248 |
/* ...which might point to... */ |
/* ...which might point to... */ |
| 249 |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
| 256 |
const char *dlerr; /* Dynamic linker error */ |
const char *dlerr; /* Dynamic linker error */ |
| 257 |
char *mangled; /* Mangled function name */ |
char *mangled; /* Mangled function name */ |
| 258 |
|
|
|
/* Create a new stack item containing a new value */ |
|
|
new_item= malloc(sizeof(stackitem)); |
|
| 259 |
new_value= malloc(sizeof(value)); |
new_value= malloc(sizeof(value)); |
|
new_item->item=new_value; |
|
| 260 |
|
|
| 261 |
/* The new value is a symbol */ |
/* The new value is a symbol */ |
| 262 |
new_value->type= symb; |
new_value->type= symb; |
| 300 |
new_fvalue->refcount= 1; |
new_fvalue->refcount= 1; |
| 301 |
} |
} |
| 302 |
} |
} |
| 303 |
push(&(env->head), new_item); |
push_val(env, new_value); |
| 304 |
} |
} |
| 305 |
|
|
| 306 |
/* Print newline. */ |
/* Print newline. */ |
| 464 |
} |
} |
| 465 |
toss(env); /* toss the symbol */ |
toss(env); /* toss the symbol */ |
| 466 |
if(env->err) return; |
if(env->err) return; |
| 467 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(env, val); /* Return its bound value */ |
| 468 |
} |
} |
| 469 |
|
|
|
void stack_read(environment*, char*); |
|
|
|
|
| 470 |
/* 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 |
| 471 |
function value, and if it is, toss the symbol and execute the |
function value, and if it is, toss the symbol and execute the |
| 472 |
function. */ |
function. */ |
| 475 |
funcp in_func; |
funcp in_func; |
| 476 |
value* temp_val; |
value* temp_val; |
| 477 |
stackitem* iterator; |
stackitem* iterator; |
|
char* temp_string; |
|
| 478 |
|
|
| 479 |
if(env->head==NULL) { |
if(env->head==NULL) { |
| 480 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 509 |
if(env->err) return; |
if(env->err) return; |
| 510 |
iterator= (stackitem*)temp_val->content.ptr; |
iterator= (stackitem*)temp_val->content.ptr; |
| 511 |
while(iterator!=NULL) { |
while(iterator!=NULL) { |
| 512 |
push_val(&(env->head), iterator->item); |
push_val(env, iterator->item); |
| 513 |
if(env->head->item->type==symb |
if(env->head->item->type==symb |
| 514 |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
| 515 |
toss(env); |
toss(env); |
| 526 |
free_val(temp_val); |
free_val(temp_val); |
| 527 |
return; |
return; |
| 528 |
|
|
| 529 |
/* 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); |
|
|
goto eval_start; |
|
|
|
|
|
case integer: |
|
| 530 |
return; |
return; |
| 531 |
} |
} |
| 532 |
} |
} |
| 561 |
/* Make a list. */ |
/* Make a list. */ |
| 562 |
extern void pack(environment *env) |
extern void pack(environment *env) |
| 563 |
{ |
{ |
|
void* delimiter; |
|
| 564 |
stackitem *iterator, *temp; |
stackitem *iterator, *temp; |
| 565 |
value *pack; |
value *pack; |
| 566 |
|
|
|
delimiter= env->head->item->content.ptr; /* Get delimiter */ |
|
|
toss(env); |
|
|
|
|
| 567 |
iterator= env->head; |
iterator= env->head; |
| 568 |
|
|
| 569 |
if(iterator==NULL || iterator->item->content.ptr==delimiter) { |
if(iterator==NULL |
| 570 |
|
|| (iterator->item->type==symb |
| 571 |
|
&& ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) { |
| 572 |
temp= NULL; |
temp= NULL; |
| 573 |
toss(env); |
toss(env); |
| 574 |
} else { |
} else { |
| 575 |
/* Search for first delimiter */ |
/* Search for first delimiter */ |
| 576 |
while(iterator->next!=NULL |
while(iterator->next!=NULL |
| 577 |
&& iterator->next->item->content.ptr!=delimiter) |
&& (iterator->next->item->type!=symb |
| 578 |
|
|| ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) |
| 579 |
iterator= iterator->next; |
iterator= iterator->next; |
| 580 |
|
|
| 581 |
/* Extract list */ |
/* Extract list */ |
| 593 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
| 594 |
pack->refcount= 1; |
pack->refcount= 1; |
| 595 |
|
|
| 596 |
temp= malloc(sizeof(stackitem)); |
push_val(env, pack); |
|
temp->item= pack; |
|
|
|
|
|
push(&(env->head), temp); |
|
| 597 |
rev(env); |
rev(env); |
| 598 |
} |
} |
| 599 |
|
|
|
/* 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); |
|
|
} |
|
|
|
|
| 600 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
| 601 |
extern void expand(environment *env) |
extern void expand(environment *env) |
| 602 |
{ |
{ |
| 653 |
result= (left==right); |
result= (left==right); |
| 654 |
|
|
| 655 |
toss(env); toss(env); |
toss(env); toss(env); |
| 656 |
push_int(&(env->head), result); |
push_int(env, result); |
| 657 |
} |
} |
| 658 |
|
|
| 659 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
| 675 |
|
|
| 676 |
val= env->head->item->content.val; |
val= env->head->item->content.val; |
| 677 |
toss(env); |
toss(env); |
| 678 |
push_int(&(env->head), !val); |
push_int(env, !val); |
| 679 |
} |
} |
| 680 |
|
|
| 681 |
/* 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 |
| 718 |
toss(env); toss(env); |
toss(env); toss(env); |
| 719 |
} |
} |
| 720 |
|
|
| 721 |
|
extern void clear(environment *); |
| 722 |
|
void forget_sym(symbol **); |
| 723 |
|
|
| 724 |
/* Quit stack. */ |
/* Quit stack. */ |
| 725 |
extern void quit(environment *env) |
extern void quit(environment *env) |
| 726 |
{ |
{ |
| 727 |
|
long i; |
| 728 |
|
|
| 729 |
|
clear(env); |
| 730 |
|
if (env->err) return; |
| 731 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
| 732 |
|
while(env->symbols[i]!= NULL) { |
| 733 |
|
forget_sym(&(env->symbols[i])); |
| 734 |
|
env->symbols[i]= NULL; |
| 735 |
|
} |
| 736 |
|
} |
| 737 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
| 738 |
} |
} |
| 739 |
|
|
| 759 |
} |
} |
| 760 |
} |
} |
| 761 |
|
|
| 762 |
|
/* Internal forget function */ |
| 763 |
|
void forget_sym(symbol **hash_entry) { |
| 764 |
|
symbol *temp; |
| 765 |
|
|
| 766 |
|
temp= *hash_entry; |
| 767 |
|
*hash_entry= (*hash_entry)->next; |
| 768 |
|
|
| 769 |
|
if(temp->val!=NULL) { |
| 770 |
|
free_val(temp->val); |
| 771 |
|
} |
| 772 |
|
free(temp->id); |
| 773 |
|
free(temp); |
| 774 |
|
} |
| 775 |
|
|
| 776 |
/* Forgets a symbol (remove it from the hash table) */ |
/* Forgets a symbol (remove it from the hash table) */ |
| 777 |
extern void forget(environment *env) |
extern void forget(environment *env) |
| 778 |
{ |
{ |
| 779 |
char* sym_id; |
char* sym_id; |
| 780 |
stackitem *stack_head= env->head; |
stackitem *stack_head= env->head; |
|
symbol **hash_entry, *temp; |
|
| 781 |
|
|
| 782 |
if(stack_head==NULL) { |
if(stack_head==NULL) { |
| 783 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 794 |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
| 795 |
toss(env); |
toss(env); |
| 796 |
|
|
| 797 |
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); |
|
| 798 |
} |
} |
| 799 |
|
|
| 800 |
/* Returns the current error number to the stack */ |
/* Returns the current error number to the stack */ |
| 801 |
extern void errn(environment *env){ |
extern void errn(environment *env){ |
| 802 |
push_int(&(env->head), env->err); |
push_int(env, env->err); |
| 803 |
} |
} |
| 804 |
|
|
| 805 |
extern void read(environment*); |
extern void read(environment*); |
| 811 |
init_env(&myenv); |
init_env(&myenv); |
| 812 |
|
|
| 813 |
while(1) { |
while(1) { |
| 814 |
fprintf(stderr, "okidok\n "); |
if(myenv.in_string==NULL) |
| 815 |
|
printstack(&myenv); |
| 816 |
read(&myenv); |
read(&myenv); |
| 817 |
if(myenv.err) { |
if(myenv.err) { |
| 818 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
| 819 |
myenv.err=0; |
myenv.err=0; |
| 820 |
} else if(myenv.head->item->type==symb |
} else if(myenv.head!=NULL |
| 821 |
|
&& myenv.head->item->type==symb |
| 822 |
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
| 823 |
toss(&myenv); /* No error check in main */ |
toss(&myenv); /* No error check in main */ |
| 824 |
eval(&myenv); |
eval(&myenv); |
| 854 |
strcpy(new_string, b_val->content.ptr); |
strcpy(new_string, b_val->content.ptr); |
| 855 |
strcat(new_string, a_val->content.ptr); |
strcat(new_string, a_val->content.ptr); |
| 856 |
free_val(a_val); free_val(b_val); |
free_val(a_val); free_val(b_val); |
| 857 |
push_cstring(&(env->head), new_string); |
push_cstring(env, new_string); |
| 858 |
free(new_string); |
free(new_string); |
| 859 |
return; |
return; |
| 860 |
} |
} |
| 874 |
b=env->head->item->content.val; |
b=env->head->item->content.val; |
| 875 |
toss(env); |
toss(env); |
| 876 |
if(env->err) return; |
if(env->err) return; |
| 877 |
push_int(&(env->head), a+b); |
push_int(env, a+b); |
| 878 |
} |
} |
| 879 |
} |
} |
| 880 |
|
|
| 903 |
b=env->head->item->content.val; |
b=env->head->item->content.val; |
| 904 |
toss(env); |
toss(env); |
| 905 |
if(env->err) return; |
if(env->err) return; |
| 906 |
push_int(&(env->head), b-a); |
push_int(env, b-a); |
| 907 |
} |
} |
| 908 |
} |
} |
| 909 |
|
|
| 932 |
b=env->head->item->content.val; |
b=env->head->item->content.val; |
| 933 |
toss(env); |
toss(env); |
| 934 |
if(env->err) return; |
if(env->err) return; |
| 935 |
push_int(&(env->head), b>a); |
push_int(env, b>a); |
| 936 |
} |
} |
| 937 |
} |
} |
| 938 |
|
|
| 988 |
env->err=1; |
env->err=1; |
| 989 |
return; |
return; |
| 990 |
} |
} |
| 991 |
push_val(&(env->head), copy_val(env->head->item)); |
push_val(env, copy_val(env->head->item)); |
| 992 |
} |
} |
| 993 |
|
|
| 994 |
/* "if", If-Then */ |
/* "if", If-Then */ |
| 1079 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
| 1080 |
|
|
| 1081 |
do { |
do { |
| 1082 |
push_val(&(env->head), test); |
push_val(env, test); |
| 1083 |
eval(env); |
eval(env); |
| 1084 |
|
|
| 1085 |
if(env->head->item->type != integer) { |
if(env->head->item->type != integer) { |
| 1092 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
| 1093 |
|
|
| 1094 |
if(truth) { |
if(truth) { |
| 1095 |
push_val(&(env->head), loop); |
push_val(env, loop); |
| 1096 |
eval(env); |
eval(env); |
| 1097 |
} else { |
} else { |
| 1098 |
toss(env); |
toss(env); |
| 1133 |
iterator= foo->content.ptr; |
iterator= foo->content.ptr; |
| 1134 |
|
|
| 1135 |
while(iterator!=NULL) { |
while(iterator!=NULL) { |
| 1136 |
push_val(&(env->head), iterator->item); |
push_val(env, iterator->item); |
| 1137 |
push_val(&(env->head), loop); |
push_val(env, loop); |
| 1138 |
eval(env); if(env->err) return; |
eval(env); if(env->err) return; |
| 1139 |
iterator= iterator->next; |
iterator= iterator->next; |
| 1140 |
} |
} |
| 1146 |
/* 'to' */ |
/* 'to' */ |
| 1147 |
extern void to(environment *env) { |
extern void to(environment *env) { |
| 1148 |
int i, start, ending; |
int i, start, ending; |
| 1149 |
|
stackitem *temp_head; |
| 1150 |
|
value *temp_val; |
| 1151 |
|
|
| 1152 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
| 1153 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 1167 |
start= env->head->item->content.val; |
start= env->head->item->content.val; |
| 1168 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
| 1169 |
|
|
| 1170 |
push_sym(env, "["); |
temp_head= env->head; |
| 1171 |
|
env->head= NULL; |
| 1172 |
|
|
| 1173 |
if(ending>=start) { |
if(ending>=start) { |
| 1174 |
for(i= start; i<=ending; i++) |
for(i= ending; i>=start; i--) |
| 1175 |
push_int(&(env->head), i); |
push_int(env, i); |
| 1176 |
} else { |
} else { |
| 1177 |
for(i= start; i>=ending; i--) |
for(i= ending; i<=start; i++) |
| 1178 |
push_int(&(env->head), i); |
push_int(env, i); |
| 1179 |
} |
} |
| 1180 |
|
|
| 1181 |
push_sym(env, "["); |
temp_val= malloc(sizeof(value)); |
| 1182 |
pack(env); if(env->err) return; |
temp_val->content.ptr= env->head; |
| 1183 |
|
temp_val->refcount= 1; |
| 1184 |
|
temp_val->type= list; |
| 1185 |
|
env->head= temp_head; |
| 1186 |
|
push_val(env, temp_val); |
| 1187 |
} |
} |
| 1188 |
|
|
| 1189 |
/* Read a string */ |
/* Read a string */ |
| 1191 |
char in_string[101]; |
char in_string[101]; |
| 1192 |
|
|
| 1193 |
fgets(in_string, 100, stdin); |
fgets(in_string, 100, stdin); |
| 1194 |
push_cstring(&(env->head), in_string); |
push_cstring(env, in_string); |
| 1195 |
} |
} |
| 1196 |
|
|
| 1197 |
/* Read a value and place on stack */ |
/* Read a value and place on stack */ |
| 1198 |
extern void read(environment *env) { |
extern void read(environment *env) { |
| 1199 |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%100c"; |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
| 1200 |
const char strform[]= "\"%[^\"]\"%100c"; |
const char strform[]= "\"%[^\"]\"%n"; |
| 1201 |
const char intform[]= "%i%100c"; |
const char intform[]= "%i%n"; |
| 1202 |
const char blankform[]= "%*[ \t]%100c"; |
const char blankform[]= "%*[ \t]%n"; |
| 1203 |
const char ebrackform[]= "%*1[]]%100c"; |
const char ebrackform[]= "%*1[]]%n"; |
| 1204 |
const char semicform[]= "%*1[;]%100c"; |
const char semicform[]= "%*1[;]%n"; |
| 1205 |
const char bbrackform[]= "%*1[[]%100c"; |
const char bbrackform[]= "%*1[[]%n"; |
| 1206 |
|
|
| 1207 |
int itemp, rerun= 0; |
int itemp, readlength= -1; |
| 1208 |
static int depth= 0; |
static int depth= 0; |
| 1209 |
char *rest, *match; |
char *rest, *match; |
|
static char *in_string= NULL; |
|
| 1210 |
size_t inlength; |
size_t inlength; |
| 1211 |
|
|
| 1212 |
if(in_string==NULL) { |
if(env->in_string==NULL) { |
| 1213 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
| 1214 |
|
|
| 1215 |
in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
| 1216 |
strcpy(in_string, env->head->item->content.ptr); |
env->free_string= env->in_string; /* Save the original pointer */ |
| 1217 |
|
strcpy(env->in_string, env->head->item->content.ptr); |
| 1218 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
| 1219 |
} |
} |
| 1220 |
|
|
| 1221 |
inlength= strlen(in_string)+1; |
inlength= strlen(env->in_string)+1; |
| 1222 |
match= malloc(inlength); |
match= malloc(inlength); |
| 1223 |
rest= malloc(inlength); |
rest= malloc(inlength); |
| 1224 |
|
|
| 1225 |
if(sscanf(in_string, blankform, rest)) { |
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
| 1226 |
rerun= 1; |
&& readlength != -1) { |
| 1227 |
} else if(sscanf(in_string, intform, &itemp, rest) > 0) { |
; |
| 1228 |
push_int(&(env->head), itemp); |
} else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF |
| 1229 |
} else if(sscanf(in_string, strform, match, rest) > 0) { |
&& readlength != -1) { |
| 1230 |
push_cstring(&(env->head), match); |
push_int(env, itemp); |
| 1231 |
} else if(sscanf(in_string, symbform, match, rest) > 0) { |
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
| 1232 |
|
&& readlength != -1) { |
| 1233 |
|
push_cstring(env, match); |
| 1234 |
|
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
| 1235 |
|
&& readlength != -1) { |
| 1236 |
push_sym(env, match); |
push_sym(env, match); |
| 1237 |
} else if(sscanf(in_string, ebrackform, rest) > 0) { |
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
| 1238 |
push_sym(env, "["); |
&& readlength != -1) { |
| 1239 |
pack(env); if(env->err) return; |
pack(env); if(env->err) return; |
| 1240 |
if(depth!=0) depth--; |
if(depth != 0) depth--; |
| 1241 |
} else if(sscanf(in_string, semicform, rest) > 0) { |
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
| 1242 |
|
&& readlength != -1) { |
| 1243 |
push_sym(env, ";"); |
push_sym(env, ";"); |
| 1244 |
} else if(sscanf(in_string, bbrackform, rest) > 0) { |
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
| 1245 |
|
&& readlength != -1) { |
| 1246 |
push_sym(env, "["); |
push_sym(env, "["); |
| 1247 |
depth++; |
depth++; |
| 1248 |
} else { |
} else { |
| 1249 |
free(rest); |
free(env->free_string); |
| 1250 |
rest= NULL; |
env->in_string = env->free_string = NULL; |
| 1251 |
rerun= 1; |
free(match); |
| 1252 |
|
} |
| 1253 |
|
if ( env->in_string != NULL) { |
| 1254 |
|
env->in_string += readlength; |
| 1255 |
} |
} |
|
|
|
|
free(in_string); |
|
|
free(match); |
|
|
|
|
|
in_string= rest; |
|
| 1256 |
|
|
| 1257 |
if(rerun || depth) |
if(depth) |
| 1258 |
return read(env); |
return read(env); |
| 1259 |
} |
} |