/[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.43 by masse, Wed Feb 6 23:27:22 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 178  int push_sym(environment *env, const cha Line 175  int push_sym(environment *env, const cha
175    new_symbol= hash(env->symbols, in_string);    new_symbol= hash(env->symbols, in_string);
176    new_value->content.ptr= *new_symbol;    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);
# 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        /* 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 292  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 310  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 324  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 348  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      val=((symbol *)(env->head->item->content.ptr))->val;
415      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  /* 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     function value, and if it is, toss the symbol and execute the
# Line 355  stackitem* copy(stackitem* in_item) Line 428  stackitem* copy(stackitem* in_item)
428  extern void eval(environment *env)  extern void eval(environment *env)
429  {  {
430    funcp in_func;    funcp in_func;
   value* val;  
431    if(env->head==NULL) {    if(env->head==NULL) {
432      printerr("Stack empty");      printerr("Too Few Arguments");
433        env->err=1;
434      return;      return;
435    }    }
436    
437    /* if it's a symbol */    /* if it's a symbol */
438    if(env->head->item->type==symb) {    if(env->head->item->type==symb) {
439    
440      /* If it's not bound to anything */      rcl(env);                   /* get its contents */
441      if (((symbol *)(env->head->item->content.ptr))->val == NULL) {      if(env->err) return;
442        printerr("Unbound variable");      if(env->head->item->type!=symb){ /* don't recurse symbols */
443        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 */  
444        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 */  
445      }      }
446    }    }
447    
# Line 388  extern void eval(environment *env) Line 449  extern void eval(environment *env)
449    if(env->head->item->type==func) {    if(env->head->item->type==func) {
450      in_func= (funcp)(env->head->item->content.ptr);      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);
454      }
455    }
456    
457    /* Reverse a list */
458    extern void rev(environment *env){
459      stackitem *old_head, *new_head, *item;
460    
461      if((env->head)==NULL) {
462        printerr("Too Few Arguments");
463        env->err=1;
464      return;      return;
465    }    }
466    
467  /*    push(&(env->head), copy(env->head)); */    if(env->head->item->type!=list) {
468  /*    swap(env); */      printerr("Bad Argument Type");
469  /*    toss(env); */      env->err=2;
470        return;
471      }
472    
473      old_head=(stackitem *)(env->head->item->content.ptr);
474      new_head=NULL;
475      while(old_head != NULL){
476        item=old_head;
477        old_head=old_head->next;
478        item->next=new_head;
479        new_head=item;
480      }
481      env->head->item->content.ptr=new_head;
482  }  }
483    
484  /* Make a list. */  /* Make a list. */
# Line 437  extern void pack(environment *env) Line 521  extern void pack(environment *env)
521    temp->item= pack;    temp->item= pack;
522    
523    push(&(env->head), temp);    push(&(env->head), temp);
524      rev(env);
525  }  }
526    
527  /* Parse input. */  /* Parse input. */
528  int stack_read(environment *env, char *in_line)  void stack_read(environment *env, char *in_line)
529  {  {
530    char *temp, *rest;    char *temp, *rest;
531    int itemp;    int itemp;
# Line 452  int stack_read(environment *env, char *i Line 537  int stack_read(environment *env, char *i
537    rest= malloc(inlength);    rest= malloc(inlength);
538    
539    do {    do {
540        /* If comment */
541        if((convert= sscanf(in_line, "#%[^\n\r]", rest))) {
542          free(temp); free(rest);
543          return;
544        }
545    
546      /* If string */      /* If string */
547      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
548        push_cstring(&(env->head), temp);        push_cstring(&(env->head), temp);
# Line 469  int stack_read(environment *env, char *i Line 560  int stack_read(environment *env, char *i
560        break;        break;
561      }      }
562      /* If symbol */      /* If symbol */
563      if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
564          push_sym(env, temp);          push_sym(env, temp);
565          break;          break;
566      }      }
# Line 501  int stack_read(environment *env, char *i Line 592  int stack_read(environment *env, char *i
592      }      }
593    } while(0);    } while(0);
594    
   
595    free(temp);    free(temp);
596    
597    if(convert<2) {    if(convert<2) {
598      free(rest);      free(rest);
599      return 0;      return;
600    }    }
601        
602    stack_read(env, rest);    stack_read(env, rest);
603        
604    free(rest);    free(rest);
   return 1;  
605  }  }
606    
607  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
# Line 521  extern void expand(environment *env) Line 610  extern void expand(environment *env)
610    stackitem *temp, *new_head;    stackitem *temp, *new_head;
611    
612    /* Is top element a list? */    /* Is top element a list? */
613    if(env->head==NULL || env->head->item->type!=list) {    if(env->head==NULL) {
614      printerr("Stack empty or not a list");      printerr("Too Few Arguments");
615        env->err=1;
616      return;      return;
617    }    }
618      if(env->head->item->type!=list) {
619        printerr("Bad Argument Type");
620        env->err=2;
621        return;
622      }
623    
624      rev(env);
625    
626      if(env->err)
627        return;
628    
629    /* The first list element is the new stack head */    /* The first list element is the new stack head */
630    new_head= temp= env->head->item->content.ptr;    new_head= temp= env->head->item->content.ptr;
# Line 549  extern void eq(environment *env) Line 649  extern void eq(environment *env)
649    int result;    int result;
650    
651    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
652      printerr("Not enough elements to compare");      printerr("Too Few Arguments");
653        env->err=1;
654      return;      return;
655    }    }
656    
# Line 567  extern void not(environment *env) Line 668  extern void not(environment *env)
668  {  {
669    int val;    int val;
670    
671    if((env->head)==NULL || env->head->item->type!=integer) {    if((env->head)==NULL) {
672      printerr("Stack empty or element is not a integer");      printerr("Too Few Arguments");
673        env->err=1;
674        return;
675      }
676    
677      if(env->head->item->type!=integer) {
678        printerr("Bad Argument Type");
679        env->err=2;
680      return;      return;
681    }    }
682    
# Line 591  extern void def(environment *env) Line 699  extern void def(environment *env)
699    symbol *sym;    symbol *sym;
700    
701    /* 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 */
702    if(env->head==NULL || env->head->next==NULL    if(env->head==NULL || env->head->next==NULL) {
703       || env->head->item->type!=symb) {      printerr("Too Few Arguments");
704      printerr("Define what?");      env->err=1;
705        return;
706      }
707    
708      if(env->head->item->type!=symb) {
709        printerr("Bad Argument Type");
710        env->err=2;
711      return;      return;
712    }    }
713    
# Line 624  extern void clear(environment *env) Line 738  extern void clear(environment *env)
738      toss(env);      toss(env);
739  }  }
740    
741    /* List all defined words */
742    extern void words(environment *env)
743    {
744      symbol *temp;
745      int i;
746      
747      for(i= 0; i<HASHTBLSIZE; i++) {
748        temp= env->symbols[i];
749        while(temp!=NULL) {
750          printf("%s\n", temp->id);
751          temp= temp->next;
752        }
753      }
754    }
755    
756    /* Forgets a symbol (remove it from the hash table) */
757    extern void forget(environment *env)
758    {
759      char* sym_id;
760      stackitem *stack_head= env->head;
761      symbol **hash_entry, *temp;
762    
763      if(stack_head==NULL) {
764        printerr("Too Few Arguments");
765        env->err=1;
766        return;
767      }
768      
769      if(stack_head->item->type!=symb) {
770        printerr("Bad Argument Type");
771        env->err=2;
772        return;
773      }
774    
775      sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
776      toss(env);
777    
778      hash_entry= hash(env->symbols, sym_id);
779      temp= *hash_entry;
780      *hash_entry= (*hash_entry)->next;
781      
782      if(temp->val!=NULL) {
783        free_val(temp->val);
784      }
785      free(temp->id);
786      free(temp);
787    }
788    
789    /* Returns the current error number to the stack */
790    extern void errn(environment *env){
791      push_int(&(env->head), env->err);
792    }
793    
794  int main()  int main()
795  {  {
796    environment myenv;    environment myenv;
# Line 635  int main() Line 802  int main()
802    
803    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
804      stack_read(&myenv, in_string);      stack_read(&myenv, in_string);
805        if(myenv.err) {
806          printf("(error %d) ", myenv.err);
807          myenv.err=0;
808        }
809      printf("okidok\n ");      printf("okidok\n ");
810    }    }
811      quit(&myenv);
812    exit(EXIT_SUCCESS);    return EXIT_FAILURE;
813  }  }

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26