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

Diff of /stack/stack.c

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

revision 1.63 by masse, Fri Feb 8 13:01:16 2002 UTC revision 1.84 by teddy, Fri Feb 15 01:21:13 2002 UTC
# Line 1  Line 1 
1  /* printf, sscanf, fgets, fprintf */  /* printf, sscanf, fgets, fprintf, fopen, perror */
2  #include <stdio.h>  #include <stdio.h>
3  /* exit, EXIT_SUCCESS, malloc, free */  /* exit, EXIT_SUCCESS, malloc, free */
4  #include <stdlib.h>  #include <stdlib.h>
# Line 8  Line 8 
8  #include <dlfcn.h>  #include <dlfcn.h>
9  /* strcmp, strcpy, strlen, strcat, strdup */  /* strcmp, strcpy, strlen, strcat, strdup */
10  #include <string.h>  #include <string.h>
11    /* getopt, STDIN_FILENO, STDOUT_FILENO */
12    #include <unistd.h>
13    /* EX_NOINPUT, EX_USAGE */
14    #include <sysexits.h>
15    /* mtrace, muntrace */
16    #include <mcheck.h>
17    
18  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 2048
19    
20  /* First, define some types. */  /* First, define some types. */
21    
# Line 58  typedef struct { Line 64  typedef struct {
64    stackitem *head;              /* Head of the stack */    stackitem *head;              /* Head of the stack */
65    hashtbl symbols;              /* Hash table of all variable bindings */    hashtbl symbols;              /* Hash table of all variable bindings */
66    int err;                      /* Error flag */    int err;                      /* Error flag */
67    int non_eval_flag;    char *in_string;              /* Input pending to be read */
68      char *free_string;            /* Free this string when all input is
69                                       read from in_string */
70      FILE *inputstream;            /* stdin or a file, most likely */
71      int interactive;              /* print prompts, stack, etc */
72  } environment;  } environment;
73    
74  /* A type for pointers to external functions */  /* A type for pointers to external functions */
# Line 70  void init_env(environment *env) Line 80  void init_env(environment *env)
80  {  {
81    int i;    int i;
82    
83    env->err= 0;    env->head= NULL;
   env->non_eval_flag= 0;  
84    for(i= 0; i<HASHTBLSIZE; i++)    for(i= 0; i<HASHTBLSIZE; i++)
85      env->symbols[i]= NULL;      env->symbols[i]= NULL;
86      env->err= 0;
87      env->in_string= NULL;
88      env->free_string= NULL;
89      env->inputstream= stdin;
90      env->interactive= 1;
91  }  }
92    
93  void printerr(const char* in_string) {  void printerr(const char* in_string) {
# Line 98  void free_val(value *val){ Line 112  void free_val(value *val){
112          free(item);             /* free the stackitem */          free(item);             /* free the stackitem */
113          item=temp;              /* go to next stackitem */          item=temp;              /* go to next stackitem */
114        }        }
       free(val);                /* Free the actual list value */  
115        break;        break;
116      case integer:      case integer:
117      case func:      case func:
118          break;
119      case symb:      case symb:
120          free(((symbol*)(val->content.ptr))->id);
121          if(((symbol*)(val->content.ptr))->val!=NULL)
122            free_val(((symbol*)(val->content.ptr))->val);
123          free(val->content.ptr);
124        break;        break;
125      }      }
126        free(val);          /* Free the actual value structure */
127    }    }
128  }  }
129    
# Line 153  symbol **hash(hashtbl in_hashtbl, const Line 172  symbol **hash(hashtbl in_hashtbl, const
172    }    }
173  }  }
174    
 /* Generic push function. */  
 void push(stackitem** stack_head, stackitem* in_item)  
 {  
   in_item->next= *stack_head;  
   *stack_head= in_item;  
 }  
   
175  /* Push a value onto the stack */  /* Push a value onto the stack */
176  void push_val(stackitem **stack_head, value *val)  void push_val(environment *env, value *val)
177  {  {
178    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
179    new_item->item= val;    new_item->item= val;
180    val->refcount++;    val->refcount++;
181    push(stack_head, new_item);    new_item->next= env->head;
182      env->head= new_item;
183  }  }
184    
185  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
186  void push_int(stackitem **stack_head, int in_val)  void push_int(environment *env, int in_val)
187  {  {
188    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
   stackitem *new_item= malloc(sizeof(stackitem));  
   new_item->item= new_value;  
189        
190    new_value->content.val= in_val;    new_value->content.val= in_val;
191    new_value->type= integer;    new_value->type= integer;
192    new_value->refcount=1;    new_value->refcount= 0;
193    
194    push(stack_head, new_item);    push_val(env, new_value);
195  }  }
196    
197  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
198  void push_cstring(stackitem **stack_head, const char *in_string)  void push_cstring(environment *env, const char *in_string)
199  {  {
200    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
   stackitem *new_item= malloc(sizeof(stackitem));  
   new_item->item=new_value;  
201    
202    new_value->content.ptr= malloc(strlen(in_string)+1);    new_value->content.ptr= malloc(strlen(in_string)+1);
203    strcpy(new_value->content.ptr, in_string);    strcpy(new_value->content.ptr, in_string);
204    new_value->type= string;    new_value->type= string;
205    new_value->refcount=1;    new_value->refcount= 0;
206    
207    push(stack_head, new_item);    push_val(env, new_value);
208  }  }
209    
210  /* 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 228  char *mangle_str(const char *old_string)
228  }  }
229    
230  extern void mangle(environment *env){  extern void mangle(environment *env){
   value *new_value;  
231    char *new_string;    char *new_string;
232    
233    if((env->head)==NULL) {    if((env->head)==NULL) {
# Line 239  extern void mangle(environment *env){ Line 247  extern void mangle(environment *env){
247    toss(env);    toss(env);
248    if(env->err) return;    if(env->err) return;
249    
250    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);  
251  }  }
252    
253  /* Push a symbol onto the stack. */  /* Push a symbol onto the stack. */
254  void push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
255  {  {
   stackitem *new_item;          /* The new stack item */  
   /* ...which will contain... */  
256    value *new_value;             /* A new symbol value */    value *new_value;             /* A new symbol value */
257    /* ...which might point to... */    /* ...which might point to... */
258    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 265  void push_sym(environment *env, const ch
265    const char *dlerr;            /* Dynamic linker error */    const char *dlerr;            /* Dynamic linker error */
266    char *mangled;                /* Mangled function name */    char *mangled;                /* Mangled function name */
267    
   /* Create a new stack item containing a new value */  
   new_item= malloc(sizeof(stackitem));  
268    new_value= malloc(sizeof(value));    new_value= malloc(sizeof(value));
   new_item->item=new_value;  
269    
270    /* The new value is a symbol */    /* The new value is a symbol */
271    new_value->type= symb;    new_value->type= symb;
# Line 311  void push_sym(environment *env, const ch Line 309  void push_sym(environment *env, const ch
309        new_fvalue->refcount= 1;        new_fvalue->refcount= 1;
310      }      }
311    }    }
312    push(&(env->head), new_item);    push_val(env, new_value);
313  }  }
314    
315  /* Print newline. */  /* Print newline. */
# Line 351  extern void type(environment *env){ Line 349  extern void type(environment *env){
349  }      }    
350    
351  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
352  void print_h(stackitem *stack_head)  void print_h(stackitem *stack_head, int noquote)
353  {  {
354    switch(stack_head->item->type) {    switch(stack_head->item->type) {
355    case integer:    case integer:
356      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
357      break;      break;
358    case string:    case string:
359      printf("%s", (char*)stack_head->item->content.ptr);      if(noquote)
360          printf("%s", (char*)stack_head->item->content.ptr);
361        else
362          printf("\"%s\"", (char*)stack_head->item->content.ptr);
363      break;      break;
364    case symb:    case symb:
365      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 372  void print_h(stackitem *stack_head)
372      stack_head=(stackitem *)(stack_head->item->content.ptr);      stack_head=(stackitem *)(stack_head->item->content.ptr);
373      printf("[ ");      printf("[ ");
374      while(stack_head != NULL) {      while(stack_head != NULL) {
375        print_h(stack_head);        print_h(stack_head, noquote);
376        printf(" ");        printf(" ");
377        stack_head=stack_head->next;        stack_head=stack_head->next;
378      }      }
# Line 386  extern void print_(environment *env) { Line 387  extern void print_(environment *env) {
387      env->err=1;      env->err=1;
388      return;      return;
389    }    }
390    print_h(env->head);    print_h(env->head, 0);
391      nl();
392  }  }
393    
394  /* 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 399  extern void print(environment *env)
399    toss(env);    toss(env);
400  }  }
401    
402    extern void princ_(environment *env) {
403      if(env->head==NULL) {
404        printerr("Too Few Arguments");
405        env->err=1;
406        return;
407      }
408      print_h(env->head, 1);
409    }
410    
411    /* Prints the top element of the stack and then discards it. */
412    extern void princ(environment *env)
413    {
414      princ_(env);
415      if(env->err) return;
416      toss(env);
417    }
418    
419  /* Only to be called by function printstack. */  /* Only to be called by function printstack. */
420  void print_st(stackitem *stack_head, long counter)  void print_st(stackitem *stack_head, long counter)
421  {  {
422    if(stack_head->next != NULL)    if(stack_head->next != NULL)
423      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
424    printf("%ld: ", counter);    printf("%ld: ", counter);
425    print_h(stack_head);    print_h(stack_head, 0);
426    nl();    nl();
427  }  }
428    
# Line 411  void print_st(stackitem *stack_head, lon Line 430  void print_st(stackitem *stack_head, lon
430  extern void printstack(environment *env)  extern void printstack(environment *env)
431  {  {
432    if(env->head == NULL) {    if(env->head == NULL) {
433        printf("Stack Empty\n");
434      return;      return;
435    }    }
436    print_st(env->head, 1);    print_st(env->head, 1);
   nl();  
437  }  }
438    
439  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 475  extern void rcl(environment *env) Line 494  extern void rcl(environment *env)
494    }    }
495    toss(env);            /* toss the symbol */    toss(env);            /* toss the symbol */
496    if(env->err) return;    if(env->err) return;
497    push_val(&(env->head), val); /* Return its bound value */    push_val(env, val); /* Return its bound value */
498  }  }
499    
 void stack_read(environment*, char*);  
   
500  /* 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
501     function value, and if it is, toss the symbol and execute the     function value, and if it is, toss the symbol and execute the
502     function. */     function. */
# Line 488  extern void eval(environment *env) Line 505  extern void eval(environment *env)
505    funcp in_func;    funcp in_func;
506    value* temp_val;    value* temp_val;
507    stackitem* iterator;    stackitem* iterator;
508    char* temp_string;  
509     eval_start:
510    
511    if(env->head==NULL) {    if(env->head==NULL) {
512      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 502  extern void eval(environment *env) Line 520  extern void eval(environment *env)
520      rcl(env);                   /* get its contents */      rcl(env);                   /* get its contents */
521      if(env->err) return;      if(env->err) return;
522      if(env->head->item->type!=symb){ /* don't recurse symbols */      if(env->head->item->type!=symb){ /* don't recurse symbols */
523        return eval(env);         /* evaluate the value */        goto eval_start;
524      }      }
525      return;      return;
526    
# Line 521  extern void eval(environment *env) Line 539  extern void eval(environment *env)
539      if(env->err) return;      if(env->err) return;
540      iterator= (stackitem*)temp_val->content.ptr;      iterator= (stackitem*)temp_val->content.ptr;
541      while(iterator!=NULL) {      while(iterator!=NULL) {
542        push_val(&(env->head), iterator->item);        push_val(env, iterator->item);
543        if(env->head->item->type==symb        if(env->head->item->type==symb
544          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
545          toss(env);          toss(env);
546          if(env->err) return;          if(env->err) return;
547          if(iterator->next == NULL){          if(iterator->next == NULL){
548            free_val(temp_val);            free_val(temp_val);
549            return eval(env);            goto eval_start;
550          }          }
551          eval(env);          eval(env);
552          if(env->err) return;          if(env->err) return;
# Line 538  extern void eval(environment *env) Line 556  extern void eval(environment *env)
556      free_val(temp_val);      free_val(temp_val);
557      return;      return;
558    
559      /* 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:  
560      return;      return;
561    }    }
562  }  }
# Line 588  extern void rev(environment *env){ Line 591  extern void rev(environment *env){
591  /* Make a list. */  /* Make a list. */
592  extern void pack(environment *env)  extern void pack(environment *env)
593  {  {
   void* delimiter;  
594    stackitem *iterator, *temp;    stackitem *iterator, *temp;
595    value *pack;    value *pack;
596    
   delimiter= env->head->item->content.ptr; /* Get delimiter */  
   toss(env);  
   
597    iterator= env->head;    iterator= env->head;
598    
599    if(iterator==NULL || iterator->item->content.ptr==delimiter) {    if(iterator==NULL
600         || (iterator->item->type==symb
601         && ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) {
602      temp= NULL;      temp= NULL;
603      toss(env);      toss(env);
604    } else {    } else {
605      /* Search for first delimiter */      /* Search for first delimiter */
606      while(iterator->next!=NULL      while(iterator->next!=NULL
607            && iterator->next->item->content.ptr!=delimiter)            && (iterator->next->item->type!=symb
608              || ((symbol*)(iterator->next->item->content.ptr))->id[0]!='['))
609        iterator= iterator->next;        iterator= iterator->next;
610            
611      /* Extract list */      /* Extract list */
# Line 619  extern void pack(environment *env) Line 621  extern void pack(environment *env)
621    pack= malloc(sizeof(value));    pack= malloc(sizeof(value));
622    pack->type= list;    pack->type= list;
623    pack->content.ptr= temp;    pack->content.ptr= temp;
624    pack->refcount= 1;    pack->refcount= 0;
   
   temp= malloc(sizeof(stackitem));  
   temp->item= pack;  
625    
626    push(&(env->head), temp);    push_val(env, pack);
627    rev(env);    rev(env);
628  }  }
629    
 /* 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);  
 }  
   
630  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
631  extern void expand(environment *env)  extern void expand(environment *env)
632  {  {
# Line 763  extern void eq(environment *env) Line 683  extern void eq(environment *env)
683    result= (left==right);    result= (left==right);
684        
685    toss(env); toss(env);    toss(env); toss(env);
686    push_int(&(env->head), result);    push_int(env, result);
687  }  }
688    
689  /* Negates the top element on the stack. */  /* Negates the top element on the stack. */
# Line 785  extern void not(environment *env) Line 705  extern void not(environment *env)
705    
706    val= env->head->item->content.val;    val= env->head->item->content.val;
707    toss(env);    toss(env);
708    push_int(&(env->head), !val);    push_int(env, !val);
709  }  }
710    
711  /* 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 748  extern void def(environment *env)
748    toss(env); toss(env);    toss(env); toss(env);
749  }  }
750    
751    extern void clear(environment *);
752    void forget_sym(symbol **);
753    extern void words(environment *);
754    
755  /* Quit stack. */  /* Quit stack. */
756  extern void quit(environment *env)  extern void quit(environment *env)
757  {  {
758      long i;
759    
760      clear(env);
761    
762      if (env->err) return;
763      for(i= 0; i<HASHTBLSIZE; i++) {
764        while(env->symbols[i]!= NULL) {
765          forget_sym(&(env->symbols[i]));
766        }
767        env->symbols[i]= NULL;
768      }
769    
770      if(env->free_string!=NULL)
771        free(env->free_string);
772      
773      muntrace();
774    
775    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
776  }  }
777    
# Line 856  extern void words(environment *env) Line 797  extern void words(environment *env)
797    }    }
798  }  }
799    
800    /* Internal forget function */
801    void forget_sym(symbol **hash_entry) {
802      symbol *temp;
803    
804      temp= *hash_entry;
805      *hash_entry= (*hash_entry)->next;
806      
807      if(temp->val!=NULL) {
808        free_val(temp->val);
809      }
810      free(temp->id);
811      free(temp);
812    }
813    
814  /* Forgets a symbol (remove it from the hash table) */  /* Forgets a symbol (remove it from the hash table) */
815  extern void forget(environment *env)  extern void forget(environment *env)
816  {  {
817    char* sym_id;    char* sym_id;
818    stackitem *stack_head= env->head;    stackitem *stack_head= env->head;
   symbol **hash_entry, *temp;  
819    
820    if(stack_head==NULL) {    if(stack_head==NULL) {
821      printerr("Too Few Arguments");      printerr("Too Few Arguments");
# Line 878  extern void forget(environment *env) Line 832  extern void forget(environment *env)
832    sym_id= ((symbol*)(stack_head->item->content.ptr))->id;    sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
833    toss(env);    toss(env);
834    
835    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);  
836  }  }
837    
838  /* Returns the current error number to the stack */  /* Returns the current error number to the stack */
839  extern void errn(environment *env){  extern void errn(environment *env){
840    push_int(&(env->head), env->err);    push_int(env, env->err);
841  }  }
842    
843  int main()  extern void sx_72656164(environment*);
844    
845    int main(int argc, char **argv)
846  {  {
847    environment myenv;    environment myenv;
848    char in_string[100];  
849      int c;                        /* getopt option character */
850    
851      mtrace();
852    
853    init_env(&myenv);    init_env(&myenv);
854    
855    printf("okidok\n ");    myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
856    
857    while(fgets(in_string, 100, stdin) != NULL) {    while ((c = getopt (argc, argv, "i")) != -1)
858      stack_read(&myenv, in_string);      switch (c)
859          {
860          case 'i':
861            myenv.interactive = 1;
862            break;
863          case '?':
864            fprintf (stderr,
865                     "Unknown option character `\\x%x'.\n",
866                     optopt);
867            return EX_USAGE;
868          default:
869            abort ();
870          }
871      
872      if (optind < argc) {
873        myenv.interactive = 0;
874        myenv.inputstream= fopen(argv[optind], "r");
875        if(myenv.inputstream== NULL) {
876          perror(argv[0]);
877          exit (EX_NOINPUT);
878        }
879      }
880    
881      while(1) {
882        if(myenv.in_string==NULL && myenv.interactive) {
883          nl();
884          printstack(&myenv);
885          printf("> ");
886        }
887        sx_72656164(&myenv);
888      if(myenv.err) {      if(myenv.err) {
889        printf("(error %d) ", myenv.err);        printf("(error %d) ", myenv.err);
890          if (myenv.err==4)
891            return EX_NOINPUT;
892        myenv.err=0;        myenv.err=0;
893        } else if(myenv.head!=NULL
894                  && myenv.head->item->type==symb
895                  && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') {
896          toss(&myenv);             /* No error check in main */
897          eval(&myenv);
898      }      }
     printf("okidok\n ");  
899    }    }
900    quit(&myenv);    quit(&myenv);
901    return EXIT_FAILURE;    return EXIT_FAILURE;
# Line 941  extern void sx_2b(environment *env) { Line 927  extern void sx_2b(environment *env) {
927      strcpy(new_string, b_val->content.ptr);      strcpy(new_string, b_val->content.ptr);
928      strcat(new_string, a_val->content.ptr);      strcat(new_string, a_val->content.ptr);
929      free_val(a_val); free_val(b_val);      free_val(a_val); free_val(b_val);
930      push_cstring(&(env->head), new_string);      push_cstring(env, new_string);
931      free(new_string);      free(new_string);
932      return;      return;
933    }    }
# Line 961  extern void sx_2b(environment *env) { Line 947  extern void sx_2b(environment *env) {
947      b=env->head->item->content.val;      b=env->head->item->content.val;
948      toss(env);      toss(env);
949      if(env->err) return;      if(env->err) return;
950      push_int(&(env->head), a+b);      push_int(env, a+b);
951    }    }
952  }  }
953    
# Line 990  extern void sx_2d(environment *env) { Line 976  extern void sx_2d(environment *env) {
976      b=env->head->item->content.val;      b=env->head->item->content.val;
977      toss(env);      toss(env);
978      if(env->err) return;      if(env->err) return;
979      push_int(&(env->head), b-a);      push_int(env, b-a);
980    }    }
981  }  }
982    
# Line 1019  extern void sx_3e(environment *env) { Line 1005  extern void sx_3e(environment *env) {
1005      b=env->head->item->content.val;      b=env->head->item->content.val;
1006      toss(env);      toss(env);
1007      if(env->err) return;      if(env->err) return;
1008      push_int(&(env->head), b>a);      push_int(env, b>a);
1009    }    }
1010  }  }
1011    
# Line 1068  value *copy_val(value *old_value){ Line 1054  value *copy_val(value *old_value){
1054    return new_value;    return new_value;
1055  }  }
1056    
1057  /* duplicates an item on the stack */  /* "dup"; duplicates an item on the stack */
1058  extern void dup(environment *env) {  extern void sx_647570(environment *env) {
1059    if((env->head)==NULL) {    if((env->head)==NULL) {
1060      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1061      env->err=1;      env->err=1;
1062      return;      return;
1063    }    }
1064    push_val(&(env->head), copy_val(env->head->item));    push_val(env, copy_val(env->head->item));
1065  }  }
1066    
1067  /* "if", If-Then */  /* "if", If-Then */
# Line 1166  extern void sx_7768696c65(environment *e Line 1152  extern void sx_7768696c65(environment *e
1152    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1153    
1154    do {    do {
1155      push_val(&(env->head), test);      push_val(env, test);
1156      eval(env);      eval(env);
1157            
1158      if(env->head->item->type != integer) {      if(env->head->item->type != integer) {
# Line 1179  extern void sx_7768696c65(environment *e Line 1165  extern void sx_7768696c65(environment *e
1165      toss(env); if(env->err) return;      toss(env); if(env->err) return;
1166            
1167      if(truth) {      if(truth) {
1168        push_val(&(env->head), loop);        push_val(env, loop);
1169        eval(env);        eval(env);
1170      } else {      } else {
1171        toss(env);        toss(env);
# Line 1190  extern void sx_7768696c65(environment *e Line 1176  extern void sx_7768696c65(environment *e
1176    free_val(test);    free_val(test);
1177    free_val(loop);    free_val(loop);
1178  }  }
1179    
1180    /* "for"; For-loop */
1181    extern void sx_666f72(environment *env) {
1182      
1183      value *loop, *foo;
1184      stackitem *iterator;
1185      
1186      if((env->head)==NULL || env->head->next==NULL) {
1187        printerr("Too Few Arguments");
1188        env->err=1;
1189        return;
1190      }
1191    
1192      if(env->head->next->item->type != list) {
1193        printerr("Bad Argument Type");
1194        env->err=2;
1195        return;
1196      }
1197    
1198      loop= env->head->item;
1199      loop->refcount++;
1200      toss(env); if(env->err) return;
1201    
1202      foo= env->head->item;
1203      foo->refcount++;
1204      toss(env); if(env->err) return;
1205    
1206      iterator= foo->content.ptr;
1207    
1208      while(iterator!=NULL) {
1209        push_val(env, iterator->item);
1210        push_val(env, loop);
1211        eval(env); if(env->err) return;
1212        iterator= iterator->next;
1213      }
1214    
1215      free_val(loop);
1216      free_val(foo);
1217    }
1218    
1219    /* 'to' */
1220    extern void to(environment *env) {
1221      int i, start, ending;
1222      stackitem *temp_head;
1223      value *temp_val;
1224      
1225      if((env->head)==NULL || env->head->next==NULL) {
1226        printerr("Too Few Arguments");
1227        env->err=1;
1228        return;
1229      }
1230    
1231      if(env->head->item->type!=integer
1232         || env->head->next->item->type!=integer) {
1233        printerr("Bad Argument Type");
1234        env->err=2;
1235        return;
1236      }
1237    
1238      ending= env->head->item->content.val;
1239      toss(env); if(env->err) return;
1240      start= env->head->item->content.val;
1241      toss(env); if(env->err) return;
1242    
1243      temp_head= env->head;
1244      env->head= NULL;
1245    
1246      if(ending>=start) {
1247        for(i= ending; i>=start; i--)
1248          push_int(env, i);
1249      } else {
1250        for(i= ending; i<=start; i++)
1251          push_int(env, i);
1252      }
1253    
1254      temp_val= malloc(sizeof(value));
1255      temp_val->content.ptr= env->head;
1256      temp_val->refcount= 0;
1257      temp_val->type= list;
1258      env->head= temp_head;
1259      push_val(env, temp_val);
1260    }
1261    
1262    /* Read a string */
1263    extern void readline(environment *env) {
1264      char in_string[101];
1265    
1266      if(fgets(in_string, 100, env->inputstream)==NULL)
1267        push_cstring(env, "");
1268      else
1269        push_cstring(env, in_string);
1270    }
1271    
1272    /* "read"; Read a value and place on stack */
1273    extern void sx_72656164(environment *env) {
1274      const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";
1275      const char strform[]= "\"%[^\"]\"%n";
1276      const char intform[]= "%i%n";
1277      const char blankform[]= "%*[ \t]%n";
1278      const char ebrackform[]= "%*1[]]%n";
1279      const char semicform[]= "%*1[;]%n";
1280      const char bbrackform[]= "%*1[[]%n";
1281    
1282      int itemp, readlength= -1;
1283      static int depth= 0;
1284      char *match;
1285      size_t inlength;
1286    
1287      if(env->in_string==NULL) {
1288        if(depth > 0 && env->interactive) {
1289          printf("]> ");
1290        }
1291        readline(env); if(env->err) return;
1292    
1293        if(((char *)(env->head->item->content.ptr))[0]=='\0'){
1294          env->err= 4;              /* EOF */
1295          return;
1296        }
1297        
1298        env->in_string= malloc(strlen(env->head->item->content.ptr)+1);
1299        env->free_string= env->in_string; /* Save the original pointer */
1300        strcpy(env->in_string, env->head->item->content.ptr);
1301        toss(env); if(env->err) return;
1302      }
1303      
1304      inlength= strlen(env->in_string)+1;
1305      match= malloc(inlength);
1306    
1307      if(sscanf(env->in_string, blankform, &readlength)!=EOF
1308         && readlength != -1) {
1309        ;
1310      } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF
1311                && readlength != -1) {
1312        push_int(env, itemp);
1313      } else if(sscanf(env->in_string, strform, match, &readlength) != EOF
1314                && readlength != -1) {
1315        push_cstring(env, match);
1316      } else if(sscanf(env->in_string, symbform, match, &readlength) != EOF
1317                && readlength != -1) {
1318        push_sym(env, match);
1319      } else if(sscanf(env->in_string, ebrackform, &readlength) != EOF
1320                && readlength != -1) {
1321        pack(env); if(env->err) return;
1322        if(depth != 0) depth--;
1323      } else if(sscanf(env->in_string, semicform, &readlength) != EOF
1324                && readlength != -1) {
1325        push_sym(env, ";");
1326      } else if(sscanf(env->in_string, bbrackform, &readlength) != EOF
1327                && readlength != -1) {
1328        push_sym(env, "[");
1329        depth++;
1330      } else {
1331        free(env->free_string);
1332        env->in_string = env->free_string = NULL;
1333      }
1334      if ( env->in_string != NULL) {
1335        env->in_string += readlength;
1336      }
1337    
1338      free(match);
1339    
1340      if(depth)
1341        return sx_72656164(env);
1342    }

Legend:
Removed from v.1.63  
changed lines
  Added in v.1.84

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26