/[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.36 by teddy, Wed Feb 6 00:52:31 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 244  extern void toss(environment *env) Line 240  extern void toss(environment *env)
240    stackitem *temp= env->head;    stackitem *temp= env->head;
241    
242    if((env->head)==NULL) {    if((env->head)==NULL) {
243      printerr("Stack empty");      printerr("Too Few Arguments");
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("Too Few Arguments");
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) {
317      print_st(env->head, 1);      printerr("Too Few Arguments");
318      nl();      env->err=1;
319    } else {      return;
     printerr("Stack empty");  
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 324  extern void swap(environment *env) Line 328  extern void swap(environment *env)
328    stackitem *temp= env->head;    stackitem *temp= env->head;
329        
330    if((env->head)==NULL) {    if((env->head)==NULL) {
331      printerr("Stack empty");      printerr("Too Few Arguments");
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("Too Few 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("Too Few Arguments");
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("Bad Argument Type");
370        env->err=2;
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=3;
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("Too Few Arguments");
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        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 */  
404        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 */  
405      }      }
406    }    }
407    
# Line 405  extern void eval(environment *env) Line 409  extern void eval(environment *env)
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 535  extern void expand(environment *env) Line 538  extern void expand(environment *env)
538    stackitem *temp, *new_head;    stackitem *temp, *new_head;
539    
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) {
542      printerr("Stack empty or not a list");      printerr("Too Few Arguments");
543        env->err=1;
544        return;
545      }
546      if(env->head->item->type!=list) {
547        printerr("Bad Argument Type");
548        env->err=2;
549      return;      return;
550    }    }
551    
# Line 563  extern void eq(environment *env) Line 572  extern void eq(environment *env)
572    int result;    int result;
573    
574    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
575      printerr("Not enough elements to compare");      printerr("Too Few Arguments");
576        env->err=1;
577      return;      return;
578    }    }
579    
# Line 581  extern void not(environment *env) Line 591  extern void not(environment *env)
591  {  {
592    int val;    int val;
593    
594    if((env->head)==NULL || env->head->item->type!=integer) {    if((env->head)==NULL) {
595      printerr("Stack empty or element is not a integer");      printerr("Too Few Arguments");
596        env->err=1;
597        return;
598      }
599    
600      if(env->head->item->type!=integer) {
601        printerr("Bad Argument Type");
602        env->err=2;
603      return;      return;
604    }    }
605    
# Line 605  extern void def(environment *env) Line 622  extern void def(environment *env)
622    symbol *sym;    symbol *sym;
623    
624    /* 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 */
625    if(env->head==NULL || env->head->next==NULL    if(env->head==NULL || env->head->next==NULL) {
626       || env->head->item->type!=symb) {      printerr("Too Few Arguments");
627      printerr("Define what?");      env->err=1;
628        return;
629      }
630    
631      if(env->head->item->type!=symb) {
632        printerr("Bad Argument Type");
633        env->err=2;
634      return;      return;
635    }    }
636    
# Line 638  extern void clear(environment *env) Line 661  extern void clear(environment *env)
661      toss(env);      toss(env);
662  }  }
663    
664    /* List all defined words */
665    extern void words(environment *env)
666    {
667      symbol *temp;
668      int i;
669      
670      for(i= 0; i<HASHTBLSIZE; i++) {
671        temp= env->symbols[i];
672        while(temp!=NULL) {
673          printf("%s\n", temp->id);
674          temp= temp->next;
675        }
676      }
677    }
678    
679    /* Forgets a symbol (remove it from the hash table) */
680    extern void forget(environment *env)
681    {
682      char* sym_id;
683      stackitem *stack_head= env->head;
684      symbol **hash_entry, *temp;
685    
686      if(stack_head==NULL) {
687        printerr("Too Few Arguments");
688        env->err=1;
689        return;
690      }
691      
692      if(stack_head->item->type!=symb) {
693        printerr("Bad Argument Type");
694        env->err=2;
695        return;
696      }
697    
698      sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
699      toss(env);
700    
701      hash_entry= hash(env->symbols, sym_id);
702      temp= *hash_entry;
703      *hash_entry= (*hash_entry)->next;
704      
705      if(temp->val!=NULL) {
706        free_val(temp->val);
707      }
708      free(temp->id);
709      free(temp);
710    }
711    
712    /* Returns the current error number to the stack */
713    extern void errn(environment *env){
714      push_int(&(env->head), env->err);
715    }
716    
717  int main()  int main()
718  {  {
719    environment myenv;    environment myenv;
# Line 649  int main() Line 725  int main()
725    
726    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
727      stack_read(&myenv, in_string);      stack_read(&myenv, in_string);
728        if(myenv.err) {
729          printf("(error %d) ", myenv.err);
730          myenv.err=0;
731        }
732      printf("okidok\n ");      printf("okidok\n ");
733    }    }
734    

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26