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

Diff of /stack/stack.c

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

revision 1.28 by teddy, Mon Feb 4 21:47:26 2002 UTC revision 1.39 by teddy, Wed Feb 6 11:39:20 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;
110    return 1;  }
111    
112    /* Push a value onto the stack */
113    void push_val(stackitem **stack_head, value *val)
114    {
115      stackitem *new_item= malloc(sizeof(stackitem));
116      new_item->item= val;
117      val->refcount++;
118      push(stack_head, new_item);
119  }  }
120    
121  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
122  int push_val(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 122  int push_val(stackitem **stack_head, int Line 130  int push_val(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 138  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... */
155    value *new_value;             /* A new symbol value */    value *new_value;             /* A new symbol value */
156    /* ...which might point to... */    /* ...which might point to... */
157    symbol *new_symbol;           /* (if needed) A new actual symbol */    symbol **new_symbol;          /* (if needed) A new actual symbol */
158    /* ...which, if possible, will be bound to... */    /* ...which, if possible, will be bound to... */
159    value *new_fvalue;            /* (if needed) A new function value */    value *new_fvalue;            /* (if needed) A new function value */
160    /* ...which will point to... */    /* ...which will point to... */
# Line 166  int push_sym(environment *env, const cha Line 172  int push_sym(environment *env, const cha
172    new_value->refcount= 1;    new_value->refcount= 1;
173    
174    /* Look up the symbol name in the hash table */    /* Look up the symbol name in the hash table */
175    new_value->content.ptr= *hash(env->symbols, in_string);    new_symbol= hash(env->symbols, in_string);
176      new_value->content.ptr= *new_symbol;
177    
178    if(new_value->content.ptr==NULL) { /* If symbol was undefined */    if(*new_symbol==NULL) { /* If symbol was undefined */
179    
180      /* Create a new symbol */      /* Create a new symbol */
181      new_symbol= malloc(sizeof(symbol));      (*new_symbol)= malloc(sizeof(symbol));
182      new_symbol->val= NULL;      /* undefined value */      (*new_symbol)->val= NULL;   /* undefined value */
183      new_symbol->next= NULL;      (*new_symbol)->next= NULL;
184      new_symbol->id= malloc(strlen(in_string)+1);      (*new_symbol)->id= malloc(strlen(in_string)+1);
185      strcpy(new_symbol->id, in_string);      strcpy((*new_symbol)->id, in_string);
186    
187      /* Intern the new symbol in the hash table */      /* Intern the new symbol in the hash table */
188      new_value->content.ptr= new_symbol;      new_value->content.ptr= *new_symbol;
189    
190      /* Try to load the symbol name as an external function, to see if      /* Try to load the symbol name as an external function, to see if
191         we should bind the symbol to a new function pointer value */         we should bind the symbol to a new function pointer value */
# Line 190  int push_sym(environment *env, const cha Line 197  int push_sym(environment *env, const cha
197        new_fvalue= malloc(sizeof(value)); /* Create a new value */        new_fvalue= malloc(sizeof(value)); /* Create a new value */
198        new_fvalue->type=func;    /* The new value is a function pointer */        new_fvalue->type=func;    /* The new value is a function pointer */
199        new_fvalue->content.ptr=funcptr; /* Store function pointer */        new_fvalue->content.ptr=funcptr; /* Store function pointer */
200        new_symbol->val= new_fvalue;      /* Bind the symbol to the new        (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
201                                             function value */                                           function value */
202        new_fvalue->refcount= 1;        new_fvalue->refcount= 1;
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 212  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 234  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 249  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 268  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        /* A list is just a stack, so make stack_head point to it */
311        stack_head=(stackitem *)(stack_head->item->content.ptr);
312        printf("[ ");
313        while(stack_head != NULL) {
314          print_h(stack_head);
315          printf(" ");
316          stack_head=stack_head->next;
317        }
318        printf("]");
319        break;
320    default:    default:
321      printf("%p", (funcp)(stack_head->item->content.ptr));      printf("#<unknown %p>", (stack_head->item->content.ptr));
322      break;      break;
323    }    }
324  }  }
325    
326  extern void print_(environment *env) {  extern void print_(environment *env) {
327      if(env->head==NULL) {
328        printerr("Too Few Arguments");
329        env->err=1;
330        return;
331      }
332    print_h(env->head);    print_h(env->head);
333  }  }
334    
# Line 282  extern void print_(environment *env) { Line 336  extern void print_(environment *env) {
336  extern void print(environment *env)  extern void print(environment *env)
337  {  {
338    print_(env);    print_(env);
339      if(env->err) return;
340    toss(env);    toss(env);
341  }  }
342    
# Line 300  void print_st(stackitem *stack_head, lon Line 355  void print_st(stackitem *stack_head, lon
355  /* Prints the stack. */  /* Prints the stack. */
356  extern void printstack(environment *env)  extern void printstack(environment *env)
357  {  {
358    if(env->head != NULL) {    if(env->head == NULL) {
359      print_st(env->head, 1);      return;
     nl();  
   } else {  
     printerr("Stack empty");  
360    }    }
361      print_st(env->head, 1);
362      nl();
363  }  }
364    
365  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 314  extern void swap(environment *env) Line 368  extern void swap(environment *env)
368    stackitem *temp= env->head;    stackitem *temp= env->head;
369        
370    if((env->head)==NULL) {    if((env->head)==NULL) {
371      printerr("Stack empty");      printerr("Too Few Arguments");
372        env->err=1;
373      return;      return;
374    }    }
375    
376    if(env->head->next==NULL) {    if(env->head->next==NULL) {
377      printerr("Not enough arguments");      printerr("Too Few Arguments");
378        env->err=1;
379      return;      return;
380    }    }
381    
# Line 338  stackitem* copy(stackitem* in_item) Line 394  stackitem* copy(stackitem* in_item)
394    return out_item;    return out_item;
395  }  }
396    
397    /* Recall a value from a symbol, if bound */
398    extern void rcl(environment *env)
399    {
400      value *val;
401    
402      if(env->head == NULL) {
403        printerr("Too Few Arguments");
404        env->err=1;
405        return;
406      }
407    
408      if(env->head->item->type!=symb) {
409        printerr("Bad Argument Type");
410        env->err=2;
411        return;
412      }
413    
414  /* If the top element is a reference, determine if it's a reference to a    val=((symbol *)(env->head->item->content.ptr))->val;
415     function, and if it is, toss the reference and execute the function. */    if(val == NULL){
416        printerr("Unbound Variable");
417        env->err=3;
418        return;
419      }
420      toss(env);            /* toss the symbol */
421      if(env->err) return;
422      push_val(&(env->head), val); /* Return its bound value */
423    }
424    
425    /* If the top element is a symbol, determine if it's bound to a
426       function value, and if it is, toss the symbol and execute the
427       function. */
428  extern void eval(environment *env)  extern void eval(environment *env)
429  {  {
430    funcp in_func;    funcp in_func;
431    stackitem* temp= env->head;    if(env->head==NULL) {
432        printerr("Too Few Arguments");
433    if(temp==NULL) {      env->err=1;
     printerr("Stack empty");  
434      return;      return;
435    }    }
436    
437    if(temp->item->type==symb    /* if it's a symbol */
438       && ((symbol *)(temp->item->content.ptr))->val != NULL    if(env->head->item->type==symb) {
439       && ((symbol *)(temp->item->content.ptr))->val->type == func) {  
440      in_func= (funcp)(((symbol *)(temp->item->content.ptr))->val->content.ptr);      rcl(env);                   /* get its contents */
441      toss(env);      if(env->err) return;
442      (*in_func)(env);      if(env->head->item->type!=symb){ /* don't recurse symbols */
443      return;        eval(env);                        /* evaluate the value */
444          return;
445        }
446    }    }
       
447    
448    if(temp->item->type==func) {    /* If it's a lone function value, run it */
449      in_func= (funcp)(temp->item->content.ptr);    if(env->head->item->type==func) {
450        in_func= (funcp)(env->head->item->content.ptr);
451      toss(env);      toss(env);
452        if(env->err) return;
453      (*in_func)(env);      (*in_func)(env);
     return;  
454    }    }
   
   push(&(env->head), copy(temp));  
   swap(env);  
   toss(env);  
455  }  }
456    
457  /* Make a list. */  /* Make a list. */
# Line 416  extern void pack(environment *env) Line 497  extern void pack(environment *env)
497  }  }
498    
499  /* Parse input. */  /* Parse input. */
500  int stack_read(environment *env, char *in_line)  void stack_read(environment *env, char *in_line)
501  {  {
502    char *temp, *rest;    char *temp, *rest;
503    int itemp;    int itemp;
# Line 435  int stack_read(environment *env, char *i Line 516  int stack_read(environment *env, char *i
516      }      }
517      /* If integer */      /* If integer */
518      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
519        push_val(&(env->head), itemp);        push_int(&(env->head), itemp);
520        break;        break;
521      }      }
522      /* Escape ';' with '\' */      /* Escape ';' with '\' */
# Line 445  int stack_read(environment *env, char *i Line 526  int stack_read(environment *env, char *i
526        break;        break;
527      }      }
528      /* If symbol */      /* If symbol */
529      if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
530          push_sym(env, temp);          push_sym(env, temp);
531          break;          break;
532      }      }
# Line 477  int stack_read(environment *env, char *i Line 558  int stack_read(environment *env, char *i
558      }      }
559    } while(0);    } while(0);
560    
   
561    free(temp);    free(temp);
562    
563    if(convert<2) {    if(convert<2) {
564      free(rest);      free(rest);
565      return 0;      return;
566    }    }
567        
568    stack_read(env, rest);    stack_read(env, rest);
569        
570    free(rest);    free(rest);
   return 1;  
571  }  }
572    
573  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
# Line 497  extern void expand(environment *env) Line 576  extern void expand(environment *env)
576    stackitem *temp, *new_head;    stackitem *temp, *new_head;
577    
578    /* Is top element a list? */    /* Is top element a list? */
579    if(env->head==NULL || env->head->item->type!=list) {    if(env->head==NULL) {
580      printerr("Stack empty or not a list");      printerr("Too Few Arguments");
581        env->err=1;
582        return;
583      }
584      if(env->head->item->type!=list) {
585        printerr("Bad Argument Type");
586        env->err=2;
587      return;      return;
588    }    }
589    
# Line 518  extern void expand(environment *env) Line 603  extern void expand(environment *env)
603    
604  }  }
605    
606    /* Reverse a list */
607    extern void rev(environment *env){
608      stackitem *old_head, *new_head, *item;
609    
610      if((env->head)==NULL) {
611        printerr("Too Few Arguments");
612        env->err=1;
613        return;
614      }
615    
616      if(env->head->item->type!=list) {
617        printerr("Bad Argument Type");
618        env->err=2;
619        return;
620      }
621    
622      old_head=(stackitem *)(env->head->item->content.ptr);
623      new_head=NULL;
624      while(old_head != NULL){
625        item=old_head;
626        old_head=old_head->next;
627        item->next=new_head;
628        new_head=item;
629      }
630      env->head->item->content.ptr=new_head;
631    }
632    
633  /* Compares two elements by reference. */  /* Compares two elements by reference. */
634  extern void eq(environment *env)  extern void eq(environment *env)
635  {  {
# Line 525  extern void eq(environment *env) Line 637  extern void eq(environment *env)
637    int result;    int result;
638    
639    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
640      printerr("Not enough elements to compare");      printerr("Too Few Arguments");
641        env->err=1;
642      return;      return;
643    }    }
644    
# Line 535  extern void eq(environment *env) Line 648  extern void eq(environment *env)
648    result= (left==right);    result= (left==right);
649        
650    toss(env); toss(env);    toss(env); toss(env);
651    push_val(&(env->head), result);    push_int(&(env->head), result);
652  }  }
653    
654  /* Negates the top element on the stack. */  /* Negates the top element on the stack. */
# Line 543  extern void not(environment *env) Line 656  extern void not(environment *env)
656  {  {
657    int val;    int val;
658    
659    if((env->head)==NULL || env->head->item->type!=integer) {    if((env->head)==NULL) {
660      printerr("Stack empty or element is not a integer");      printerr("Too Few Arguments");
661        env->err=1;
662        return;
663      }
664    
665      if(env->head->item->type!=integer) {
666        printerr("Bad Argument Type");
667        env->err=2;
668      return;      return;
669    }    }
670    
671    val= env->head->item->content.val;    val= env->head->item->content.val;
672    toss(env);    toss(env);
673    push_val(&(env->head), !val);    push_int(&(env->head), !val);
674  }  }
675    
676  /* Compares the two top elements on the stack and return 0 if they're the  /* Compares the two top elements on the stack and return 0 if they're the
# Line 567  extern void def(environment *env) Line 687  extern void def(environment *env)
687    symbol *sym;    symbol *sym;
688    
689    /* 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 */
690    if(env->head==NULL || env->head->next==NULL    if(env->head==NULL || env->head->next==NULL) {
691       || env->head->item->type!=symb) {      printerr("Too Few Arguments");
692      printerr("Define what?");      env->err=1;
693        return;
694      }
695    
696      if(env->head->item->type!=symb) {
697        printerr("Bad Argument Type");
698        env->err=2;
699      return;      return;
700    }    }
701    
# Line 600  extern void clear(environment *env) Line 726  extern void clear(environment *env)
726      toss(env);      toss(env);
727  }  }
728    
729    /* List all defined words */
730    extern void words(environment *env)
731    {
732      symbol *temp;
733      int i;
734      
735      for(i= 0; i<HASHTBLSIZE; i++) {
736        temp= env->symbols[i];
737        while(temp!=NULL) {
738          printf("%s\n", temp->id);
739          temp= temp->next;
740        }
741      }
742    }
743    
744    /* Forgets a symbol (remove it from the hash table) */
745    extern void forget(environment *env)
746    {
747      char* sym_id;
748      stackitem *stack_head= env->head;
749      symbol **hash_entry, *temp;
750    
751      if(stack_head==NULL) {
752        printerr("Too Few Arguments");
753        env->err=1;
754        return;
755      }
756      
757      if(stack_head->item->type!=symb) {
758        printerr("Bad Argument Type");
759        env->err=2;
760        return;
761      }
762    
763      sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
764      toss(env);
765    
766      hash_entry= hash(env->symbols, sym_id);
767      temp= *hash_entry;
768      *hash_entry= (*hash_entry)->next;
769      
770      if(temp->val!=NULL) {
771        free_val(temp->val);
772      }
773      free(temp->id);
774      free(temp);
775    }
776    
777    /* Returns the current error number to the stack */
778    extern void errn(environment *env){
779      push_int(&(env->head), env->err);
780    }
781    
782  int main()  int main()
783  {  {
784    environment myenv;    environment myenv;
# Line 611  int main() Line 790  int main()
790    
791    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
792      stack_read(&myenv, in_string);      stack_read(&myenv, in_string);
793        if(myenv.err) {
794          printf("(error %d) ", myenv.err);
795          myenv.err=0;
796        }
797      printf("okidok\n ");      printf("okidok\n ");
798    }    }
799    

Legend:
Removed from v.1.28  
changed lines
  Added in v.1.39

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26