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

Diff of /stack/stack.c

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

revision 1.29 by teddy, Tue Feb 5 12:14:42 2002 UTC revision 1.64 by teddy, Fri Feb 8 16:33:14 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 58  typedef struct stackitem_struct Line 57  typedef struct stackitem_struct
57  typedef struct {  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 */
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 67  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 103  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 120  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 131  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 147  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 164  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 178  int push_sym(environment *env, const cha Line 277  int push_sym(environment *env, const cha
277    new_symbol= hash(env->symbols, in_string);    new_symbol= hash(env->symbols, in_string);
278    new_value->content.ptr= *new_symbol;    new_value->content.ptr= *new_symbol;
279    
280    if(new_value->content.ptr==NULL) { /* If symbol was undefined */    if(*new_symbol==NULL) { /* If symbol was undefined */
281    
282      /* Create a new symbol */      /* Create a new symbol */
283      *new_symbol= malloc(sizeof(symbol));      (*new_symbol)= malloc(sizeof(symbol));
284      (*new_symbol)->val= NULL;   /* undefined value */      (*new_symbol)->val= NULL;   /* undefined value */
285      (*new_symbol)->next= NULL;      (*new_symbol)->next= NULL;
286      (*new_symbol)->id= malloc(strlen(in_string)+1);      (*new_symbol)->id= malloc(strlen(in_string)+1);
# Line 196  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 206  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 259  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      }
351    }    
352    
353    /* Prints the top element of the stack. */
354    void print_h(stackitem *stack_head)
355    {
356    switch(stack_head->item->type) {    switch(stack_head->item->type) {
357    case integer:    case integer:
358      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
359      break;      break;
360    case string:    case string:
361      printf("\"%s\"", (char*)stack_head->item->content.ptr);      printf("%s", (char*)stack_head->item->content.ptr);
362      break;      break;
363    case symb:    case symb:
364      printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id);      printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);
365      break;      break;
366    default:    case func:
367      printf("%p", (funcp)(stack_head->item->content.ptr));      printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
368        break;
369      case list:
370        /* A list is just a stack, so make stack_head point to it */
371        stack_head=(stackitem *)(stack_head->item->content.ptr);
372        printf("[ ");
373        while(stack_head != NULL) {
374          print_h(stack_head);
375          printf(" ");
376          stack_head=stack_head->next;
377        }
378        printf("]");
379      break;      break;
380    }    }
381  }  }
382    
383  extern void print_(environment *env) {  extern void print_(environment *env) {
384      if(env->head==NULL) {
385        printerr("Too Few Arguments");
386        env->err=1;
387        return;
388      }
389    print_h(env->head);    print_h(env->head);
390  }  }
391    
# Line 292  extern void print_(environment *env) { Line 393  extern void print_(environment *env) {
393  extern void print(environment *env)  extern void print(environment *env)
394  {  {
395    print_(env);    print_(env);
396      if(env->err) return;
397    toss(env);    toss(env);
398  }  }
399    
# Line 305  void print_st(stackitem *stack_head, lon Line 407  void print_st(stackitem *stack_head, lon
407    nl();    nl();
408  }  }
409    
   
   
410  /* Prints the stack. */  /* Prints the stack. */
411  extern void printstack(environment *env)  extern void printstack(environment *env)
412  {  {
413    if(env->head != NULL) {    if(env->head == NULL) {
414      print_st(env->head, 1);      return;
     nl();  
   } else {  
     printerr("Stack empty");  
415    }    }
416      print_st(env->head, 1);
417      nl();
418  }  }
419    
420  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 323  extern void swap(environment *env) Line 422  extern void swap(environment *env)
422  {  {
423    stackitem *temp= env->head;    stackitem *temp= env->head;
424        
425    if((env->head)==NULL) {    if(env->head==NULL || env->head->next==NULL) {
426      printerr("Stack empty");      printerr("Too Few Arguments");
427      return;      env->err=1;
   }  
   
   if(env->head->next==NULL) {  
     printerr("Not enough arguments");  
428      return;      return;
429    }    }
430    
# Line 338  extern void swap(environment *env) Line 433  extern void swap(environment *env)
433    env->head->next= temp;    env->head->next= temp;
434  }  }
435    
436  stackitem* copy(stackitem* in_item)  /* Rotate the first three elements on the stack. */
437    extern void rot(environment *env)
438    {
439      stackitem *temp= env->head;
440      
441      if(env->head==NULL || env->head->next==NULL
442          || env->head->next->next==NULL) {
443        printerr("Too Few Arguments");
444        env->err=1;
445        return;
446      }
447    
448      env->head= env->head->next->next;
449      temp->next->next= env->head->next;
450      env->head->next= temp;
451    }
452    
453    /* Recall a value from a symbol, if bound */
454    extern void rcl(environment *env)
455  {  {
456    stackitem *out_item= malloc(sizeof(stackitem));    value *val;
457    
458    memcpy(out_item, in_item, sizeof(stackitem));    if(env->head == NULL) {
459    out_item->next= NULL;      printerr("Too Few Arguments");
460        env->err=1;
461        return;
462      }
463    
464      if(env->head->item->type!=symb) {
465        printerr("Bad Argument Type");
466        env->err=2;
467        return;
468      }
469    
470    return out_item;    val=((symbol *)(env->head->item->content.ptr))->val;
471      if(val == NULL){
472        printerr("Unbound Variable");
473        env->err=3;
474        return;
475      }
476      toss(env);            /* toss the symbol */
477      if(env->err) return;
478      push_val(&(env->head), val); /* Return its bound value */
479  }  }
480    
481    void stack_read(environment*, char*);
482    
483  /* 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
484     function value, and if it is, toss the symbol and execute the     function value, and if it is, toss the symbol and execute the
# Line 355  stackitem* copy(stackitem* in_item) Line 486  stackitem* copy(stackitem* in_item)
486  extern void eval(environment *env)  extern void eval(environment *env)
487  {  {
488    funcp in_func;    funcp in_func;
489    value* val;    value* temp_val;
490      stackitem* iterator;
491      char* temp_string;
492    
493    if(env->head==NULL) {    if(env->head==NULL) {
494      printerr("Stack empty");      printerr("Too Few Arguments");
495        env->err=1;
496      return;      return;
497    }    }
498    
499    /* if it's a symbol */   eval_start:
   if(env->head->item->type==symb) {  
500    
501      /* If it's not bound to anything */    switch(env->head->item->type) {
502      if (((symbol *)(env->head->item->content.ptr))->val == NULL) {      /* if it's a symbol */
503        printerr("Unbound variable");    case symb:
504        return;      rcl(env);                   /* get its contents */
505      }      if(env->err) return;
506            if(env->head->item->type!=symb){ /* don't recurse symbols */
507      /* If it contains a function */        goto eval_start;
     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 */  
       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 */  
508      }      }
509    }      return;
510    
511    /* If it's a lone function value, run it */      /* If it's a lone function value, run it */
512    if(env->head->item->type==func) {    case func:
513      in_func= (funcp)(env->head->item->content.ptr);      in_func= (funcp)(env->head->item->content.ptr);
514      toss(env);      toss(env);
515      (*in_func)(env);      if(env->err) return;
516        return (*in_func)(env);
517    
518        /* If it's a list */
519      case list:
520        temp_val= env->head->item;
521        env->head->item->refcount++;
522        toss(env);
523        if(env->err) return;
524        iterator= (stackitem*)temp_val->content.ptr;
525        while(iterator!=NULL) {
526          push_val(&(env->head), iterator->item);
527          if(env->head->item->type==symb
528            && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
529            toss(env);
530            if(env->err) return;
531            if(iterator->next == NULL){
532              free_val(temp_val);
533              goto eval_start;
534            }
535            eval(env);
536            if(env->err) return;
537          }
538          iterator= iterator->next;
539        }
540        free_val(temp_val);
541        return;
542    
543        /* If it's a string */
544      case string:
545        temp_val= env->head->item;
546        env->head->item->refcount++;
547        toss(env);
548        if(env->err) return;
549        temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);
550        strcpy(temp_string, "[ ");
551        strcpy(temp_string+2, (char*)temp_val->content.ptr);
552        free_val(temp_val);
553        strcat(temp_string, " ]");
554        stack_read(env, temp_string);
555        free(temp_string);
556        goto eval_start;
557    
558      case integer:
559        return;
560      }
561    }
562    
563    /* Reverse (flip) a list */
564    extern void rev(environment *env){
565      stackitem *old_head, *new_head, *item;
566    
567      if((env->head)==NULL) {
568        printerr("Too Few Arguments");
569        env->err=1;
570        return;
571      }
572    
573      if(env->head->item->type!=list) {
574        printerr("Bad Argument Type");
575        env->err=2;
576      return;      return;
577    }    }
578    
579  /*    push(&(env->head), copy(env->head)); */    old_head=(stackitem *)(env->head->item->content.ptr);
580  /*    swap(env); */    new_head=NULL;
581  /*    toss(env); */    while(old_head != NULL){
582        item=old_head;
583        old_head=old_head->next;
584        item->next=new_head;
585        new_head=item;
586      }
587      env->head->item->content.ptr=new_head;
588  }  }
589    
590  /* Make a list. */  /* Make a list. */
# Line 437  extern void pack(environment *env) Line 627  extern void pack(environment *env)
627    temp->item= pack;    temp->item= pack;
628    
629    push(&(env->head), temp);    push(&(env->head), temp);
630      rev(env);
631  }  }
632    
633  /* Parse input. */  /* Parse input. */
634  int stack_read(environment *env, char *in_line)  void stack_read(environment *env, char *in_line)
635  {  {
636    char *temp, *rest;    char *temp, *rest;
637    int itemp;    int itemp;
638    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
639    int convert= 0;    int convert= 0;
   static int non_eval_flag= 0;  
640    
641    temp= malloc(inlength);    temp= malloc(inlength);
642    rest= malloc(inlength);    rest= malloc(inlength);
643    
644    do {    do {
645        /* If comment */
646        if((convert= sscanf(in_line, "#%[^\n\r]", rest))) {
647          free(temp); free(rest);
648          return;
649        }
650    
651      /* If string */      /* If string */
652      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
653        push_cstring(&(env->head), temp);        push_cstring(&(env->head), temp);
# Line 469  int stack_read(environment *env, char *i Line 665  int stack_read(environment *env, char *i
665        break;        break;
666      }      }
667      /* If symbol */      /* If symbol */
668      if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
669          push_sym(env, temp);          push_sym(env, temp);
670          break;          break;
671      }      }
672      /* If single char */      /* If single char */
673      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
674        if(*temp==';') {        if(*temp==';') {
675          if(!non_eval_flag) {          if(!env->non_eval_flag) {
676            eval(env);            /* Evaluate top element */            eval(env);            /* Evaluate top element */
677            break;            break;
678          }          }
# Line 488  int stack_read(environment *env, char *i Line 684  int stack_read(environment *env, char *i
684        if(*temp==']') {        if(*temp==']') {
685          push_sym(env, "[");          push_sym(env, "[");
686          pack(env);          pack(env);
687          if(non_eval_flag!=0)          if(env->non_eval_flag)
688            non_eval_flag--;            env->non_eval_flag--;
689          break;          break;
690        }        }
691    
692        if(*temp=='[') {        if(*temp=='[') {
693          push_sym(env, "[");          push_sym(env, "[");
694          non_eval_flag++;          env->non_eval_flag++;
695          break;          break;
696        }        }
697      }      }
698    } while(0);    } while(0);
699    
   
700    free(temp);    free(temp);
701    
702    if(convert<2) {    if(convert<2) {
703      free(rest);      free(rest);
704      return 0;      return;
705    }    }
706        
707    stack_read(env, rest);    stack_read(env, rest);
708        
709    free(rest);    free(rest);
   return 1;  
710  }  }
711    
712  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
# Line 521  extern void expand(environment *env) Line 715  extern void expand(environment *env)
715    stackitem *temp, *new_head;    stackitem *temp, *new_head;
716    
717    /* Is top element a list? */    /* Is top element a list? */
718    if(env->head==NULL || env->head->item->type!=list) {    if(env->head==NULL) {
719      printerr("Stack empty or not a list");      printerr("Too Few Arguments");
720        env->err=1;
721        return;
722      }
723      if(env->head->item->type!=list) {
724        printerr("Bad Argument Type");
725        env->err=2;
726      return;      return;
727    }    }
728    
729      rev(env);
730    
731      if(env->err)
732        return;
733    
734    /* The first list element is the new stack head */    /* The first list element is the new stack head */
735    new_head= temp= env->head->item->content.ptr;    new_head= temp= env->head->item->content.ptr;
736    
# Line 549  extern void eq(environment *env) Line 754  extern void eq(environment *env)
754    int result;    int result;
755    
756    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
757      printerr("Not enough elements to compare");      printerr("Too Few Arguments");
758        env->err=1;
759      return;      return;
760    }    }
761    
# Line 567  extern void not(environment *env) Line 773  extern void not(environment *env)
773  {  {
774    int val;    int val;
775    
776    if((env->head)==NULL || env->head->item->type!=integer) {    if((env->head)==NULL) {
777      printerr("Stack empty or element is not a integer");      printerr("Too Few Arguments");
778        env->err=1;
779        return;
780      }
781    
782      if(env->head->item->type!=integer) {
783        printerr("Bad Argument Type");
784        env->err=2;
785      return;      return;
786    }    }
787    
# Line 591  extern void def(environment *env) Line 804  extern void def(environment *env)
804    symbol *sym;    symbol *sym;
805    
806    /* 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 */
807    if(env->head==NULL || env->head->next==NULL    if(env->head==NULL || env->head->next==NULL) {
808       || env->head->item->type!=symb) {      printerr("Too Few Arguments");
809      printerr("Define what?");      env->err=1;
810        return;
811      }
812    
813      if(env->head->item->type!=symb) {
814        printerr("Bad Argument Type");
815        env->err=2;
816      return;      return;
817    }    }
818    
# Line 624  extern void clear(environment *env) Line 843  extern void clear(environment *env)
843      toss(env);      toss(env);
844  }  }
845    
846    /* List all defined words */
847    extern void words(environment *env)
848    {
849      symbol *temp;
850      int i;
851      
852      for(i= 0; i<HASHTBLSIZE; i++) {
853        temp= env->symbols[i];
854        while(temp!=NULL) {
855          printf("%s\n", temp->id);
856          temp= temp->next;
857        }
858      }
859    }
860    
861    /* Forgets a symbol (remove it from the hash table) */
862    extern void forget(environment *env)
863    {
864      char* sym_id;
865      stackitem *stack_head= env->head;
866      symbol **hash_entry, *temp;
867    
868      if(stack_head==NULL) {
869        printerr("Too Few Arguments");
870        env->err=1;
871        return;
872      }
873      
874      if(stack_head->item->type!=symb) {
875        printerr("Bad Argument Type");
876        env->err=2;
877        return;
878      }
879    
880      sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
881      toss(env);
882    
883      hash_entry= hash(env->symbols, sym_id);
884      temp= *hash_entry;
885      *hash_entry= (*hash_entry)->next;
886      
887      if(temp->val!=NULL) {
888        free_val(temp->val);
889      }
890      free(temp->id);
891      free(temp);
892    }
893    
894    /* Returns the current error number to the stack */
895    extern void errn(environment *env){
896      push_int(&(env->head), env->err);
897    }
898    
899  int main()  int main()
900  {  {
901    environment myenv;    environment myenv;
# Line 635  int main() Line 907  int main()
907    
908    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
909      stack_read(&myenv, in_string);      stack_read(&myenv, in_string);
910        if(myenv.err) {
911          printf("(error %d) ", myenv.err);
912          myenv.err=0;
913        }
914      printf("okidok\n ");      printf("okidok\n ");
915    }    }
916      quit(&myenv);
917      return EXIT_FAILURE;
918    }
919    
920    exit(EXIT_SUCCESS);  /* + */
921    extern void sx_2b(environment *env) {
922      int a, b;
923      size_t len;
924      char* new_string;
925      value *a_val, *b_val;
926    
927      if((env->head)==NULL || env->head->next==NULL) {
928        printerr("Too Few Arguments");
929        env->err=1;
930        return;
931      }
932    
933      if(env->head->item->type==string
934         && env->head->next->item->type==string) {
935        a_val= env->head->item;
936        b_val= env->head->next->item;
937        a_val->refcount++;
938        b_val->refcount++;
939        toss(env); if(env->err) return;
940        toss(env); if(env->err) return;
941        len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
942        new_string= malloc(len);
943        strcpy(new_string, b_val->content.ptr);
944        strcat(new_string, a_val->content.ptr);
945        free_val(a_val); free_val(b_val);
946        push_cstring(&(env->head), new_string);
947        free(new_string);
948        return;
949      }
950      
951      if(env->head->item->type!=integer
952         || env->head->next->item->type!=integer) {
953        printerr("Bad Argument Type");
954        env->err=2;
955        return;
956      }
957      a=env->head->item->content.val;
958      toss(env);
959      if(env->err) return;
960      if(env->head->item->refcount == 1)
961        env->head->item->content.val += a;
962      else {
963        b=env->head->item->content.val;
964        toss(env);
965        if(env->err) return;
966        push_int(&(env->head), a+b);
967      }
968    }
969    
970    /* - */
971    extern void sx_2d(environment *env) {
972      int a, b;
973    
974      if((env->head)==NULL || env->head->next==NULL) {
975        printerr("Too Few Arguments");
976        env->err=1;
977        return;
978      }
979      
980      if(env->head->item->type!=integer
981         || env->head->next->item->type!=integer) {
982        printerr("Bad Argument Type");
983        env->err=2;
984        return;
985      }
986      a=env->head->item->content.val;
987      toss(env);
988      if(env->err) return;
989      if(env->head->item->refcount == 1)
990        env->head->item->content.val -= a;
991      else {
992        b=env->head->item->content.val;
993        toss(env);
994        if(env->err) return;
995        push_int(&(env->head), b-a);
996      }
997    }
998    
999    /* > */
1000    extern void sx_3e(environment *env) {
1001      int a, b;
1002    
1003      if((env->head)==NULL || env->head->next==NULL) {
1004        printerr("Too Few Arguments");
1005        env->err=1;
1006        return;
1007      }
1008      
1009      if(env->head->item->type!=integer
1010         || env->head->next->item->type!=integer) {
1011        printerr("Bad Argument Type");
1012        env->err=2;
1013        return;
1014      }
1015      a=env->head->item->content.val;
1016      toss(env);
1017      if(env->err) return;
1018      if(env->head->item->refcount == 1)
1019        env->head->item->content.val = (env->head->item->content.val > a);
1020      else {
1021        b=env->head->item->content.val;
1022        toss(env);
1023        if(env->err) return;
1024        push_int(&(env->head), b>a);
1025      }
1026    }
1027    
1028    /* Return copy of a value */
1029    value *copy_val(value *old_value){
1030      stackitem *old_item, *new_item, *prev_item;
1031    
1032      value *new_value=malloc(sizeof(value));
1033    
1034      new_value->type=old_value->type;
1035      new_value->refcount=0;        /* This is increased if/when this
1036                                       value is referenced somewhere, like
1037                                       in a stack item or a variable */
1038      switch(old_value->type){
1039      case integer:
1040        new_value->content.val=old_value->content.val;
1041        break;
1042      case string:
1043        (char *)(new_value->content.ptr)
1044          = strdup((char *)(old_value->content.ptr));
1045        break;
1046      case func:
1047      case symb:
1048        new_value->content.ptr=old_value->content.ptr;
1049        break;
1050      case list:
1051        new_value->content.ptr=NULL;
1052    
1053        prev_item=NULL;
1054        old_item=(stackitem *)(old_value->content.ptr);
1055    
1056        while(old_item != NULL) {   /* While list is not empty */
1057          new_item= malloc(sizeof(stackitem));
1058          new_item->item=copy_val(old_item->item); /* recurse */
1059          new_item->next=NULL;
1060          if(prev_item != NULL)     /* If this wasn't the first item */
1061            prev_item->next=new_item; /* point the previous item to the
1062                                         new item */
1063          else
1064            new_value->content.ptr=new_item;
1065          old_item=old_item->next;
1066          prev_item=new_item;
1067        }    
1068        break;
1069      }
1070      return new_value;
1071    }
1072    
1073    /* duplicates an item on the stack */
1074    extern void dup(environment *env) {
1075      if((env->head)==NULL) {
1076        printerr("Too Few Arguments");
1077        env->err=1;
1078        return;
1079      }
1080      push_val(&(env->head), copy_val(env->head->item));
1081    }
1082    
1083    /* "if", If-Then */
1084    extern void sx_6966(environment *env) {
1085    
1086      int truth;
1087    
1088      if((env->head)==NULL || env->head->next==NULL) {
1089        printerr("Too Few Arguments");
1090        env->err=1;
1091        return;
1092      }
1093    
1094      if(env->head->next->item->type != integer) {
1095        printerr("Bad Argument Type");
1096        env->err=2;
1097        return;
1098      }
1099      
1100      swap(env);
1101      if(env->err) return;
1102      
1103      truth=env->head->item->content.val;
1104    
1105      toss(env);
1106      if(env->err) return;
1107    
1108      if(truth)
1109        eval(env);
1110      else
1111        toss(env);
1112    }
1113    
1114    /* If-Then-Else */
1115    extern void ifelse(environment *env) {
1116    
1117      int truth;
1118    
1119      if((env->head)==NULL || env->head->next==NULL
1120         || env->head->next->next==NULL) {
1121        printerr("Too Few Arguments");
1122        env->err=1;
1123        return;
1124      }
1125    
1126      if(env->head->next->next->item->type != integer) {
1127        printerr("Bad Argument Type");
1128        env->err=2;
1129        return;
1130      }
1131      
1132      rot(env);
1133      if(env->err) return;
1134      
1135      truth=env->head->item->content.val;
1136    
1137      toss(env);
1138      if(env->err) return;
1139    
1140      if(!truth)
1141        swap(env);
1142      if(env->err) return;
1143    
1144      toss(env);
1145      if(env->err) return;
1146    
1147      eval(env);
1148    }
1149    
1150    /* while */
1151    extern void sx_7768696c65(environment *env) {
1152    
1153      int truth;
1154      value *loop, *test;
1155    
1156      if((env->head)==NULL || env->head->next==NULL) {
1157        printerr("Too Few Arguments");
1158        env->err=1;
1159        return;
1160      }
1161    
1162      loop= env->head->item;
1163      loop->refcount++;
1164      toss(env); if(env->err) return;
1165    
1166      test= env->head->item;
1167      test->refcount++;
1168      toss(env); if(env->err) return;
1169    
1170      do {
1171        push_val(&(env->head), test);
1172        eval(env);
1173        
1174        if(env->head->item->type != integer) {
1175          printerr("Bad Argument Type");
1176          env->err=2;
1177          return;
1178        }
1179        
1180        truth= env->head->item->content.val;
1181        toss(env); if(env->err) return;
1182        
1183        if(truth) {
1184          push_val(&(env->head), loop);
1185          eval(env);
1186        } else {
1187          toss(env);
1188        }
1189      
1190      } while(truth);
1191    
1192      free_val(test);
1193      free_val(loop);
1194  }  }

Legend:
Removed from v.1.29  
changed lines
  Added in v.1.64

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26