--- stack/stack.c 2002/02/11 00:27:18 1.68 +++ stack/stack.c 2002/02/12 23:16:56 1.73 @@ -9,7 +9,7 @@ /* strcmp, strcpy, strlen, strcat, strdup */ #include -#define HASHTBLSIZE 65536 +#define HASHTBLSIZE 2048 /* First, define some types. */ @@ -59,6 +59,7 @@ hashtbl symbols; /* Hash table of all variable bindings */ int err; /* Error flag */ int non_eval_flag; + char *in_string; /* Input pending to be read */ } environment; /* A type for pointers to external functions */ @@ -70,6 +71,7 @@ { int i; + env->in_string= NULL; env->err= 0; env->non_eval_flag= 0; for(i= 0; inext= *stack_head; - *stack_head= in_item; + in_item->next= env->head; + env->head= in_item; } /* Push a value onto the stack */ -void push_val(stackitem **stack_head, value *val) +void push_val(environment *env, value *val) { stackitem *new_item= malloc(sizeof(stackitem)); new_item->item= val; val->refcount++; - push(stack_head, new_item); + push(env, new_item); } /* Push an integer onto the stack. */ -void push_int(stackitem **stack_head, int in_val) +void push_int(environment *env, int in_val) { value *new_value= malloc(sizeof(value)); stackitem *new_item= malloc(sizeof(stackitem)); @@ -180,11 +182,11 @@ new_value->type= integer; new_value->refcount=1; - push(stack_head, new_item); + push(env, new_item); } /* Copy a string onto the stack. */ -void push_cstring(stackitem **stack_head, const char *in_string) +void push_cstring(environment *env, const char *in_string) { value *new_value= malloc(sizeof(value)); stackitem *new_item= malloc(sizeof(stackitem)); @@ -195,7 +197,7 @@ new_value->type= string; new_value->refcount=1; - push(stack_head, new_item); + push(env, new_item); } /* Mangle a symbol name to a valid C identifier name */ @@ -244,7 +246,7 @@ new_value->type= string; new_value->refcount=1; - push_val(&(env->head), new_value); + push_val(env, new_value); } /* Push a symbol onto the stack. */ @@ -311,7 +313,7 @@ new_fvalue->refcount= 1; } } - push(&(env->head), new_item); + push(env, new_item); } /* Print newline. */ @@ -475,11 +477,9 @@ } toss(env); /* toss the symbol */ if(env->err) return; - push_val(&(env->head), val); /* Return its bound value */ + push_val(env, val); /* Return its bound value */ } -void stack_read(environment*, char*); - /* If the top element is a symbol, determine if it's bound to a function value, and if it is, toss the symbol and execute the function. */ @@ -488,7 +488,6 @@ funcp in_func; value* temp_val; stackitem* iterator; - char* temp_string; if(env->head==NULL) { printerr("Too Few Arguments"); @@ -523,7 +522,7 @@ if(env->err) return; iterator= (stackitem*)temp_val->content.ptr; while(iterator!=NULL) { - push_val(&(env->head), iterator->item); + push_val(env, iterator->item); if(env->head->item->type==symb && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { toss(env); @@ -540,22 +539,7 @@ free_val(temp_val); return; - /* If it's a string */ - 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: + default: return; } } @@ -590,22 +574,21 @@ /* Make a list. */ extern void pack(environment *env) { - void* delimiter; stackitem *iterator, *temp; value *pack; - delimiter= env->head->item->content.ptr; /* Get delimiter */ - toss(env); - iterator= env->head; - if(iterator==NULL || iterator->item->content.ptr==delimiter) { + if(iterator==NULL + || (iterator->item->type==symb + && ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) { temp= NULL; toss(env); } else { /* Search for first delimiter */ while(iterator->next!=NULL - && iterator->next->item->content.ptr!=delimiter) + && (iterator->next->item->type!=symb + || ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) iterator= iterator->next; /* Extract list */ @@ -626,89 +609,10 @@ temp= malloc(sizeof(stackitem)); temp->item= pack; - push(&(env->head), temp); + push(env, temp); rev(env); } -/* 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); -} - /* Relocate elements of the list on the stack. */ extern void expand(environment *env) { @@ -765,7 +669,7 @@ result= (left==right); toss(env); toss(env); - push_int(&(env->head), result); + push_int(env, result); } /* Negates the top element on the stack. */ @@ -787,7 +691,7 @@ val= env->head->item->content.val; toss(env); - push_int(&(env->head), !val); + push_int(env, !val); } /* Compares the two top elements on the stack and return 0 if they're the @@ -893,25 +797,30 @@ /* Returns the current error number to the stack */ extern void errn(environment *env){ - push_int(&(env->head), env->err); + push_int(env, env->err); } +extern void read(environment*); + int main() { environment myenv; - char in_string[100]; init_env(&myenv); - printf("okidok\n "); - - while(fgets(in_string, 100, stdin) != NULL) { - stack_read(&myenv, in_string); + while(1) { + if(myenv.in_string==NULL) + printstack(&myenv); + read(&myenv); if(myenv.err) { printf("(error %d) ", myenv.err); myenv.err=0; + } else if(myenv.head!=NULL + && myenv.head->item->type==symb + && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { + toss(&myenv); /* No error check in main */ + eval(&myenv); } - printf("okidok\n "); } quit(&myenv); return EXIT_FAILURE; @@ -943,7 +852,7 @@ strcpy(new_string, b_val->content.ptr); strcat(new_string, a_val->content.ptr); free_val(a_val); free_val(b_val); - push_cstring(&(env->head), new_string); + push_cstring(env, new_string); free(new_string); return; } @@ -963,7 +872,7 @@ b=env->head->item->content.val; toss(env); if(env->err) return; - push_int(&(env->head), a+b); + push_int(env, a+b); } } @@ -992,7 +901,7 @@ b=env->head->item->content.val; toss(env); if(env->err) return; - push_int(&(env->head), b-a); + push_int(env, b-a); } } @@ -1021,7 +930,7 @@ b=env->head->item->content.val; toss(env); if(env->err) return; - push_int(&(env->head), b>a); + push_int(env, b>a); } } @@ -1077,7 +986,7 @@ env->err=1; return; } - push_val(&(env->head), copy_val(env->head->item)); + push_val(env, copy_val(env->head->item)); } /* "if", If-Then */ @@ -1168,7 +1077,7 @@ toss(env); if(env->err) return; do { - push_val(&(env->head), test); + push_val(env, test); eval(env); if(env->head->item->type != integer) { @@ -1181,7 +1090,7 @@ toss(env); if(env->err) return; if(truth) { - push_val(&(env->head), loop); + push_val(env, loop); eval(env); } else { toss(env); @@ -1222,8 +1131,8 @@ iterator= foo->content.ptr; while(iterator!=NULL) { - push_val(&(env->head), iterator->item); - push_val(&(env->head), loop); + push_val(env, iterator->item); + push_val(env, loop); eval(env); if(env->err) return; iterator= iterator->next; } @@ -1258,13 +1167,12 @@ if(ending>=start) { for(i= start; i<=ending; i++) - push_int(&(env->head), i); + push_int(env, i); } else { for(i= start; i>=ending; i--) - push_int(&(env->head), i); + push_int(env, i); } - push_sym(env, "["); pack(env); if(env->err) return; } @@ -1273,65 +1181,62 @@ char in_string[101]; fgets(in_string, 100, stdin); - push_cstring(&(env->head), in_string); + push_cstring(env, in_string); } /* Read a value and place on stack */ extern void read(environment *env) { - const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%100c"; - const char strform[]= "\"%[^\"]\"%100c"; - const char intform[]= "%i%100c"; - const char blankform[]= "%*[ \t]%100c"; - const char ebrackform[]= "%*1[]]%100c"; - const char semicform[]= "%*1[;]%100c"; - const char bbrackform[]= "%*1[[]%100c"; + const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%[\001-\377]"; + const char strform[]= "\"%[^\"]\"%[\001-\377]"; + const char intform[]= "%i%[\001-\377]"; + const char blankform[]= "%*[ \t]%[\001-\377]"; + const char ebrackform[]= "%*1[]]%[\001-\377]"; + const char semicform[]= "%*1[;]%[\001-\377]"; + const char bbrackform[]= "%*1[[]%[\001-\377]"; - int itemp, rerun= 0; + int itemp; static int depth= 0; char *rest, *match; - static char *in_string= NULL; size_t inlength; - if(in_string==NULL) { + if(env->in_string==NULL) { readline(env); if(env->err) return; - in_string= malloc(strlen(env->head->item->content.ptr)+1); - strcpy(in_string, env->head->item->content.ptr); + env->in_string= malloc(strlen(env->head->item->content.ptr)+1); + strcpy(env->in_string, env->head->item->content.ptr); toss(env); if(env->err) return; } - inlength= strlen(in_string)+1; + inlength= strlen(env->in_string)+1; match= malloc(inlength); rest= malloc(inlength); - if(sscanf(in_string, blankform, rest)) { - rerun= 1; - } else if(sscanf(in_string, intform, &itemp, rest) > 0) { - push_int(&(env->head), itemp); - } else if(sscanf(in_string, strform, match, rest) > 0) { - push_cstring(&(env->head), match); - } else if(sscanf(in_string, symbform, match, rest) > 0) { + if(sscanf(env->in_string, blankform, rest)) { + ; + } else if(sscanf(env->in_string, intform, &itemp, rest) > 0) { + push_int(env, itemp); + } else if(sscanf(env->in_string, strform, match, rest) > 0) { + push_cstring(env, match); + } else if(sscanf(env->in_string, symbform, match, rest) > 0) { push_sym(env, match); - } else if(sscanf(in_string, ebrackform, rest) > 0) { - push_sym(env, "["); + } else if(sscanf(env->in_string, ebrackform, rest) > 0) { pack(env); if(env->err) return; if(depth!=0) depth--; - } else if(sscanf(in_string, semicform, rest) > 0) { + } else if(sscanf(env->in_string, semicform, rest) > 0) { push_sym(env, ";"); - } else if(sscanf(in_string, bbrackform, rest) > 0) { + } else if(sscanf(env->in_string, bbrackform, rest) > 0) { push_sym(env, "["); depth++; } else { free(rest); rest= NULL; - rerun= 1; } - free(in_string); + free(env->in_string); free(match); - in_string= rest; + env->in_string= rest; - if(rerun || depth) + if(depth) return read(env); }