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

Diff of /stack/stack.c

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

revision 1.60 by teddy, Fri Feb 8 05:12:37 2002 UTC revision 1.81 by masse, Thu Feb 14 19:20:28 2002 UTC
# Line 9  Line 9 
9  /* strcmp, strcpy, strlen, strcat, strdup */  /* strcmp, strcpy, strlen, strcat, strdup */
10  #include <string.h>  #include <string.h>
11    
12  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 2048
13    
14  /* First, define some types. */  /* First, define some types. */
15    
# Line 58  typedef struct { Line 58  typedef struct {
58    stackitem *head;              /* Head of the stack */    stackitem *head;              /* Head of the stack */
59    hashtbl symbols;              /* Hash table of all variable bindings */    hashtbl symbols;              /* Hash table of all variable bindings */
60    int err;                      /* Error flag */    int err;                      /* Error flag */
61    int non_eval_flag;    char *in_string;              /* Input pending to be read */
62      char *free_string;            /* Free this string when all input is
63                                       read from in_string */
64  } environment;  } environment;
65    
66  /* A type for pointers to external functions */  /* A type for pointers to external functions */
# Line 70  void init_env(environment *env) Line 72  void init_env(environment *env)
72  {  {
73    int i;    int i;
74    
75      env->in_string= NULL;
76    env->err= 0;    env->err= 0;
   env->non_eval_flag= 0;  
77    for(i= 0; i<HASHTBLSIZE; i++)    for(i= 0; i<HASHTBLSIZE; i++)
78      env->symbols[i]= NULL;      env->symbols[i]= NULL;
79  }  }
# Line 98  void free_val(value *val){ Line 100  void free_val(value *val){
100          free(item);             /* free the stackitem */          free(item);             /* free the stackitem */
101          item=temp;              /* go to next stackitem */          item=temp;              /* go to next stackitem */
102        }        }
       free(val);                /* Free the actual list value */  
103        break;        break;
104      case integer:      case integer:
105      case func:      case func:
106      case symb:      case symb:
107        break;        break;
108      }      }
109        if(val->id!=NULL)
110          free(val->id);
111        free(val);          /* Free the actual list value */
112    }    }
113  }  }
114    
# Line 153  symbol **hash(hashtbl in_hashtbl, const Line 157  symbol **hash(hashtbl in_hashtbl, const
157    }    }
158  }  }
159    
 /* Generic push function. */  
 void push(stackitem** stack_head, stackitem* in_item)  
 {  
   in_item->next= *stack_head;  
   *stack_head= in_item;  
 }  
   
160  /* Push a value onto the stack */  /* Push a value onto the stack */
161  void push_val(stackitem **stack_head, value *val)  void push_val(environment *env, value *val)
162  {  {
163    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
164    new_item->item= val;    new_item->item= val;
165    val->refcount++;    val->refcount++;
166    push(stack_head, new_item);    new_item->next= env->head;
167      env->head= new_item;
168  }  }
169    
170  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
171  void push_int(stackitem **stack_head, int in_val)  void push_int(environment *env, int in_val)
172  {  {
173    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
   stackitem *new_item= malloc(sizeof(stackitem));  
   new_item->item= new_value;  
174        
175    new_value->content.val= in_val;    new_value->content.val= in_val;
176    new_value->type= integer;    new_value->type= integer;
177    new_value->refcount=1;    new_value->refcount= 1;
178      new_value->id= NULL;
179    
180    push(stack_head, new_item);    push_val(env, new_value);
181  }  }
182    
183  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
184  void push_cstring(stackitem **stack_head, const char *in_string)  void push_cstring(environment *env, const char *in_string)
185  {  {
186    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
   stackitem *new_item= malloc(sizeof(stackitem));  
   new_item->item=new_value;  
187    
188    new_value->content.ptr= malloc(strlen(in_string)+1);    new_value->content.ptr= malloc(strlen(in_string)+1);
189    strcpy(new_value->content.ptr, in_string);    strcpy(new_value->content.ptr, in_string);
190    new_value->type= string;    new_value->type= string;
191    new_value->refcount=1;    new_value->refcount= 1;
192      new_value->id= NULL;
193    
194    push(stack_head, new_item);    push_val(env, new_value);
195  }  }
196    
197  /* 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 215  char *mangle_str(const char *old_string)
215  }  }
216    
217  extern void mangle(environment *env){  extern void mangle(environment *env){
   value *new_value;  
218    char *new_string;    char *new_string;
219    
220    if((env->head)==NULL) {    if((env->head)==NULL) {
# Line 239  extern void mangle(environment *env){ Line 234  extern void mangle(environment *env){
234    toss(env);    toss(env);
235    if(env->err) return;    if(env->err) return;
236    
237    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);  
238  }  }
239    
240  /* Push a symbol onto the stack. */  /* Push a symbol onto the stack. */
241  void push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
242  {  {
   stackitem *new_item;          /* The new stack item */  
   /* ...which will contain... */  
243    value *new_value;             /* A new symbol value */    value *new_value;             /* A new symbol value */
244    /* ...which might point to... */    /* ...which might point to... */
245    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 252  void push_sym(environment *env, const ch
252    const char *dlerr;            /* Dynamic linker error */    const char *dlerr;            /* Dynamic linker error */
253    char *mangled;                /* Mangled function name */    char *mangled;                /* Mangled function name */
254    
   /* Create a new stack item containing a new value */  
   new_item= malloc(sizeof(stackitem));  
255    new_value= malloc(sizeof(value));    new_value= malloc(sizeof(value));
   new_item->item=new_value;  
256    
257    /* The new value is a symbol */    /* The new value is a symbol */
258    new_value->type= symb;    new_value->type= symb;
# Line 311  void push_sym(environment *env, const ch Line 296  void push_sym(environment *env, const ch
296        new_fvalue->refcount= 1;        new_fvalue->refcount= 1;
297      }      }
298    }    }
299    push(&(env->head), new_item);    push_val(env, new_value);
300  }  }
301    
302  /* Print newline. */  /* Print newline. */
# Line 351  extern void type(environment *env){ Line 336  extern void type(environment *env){
336  }      }    
337    
338  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
339  void print_h(stackitem *stack_head)  void print_h(stackitem *stack_head, int noquote)
340  {  {
341    switch(stack_head->item->type) {    switch(stack_head->item->type) {
342    case integer:    case integer:
343      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
344      break;      break;
345    case string:    case string:
346      printf("%s", (char*)stack_head->item->content.ptr);      if(noquote)
347          printf("%s", (char*)stack_head->item->content.ptr);
348        else
349          printf("\"%s\"", (char*)stack_head->item->content.ptr);
350      break;      break;
351    case symb:    case symb:
352      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 359  void print_h(stackitem *stack_head)
359      stack_head=(stackitem *)(stack_head->item->content.ptr);      stack_head=(stackitem *)(stack_head->item->content.ptr);
360      printf("[ ");      printf("[ ");
361      while(stack_head != NULL) {      while(stack_head != NULL) {
362        print_h(stack_head);        print_h(stack_head, noquote);
363        printf(" ");        printf(" ");
364        stack_head=stack_head->next;        stack_head=stack_head->next;
365      }      }
# Line 386  extern void print_(environment *env) { Line 374  extern void print_(environment *env) {
374      env->err=1;      env->err=1;
375      return;      return;
376    }    }
377    print_h(env->head);    print_h(env->head, 0);
378      nl();
379  }  }
380    
381  /* 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 386  extern void print(environment *env)
386    toss(env);    toss(env);
387  }  }
388    
389    extern void princ_(environment *env) {
390      if(env->head==NULL) {
391        printerr("Too Few Arguments");
392        env->err=1;
393        return;
394      }
395      print_h(env->head, 1);
396    }
397    
398    /* Prints the top element of the stack and then discards it. */
399    extern void princ(environment *env)
400    {
401      princ_(env);
402      if(env->err) return;
403      toss(env);
404    }
405    
406  /* Only to be called by function printstack. */  /* Only to be called by function printstack. */
407  void print_st(stackitem *stack_head, long counter)  void print_st(stackitem *stack_head, long counter)
408  {  {
409    if(stack_head->next != NULL)    if(stack_head->next != NULL)
410      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
411    printf("%ld: ", counter);    printf("%ld: ", counter);
412    print_h(stack_head);    print_h(stack_head, 0);
413    nl();    nl();
414  }  }
415    
# Line 411  void print_st(stackitem *stack_head, lon Line 417  void print_st(stackitem *stack_head, lon
417  extern void printstack(environment *env)  extern void printstack(environment *env)
418  {  {
419    if(env->head == NULL) {    if(env->head == NULL) {
420        printf("Stack Empty\n");
421      return;      return;
422    }    }
423    print_st(env->head, 1);    print_st(env->head, 1);
   nl();  
424  }  }
425    
426  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 475  extern void rcl(environment *env) Line 481  extern void rcl(environment *env)
481    }    }
482    toss(env);            /* toss the symbol */    toss(env);            /* toss the symbol */
483    if(env->err) return;    if(env->err) return;
484    push_val(&(env->head), val); /* Return its bound value */    push_val(env, val); /* Return its bound value */
485  }  }
486    
 void stack_read(environment*, char*);  
   
487  /* 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
488     function value, and if it is, toss the symbol and execute the     function value, and if it is, toss the symbol and execute the
489     function. */     function. */
# Line 488  extern void eval(environment *env) Line 492  extern void eval(environment *env)
492    funcp in_func;    funcp in_func;
493    value* temp_val;    value* temp_val;
494    stackitem* iterator;    stackitem* iterator;
495    char* temp_string;  
496     eval_start:
497    
498    if(env->head==NULL) {    if(env->head==NULL) {
499      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 502  extern void eval(environment *env) Line 507  extern void eval(environment *env)
507      rcl(env);                   /* get its contents */      rcl(env);                   /* get its contents */
508      if(env->err) return;      if(env->err) return;
509      if(env->head->item->type!=symb){ /* don't recurse symbols */      if(env->head->item->type!=symb){ /* don't recurse symbols */
510        return eval(env);         /* evaluate the value */        goto eval_start;
511      }      }
512      return;      return;
513    
# Line 521  extern void eval(environment *env) Line 526  extern void eval(environment *env)
526      if(env->err) return;      if(env->err) return;
527      iterator= (stackitem*)temp_val->content.ptr;      iterator= (stackitem*)temp_val->content.ptr;
528      while(iterator!=NULL) {      while(iterator!=NULL) {
529        push_val(&(env->head), iterator->item);        push_val(env, iterator->item);
530        if(env->head->item->type==symb        if(env->head->item->type==symb
531          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
532          toss(env);          toss(env);
533          if(env->err) return;          if(env->err) return;
534          if(iterator->next == NULL){          if(iterator->next == NULL){
535            free_val(temp_val);            free_val(temp_val);
536            return eval(env);            goto eval_start;
537          }          }
538          eval(env);          eval(env);
539          if(env->err) return;          if(env->err) return;
# Line 538  extern void eval(environment *env) Line 543  extern void eval(environment *env)
543      free_val(temp_val);      free_val(temp_val);
544      return;      return;
545    
546      /* 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);  
     return eval(env);  
   
   case integer:  
547      return;      return;
548    }    }
549  }  }
# Line 588  extern void rev(environment *env){ Line 578  extern void rev(environment *env){
578  /* Make a list. */  /* Make a list. */
579  extern void pack(environment *env)  extern void pack(environment *env)
580  {  {
   void* delimiter;  
581    stackitem *iterator, *temp;    stackitem *iterator, *temp;
582    value *pack;    value *pack;
583    
   delimiter= env->head->item->content.ptr; /* Get delimiter */  
   toss(env);  
   
584    iterator= env->head;    iterator= env->head;
585    
586    if(iterator==NULL || iterator->item->content.ptr==delimiter) {    if(iterator==NULL
587         || (iterator->item->type==symb
588         && ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) {
589      temp= NULL;      temp= NULL;
590      toss(env);      toss(env);
591    } else {    } else {
592      /* Search for first delimiter */      /* Search for first delimiter */
593      while(iterator->next!=NULL      while(iterator->next!=NULL
594            && iterator->next->item->content.ptr!=delimiter)            && (iterator->next->item->type!=symb
595              || ((symbol*)(iterator->next->item->content.ptr))->id[0]!='['))
596        iterator= iterator->next;        iterator= iterator->next;
597            
598      /* Extract list */      /* Extract list */
# Line 621  extern void pack(environment *env) Line 610  extern void pack(environment *env)
610    pack->content.ptr= temp;    pack->content.ptr= temp;
611    pack->refcount= 1;    pack->refcount= 1;
612    
613    temp= malloc(sizeof(stackitem));    push_val(env, pack);
   temp->item= pack;  
   
   push(&(env->head), temp);  
614    rev(env);    rev(env);
615  }  }
616    
 /* 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);  
 }  
   
617  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
618  extern void expand(environment *env)  extern void expand(environment *env)
619  {  {
# Line 763  extern void eq(environment *env) Line 670  extern void eq(environment *env)
670    result= (left==right);    result= (left==right);
671        
672    toss(env); toss(env);    toss(env); toss(env);
673    push_int(&(env->head), result);    push_int(env, result);
674  }  }
675    
676  /* Negates the top element on the stack. */  /* Negates the top element on the stack. */
# Line 785  extern void not(environment *env) Line 692  extern void not(environment *env)
692    
693    val= env->head->item->content.val;    val= env->head->item->content.val;
694    toss(env);    toss(env);
695    push_int(&(env->head), !val);    push_int(env, !val);
696  }  }
697    
698  /* 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 828  extern void def(environment *env) Line 735  extern void def(environment *env)
735    toss(env); toss(env);    toss(env); toss(env);
736  }  }
737    
738    extern void clear(environment *);
739    void forget_sym(symbol **);
740    
741  /* Quit stack. */  /* Quit stack. */
742  extern void quit(environment *env)  extern void quit(environment *env)
743  {  {
744      long i;
745    
746      clear(env);
747      if (env->err) return;
748      for(i= 0; i<HASHTBLSIZE; i++) {
749        while(env->symbols[i]!= NULL) {
750          forget_sym(&(env->symbols[i]));
751        }
752        env->symbols[i]= NULL;
753      }
754    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
755  }  }
756    
# Line 856  extern void words(environment *env) Line 776  extern void words(environment *env)
776    }    }
777  }  }
778    
779    /* Internal forget function */
780    void forget_sym(symbol **hash_entry) {
781      symbol *temp;
782    
783      temp= *hash_entry;
784      *hash_entry= (*hash_entry)->next;
785      
786      if(temp->val!=NULL) {
787        free_val(temp->val);
788      }
789      free(temp->id);
790      free(temp);
791    }
792    
793  /* Forgets a symbol (remove it from the hash table) */  /* Forgets a symbol (remove it from the hash table) */
794  extern void forget(environment *env)  extern void forget(environment *env)
795  {  {
796    char* sym_id;    char* sym_id;
797    stackitem *stack_head= env->head;    stackitem *stack_head= env->head;
   symbol **hash_entry, *temp;  
798    
799    if(stack_head==NULL) {    if(stack_head==NULL) {
800      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 878  extern void forget(environment *env) Line 811  extern void forget(environment *env)
811    sym_id= ((symbol*)(stack_head->item->content.ptr))->id;    sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
812    toss(env);    toss(env);
813    
814    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);  
815  }  }
816    
817  /* Returns the current error number to the stack */  /* Returns the current error number to the stack */
818  extern void errn(environment *env){  extern void errn(environment *env){
819    push_int(&(env->head), env->err);    push_int(env, env->err);
820  }  }
821    
822    extern void read(environment*);
823    
824  int main()  int main()
825  {  {
826    environment myenv;    environment myenv;
   char in_string[100];  
827    
828    init_env(&myenv);    init_env(&myenv);
829    
830    printf("okidok\n ");    while(1) {
831        if(myenv.in_string==NULL) {
832    while(fgets(in_string, 100, stdin) != NULL) {        nl();
833      stack_read(&myenv, in_string);        printstack(&myenv);
834          printf("> ");
835        }
836        read(&myenv);
837      if(myenv.err) {      if(myenv.err) {
838        printf("(error %d) ", myenv.err);        printf("(error %d) ", myenv.err);
839        myenv.err=0;        myenv.err=0;
840        } else if(myenv.head!=NULL
841                  && myenv.head->item->type==symb
842                  && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') {
843          toss(&myenv);             /* No error check in main */
844          eval(&myenv);
845      }      }
     printf("okidok\n ");  
846    }    }
847    quit(&myenv);    quit(&myenv);
848    return EXIT_FAILURE;    return EXIT_FAILURE;
# Line 941  extern void sx_2b(environment *env) { Line 874  extern void sx_2b(environment *env) {
874      strcpy(new_string, b_val->content.ptr);      strcpy(new_string, b_val->content.ptr);
875      strcat(new_string, a_val->content.ptr);      strcat(new_string, a_val->content.ptr);
876      free_val(a_val); free_val(b_val);      free_val(a_val); free_val(b_val);
877      push_cstring(&(env->head), new_string);      push_cstring(env, new_string);
878      free(new_string);      free(new_string);
879      return;      return;
880    }    }
# Line 955  extern void sx_2b(environment *env) { Line 888  extern void sx_2b(environment *env) {
888    a=env->head->item->content.val;    a=env->head->item->content.val;
889    toss(env);    toss(env);
890    if(env->err) return;    if(env->err) return;
891    b=env->head->item->content.val;    if(env->head->item->refcount == 1)
892    toss(env);      env->head->item->content.val += a;
893    if(env->err) return;    else {
894    push_int(&(env->head), a+b);      b=env->head->item->content.val;
895        toss(env);
896        if(env->err) return;
897        push_int(env, a+b);
898      }
899  }  }
900    
901  /* - */  /* - */
902  extern void sx_2d(environment *env) {  extern void sx_2d(environment *env) {
903    int a;    int a, b;
904    
905    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
906      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 980  extern void sx_2d(environment *env) { Line 917  extern void sx_2d(environment *env) {
917    a=env->head->item->content.val;    a=env->head->item->content.val;
918    toss(env);    toss(env);
919    if(env->err) return;    if(env->err) return;
920    env->head->item->content.val -= a;    if(env->head->item->refcount == 1)
921        env->head->item->content.val -= a;
922      else {
923        b=env->head->item->content.val;
924        toss(env);
925        if(env->err) return;
926        push_int(env, b-a);
927      }
928    }
929    
930    /* > */
931    extern void sx_3e(environment *env) {
932      int a, b;
933    
934      if((env->head)==NULL || env->head->next==NULL) {
935        printerr("Too Few Arguments");
936        env->err=1;
937        return;
938      }
939      
940      if(env->head->item->type!=integer
941         || env->head->next->item->type!=integer) {
942        printerr("Bad Argument Type");
943        env->err=2;
944        return;
945      }
946      a=env->head->item->content.val;
947      toss(env);
948      if(env->err) return;
949      if(env->head->item->refcount == 1)
950        env->head->item->content.val = (env->head->item->content.val > a);
951      else {
952        b=env->head->item->content.val;
953        toss(env);
954        if(env->err) return;
955        push_int(env, b>a);
956      }
957  }  }
958    
959  /* Return copy of a value */  /* Return copy of a value */
# Line 1035  extern void dup(environment *env) { Line 1008  extern void dup(environment *env) {
1008      env->err=1;      env->err=1;
1009      return;      return;
1010    }    }
1011    push_val(&(env->head), copy_val(env->head->item));    push_val(env, copy_val(env->head->item));
1012  }  }
1013    
1014  /* "if", If-Then */  /* "if", If-Then */
# Line 1109  extern void ifelse(environment *env) { Line 1082  extern void ifelse(environment *env) {
1082  extern void sx_7768696c65(environment *env) {  extern void sx_7768696c65(environment *env) {
1083    
1084    int truth;    int truth;
1085      value *loop, *test;
1086    
1087    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
1088      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 1116  extern void sx_7768696c65(environment *e Line 1090  extern void sx_7768696c65(environment *e
1090      return;      return;
1091    }    }
1092    
1093      loop= env->head->item;
1094      loop->refcount++;
1095      toss(env); if(env->err) return;
1096    
1097      test= env->head->item;
1098      test->refcount++;
1099      toss(env); if(env->err) return;
1100    
1101    do {    do {
1102      swap(env); if(env->err) return;      push_val(env, test);
1103      dup(env); if(env->err) return;      eval(env);
     eval(env); if(env->err) return;  
1104            
1105      if(env->head->item->type != integer) {      if(env->head->item->type != integer) {
1106        printerr("Bad Argument Type");        printerr("Bad Argument Type");
# Line 1128  extern void sx_7768696c65(environment *e Line 1109  extern void sx_7768696c65(environment *e
1109      }      }
1110            
1111      truth= env->head->item->content.val;      truth= env->head->item->content.val;
       
1112      toss(env); if(env->err) return;      toss(env); if(env->err) return;
     swap(env); if(env->err) return;  
1113            
1114      if(truth) {      if(truth) {
1115        dup(env);        push_val(env, loop);
1116        eval(env);        eval(env);
1117      } else {      } else {
1118        toss(env);        toss(env);
       toss(env);  
1119      }      }
1120        
1121    } while(truth);    } while(truth);
1122    
1123      free_val(test);
1124      free_val(loop);
1125    }
1126    
1127    /* For-loop */
1128    extern void sx_666f72(environment *env) {
1129      
1130      value *loop, *foo;
1131      stackitem *iterator;
1132      
1133      if((env->head)==NULL || env->head->next==NULL) {
1134        printerr("Too Few Arguments");
1135        env->err=1;
1136        return;
1137      }
1138    
1139      if(env->head->next->item->type != list) {
1140        printerr("Bad Argument Type");
1141        env->err=2;
1142        return;
1143      }
1144    
1145      loop= env->head->item;
1146      loop->refcount++;
1147      toss(env); if(env->err) return;
1148    
1149      foo= env->head->item;
1150      foo->refcount++;
1151      toss(env); if(env->err) return;
1152    
1153      iterator= foo->content.ptr;
1154    
1155      while(iterator!=NULL) {
1156        push_val(env, iterator->item);
1157        push_val(env, loop);
1158        eval(env); if(env->err) return;
1159        iterator= iterator->next;
1160      }
1161    
1162      free_val(loop);
1163      free_val(foo);
1164    }
1165    
1166    /* 'to' */
1167    extern void to(environment *env) {
1168      int i, start, ending;
1169      stackitem *temp_head;
1170      value *temp_val;
1171      
1172      if((env->head)==NULL || env->head->next==NULL) {
1173        printerr("Too Few Arguments");
1174        env->err=1;
1175        return;
1176      }
1177    
1178      if(env->head->item->type!=integer
1179         || env->head->next->item->type!=integer) {
1180        printerr("Bad Argument Type");
1181        env->err=2;
1182        return;
1183      }
1184    
1185      ending= env->head->item->content.val;
1186      toss(env); if(env->err) return;
1187      start= env->head->item->content.val;
1188      toss(env); if(env->err) return;
1189    
1190      temp_head= env->head;
1191      env->head= NULL;
1192    
1193      if(ending>=start) {
1194        for(i= ending; i>=start; i--)
1195          push_int(env, i);
1196      } else {
1197        for(i= ending; i<=start; i++)
1198          push_int(env, i);
1199      }
1200    
1201      temp_val= malloc(sizeof(value));
1202      temp_val->content.ptr= env->head;
1203      temp_val->refcount= 1;
1204      temp_val->type= list;
1205      env->head= temp_head;
1206      push_val(env, temp_val);
1207    }
1208    
1209    /* Read a string */
1210    extern void readline(environment *env) {
1211      char in_string[101];
1212    
1213      fgets(in_string, 100, stdin);
1214      push_cstring(env, in_string);
1215    }
1216    
1217    /* Read a value and place on stack */
1218    extern void read(environment *env) {
1219      const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";
1220      const char strform[]= "\"%[^\"]\"%n";
1221      const char intform[]= "%i%n";
1222      const char blankform[]= "%*[ \t]%n";
1223      const char ebrackform[]= "%*1[]]%n";
1224      const char semicform[]= "%*1[;]%n";
1225      const char bbrackform[]= "%*1[[]%n";
1226    
1227      int itemp, readlength= -1;
1228      static int depth= 0;
1229      char *rest, *match;
1230      size_t inlength;
1231    
1232      if(env->in_string==NULL) {
1233        if(depth > 0) {
1234          printf("]> ");
1235        }
1236        readline(env); if(env->err) return;
1237        
1238        env->in_string= malloc(strlen(env->head->item->content.ptr)+1);
1239        env->free_string= env->in_string; /* Save the original pointer */
1240        strcpy(env->in_string, env->head->item->content.ptr);
1241        toss(env); if(env->err) return;
1242      }
1243      
1244      inlength= strlen(env->in_string)+1;
1245      match= malloc(inlength);
1246      rest= malloc(inlength);
1247    
1248      if(sscanf(env->in_string, blankform, &readlength)!=EOF
1249         && readlength != -1) {
1250        ;
1251      } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF
1252                && readlength != -1) {
1253        push_int(env, itemp);
1254      } else if(sscanf(env->in_string, strform, match, &readlength) != EOF
1255                && readlength != -1) {
1256        push_cstring(env, match);
1257      } else if(sscanf(env->in_string, symbform, match, &readlength) != EOF
1258                && readlength != -1) {
1259        push_sym(env, match);
1260      } else if(sscanf(env->in_string, ebrackform, &readlength) != EOF
1261                && readlength != -1) {
1262        pack(env); if(env->err) return;
1263        if(depth != 0) depth--;
1264      } else if(sscanf(env->in_string, semicform, &readlength) != EOF
1265                && readlength != -1) {
1266        push_sym(env, ";");
1267      } else if(sscanf(env->in_string, bbrackform, &readlength) != EOF
1268                && readlength != -1) {
1269        push_sym(env, "[");
1270        depth++;
1271      } else {
1272        free(env->free_string);
1273        env->in_string = env->free_string = NULL;
1274        free(match);
1275      }
1276      if ( env->in_string != NULL) {
1277        env->in_string += readlength;
1278      }
1279    
1280      if(depth)
1281        return read(env);
1282  }  }

Legend:
Removed from v.1.60  
changed lines
  Added in v.1.81

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26