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

Diff of /stack/stack.c

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

revision 1.34 by masse, Tue Feb 5 23:26:46 2002 UTC revision 1.58 by masse, Fri Feb 8 03:45:00 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 */
10  #include <assert.h>  #include <string.h>
11    
12  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 65536
13    
# Line 18  typedef struct { Line 18  typedef struct {
18    enum {    enum {
19      integer,      integer,
20      string,      string,
     ref,                        /* Reference (to an element in the  
                                    hash table) */  
21      func,                       /* Function pointer */      func,                       /* Function pointer */
22      symb,      symb,
23      list      list
# 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 59  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;
62  } environment;  } environment;
63    
64  /* A type for pointers to external functions */  /* A type for pointers to external functions */
# Line 68  typedef void (*funcp)(environment *); /* Line 68  typedef void (*funcp)(environment *); /*
68  /* Initialize a newly created environment */  /* Initialize a newly created environment */
69  void init_env(environment *env)  void init_env(environment *env)
70  {  {
71    long i;    int i;
72    
73      env->err= 0;
74      env->non_eval_flag= 0;
75    for(i= 0; i<HASHTBLSIZE; i++)    for(i= 0; i<HASHTBLSIZE; i++)
76      env->symbols[i]= NULL;      env->symbols[i]= NULL;
77  }  }
78    
79    void printerr(const char* in_string) {
80      fprintf(stderr, "Err: %s\n", in_string);
81    }
82    
83    /* Throw away a value */
84    void free_val(value *val){
85      stackitem *item, *temp;
86    
87      val->refcount--;              /* Decrease the reference count */
88      if(val->refcount == 0){
89        switch (val->type){         /* and free the contents if necessary */
90        case string:
91          free(val->content.ptr);
92          break;
93        case list:                  /* lists needs to be freed recursively */
94          item=val->content.ptr;
95          while(item != NULL) {     /* for all stack items */
96            free_val(item->item);   /* free the value */
97            temp=item->next;        /* save next ptr */
98            free(item);             /* free the stackitem */
99            item=temp;              /* go to next stackitem */
100          }
101          free(val);                /* Free the actual list value */
102          break;
103        case integer:
104        case func:
105        case symb:
106          break;
107        }
108      }
109    }
110    
111    /* Discard the top element of the stack. */
112    extern void toss(environment *env)
113    {
114      stackitem *temp= env->head;
115    
116      if((env->head)==NULL) {
117        printerr("Too Few Arguments");
118        env->err=1;
119        return;
120      }
121      
122      free_val(env->head->item);    /* Free the value */
123      env->head= env->head->next;   /* Remove the top stack item */
124      free(temp);                   /* Free the old top stack item */
125    }
126    
127  /* 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. */
128  symbol **hash(hashtbl in_hashtbl, const char *in_string)  symbol **hash(hashtbl in_hashtbl, const char *in_string)
129  {  {
130    long i= 0;    int i= 0;
131    unsigned long out_hash= 0;    unsigned int out_hash= 0;
132    char key= '\0';    char key= '\0';
133    symbol **position;    symbol **position;
134        
# Line 104  symbol **hash(hashtbl in_hashtbl, const Line 154  symbol **hash(hashtbl in_hashtbl, const
154  }  }
155    
156  /* Generic push function. */  /* Generic push function. */
157  int push(stackitem** stack_head, stackitem* in_item)  void push(stackitem** stack_head, stackitem* in_item)
158  {  {
159    in_item->next= *stack_head;    in_item->next= *stack_head;
160    *stack_head= in_item;    *stack_head= in_item;
   return 1;  
161  }  }
162    
163  /* Push a value onto the stack */  /* Push a value onto the stack */
# Line 121  void push_val(stackitem **stack_head, va Line 170  void push_val(stackitem **stack_head, va
170  }  }
171    
172  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
173  int push_int(stackitem **stack_head, int in_val)  void push_int(stackitem **stack_head, int in_val)
174  {  {
175    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
176    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
# Line 132  int push_int(stackitem **stack_head, int Line 181  int push_int(stackitem **stack_head, int
181    new_value->refcount=1;    new_value->refcount=1;
182    
183    push(stack_head, new_item);    push(stack_head, new_item);
   return 1;  
184  }  }
185    
186  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
187  int push_cstring(stackitem **stack_head, const char *in_string)  void push_cstring(stackitem **stack_head, const char *in_string)
188  {  {
189    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
190    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
# Line 148  int push_cstring(stackitem **stack_head, Line 196  int push_cstring(stackitem **stack_head,
196    new_value->refcount=1;    new_value->refcount=1;
197    
198    push(stack_head, new_item);    push(stack_head, new_item);
199    return 1;  }
200    
201    /* Mangle a symbol name to a valid C identifier name */
202    char *mangle_str(const char *old_string){
203      char validchars[]
204        ="0123456789abcdef";
205      char *new_string, *current;
206    
207      new_string=malloc((strlen(old_string)*2)+4);
208      strcpy(new_string, "sx_");    /* Stack eXternal */
209      current=new_string+3;
210      while(old_string[0] != '\0'){
211        current[0]=validchars[(unsigned char)(old_string[0])/16];
212        current[1]=validchars[(unsigned char)(old_string[0])%16];
213        current+=2;
214        old_string++;
215      }
216      current[0]='\0';
217    
218      return new_string;            /* The caller must free() it */
219    }
220    
221    extern void mangle(environment *env){
222      value *new_value;
223      char *new_string;
224    
225      if((env->head)==NULL) {
226        printerr("Too Few Arguments");
227        env->err=1;
228        return;
229      }
230    
231      if(env->head->item->type!=string) {
232        printerr("Bad Argument Type");
233        env->err=2;
234        return;
235      }
236    
237      new_string= mangle_str((const char *)(env->head->item->content.ptr));
238    
239      toss(env);
240      if(env->err) return;
241    
242      new_value= malloc(sizeof(value));
243      new_value->content.ptr= new_string;
244      new_value->type= string;
245      new_value->refcount=1;
246    
247      push_val(&(env->head), new_value);
248  }  }
249    
250  /* Push a symbol onto the stack. */  /* Push a symbol onto the stack. */
251  int push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
252  {  {
253    stackitem *new_item;          /* The new stack item */    stackitem *new_item;          /* The new stack item */
254    /* ...which will contain... */    /* ...which will contain... */
# Line 165  int push_sym(environment *env, const cha Line 261  int push_sym(environment *env, const cha
261    void *funcptr;                /* A function pointer */    void *funcptr;                /* A function pointer */
262    
263    static void *handle= NULL;    /* Dynamic linker handle */    static void *handle= NULL;    /* Dynamic linker handle */
264      const char *dlerr;            /* Dynamic linker error */
265      char *mangled;                /* Mangled function name */
266    
267    /* Create a new stack item containing a new value */    /* Create a new stack item containing a new value */
268    new_item= malloc(sizeof(stackitem));    new_item= malloc(sizeof(stackitem));
# Line 197  int push_sym(environment *env, const cha Line 295  int push_sym(environment *env, const cha
295        handle= dlopen(NULL, RTLD_LAZY);        handle= dlopen(NULL, RTLD_LAZY);
296    
297      funcptr= dlsym(handle, in_string); /* Get function pointer */      funcptr= dlsym(handle, in_string); /* Get function pointer */
298      if(dlerror()==NULL) {       /* If a function was found */      dlerr=dlerror();
299        if(dlerr != NULL) {         /* If no function was found */
300          mangled=mangle_str(in_string);
301          funcptr= dlsym(handle, mangled); /* try mangling it */
302          free(mangled);
303          dlerr=dlerror();
304        }
305        if(dlerr==NULL) {           /* If a function was found */
306        new_fvalue= malloc(sizeof(value)); /* Create a new value */        new_fvalue= malloc(sizeof(value)); /* Create a new value */
307        new_fvalue->type=func;    /* The new value is a function pointer */        new_fvalue->type=func;    /* The new value is a function pointer */
308        new_fvalue->content.ptr=funcptr; /* Store function pointer */        new_fvalue->content.ptr=funcptr; /* Store function pointer */
# Line 207  int push_sym(environment *env, const cha Line 312  int push_sym(environment *env, const cha
312      }      }
313    }    }
314    push(&(env->head), new_item);    push(&(env->head), new_item);
   return 1;  
 }  
   
 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);  
     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("Stack empty");  
     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 */  
315  }  }
316    
317  /* Print newline. */  /* Print newline. */
# Line 260  extern void nl() Line 320  extern void nl()
320    printf("\n");    printf("\n");
321  }  }
322    
323  /* Prints the top element of the stack. */  /* Gets the type of a value */
324  void print_h(stackitem *stack_head)  extern void type(environment *env){
325  {    int typenum;
326    
327    if(stack_head==NULL) {    if((env->head)==NULL) {
328      printerr("Stack empty");      printerr("Too Few Arguments");
329        env->err=1;
330      return;      return;
331    }    }
332      typenum=env->head->item->type;
333      toss(env);
334      switch(typenum){
335      case integer:
336        push_sym(env, "integer");
337        break;
338      case string:
339        push_sym(env, "string");
340        break;
341      case symb:
342        push_sym(env, "symbol");
343        break;
344      case func:
345        push_sym(env, "function");
346        break;
347      case list:
348        push_sym(env, "list");
349        break;
350      default:
351        push_sym(env, "unknown");
352        break;
353      }
354    }    
355    
356    /* Prints the top element of the stack. */
357    void print_h(stackitem *stack_head)
358    {
359    switch(stack_head->item->type) {    switch(stack_head->item->type) {
360    case integer:    case integer:
361      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
362      break;      break;
363    case string:    case string:
364      printf("\"%s\"", (char*)stack_head->item->content.ptr);      printf("%s", (char*)stack_head->item->content.ptr);
365      break;      break;
366    case symb:    case symb:
367      printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id);      printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);
368        break;
369      case func:
370        printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
371        break;
372      case list:
373        /* A list is just a stack, so make stack_head point to it */
374        stack_head=(stackitem *)(stack_head->item->content.ptr);
375        printf("[ ");
376        while(stack_head != NULL) {
377          print_h(stack_head);
378          printf(" ");
379          stack_head=stack_head->next;
380        }
381        printf("]");
382      break;      break;
383    default:    default:
384      printf("%p", (funcp)(stack_head->item->content.ptr));      printf("#<unknown %p>", (stack_head->item->content.ptr));
385      break;      break;
386    }    }
387  }  }
388    
389  extern void print_(environment *env) {  extern void print_(environment *env) {
390      if(env->head==NULL) {
391        printerr("Too Few Arguments");
392        env->err=1;
393        return;
394      }
395    print_h(env->head);    print_h(env->head);
396  }  }
397    
# Line 293  extern void print_(environment *env) { Line 399  extern void print_(environment *env) {
399  extern void print(environment *env)  extern void print(environment *env)
400  {  {
401    print_(env);    print_(env);
402      if(env->err) return;
403    toss(env);    toss(env);
404  }  }
405    
# Line 306  void print_st(stackitem *stack_head, lon Line 413  void print_st(stackitem *stack_head, lon
413    nl();    nl();
414  }  }
415    
   
   
416  /* Prints the stack. */  /* Prints the stack. */
417  extern void printstack(environment *env)  extern void printstack(environment *env)
418  {  {
419    if(env->head != NULL) {    if(env->head == NULL) {
420      print_st(env->head, 1);      return;
     nl();  
   } else {  
     printerr("Stack empty");  
421    }    }
422      print_st(env->head, 1);
423      nl();
424  }  }
425    
426  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 324  extern void swap(environment *env) Line 428  extern void swap(environment *env)
428  {  {
429    stackitem *temp= env->head;    stackitem *temp= env->head;
430        
431    if((env->head)==NULL) {    if(env->head==NULL || env->head->next==NULL) {
432      printerr("Stack empty");      printerr("Too Few Arguments");
433      return;      env->err=1;
   }  
   
   if(env->head->next==NULL) {  
     printerr("Not enough arguments");  
434      return;      return;
435    }    }
436    
# Line 339  extern void swap(environment *env) Line 439  extern void swap(environment *env)
439    env->head->next= temp;    env->head->next= temp;
440  }  }
441    
442  stackitem* copy(stackitem* in_item)  /* Rotate the first three elements on the stack. */
443    extern void rot(environment *env)
444  {  {
445    stackitem *out_item= malloc(sizeof(stackitem));    stackitem *temp= env->head;
446      
447    memcpy(out_item, in_item, sizeof(stackitem));    if(env->head==NULL || env->head->next==NULL
448    out_item->next= NULL;        || env->head->next->next==NULL) {
449        printerr("Too Few Arguments");
450        env->err=1;
451        return;
452      }
453    
454    return out_item;    env->head= env->head->next->next;
455      temp->next->next= env->head->next;
456      env->head->next= temp;
457  }  }
458    
459  /* Recall a value from a symbol, if bound */  /* Recall a value from a symbol, if bound */
# Line 355  extern void rcl(environment *env) Line 462  extern void rcl(environment *env)
462    value *val;    value *val;
463    
464    if(env->head == NULL) {    if(env->head == NULL) {
465      printerr("Stack empty");      printerr("Too Few Arguments");
466        env->err=1;
467      return;      return;
468    }    }
469    
470    if(env->head->item->type!=symb) {    if(env->head->item->type!=symb) {
471      printerr("Not a symbol");      printerr("Bad Argument Type");
472        env->err=2;
473      return;      return;
474    }    }
475    
476    val=((symbol *)(env->head->item->content.ptr))->val;    val=((symbol *)(env->head->item->content.ptr))->val;
477    if(val == NULL){    if(val == NULL){
478      printerr("Unbound variable");      printerr("Unbound Variable");
479        env->err=3;
480      return;      return;
481    }    }
482    toss(env);            /* toss the symbol */    toss(env);            /* toss the symbol */
483      if(env->err) return;
484    push_val(&(env->head), val); /* Return its bound value */    push_val(&(env->head), val); /* Return its bound value */
485  }  }
486    
487    void stack_read(environment*, char*);
488    
489  /* 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
490     function value, and if it is, toss the symbol and execute the     function value, and if it is, toss the symbol and execute the
491     function. */     function. */
492  extern void eval(environment *env)  extern void eval(environment *env)
493  {  {
494    funcp in_func;    funcp in_func;
495    value* val;    value* temp_val;
496      stackitem* iterator;
497      char* temp_string;
498    
499    if(env->head==NULL) {    if(env->head==NULL) {
500      printerr("Stack empty");      printerr("Too Few Arguments");
501        env->err=1;
502      return;      return;
503    }    }
504    
505    /* if it's a symbol */    switch(env->head->item->type) {
506    if(env->head->item->type==symb) {      /* if it's a symbol */
507      case symb:
508      /* If it's not bound to anything */      rcl(env);                   /* get its contents */
509      if (((symbol *)(env->head->item->content.ptr))->val == NULL) {      if(env->err) return;
510        printerr("Unbound variable");      if(env->head->item->type!=symb){ /* don't recurse symbols */
511        return;        eval(env);                        /* evaluate the value */
     }  
       
     /* If it contains a function */  
     if (((symbol *)(env->head->item->content.ptr))->val->type == func) {  
       in_func=  
         (funcp)(((symbol *)(env->head->item->content.ptr))->val->content.ptr);  
       toss(env);  
       (*in_func)(env);          /* Run the function */  
512        return;        return;
     } else {                    /* If it's not a function */  
       val=((symbol *)(env->head->item->content.ptr))->val;  
       toss(env);                /* toss the symbol */  
       push_val(&(env->head), val); /* Return its bound value */  
513      }      }
514    }      break;
515    
516    /* If it's a lone function value, run it */      /* If it's a lone function value, run it */
517    if(env->head->item->type==func) {    case func:
518      in_func= (funcp)(env->head->item->content.ptr);      in_func= (funcp)(env->head->item->content.ptr);
519      toss(env);      toss(env);
520        if(env->err) return;
521      (*in_func)(env);      (*in_func)(env);
522        break;
523    
524        /* If it's a list */
525      case list:
526        temp_val= env->head->item;
527        env->head->item->refcount++;
528        toss(env);
529        if(env->err) return;
530        iterator= (stackitem*)temp_val->content.ptr;
531        while(iterator!=NULL && iterator->item!=NULL) {
532          push_val(&(env->head), iterator->item);
533          if(env->head->item->type==symb
534            && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
535            toss(env);
536            if(env->err) return;
537            eval(env);
538            if(env->err) return;
539          }
540          iterator= iterator->next;
541        }
542        free_val(temp_val);
543        break;
544    
545        /* If it's a string */
546      case string:
547        temp_val= env->head->item;
548        env->head->item->refcount++;
549        toss(env);
550        if(env->err) return;
551        temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);
552        strcpy(temp_string, "[ ");
553        strcat(temp_string, (char*)temp_val->content.ptr);
554        strcat(temp_string, " ]");
555        stack_read(env, temp_string);
556        eval(env);
557        if(env->err) return;
558        free_val(temp_val);
559        free(temp_string);
560        break;
561    
562      case integer:
563        break;
564      }
565    }
566    
567    /* Reverse (flip) a list */
568    extern void rev(environment *env){
569      stackitem *old_head, *new_head, *item;
570    
571      if((env->head)==NULL) {
572        printerr("Too Few Arguments");
573        env->err=1;
574      return;      return;
575    }    }
576    
577      if(env->head->item->type!=list) {
578        printerr("Bad Argument Type");
579        env->err=2;
580        return;
581      }
582    
583      old_head=(stackitem *)(env->head->item->content.ptr);
584      new_head=NULL;
585      while(old_head != NULL){
586        item=old_head;
587        old_head=old_head->next;
588        item->next=new_head;
589        new_head=item;
590      }
591      env->head->item->content.ptr=new_head;
592  }  }
593    
594  /* Make a list. */  /* Make a list. */
# Line 457  extern void pack(environment *env) Line 631  extern void pack(environment *env)
631    temp->item= pack;    temp->item= pack;
632    
633    push(&(env->head), temp);    push(&(env->head), temp);
634      rev(env);
635  }  }
636    
637  /* Parse input. */  /* Parse input. */
638  int stack_read(environment *env, char *in_line)  void stack_read(environment *env, char *in_line)
639  {  {
640    char *temp, *rest;    char *temp, *rest;
641    int itemp;    int itemp;
642    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
643    int convert= 0;    int convert= 0;
   static int non_eval_flag= 0;  
644    
645    temp= malloc(inlength);    temp= malloc(inlength);
646    rest= malloc(inlength);    rest= malloc(inlength);
647    
648    do {    do {
649        /* If comment */
650        if((convert= sscanf(in_line, "#%[^\n\r]", rest))) {
651          free(temp); free(rest);
652          return;
653        }
654    
655      /* If string */      /* If string */
656      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
657        push_cstring(&(env->head), temp);        push_cstring(&(env->head), temp);
# Line 496  int stack_read(environment *env, char *i Line 676  int stack_read(environment *env, char *i
676      /* If single char */      /* If single char */
677      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
678        if(*temp==';') {        if(*temp==';') {
679          if(!non_eval_flag) {          if(!env->non_eval_flag) {
680            eval(env);            /* Evaluate top element */            eval(env);            /* Evaluate top element */
681            break;            break;
682          }          }
# Line 508  int stack_read(environment *env, char *i Line 688  int stack_read(environment *env, char *i
688        if(*temp==']') {        if(*temp==']') {
689          push_sym(env, "[");          push_sym(env, "[");
690          pack(env);          pack(env);
691          if(non_eval_flag!=0)          if(env->non_eval_flag)
692            non_eval_flag--;            env->non_eval_flag--;
693          break;          break;
694        }        }
695    
696        if(*temp=='[') {        if(*temp=='[') {
697          push_sym(env, "[");          push_sym(env, "[");
698          non_eval_flag++;          env->non_eval_flag++;
699          break;          break;
700        }        }
701      }      }
702    } while(0);    } while(0);
703    
   
704    free(temp);    free(temp);
705    
706    if(convert<2) {    if(convert<2) {
707      free(rest);      free(rest);
708      return 0;      return;
709    }    }
710        
711    stack_read(env, rest);    stack_read(env, rest);
712        
713    free(rest);    free(rest);
   return 1;  
714  }  }
715    
716  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
# Line 541  extern void expand(environment *env) Line 719  extern void expand(environment *env)
719    stackitem *temp, *new_head;    stackitem *temp, *new_head;
720    
721    /* Is top element a list? */    /* Is top element a list? */
722    if(env->head==NULL || env->head->item->type!=list) {    if(env->head==NULL) {
723      printerr("Stack empty or not a list");      printerr("Too Few Arguments");
724        env->err=1;
725      return;      return;
726    }    }
727      if(env->head->item->type!=list) {
728        printerr("Bad Argument Type");
729        env->err=2;
730        return;
731      }
732    
733      rev(env);
734    
735      if(env->err)
736        return;
737    
738    /* The first list element is the new stack head */    /* The first list element is the new stack head */
739    new_head= temp= env->head->item->content.ptr;    new_head= temp= env->head->item->content.ptr;
# Line 569  extern void eq(environment *env) Line 758  extern void eq(environment *env)
758    int result;    int result;
759    
760    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
761      printerr("Not enough elements to compare");      printerr("Too Few Arguments");
762        env->err=1;
763      return;      return;
764    }    }
765    
# Line 587  extern void not(environment *env) Line 777  extern void not(environment *env)
777  {  {
778    int val;    int val;
779    
780    if((env->head)==NULL || env->head->item->type!=integer) {    if((env->head)==NULL) {
781      printerr("Stack empty or element is not a integer");      printerr("Too Few Arguments");
782        env->err=1;
783        return;
784      }
785    
786      if(env->head->item->type!=integer) {
787        printerr("Bad Argument Type");
788        env->err=2;
789      return;      return;
790    }    }
791    
# Line 611  extern void def(environment *env) Line 808  extern void def(environment *env)
808    symbol *sym;    symbol *sym;
809    
810    /* Needs two values on the stack, the top one must be a symbol */    /* Needs two values on the stack, the top one must be a symbol */
811    if(env->head==NULL || env->head->next==NULL    if(env->head==NULL || env->head->next==NULL) {
812       || env->head->item->type!=symb) {      printerr("Too Few Arguments");
813      printerr("Define what?");      env->err=1;
814        return;
815      }
816    
817      if(env->head->item->type!=symb) {
818        printerr("Bad Argument Type");
819        env->err=2;
820      return;      return;
821    }    }
822    
# Line 666  extern void forget(environment *env) Line 869  extern void forget(environment *env)
869    stackitem *stack_head= env->head;    stackitem *stack_head= env->head;
870    symbol **hash_entry, *temp;    symbol **hash_entry, *temp;
871    
872    if(stack_head==NULL || stack_head->item->type!=symb) {    if(stack_head==NULL) {
873      printerr("Stack empty or not a symbol");      printerr("Too Few Arguments");
874        env->err=1;
875        return;
876      }
877      
878      if(stack_head->item->type!=symb) {
879        printerr("Bad Argument Type");
880        env->err=2;
881      return;      return;
882    }    }
883    
# Line 683  extern void forget(environment *env) Line 893  extern void forget(environment *env)
893    }    }
894    free(temp->id);    free(temp->id);
895    free(temp);    free(temp);
896  }  }
897    
898    /* Returns the current error number to the stack */
899    extern void errn(environment *env){
900      push_int(&(env->head), env->err);
901    }
902    
903  int main()  int main()
904  {  {
# Line 696  int main() Line 911  int main()
911    
912    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
913      stack_read(&myenv, in_string);      stack_read(&myenv, in_string);
914        if(myenv.err) {
915          printf("(error %d) ", myenv.err);
916          myenv.err=0;
917        }
918      printf("okidok\n ");      printf("okidok\n ");
919    }    }
920      quit(&myenv);
921      return EXIT_FAILURE;
922    }
923    
924    exit(EXIT_SUCCESS);  /* + */
925    extern void sx_2b(environment *env) {
926      int a, b;
927      size_t len;
928      char* new_string;
929      value *a_val, *b_val;
930    
931      if((env->head)==NULL || env->head->next==NULL) {
932        printerr("Too Few Arguments");
933        env->err=1;
934        return;
935      }
936    
937      if(env->head->item->type==string
938         && env->head->next->item->type==string) {
939        a_val= env->head->item;
940        b_val= env->head->next->item;
941        a_val->refcount++;
942        b_val->refcount++;
943        toss(env); if(env->err) return;
944        toss(env); if(env->err) return;
945        len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
946        new_string= malloc(len);
947        strcpy(new_string, b_val->content.ptr);
948        strcat(new_string, a_val->content.ptr);
949        free_val(a_val); free_val(b_val);
950        push_cstring(&(env->head), new_string);
951        free(new_string);
952        return;
953      }
954      
955      if(env->head->item->type!=integer
956         || env->head->next->item->type!=integer) {
957        printerr("Bad Argument Type");
958        env->err=2;
959        return;
960      }
961      a=env->head->item->content.val;
962      toss(env);
963      if(env->err) return;
964      b=env->head->item->content.val;
965      toss(env);
966      if(env->err) return;
967      push_int(&(env->head), a+b);
968    }
969    
970    /* Return copy of a value */
971    value *copy_val(value *old_value){
972      stackitem *old_item, *new_item, *prev_item;
973    
974      value *new_value=malloc(sizeof(value));
975    
976      new_value->type=old_value->type;
977      new_value->refcount=0;        /* This is increased if/when this
978                                       value is referenced somewhere, like
979                                       in a stack item or a variable */
980      switch(old_value->type){
981      case integer:
982        new_value->content.val=old_value->content.val;
983        break;
984      case string:
985        (char *)(new_value->content.ptr)
986          = strdup((char *)(old_value->content.ptr));
987        break;
988      case func:
989      case symb:
990        new_value->content.ptr=old_value->content.ptr;
991        break;
992      case list:
993        new_value->content.ptr=NULL;
994    
995        prev_item=NULL;
996        old_item=(stackitem *)(old_value->content.ptr);
997    
998        while(old_item != NULL) {   /* While list is not empty */
999          new_item= malloc(sizeof(stackitem));
1000          new_item->item=copy_val(old_item->item); /* recurse */
1001          new_item->next=NULL;
1002          if(prev_item != NULL)     /* If this wasn't the first item */
1003            prev_item->next=new_item; /* point the previous item to the
1004                                         new item */
1005          else
1006            new_value->content.ptr=new_item;
1007          old_item=old_item->next;
1008          prev_item=new_item;
1009        }    
1010        break;
1011      }
1012      return new_value;
1013    }
1014    
1015    /* duplicates an item on the stack */
1016    extern void dup(environment *env) {
1017      if((env->head)==NULL) {
1018        printerr("Too Few Arguments");
1019        env->err=1;
1020        return;
1021      }
1022      push_val(&(env->head), copy_val(env->head->item));
1023    }
1024    
1025    /* If-Then */
1026    extern void sx_6966(environment *env) {
1027    
1028      int truth;
1029    
1030      if((env->head)==NULL || env->head->next==NULL) {
1031        printerr("Too Few Arguments");
1032        env->err=1;
1033        return;
1034      }
1035    
1036      if(env->head->next->item->type != integer) {
1037        printerr("Bad Argument Type");
1038        env->err=2;
1039        return;
1040      }
1041      
1042      swap(env);
1043      if(env->err) return;
1044      
1045      truth=env->head->item->content.val;
1046    
1047      toss(env);
1048      if(env->err) return;
1049    
1050      if(truth)
1051        eval(env);
1052      else
1053        toss(env);
1054    }
1055    
1056    /* If-Then-Else */
1057    extern void ifelse(environment *env) {
1058    
1059      int truth;
1060    
1061      if((env->head)==NULL || env->head->next==NULL
1062         || env->head->next->next==NULL) {
1063        printerr("Too Few Arguments");
1064        env->err=1;
1065        return;
1066      }
1067    
1068      if(env->head->next->next->item->type != integer) {
1069        printerr("Bad Argument Type");
1070        env->err=2;
1071        return;
1072      }
1073      
1074      rot(env);
1075      if(env->err) return;
1076      
1077      truth=env->head->item->content.val;
1078    
1079      toss(env);
1080      if(env->err) return;
1081    
1082      if(!truth)
1083        swap(env);
1084      if(env->err) return;
1085    
1086      toss(env);
1087      if(env->err) return;
1088    
1089      eval(env);
1090    }
1091    
1092    /* while */
1093    extern void sx_7768696c65(environment *env) {
1094    
1095      int truth;
1096    
1097      if((env->head)==NULL || env->head->next==NULL) {
1098        printerr("Too Few Arguments");
1099        env->err=1;
1100        return;
1101      }
1102    
1103      do {
1104        swap(env); if(env->err) return;
1105        dup(env); if(env->err) return;
1106        eval(env); if(env->err) return;
1107        
1108        if(env->head->item->type != integer) {
1109          printerr("Bad Argument Type");
1110          env->err=2;
1111          return;
1112        }
1113        
1114        truth= env->head->item->content.val;
1115        
1116        toss(env); if(env->err) return;
1117        swap(env); if(env->err) return;
1118        
1119        if(truth) {
1120          dup(env);
1121          eval(env);
1122        } else {
1123          toss(env);
1124          toss(env);
1125        }
1126      
1127      } while(truth);
1128  }  }

Legend:
Removed from v.1.34  
changed lines
  Added in v.1.58

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26