/[cvs]/stack/stack.c
ViewVC logotype

Diff of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.86 by teddy, Fri Feb 15 14:44:24 2002 UTC revision 1.89 by masse, Sun Feb 17 04:03:57 2002 UTC
# Line 15  Line 15 
15  /* mtrace, muntrace */  /* mtrace, muntrace */
16  #include <mcheck.h>  #include <mcheck.h>
17    
18  #define HASHTBLSIZE 2048  #include "stack.h"
   
 /* First, define some types. */  
   
 /* A value of some type */  
 typedef struct {  
   enum {  
     integer,  
     string,  
     func,                       /* Function pointer */  
     symb,  
     list  
   } type;                       /* Type of stack element */  
   
   union {  
     void *ptr;                  /* Pointer to the content */  
     int val;                    /* ...or an integer */  
   } content;                    /* Stores a pointer or an integer */  
   
   int refcount;                 /* Reference counter */  
   
 } value;  
   
 /* A symbol with a name and possible value */  
 /* (These do not need reference counters, they are kept unique by  
    hashing.) */  
 typedef struct symbol_struct {  
   char *id;                     /* Symbol name */  
   value *val;                   /* The value (if any) bound to it */  
   struct symbol_struct *next;   /* In case of hashing conflicts, a */  
 } symbol;                       /* symbol is a kind of stack item. */  
   
 /* A type for a hash table for symbols */  
 typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */  
   
 /* An item (value) on a stack */  
 typedef struct stackitem_struct  
 {  
   value *item;                  /* The value on the stack */  
                                 /* (This is never NULL) */  
   struct stackitem_struct *next; /* Next item */  
 } stackitem;  
   
 /* An environment; gives access to the stack and a hash table of  
    defined symbols */  
 typedef struct {  
   stackitem *head;              /* Head of the stack */  
   hashtbl symbols;              /* Hash table of all variable bindings */  
   int err;                      /* Error 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 */  
 typedef void (*funcp)(environment *); /* funcp is a pointer to a void  
                                          function (environment *) */  
19    
20  /* Initialize a newly created environment */  /* Initialize a newly created environment */
21  void init_env(environment *env)  void init_env(environment *env)
22  {  {
23    int i;    int i;
24    
25      env->gc_limit= 20;
26      env->gc_count= 0;
27    
28    env->head= NULL;    env->head= NULL;
29    for(i= 0; i<HASHTBLSIZE; i++)    for(i= 0; i<HASHTBLSIZE; i++)
30      env->symbols[i]= NULL;      env->symbols[i]= NULL;
# Line 94  void printerr(const char* in_string) { Line 39  void printerr(const char* in_string) {
39    fprintf(stderr, "Err: %s\n", in_string);    fprintf(stderr, "Err: %s\n", in_string);
40  }  }
41    
 /* Throw away a value */  
 void free_val(value *val){  
   stackitem *item, *temp;  
   
   val->refcount--;              /* Decrease the reference count */  
   if(val->refcount == 0){  
     switch (val->type){         /* and free the contents if necessary */  
     case string:  
       free(val->content.ptr);  
       break;  
     case list:                  /* lists needs to be freed recursively */  
       item=val->content.ptr;  
       while(item != NULL) {     /* for all stack items */  
         free_val(item->item);   /* free the value */  
         temp=item->next;        /* save next ptr */  
         free(item);             /* free the stackitem */  
         item=temp;              /* go to next stackitem */  
       }  
       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 */  
   }  
 }  
   
42  /* Discard the top element of the stack. */  /* Discard the top element of the stack. */
43  extern void toss(environment *env)  extern void toss(environment *env)
44  {  {
# Line 138  extern void toss(environment *env) Line 50  extern void toss(environment *env)
50      return;      return;
51    }    }
52        
   free_val(env->head->item);    /* Free the value */  
53    env->head= env->head->next;   /* Remove the top stack item */    env->head= env->head->next;   /* Remove the top stack item */
54    free(temp);                   /* Free the old top stack item */    free(temp);                   /* Free the old top stack item */
55  }  }
# Line 172  symbol **hash(hashtbl in_hashtbl, const Line 83  symbol **hash(hashtbl in_hashtbl, const
83    }    }
84  }  }
85    
86    value* new_val(environment *env) {
87      value *nval= malloc(sizeof(value));
88      stackitem *nitem= malloc(sizeof(stackitem));
89    
90      nval->content.ptr= NULL;
91    
92      nitem->item= nval;
93      nitem->next= env->gc_ref;
94      env->gc_ref= nitem;
95    
96      env->gc_count++;
97    
98      return nval;
99    }
100    
101    void gc_mark(value *val) {
102      stackitem *iterator;
103    
104      if(val==NULL || val->gc_garb==0)
105        return;
106    
107      val->gc_garb= 0;
108    
109      if(val->type==list) {
110        iterator= val->content.ptr;
111    
112        while(iterator!=NULL) {
113          gc_mark(iterator->item);
114          iterator= iterator->next;
115        }
116      }
117    }
118    
119    extern void gc_init(environment *env) {
120      stackitem *new_head= NULL, *titem, *iterator= env->gc_ref;
121      symbol *tsymb;
122      int i;
123    
124      if(env->gc_count < env->gc_limit)
125        return;
126    
127      while(iterator!=NULL) {
128        iterator->item->gc_garb= 1;
129        iterator= iterator->next;
130      }
131    
132      /* Mark */
133      iterator= env->head;
134      while(iterator!=NULL) {
135        gc_mark(iterator->item);
136        iterator= iterator->next;
137      }
138    
139      for(i= 0; i<HASHTBLSIZE; i++) {
140        tsymb= env->symbols[i];
141        while(tsymb!=NULL) {
142          gc_mark(tsymb->val);
143          tsymb= tsymb->next;
144        }
145      }
146    
147      env->gc_count= 0;
148    
149      /* Sweep */
150      while(env->gc_ref!=NULL) {
151        if(env->gc_ref->item->gc_garb) {
152          switch(env->gc_ref->item->type) {
153          case string:
154            free(env->gc_ref->item->content.ptr);
155            break;
156          case integer:
157            break;
158          case list:
159            while(env->gc_ref->item->content.ptr!=NULL) {
160              titem= env->gc_ref->item->content.ptr;
161              env->gc_ref->item->content.ptr= titem->next;
162              free(titem);
163            }
164            break;
165          default:
166            break;
167          }
168          free(env->gc_ref->item);
169          titem= env->gc_ref->next;
170          free(env->gc_ref);
171          env->gc_ref= titem;
172        } else {
173          titem= env->gc_ref->next;
174          env->gc_ref->next= new_head;
175          new_head= env->gc_ref;
176          env->gc_ref= titem;
177          env->gc_count++;
178        }
179      }
180    
181      env->gc_limit= env->gc_count*2;
182      env->gc_ref= new_head;
183    }
184    
185  /* Push a value onto the stack */  /* Push a value onto the stack */
186  void push_val(environment *env, value *val)  void push_val(environment *env, value *val)
187  {  {
188    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
189    new_item->item= val;    new_item->item= val;
   val->refcount++;  
190    new_item->next= env->head;    new_item->next= env->head;
191    env->head= new_item;    env->head= new_item;
192  }  }
# Line 185  void push_val(environment *env, value *v Line 194  void push_val(environment *env, value *v
194  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
195  void push_int(environment *env, int in_val)  void push_int(environment *env, int in_val)
196  {  {
197    value *new_value= malloc(sizeof(value));    value *new_value= new_val(env);
198        
199    new_value->content.val= in_val;    new_value->content.val= in_val;
200    new_value->type= integer;    new_value->type= integer;
   new_value->refcount= 0;  
201    
202    push_val(env, new_value);    push_val(env, new_value);
203  }  }
# Line 197  void push_int(environment *env, int in_v Line 205  void push_int(environment *env, int in_v
205  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
206  void push_cstring(environment *env, const char *in_string)  void push_cstring(environment *env, const char *in_string)
207  {  {
208    value *new_value= malloc(sizeof(value));    value *new_value= new_val(env);
209    
210    new_value->content.ptr= malloc(strlen(in_string)+1);    new_value->content.ptr= malloc(strlen(in_string)+1);
211    strcpy(new_value->content.ptr, in_string);    strcpy(new_value->content.ptr, in_string);
212    new_value->type= string;    new_value->type= string;
   new_value->refcount= 0;  
213    
214    push_val(env, new_value);    push_val(env, new_value);
215  }  }
# Line 265  void push_sym(environment *env, const ch Line 272  void push_sym(environment *env, const ch
272    const char *dlerr;            /* Dynamic linker error */    const char *dlerr;            /* Dynamic linker error */
273    char *mangled;                /* Mangled function name */    char *mangled;                /* Mangled function name */
274    
275    new_value= malloc(sizeof(value));    new_value= new_val(env);
276    
277    /* The new value is a symbol */    /* The new value is a symbol */
278    new_value->type= symb;    new_value->type= symb;
   new_value->refcount= 1;  
279    
280    /* Look up the symbol name in the hash table */    /* Look up the symbol name in the hash table */
281    new_symbol= hash(env->symbols, in_string);    new_symbol= hash(env->symbols, in_string);
# Line 301  void push_sym(environment *env, const ch Line 307  void push_sym(environment *env, const ch
307        dlerr=dlerror();        dlerr=dlerror();
308      }      }
309      if(dlerr==NULL) {           /* If a function was found */      if(dlerr==NULL) {           /* If a function was found */
310        new_fvalue= malloc(sizeof(value)); /* Create a new value */        new_fvalue= new_val(env); /* Create a new value */
311        new_fvalue->type=func;    /* The new value is a function pointer */        new_fvalue->type=func;    /* The new value is a function pointer */
312        new_fvalue->content.ptr=funcptr; /* Store function pointer */        new_fvalue->content.ptr=funcptr; /* Store function pointer */
313        (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new        (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
314                                           function value */                                           function value */
       new_fvalue->refcount= 1;  
315      }      }
316    }    }
317    push_val(env, new_value);    push_val(env, new_value);
# Line 529  extern void eval(environment *env) Line 534  extern void eval(environment *env)
534      in_func= (funcp)(env->head->item->content.ptr);      in_func= (funcp)(env->head->item->content.ptr);
535      toss(env);      toss(env);
536      if(env->err) return;      if(env->err) return;
537      return (*in_func)(env);      return in_func(env);
538    
539      /* If it's a list */      /* If it's a list */
540    case list:    case list:
541      temp_val= env->head->item;      temp_val= env->head->item;
     env->head->item->refcount++;  
542      toss(env);      toss(env);
543      if(env->err) return;      if(env->err) return;
544      iterator= (stackitem*)temp_val->content.ptr;      iterator= (stackitem*)temp_val->content.ptr;
# Line 545  extern void eval(environment *env) Line 549  extern void eval(environment *env)
549          toss(env);          toss(env);
550          if(env->err) return;          if(env->err) return;
551          if(iterator->next == NULL){          if(iterator->next == NULL){
           free_val(temp_val);  
552            goto eval_start;            goto eval_start;
553          }          }
554          eval(env);          eval(env);
# Line 553  extern void eval(environment *env) Line 556  extern void eval(environment *env)
556        }        }
557        iterator= iterator->next;        iterator= iterator->next;
558      }      }
     free_val(temp_val);  
559      return;      return;
560    
561    default:    default:
# Line 618  extern void pack(environment *env) Line 620  extern void pack(environment *env)
620    }    }
621    
622    /* Push list */    /* Push list */
623    pack= malloc(sizeof(value));    pack= new_val(env);
624    pack->type= list;    pack->type= list;
625    pack->content.ptr= temp;    pack->content.ptr= temp;
   pack->refcount= 0;  
626    
627    push_val(env, pack);    push_val(env, pack);
628    rev(env);    rev(env);
# Line 652  extern void expand(environment *env) Line 653  extern void expand(environment *env)
653    /* The first list element is the new stack head */    /* The first list element is the new stack head */
654    new_head= temp= env->head->item->content.ptr;    new_head= temp= env->head->item->content.ptr;
655    
   env->head->item->refcount++;  
656    toss(env);    toss(env);
657    
658    /* Find the end of the list */    /* Find the end of the list */
# Line 738  extern void def(environment *env) Line 738  extern void def(environment *env)
738    sym=env->head->item->content.ptr;    sym=env->head->item->content.ptr;
739    
740    /* if the symbol was bound to something else, throw it away */    /* if the symbol was bound to something else, throw it away */
   if(sym->val != NULL)  
     free_val(sym->val);  
741    
742    /* Bind the symbol to the value */    /* Bind the symbol to the value */
743    sym->val= env->head->next->item;    sym->val= env->head->next->item;
   sym->val->refcount++;         /* Increase the reference counter */  
744    
745    toss(env); toss(env);    toss(env); toss(env);
746  }  }
747    
 extern void clear(environment *);  
 void forget_sym(symbol **);  
 extern void words(environment *);  
   
748  /* Quit stack. */  /* Quit stack. */
749  extern void quit(environment *env)  extern void quit(environment *env)
750  {  {
# Line 767  extern void quit(environment *env) Line 760  extern void quit(environment *env)
760      env->symbols[i]= NULL;      env->symbols[i]= NULL;
761    }    }
762    
763      gc_init(env);
764    
765    if(env->free_string!=NULL)    if(env->free_string!=NULL)
766      free(env->free_string);      free(env->free_string);
767        
# Line 804  void forget_sym(symbol **hash_entry) { Line 799  void forget_sym(symbol **hash_entry) {
799    temp= *hash_entry;    temp= *hash_entry;
800    *hash_entry= (*hash_entry)->next;    *hash_entry= (*hash_entry)->next;
801        
   if(temp->val!=NULL) {  
     free_val(temp->val);  
   }  
802    free(temp->id);    free(temp->id);
803    free(temp);    free(temp);
804  }  }
# Line 840  extern void errn(environment *env){ Line 832  extern void errn(environment *env){
832    push_int(env, env->err);    push_int(env, env->err);
833  }  }
834    
 extern void sx_72656164(environment*);  
   
835  int main(int argc, char **argv)  int main(int argc, char **argv)
836  {  {
837    environment myenv;    environment myenv;
# Line 899  int main(int argc, char **argv) Line 889  int main(int argc, char **argv)
889        toss(&myenv);             /* No error check in main */        toss(&myenv);             /* No error check in main */
890        eval(&myenv);        eval(&myenv);
891      }      }
892        gc_init(&myenv);
893    }    }
894    quit(&myenv);    quit(&myenv);
895    return EXIT_FAILURE;    return EXIT_FAILURE;
# Line 921  extern void sx_2b(environment *env) { Line 912  extern void sx_2b(environment *env) {
912       && env->head->next->item->type==string) {       && env->head->next->item->type==string) {
913      a_val= env->head->item;      a_val= env->head->item;
914      b_val= env->head->next->item;      b_val= env->head->next->item;
     a_val->refcount++;  
     b_val->refcount++;  
915      toss(env); if(env->err) return;      toss(env); if(env->err) return;
916      toss(env); if(env->err) return;      toss(env); if(env->err) return;
917      len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;      len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
918      new_string= malloc(len);      new_string= malloc(len);
919      strcpy(new_string, b_val->content.ptr);      strcpy(new_string, b_val->content.ptr);
920      strcat(new_string, a_val->content.ptr);      strcat(new_string, a_val->content.ptr);
     free_val(a_val); free_val(b_val);  
921      push_cstring(env, new_string);      push_cstring(env, new_string);
922      free(new_string);      free(new_string);
923      return;      return;
# Line 942  extern void sx_2b(environment *env) { Line 930  extern void sx_2b(environment *env) {
930      return;      return;
931    }    }
932    a=env->head->item->content.val;    a=env->head->item->content.val;
933    toss(env);    toss(env); if(env->err) return;
934    if(env->err) return;    
935    if(env->head->item->refcount == 1)    b=env->head->item->content.val;
936      env->head->item->content.val += a;    toss(env); if(env->err) return;
937    else {    push_int(env, a+b);
     b=env->head->item->content.val;  
     toss(env);  
     if(env->err) return;  
     push_int(env, a+b);  
   }  
938  }  }
939    
940  /* "-" */  /* "-" */
# Line 971  extern void sx_2d(environment *env) { Line 954  extern void sx_2d(environment *env) {
954      return;      return;
955    }    }
956    a=env->head->item->content.val;    a=env->head->item->content.val;
957    toss(env);    toss(env); if(env->err) return;
958    if(env->err) return;    b=env->head->item->content.val;
959    if(env->head->item->refcount == 1)    toss(env); if(env->err) return;
960      env->head->item->content.val -= a;    push_int(env, b-a);
   else {  
     b=env->head->item->content.val;  
     toss(env);  
     if(env->err) return;  
     push_int(env, b-a);  
   }  
961  }  }
962    
963  /* ">" */  /* ">" */
# Line 1000  extern void sx_3e(environment *env) { Line 977  extern void sx_3e(environment *env) {
977      return;      return;
978    }    }
979    a=env->head->item->content.val;    a=env->head->item->content.val;
980    toss(env);    toss(env); if(env->err) return;
981    if(env->err) return;    b=env->head->item->content.val;
982    if(env->head->item->refcount == 1)    toss(env); if(env->err) return;
983      env->head->item->content.val = (env->head->item->content.val > a);    push_int(env, b>a);
   else {  
     b=env->head->item->content.val;  
     toss(env);  
     if(env->err) return;  
     push_int(env, b>a);  
   }  
984  }  }
985    
986  /* Return copy of a value */  /* Return copy of a value */
987  value *copy_val(value *old_value){  value *copy_val(environment *env, value *old_value){
988    stackitem *old_item, *new_item, *prev_item;    stackitem *old_item, *new_item, *prev_item;
989    
990    value *new_value=malloc(sizeof(value));    value *new_value=new_val(env);
991    
992    new_value->type=old_value->type;    new_value->type=old_value->type;
993    new_value->refcount=0;        /* This is increased if/when this  
                                    value is referenced somewhere, like  
                                    in a stack item or a variable */  
994    switch(old_value->type){    switch(old_value->type){
995    case integer:    case integer:
996      new_value->content.val=old_value->content.val;      new_value->content.val=old_value->content.val;
# Line 1042  value *copy_val(value *old_value){ Line 1011  value *copy_val(value *old_value){
1011    
1012      while(old_item != NULL) {   /* While list is not empty */      while(old_item != NULL) {   /* While list is not empty */
1013        new_item= malloc(sizeof(stackitem));        new_item= malloc(sizeof(stackitem));
1014        new_item->item=copy_val(old_item->item); /* recurse */        new_item->item=copy_val(env, old_item->item); /* recurse */
1015        new_item->next=NULL;        new_item->next=NULL;
1016        if(prev_item != NULL)     /* If this wasn't the first item */        if(prev_item != NULL)     /* If this wasn't the first item */
1017          prev_item->next=new_item; /* point the previous item to the          prev_item->next=new_item; /* point the previous item to the
# Line 1064  extern void sx_647570(environment *env) Line 1033  extern void sx_647570(environment *env)
1033      env->err=1;      env->err=1;
1034      return;      return;
1035    }    }
1036    push_val(env, copy_val(env->head->item));    push_val(env, copy_val(env, env->head->item));
1037  }  }
1038    
1039  /* "if", If-Then */  /* "if", If-Then */
# Line 1147  extern void sx_7768696c65(environment *e Line 1116  extern void sx_7768696c65(environment *e
1116    }    }
1117    
1118    loop= env->head->item;    loop= env->head->item;
   loop->refcount++;  
1119    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1120    
1121    test= env->head->item;    test= env->head->item;
   test->refcount++;  
1122    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1123    
1124    do {    do {
# Line 1175  extern void sx_7768696c65(environment *e Line 1142  extern void sx_7768696c65(environment *e
1142      }      }
1143        
1144    } while(truth);    } while(truth);
   
   free_val(test);  
   free_val(loop);  
1145  }  }
1146    
1147  /* "for"; For-loop */  
1148    /* "for"; for-loop */
1149  extern void sx_666f72(environment *env) {  extern void sx_666f72(environment *env) {
1150      value *loop;
1151      int foo1, foo2;
1152    
1153      if(env->head==NULL || env->head->next==NULL
1154         || env->head->next->next==NULL) {
1155        printerr("Too Few Arguments");
1156        env->err= 1;
1157        return;
1158      }
1159    
1160      if(env->head->next->item->type!=integer
1161         || env->head->next->next->item->type!=integer) {
1162        printerr("Bad Argument Type");
1163        env->err= 2;
1164        return;
1165      }
1166    
1167      loop= env->head->item;
1168      toss(env); if(env->err) return;
1169    
1170      foo2= env->head->item->content.val;
1171      toss(env); if(env->err) return;
1172    
1173      foo1= env->head->item->content.val;
1174      toss(env); if(env->err) return;
1175    
1176      if(foo1<=foo2) {
1177        while(foo1<=foo2) {
1178          push_int(env, foo1);
1179          push_val(env, loop);
1180          eval(env); if(env->err) return;
1181          foo1++;
1182        }
1183      } else {
1184        while(foo1>=foo2) {
1185          push_int(env, foo1);
1186          push_val(env, loop);
1187          eval(env); if(env->err) return;
1188          foo1--;
1189        }
1190      }
1191    }
1192    
1193    /* Variant of for-loop */
1194    extern void foreach(environment *env) {
1195        
1196    value *loop, *foo;    value *loop, *foo;
1197    stackitem *iterator;    stackitem *iterator;
# Line 1199  extern void sx_666f72(environment *env) Line 1209  extern void sx_666f72(environment *env)
1209    }    }
1210    
1211    loop= env->head->item;    loop= env->head->item;
   loop->refcount++;  
1212    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1213    
1214    foo= env->head->item;    foo= env->head->item;
   foo->refcount++;  
1215    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1216    
1217    iterator= foo->content.ptr;    iterator= foo->content.ptr;
# Line 1214  extern void sx_666f72(environment *env) Line 1222  extern void sx_666f72(environment *env)
1222      eval(env); if(env->err) return;      eval(env); if(env->err) return;
1223      iterator= iterator->next;      iterator= iterator->next;
1224    }    }
   
   free_val(loop);  
   free_val(foo);  
1225  }  }
1226    
1227  /* "to" */  /* "to" */
# Line 1254  extern void to(environment *env) { Line 1259  extern void to(environment *env) {
1259        push_int(env, i);        push_int(env, i);
1260    }    }
1261    
1262    temp_val= malloc(sizeof(value));    temp_val= new_val(env);
1263    temp_val->content.ptr= env->head;    temp_val->content.ptr= env->head;
   temp_val->refcount= 0;  
1264    temp_val->type= list;    temp_val->type= list;
1265    env->head= temp_head;    env->head= temp_head;
1266    push_val(env, temp_val);    push_val(env, temp_val);

Legend:
Removed from v.1.86  
changed lines
  Added in v.1.89

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26