--- stack/stack.c 2002/02/08 03:10:30 1.57 +++ stack/stack.c 2002/02/14 19:20:28 1.81 @@ -9,7 +9,7 @@ /* strcmp, strcpy, strlen, strcat, strdup */ #include -#define HASHTBLSIZE 65536 +#define HASHTBLSIZE 2048 /* First, define some types. */ @@ -58,7 +58,9 @@ 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 */ } environment; /* A type for pointers to external functions */ @@ -70,8 +72,8 @@ { int i; + env->in_string= NULL; env->err= 0; - env->non_eval_flag= 0; for(i= 0; isymbols[i]= NULL; } @@ -98,13 +100,15 @@ free(item); /* free the stackitem */ item=temp; /* go to next stackitem */ } - free(val); /* Free the actual list value */ break; case integer: case func: case symb: break; } + if(val->id!=NULL) + free(val->id); + free(val); /* Free the actual list value */ } } @@ -153,49 +157,41 @@ } } -/* Generic push function. */ -void push(stackitem** stack_head, stackitem* in_item) -{ - in_item->next= *stack_head; - *stack_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); + new_item->next= env->head; + env->head= 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)); - new_item->item= new_value; new_value->content.val= in_val; new_value->type= integer; - new_value->refcount=1; + new_value->refcount= 1; + new_value->id= NULL; - push(stack_head, new_item); + push_val(env, new_value); } /* 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)); - 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= 1; + new_value->id= NULL; - push(stack_head, new_item); + push_val(env, new_value); } /* Mangle a symbol name to a valid C identifier name */ @@ -219,7 +215,6 @@ } extern void mangle(environment *env){ - value *new_value; char *new_string; if((env->head)==NULL) { @@ -239,19 +234,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->head), 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 */ @@ -264,10 +252,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; @@ -311,7 +296,7 @@ new_fvalue->refcount= 1; } } - push(&(env->head), new_item); + push_val(env, new_value); } /* Print newline. */ @@ -347,21 +332,21 @@ case list: push_sym(env, "list"); break; - default: - push_sym(env, "unknown"); - break; } } /* 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); @@ -374,15 +359,12 @@ 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; } printf("]"); break; - default: - printf("#", (stack_head->item->content.ptr)); - break; } } @@ -392,7 +374,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. */ @@ -403,13 +386,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(); } @@ -417,10 +417,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. */ @@ -481,11 +481,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. */ @@ -494,7 +492,8 @@ funcp in_func; value* temp_val; stackitem* iterator; - char* temp_string; + + eval_start: if(env->head==NULL) { printerr("Too Few Arguments"); @@ -508,18 +507,16 @@ rcl(env); /* get its contents */ if(env->err) return; if(env->head->item->type!=symb){ /* don't recurse symbols */ - eval(env); /* evaluate the value */ - return; + goto eval_start; } - break; + return; /* If it's a lone function value, run it */ case func: in_func= (funcp)(env->head->item->content.ptr); toss(env); if(env->err) return; - (*in_func)(env); - break; + return (*in_func)(env); /* If it's a list */ case list: @@ -528,39 +525,26 @@ toss(env); if(env->err) return; iterator= (stackitem*)temp_val->content.ptr; - while(iterator!=NULL && iterator->item!=NULL) { - push_val(&(env->head), iterator->item); + while(iterator!=NULL) { + push_val(env, iterator->item); if(env->head->item->type==symb && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { toss(env); if(env->err) return; + if(iterator->next == NULL){ + free_val(temp_val); + goto eval_start; + } eval(env); if(env->err) return; } iterator= iterator->next; } free_val(temp_val); - break; - - /* 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, "[ "); - strcat(temp_string, (char*)temp_val->content.ptr); - strcat(temp_string, " ]"); - stack_read(env, temp_string); - eval(env); - if(env->err) return; - free_val(temp_val); - free(temp_string); - break; + return; - case integer: - break; + default: + return; } } @@ -594,22 +578,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 */ @@ -627,92 +610,10 @@ pack->content.ptr= temp; pack->refcount= 1; - temp= malloc(sizeof(stackitem)); - temp->item= pack; - - push(&(env->head), temp); + push_val(env, pack); 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) { @@ -769,7 +670,7 @@ result= (left==right); toss(env); toss(env); - push_int(&(env->head), result); + push_int(env, result); } /* Negates the top element on the stack. */ @@ -791,7 +692,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 @@ -834,9 +735,22 @@ toss(env); toss(env); } +extern void clear(environment *); +void forget_sym(symbol **); + /* 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; + } exit(EXIT_SUCCESS); } @@ -862,12 +776,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"); @@ -884,38 +811,38 @@ 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 */ 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) { + nl(); + printstack(&myenv); + printf("> "); + } + 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; @@ -947,7 +874,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; } @@ -961,10 +888,72 @@ a=env->head->item->content.val; toss(env); if(env->err) return; - b=env->head->item->content.val; + if(env->head->item->refcount == 1) + env->head->item->content.val += a; + else { + b=env->head->item->content.val; + toss(env); + if(env->err) return; + push_int(env, a+b); + } +} + +/* - */ +extern void sx_2d(environment *env) { + int a, b; + + if((env->head)==NULL || env->head->next==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + + if(env->head->item->type!=integer + || env->head->next->item->type!=integer) { + printerr("Bad Argument Type"); + env->err=2; + return; + } + a=env->head->item->content.val; + toss(env); + if(env->err) return; + if(env->head->item->refcount == 1) + env->head->item->content.val -= a; + else { + b=env->head->item->content.val; + toss(env); + if(env->err) return; + push_int(env, b-a); + } +} + +/* > */ +extern void sx_3e(environment *env) { + int a, b; + + if((env->head)==NULL || env->head->next==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + + if(env->head->item->type!=integer + || env->head->next->item->type!=integer) { + printerr("Bad Argument Type"); + env->err=2; + return; + } + a=env->head->item->content.val; toss(env); if(env->err) return; - push_int(&(env->head), a+b); + if(env->head->item->refcount == 1) + env->head->item->content.val = (env->head->item->content.val > a); + else { + b=env->head->item->content.val; + toss(env); + if(env->err) return; + push_int(env, b>a); + } } /* Return copy of a value */ @@ -1019,10 +1008,10 @@ env->err=1; return; } - push_val(&(env->head), copy_val(env->head->item)); + push_val(env, copy_val(env->head->item)); } -/* If-Then */ +/* "if", If-Then */ extern void sx_6966(environment *env) { int truth; @@ -1088,3 +1077,206 @@ eval(env); } + +/* while */ +extern void sx_7768696c65(environment *env) { + + int truth; + value *loop, *test; + + if((env->head)==NULL || env->head->next==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + + loop= env->head->item; + loop->refcount++; + toss(env); if(env->err) return; + + test= env->head->item; + test->refcount++; + toss(env); if(env->err) return; + + do { + push_val(env, test); + eval(env); + + if(env->head->item->type != integer) { + printerr("Bad Argument Type"); + env->err=2; + return; + } + + truth= env->head->item->content.val; + toss(env); if(env->err) return; + + if(truth) { + push_val(env, loop); + eval(env); + } else { + toss(env); + } + + } while(truth); + + free_val(test); + free_val(loop); +} + +/* For-loop */ +extern void sx_666f72(environment *env) { + + value *loop, *foo; + stackitem *iterator; + + if((env->head)==NULL || env->head->next==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + + if(env->head->next->item->type != list) { + printerr("Bad Argument Type"); + env->err=2; + return; + } + + loop= env->head->item; + loop->refcount++; + toss(env); if(env->err) return; + + foo= env->head->item; + foo->refcount++; + toss(env); if(env->err) return; + + iterator= foo->content.ptr; + + while(iterator!=NULL) { + push_val(env, iterator->item); + push_val(env, loop); + eval(env); if(env->err) return; + iterator= iterator->next; + } + + free_val(loop); + free_val(foo); +} + +/* '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"); + env->err=1; + return; + } + + if(env->head->item->type!=integer + || env->head->next->item->type!=integer) { + printerr("Bad Argument Type"); + env->err=2; + return; + } + + ending= env->head->item->content.val; + toss(env); if(env->err) return; + start= env->head->item->content.val; + toss(env); if(env->err) return; + + temp_head= env->head; + env->head= NULL; + + if(ending>=start) { + for(i= ending; i>=start; i--) + push_int(env, i); + } else { + for(i= ending; i<=start; i++) + push_int(env, i); + } + + temp_val= malloc(sizeof(value)); + temp_val->content.ptr= env->head; + temp_val->refcount= 1; + 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); +} + +/* Read a value and place on stack */ +extern void read(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, readlength= -1; + static int depth= 0; + char *rest, *match; + size_t inlength; + + if(env->in_string==NULL) { + if(depth > 0) { + printf("]> "); + } + readline(env); if(env->err) 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, &readlength)!=EOF + && readlength != -1) { + ; + } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF + && readlength != -1) { + push_int(env, itemp); + } else if(sscanf(env->in_string, strform, match, &readlength) != EOF + && readlength != -1) { + push_cstring(env, match); + } else if(sscanf(env->in_string, symbform, match, &readlength) != EOF + && readlength != -1) { + push_sym(env, match); + } 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, &readlength) != EOF + && readlength != -1) { + push_sym(env, ";"); + } else if(sscanf(env->in_string, bbrackform, &readlength) != EOF + && readlength != -1) { + push_sym(env, "["); + depth++; + } else { + free(env->free_string); + env->in_string = env->free_string = NULL; + free(match); + } + if ( env->in_string != NULL) { + env->in_string += readlength; + } + + if(depth) + return read(env); +}