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

Diff of /stack/stack.c

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

revision 1.31 by teddy, Tue Feb 5 22:25:04 2002 UTC revision 1.35 by teddy, Wed Feb 6 00:30:14 2002 UTC
# 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 58  typedef struct stackitem_struct Line 56  typedef struct stackitem_struct
56  typedef struct {  typedef struct {
57    stackitem *head;              /* Head of the stack */    stackitem *head;              /* Head of the stack */
58    hashtbl symbols;              /* Hash table of all variable bindings */    hashtbl symbols;              /* Hash table of all variable bindings */
59      int err;                      /* Error flag */
60  } environment;  } environment;
61    
62  /* A type for pointers to external functions */  /* A type for pointers to external functions */
# Line 69  void init_env(environment *env) Line 68  void init_env(environment *env)
68  {  {
69    long i;    long i;
70    
71      env->err=0;
72    for(i= 0; i<HASHTBLSIZE; i++)    for(i= 0; i<HASHTBLSIZE; i++)
73      env->symbols[i]= NULL;      env->symbols[i]= NULL;
74  }  }
# Line 103  symbol **hash(hashtbl in_hashtbl, const Line 103  symbol **hash(hashtbl in_hashtbl, const
103  }  }
104    
105  /* Generic push function. */  /* Generic push function. */
106  int push(stackitem** stack_head, stackitem* in_item)  void push(stackitem** stack_head, stackitem* in_item)
107  {  {
108    in_item->next= *stack_head;    in_item->next= *stack_head;
109    *stack_head= in_item;    *stack_head= in_item;
   return 1;  
110  }  }
111    
112  /* Push a value onto the stack */  /* Push a value onto the stack */
# Line 120  void push_val(stackitem **stack_head, va Line 119  void push_val(stackitem **stack_head, va
119  }  }
120    
121  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
122  int push_int(stackitem **stack_head, int in_val)  void push_int(stackitem **stack_head, int in_val)
123  {  {
124    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
125    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
# Line 131  int push_int(stackitem **stack_head, int Line 130  int push_int(stackitem **stack_head, int
130    new_value->refcount=1;    new_value->refcount=1;
131    
132    push(stack_head, new_item);    push(stack_head, new_item);
   return 1;  
133  }  }
134    
135  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
136  int push_cstring(stackitem **stack_head, const char *in_string)  void push_cstring(stackitem **stack_head, const char *in_string)
137  {  {
138    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
139    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
# Line 147  int push_cstring(stackitem **stack_head, Line 145  int push_cstring(stackitem **stack_head,
145    new_value->refcount=1;    new_value->refcount=1;
146    
147    push(stack_head, new_item);    push(stack_head, new_item);
   return 1;  
148  }  }
149    
150  /* Push a symbol onto the stack. */  /* Push a symbol onto the stack. */
151  int push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
152  {  {
153    stackitem *new_item;          /* The new stack item */    stackitem *new_item;          /* The new stack item */
154    /* ...which will contain... */    /* ...which will contain... */
# Line 206  int push_sym(environment *env, const cha Line 203  int push_sym(environment *env, const cha
203      }      }
204    }    }
205    push(&(env->head), new_item);    push(&(env->head), new_item);
   return 1;  
206  }  }
207    
208  void printerr(const char* in_string) {  void printerr(const char* in_string) {
# Line 245  extern void toss(environment *env) Line 241  extern void toss(environment *env)
241    
242    if((env->head)==NULL) {    if((env->head)==NULL) {
243      printerr("Stack empty");      printerr("Stack empty");
244        env->err=1;
245      return;      return;
246    }    }
247        
# Line 262  extern void nl() Line 259  extern void nl()
259  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
260  void print_h(stackitem *stack_head)  void print_h(stackitem *stack_head)
261  {  {
   
   if(stack_head==NULL) {  
     printerr("Stack empty");  
     return;  
   }  
   
262    switch(stack_head->item->type) {    switch(stack_head->item->type) {
263    case integer:    case integer:
264      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
# Line 278  void print_h(stackitem *stack_head) Line 269  void print_h(stackitem *stack_head)
269    case symb:    case symb:
270      printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id);      printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id);
271      break;      break;
272      case func:
273        printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
274        break;
275      case list:
276        printf("#<list %p>", (funcp)(stack_head->item->content.ptr));
277        break;
278    default:    default:
279      printf("%p", (funcp)(stack_head->item->content.ptr));      printf("#<unknown %p>", (funcp)(stack_head->item->content.ptr));
280      break;      break;
281    }    }
282  }  }
283    
284  extern void print_(environment *env) {  extern void print_(environment *env) {
285      if(env->head==NULL) {
286        printerr("Stack empty");
287        env->err=1;
288        return;
289      }
290    print_h(env->head);    print_h(env->head);
291  }  }
292    
# Line 292  extern void print_(environment *env) { Line 294  extern void print_(environment *env) {
294  extern void print(environment *env)  extern void print(environment *env)
295  {  {
296    print_(env);    print_(env);
297      if(env->err) return;
298    toss(env);    toss(env);
299  }  }
300    
# Line 310  void print_st(stackitem *stack_head, lon Line 313  void print_st(stackitem *stack_head, lon
313  /* Prints the stack. */  /* Prints the stack. */
314  extern void printstack(environment *env)  extern void printstack(environment *env)
315  {  {
316    if(env->head != NULL) {    if(env->head == NULL) {
     print_st(env->head, 1);  
     nl();  
   } else {  
317      printerr("Stack empty");      printerr("Stack empty");
318        env->err=1;
319        return;
320    }    }
321      print_st(env->head, 1);
322      nl();
323  }  }
324    
325  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 325  extern void swap(environment *env) Line 329  extern void swap(environment *env)
329        
330    if((env->head)==NULL) {    if((env->head)==NULL) {
331      printerr("Stack empty");      printerr("Stack empty");
332        env->err=1;
333      return;      return;
334    }    }
335    
336    if(env->head->next==NULL) {    if(env->head->next==NULL) {
337      printerr("Not enough arguments");      printerr("Not enough arguments");
338        env->err=1;
339      return;      return;
340    }    }
341    
# Line 348  stackitem* copy(stackitem* in_item) Line 354  stackitem* copy(stackitem* in_item)
354    return out_item;    return out_item;
355  }  }
356    
357    /* Recall a value from a symbol, if bound */
358  extern void rcl(environment *env)  extern void rcl(environment *env)
359  {  {
360    value *val;    value *val;
361    
362    if(env->head == NULL) {    if(env->head == NULL) {
363      printerr("Stack empty");      printerr("Stack empty");
364        env->err=1;
365      return;      return;
366    }    }
367    
368    if(env->head->item->type!=symb) {    if(env->head->item->type!=symb) {
369      printerr("Not a symbol");      printerr("Not a symbol");
370        env->err=1;
371      return;      return;
372    }    }
373    
374    val=((symbol *)(env->head->item->content.ptr))->val;    val=((symbol *)(env->head->item->content.ptr))->val;
375      if(val == NULL){
376        printerr("Unbound variable");
377        env->err=1;
378        return;
379      }
380    toss(env);            /* toss the symbol */    toss(env);            /* toss the symbol */
381      if(env->err) return;
382    push_val(&(env->head), val); /* Return its bound value */    push_val(&(env->head), val); /* Return its bound value */
383  }  }
384    
# Line 372  extern void rcl(environment *env) Line 388  extern void rcl(environment *env)
388  extern void eval(environment *env)  extern void eval(environment *env)
389  {  {
390    funcp in_func;    funcp in_func;
   value* val;  
391    if(env->head==NULL) {    if(env->head==NULL) {
392      printerr("Stack empty");      printerr("Stack empty");
393        env->err=1;
394      return;      return;
395    }    }
396    
397    /* if it's a symbol */    /* if it's a symbol */
398    if(env->head->item->type==symb) {    if(env->head->item->type==symb) {
399    
400      /* If it's not bound to anything */      rcl(env);                   /* get its contents */
401      if (((symbol *)(env->head->item->content.ptr))->val == NULL) {      if(env->err) return;
402        printerr("Unbound variable");      if(env->head->item->type!=symb){ /* don't recurse symbols */
403          eval(env);                        /* evaluate the value */
404        return;        return;
405      }      }
       
     /* 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 */  
       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 */  
     }  
406    }    }
407    
408    /* If it's a lone function value, run it */    /* If it's a lone function value, run it */
409    if(env->head->item->type==func) {    if(env->head->item->type==func) {
410      in_func= (funcp)(env->head->item->content.ptr);      in_func= (funcp)(env->head->item->content.ptr);
411      toss(env);      toss(env);
412        if(env->err) return;
413      (*in_func)(env);      (*in_func)(env);
     return;  
414    }    }
   
415  }  }
416    
417  /* Make a list. */  /* Make a list. */
# Line 483  int stack_read(environment *env, char *i Line 486  int stack_read(environment *env, char *i
486        break;        break;
487      }      }
488      /* If symbol */      /* If symbol */
489      if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
490          push_sym(env, temp);          push_sym(env, temp);
491          break;          break;
492      }      }
# Line 537  extern void expand(environment *env) Line 540  extern void expand(environment *env)
540    /* Is top element a list? */    /* Is top element a list? */
541    if(env->head==NULL || env->head->item->type!=list) {    if(env->head==NULL || env->head->item->type!=list) {
542      printerr("Stack empty or not a list");      printerr("Stack empty or not a list");
543        env->err=1;
544      return;      return;
545    }    }
546    
# Line 564  extern void eq(environment *env) Line 568  extern void eq(environment *env)
568    
569    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
570      printerr("Not enough elements to compare");      printerr("Not enough elements to compare");
571        env->err=1;
572      return;      return;
573    }    }
574    
# Line 583  extern void not(environment *env) Line 588  extern void not(environment *env)
588    
589    if((env->head)==NULL || env->head->item->type!=integer) {    if((env->head)==NULL || env->head->item->type!=integer) {
590      printerr("Stack empty or element is not a integer");      printerr("Stack empty or element is not a integer");
591        env->err=1;
592      return;      return;
593    }    }
594    
# Line 608  extern void def(environment *env) Line 614  extern void def(environment *env)
614    if(env->head==NULL || env->head->next==NULL    if(env->head==NULL || env->head->next==NULL
615       || env->head->item->type!=symb) {       || env->head->item->type!=symb) {
616      printerr("Define what?");      printerr("Define what?");
617        env->err=1;
618      return;      return;
619    }    }
620    
# Line 638  extern void clear(environment *env) Line 645  extern void clear(environment *env)
645      toss(env);      toss(env);
646  }  }
647    
648    /* List all defined words */
649    extern void words(environment *env)
650    {
651      symbol *temp;
652      int i;
653      
654      for(i= 0; i<HASHTBLSIZE; i++) {
655        temp= env->symbols[i];
656        while(temp!=NULL) {
657          printf("%s\n", temp->id);
658          temp= temp->next;
659        }
660      }
661    }
662    
663    /* Forgets a symbol (remove it from the hash table) */
664    extern void forget(environment *env)
665    {
666      char* sym_id;
667      stackitem *stack_head= env->head;
668      symbol **hash_entry, *temp;
669    
670      if(stack_head==NULL || stack_head->item->type!=symb) {
671        printerr("Stack empty or not a symbol");
672        return;
673      }
674    
675      sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
676      toss(env);
677    
678      hash_entry= hash(env->symbols, sym_id);
679      temp= *hash_entry;
680      *hash_entry= (*hash_entry)->next;
681      
682      if(temp->val!=NULL) {
683        free_val(temp->val);
684      }
685      free(temp->id);
686      free(temp);
687    }
688    
689  int main()  int main()
690  {  {
691    environment myenv;    environment myenv;
# Line 649  int main() Line 697  int main()
697    
698    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
699      stack_read(&myenv, in_string);      stack_read(&myenv, in_string);
700        if(myenv.err) {
701          printf("(error %d) ", myenv.err);
702          myenv.err=0;
703        }
704      printf("okidok\n ");      printf("okidok\n ");
705    }    }
706    

Legend:
Removed from v.1.31  
changed lines
  Added in v.1.35

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26