/[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.37 by teddy, Wed Feb 6 01:51:08 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 222  void free_val(value *val){ Line 218  void free_val(value *val){
218      switch (val->type){         /* and free the contents if necessary */      switch (val->type){         /* and free the contents if necessary */
219      case string:      case string:
220        free(val->content.ptr);        free(val->content.ptr);
221          break;
222      case list:                  /* lists needs to be freed recursively */      case list:                  /* lists needs to be freed recursively */
223        item=val->content.ptr;        item=val->content.ptr;
224        while(item != NULL) {     /* for all stack items */        while(item != NULL) {     /* for all stack items */
# Line 244  extern void toss(environment *env) Line 241  extern void toss(environment *env)
241    stackitem *temp= env->head;    stackitem *temp= env->head;
242    
243    if((env->head)==NULL) {    if((env->head)==NULL) {
244      printerr("Stack empty");      printerr("Too Few Arguments");
245        env->err=1;
246      return;      return;
247    }    }
248        
# Line 259  extern void nl() Line 257  extern void nl()
257    printf("\n");    printf("\n");
258  }  }
259    
260  /* Prints the top element of the stack. */  /* Gets the type of a value */
261  void print_h(stackitem *stack_head)  extern void type(environment *env){
262  {    int typenum;
263    
264    if(stack_head==NULL) {    if((env->head)==NULL) {
265      printerr("Stack empty");      printerr("Too Few Arguments");
266        env->err=1;
267      return;      return;
268    }    }
269      typenum=env->head->item->type;
270      toss(env);
271      switch(typenum){
272      case integer:
273        push_sym(env, "integer");
274        break;
275      case string:
276        push_sym(env, "string");
277        break;
278      case symb:
279        push_sym(env, "symbol");
280        break;
281      case func:
282        push_sym(env, "function");
283        break;
284      case list:
285        push_sym(env, "list");
286        break;
287      default:
288        push_sym(env, "unknown");
289        break;
290      }
291    }    
292    
293    /* Prints the top element of the stack. */
294    void print_h(stackitem *stack_head)
295    {
296    switch(stack_head->item->type) {    switch(stack_head->item->type) {
297    case integer:    case integer:
298      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
# Line 278  void print_h(stackitem *stack_head) Line 303  void print_h(stackitem *stack_head)
303    case symb:    case symb:
304      printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id);      printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id);
305      break;      break;
306      case func:
307        printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
308        break;
309      case list:
310        printf("#<list %p>", (funcp)(stack_head->item->content.ptr));
311        break;
312    default:    default:
313      printf("%p", (funcp)(stack_head->item->content.ptr));      printf("#<unknown %p>", (funcp)(stack_head->item->content.ptr));
314      break;      break;
315    }    }
316  }  }
317    
318  extern void print_(environment *env) {  extern void print_(environment *env) {
319      if(env->head==NULL) {
320        printerr("Too Few Arguments");
321        env->err=1;
322        return;
323      }
324    print_h(env->head);    print_h(env->head);
325  }  }
326    
# Line 292  extern void print_(environment *env) { Line 328  extern void print_(environment *env) {
328  extern void print(environment *env)  extern void print(environment *env)
329  {  {
330    print_(env);    print_(env);
331      if(env->err) return;
332    toss(env);    toss(env);
333  }  }
334    
# Line 310  void print_st(stackitem *stack_head, lon Line 347  void print_st(stackitem *stack_head, lon
347  /* Prints the stack. */  /* Prints the stack. */
348  extern void printstack(environment *env)  extern void printstack(environment *env)
349  {  {
350    if(env->head != NULL) {    if(env->head == NULL) {
351      print_st(env->head, 1);      return;
     nl();  
   } else {  
     printerr("Stack empty");  
352    }    }
353      print_st(env->head, 1);
354      nl();
355  }  }
356    
357  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 324  extern void swap(environment *env) Line 360  extern void swap(environment *env)
360    stackitem *temp= env->head;    stackitem *temp= env->head;
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->next==NULL) {    if(env->head->next==NULL) {
369      printerr("Not enough arguments");      printerr("Too Few Arguments");
370        env->err=1;
371      return;      return;
372    }    }
373    
# Line 348  stackitem* copy(stackitem* in_item) Line 386  stackitem* copy(stackitem* in_item)
386    return out_item;    return out_item;
387  }  }
388    
389    /* Recall a value from a symbol, if bound */
390  extern void rcl(environment *env)  extern void rcl(environment *env)
391  {  {
392    value *val;    value *val;
393    
394    if(env->head == NULL) {    if(env->head == NULL) {
395      printerr("Stack empty");      printerr("Too Few Arguments");
396        env->err=1;
397      return;      return;
398    }    }
399    
400    if(env->head->item->type!=symb) {    if(env->head->item->type!=symb) {
401      printerr("Not a symbol");      printerr("Bad Argument Type");
402        env->err=2;
403      return;      return;
404    }    }
405    
406    val=((symbol *)(env->head->item->content.ptr))->val;    val=((symbol *)(env->head->item->content.ptr))->val;
407      if(val == NULL){
408        printerr("Unbound Variable");
409        env->err=3;
410        return;
411      }
412    toss(env);            /* toss the symbol */    toss(env);            /* toss the symbol */
413      if(env->err) return;
414    push_val(&(env->head), val); /* Return its bound value */    push_val(&(env->head), val); /* Return its bound value */
415  }  }
416    
# Line 372  extern void rcl(environment *env) Line 420  extern void rcl(environment *env)
420  extern void eval(environment *env)  extern void eval(environment *env)
421  {  {
422    funcp in_func;    funcp in_func;
   value* val;  
423    if(env->head==NULL) {    if(env->head==NULL) {
424      printerr("Stack empty");      printerr("Too Few Arguments");
425        env->err=1;
426      return;      return;
427    }    }
428    
429    /* if it's a symbol */    /* if it's a symbol */
430    if(env->head->item->type==symb) {    if(env->head->item->type==symb) {
431    
432      /* If it's not bound to anything */      rcl(env);                   /* get its contents */
433      if (((symbol *)(env->head->item->content.ptr))->val == NULL) {      if(env->err) return;
434        printerr("Unbound variable");      if(env->head->item->type!=symb){ /* don't recurse symbols */
435          eval(env);                        /* evaluate the value */
436        return;        return;
437      }      }
       
     /* 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 */  
     }  
438    }    }
439    
440    /* If it's a lone function value, run it */    /* If it's a lone function value, run it */
441    if(env->head->item->type==func) {    if(env->head->item->type==func) {
442      in_func= (funcp)(env->head->item->content.ptr);      in_func= (funcp)(env->head->item->content.ptr);
443      toss(env);      toss(env);
444        if(env->err) return;
445      (*in_func)(env);      (*in_func)(env);
     return;  
446    }    }
   
447  }  }
448    
449  /* Make a list. */  /* Make a list. */
# Line 454  extern void pack(environment *env) Line 489  extern void pack(environment *env)
489  }  }
490    
491  /* Parse input. */  /* Parse input. */
492  int stack_read(environment *env, char *in_line)  void stack_read(environment *env, char *in_line)
493  {  {
494    char *temp, *rest;    char *temp, *rest;
495    int itemp;    int itemp;
# Line 483  int stack_read(environment *env, char *i Line 518  int stack_read(environment *env, char *i
518        break;        break;
519      }      }
520      /* If symbol */      /* If symbol */
521      if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
522          push_sym(env, temp);          push_sym(env, temp);
523          break;          break;
524      }      }
# Line 515  int stack_read(environment *env, char *i Line 550  int stack_read(environment *env, char *i
550      }      }
551    } while(0);    } while(0);
552    
   
553    free(temp);    free(temp);
554    
555    if(convert<2) {    if(convert<2) {
556      free(rest);      free(rest);
557      return 0;      return;
558    }    }
559        
560    stack_read(env, rest);    stack_read(env, rest);
561        
562    free(rest);    free(rest);
   return 1;  
563  }  }
564    
565  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
# Line 535  extern void expand(environment *env) Line 568  extern void expand(environment *env)
568    stackitem *temp, *new_head;    stackitem *temp, *new_head;
569    
570    /* Is top element a list? */    /* Is top element a list? */
571    if(env->head==NULL || env->head->item->type!=list) {    if(env->head==NULL) {
572      printerr("Stack empty or not a list");      printerr("Too Few Arguments");
573        env->err=1;
574        return;
575      }
576      if(env->head->item->type!=list) {
577        printerr("Bad Argument Type");
578        env->err=2;
579      return;      return;
580    }    }
581    
# Line 563  extern void eq(environment *env) Line 602  extern void eq(environment *env)
602    int result;    int result;
603    
604    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
605      printerr("Not enough elements to compare");      printerr("Too Few Arguments");
606        env->err=1;
607      return;      return;
608    }    }
609    
# Line 581  extern void not(environment *env) Line 621  extern void not(environment *env)
621  {  {
622    int val;    int val;
623    
624    if((env->head)==NULL || env->head->item->type!=integer) {    if((env->head)==NULL) {
625      printerr("Stack empty or element is not a integer");      printerr("Too Few Arguments");
626        env->err=1;
627        return;
628      }
629    
630      if(env->head->item->type!=integer) {
631        printerr("Bad Argument Type");
632        env->err=2;
633      return;      return;
634    }    }
635    
# Line 605  extern void def(environment *env) Line 652  extern void def(environment *env)
652    symbol *sym;    symbol *sym;
653    
654    /* 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 */
655    if(env->head==NULL || env->head->next==NULL    if(env->head==NULL || env->head->next==NULL) {
656       || env->head->item->type!=symb) {      printerr("Too Few Arguments");
657      printerr("Define what?");      env->err=1;
658        return;
659      }
660    
661      if(env->head->item->type!=symb) {
662        printerr("Bad Argument Type");
663        env->err=2;
664      return;      return;
665    }    }
666    
# Line 638  extern void clear(environment *env) Line 691  extern void clear(environment *env)
691      toss(env);      toss(env);
692  }  }
693    
694    /* List all defined words */
695    extern void words(environment *env)
696    {
697      symbol *temp;
698      int i;
699      
700      for(i= 0; i<HASHTBLSIZE; i++) {
701        temp= env->symbols[i];
702        while(temp!=NULL) {
703          printf("%s\n", temp->id);
704          temp= temp->next;
705        }
706      }
707    }
708    
709    /* Forgets a symbol (remove it from the hash table) */
710    extern void forget(environment *env)
711    {
712      char* sym_id;
713      stackitem *stack_head= env->head;
714      symbol **hash_entry, *temp;
715    
716      if(stack_head==NULL) {
717        printerr("Too Few Arguments");
718        env->err=1;
719        return;
720      }
721      
722      if(stack_head->item->type!=symb) {
723        printerr("Bad Argument Type");
724        env->err=2;
725        return;
726      }
727    
728      sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
729      toss(env);
730    
731      hash_entry= hash(env->symbols, sym_id);
732      temp= *hash_entry;
733      *hash_entry= (*hash_entry)->next;
734      
735      if(temp->val!=NULL) {
736        free_val(temp->val);
737      }
738      free(temp->id);
739      free(temp);
740    }
741    
742    /* Returns the current error number to the stack */
743    extern void errn(environment *env){
744      push_int(&(env->head), env->err);
745    }
746    
747  int main()  int main()
748  {  {
749    environment myenv;    environment myenv;
# Line 649  int main() Line 755  int main()
755    
756    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
757      stack_read(&myenv, in_string);      stack_read(&myenv, in_string);
758        if(myenv.err) {
759          printf("(error %d) ", myenv.err);
760          myenv.err=0;
761        }
762      printf("okidok\n ");      printf("okidok\n ");
763    }    }
764    

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26