/[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.48 by teddy, Thu Feb 7 04:34:42 2002 UTC
# Line 77  void init_env(environment *env) Line 77  void init_env(environment *env)
77      env->symbols[i]= NULL;      env->symbols[i]= NULL;
78  }  }
79    
80    void printerr(const char* in_string) {
81      fprintf(stderr, "Err: %s\n", in_string);
82    }
83    
84    /* Throw away a value */
85    void free_val(value *val){
86      stackitem *item, *temp;
87    
88      val->refcount--;              /* Decrease the reference count */
89      if(val->refcount == 0){
90        switch (val->type){         /* and free the contents if necessary */
91        case string:
92          free(val->content.ptr);
93          break;
94        case list:                  /* lists needs to be freed recursively */
95          item=val->content.ptr;
96          while(item != NULL) {     /* for all stack items */
97            free_val(item->item);   /* free the value */
98            temp=item->next;        /* save next ptr */
99            free(item);             /* free the stackitem */
100            item=temp;              /* go to next stackitem */
101          }
102          free(val);                /* Free the actual list value */
103          break;
104        default:
105          break;
106        }
107      }
108    }
109    
110    /* Discard the top element of the stack. */
111    extern void toss(environment *env)
112    {
113      stackitem *temp= env->head;
114    
115      if((env->head)==NULL) {
116        printerr("Too Few Arguments");
117        env->err=1;
118        return;
119      }
120      
121      free_val(env->head->item);    /* Free the value */
122      env->head= env->head->next;   /* Remove the top stack item */
123      free(temp);                   /* Free the old top stack item */
124    }
125    
126  /* 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. */
127  symbol **hash(hashtbl in_hashtbl, const char *in_string)  symbol **hash(hashtbl in_hashtbl, const char *in_string)
128  {  {
# Line 151  void push_cstring(stackitem **stack_head Line 197  void push_cstring(stackitem **stack_head
197    push(stack_head, new_item);    push(stack_head, new_item);
198  }  }
199    
200    /* Mangle a symbol name to a valid C identifier name */
201    char *mangle_(const char *old_string){
202      char validchars[]
203        ="0123456789abcdef";
204      char *new_string, *current;
205    
206      new_string=malloc(strlen(old_string)+4);
207      strcpy(new_string, "sx_");    /* Stack eXternal */
208      current=new_string+3;
209      while(old_string[0] != '\0'){
210        current[0]=validchars[old_string[0]/16];
211        current[1]=validchars[old_string[0]%16];
212        current+=2;
213        old_string++;
214      }
215      current[0]='\0';
216    
217      return new_string;            /* The caller must free() it */
218    }
219    
220    extern void mangle(environment *env){
221      value *new_value;
222      char *new_string;
223    
224      if((env->head)==NULL) {
225        printerr("Too Few Arguments");
226        env->err=1;
227        return;
228      }
229    
230      if(env->head->item->type!=string) {
231        printerr("Bad Argument Type");
232        env->err=2;
233        return;
234      }
235    
236      new_string= mangle_((const char *)(env->head->item->content.ptr));
237    
238      toss(env);
239      if(env->err) return;
240    
241      new_value= malloc(sizeof(value));
242      new_value->content.ptr= new_string;
243      new_value->type= string;
244      new_value->refcount=1;
245    
246      push_val(&(env->head), new_value);
247    }
248    
249  /* Push a symbol onto the stack. */  /* Push a symbol onto the stack. */
250  void push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
251  {  {
# Line 165  void push_sym(environment *env, const ch Line 260  void push_sym(environment *env, const ch
260    void *funcptr;                /* A function pointer */    void *funcptr;                /* A function pointer */
261    
262    static void *handle= NULL;    /* Dynamic linker handle */    static void *handle= NULL;    /* Dynamic linker handle */
263      const char *dlerr;            /* Dynamic linker error */
264      char *mangled;                /* Mangled function name */
265    
266    /* Create a new stack item containing a new value */    /* Create a new stack item containing a new value */
267    new_item= malloc(sizeof(stackitem));    new_item= malloc(sizeof(stackitem));
# Line 197  void push_sym(environment *env, const ch Line 294  void push_sym(environment *env, const ch
294        handle= dlopen(NULL, RTLD_LAZY);        handle= dlopen(NULL, RTLD_LAZY);
295    
296      funcptr= dlsym(handle, in_string); /* Get function pointer */      funcptr= dlsym(handle, in_string); /* Get function pointer */
297      if(dlerror()==NULL) {       /* If a function was found */      dlerr=dlerror();
298        if(dlerr != NULL) {         /* If no function was found */
299          mangled=mangle_(in_string);
300          funcptr= dlsym(handle, mangled); /* try mangling it */
301          free(mangled);
302          dlerr=dlerror();
303        }
304        if(dlerr==NULL) {           /* If a function was found */
305        new_fvalue= malloc(sizeof(value)); /* Create a new value */        new_fvalue= malloc(sizeof(value)); /* Create a new value */
306        new_fvalue->type=func;    /* The new value is a function pointer */        new_fvalue->type=func;    /* The new value is a function pointer */
307        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 313  void push_sym(environment *env, const ch
313    push(&(env->head), new_item);    push(&(env->head), new_item);
314  }  }
315    
 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 */  
 }  
   
316  /* Print newline. */  /* Print newline. */
317  extern void nl()  extern void nl()
318  {  {
# Line 843  int main() Line 901  int main()
901    quit(&myenv);    quit(&myenv);
902    return EXIT_FAILURE;    return EXIT_FAILURE;
903  }  }
904    
905    /* + */
906    extern void sx_2b(environment *env) {
907      int a, b;
908    
909      if((env->head)==NULL || env->head->next==NULL) {
910        printerr("Too Few Arguments");
911        env->err=1;
912        return;
913      }
914      
915      if(env->head->item->type!=integer
916         || env->head->next->item->type!=integer) {
917        printerr("Bad Argument Type");
918        env->err=2;
919        return;
920      }
921      a=env->head->item->content.val;
922      toss(env);
923      if(env->err) return;
924      b=env->head->item->content.val;
925      toss(env);
926      if(env->err) return;
927      push_int(&(env->head), a+b);
928    }

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26