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

Diff of /stack/stack.c

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

revision 1.60 by teddy, Fri Feb 8 05:12:37 2002 UTC revision 1.87 by masse, Fri Feb 15 18:27:18 2002 UTC
# Line 1  Line 1 
1  /* printf, sscanf, fgets, fprintf */  /* printf, sscanf, fgets, fprintf, fopen, perror */
2  #include <stdio.h>  #include <stdio.h>
3  /* exit, EXIT_SUCCESS, malloc, free */  /* exit, EXIT_SUCCESS, malloc, free */
4  #include <stdlib.h>  #include <stdlib.h>
# Line 8  Line 8 
8  #include <dlfcn.h>  #include <dlfcn.h>
9  /* strcmp, strcpy, strlen, strcat, strdup */  /* strcmp, strcpy, strlen, strcat, strdup */
10  #include <string.h>  #include <string.h>
11    /* getopt, STDIN_FILENO, STDOUT_FILENO */
12    #include <unistd.h>
13    /* EX_NOINPUT, EX_USAGE */
14    #include <sysexits.h>
15    /* mtrace, muntrace */
16    #include <mcheck.h>
17    
18  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 2048
19    
20  /* First, define some types. */  /* First, define some types. */
21    
# Line 28  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 55  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 */
70    int non_eval_flag;    char *in_string;              /* Input pending to be read */
71      char *free_string;            /* Free this string when all input is
72                                       read from in_string */
73      FILE *inputstream;            /* stdin or a file, most likely */
74      int interactive;              /* print prompts, stack, etc */
75  } environment;  } environment;
76    
77  /* A type for pointers to external functions */  /* A type for pointers to external functions */
# Line 70  void init_env(environment *env) Line 83  void init_env(environment *env)
83  {  {
84    int i;    int i;
85    
86    env->err= 0;    env->gc_limit= 20;
87    env->non_eval_flag= 0;    env->gc_count= 0;
88    
89      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;
92      env->err= 0;
93      env->in_string= NULL;
94      env->free_string= NULL;
95      env->inputstream= stdin;
96      env->interactive= 1;
97  }  }
98    
99  void printerr(const char* in_string) {  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 */  
       }  
       free(val);                /* Free the actual list value */  
       break;  
     case integer:  
     case func:  
     case symb:  
       break;  
     }  
   }  
 }  
   
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 119  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 153  symbol **hash(hashtbl in_hashtbl, const Line 144  symbol **hash(hashtbl in_hashtbl, const
144    }    }
145  }  }
146    
147  /* Generic push function. */  extern void gc_init(environment*);
148  void push(stackitem** stack_head, stackitem* in_item)  
149  {  value* new_val(environment *env) {
150    in_item->next= *stack_head;    value *nval= malloc(sizeof(value));
151    *stack_head= in_item;    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(stackitem **stack_head, 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;
253    val->refcount++;    new_item->next= env->head;
254    push(stack_head, new_item);    env->head= new_item;
255  }  }
256    
257  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
258  void push_int(stackitem **stack_head, 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);
   stackitem *new_item= malloc(sizeof(stackitem));  
   new_item->item= new_value;  
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=1;  
264    
265    push(stack_head, new_item);    push_val(env, new_value);
266  }  }
267    
268  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
269  void push_cstring(stackitem **stack_head, 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);
   stackitem *new_item= malloc(sizeof(stackitem));  
   new_item->item=new_value;  
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=1;  
276    
277    push(stack_head, new_item);    push_val(env, new_value);
278  }  }
279    
280  /* Mangle a symbol name to a valid C identifier name */  /* Mangle a symbol name to a valid C identifier name */
# Line 219  char *mangle_str(const char *old_string) Line 298  char *mangle_str(const char *old_string)
298  }  }
299    
300  extern void mangle(environment *env){  extern void mangle(environment *env){
   value *new_value;  
301    char *new_string;    char *new_string;
302    
303    if((env->head)==NULL) {    if((env->head)==NULL) {
# Line 239  extern void mangle(environment *env){ Line 317  extern void mangle(environment *env){
317    toss(env);    toss(env);
318    if(env->err) return;    if(env->err) return;
319    
320    new_value= malloc(sizeof(value));    push_cstring(env, new_string);
   new_value->content.ptr= new_string;  
   new_value->type= string;  
   new_value->refcount=1;  
   
   push_val(&(env->head), new_value);  
321  }  }
322    
323  /* Push a symbol onto the stack. */  /* Push a symbol onto the stack. */
324  void push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
325  {  {
   stackitem *new_item;          /* The new stack item */  
   /* ...which will contain... */  
326    value *new_value;             /* A new symbol value */    value *new_value;             /* A new symbol value */
327    /* ...which might point to... */    /* ...which might point to... */
328    symbol **new_symbol;          /* (if needed) A new actual symbol */    symbol **new_symbol;          /* (if needed) A new actual symbol */
# Line 264  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    /* Create a new stack item containing a new value */    new_value= new_val(env);
   new_item= malloc(sizeof(stackitem));  
   new_value= malloc(sizeof(value));  
   new_item->item=new_value;  
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 294  void push_sym(environment *env, const ch Line 361  void push_sym(environment *env, const ch
361      if(handle==NULL)            /* If no handle */      if(handle==NULL)            /* If no handle */
362        handle= dlopen(NULL, RTLD_LAZY);        handle= dlopen(NULL, RTLD_LAZY);
363    
364      funcptr= dlsym(handle, in_string); /* Get function pointer */      mangled=mangle_str(in_string); /* mangle the name */
365        funcptr= dlsym(handle, mangled); /* and try to find it */
366        free(mangled);
367      dlerr=dlerror();      dlerr=dlerror();
368      if(dlerr != NULL) {         /* If no function was found */      if(dlerr != NULL) {         /* If no function was found */
369        mangled=mangle_str(in_string);        funcptr= dlsym(handle, in_string); /* Get function pointer */
       funcptr= dlsym(handle, mangled); /* try mangling it */  
       free(mangled);  
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(&(env->head), new_item);    push_val(env, new_value);
381  }  }
382    
383  /* Print newline. */  /* Print newline. */
# Line 351  extern void type(environment *env){ Line 417  extern void type(environment *env){
417  }      }    
418    
419  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
420  void print_h(stackitem *stack_head)  void print_h(stackitem *stack_head, int noquote)
421  {  {
422    switch(stack_head->item->type) {    switch(stack_head->item->type) {
423    case integer:    case integer:
424      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
425      break;      break;
426    case string:    case string:
427      printf("%s", (char*)stack_head->item->content.ptr);      if(noquote)
428          printf("%s", (char*)stack_head->item->content.ptr);
429        else
430          printf("\"%s\"", (char*)stack_head->item->content.ptr);
431      break;      break;
432    case symb:    case symb:
433      printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);      printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);
# Line 371  void print_h(stackitem *stack_head) Line 440  void print_h(stackitem *stack_head)
440      stack_head=(stackitem *)(stack_head->item->content.ptr);      stack_head=(stackitem *)(stack_head->item->content.ptr);
441      printf("[ ");      printf("[ ");
442      while(stack_head != NULL) {      while(stack_head != NULL) {
443        print_h(stack_head);        print_h(stack_head, noquote);
444        printf(" ");        printf(" ");
445        stack_head=stack_head->next;        stack_head=stack_head->next;
446      }      }
# Line 386  extern void print_(environment *env) { Line 455  extern void print_(environment *env) {
455      env->err=1;      env->err=1;
456      return;      return;
457    }    }
458    print_h(env->head);    print_h(env->head, 0);
459      nl();
460  }  }
461    
462  /* Prints the top element of the stack and then discards it. */  /* Prints the top element of the stack and then discards it. */
# Line 397  extern void print(environment *env) Line 467  extern void print(environment *env)
467    toss(env);    toss(env);
468  }  }
469    
470    extern void princ_(environment *env) {
471      if(env->head==NULL) {
472        printerr("Too Few Arguments");
473        env->err=1;
474        return;
475      }
476      print_h(env->head, 1);
477    }
478    
479    /* Prints the top element of the stack and then discards it. */
480    extern void princ(environment *env)
481    {
482      princ_(env);
483      if(env->err) return;
484      toss(env);
485    }
486    
487  /* Only to be called by function printstack. */  /* Only to be called by function printstack. */
488  void print_st(stackitem *stack_head, long counter)  void print_st(stackitem *stack_head, long counter)
489  {  {
490    if(stack_head->next != NULL)    if(stack_head->next != NULL)
491      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
492    printf("%ld: ", counter);    printf("%ld: ", counter);
493    print_h(stack_head);    print_h(stack_head, 0);
494    nl();    nl();
495  }  }
496    
# Line 411  void print_st(stackitem *stack_head, lon Line 498  void print_st(stackitem *stack_head, lon
498  extern void printstack(environment *env)  extern void printstack(environment *env)
499  {  {
500    if(env->head == NULL) {    if(env->head == NULL) {
501        printf("Stack Empty\n");
502      return;      return;
503    }    }
504    print_st(env->head, 1);    print_st(env->head, 1);
   nl();  
505  }  }
506    
507  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 475  extern void rcl(environment *env) Line 562  extern void rcl(environment *env)
562    }    }
563    toss(env);            /* toss the symbol */    toss(env);            /* toss the symbol */
564    if(env->err) return;    if(env->err) return;
565    push_val(&(env->head), val); /* Return its bound value */    push_val(env, val); /* Return its bound value */
566  }  }
567    
 void stack_read(environment*, char*);  
   
568  /* If the top element is a symbol, determine if it's bound to a  /* If the top element is a symbol, determine if it's bound to a
569     function value, and if it is, toss the symbol and execute the     function value, and if it is, toss the symbol and execute the
570     function. */     function. */
# Line 488  extern void eval(environment *env) Line 573  extern void eval(environment *env)
573    funcp in_func;    funcp in_func;
574    value* temp_val;    value* temp_val;
575    stackitem* iterator;    stackitem* iterator;
576    char* temp_string;  
577     eval_start:
578    
579    if(env->head==NULL) {    if(env->head==NULL) {
580      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 502  extern void eval(environment *env) Line 588  extern void eval(environment *env)
588      rcl(env);                   /* get its contents */      rcl(env);                   /* get its contents */
589      if(env->err) return;      if(env->err) return;
590      if(env->head->item->type!=symb){ /* don't recurse symbols */      if(env->head->item->type!=symb){ /* don't recurse symbols */
591        return eval(env);         /* evaluate the value */        goto eval_start;
592      }      }
593      return;      return;
594    
# Line 516  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;
608      while(iterator!=NULL) {      while(iterator!=NULL) {
609        push_val(&(env->head), iterator->item);        push_val(env, iterator->item);
610        if(env->head->item->type==symb        if(env->head->item->type==symb
611          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
612          toss(env);          toss(env);
613          if(env->err) return;          if(env->err) return;
614          if(iterator->next == NULL){          if(iterator->next == NULL){
615            free_val(temp_val);            goto eval_start;
           return eval(env);  
616          }          }
617          eval(env);          eval(env);
618          if(env->err) return;          if(env->err) return;
619        }        }
620        iterator= iterator->next;        iterator= iterator->next;
621      }      }
     free_val(temp_val);  
622      return;      return;
623    
624      /* If it's a string */    default:
   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, "[ ");  
     strcpy(temp_string+2, (char*)temp_val->content.ptr);  
     free_val(temp_val);  
     strcat(temp_string, " ]");  
     stack_read(env, temp_string);  
     free(temp_string);  
     return eval(env);  
   
   case integer:  
625      return;      return;
626    }    }
627  }  }
# Line 588  extern void rev(environment *env){ Line 656  extern void rev(environment *env){
656  /* Make a list. */  /* Make a list. */
657  extern void pack(environment *env)  extern void pack(environment *env)
658  {  {
   void* delimiter;  
659    stackitem *iterator, *temp;    stackitem *iterator, *temp;
660    value *pack;    value *pack;
661    
   delimiter= env->head->item->content.ptr; /* Get delimiter */  
   toss(env);  
   
662    iterator= env->head;    iterator= env->head;
663    
664    if(iterator==NULL || iterator->item->content.ptr==delimiter) {    if(iterator==NULL
665         || (iterator->item->type==symb
666         && ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) {
667      temp= NULL;      temp= NULL;
668      toss(env);      toss(env);
669    } else {    } else {
670      /* Search for first delimiter */      /* Search for first delimiter */
671      while(iterator->next!=NULL      while(iterator->next!=NULL
672            && iterator->next->item->content.ptr!=delimiter)            && (iterator->next->item->type!=symb
673              || ((symbol*)(iterator->next->item->content.ptr))->id[0]!='['))
674        iterator= iterator->next;        iterator= iterator->next;
675            
676      /* Extract list */      /* Extract list */
# Line 616  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= 1;  
   
   temp= malloc(sizeof(stackitem));  
   temp->item= pack;  
689    
690    push(&(env->head), temp);    push_val(env, pack);
691    rev(env);    rev(env);
692  }  }
693    
 /* 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);  
 }  
   
694  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
695  extern void expand(environment *env)  extern void expand(environment *env)
696  {  {
# Line 732  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 763  extern void eq(environment *env) Line 746  extern void eq(environment *env)
746    result= (left==right);    result= (left==right);
747        
748    toss(env); toss(env);    toss(env); toss(env);
749    push_int(&(env->head), result);    push_int(env, result);
750  }  }
751    
752  /* Negates the top element on the stack. */  /* Negates the top element on the stack. */
# Line 785  extern void not(environment *env) Line 768  extern void not(environment *env)
768    
769    val= env->head->item->content.val;    val= env->head->item->content.val;
770    toss(env);    toss(env);
771    push_int(&(env->head), !val);    push_int(env, !val);
772  }  }
773    
774  /* Compares the two top elements on the stack and return 0 if they're the  /* Compares the two top elements on the stack and return 0 if they're the
# Line 818  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 *);
812    void forget_sym(symbol **);
813    
814  /* Quit stack. */  /* Quit stack. */
815  extern void quit(environment *env)  extern void quit(environment *env)
816  {  {
817      long i;
818    
819      clear(env);
820    
821      if (env->err) return;
822      for(i= 0; i<HASHTBLSIZE; i++) {
823        while(env->symbols[i]!= NULL) {
824          forget_sym(&(env->symbols[i]));
825        }
826        env->symbols[i]= NULL;
827      }
828    
829      gc_init(env);
830    
831      if(env->free_string!=NULL)
832        free(env->free_string);
833      
834      muntrace();
835    
836    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
837  }  }
838    
# Line 856  extern void words(environment *env) Line 858  extern void words(environment *env)
858    }    }
859  }  }
860    
861    /* Internal forget function */
862    void forget_sym(symbol **hash_entry) {
863      symbol *temp;
864    
865      temp= *hash_entry;
866      *hash_entry= (*hash_entry)->next;
867      
868      free(temp->id);
869      free(temp);
870    }
871    
872  /* Forgets a symbol (remove it from the hash table) */  /* Forgets a symbol (remove it from the hash table) */
873  extern void forget(environment *env)  extern void forget(environment *env)
874  {  {
875    char* sym_id;    char* sym_id;
876    stackitem *stack_head= env->head;    stackitem *stack_head= env->head;
   symbol **hash_entry, *temp;  
877    
878    if(stack_head==NULL) {    if(stack_head==NULL) {
879      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 878  extern void forget(environment *env) Line 890  extern void forget(environment *env)
890    sym_id= ((symbol*)(stack_head->item->content.ptr))->id;    sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
891    toss(env);    toss(env);
892    
893    hash_entry= hash(env->symbols, sym_id);    return forget_sym(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);  
894  }  }
895    
896  /* Returns the current error number to the stack */  /* Returns the current error number to the stack */
897  extern void errn(environment *env){  extern void errn(environment *env){
898    push_int(&(env->head), env->err);    push_int(env, env->err);
899  }  }
900    
901  int main()  extern void sx_72656164(environment*);
902    
903    int main(int argc, char **argv)
904  {  {
905    environment myenv;    environment myenv;
906    char in_string[100];  
907      int c;                        /* getopt option character */
908    
909      mtrace();
910    
911    init_env(&myenv);    init_env(&myenv);
912    
913    printf("okidok\n ");    myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
914    
915    while(fgets(in_string, 100, stdin) != NULL) {    while ((c = getopt (argc, argv, "i")) != -1)
916      stack_read(&myenv, in_string);      switch (c)
917      if(myenv.err) {        {
918        printf("(error %d) ", myenv.err);        case 'i':
919            myenv.interactive = 1;
920            break;
921          case '?':
922            fprintf (stderr,
923                     "Unknown option character `\\x%x'.\n",
924                     optopt);
925            return EX_USAGE;
926          default:
927            abort ();
928          }
929      
930      if (optind < argc) {
931        myenv.interactive = 0;
932        myenv.inputstream= fopen(argv[optind], "r");
933        if(myenv.inputstream== NULL) {
934          perror(argv[0]);
935          exit (EX_NOINPUT);
936        }
937      }
938    
939      while(1) {
940        if(myenv.in_string==NULL) {
941          if (myenv.interactive) {
942            if(myenv.err) {
943              printf("(error %d)\n", myenv.err);
944            }
945            nl();
946            printstack(&myenv);
947            printf("> ");
948          }
949        myenv.err=0;        myenv.err=0;
950      }      }
951      printf("okidok\n ");      sx_72656164(&myenv);
952        if (myenv.err==4) {
953          return EX_NOINPUT;
954        } else if(myenv.head!=NULL
955                  && myenv.head->item->type==symb
956                  && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') {
957          toss(&myenv);             /* No error check in main */
958          eval(&myenv);
959        }
960    }    }
961    quit(&myenv);    quit(&myenv);
962    return EXIT_FAILURE;    return EXIT_FAILURE;
963  }  }
964    
965  /* + */  /* "+" */
966  extern void sx_2b(environment *env) {  extern void sx_2b(environment *env) {
967    int a, b;    int a, b;
968    size_t len;    size_t len;
# Line 932  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);
988      free_val(a_val); free_val(b_val);      push_cstring(env, new_string);
     push_cstring(&(env->head), new_string);  
989      free(new_string);      free(new_string);
990      return;      return;
991    }    }
# Line 953  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    b=env->head->item->content.val;    b=env->head->item->content.val;
1003    toss(env);    toss(env); if(env->err) return;
1004    if(env->err) return;    push_int(env, a+b);
   push_int(&(env->head), a+b);  
1005  }  }
1006    
1007  /* - */  /* "-" */
1008  extern void sx_2d(environment *env) {  extern void sx_2d(environment *env) {
1009    int a;    int a, b;
1010    
1011    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
1012      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 978  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    env->head->item->content.val -= a;    toss(env); if(env->err) return;
1027      push_int(env, b-a);
1028    }
1029    
1030    /* ">" */
1031    extern void sx_3e(environment *env) {
1032      int a, b;
1033    
1034      if((env->head)==NULL || env->head->next==NULL) {
1035        printerr("Too Few Arguments");
1036        env->err=1;
1037        return;
1038      }
1039      
1040      if(env->head->item->type!=integer
1041         || env->head->next->item->type!=integer) {
1042        printerr("Bad Argument Type");
1043        env->err=2;
1044        return;
1045      }
1046      a=env->head->item->content.val;
1047      toss(env); if(env->err) return;
1048      b=env->head->item->content.val;
1049      toss(env); if(env->err) return;
1050      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 1013  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 1028  value *copy_val(value *old_value){ Line 1093  value *copy_val(value *old_value){
1093    return new_value;    return new_value;
1094  }  }
1095    
1096  /* duplicates an item on the stack */  /* "dup"; duplicates an item on the stack */
1097  extern void dup(environment *env) {  extern void sx_647570(environment *env) {
1098    if((env->head)==NULL) {    if((env->head)==NULL) {
1099      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1100      env->err=1;      env->err=1;
1101      return;      return;
1102    }    }
1103    push_val(&(env->head), copy_val(env->head->item));    push_val(env, copy_val(env, env->head->item));
1104  }  }
1105    
1106  /* "if", If-Then */  /* "if", If-Then */
# Line 1105  extern void ifelse(environment *env) { Line 1170  extern void ifelse(environment *env) {
1170    eval(env);    eval(env);
1171  }  }
1172    
1173  /* while */  /* "while" */
1174  extern void sx_7768696c65(environment *env) {  extern void sx_7768696c65(environment *env) {
1175    
1176    int truth;    int truth;
1177      value *loop, *test;
1178    
1179    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
1180      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 1116  extern void sx_7768696c65(environment *e Line 1182  extern void sx_7768696c65(environment *e
1182      return;      return;
1183    }    }
1184    
1185      loop= env->head->item;
1186      toss(env); if(env->err) return;
1187    
1188      test= env->head->item;
1189      toss(env); if(env->err) return;
1190    
1191    do {    do {
1192      swap(env); if(env->err) return;      push_val(env, test);
1193      dup(env); if(env->err) return;      eval(env);
     eval(env); if(env->err) return;  
1194            
1195      if(env->head->item->type != integer) {      if(env->head->item->type != integer) {
1196        printerr("Bad Argument Type");        printerr("Bad Argument Type");
# Line 1128  extern void sx_7768696c65(environment *e Line 1199  extern void sx_7768696c65(environment *e
1199      }      }
1200            
1201      truth= env->head->item->content.val;      truth= env->head->item->content.val;
       
1202      toss(env); if(env->err) return;      toss(env); if(env->err) return;
     swap(env); if(env->err) return;  
1203            
1204      if(truth) {      if(truth) {
1205        dup(env);        push_val(env, loop);
1206        eval(env);        eval(env);
1207      } else {      } else {
1208        toss(env);        toss(env);
       toss(env);  
1209      }      }
1210        
1211    } while(truth);    } while(truth);
1212  }  }
1213    
1214    /* "for"; For-loop */
1215    extern void sx_666f72(environment *env) {
1216      
1217      value *loop, *foo;
1218      stackitem *iterator;
1219      
1220      if((env->head)==NULL || env->head->next==NULL) {
1221        printerr("Too Few Arguments");
1222        env->err=1;
1223        return;
1224      }
1225    
1226      if(env->head->next->item->type != list) {
1227        printerr("Bad Argument Type");
1228        env->err=2;
1229        return;
1230      }
1231    
1232      loop= env->head->item;
1233      toss(env); if(env->err) return;
1234    
1235      foo= env->head->item;
1236      toss(env); if(env->err) return;
1237    
1238      iterator= foo->content.ptr;
1239    
1240      while(iterator!=NULL) {
1241        push_val(env, iterator->item);
1242        push_val(env, loop);
1243        eval(env); if(env->err) return;
1244        iterator= iterator->next;
1245      }
1246    }
1247    
1248    /* "to" */
1249    extern void to(environment *env) {
1250      int i, start, ending;
1251      stackitem *temp_head;
1252      value *temp_val;
1253      
1254      if((env->head)==NULL || env->head->next==NULL) {
1255        printerr("Too Few Arguments");
1256        env->err=1;
1257        return;
1258      }
1259    
1260      if(env->head->item->type!=integer
1261         || env->head->next->item->type!=integer) {
1262        printerr("Bad Argument Type");
1263        env->err=2;
1264        return;
1265      }
1266    
1267      ending= env->head->item->content.val;
1268      toss(env); if(env->err) return;
1269      start= env->head->item->content.val;
1270      toss(env); if(env->err) return;
1271    
1272      temp_head= env->head;
1273      env->head= NULL;
1274    
1275      if(ending>=start) {
1276        for(i= ending; i>=start; i--)
1277          push_int(env, i);
1278      } else {
1279        for(i= ending; i<=start; i++)
1280          push_int(env, i);
1281      }
1282    
1283      temp_val= new_val(env);
1284      temp_val->content.ptr= env->head;
1285      temp_val->type= list;
1286      env->head= temp_head;
1287      push_val(env, temp_val);
1288    }
1289    
1290    /* Read a string */
1291    extern void readline(environment *env) {
1292      char in_string[101];
1293    
1294      if(fgets(in_string, 100, env->inputstream)==NULL)
1295        push_cstring(env, "");
1296      else
1297        push_cstring(env, in_string);
1298    }
1299    
1300    /* "read"; Read a value and place on stack */
1301    extern void sx_72656164(environment *env) {
1302      const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";
1303      const char strform[]= "\"%[^\"]\"%n";
1304      const char intform[]= "%i%n";
1305      const char blankform[]= "%*[ \t]%n";
1306      const char ebrackform[]= "%*1[]]%n";
1307      const char semicform[]= "%*1[;]%n";
1308      const char bbrackform[]= "%*1[[]%n";
1309    
1310      int itemp, readlength= -1;
1311      static int depth= 0;
1312      char *match;
1313      size_t inlength;
1314    
1315      if(env->in_string==NULL) {
1316        if(depth > 0 && env->interactive) {
1317          printf("]> ");
1318        }
1319        readline(env); if(env->err) return;
1320    
1321        if(((char *)(env->head->item->content.ptr))[0]=='\0'){
1322          env->err= 4;              /* "" means EOF */
1323          return;
1324        }
1325        
1326        env->in_string= malloc(strlen(env->head->item->content.ptr)+1);
1327        env->free_string= env->in_string; /* Save the original pointer */
1328        strcpy(env->in_string, env->head->item->content.ptr);
1329        toss(env); if(env->err) return;
1330      }
1331      
1332      inlength= strlen(env->in_string)+1;
1333      match= malloc(inlength);
1334    
1335      if(sscanf(env->in_string, blankform, &readlength)!=EOF
1336         && readlength != -1) {
1337        ;
1338      } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF
1339                && readlength != -1) {
1340        push_int(env, itemp);
1341      } else if(sscanf(env->in_string, strform, match, &readlength) != EOF
1342                && readlength != -1) {
1343        push_cstring(env, match);
1344      } else if(sscanf(env->in_string, symbform, match, &readlength) != EOF
1345                && readlength != -1) {
1346        push_sym(env, match);
1347      } else if(sscanf(env->in_string, ebrackform, &readlength) != EOF
1348                && readlength != -1) {
1349        pack(env); if(env->err) return;
1350        if(depth != 0) depth--;
1351      } else if(sscanf(env->in_string, semicform, &readlength) != EOF
1352                && readlength != -1) {
1353        push_sym(env, ";");
1354      } else if(sscanf(env->in_string, bbrackform, &readlength) != EOF
1355                && readlength != -1) {
1356        push_sym(env, "[");
1357        depth++;
1358      } else {
1359        free(env->free_string);
1360        env->in_string = env->free_string = NULL;
1361      }
1362      if ( env->in_string != NULL) {
1363        env->in_string += readlength;
1364      }
1365    
1366      free(match);
1367    
1368      if(depth)
1369        return sx_72656164(env);
1370    }

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26