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

Diff of /stack/stack.c

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

revision 1.48 by teddy, Thu Feb 7 04:34:42 2002 UTC revision 1.59 by teddy, Fri Feb 8 04:58:23 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 101  void free_val(value *val){ Line 100  void free_val(value *val){
100        }        }
101        free(val);                /* Free the actual list value */        free(val);                /* Free the actual list value */
102        break;        break;
103      default:      case integer:
104        case func:
105        case symb:
106        break;        break;
107      }      }
108    }    }
# Line 198  void push_cstring(stackitem **stack_head Line 199  void push_cstring(stackitem **stack_head
199  }  }
200    
201  /* Mangle a symbol name to a valid C identifier name */  /* Mangle a symbol name to a valid C identifier name */
202  char *mangle_(const char *old_string){  char *mangle_str(const char *old_string){
203    char validchars[]    char validchars[]
204      ="0123456789abcdef";      ="0123456789abcdef";
205    char *new_string, *current;    char *new_string, *current;
206    
207    new_string=malloc(strlen(old_string)+4);    new_string=malloc((strlen(old_string)*2)+4);
208    strcpy(new_string, "sx_");    /* Stack eXternal */    strcpy(new_string, "sx_");    /* Stack eXternal */
209    current=new_string+3;    current=new_string+3;
210    while(old_string[0] != '\0'){    while(old_string[0] != '\0'){
211      current[0]=validchars[old_string[0]/16];      current[0]=validchars[(unsigned char)(old_string[0])/16];
212      current[1]=validchars[old_string[0]%16];      current[1]=validchars[(unsigned char)(old_string[0])%16];
213      current+=2;      current+=2;
214      old_string++;      old_string++;
215    }    }
# Line 233  extern void mangle(environment *env){ Line 234  extern void mangle(environment *env){
234      return;      return;
235    }    }
236    
237    new_string= mangle_((const char *)(env->head->item->content.ptr));    new_string= mangle_str((const char *)(env->head->item->content.ptr));
238    
239    toss(env);    toss(env);
240    if(env->err) return;    if(env->err) return;
# Line 296  void push_sym(environment *env, const ch Line 297  void push_sym(environment *env, const ch
297      funcptr= dlsym(handle, in_string); /* Get function pointer */      funcptr= dlsym(handle, in_string); /* Get function pointer */
298      dlerr=dlerror();      dlerr=dlerror();
299      if(dlerr != NULL) {         /* If no function was found */      if(dlerr != NULL) {         /* If no function was found */
300        mangled=mangle_(in_string);        mangled=mangle_str(in_string);
301        funcptr= dlsym(handle, mangled); /* try mangling it */        funcptr= dlsym(handle, mangled); /* try mangling it */
302        free(mangled);        free(mangled);
303        dlerr=dlerror();        dlerr=dlerror();
# Line 346  extern void type(environment *env){ Line 347  extern void type(environment *env){
347    case list:    case list:
348      push_sym(env, "list");      push_sym(env, "list");
349      break;      break;
   default:  
     push_sym(env, "unknown");  
     break;  
350    }    }
351  }      }    
352    
# Line 360  void print_h(stackitem *stack_head) Line 358  void print_h(stackitem *stack_head)
358      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
359      break;      break;
360    case string:    case string:
361      printf("\"%s\"", (char*)stack_head->item->content.ptr);      printf("%s", (char*)stack_head->item->content.ptr);
362      break;      break;
363    case symb:    case symb:
364      printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);      printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);
# Line 379  void print_h(stackitem *stack_head) Line 377  void print_h(stackitem *stack_head)
377      }      }
378      printf("]");      printf("]");
379      break;      break;
   default:  
     printf("#<unknown %p>", (stack_head->item->content.ptr));  
     break;  
380    }    }
381  }  }
382    
# Line 438  extern void swap(environment *env) Line 433  extern void swap(environment *env)
433    env->head->next= temp;    env->head->next= temp;
434  }  }
435    
436    /* Rotate the first three elements on the stack. */
437    extern void rot(environment *env)
438    {
439      stackitem *temp= env->head;
440      
441      if(env->head==NULL || env->head->next==NULL
442          || env->head->next->next==NULL) {
443        printerr("Too Few Arguments");
444        env->err=1;
445        return;
446      }
447    
448      env->head= env->head->next->next;
449      temp->next->next= env->head->next;
450      env->head->next= temp;
451    }
452    
453  /* Recall a value from a symbol, if bound */  /* Recall a value from a symbol, if bound */
454  extern void rcl(environment *env)  extern void rcl(environment *env)
455  {  {
# Line 490  extern void eval(environment *env) Line 502  extern void eval(environment *env)
502      rcl(env);                   /* get its contents */      rcl(env);                   /* get its contents */
503      if(env->err) return;      if(env->err) return;
504      if(env->head->item->type!=symb){ /* don't recurse symbols */      if(env->head->item->type!=symb){ /* don't recurse symbols */
505        eval(env);                        /* evaluate the value */        return eval(env);         /* evaluate the value */
       return;  
506      }      }
507      break;      return;
508    
509      /* If it's a lone function value, run it */      /* If it's a lone function value, run it */
510    case func:    case func:
511      in_func= (funcp)(env->head->item->content.ptr);      in_func= (funcp)(env->head->item->content.ptr);
512      toss(env);      toss(env);
513      if(env->err) return;      if(env->err) return;
514      (*in_func)(env);      return (*in_func)(env);
     break;  
515    
516      /* If it's a list */      /* If it's a list */
517    case list:    case list:
# Line 510  extern void eval(environment *env) Line 520  extern void eval(environment *env)
520      toss(env);      toss(env);
521      if(env->err) return;      if(env->err) return;
522      iterator= (stackitem*)temp_val->content.ptr;      iterator= (stackitem*)temp_val->content.ptr;
523      while(iterator!=NULL && iterator->item!=NULL) {      while(iterator!=NULL) {
524        push_val(&(env->head), iterator->item);        push_val(&(env->head), iterator->item);
525        if(env->head->item->type==symb        if(env->head->item->type==symb
526          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
527          toss(env);          toss(env);
528          if(env->err) return;          if(env->err) return;
529            if(iterator->next == NULL){
530              free_val(temp_val);
531              return eval(env);
532            }
533          eval(env);          eval(env);
534          if(env->err) return;          if(env->err) return;
535        }        }
536        iterator= iterator->next;        iterator= iterator->next;
537      }      }
538      free_val(temp_val);      free_val(temp_val);
539      break;      return;
540    
541      /* If it's a string */      /* If it's a string */
542    case string:    case string:
# Line 531  extern void eval(environment *env) Line 545  extern void eval(environment *env)
545      toss(env);      toss(env);
546      if(env->err) return;      if(env->err) return;
547      temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);      temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);
548      strcat(temp_string, "[ ");      strcpy(temp_string, "[ ");
549      strcat(temp_string, (char*)temp_val->content.ptr);      strcpy(temp_string+2, (char*)temp_val->content.ptr);
550        free_val(temp_val);
551      strcat(temp_string, " ]");      strcat(temp_string, " ]");
552      stack_read(env, temp_string);      stack_read(env, temp_string);
     eval(env);  
     if(env->err) return;  
     free_val(temp_val);  
553      free(temp_string);      free(temp_string);
554      break;      return eval(env);
555    
556    default:    case integer:
557        return;
558    }    }
559  }  }
560    
# Line 905  int main() Line 918  int main()
918  /* + */  /* + */
919  extern void sx_2b(environment *env) {  extern void sx_2b(environment *env) {
920    int a, b;    int a, b;
921      size_t len;
922      char* new_string;
923      value *a_val, *b_val;
924    
925    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
926      printerr("Too Few Arguments");      printerr("Too Few Arguments");
927      env->err=1;      env->err=1;
928      return;      return;
929    }    }
930    
931      if(env->head->item->type==string
932         && env->head->next->item->type==string) {
933        a_val= env->head->item;
934        b_val= env->head->next->item;
935        a_val->refcount++;
936        b_val->refcount++;
937        toss(env); if(env->err) return;
938        toss(env); if(env->err) return;
939        len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
940        new_string= malloc(len);
941        strcpy(new_string, b_val->content.ptr);
942        strcat(new_string, a_val->content.ptr);
943        free_val(a_val); free_val(b_val);
944        push_cstring(&(env->head), new_string);
945        free(new_string);
946        return;
947      }
948        
949    if(env->head->item->type!=integer    if(env->head->item->type!=integer
950       || env->head->next->item->type!=integer) {       || env->head->next->item->type!=integer) {
# Line 926  extern void sx_2b(environment *env) { Line 960  extern void sx_2b(environment *env) {
960    if(env->err) return;    if(env->err) return;
961    push_int(&(env->head), a+b);    push_int(&(env->head), a+b);
962  }  }
963    
964    /* Return copy of a value */
965    value *copy_val(value *old_value){
966      stackitem *old_item, *new_item, *prev_item;
967    
968      value *new_value=malloc(sizeof(value));
969    
970      new_value->type=old_value->type;
971      new_value->refcount=0;        /* This is increased if/when this
972                                       value is referenced somewhere, like
973                                       in a stack item or a variable */
974      switch(old_value->type){
975      case integer:
976        new_value->content.val=old_value->content.val;
977        break;
978      case string:
979        (char *)(new_value->content.ptr)
980          = strdup((char *)(old_value->content.ptr));
981        break;
982      case func:
983      case symb:
984        new_value->content.ptr=old_value->content.ptr;
985        break;
986      case list:
987        new_value->content.ptr=NULL;
988    
989        prev_item=NULL;
990        old_item=(stackitem *)(old_value->content.ptr);
991    
992        while(old_item != NULL) {   /* While list is not empty */
993          new_item= malloc(sizeof(stackitem));
994          new_item->item=copy_val(old_item->item); /* recurse */
995          new_item->next=NULL;
996          if(prev_item != NULL)     /* If this wasn't the first item */
997            prev_item->next=new_item; /* point the previous item to the
998                                         new item */
999          else
1000            new_value->content.ptr=new_item;
1001          old_item=old_item->next;
1002          prev_item=new_item;
1003        }    
1004        break;
1005      }
1006      return new_value;
1007    }
1008    
1009    /* duplicates an item on the stack */
1010    extern void dup(environment *env) {
1011      if((env->head)==NULL) {
1012        printerr("Too Few Arguments");
1013        env->err=1;
1014        return;
1015      }
1016      push_val(&(env->head), copy_val(env->head->item));
1017    }
1018    
1019    /* "if", If-Then */
1020    extern void sx_6966(environment *env) {
1021    
1022      int truth;
1023    
1024      if((env->head)==NULL || env->head->next==NULL) {
1025        printerr("Too Few Arguments");
1026        env->err=1;
1027        return;
1028      }
1029    
1030      if(env->head->next->item->type != integer) {
1031        printerr("Bad Argument Type");
1032        env->err=2;
1033        return;
1034      }
1035      
1036      swap(env);
1037      if(env->err) return;
1038      
1039      truth=env->head->item->content.val;
1040    
1041      toss(env);
1042      if(env->err) return;
1043    
1044      if(truth)
1045        eval(env);
1046      else
1047        toss(env);
1048    }
1049    
1050    /* If-Then-Else */
1051    extern void ifelse(environment *env) {
1052    
1053      int truth;
1054    
1055      if((env->head)==NULL || env->head->next==NULL
1056         || env->head->next->next==NULL) {
1057        printerr("Too Few Arguments");
1058        env->err=1;
1059        return;
1060      }
1061    
1062      if(env->head->next->next->item->type != integer) {
1063        printerr("Bad Argument Type");
1064        env->err=2;
1065        return;
1066      }
1067      
1068      rot(env);
1069      if(env->err) return;
1070      
1071      truth=env->head->item->content.val;
1072    
1073      toss(env);
1074      if(env->err) return;
1075    
1076      if(!truth)
1077        swap(env);
1078      if(env->err) return;
1079    
1080      toss(env);
1081      if(env->err) return;
1082    
1083      eval(env);
1084    }
1085    
1086    /* while */
1087    extern void sx_7768696c65(environment *env) {
1088    
1089      int truth;
1090    
1091      if((env->head)==NULL || env->head->next==NULL) {
1092        printerr("Too Few Arguments");
1093        env->err=1;
1094        return;
1095      }
1096    
1097      do {
1098        swap(env); if(env->err) return;
1099        dup(env); if(env->err) return;
1100        eval(env); if(env->err) return;
1101        
1102        if(env->head->item->type != integer) {
1103          printerr("Bad Argument Type");
1104          env->err=2;
1105          return;
1106        }
1107        
1108        truth= env->head->item->content.val;
1109        
1110        toss(env); if(env->err) return;
1111        swap(env); if(env->err) return;
1112        
1113        if(truth) {
1114          dup(env);
1115          eval(env);
1116        } else {
1117          toss(env);
1118          toss(env);
1119        }
1120      
1121      } while(truth);
1122    }

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26