/[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.87 by masse, Fri Feb 15 18:27:18 2002 UTC
# Line 34  typedef struct { Line 34  typedef struct {
34      int val;                    /* ...or an integer */      int val;                    /* ...or an integer */
35    } content;                    /* Stores a pointer or an integer */    } content;                    /* Stores a pointer or an integer */
36    
37    int refcount;                 /* Reference counter */    int gc_garb;
38    
39  } value;  } value;
40    
# Line 61  typedef struct stackitem_struct Line 61  typedef struct stackitem_struct
61  /* An environment; gives access to the stack and a hash table of  /* An environment; gives access to the stack and a hash table of
62     defined symbols */     defined symbols */
63  typedef struct {  typedef struct {
64      stackitem *gc_ref;
65      int gc_limit, gc_count;
66    
67    stackitem *head;              /* Head of the stack */    stackitem *head;              /* Head of the stack */
68    hashtbl symbols;              /* Hash table of all variable bindings */    hashtbl symbols;              /* Hash table of all variable bindings */
69    int err;                      /* Error flag */    int err;                      /* Error flag */
# Line 80  void init_env(environment *env) Line 83  void init_env(environment *env)
83  {  {
84    int i;    int i;
85    
86      env->gc_limit= 20;
87      env->gc_count= 0;
88    
89    env->head= NULL;    env->head= NULL;
90    for(i= 0; i<HASHTBLSIZE; i++)    for(i= 0; i<HASHTBLSIZE; i++)
91      env->symbols[i]= NULL;      env->symbols[i]= NULL;
# Line 94  void printerr(const char* in_string) { Line 100  void printerr(const char* in_string) {
100    fprintf(stderr, "Err: %s\n", in_string);    fprintf(stderr, "Err: %s\n", in_string);
101  }  }
102    
 /* 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 */  
   }  
 }  
   
103  /* Discard the top element of the stack. */  /* Discard the top element of the stack. */
104  extern void toss(environment *env)  extern void toss(environment *env)
105  {  {
# Line 138  extern void toss(environment *env) Line 111  extern void toss(environment *env)
111      return;      return;
112    }    }
113        
   free_val(env->head->item);    /* Free the value */  
114    env->head= env->head->next;   /* Remove the top stack item */    env->head= env->head->next;   /* Remove the top stack item */
115    free(temp);                   /* Free the old top stack item */    free(temp);                   /* Free the old top stack item */
116  }  }
# Line 172  symbol **hash(hashtbl in_hashtbl, const Line 144  symbol **hash(hashtbl in_hashtbl, const
144    }    }
145  }  }
146    
147    extern void gc_init(environment*);
148    
149    value* new_val(environment *env) {
150      value *nval= malloc(sizeof(value));
151      stackitem *nitem= malloc(sizeof(stackitem));
152    
153      if(env->gc_count >= env->gc_limit)
154        gc_init(env);
155    
156      nval->content.ptr= NULL;
157    
158      nitem->item= nval;
159      nitem->next= env->gc_ref;
160      env->gc_ref= nitem;
161    
162      env->gc_count++;
163    
164      return nval;
165    }
166    
167    void gc_mark(value *val) {
168      stackitem *iterator;
169    
170      if(val==NULL || val->gc_garb==0)
171        return;
172    
173      val->gc_garb= 0;
174    
175      if(val->type==list) {
176        iterator= val->content.ptr;
177    
178        while(iterator!=NULL) {
179          gc_mark(iterator->item);
180          iterator= iterator->next;
181        }
182      }
183    }
184    
185    extern void gc_init(environment *env) {
186      stackitem *new_head= NULL, *titem, *iterator= env->gc_ref;
187      symbol *tsymb;
188      int i;
189    
190      while(iterator!=NULL) {
191        iterator->item->gc_garb= 1;
192        iterator= iterator->next;
193      }
194    
195      /* Mark */
196      iterator= env->head;
197      while(iterator!=NULL) {
198        gc_mark(iterator->item);
199        iterator= iterator->next;
200      }
201    
202      for(i= 0; i<HASHTBLSIZE; i++) {
203        tsymb= env->symbols[i];
204        while(tsymb!=NULL) {
205          gc_mark(tsymb->val);
206          tsymb= tsymb->next;
207        }
208      }
209    
210      env->gc_count= 0;
211    
212      /* Sweep */
213      while(env->gc_ref!=NULL) {
214        if(env->gc_ref->item->gc_garb) {
215          switch(env->gc_ref->item->type) {
216          case string:
217            free(env->gc_ref->item->content.ptr);
218            break;
219          case integer:
220            break;
221          case list:
222            while(env->gc_ref->item->content.ptr!=NULL) {
223              titem= env->gc_ref->item->content.ptr;
224              env->gc_ref->item->content.ptr= titem->next;
225              free(titem);
226            }
227            break;
228          default:
229            break;
230          }
231          free(env->gc_ref->item);
232          titem= env->gc_ref->next;
233          free(env->gc_ref);
234          env->gc_ref= titem;
235        } else {
236          titem= env->gc_ref->next;
237          env->gc_ref->next= new_head;
238          new_head= env->gc_ref;
239          env->gc_ref= titem;
240          env->gc_count++;
241        }
242      }
243    
244      env->gc_limit= env->gc_count+20;
245      env->gc_ref= new_head;
246    }
247    
248  /* Push a value onto the stack */  /* Push a value onto the stack */
249  void push_val(environment *env, value *val)  void push_val(environment *env, value *val)
250  {  {
251    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
252    new_item->item= val;    new_item->item= val;
   val->refcount++;  
253    new_item->next= env->head;    new_item->next= env->head;
254    env->head= new_item;    env->head= new_item;
255  }  }
# Line 185  void push_val(environment *env, value *v Line 257  void push_val(environment *env, value *v
257  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
258  void push_int(environment *env, int in_val)  void push_int(environment *env, int in_val)
259  {  {
260    value *new_value= malloc(sizeof(value));    value *new_value= new_val(env);
261        
262    new_value->content.val= in_val;    new_value->content.val= in_val;
263    new_value->type= integer;    new_value->type= integer;
   new_value->refcount= 0;  
264    
265    push_val(env, new_value);    push_val(env, new_value);
266  }  }
# Line 197  void push_int(environment *env, int in_v Line 268  void push_int(environment *env, int in_v
268  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
269  void push_cstring(environment *env, const char *in_string)  void push_cstring(environment *env, const char *in_string)
270  {  {
271    value *new_value= malloc(sizeof(value));    value *new_value= new_val(env);
272    
273    new_value->content.ptr= malloc(strlen(in_string)+1);    new_value->content.ptr= malloc(strlen(in_string)+1);
274    strcpy(new_value->content.ptr, in_string);    strcpy(new_value->content.ptr, in_string);
275    new_value->type= string;    new_value->type= string;
   new_value->refcount= 0;  
276    
277    push_val(env, new_value);    push_val(env, new_value);
278  }  }
# Line 265  void push_sym(environment *env, const ch Line 335  void push_sym(environment *env, const ch
335    const char *dlerr;            /* Dynamic linker error */    const char *dlerr;            /* Dynamic linker error */
336    char *mangled;                /* Mangled function name */    char *mangled;                /* Mangled function name */
337    
338    new_value= malloc(sizeof(value));    new_value= new_val(env);
339    
340    /* The new value is a symbol */    /* The new value is a symbol */
341    new_value->type= symb;    new_value->type= symb;
   new_value->refcount= 1;  
342    
343    /* Look up the symbol name in the hash table */    /* Look up the symbol name in the hash table */
344    new_symbol= hash(env->symbols, in_string);    new_symbol= hash(env->symbols, in_string);
# Line 301  void push_sym(environment *env, const ch Line 370  void push_sym(environment *env, const ch
370        dlerr=dlerror();        dlerr=dlerror();
371      }      }
372      if(dlerr==NULL) {           /* If a function was found */      if(dlerr==NULL) {           /* If a function was found */
373        new_fvalue= malloc(sizeof(value)); /* Create a new value */        new_fvalue= new_val(env); /* Create a new value */
374        new_fvalue->type=func;    /* The new value is a function pointer */        new_fvalue->type=func;    /* The new value is a function pointer */
375        new_fvalue->content.ptr=funcptr; /* Store function pointer */        new_fvalue->content.ptr=funcptr; /* Store function pointer */
376        (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new        (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
377                                           function value */                                           function value */
       new_fvalue->refcount= 1;  
378      }      }
379    }    }
380    push_val(env, new_value);    push_val(env, new_value);
# Line 534  extern void eval(environment *env) Line 602  extern void eval(environment *env)
602      /* If it's a list */      /* If it's a list */
603    case list:    case list:
604      temp_val= env->head->item;      temp_val= env->head->item;
     env->head->item->refcount++;  
605      toss(env);      toss(env);
606      if(env->err) return;      if(env->err) return;
607      iterator= (stackitem*)temp_val->content.ptr;      iterator= (stackitem*)temp_val->content.ptr;
# Line 545  extern void eval(environment *env) Line 612  extern void eval(environment *env)
612          toss(env);          toss(env);
613          if(env->err) return;          if(env->err) return;
614          if(iterator->next == NULL){          if(iterator->next == NULL){
           free_val(temp_val);  
615            goto eval_start;            goto eval_start;
616          }          }
617          eval(env);          eval(env);
# Line 553  extern void eval(environment *env) Line 619  extern void eval(environment *env)
619        }        }
620        iterator= iterator->next;        iterator= iterator->next;
621      }      }
     free_val(temp_val);  
622      return;      return;
623    
624    default:    default:
# Line 618  extern void pack(environment *env) Line 683  extern void pack(environment *env)
683    }    }
684    
685    /* Push list */    /* Push list */
686    pack= malloc(sizeof(value));    pack= new_val(env);
687    pack->type= list;    pack->type= list;
688    pack->content.ptr= temp;    pack->content.ptr= temp;
   pack->refcount= 0;  
689    
690    push_val(env, pack);    push_val(env, pack);
691    rev(env);    rev(env);
# Line 652  extern void expand(environment *env) Line 716  extern void expand(environment *env)
716    /* The first list element is the new stack head */    /* The first list element is the new stack head */
717    new_head= temp= env->head->item->content.ptr;    new_head= temp= env->head->item->content.ptr;
718    
   env->head->item->refcount++;  
719    toss(env);    toss(env);
720    
721    /* Find the end of the list */    /* Find the end of the list */
# Line 738  extern void def(environment *env) Line 801  extern void def(environment *env)
801    sym=env->head->item->content.ptr;    sym=env->head->item->content.ptr;
802    
803    /* 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);  
804    
805    /* Bind the symbol to the value */    /* Bind the symbol to the value */
806    sym->val= env->head->next->item;    sym->val= env->head->next->item;
   sym->val->refcount++;         /* Increase the reference counter */  
807    
808    toss(env); toss(env);    toss(env); toss(env);
809  }  }
810    
811  extern void clear(environment *);  extern void clear(environment *);
812  void forget_sym(symbol **);  void forget_sym(symbol **);
 extern void words(environment *);  
813    
814  /* Quit stack. */  /* Quit stack. */
815  extern void quit(environment *env)  extern void quit(environment *env)
# Line 767  extern void quit(environment *env) Line 826  extern void quit(environment *env)
826      env->symbols[i]= NULL;      env->symbols[i]= NULL;
827    }    }
828    
829      gc_init(env);
830    
831    if(env->free_string!=NULL)    if(env->free_string!=NULL)
832      free(env->free_string);      free(env->free_string);
833        
# Line 804  void forget_sym(symbol **hash_entry) { Line 865  void forget_sym(symbol **hash_entry) {
865    temp= *hash_entry;    temp= *hash_entry;
866    *hash_entry= (*hash_entry)->next;    *hash_entry= (*hash_entry)->next;
867        
   if(temp->val!=NULL) {  
     free_val(temp->val);  
   }  
868    free(temp->id);    free(temp->id);
869    free(temp);    free(temp);
870  }  }
# Line 921  extern void sx_2b(environment *env) { Line 979  extern void sx_2b(environment *env) {
979       && env->head->next->item->type==string) {       && env->head->next->item->type==string) {
980      a_val= env->head->item;      a_val= env->head->item;
981      b_val= env->head->next->item;      b_val= env->head->next->item;
     a_val->refcount++;  
     b_val->refcount++;  
982      toss(env); if(env->err) return;      toss(env); if(env->err) return;
983      toss(env); if(env->err) return;      toss(env); if(env->err) return;
984      len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;      len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
985      new_string= malloc(len);      new_string= malloc(len);
986      strcpy(new_string, b_val->content.ptr);      strcpy(new_string, b_val->content.ptr);
987      strcat(new_string, a_val->content.ptr);      strcat(new_string, a_val->content.ptr);
     free_val(a_val); free_val(b_val);  
988      push_cstring(env, new_string);      push_cstring(env, new_string);
989      free(new_string);      free(new_string);
990      return;      return;
# Line 942  extern void sx_2b(environment *env) { Line 997  extern void sx_2b(environment *env) {
997      return;      return;
998    }    }
999    a=env->head->item->content.val;    a=env->head->item->content.val;
1000    toss(env);    toss(env); if(env->err) return;
1001    if(env->err) return;    
1002    if(env->head->item->refcount == 1)    b=env->head->item->content.val;
1003      env->head->item->content.val += a;    toss(env); if(env->err) return;
1004    else {    push_int(env, a+b);
     b=env->head->item->content.val;  
     toss(env);  
     if(env->err) return;  
     push_int(env, a+b);  
   }  
1005  }  }
1006    
1007  /* "-" */  /* "-" */
# Line 971  extern void sx_2d(environment *env) { Line 1021  extern void sx_2d(environment *env) {
1021      return;      return;
1022    }    }
1023    a=env->head->item->content.val;    a=env->head->item->content.val;
1024    toss(env);    toss(env); if(env->err) return;
1025    if(env->err) return;    b=env->head->item->content.val;
1026    if(env->head->item->refcount == 1)    toss(env); if(env->err) return;
1027      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);  
   }  
1028  }  }
1029    
1030  /* ">" */  /* ">" */
# Line 1000  extern void sx_3e(environment *env) { Line 1044  extern void sx_3e(environment *env) {
1044      return;      return;
1045    }    }
1046    a=env->head->item->content.val;    a=env->head->item->content.val;
1047    toss(env);    toss(env); if(env->err) return;
1048    if(env->err) return;    b=env->head->item->content.val;
1049    if(env->head->item->refcount == 1)    toss(env); if(env->err) return;
1050      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);  
   }  
1051  }  }
1052    
1053  /* Return copy of a value */  /* Return copy of a value */
1054  value *copy_val(value *old_value){  value *copy_val(environment *env, value *old_value){
1055    stackitem *old_item, *new_item, *prev_item;    stackitem *old_item, *new_item, *prev_item;
1056    
1057    value *new_value=malloc(sizeof(value));    value *new_value=new_val(env);
1058    
1059    new_value->type=old_value->type;    new_value->type=old_value->type;
1060    new_value->refcount=0;        /* This is increased if/when this  
                                    value is referenced somewhere, like  
                                    in a stack item or a variable */  
1061    switch(old_value->type){    switch(old_value->type){
1062    case integer:    case integer:
1063      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 1078  value *copy_val(value *old_value){
1078    
1079      while(old_item != NULL) {   /* While list is not empty */      while(old_item != NULL) {   /* While list is not empty */
1080        new_item= malloc(sizeof(stackitem));        new_item= malloc(sizeof(stackitem));
1081        new_item->item=copy_val(old_item->item); /* recurse */        new_item->item=copy_val(env, old_item->item); /* recurse */
1082        new_item->next=NULL;        new_item->next=NULL;
1083        if(prev_item != NULL)     /* If this wasn't the first item */        if(prev_item != NULL)     /* If this wasn't the first item */
1084          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 1100  extern void sx_647570(environment *env)
1100      env->err=1;      env->err=1;
1101      return;      return;
1102    }    }
1103    push_val(env, copy_val(env->head->item));    push_val(env, copy_val(env, env->head->item));
1104  }  }
1105    
1106  /* "if", If-Then */  /* "if", If-Then */
# Line 1147  extern void sx_7768696c65(environment *e Line 1183  extern void sx_7768696c65(environment *e
1183    }    }
1184    
1185    loop= env->head->item;    loop= env->head->item;
   loop->refcount++;  
1186    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1187    
1188    test= env->head->item;    test= env->head->item;
   test->refcount++;  
1189    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1190    
1191    do {    do {
# Line 1175  extern void sx_7768696c65(environment *e Line 1209  extern void sx_7768696c65(environment *e
1209      }      }
1210        
1211    } while(truth);    } while(truth);
   
   free_val(test);  
   free_val(loop);  
1212  }  }
1213    
1214  /* "for"; For-loop */  /* "for"; For-loop */
# Line 1199  extern void sx_666f72(environment *env) Line 1230  extern void sx_666f72(environment *env)
1230    }    }
1231    
1232    loop= env->head->item;    loop= env->head->item;
   loop->refcount++;  
1233    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1234    
1235    foo= env->head->item;    foo= env->head->item;
   foo->refcount++;  
1236    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1237    
1238    iterator= foo->content.ptr;    iterator= foo->content.ptr;
# Line 1214  extern void sx_666f72(environment *env) Line 1243  extern void sx_666f72(environment *env)
1243      eval(env); if(env->err) return;      eval(env); if(env->err) return;
1244      iterator= iterator->next;      iterator= iterator->next;
1245    }    }
   
   free_val(loop);  
   free_val(foo);  
1246  }  }
1247    
1248  /* "to" */  /* "to" */
# Line 1254  extern void to(environment *env) { Line 1280  extern void to(environment *env) {
1280        push_int(env, i);        push_int(env, i);
1281    }    }
1282    
1283    temp_val= malloc(sizeof(value));    temp_val= new_val(env);
1284    temp_val->content.ptr= env->head;    temp_val->content.ptr= env->head;
   temp_val->refcount= 0;  
1285    temp_val->type= list;    temp_val->type= list;
1286    env->head= temp_head;    env->head= temp_head;
1287    push_val(env, temp_val);    push_val(env, temp_val);

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26