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

Diff of /stack/stack.c

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

revision 1.21 by masse, Sat Feb 2 15:26:10 2002 UTC revision 1.24 by masse, Sat Feb 2 19:30:07 2002 UTC
# Line 160  int push_ref(stackitem** stack_head, has Line 160  int push_ref(stackitem** stack_head, has
160      else      else
161        def_sym(in_hash, in_string); /* Make symbol */        def_sym(in_hash, in_string); /* Make symbol */
162                
163      new_item->content.ptr= *hash(in_hash, in_string); /* XXX */      new_item->content.ptr= *hash(in_hash, in_string); /* The new reference
164                                                             shouldn't point at
165                                                             NULL */
166      new_item->type= ref;      new_item->type= ref;
167    }    }
168    
# Line 196  extern void nl() Line 198  extern void nl()
198  }  }
199    
200  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
201  void print_(stackitem** stack_head)  extern void print_(stackitem** stack_head)
202  {  {
203    if((*stack_head)==NULL) {    stackitem* temp= *stack_head;
204    
205      if(temp==NULL) {
206      printerr("Stack empty");      printerr("Stack empty");
207      return;      return;
208    }    }
209    
210    switch((*stack_head)->type) {    while(temp->type==ref)
211        temp= temp->content.ptr;
212    
213      switch(temp->type) {
214    case value:    case value:
215      printf("%d", (*stack_head)->content.val);      printf("%d", temp->content.val);
216      break;      break;
217    case string:    case string:
218      printf("\"%s\"", (char*)(*stack_head)->content.ptr);      printf("\"%s\"", (char*)temp->content.ptr);
     break;  
   case ref:  
     printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);  
219      break;      break;
220    case symbol:    case symbol:
221        printf("%s", temp->id);
222        break;
223    default:    default:
224      printf("%p", (*stack_head)->content.ptr);      printf("%p", temp->content.ptr);
225      break;      break;
226    }    }
227  }  }
# Line 254  extern void printstack(stackitem** stack Line 260  extern void printstack(stackitem** stack
260  extern void eval(stackitem** stack_head)  extern void eval(stackitem** stack_head)
261  {  {
262    funcp in_func;    funcp in_func;
263      stackitem* temp= *stack_head;
264    
265    if((*stack_head)==NULL || (*stack_head)->type!=ref) {    if(temp==NULL) {
266      printerr("Stack empty or not a reference");      printerr("Stack empty");
267      return;      return;
268    }    }
269    
270    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {    while(temp->type==ref)
271      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;      temp= temp->content.ptr;
272    
273      if(temp->type==func) {
274        in_func= (funcp)(temp->content.ptr);
275      toss(stack_head);      toss(stack_head);
276      (*in_func)(stack_head);      (*in_func)(stack_head);
277      return;      return;
278    } else    }
279      printerr("Not a function");      
280      printerr("Couldn't evaluate");
281  }  }
282    
283  /* Make a list. */  /* Make a list. */
# Line 275  extern void pack(stackitem** stack_head) Line 286  extern void pack(stackitem** stack_head)
286    void* delimiter;    void* delimiter;
287    stackitem *iterator, *temp, *pack;    stackitem *iterator, *temp, *pack;
288    
   if((*stack_head)==NULL) {  
     printerr("Stack empty");  
     return;  
   }  
   
289    delimiter= (*stack_head)->content.ptr; /* Get delimiter */    delimiter= (*stack_head)->content.ptr; /* Get delimiter */
290    toss(stack_head);    toss(stack_head);
291    
292    iterator= *stack_head;    iterator= *stack_head;
293    
294    /* Search for first delimiter */    if(iterator==NULL || iterator->content.ptr==delimiter) {
295    while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)      temp= NULL;
     iterator= iterator->next;  
   
   /* Extract list */  
   temp= *stack_head;  
   *stack_head= iterator->next;  
   iterator->next= NULL;  
     
   if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)  
296      toss(stack_head);      toss(stack_head);
297      } else {
298        /* Search for first delimiter */
299        while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)
300          iterator= iterator->next;
301        
302        /* Extract list */
303        temp= *stack_head;
304        *stack_head= iterator->next;
305        iterator->next= NULL;
306        
307        if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)
308          toss(stack_head);
309      }
310    
311    /* Push list */    /* Push list */
312    pack= malloc(sizeof(stackitem));    pack= malloc(sizeof(stackitem));
# Line 396  extern void expand(stackitem** stack_hea Line 407  extern void expand(stackitem** stack_hea
407    new_head= temp= (*stack_head)->content.ptr;    new_head= temp= (*stack_head)->content.ptr;
408    toss(stack_head);    toss(stack_head);
409    
410      if(temp==NULL)
411        return;
412    
413    /* Search the end of the list */    /* Search the end of the list */
414    while(temp->next!=NULL)    while(temp->next!=NULL)
415      temp= temp->next;      temp= temp->next;
# Line 492  extern void quit() Line 506  extern void quit()
506    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
507  }  }
508    
509    /* Clear stack */
510    extern void clear(stackitem** stack_head)
511    {
512      while(*stack_head!=NULL)
513        toss(stack_head);
514    }
515    
516  int main()  int main()
517  {  {
518    stackitem* s= NULL;    stackitem* s= NULL;

Legend:
Removed from v.1.21  
changed lines
  Added in v.1.24

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26