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

Diff of /stack/stack.c

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

revision 1.51 by teddy, Thu Feb 7 23:53:14 2002 UTC revision 1.55 by teddy, Fri Feb 8 00:59:34 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 101  void free_val(value *val){ Line 99  void free_val(value *val){
99        }        }
100        free(val);                /* Free the actual list value */        free(val);                /* Free the actual list value */
101        break;        break;
102      default:      case integer:
103        case func:
104        case symb:
105        break;        break;
106      }      }
107    }    }
# Line 207  char *mangle_str(const char *old_string) Line 207  char *mangle_str(const char *old_string)
207    strcpy(new_string, "sx_");    /* Stack eXternal */    strcpy(new_string, "sx_");    /* Stack eXternal */
208    current=new_string+3;    current=new_string+3;
209    while(old_string[0] != '\0'){    while(old_string[0] != '\0'){
210      current[0]=validchars[old_string[0]/16];      current[0]=validchars[(unsigned char)(old_string[0])/16];
211      current[1]=validchars[old_string[0]%16];      current[1]=validchars[(unsigned char)(old_string[0])%16];
212      current+=2;      current+=2;
213      old_string++;      old_string++;
214    }    }
# Line 541  extern void eval(environment *env) Line 541  extern void eval(environment *env)
541      free(temp_string);      free(temp_string);
542      break;      break;
543    
544    default:    case integer:
545        break;
546    }    }
547  }  }
548    
# Line 947  extern void sx_2b(environment *env) { Line 948  extern void sx_2b(environment *env) {
948    if(env->err) return;    if(env->err) return;
949    push_int(&(env->head), a+b);    push_int(&(env->head), a+b);
950  }  }
951    
952    /* Return copy of a value */
953    value *copy_val(value *old_value){
954      stackitem *old_item, *new_item, *prev_item;
955    
956      value *new_value=malloc(sizeof(value));
957    
958      new_value->type=old_value->type;
959      new_value->refcount=0;        /* This is increased if/when this
960                                       value is referenced somewhere, like
961                                       in a stack item or a variable */
962      switch(old_value->type){
963      case integer:
964        new_value->content.val=old_value->content.val;
965        break;
966      case string:
967        (char *)(new_value->content.ptr)
968          = strdup((char *)(old_value->content.ptr));
969        break;
970      case func:
971      case symb:
972        new_value->content.ptr=old_value->content.ptr;
973        break;
974      case list:
975        new_value->content.ptr=NULL;
976    
977        prev_item=NULL;
978        old_item=(stackitem *)(old_value->content.ptr);
979    
980        while(old_item != NULL) {   /* While list is not empty */
981          new_item= malloc(sizeof(stackitem));
982          new_item->item=copy_val(old_item->item); /* recurse */
983          new_item->next=NULL;
984          if(prev_item != NULL)     /* If this wasn't the first item */
985            prev_item->next=new_item; /* point the previous item to the
986                                         new item */
987          else
988            new_value->content.ptr=new_item;
989          old_item=old_item->next;
990          prev_item=new_item;
991        }    
992        break;
993      }
994      return new_value;
995    }
996    
997    /* duplicates an item on the stack */
998    extern void dup(environment *env) {
999      if((env->head)==NULL) {
1000        printerr("Too Few Arguments");
1001        env->err=1;
1002        return;
1003      }
1004      push_val(&(env->head), copy_val(env->head->item));
1005    }

Legend:
Removed from v.1.51  
changed lines
  Added in v.1.55

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26