--- stack/stack.c 2002/03/08 06:44:15 1.92 +++ stack/stack.c 2002/03/10 13:00:01 1.101 @@ -48,10 +48,9 @@ { int i; - env->gc_limit= 20; + env->gc_limit= 400000; env->gc_count= 0; env->gc_ref= NULL; - env->gc_protect= NULL; env->head= NULL; for(i= 0; iinteractive= 1; } -void printerr(const char* in_string) { +void printerr(const char* in_string) +{ fprintf(stderr, "Err: %s\n", in_string); } @@ -80,8 +80,6 @@ env->head= env->head->next; /* Remove the top stack item */ free(temp); /* Free the old top stack item */ - - gc_init(env); } /* Returns a pointer to a pointer to an element in the hash table. */ @@ -113,32 +111,35 @@ } } -value* new_val(environment *env) { +/* Create new value */ +value* new_val(environment *env) +{ value *nval= malloc(sizeof(value)); stackitem *nitem= malloc(sizeof(stackitem)); nval->content.ptr= NULL; - protect(env, nval); - - gc_init(env); nitem->item= nval; nitem->next= env->gc_ref; env->gc_ref= nitem; - env->gc_count++; - unprotect(env); + env->gc_count += sizeof(value); + nval->gc.flag.mark= 0; + nval->gc.flag.protect= 0; return nval; } -void gc_mark(value *val) { +/* Mark values recursively. + Marked values are not collected by the GC. */ +inline void gc_mark(value *val) +{ stackitem *iterator; - if(val==NULL || val->gc_garb==0) + if(val->gc.flag.mark) return; - val->gc_garb= 0; + val->gc.flag.mark= 1; if(val->type==list) { iterator= val->content.ptr; @@ -150,92 +151,136 @@ } } -extern void gc_init(environment *env) { - stackitem *new_head= NULL, *titem, *iterator= env->gc_ref; - symbol *tsymb; - int i; - +inline void gc_maybe(environment *env) +{ if(env->gc_count < env->gc_limit) return; + else + return gc_init(env); +} - while(iterator!=NULL) { - iterator->item->gc_garb= 1; - iterator= iterator->next; - } +/* Start GC */ +extern void gc_init(environment *env) +{ + stackitem *new_head= NULL, *titem, *iterator; + symbol *tsymb; + int i; - /* Mark */ - iterator= env->gc_protect; - while(iterator!=NULL) { - gc_mark(iterator->item); - iterator= iterator->next; + if(env->interactive){ + printf("Garbage collecting."); } + /* Mark values on stack */ iterator= env->head; while(iterator!=NULL) { gc_mark(iterator->item); iterator= iterator->next; } + if(env->interactive){ + printf("."); + } + + /* Mark values in hashtable */ for(i= 0; isymbols[i]; while(tsymb!=NULL) { - gc_mark(tsymb->val); + if (tsymb->val != NULL) + gc_mark(tsymb->val); tsymb= tsymb->next; } } + if(env->interactive){ + printf("."); + } + env->gc_count= 0; - /* Sweep */ - while(env->gc_ref!=NULL) { + while(env->gc_ref!=NULL) { /* Sweep unused values */ + + if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */ - if(env->gc_ref->item->gc_garb) { - switch(env->gc_ref->item->type) { + switch(env->gc_ref->item->type) { /* Remove content */ case string: free(env->gc_ref->item->content.ptr); break; - case integer: - break; case list: while(env->gc_ref->item->content.ptr!=NULL) { titem= env->gc_ref->item->content.ptr; env->gc_ref->item->content.ptr= titem->next; free(titem); } - break; default: - break; } - free(env->gc_ref->item); + free(env->gc_ref->item); /* Remove from gc_ref */ titem= env->gc_ref->next; - free(env->gc_ref); + free(env->gc_ref); /* Remove value */ env->gc_ref= titem; + continue; } else { - titem= env->gc_ref->next; - env->gc_ref->next= new_head; - new_head= env->gc_ref; - env->gc_ref= titem; - env->gc_count++; + env->gc_count += sizeof(value); + if(env->gc_ref->item->type == string) + env->gc_count += strlen(env->gc_ref->item->content.ptr); } + + /* Keep values */ + titem= env->gc_ref->next; + env->gc_ref->next= new_head; + new_head= env->gc_ref; + new_head->item->gc.flag.mark= 0; + env->gc_ref= titem; } - env->gc_limit= env->gc_count*2; + if (env->gc_limit < env->gc_count*2) + env->gc_limit= env->gc_count*2; + env->gc_ref= new_head; + + if(env->interactive){ + printf("done\n"); + } + } -void protect(environment *env, value *val) +/* Protect values from GC */ +void protect(value *val) { - stackitem *new_item= malloc(sizeof(stackitem)); - new_item->item= val; - new_item->next= env->gc_protect; - env->gc_protect= new_item; + stackitem *iterator; + + if(val->gc.flag.protect) + return; + + val->gc.flag.protect= 1; + + if(val->type==list) { + iterator= val->content.ptr; + + while(iterator!=NULL) { + protect(iterator->item); + iterator= iterator->next; + } + } } -void unprotect(environment *env) +/* Unprotect values from GC */ +void unprotect(value *val) { - stackitem *temp= env->gc_protect; - env->gc_protect= env->gc_protect->next; - free(temp); + stackitem *iterator; + + if(!(val->gc.flag.protect)) + return; + + val->gc.flag.protect= 0; + + if(val->type==list) { + iterator= val->content.ptr; + + while(iterator!=NULL) { + unprotect(iterator->item); + iterator= iterator->next; + } + } } /* Push a value onto the stack */ @@ -247,23 +292,36 @@ env->head= new_item; } -/* Push an integer onto the stack. */ +/* Push an integer onto the stack */ void push_int(environment *env, int in_val) { value *new_value= new_val(env); - new_value->content.val= in_val; + new_value->content.i= in_val; new_value->type= integer; push_val(env, new_value); } +/* Push a floating point number onto the stack */ +void push_float(environment *env, float in_val) +{ + value *new_value= new_val(env); + + new_value->content.f= in_val; + new_value->type= tfloat; + + push_val(env, new_value); +} + /* Copy a string onto the stack. */ void push_cstring(environment *env, const char *in_string) { value *new_value= new_val(env); + int length= strlen(in_string)+1; - new_value->content.ptr= malloc(strlen(in_string)+1); + new_value->content.ptr= malloc(length); + env->gc_count += length; strcpy(new_value->content.ptr, in_string); new_value->type= string; @@ -271,7 +329,8 @@ } /* Mangle a symbol name to a valid C identifier name */ -char *mangle_str(const char *old_string){ +char *mangle_str(const char *old_string) +{ char validchars[]= "0123456789abcdef"; char *new_string, *current; @@ -289,7 +348,8 @@ return new_string; /* The caller must free() it */ } -extern void mangle(environment *env){ +extern void mangle(environment *env) +{ char *new_string; if((env->head)==NULL) { @@ -328,6 +388,9 @@ char *mangled; /* Mangled function name */ new_value= new_val(env); + protect(new_value); + new_fvalue= new_val(env); + protect(new_fvalue); /* The new value is a symbol */ new_value->type= symb; @@ -355,21 +418,25 @@ 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 */ funcptr= dlsym(handle, in_string); /* Get function pointer */ dlerr= dlerror(); } + if(dlerr==NULL) { /* If a function was found */ - new_fvalue= new_val(env); /* Create a new value */ new_fvalue->type= func; /* The new value is a function pointer */ new_fvalue->content.ptr= funcptr; /* Store function pointer */ (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new function value */ } + + free(mangled); } + push_val(env, new_value); + unprotect(new_value); unprotect(new_fvalue); } /* Print newline. */ @@ -379,7 +446,8 @@ } /* Gets the type of a value */ -extern void type(environment *env){ +extern void type(environment *env) +{ int typenum; if((env->head)==NULL) { @@ -393,6 +461,9 @@ case integer: push_sym(env, "integer"); break; + case tfloat: + push_sym(env, "float"); + break; case string: push_sym(env, "string"); break; @@ -413,7 +484,10 @@ { switch(stack_head->item->type) { case integer: - printf("%d", stack_head->item->content.val); + printf("%d", stack_head->item->content.i); + break; + case tfloat: + printf("%f", stack_head->item->content.f); break; case string: if(noquote) @@ -441,7 +515,8 @@ } } -extern void print_(environment *env) { +extern void print_(environment *env) +{ if(env->head==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -459,7 +534,8 @@ toss(env); } -extern void princ_(environment *env) { +extern void princ_(environment *env) +{ if(env->head==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -493,6 +569,7 @@ printf("Stack Empty\n"); return; } + print_st(env->head, 1); } @@ -552,11 +629,11 @@ env->err=3; return; } - protect(env, val); + protect(val); toss(env); /* toss the symbol */ if(env->err) return; push_val(env, val); /* Return its bound value */ - unprotect(env); + unprotect(val); } /* If the top element is a symbol, determine if it's bound to a @@ -570,6 +647,8 @@ eval_start: + gc_maybe(env); + if(env->head==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -596,17 +675,16 @@ /* If it's a list */ case list: temp_val= env->head->item; - protect(env, temp_val); - toss(env); - if(env->err) return; + protect(temp_val); + + toss(env); if(env->err) return; iterator= (stackitem*)temp_val->content.ptr; - unprotect(env); while(iterator!=NULL) { push_val(env, iterator->item); if(env->head->item->type==symb - && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { + && (((symbol*)(env->head->item->content.ptr))->id[0] == ';')) { toss(env); if(env->err) return; @@ -618,6 +696,7 @@ } iterator= iterator->next; } + unprotect(temp_val); return; default: @@ -626,7 +705,8 @@ } /* Reverse (flip) a list */ -extern void rev(environment *env){ +extern void rev(environment *env) +{ stackitem *old_head, *new_head, *item; if((env->head)==NULL) { @@ -659,6 +739,8 @@ value *pack; iterator= env->head; + pack= new_val(env); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -676,18 +758,20 @@ temp= env->head; env->head= iterator->next; iterator->next= NULL; + + pack->type= list; + pack->content.ptr= temp; if(env->head!=NULL) toss(env); } /* Push list */ - pack= new_val(env); - pack->type= list; - pack->content.ptr= temp; push_val(env, pack); rev(env); + + unprotect(pack); } /* Relocate elements of the list on the stack. */ @@ -765,7 +849,7 @@ return; } - val= env->head->item->content.val; + val= env->head->item->content.i; toss(env); push_int(env, !val); } @@ -808,7 +892,7 @@ /* Quit stack. */ extern void quit(environment *env) { - long i; + int i; clear(env); @@ -821,7 +905,7 @@ } env->gc_limit= 0; - gc_init(env); + gc_maybe(env); if(env->free_string!=NULL) free(env->free_string); @@ -854,7 +938,8 @@ } /* Internal forget function */ -void forget_sym(symbol **hash_entry) { +void forget_sym(symbol **hash_entry) +{ symbol *temp; temp= *hash_entry; @@ -889,7 +974,8 @@ } /* Returns the current error number to the stack */ -extern void errn(environment *env){ +extern void errn(environment *env) +{ push_int(env, env->err); } @@ -930,7 +1016,7 @@ } if(myenv.interactive) { - printf("Stack version $Revision: 1.92 $\n\ + printf("Stack version $Revision: 1.101 $\n\ Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn\n\ Stack comes with ABSOLUTELY NO WARRANTY; for details type `warranty;'.\n\ This is free software, and you are welcome to redistribute it\n\ @@ -958,15 +1044,17 @@ toss(&myenv); /* No error check in main */ eval(&myenv); } - gc_init(&myenv); + gc_maybe(&myenv); } quit(&myenv); return EXIT_FAILURE; } /* "+" */ -extern void sx_2b(environment *env) { +extern void sx_2b(environment *env) +{ int a, b; + float fa, fb; size_t len; char* new_string; value *a_val, *b_val; @@ -981,7 +1069,7 @@ && env->head->next->item->type==string) { a_val= env->head->item; b_val= env->head->next->item; - protect(env, a_val); protect(env, b_val); + protect(a_val); protect(b_val); toss(env); if(env->err) return; toss(env); if(env->err) return; len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; @@ -989,28 +1077,65 @@ strcpy(new_string, b_val->content.ptr); strcat(new_string, a_val->content.ptr); push_cstring(env, new_string); - unprotect(env); unprotect(env); + unprotect(a_val); unprotect(b_val); free(new_string); + return; } - if(env->head->item->type!=integer - || env->head->next->item->type!=integer) { - printerr("Bad Argument Type"); - env->err=2; + if(env->head->item->type==integer + && env->head->next->item->type==integer) { + a=env->head->item->content.i; + toss(env); if(env->err) return; + b=env->head->item->content.i; + toss(env); if(env->err) return; + push_int(env, b+a); + return; } - a= env->head->item->content.val; - toss(env); if(env->err) return; - - b= env->head->item->content.val; - toss(env); if(env->err) return; - push_int(env, a+b); + + if(env->head->item->type==tfloat + && env->head->next->item->type==tfloat) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_float(env, fb+fa); + + return; + } + + if(env->head->item->type==tfloat + && env->head->next->item->type==integer) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + b= env->head->item->content.i; + toss(env); if(env->err) return; + push_float(env, b+fa); + + return; + } + + if(env->head->item->type==integer + && env->head->next->item->type==tfloat) { + a= env->head->item->content.i; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_float(env, fb+a); + + return; + } + + printerr("Bad Argument Type"); + env->err=2; } /* "-" */ -extern void sx_2d(environment *env) { +extern void sx_2d(environment *env) +{ int a, b; + float fa, fb; if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); @@ -1018,23 +1143,59 @@ return; } - if(env->head->item->type!=integer - || env->head->next->item->type!=integer) { - printerr("Bad Argument Type"); - env->err=2; + if(env->head->item->type==integer + && env->head->next->item->type==integer) { + a=env->head->item->content.i; + toss(env); if(env->err) return; + b=env->head->item->content.i; + toss(env); if(env->err) return; + push_int(env, b-a); + return; } - a=env->head->item->content.val; - toss(env); if(env->err) return; - b=env->head->item->content.val; - toss(env); if(env->err) return; - push_int(env, b-a); + if(env->head->item->type==tfloat + && env->head->next->item->type==tfloat) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_float(env, fb-fa); + + return; + } + + if(env->head->item->type==tfloat + && env->head->next->item->type==integer) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + b= env->head->item->content.i; + toss(env); if(env->err) return; + push_float(env, b-fa); + + return; + } + + if(env->head->item->type==integer + && env->head->next->item->type==tfloat) { + a= env->head->item->content.i; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_float(env, fb-a); + + return; + } + + printerr("Bad Argument Type"); + env->err=2; } /* ">" */ -extern void sx_3e(environment *env) { +extern void sx_3e(environment *env) +{ int a, b; + float fa, fb; if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); @@ -1042,41 +1203,97 @@ return; } - if(env->head->item->type!=integer - || env->head->next->item->type!=integer) { - printerr("Bad Argument Type"); - env->err=2; + if(env->head->item->type==integer + && env->head->next->item->type==integer) { + a=env->head->item->content.i; + toss(env); if(env->err) return; + b=env->head->item->content.i; + toss(env); if(env->err) return; + push_int(env, b>a); + return; } - a=env->head->item->content.val; - toss(env); if(env->err) return; - b=env->head->item->content.val; - toss(env); if(env->err) return; - push_int(env, b>a); + if(env->head->item->type==tfloat + && env->head->next->item->type==tfloat) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_int(env, fb>fa); + + return; + } + + if(env->head->item->type==tfloat + && env->head->next->item->type==integer) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + b= env->head->item->content.i; + toss(env); if(env->err) return; + push_int(env, b>fa); + + return; + } + + if(env->head->item->type==integer + && env->head->next->item->type==tfloat) { + a= env->head->item->content.i; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_int(env, fb>a); + + return; + } + + printerr("Bad Argument Type"); + env->err=2; +} + +/* "<" */ +extern void sx_3c(environment *env) +{ + swap(env); if(env->err) return; + sx_3e(env); +} + +/* "<=" */ +extern void sx_3c3d(environment *env) +{ + sx_3e(env); if(env->err) return; + not(env); +} + +/* ">=" */ +extern void sx_3e3d(environment *env) +{ + sx_3c(env); if(env->err) return; + not(env); } /* Return copy of a value */ -value *copy_val(environment *env, value *old_value){ +value *copy_val(environment *env, value *old_value) +{ stackitem *old_item, *new_item, *prev_item; + value *new_value; - value *new_value= new_val(env); - - protect(env, old_value); + protect(old_value); + new_value= new_val(env); + protect(new_value); new_value->type= old_value->type; switch(old_value->type){ + case tfloat: case integer: - new_value->content.val= old_value->content.val; + case func: + case symb: + new_value->content= old_value->content; break; case string: (char *)(new_value->content.ptr)= strdup((char *)(old_value->content.ptr)); break; - case func: - case symb: - new_value->content.ptr= old_value->content.ptr; - break; case list: new_value->content.ptr= NULL; @@ -1098,13 +1315,14 @@ break; } - unprotect(env); + unprotect(old_value); unprotect(new_value); return new_value; } /* "dup"; duplicates an item on the stack */ -extern void sx_647570(environment *env) { +extern void sx_647570(environment *env) +{ if((env->head)==NULL) { printerr("Too Few Arguments"); env->err= 1; @@ -1114,8 +1332,8 @@ } /* "if", If-Then */ -extern void sx_6966(environment *env) { - +extern void sx_6966(environment *env) +{ int truth; if((env->head)==NULL || env->head->next==NULL) { @@ -1133,7 +1351,7 @@ swap(env); if(env->err) return; - truth=env->head->item->content.val; + truth=env->head->item->content.i; toss(env); if(env->err) return; @@ -1145,8 +1363,8 @@ } /* If-Then-Else */ -extern void ifelse(environment *env) { - +extern void ifelse(environment *env) +{ int truth; if((env->head)==NULL || env->head->next==NULL @@ -1165,7 +1383,7 @@ rot(env); if(env->err) return; - truth=env->head->item->content.val; + truth=env->head->item->content.i; toss(env); if(env->err) return; @@ -1181,8 +1399,8 @@ } /* "while" */ -extern void sx_7768696c65(environment *env) { - +extern void sx_7768696c65(environment *env) +{ int truth; value *loop, *test; @@ -1193,11 +1411,11 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; test= env->head->item; - protect(env, test); + protect(test); toss(env); if(env->err) return; do { @@ -1210,7 +1428,7 @@ return; } - truth= env->head->item->content.val; + truth= env->head->item->content.i; toss(env); if(env->err) return; if(truth) { @@ -1222,12 +1440,13 @@ } while(truth); - unprotect(env); unprotect(env); + unprotect(loop); unprotect(test); } /* "for"; for-loop */ -extern void sx_666f72(environment *env) { +extern void sx_666f72(environment *env) +{ value *loop; int foo1, foo2; @@ -1246,13 +1465,13 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; - foo2= env->head->item->content.val; + foo2= env->head->item->content.i; toss(env); if(env->err) return; - foo1= env->head->item->content.val; + foo1= env->head->item->content.i; toss(env); if(env->err) return; if(foo1<=foo2) { @@ -1270,12 +1489,12 @@ foo1--; } } - unprotect(env); + unprotect(loop); } /* Variant of for-loop */ -extern void foreach(environment *env) { - +extern void foreach(environment *env) +{ value *loop, *foo; stackitem *iterator; @@ -1292,11 +1511,11 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; foo= env->head->item; - protect(env, foo); + protect(foo); toss(env); if(env->err) return; iterator= foo->content.ptr; @@ -1307,15 +1526,16 @@ eval(env); if(env->err) return; iterator= iterator->next; } - unprotect(env); unprotect(env); + unprotect(loop); unprotect(foo); } /* "to" */ -extern void to(environment *env) { - int i, start, ending; - stackitem *temp_head; - value *temp_val; - +extern void to(environment *env) +{ + int ending, start, i; + stackitem *iterator, *temp; + value *pack; + if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -1329,13 +1549,12 @@ return; } - ending= env->head->item->content.val; + ending= env->head->item->content.i; toss(env); if(env->err) return; - start= env->head->item->content.val; + start= env->head->item->content.i; toss(env); if(env->err) return; - temp_head= env->head; - env->head= NULL; + push_sym(env, "["); if(ending>=start) { for(i= ending; i>=start; i--) @@ -1345,15 +1564,44 @@ push_int(env, i); } - temp_val= new_val(env); - temp_val->content.ptr= env->head; - temp_val->type= list; - env->head= temp_head; - push_val(env, temp_val); + iterator= env->head; + pack= new_val(env); + protect(pack); + + 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->type!=symb + || ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) + iterator= iterator->next; + + /* Extract list */ + temp= env->head; + env->head= iterator->next; + iterator->next= NULL; + + pack->type= list; + pack->content.ptr= temp; + + if(env->head!=NULL) + toss(env); + } + + /* Push list */ + + push_val(env, pack); + + unprotect(pack); } /* Read a string */ -extern void readline(environment *env) { +extern void readline(environment *env) +{ char in_string[101]; if(fgets(in_string, 100, env->inputstream)==NULL) @@ -1363,18 +1611,22 @@ } /* "read"; Read a value and place on stack */ -extern void sx_72656164(environment *env) { +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 fltform[]= "%f%n"; const char blankform[]= "%*[ \t]%n"; const char ebrackform[]= "]%n"; const char semicform[]= ";%n"; const char bbrackform[]= "[%n"; int itemp, readlength= -1; + int count= -1; + float ftemp; static int depth= 0; - char *match; + char *match, *ctemp; size_t inlength; if(env->in_string==NULL) { @@ -1397,12 +1649,17 @@ inlength= strlen(env->in_string)+1; match= malloc(inlength); - if(sscanf(env->in_string, blankform, &readlength)!=EOF + if(sscanf(env->in_string, blankform, &readlength) != EOF && readlength != -1) { ; - } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF + } else if(sscanf(env->in_string, fltform, &ftemp, &readlength) != EOF && readlength != -1) { - push_int(env, itemp); + if(sscanf(env->in_string, intform, &itemp, &count) != EOF + && count==readlength) { + push_int(env, itemp); + } else { + push_float(env, ftemp); + } } else if(sscanf(env->in_string, strform, match, &readlength) != EOF && readlength != -1) { push_cstring(env, match); @@ -1424,7 +1681,7 @@ free(env->free_string); env->in_string = env->free_string = NULL; } - if ( env->in_string != NULL) { + if (env->in_string != NULL) { env->in_string += readlength; } @@ -1434,8 +1691,8 @@ return sx_72656164(env); } -extern void beep(environment *env) { - +extern void beep(environment *env) +{ int freq, dur, period, ticks; if((env->head)==NULL || env->head->next==NULL) { @@ -1451,9 +1708,9 @@ return; } - dur=env->head->item->content.val; + dur=env->head->item->content.i; toss(env); - freq=env->head->item->content.val; + freq=env->head->item->content.i; toss(env); period=1193180/freq; /* convert freq from Hz to period @@ -1475,11 +1732,11 @@ default: abort(); } -}; +} /* "wait" */ -extern void sx_77616974(environment *env) { - +extern void sx_77616974(environment *env) +{ int dur; if((env->head)==NULL) { @@ -1494,13 +1751,14 @@ return; } - dur=env->head->item->content.val; + dur=env->head->item->content.i; toss(env); usleep(dur); -}; +} -extern void copying(environment *env){ +extern void copying(environment *env) +{ printf("GNU GENERAL PUBLIC LICENSE\n\ Version 2, June 1991\n\ \n\ @@ -1759,7 +2017,8 @@ of promoting the sharing and reuse of software generally.\n"); } -extern void warranty(environment *env){ +extern void warranty(environment *env) +{ printf(" NO WARRANTY\n\ \n\ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n\ @@ -1787,6 +2046,7 @@ extern void sx_2a(environment *env) { int a, b; + float fa, fb; if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); @@ -1794,24 +2054,59 @@ return; } - if(env->head->item->type!=integer - || env->head->next->item->type!=integer) { - printerr("Bad Argument Type"); - env->err=2; + if(env->head->item->type==integer + && env->head->next->item->type==integer) { + a=env->head->item->content.i; + toss(env); if(env->err) return; + b=env->head->item->content.i; + toss(env); if(env->err) return; + push_int(env, b*a); + return; } - a=env->head->item->content.val; - toss(env); if(env->err) return; - b=env->head->item->content.val; - toss(env); if(env->err) return; - push_int(env, b*a); + if(env->head->item->type==tfloat + && env->head->next->item->type==tfloat) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_float(env, fb*fa); + + return; + } + + if(env->head->item->type==tfloat + && env->head->next->item->type==integer) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + b= env->head->item->content.i; + toss(env); if(env->err) return; + push_float(env, b*fa); + + return; + } + + if(env->head->item->type==integer + && env->head->next->item->type==tfloat) { + a= env->head->item->content.i; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_float(env, fb*a); + + return; + } + + printerr("Bad Argument Type"); + env->err=2; } /* "/" */ extern void sx_2f(environment *env) { int a, b; + float fa, fb; if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); @@ -1819,18 +2114,52 @@ return; } - if(env->head->item->type!=integer - || env->head->next->item->type!=integer) { - printerr("Bad Argument Type"); - env->err=2; + if(env->head->item->type==integer + && env->head->next->item->type==integer) { + a=env->head->item->content.i; + toss(env); if(env->err) return; + b=env->head->item->content.i; + toss(env); if(env->err) return; + push_float(env, b/a); + return; } - a=env->head->item->content.val; - toss(env); if(env->err) return; - b=env->head->item->content.val; - toss(env); if(env->err) return; - push_int(env, b/a); + if(env->head->item->type==tfloat + && env->head->next->item->type==tfloat) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_float(env, fb/fa); + + return; + } + + if(env->head->item->type==tfloat + && env->head->next->item->type==integer) { + fa= env->head->item->content.f; + toss(env); if(env->err) return; + b= env->head->item->content.i; + toss(env); if(env->err) return; + push_float(env, b/fa); + + return; + } + + if(env->head->item->type==integer + && env->head->next->item->type==tfloat) { + a= env->head->item->content.i; + toss(env); if(env->err) return; + fb= env->head->item->content.f; + toss(env); if(env->err) return; + push_float(env, fb/a); + + return; + } + + printerr("Bad Argument Type"); + env->err=2; } /* "mod" */ @@ -1840,20 +2169,47 @@ if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } - if(env->head->item->type!=integer - || env->head->next->item->type!=integer) { - printerr("Bad Argument Type"); - env->err=2; + if(env->head->item->type==integer + && env->head->next->item->type==integer) { + a= env->head->item->content.i; + toss(env); if(env->err) return; + b= env->head->item->content.i; + toss(env); if(env->err) return; + push_int(env, b%a); + return; } - a=env->head->item->content.val; - toss(env); if(env->err) return; - b=env->head->item->content.val; - toss(env); if(env->err) return; - push_int(env, b%a); + printerr("Bad Argument Type"); + env->err=2; +} + +/* "div" */ +extern void sx_646976(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) { + a= env->head->item->content.i; + toss(env); if(env->err) return; + b= env->head->item->content.i; + toss(env); if(env->err) return; + push_int(env, (int)b/a); + + return; + } + + printerr("Bad Argument Type"); + env->err= 2; }