--- stack/stack.c 2002/02/12 22:13:12 1.72 +++ stack/stack.c 2002/02/15 14:44:24 1.86 @@ -1,4 +1,4 @@ -/* printf, sscanf, fgets, fprintf */ +/* printf, sscanf, fgets, fprintf, fopen, perror */ #include /* exit, EXIT_SUCCESS, malloc, free */ #include @@ -8,6 +8,12 @@ #include /* strcmp, strcpy, strlen, strcat, strdup */ #include +/* getopt, STDIN_FILENO, STDOUT_FILENO */ +#include +/* EX_NOINPUT, EX_USAGE */ +#include +/* mtrace, muntrace */ +#include #define HASHTBLSIZE 2048 @@ -58,8 +64,11 @@ stackitem *head; /* Head of the stack */ hashtbl symbols; /* Hash table of all variable bindings */ int err; /* Error flag */ - int non_eval_flag; char *in_string; /* Input pending to be read */ + char *free_string; /* Free this string when all input is + read from in_string */ + FILE *inputstream; /* stdin or a file, most likely */ + int interactive; /* print prompts, stack, etc */ } environment; /* A type for pointers to external functions */ @@ -71,11 +80,14 @@ { int i; - env->in_string= NULL; - env->err= 0; - env->non_eval_flag= 0; + env->head= NULL; for(i= 0; isymbols[i]= NULL; + env->err= 0; + env->in_string= NULL; + env->free_string= NULL; + env->inputstream= stdin; + env->interactive= 1; } void printerr(const char* in_string) { @@ -100,13 +112,18 @@ free(item); /* free the stackitem */ item=temp; /* go to next stackitem */ } - free(val); /* Free the actual list value */ break; case integer: case func: + break; case symb: + free(((symbol*)(val->content.ptr))->id); + if(((symbol*)(val->content.ptr))->val!=NULL) + free_val(((symbol*)(val->content.ptr))->val); + free(val->content.ptr); break; } + free(val); /* Free the actual value structure */ } } @@ -155,49 +172,39 @@ } } -/* Generic push function. */ -void push(environment *env, stackitem* in_item) -{ - in_item->next= env->head; - env->head= in_item; -} - /* Push a value onto the stack */ void push_val(environment *env, value *val) { stackitem *new_item= malloc(sizeof(stackitem)); new_item->item= val; val->refcount++; - push(env, new_item); + new_item->next= env->head; + env->head= new_item; } /* Push an integer onto the stack. */ void push_int(environment *env, int in_val) { value *new_value= malloc(sizeof(value)); - stackitem *new_item= malloc(sizeof(stackitem)); - new_item->item= new_value; new_value->content.val= in_val; new_value->type= integer; - new_value->refcount=1; + new_value->refcount= 0; - push(env, new_item); + push_val(env, new_value); } /* Copy a string onto the stack. */ void push_cstring(environment *env, const char *in_string) { value *new_value= malloc(sizeof(value)); - stackitem *new_item= malloc(sizeof(stackitem)); - new_item->item=new_value; new_value->content.ptr= malloc(strlen(in_string)+1); strcpy(new_value->content.ptr, in_string); new_value->type= string; - new_value->refcount=1; + new_value->refcount= 0; - push(env, new_item); + push_val(env, new_value); } /* Mangle a symbol name to a valid C identifier name */ @@ -221,7 +228,6 @@ } extern void mangle(environment *env){ - value *new_value; char *new_string; if((env->head)==NULL) { @@ -241,19 +247,12 @@ toss(env); if(env->err) return; - new_value= malloc(sizeof(value)); - new_value->content.ptr= new_string; - new_value->type= string; - new_value->refcount=1; - - push_val(env, new_value); + push_cstring(env, new_string); } /* Push a symbol onto the stack. */ void push_sym(environment *env, const char *in_string) { - stackitem *new_item; /* The new stack item */ - /* ...which will contain... */ value *new_value; /* A new symbol value */ /* ...which might point to... */ symbol **new_symbol; /* (if needed) A new actual symbol */ @@ -266,10 +265,7 @@ const char *dlerr; /* Dynamic linker error */ char *mangled; /* Mangled function name */ - /* Create a new stack item containing a new value */ - new_item= malloc(sizeof(stackitem)); new_value= malloc(sizeof(value)); - new_item->item=new_value; /* The new value is a symbol */ new_value->type= symb; @@ -296,12 +292,12 @@ if(handle==NULL) /* If no handle */ handle= dlopen(NULL, RTLD_LAZY); - funcptr= dlsym(handle, in_string); /* Get function pointer */ + mangled=mangle_str(in_string); /* mangle the name */ + funcptr= dlsym(handle, mangled); /* and try to find it */ + free(mangled); dlerr=dlerror(); if(dlerr != NULL) { /* If no function was found */ - mangled=mangle_str(in_string); - funcptr= dlsym(handle, mangled); /* try mangling it */ - free(mangled); + funcptr= dlsym(handle, in_string); /* Get function pointer */ dlerr=dlerror(); } if(dlerr==NULL) { /* If a function was found */ @@ -313,7 +309,7 @@ new_fvalue->refcount= 1; } } - push(env, new_item); + push_val(env, new_value); } /* Print newline. */ @@ -353,14 +349,17 @@ } /* Prints the top element of the stack. */ -void print_h(stackitem *stack_head) +void print_h(stackitem *stack_head, int noquote) { switch(stack_head->item->type) { case integer: printf("%d", stack_head->item->content.val); break; case string: - printf("%s", (char*)stack_head->item->content.ptr); + if(noquote) + printf("%s", (char*)stack_head->item->content.ptr); + else + printf("\"%s\"", (char*)stack_head->item->content.ptr); break; case symb: printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); @@ -373,7 +372,7 @@ stack_head=(stackitem *)(stack_head->item->content.ptr); printf("[ "); while(stack_head != NULL) { - print_h(stack_head); + print_h(stack_head, noquote); printf(" "); stack_head=stack_head->next; } @@ -388,7 +387,8 @@ env->err=1; return; } - print_h(env->head); + print_h(env->head, 0); + nl(); } /* Prints the top element of the stack and then discards it. */ @@ -399,13 +399,30 @@ toss(env); } +extern void princ_(environment *env) { + if(env->head==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + print_h(env->head, 1); +} + +/* Prints the top element of the stack and then discards it. */ +extern void princ(environment *env) +{ + princ_(env); + if(env->err) return; + toss(env); +} + /* Only to be called by function printstack. */ void print_st(stackitem *stack_head, long counter) { if(stack_head->next != NULL) print_st(stack_head->next, counter+1); printf("%ld: ", counter); - print_h(stack_head); + print_h(stack_head, 0); nl(); } @@ -413,10 +430,10 @@ extern void printstack(environment *env) { if(env->head == NULL) { + printf("Stack Empty\n"); return; } print_st(env->head, 1); - nl(); } /* Swap the two top elements on the stack. */ @@ -489,14 +506,14 @@ value* temp_val; stackitem* iterator; + eval_start: + if(env->head==NULL) { printerr("Too Few Arguments"); env->err=1; return; } - eval_start: - switch(env->head->item->type) { /* if it's a symbol */ case symb: @@ -574,22 +591,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 */ @@ -605,12 +621,9 @@ pack= malloc(sizeof(value)); pack->type= list; pack->content.ptr= temp; - pack->refcount= 1; - - temp= malloc(sizeof(stackitem)); - temp->item= pack; + pack->refcount= 0; - push(env, temp); + push_val(env, pack); rev(env); } @@ -735,9 +748,30 @@ toss(env); toss(env); } +extern void clear(environment *); +void forget_sym(symbol **); +extern void words(environment *); + /* Quit stack. */ extern void quit(environment *env) { + long i; + + clear(env); + + if (env->err) return; + for(i= 0; isymbols[i]!= NULL) { + forget_sym(&(env->symbols[i])); + } + env->symbols[i]= NULL; + } + + if(env->free_string!=NULL) + free(env->free_string); + + muntrace(); + exit(EXIT_SUCCESS); } @@ -763,12 +797,25 @@ } } +/* Internal forget function */ +void forget_sym(symbol **hash_entry) { + symbol *temp; + + temp= *hash_entry; + *hash_entry= (*hash_entry)->next; + + if(temp->val!=NULL) { + free_val(temp->val); + } + free(temp->id); + free(temp); +} + /* Forgets a symbol (remove it from the hash table) */ extern void forget(environment *env) { char* sym_id; stackitem *stack_head= env->head; - symbol **hash_entry, *temp; if(stack_head==NULL) { printerr("Too Few Arguments"); @@ -785,15 +832,7 @@ sym_id= ((symbol*)(stack_head->item->content.ptr))->id; toss(env); - hash_entry= 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); + return forget_sym(hash(env->symbols, sym_id)); } /* Returns the current error number to the stack */ @@ -801,21 +840,59 @@ push_int(env, env->err); } -extern void read(environment*); +extern void sx_72656164(environment*); -int main() +int main(int argc, char **argv) { environment myenv; + int c; /* getopt option character */ + + mtrace(); + init_env(&myenv); + myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO); + + while ((c = getopt (argc, argv, "i")) != -1) + switch (c) + { + case 'i': + myenv.interactive = 1; + break; + case '?': + fprintf (stderr, + "Unknown option character `\\x%x'.\n", + optopt); + return EX_USAGE; + default: + abort (); + } + + if (optind < argc) { + myenv.interactive = 0; + myenv.inputstream= fopen(argv[optind], "r"); + if(myenv.inputstream== NULL) { + perror(argv[0]); + exit (EX_NOINPUT); + } + } + while(1) { - if(myenv.in_string==NULL) - printstack(&myenv); - read(&myenv); - if(myenv.err) { - printf("(error %d) ", myenv.err); + if(myenv.in_string==NULL) { + if (myenv.interactive) { + if(myenv.err) { + printf("(error %d)\n", myenv.err); + } + nl(); + printstack(&myenv); + printf("> "); + } myenv.err=0; + } + sx_72656164(&myenv); + if (myenv.err==4) { + return EX_NOINPUT; } else if(myenv.head!=NULL && myenv.head->item->type==symb && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { @@ -827,7 +904,7 @@ return EXIT_FAILURE; } -/* + */ +/* "+" */ extern void sx_2b(environment *env) { int a, b; size_t len; @@ -877,7 +954,7 @@ } } -/* - */ +/* "-" */ extern void sx_2d(environment *env) { int a, b; @@ -906,7 +983,7 @@ } } -/* > */ +/* ">" */ extern void sx_3e(environment *env) { int a, b; @@ -980,8 +1057,8 @@ return new_value; } -/* duplicates an item on the stack */ -extern void dup(environment *env) { +/* "dup"; duplicates an item on the stack */ +extern void sx_647570(environment *env) { if((env->head)==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -1057,7 +1134,7 @@ eval(env); } -/* while */ +/* "while" */ extern void sx_7768696c65(environment *env) { int truth; @@ -1103,7 +1180,7 @@ free_val(loop); } -/* For-loop */ +/* "for"; For-loop */ extern void sx_666f72(environment *env) { value *loop, *foo; @@ -1142,9 +1219,11 @@ free_val(foo); } -/* 'to' */ +/* "to" */ extern void to(environment *env) { int i, start, ending; + stackitem *temp_head; + value *temp_val; if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); @@ -1164,82 +1243,103 @@ start= env->head->item->content.val; toss(env); if(env->err) return; - push_sym(env, "["); + temp_head= env->head; + env->head= NULL; if(ending>=start) { - for(i= start; i<=ending; i++) + for(i= ending; i>=start; i--) push_int(env, i); } else { - for(i= start; i>=ending; i--) + for(i= ending; i<=start; i++) push_int(env, i); } - push_sym(env, "["); - pack(env); if(env->err) return; + temp_val= malloc(sizeof(value)); + temp_val->content.ptr= env->head; + temp_val->refcount= 0; + temp_val->type= list; + env->head= temp_head; + push_val(env, temp_val); } /* Read a string */ extern void readline(environment *env) { char in_string[101]; - fgets(in_string, 100, stdin); - push_cstring(env, in_string); + if(fgets(in_string, 100, env->inputstream)==NULL) + push_cstring(env, ""); + else + push_cstring(env, in_string); } -/* Read a value and place on stack */ -extern void read(environment *env) { - 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]"; +/* "read"; Read a value and place on stack */ +extern void sx_72656164(environment *env) { + const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; + const char strform[]= "\"%[^\"]\"%n"; + const char intform[]= "%i%n"; + const char blankform[]= "%*[ \t]%n"; + const char ebrackform[]= "%*1[]]%n"; + const char semicform[]= "%*1[;]%n"; + const char bbrackform[]= "%*1[[]%n"; - int itemp; + int itemp, readlength= -1; static int depth= 0; - char *rest, *match; + char *match; size_t inlength; if(env->in_string==NULL) { + if(depth > 0 && env->interactive) { + printf("]> "); + } readline(env); if(env->err) return; + + if(((char *)(env->head->item->content.ptr))[0]=='\0'){ + env->err= 4; /* "" means EOF */ + return; + } env->in_string= malloc(strlen(env->head->item->content.ptr)+1); + env->free_string= env->in_string; /* Save the original pointer */ strcpy(env->in_string, env->head->item->content.ptr); toss(env); if(env->err) return; } inlength= strlen(env->in_string)+1; match= malloc(inlength); - rest= malloc(inlength); - if(sscanf(env->in_string, blankform, rest)) { + if(sscanf(env->in_string, blankform, &readlength)!=EOF + && readlength != -1) { ; - } else if(sscanf(env->in_string, intform, &itemp, rest) > 0) { + } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF + && readlength != -1) { push_int(env, itemp); - } else if(sscanf(env->in_string, strform, match, rest) > 0) { + } else if(sscanf(env->in_string, strform, match, &readlength) != EOF + && readlength != -1) { push_cstring(env, match); - } else if(sscanf(env->in_string, symbform, match, rest) > 0) { + } else if(sscanf(env->in_string, symbform, match, &readlength) != EOF + && readlength != -1) { push_sym(env, match); - } else if(sscanf(env->in_string, ebrackform, rest) > 0) { - push_sym(env, "["); + } else if(sscanf(env->in_string, ebrackform, &readlength) != EOF + && readlength != -1) { pack(env); if(env->err) return; - if(depth!=0) depth--; - } else if(sscanf(env->in_string, semicform, rest) > 0) { + if(depth != 0) depth--; + } else if(sscanf(env->in_string, semicform, &readlength) != EOF + && readlength != -1) { push_sym(env, ";"); - } else if(sscanf(env->in_string, bbrackform, rest) > 0) { + } else if(sscanf(env->in_string, bbrackform, &readlength) != EOF + && readlength != -1) { push_sym(env, "["); depth++; } else { - free(rest); - rest= NULL; + free(env->free_string); + env->in_string = env->free_string = NULL; + } + if ( env->in_string != NULL) { + env->in_string += readlength; } - - free(env->in_string); - free(match); - env->in_string= rest; + free(match); if(depth) - return read(env); + return sx_72656164(env); }