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

Diff of /stack/stack.c

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

revision 1.68 by masse, Mon Feb 11 00:27:18 2002 UTC revision 1.88 by teddy, Sat Feb 16 00:51:32 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  #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 */  
   int non_eval_flag;  
 } 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->err= 0;    env->gc_limit= 20;
26    env->non_eval_flag= 0;    env->gc_count= 0;
27    
28      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;
31      env->err= 0;
32      env->in_string= NULL;
33      env->free_string= NULL;
34      env->inputstream= stdin;
35      env->interactive= 1;
36  }  }
37    
38  void printerr(const char* in_string) {  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 */  
       }  
       free(val);                /* Free the actual list value */  
       break;  
     case integer:  
     case func:  
     case symb:  
       break;  
     }  
   }  
 }  
   
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 119  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 153  symbol **hash(hashtbl in_hashtbl, const Line 83  symbol **hash(hashtbl in_hashtbl, const
83    }    }
84  }  }
85    
86  /* Generic push function. */  value* new_val(environment *env) {
87  void push(stackitem** stack_head, stackitem* in_item)    value *nval= malloc(sizeof(value));
88  {    stackitem *nitem= malloc(sizeof(stackitem));
89    in_item->next= *stack_head;  
90    *stack_head= in_item;    if(env->gc_count >= env->gc_limit)
91        gc_init(env);
92    
93      nval->content.ptr= NULL;
94    
95      nitem->item= nval;
96      nitem->next= env->gc_ref;
97      env->gc_ref= nitem;
98    
99      env->gc_count++;
100    
101      return nval;
102    }
103    
104    void gc_mark(value *val) {
105      stackitem *iterator;
106    
107      if(val==NULL || val->gc_garb==0)
108        return;
109    
110      val->gc_garb= 0;
111    
112      if(val->type==list) {
113        iterator= val->content.ptr;
114    
115        while(iterator!=NULL) {
116          gc_mark(iterator->item);
117          iterator= iterator->next;
118        }
119      }
120    }
121    
122    extern void gc_init(environment *env) {
123      stackitem *new_head= NULL, *titem, *iterator= env->gc_ref;
124      symbol *tsymb;
125      int i;
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+20;
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(stackitem **stack_head, 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;
190    val->refcount++;    new_item->next= env->head;
191    push(stack_head, new_item);    env->head= new_item;
192  }  }
193    
194  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
195  void push_int(stackitem **stack_head, 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);
   stackitem *new_item= malloc(sizeof(stackitem));  
   new_item->item= new_value;  
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=1;  
201    
202    push(stack_head, new_item);    push_val(env, new_value);
203  }  }
204    
205  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
206  void push_cstring(stackitem **stack_head, 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);
   stackitem *new_item= malloc(sizeof(stackitem));  
   new_item->item=new_value;  
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=1;  
213    
214    push(stack_head, new_item);    push_val(env, new_value);
215  }  }
216    
217  /* 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 235  char *mangle_str(const char *old_string)
235  }  }
236    
237  extern void mangle(environment *env){  extern void mangle(environment *env){
   value *new_value;  
238    char *new_string;    char *new_string;
239    
240    if((env->head)==NULL) {    if((env->head)==NULL) {
# Line 239  extern void mangle(environment *env){ Line 254  extern void mangle(environment *env){
254    toss(env);    toss(env);
255    if(env->err) return;    if(env->err) return;
256    
257    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);  
258  }  }
259    
260  /* Push a symbol onto the stack. */  /* Push a symbol onto the stack. */
261  void push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
262  {  {
   stackitem *new_item;          /* The new stack item */  
   /* ...which will contain... */  
263    value *new_value;             /* A new symbol value */    value *new_value;             /* A new symbol value */
264    /* ...which might point to... */    /* ...which might point to... */
265    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 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    /* 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;  
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 294  void push_sym(environment *env, const ch Line 298  void push_sym(environment *env, const ch
298      if(handle==NULL)            /* If no handle */      if(handle==NULL)            /* If no handle */
299        handle= dlopen(NULL, RTLD_LAZY);        handle= dlopen(NULL, RTLD_LAZY);
300    
301      funcptr= dlsym(handle, in_string); /* Get function pointer */      mangled=mangle_str(in_string); /* mangle the name */
302        funcptr= dlsym(handle, mangled); /* and try to find it */
303        free(mangled);
304      dlerr=dlerror();      dlerr=dlerror();
305      if(dlerr != NULL) {         /* If no function was found */      if(dlerr != NULL) {         /* If no function was found */
306        mangled=mangle_str(in_string);        funcptr= dlsym(handle, in_string); /* Get function pointer */
       funcptr= dlsym(handle, mangled); /* try mangling it */  
       free(mangled);  
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(&(env->head), new_item);    push_val(env, new_value);
318  }  }
319    
320  /* Print newline. */  /* Print newline. */
# Line 351  extern void type(environment *env){ Line 354  extern void type(environment *env){
354  }      }    
355    
356  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
357  void print_h(stackitem *stack_head)  void print_h(stackitem *stack_head, int noquote)
358  {  {
359    switch(stack_head->item->type) {    switch(stack_head->item->type) {
360    case integer:    case integer:
361      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
362      break;      break;
363    case string:    case string:
364      printf("%s", (char*)stack_head->item->content.ptr);      if(noquote)
365          printf("%s", (char*)stack_head->item->content.ptr);
366        else
367          printf("\"%s\"", (char*)stack_head->item->content.ptr);
368      break;      break;
369    case symb:    case symb:
370      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 377  void print_h(stackitem *stack_head)
377      stack_head=(stackitem *)(stack_head->item->content.ptr);      stack_head=(stackitem *)(stack_head->item->content.ptr);
378      printf("[ ");      printf("[ ");
379      while(stack_head != NULL) {      while(stack_head != NULL) {
380        print_h(stack_head);        print_h(stack_head, noquote);
381        printf(" ");        printf(" ");
382        stack_head=stack_head->next;        stack_head=stack_head->next;
383      }      }
# Line 386  extern void print_(environment *env) { Line 392  extern void print_(environment *env) {
392      env->err=1;      env->err=1;
393      return;      return;
394    }    }
395    print_h(env->head);    print_h(env->head, 0);
396      nl();
397  }  }
398    
399  /* 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 404  extern void print(environment *env)
404    toss(env);    toss(env);
405  }  }
406    
407    extern void princ_(environment *env) {
408      if(env->head==NULL) {
409        printerr("Too Few Arguments");
410        env->err=1;
411        return;
412      }
413      print_h(env->head, 1);
414    }
415    
416    /* Prints the top element of the stack and then discards it. */
417    extern void princ(environment *env)
418    {
419      princ_(env);
420      if(env->err) return;
421      toss(env);
422    }
423    
424  /* Only to be called by function printstack. */  /* Only to be called by function printstack. */
425  void print_st(stackitem *stack_head, long counter)  void print_st(stackitem *stack_head, long counter)
426  {  {
427    if(stack_head->next != NULL)    if(stack_head->next != NULL)
428      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
429    printf("%ld: ", counter);    printf("%ld: ", counter);
430    print_h(stack_head);    print_h(stack_head, 0);
431    nl();    nl();
432  }  }
433    
# Line 411  void print_st(stackitem *stack_head, lon Line 435  void print_st(stackitem *stack_head, lon
435  extern void printstack(environment *env)  extern void printstack(environment *env)
436  {  {
437    if(env->head == NULL) {    if(env->head == NULL) {
438        printf("Stack Empty\n");
439      return;      return;
440    }    }
441    print_st(env->head, 1);    print_st(env->head, 1);
   nl();  
442  }  }
443    
444  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 475  extern void rcl(environment *env) Line 499  extern void rcl(environment *env)
499    }    }
500    toss(env);            /* toss the symbol */    toss(env);            /* toss the symbol */
501    if(env->err) return;    if(env->err) return;
502    push_val(&(env->head), val); /* Return its bound value */    push_val(env, val); /* Return its bound value */
503  }  }
504    
 void stack_read(environment*, char*);  
   
505  /* 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
506     function value, and if it is, toss the symbol and execute the     function value, and if it is, toss the symbol and execute the
507     function. */     function. */
# Line 488  extern void eval(environment *env) Line 510  extern void eval(environment *env)
510    funcp in_func;    funcp in_func;
511    value* temp_val;    value* temp_val;
512    stackitem* iterator;    stackitem* iterator;
513    char* temp_string;  
514     eval_start:
515    
516    if(env->head==NULL) {    if(env->head==NULL) {
517      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 496  extern void eval(environment *env) Line 519  extern void eval(environment *env)
519      return;      return;
520    }    }
521    
  eval_start:  
   
522    switch(env->head->item->type) {    switch(env->head->item->type) {
523      /* if it's a symbol */      /* if it's a symbol */
524    case symb:    case symb:
# Line 518  extern void eval(environment *env) Line 539  extern void eval(environment *env)
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;
545      while(iterator!=NULL) {      while(iterator!=NULL) {
546        push_val(&(env->head), iterator->item);        push_val(env, iterator->item);
547        if(env->head->item->type==symb        if(env->head->item->type==symb
548          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
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 537  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      /* 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);  
     goto eval_start;  
   
   case integer:  
562      return;      return;
563    }    }
564  }  }
# Line 590  extern void rev(environment *env){ Line 593  extern void rev(environment *env){
593  /* Make a list. */  /* Make a list. */
594  extern void pack(environment *env)  extern void pack(environment *env)
595  {  {
   void* delimiter;  
596    stackitem *iterator, *temp;    stackitem *iterator, *temp;
597    value *pack;    value *pack;
598    
   delimiter= env->head->item->content.ptr; /* Get delimiter */  
   toss(env);  
   
599    iterator= env->head;    iterator= env->head;
600    
601    if(iterator==NULL || iterator->item->content.ptr==delimiter) {    if(iterator==NULL
602         || (iterator->item->type==symb
603         && ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) {
604      temp= NULL;      temp= NULL;
605      toss(env);      toss(env);
606    } else {    } else {
607      /* Search for first delimiter */      /* Search for first delimiter */
608      while(iterator->next!=NULL      while(iterator->next!=NULL
609            && iterator->next->item->content.ptr!=delimiter)            && (iterator->next->item->type!=symb
610              || ((symbol*)(iterator->next->item->content.ptr))->id[0]!='['))
611        iterator= iterator->next;        iterator= iterator->next;
612            
613      /* Extract list */      /* Extract list */
# 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= 1;  
626    
627    temp= malloc(sizeof(stackitem));    push_val(env, pack);
   temp->item= pack;  
   
   push(&(env->head), temp);  
628    rev(env);    rev(env);
629  }  }
630    
 /* 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);  
 }  
   
631  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
632  extern void expand(environment *env)  extern void expand(environment *env)
633  {  {
# Line 734  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 765  extern void eq(environment *env) Line 683  extern void eq(environment *env)
683    result= (left==right);    result= (left==right);
684        
685    toss(env); toss(env);    toss(env); toss(env);
686    push_int(&(env->head), result);    push_int(env, result);
687  }  }
688    
689  /* Negates the top element on the stack. */  /* Negates the top element on the stack. */
# Line 787  extern void not(environment *env) Line 705  extern void not(environment *env)
705    
706    val= env->head->item->content.val;    val= env->head->item->content.val;
707    toss(env);    toss(env);
708    push_int(&(env->head), !val);    push_int(env, !val);
709  }  }
710    
711  /* 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 820  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  }  }
# Line 833  extern void def(environment *env) Line 748  extern void def(environment *env)
748  /* Quit stack. */  /* Quit stack. */
749  extern void quit(environment *env)  extern void quit(environment *env)
750  {  {
751      long i;
752    
753      clear(env);
754    
755      if (env->err) return;
756      for(i= 0; i<HASHTBLSIZE; i++) {
757        while(env->symbols[i]!= NULL) {
758          forget_sym(&(env->symbols[i]));
759        }
760        env->symbols[i]= NULL;
761      }
762    
763      gc_init(env);
764    
765      if(env->free_string!=NULL)
766        free(env->free_string);
767      
768      muntrace();
769    
770    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
771  }  }
772    
# Line 858  extern void words(environment *env) Line 792  extern void words(environment *env)
792    }    }
793  }  }
794    
795    /* Internal forget function */
796    void forget_sym(symbol **hash_entry) {
797      symbol *temp;
798    
799      temp= *hash_entry;
800      *hash_entry= (*hash_entry)->next;
801      
802      free(temp->id);
803      free(temp);
804    }
805    
806  /* Forgets a symbol (remove it from the hash table) */  /* Forgets a symbol (remove it from the hash table) */
807  extern void forget(environment *env)  extern void forget(environment *env)
808  {  {
809    char* sym_id;    char* sym_id;
810    stackitem *stack_head= env->head;    stackitem *stack_head= env->head;
   symbol **hash_entry, *temp;  
811    
812    if(stack_head==NULL) {    if(stack_head==NULL) {
813      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 880  extern void forget(environment *env) Line 824  extern void forget(environment *env)
824    sym_id= ((symbol*)(stack_head->item->content.ptr))->id;    sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
825    toss(env);    toss(env);
826    
827    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);  
828  }  }
829    
830  /* Returns the current error number to the stack */  /* Returns the current error number to the stack */
831  extern void errn(environment *env){  extern void errn(environment *env){
832    push_int(&(env->head), env->err);    push_int(env, env->err);
833  }  }
834    
835  int main()  int main(int argc, char **argv)
836  {  {
837    environment myenv;    environment myenv;
838    char in_string[100];  
839      int c;                        /* getopt option character */
840    
841      mtrace();
842    
843    init_env(&myenv);    init_env(&myenv);
844    
845    printf("okidok\n ");    myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
846    
847      while ((c = getopt (argc, argv, "i")) != -1)
848        switch (c)
849          {
850          case 'i':
851            myenv.interactive = 1;
852            break;
853          case '?':
854            fprintf (stderr,
855                     "Unknown option character `\\x%x'.\n",
856                     optopt);
857            return EX_USAGE;
858          default:
859            abort ();
860          }
861      
862      if (optind < argc) {
863        myenv.interactive = 0;
864        myenv.inputstream= fopen(argv[optind], "r");
865        if(myenv.inputstream== NULL) {
866          perror(argv[0]);
867          exit (EX_NOINPUT);
868        }
869      }
870    
871    while(fgets(in_string, 100, stdin) != NULL) {    while(1) {
872      stack_read(&myenv, in_string);      if(myenv.in_string==NULL) {
873      if(myenv.err) {        if (myenv.interactive) {
874        printf("(error %d) ", myenv.err);          if(myenv.err) {
875              printf("(error %d)\n", myenv.err);
876            }
877            nl();
878            printstack(&myenv);
879            printf("> ");
880          }
881        myenv.err=0;        myenv.err=0;
882      }      }
883      printf("okidok\n ");      sx_72656164(&myenv);
884        if (myenv.err==4) {
885          return EX_NOINPUT;
886        } else if(myenv.head!=NULL
887                  && myenv.head->item->type==symb
888                  && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') {
889          toss(&myenv);             /* No error check in main */
890          eval(&myenv);
891        }
892    }    }
893    quit(&myenv);    quit(&myenv);
894    return EXIT_FAILURE;    return EXIT_FAILURE;
895  }  }
896    
897  /* + */  /* "+" */
898  extern void sx_2b(environment *env) {  extern void sx_2b(environment *env) {
899    int a, b;    int a, b;
900    size_t len;    size_t len;
# Line 934  extern void sx_2b(environment *env) { Line 911  extern void sx_2b(environment *env) {
911       && env->head->next->item->type==string) {       && env->head->next->item->type==string) {
912      a_val= env->head->item;      a_val= env->head->item;
913      b_val= env->head->next->item;      b_val= env->head->next->item;
     a_val->refcount++;  
     b_val->refcount++;  
914      toss(env); if(env->err) return;      toss(env); if(env->err) return;
915      toss(env); if(env->err) return;      toss(env); if(env->err) return;
916      len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;      len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
917      new_string= malloc(len);      new_string= malloc(len);
918      strcpy(new_string, b_val->content.ptr);      strcpy(new_string, b_val->content.ptr);
919      strcat(new_string, a_val->content.ptr);      strcat(new_string, a_val->content.ptr);
920      free_val(a_val); free_val(b_val);      push_cstring(env, new_string);
     push_cstring(&(env->head), new_string);  
921      free(new_string);      free(new_string);
922      return;      return;
923    }    }
# Line 955  extern void sx_2b(environment *env) { Line 929  extern void sx_2b(environment *env) {
929      return;      return;
930    }    }
931    a=env->head->item->content.val;    a=env->head->item->content.val;
932    toss(env);    toss(env); if(env->err) return;
933    if(env->err) return;    
934    if(env->head->item->refcount == 1)    b=env->head->item->content.val;
935      env->head->item->content.val += a;    toss(env); if(env->err) return;
936    else {    push_int(env, a+b);
     b=env->head->item->content.val;  
     toss(env);  
     if(env->err) return;  
     push_int(&(env->head), a+b);  
   }  
937  }  }
938    
939  /* - */  /* "-" */
940  extern void sx_2d(environment *env) {  extern void sx_2d(environment *env) {
941    int a, b;    int a, b;
942    
# Line 984  extern void sx_2d(environment *env) { Line 953  extern void sx_2d(environment *env) {
953      return;      return;
954    }    }
955    a=env->head->item->content.val;    a=env->head->item->content.val;
956    toss(env);    toss(env); if(env->err) return;
957    if(env->err) return;    b=env->head->item->content.val;
958    if(env->head->item->refcount == 1)    toss(env); if(env->err) return;
959      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->head), b-a);  
   }  
960  }  }
961    
962  /* > */  /* ">" */
963  extern void sx_3e(environment *env) {  extern void sx_3e(environment *env) {
964    int a, b;    int a, b;
965    
# Line 1013  extern void sx_3e(environment *env) { Line 976  extern void sx_3e(environment *env) {
976      return;      return;
977    }    }
978    a=env->head->item->content.val;    a=env->head->item->content.val;
979    toss(env);    toss(env); if(env->err) return;
980    if(env->err) return;    b=env->head->item->content.val;
981    if(env->head->item->refcount == 1)    toss(env); if(env->err) return;
982      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->head), b>a);  
   }  
983  }  }
984    
985  /* Return copy of a value */  /* Return copy of a value */
986  value *copy_val(value *old_value){  value *copy_val(environment *env, value *old_value){
987    stackitem *old_item, *new_item, *prev_item;    stackitem *old_item, *new_item, *prev_item;
988    
989    value *new_value=malloc(sizeof(value));    value *new_value=new_val(env);
990    
991    new_value->type=old_value->type;    new_value->type=old_value->type;
992    new_value->refcount=0;        /* This is increased if/when this  
                                    value is referenced somewhere, like  
                                    in a stack item or a variable */  
993    switch(old_value->type){    switch(old_value->type){
994    case integer:    case integer:
995      new_value->content.val=old_value->content.val;      new_value->content.val=old_value->content.val;
# Line 1055  value *copy_val(value *old_value){ Line 1010  value *copy_val(value *old_value){
1010    
1011      while(old_item != NULL) {   /* While list is not empty */      while(old_item != NULL) {   /* While list is not empty */
1012        new_item= malloc(sizeof(stackitem));        new_item= malloc(sizeof(stackitem));
1013        new_item->item=copy_val(old_item->item); /* recurse */        new_item->item=copy_val(env, old_item->item); /* recurse */
1014        new_item->next=NULL;        new_item->next=NULL;
1015        if(prev_item != NULL)     /* If this wasn't the first item */        if(prev_item != NULL)     /* If this wasn't the first item */
1016          prev_item->next=new_item; /* point the previous item to the          prev_item->next=new_item; /* point the previous item to the
# Line 1070  value *copy_val(value *old_value){ Line 1025  value *copy_val(value *old_value){
1025    return new_value;    return new_value;
1026  }  }
1027    
1028  /* duplicates an item on the stack */  /* "dup"; duplicates an item on the stack */
1029  extern void dup(environment *env) {  extern void sx_647570(environment *env) {
1030    if((env->head)==NULL) {    if((env->head)==NULL) {
1031      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1032      env->err=1;      env->err=1;
1033      return;      return;
1034    }    }
1035    push_val(&(env->head), copy_val(env->head->item));    push_val(env, copy_val(env, env->head->item));
1036  }  }
1037    
1038  /* "if", If-Then */  /* "if", If-Then */
# Line 1147  extern void ifelse(environment *env) { Line 1102  extern void ifelse(environment *env) {
1102    eval(env);    eval(env);
1103  }  }
1104    
1105  /* while */  /* "while" */
1106  extern void sx_7768696c65(environment *env) {  extern void sx_7768696c65(environment *env) {
1107    
1108    int truth;    int truth;
# Line 1160  extern void sx_7768696c65(environment *e Line 1115  extern void sx_7768696c65(environment *e
1115    }    }
1116    
1117    loop= env->head->item;    loop= env->head->item;
   loop->refcount++;  
1118    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1119    
1120    test= env->head->item;    test= env->head->item;
   test->refcount++;  
1121    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1122    
1123    do {    do {
1124      push_val(&(env->head), test);      push_val(env, test);
1125      eval(env);      eval(env);
1126            
1127      if(env->head->item->type != integer) {      if(env->head->item->type != integer) {
# Line 1181  extern void sx_7768696c65(environment *e Line 1134  extern void sx_7768696c65(environment *e
1134      toss(env); if(env->err) return;      toss(env); if(env->err) return;
1135            
1136      if(truth) {      if(truth) {
1137        push_val(&(env->head), loop);        push_val(env, loop);
1138        eval(env);        eval(env);
1139      } else {      } else {
1140        toss(env);        toss(env);
1141      }      }
1142        
1143    } while(truth);    } while(truth);
   
   free_val(test);  
   free_val(loop);  
1144  }  }
1145    
1146  /* For-loop */  /* "for"; For-loop */
1147  extern void sx_666f72(environment *env) {  extern void sx_666f72(environment *env) {
1148        
1149    value *loop, *foo;    value *loop, *foo;
# Line 1212  extern void sx_666f72(environment *env) Line 1162  extern void sx_666f72(environment *env)
1162    }    }
1163    
1164    loop= env->head->item;    loop= env->head->item;
   loop->refcount++;  
1165    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1166    
1167    foo= env->head->item;    foo= env->head->item;
   foo->refcount++;  
1168    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1169    
1170    iterator= foo->content.ptr;    iterator= foo->content.ptr;
1171    
1172    while(iterator!=NULL) {    while(iterator!=NULL) {
1173      push_val(&(env->head), iterator->item);      push_val(env, iterator->item);
1174      push_val(&(env->head), loop);      push_val(env, loop);
1175      eval(env); if(env->err) return;      eval(env); if(env->err) return;
1176      iterator= iterator->next;      iterator= iterator->next;
1177    }    }
   
   free_val(loop);  
   free_val(foo);  
1178  }  }
1179    
1180  /* 'to' */  /* "to" */
1181  extern void to(environment *env) {  extern void to(environment *env) {
1182    int i, start, ending;    int i, start, ending;
1183      stackitem *temp_head;
1184      value *temp_val;
1185        
1186    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
1187      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 1254  extern void to(environment *env) { Line 1201  extern void to(environment *env) {
1201    start= env->head->item->content.val;    start= env->head->item->content.val;
1202    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1203    
1204    push_sym(env, "[");    temp_head= env->head;
1205      env->head= NULL;
1206    
1207    if(ending>=start) {    if(ending>=start) {
1208      for(i= start; i<=ending; i++)      for(i= ending; i>=start; i--)
1209        push_int(&(env->head), i);        push_int(env, i);
1210    } else {    } else {
1211      for(i= start; i>=ending; i--)      for(i= ending; i<=start; i++)
1212        push_int(&(env->head), i);        push_int(env, i);
1213    }    }
1214    
1215    push_sym(env, "[");    temp_val= new_val(env);
1216    pack(env); if(env->err) return;    temp_val->content.ptr= env->head;
1217      temp_val->type= list;
1218      env->head= temp_head;
1219      push_val(env, temp_val);
1220  }  }
1221    
1222  /* Read a string */  /* Read a string */
1223  extern void readline(environment *env) {  extern void readline(environment *env) {
1224    char in_string[101];    char in_string[101];
1225    
1226    fgets(in_string, 100, stdin);    if(fgets(in_string, 100, env->inputstream)==NULL)
1227    push_cstring(&(env->head), in_string);      push_cstring(env, "");
1228      else
1229        push_cstring(env, in_string);
1230  }  }
1231    
1232  /* Read a value and place on stack */  /* "read"; Read a value and place on stack */
1233  extern void read(environment *env) {  extern void sx_72656164(environment *env) {
1234    const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%100c";    const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";
1235    const char strform[]= "\"%[^\"]\"%100c";    const char strform[]= "\"%[^\"]\"%n";
1236    const char intform[]= "%i%100c";    const char intform[]= "%i%n";
1237    const char blankform[]= "%*[ \t]%100c";    const char blankform[]= "%*[ \t]%n";
1238    const char ebrackform[]= "%*1[]]%100c";    const char ebrackform[]= "%*1[]]%n";
1239    const char semicform[]= "%*1[;]%100c";    const char semicform[]= "%*1[;]%n";
1240    const char bbrackform[]= "%*1[[]%100c";    const char bbrackform[]= "%*1[[]%n";
1241    
1242    int itemp, rerun= 0;    int itemp, readlength= -1;
1243    static int depth= 0;    static int depth= 0;
1244    char *rest, *match;    char *match;
   static char *in_string= NULL;  
1245    size_t inlength;    size_t inlength;
1246    
1247    if(in_string==NULL) {    if(env->in_string==NULL) {
1248        if(depth > 0 && env->interactive) {
1249          printf("]> ");
1250        }
1251      readline(env); if(env->err) return;      readline(env); if(env->err) return;
1252    
1253        if(((char *)(env->head->item->content.ptr))[0]=='\0'){
1254          env->err= 4;              /* "" means EOF */
1255          return;
1256        }
1257            
1258      in_string= malloc(strlen(env->head->item->content.ptr)+1);      env->in_string= malloc(strlen(env->head->item->content.ptr)+1);
1259      strcpy(in_string, env->head->item->content.ptr);      env->free_string= env->in_string; /* Save the original pointer */
1260        strcpy(env->in_string, env->head->item->content.ptr);
1261      toss(env); if(env->err) return;      toss(env); if(env->err) return;
1262    }    }
1263        
1264    inlength= strlen(in_string)+1;    inlength= strlen(env->in_string)+1;
1265    match= malloc(inlength);    match= malloc(inlength);
   rest= malloc(inlength);  
1266    
1267    if(sscanf(in_string, blankform, rest)) {    if(sscanf(env->in_string, blankform, &readlength)!=EOF
1268      rerun= 1;           && readlength != -1) {
1269    } else if(sscanf(in_string, intform, &itemp, rest) > 0) {      ;
1270      push_int(&(env->head), itemp);    } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF
1271    } else if(sscanf(in_string, strform, match, rest) > 0) {              && readlength != -1) {
1272      push_cstring(&(env->head), match);      push_int(env, itemp);
1273    } else if(sscanf(in_string, symbform, match, rest) > 0) {    } else if(sscanf(env->in_string, strform, match, &readlength) != EOF
1274                && readlength != -1) {
1275        push_cstring(env, match);
1276      } else if(sscanf(env->in_string, symbform, match, &readlength) != EOF
1277                && readlength != -1) {
1278      push_sym(env, match);      push_sym(env, match);
1279    } else if(sscanf(in_string, ebrackform, rest) > 0) {    } else if(sscanf(env->in_string, ebrackform, &readlength) != EOF
1280      push_sym(env, "[");              && readlength != -1) {
1281      pack(env); if(env->err) return;      pack(env); if(env->err) return;
1282      if(depth!=0) depth--;      if(depth != 0) depth--;
1283    } else if(sscanf(in_string, semicform, rest) > 0) {    } else if(sscanf(env->in_string, semicform, &readlength) != EOF
1284                && readlength != -1) {
1285      push_sym(env, ";");      push_sym(env, ";");
1286    } else if(sscanf(in_string, bbrackform, rest) > 0) {    } else if(sscanf(env->in_string, bbrackform, &readlength) != EOF
1287                && readlength != -1) {
1288      push_sym(env, "[");      push_sym(env, "[");
1289      depth++;      depth++;
1290    } else {    } else {
1291      free(rest);      free(env->free_string);
1292      rest= NULL;      env->in_string = env->free_string = NULL;
1293      rerun= 1;    }
1294      if ( env->in_string != NULL) {
1295        env->in_string += readlength;
1296    }    }
         
   free(in_string);  
   free(match);  
1297    
1298    in_string= rest;    free(match);
1299    
1300    if(rerun || depth)    if(depth)
1301      return read(env);      return sx_72656164(env);
1302  }  }

Legend:
Removed from v.1.68  
changed lines
  Added in v.1.88

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26