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

Diff of /stack/stack.c

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

revision 1.47 by masse, Thu Feb 7 04:11:10 2002 UTC revision 1.56 by teddy, Fri Feb 8 02:28:41 2002 UTC
# Line 1  Line 1 
1  /* printf */  /* printf, sscanf, fgets, fprintf */
2  #include <stdio.h>  #include <stdio.h>
3  /* EXIT_SUCCESS */  /* exit, EXIT_SUCCESS, malloc, free */
4  #include <stdlib.h>  #include <stdlib.h>
5  /* NULL */  /* NULL */
6  #include <stddef.h>  #include <stddef.h>
7  /* dlopen, dlsym, dlerror */  /* dlopen, dlsym, dlerror */
8  #include <dlfcn.h>  #include <dlfcn.h>
9  /* assert */  /* strcmp, strcpy, strlen, strcat, strdup */
 #include <assert.h>  
 /* strcat */  
10  #include <string.h>  #include <string.h>
11    
12  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 65536
# Line 50  typedef symbol *hashtbl[HASHTBLSIZE]; /* Line 48  typedef symbol *hashtbl[HASHTBLSIZE]; /*
48  typedef struct stackitem_struct  typedef struct stackitem_struct
49  {  {
50    value *item;                  /* The value on the stack */    value *item;                  /* The value on the stack */
51                                    /* (This is never NULL) */
52    struct stackitem_struct *next; /* Next item */    struct stackitem_struct *next; /* Next item */
53  } stackitem;  } stackitem;
54    
# Line 77  void init_env(environment *env) Line 76  void init_env(environment *env)
76      env->symbols[i]= NULL;      env->symbols[i]= NULL;
77  }  }
78    
79    void printerr(const char* in_string) {
80      fprintf(stderr, "Err: %s\n", in_string);
81    }
82    
83    /* Throw away a value */
84    void free_val(value *val){
85      stackitem *item, *temp;
86    
87      val->refcount--;              /* Decrease the reference count */
88      if(val->refcount == 0){
89        switch (val->type){         /* and free the contents if necessary */
90        case string:
91          free(val->content.ptr);
92          break;
93        case list:                  /* lists needs to be freed recursively */
94          item=val->content.ptr;
95          while(item != NULL) {     /* for all stack items */
96            free_val(item->item);   /* free the value */
97            temp=item->next;        /* save next ptr */
98            free(item);             /* free the stackitem */
99            item=temp;              /* go to next stackitem */
100          }
101          free(val);                /* Free the actual list value */
102          break;
103        case integer:
104        case func:
105        case symb:
106          break;
107        }
108      }
109    }
110    
111    /* Discard the top element of the stack. */
112    extern void toss(environment *env)
113    {
114      stackitem *temp= env->head;
115    
116      if((env->head)==NULL) {
117        printerr("Too Few Arguments");
118        env->err=1;
119        return;
120      }
121      
122      free_val(env->head->item);    /* Free the value */
123      env->head= env->head->next;   /* Remove the top stack item */
124      free(temp);                   /* Free the old top stack item */
125    }
126    
127  /* Returns a pointer to a pointer to an element in the hash table. */  /* Returns a pointer to a pointer to an element in the hash table. */
128  symbol **hash(hashtbl in_hashtbl, const char *in_string)  symbol **hash(hashtbl in_hashtbl, const char *in_string)
129  {  {
# Line 151  void push_cstring(stackitem **stack_head Line 198  void push_cstring(stackitem **stack_head
198    push(stack_head, new_item);    push(stack_head, new_item);
199  }  }
200    
201    /* Mangle a symbol name to a valid C identifier name */
202    char *mangle_str(const char *old_string){
203      char validchars[]
204        ="0123456789abcdef";
205      char *new_string, *current;
206    
207      new_string=malloc((strlen(old_string)*2)+4);
208      strcpy(new_string, "sx_");    /* Stack eXternal */
209      current=new_string+3;
210      while(old_string[0] != '\0'){
211        current[0]=validchars[(unsigned char)(old_string[0])/16];
212        current[1]=validchars[(unsigned char)(old_string[0])%16];
213        current+=2;
214        old_string++;
215      }
216      current[0]='\0';
217    
218      return new_string;            /* The caller must free() it */
219    }
220    
221    extern void mangle(environment *env){
222      value *new_value;
223      char *new_string;
224    
225      if((env->head)==NULL) {
226        printerr("Too Few Arguments");
227        env->err=1;
228        return;
229      }
230    
231      if(env->head->item->type!=string) {
232        printerr("Bad Argument Type");
233        env->err=2;
234        return;
235      }
236    
237      new_string= mangle_str((const char *)(env->head->item->content.ptr));
238    
239      toss(env);
240      if(env->err) return;
241    
242      new_value= malloc(sizeof(value));
243      new_value->content.ptr= new_string;
244      new_value->type= string;
245      new_value->refcount=1;
246    
247      push_val(&(env->head), new_value);
248    }
249    
250  /* Push a symbol onto the stack. */  /* Push a symbol onto the stack. */
251  void push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
252  {  {
# Line 165  void push_sym(environment *env, const ch Line 261  void push_sym(environment *env, const ch
261    void *funcptr;                /* A function pointer */    void *funcptr;                /* A function pointer */
262    
263    static void *handle= NULL;    /* Dynamic linker handle */    static void *handle= NULL;    /* Dynamic linker handle */
264      const char *dlerr;            /* Dynamic linker error */
265      char *mangled;                /* Mangled function name */
266    
267    /* Create a new stack item containing a new value */    /* Create a new stack item containing a new value */
268    new_item= malloc(sizeof(stackitem));    new_item= malloc(sizeof(stackitem));
# Line 197  void push_sym(environment *env, const ch Line 295  void push_sym(environment *env, const ch
295        handle= dlopen(NULL, RTLD_LAZY);        handle= dlopen(NULL, RTLD_LAZY);
296    
297      funcptr= dlsym(handle, in_string); /* Get function pointer */      funcptr= dlsym(handle, in_string); /* Get function pointer */
298      if(dlerror()==NULL) {       /* If a function was found */      dlerr=dlerror();
299        if(dlerr != NULL) {         /* If no function was found */
300          mangled=mangle_str(in_string);
301          funcptr= dlsym(handle, mangled); /* try mangling it */
302          free(mangled);
303          dlerr=dlerror();
304        }
305        if(dlerr==NULL) {           /* If a function was found */
306        new_fvalue= malloc(sizeof(value)); /* Create a new value */        new_fvalue= malloc(sizeof(value)); /* Create a new value */
307        new_fvalue->type=func;    /* The new value is a function pointer */        new_fvalue->type=func;    /* The new value is a function pointer */
308        new_fvalue->content.ptr=funcptr; /* Store function pointer */        new_fvalue->content.ptr=funcptr; /* Store function pointer */
# Line 209  void push_sym(environment *env, const ch Line 314  void push_sym(environment *env, const ch
314    push(&(env->head), new_item);    push(&(env->head), new_item);
315  }  }
316    
 void printerr(const char* in_string) {  
   fprintf(stderr, "Err: %s\n", in_string);  
 }  
   
 /* 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;  
     default:  
       break;  
     }  
   }  
 }  
   
 /* Discard the top element of the stack. */  
 extern void toss(environment *env)  
 {  
   stackitem *temp= env->head;  
   
   if((env->head)==NULL) {  
     printerr("Too Few Arguments");  
     env->err=1;  
     return;  
   }  
     
   free_val(env->head->item);    /* Free the value */  
   env->head= env->head->next;   /* Remove the top stack item */  
   free(temp);                   /* Free the old top stack item */  
 }  
   
317  /* Print newline. */  /* Print newline. */
318  extern void nl()  extern void nl()
319  {  {
# Line 380  extern void swap(environment *env) Line 439  extern void swap(environment *env)
439    env->head->next= temp;    env->head->next= temp;
440  }  }
441    
442    /* Rotate the first three elements on the stack. */
443    extern void rot(environment *env)
444    {
445      stackitem *temp= env->head;
446      
447      if(env->head==NULL || env->head->next==NULL
448          || env->head->next->next==NULL) {
449        printerr("Too Few Arguments");
450        env->err=1;
451        return;
452      }
453    
454      env->head= env->head->next->next;
455      temp->next->next= env->head->next;
456      env->head->next= temp;
457    }
458    
459  /* Recall a value from a symbol, if bound */  /* Recall a value from a symbol, if bound */
460  extern void rcl(environment *env)  extern void rcl(environment *env)
461  {  {
# Line 473  extern void eval(environment *env) Line 549  extern void eval(environment *env)
549      toss(env);      toss(env);
550      if(env->err) return;      if(env->err) return;
551      temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);      temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);
552      strcat(temp_string, "[ ");      strcpy(temp_string, "[ ");
553      strcat(temp_string, (char*)temp_val->content.ptr);      strcat(temp_string, (char*)temp_val->content.ptr);
554      strcat(temp_string, " ]");      strcat(temp_string, " ]");
555      stack_read(env, temp_string);      stack_read(env, temp_string);
# Line 483  extern void eval(environment *env) Line 559  extern void eval(environment *env)
559      free(temp_string);      free(temp_string);
560      break;      break;
561    
562    default:    case integer:
563        break;
564    }    }
565  }  }
566    
# Line 843  int main() Line 920  int main()
920    quit(&myenv);    quit(&myenv);
921    return EXIT_FAILURE;    return EXIT_FAILURE;
922  }  }
923    
924    /* + */
925    extern void sx_2b(environment *env) {
926      int a, b;
927      size_t len;
928      char* new_string;
929      value *a_val, *b_val;
930    
931      if((env->head)==NULL || env->head->next==NULL) {
932        printerr("Too Few Arguments");
933        env->err=1;
934        return;
935      }
936    
937      if(env->head->item->type==string
938         && env->head->next->item->type==string) {
939        a_val= env->head->item;
940        b_val= env->head->next->item;
941        a_val->refcount++;
942        b_val->refcount++;
943        toss(env); if(env->err) return;
944        toss(env); if(env->err) return;
945        len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
946        new_string= malloc(len);
947        strcpy(new_string, b_val->content.ptr);
948        strcat(new_string, a_val->content.ptr);
949        free_val(a_val); free_val(b_val);
950        push_cstring(&(env->head), new_string);
951        free(new_string);
952        return;
953      }
954      
955      if(env->head->item->type!=integer
956         || env->head->next->item->type!=integer) {
957        printerr("Bad Argument Type");
958        env->err=2;
959        return;
960      }
961      a=env->head->item->content.val;
962      toss(env);
963      if(env->err) return;
964      b=env->head->item->content.val;
965      toss(env);
966      if(env->err) return;
967      push_int(&(env->head), a+b);
968    }
969    
970    /* Return copy of a value */
971    value *copy_val(value *old_value){
972      stackitem *old_item, *new_item, *prev_item;
973    
974      value *new_value=malloc(sizeof(value));
975    
976      new_value->type=old_value->type;
977      new_value->refcount=0;        /* This is increased if/when this
978                                       value is referenced somewhere, like
979                                       in a stack item or a variable */
980      switch(old_value->type){
981      case integer:
982        new_value->content.val=old_value->content.val;
983        break;
984      case string:
985        (char *)(new_value->content.ptr)
986          = strdup((char *)(old_value->content.ptr));
987        break;
988      case func:
989      case symb:
990        new_value->content.ptr=old_value->content.ptr;
991        break;
992      case list:
993        new_value->content.ptr=NULL;
994    
995        prev_item=NULL;
996        old_item=(stackitem *)(old_value->content.ptr);
997    
998        while(old_item != NULL) {   /* While list is not empty */
999          new_item= malloc(sizeof(stackitem));
1000          new_item->item=copy_val(old_item->item); /* recurse */
1001          new_item->next=NULL;
1002          if(prev_item != NULL)     /* If this wasn't the first item */
1003            prev_item->next=new_item; /* point the previous item to the
1004                                         new item */
1005          else
1006            new_value->content.ptr=new_item;
1007          old_item=old_item->next;
1008          prev_item=new_item;
1009        }    
1010        break;
1011      }
1012      return new_value;
1013    }
1014    
1015    /* duplicates an item on the stack */
1016    extern void dup(environment *env) {
1017      if((env->head)==NULL) {
1018        printerr("Too Few Arguments");
1019        env->err=1;
1020        return;
1021      }
1022      push_val(&(env->head), copy_val(env->head->item));
1023    }
1024    
1025    /* If-Then */
1026    extern void ift(environment *env) {
1027    
1028      int truth;
1029    
1030      if((env->head)==NULL || env->head->next==NULL) {
1031        printerr("Too Few Arguments");
1032        env->err=1;
1033        return;
1034      }
1035    
1036      if(env->head->next->item->type != integer) {
1037        printerr("Bad Argument Type");
1038        env->err=2;
1039        return;
1040      }
1041      
1042      swap(env);
1043      if(env->err) return;
1044      
1045      truth=env->head->item->content.val;
1046    
1047      toss(env);
1048      if(env->err) return;
1049    
1050      if(truth)
1051        eval(env);
1052      else
1053        toss(env);
1054    }
1055    
1056    
1057    /* If-Then-Else */
1058    extern void ifte(environment *env) {
1059    
1060      int truth;
1061    
1062      if((env->head)==NULL || env->head->next==NULL
1063         || env->head->next->next==NULL) {
1064        printerr("Too Few Arguments");
1065        env->err=1;
1066        return;
1067      }
1068    
1069      if(env->head->next->next->item->type != integer) {
1070        printerr("Bad Argument Type");
1071        env->err=2;
1072        return;
1073      }
1074      
1075      rot(env);
1076      if(env->err) return;
1077      
1078      truth=env->head->item->content.val;
1079    
1080      toss(env);
1081      if(env->err) return;
1082    
1083      if(!truth)
1084        swap(env);
1085      if(env->err) return;
1086    
1087      toss(env);
1088      if(env->err) return;
1089    
1090      eval(env);
1091    }
1092    

Legend:
Removed from v.1.47  
changed lines
  Added in v.1.56

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26