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

Diff of /stack/stack.c

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

revision 1.16 by masse, Thu Jan 31 22:32:59 2002 UTC revision 1.17 by masse, Thu Jan 31 23:09:07 2002 UTC
# Line 162  int push_ref(stackitem** stack_head, has Line 162  int push_ref(stackitem** stack_head, has
162    return 1;    return 1;
163  }  }
164    
165    void printerr(const char* in_string) {
166      fprintf(stderr, "Err: %s\n", in_string);
167    }
168    
169  /* Discard the top element of the stack. */  /* Discard the top element of the stack. */
170  extern void toss(stackitem** stack_head)  extern void toss(stackitem** stack_head)
171  {  {
172    stackitem* temp= *stack_head;    stackitem* temp= *stack_head;
173    
174    if((*stack_head)==NULL)    if((*stack_head)==NULL) {
175        printerr("Stack empty");
176      return;      return;
177      }
178        
179    if((*stack_head)->type==string)    if((*stack_head)->type==string)
180      free((*stack_head)->content.ptr);      free((*stack_head)->content.ptr);
# Line 186  extern void nl() Line 192  extern void nl()
192  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
193  void print_(stackitem** stack_head)  void print_(stackitem** stack_head)
194  {  {
195    if((*stack_head)==NULL)    if((*stack_head)==NULL) {
196        printerr("Stack empty");
197      return;      return;
198      }
199    
200    switch((*stack_head)->type) {    switch((*stack_head)->type) {
201    case value:    case value:
# Line 230  extern void printstack(stackitem** stack Line 238  extern void printstack(stackitem** stack
238    if(*stack_head != NULL) {    if(*stack_head != NULL) {
239      print_st(*stack_head, 1);      print_st(*stack_head, 1);
240      printf("\n");      printf("\n");
241      } else {
242        printerr("Stack empty");
243    }    }
244  }  }
245    
# Line 239  extern void eval(stackitem** stack_head) Line 249  extern void eval(stackitem** stack_head)
249  {  {
250    funcp in_func;    funcp in_func;
251    
252    if((*stack_head)==NULL || (*stack_head)->type!=ref)    if((*stack_head)==NULL || (*stack_head)->type!=ref) {
253        printerr("Stack empty or not a reference");
254      return;      return;
255      }
256    
257    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {
258      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;
259      toss(stack_head);      toss(stack_head);
260      (*in_func)(stack_head);      (*in_func)(stack_head);
261      return;      return;
262    }    } else
263        printerr("Not a function");
264  }  }
265    
266  /* Parse input. */  /* Parse input. */
# Line 310  extern void pack(stackitem** stack_head) Line 323  extern void pack(stackitem** stack_head)
323    void* delimiter;    void* delimiter;
324    stackitem *iterator, *temp, *pack;    stackitem *iterator, *temp, *pack;
325    
326    if((*stack_head)==NULL)       /* No delimiter */    if((*stack_head)==NULL) {
327        printerr("Stack empty");
328      return;      return;
329      }
330    
331    delimiter= (*stack_head)->content.ptr; /* Get delimiter */    delimiter= (*stack_head)->content.ptr; /* Get delimiter */
332    toss(stack_head);    toss(stack_head);
# Line 344  extern void expand(stackitem** stack_hea Line 359  extern void expand(stackitem** stack_hea
359    stackitem *temp, *new_head;    stackitem *temp, *new_head;
360    
361    /* Is top element a list? */    /* Is top element a list? */
362    if((*stack_head)==NULL || (*stack_head)->type!=list)    if((*stack_head)==NULL || (*stack_head)->type!=list) {
363        printerr("Stack empty or not a list");
364      return;      return;
365      }
366    
367    /* The first list element is the new stack head */    /* The first list element is the new stack head */
368    new_head= temp= (*stack_head)->content.ptr;    new_head= temp= (*stack_head)->content.ptr;
# Line 365  extern void swap(stackitem** stack_head) Line 382  extern void swap(stackitem** stack_head)
382  {  {
383    stackitem* temp= (*stack_head);    stackitem* temp= (*stack_head);
384        
385    if((*stack_head)==NULL || (*stack_head)->next==NULL)    if((*stack_head)==NULL) {
386        printerr("Stack empty");
387        return;
388      }
389    
390      if((*stack_head)->next==NULL)
391      return;      return;
392    
393    *stack_head= (*stack_head)->next;    *stack_head= (*stack_head)->next;
# Line 379  extern void eq(stackitem** stack_head) Line 401  extern void eq(stackitem** stack_head)
401    void *left, *right;    void *left, *right;
402    int result;    int result;
403    
404    if((*stack_head)==NULL || (*stack_head)->next==NULL)    if((*stack_head)==NULL || (*stack_head)->next==NULL) {
405        printerr("Not enough elements to compare");
406      return;      return;
407      }
408    
409    left= (*stack_head)->content.ptr;    left= (*stack_head)->content.ptr;
410    swap(stack_head);    swap(stack_head);
# Line 396  extern void not(stackitem** stack_head) Line 420  extern void not(stackitem** stack_head)
420  {  {
421    int value;    int value;
422    
423    if((*stack_head)==NULL || (*stack_head)->type!=value)    if((*stack_head)==NULL || (*stack_head)->type!=value) {
424        printerr("Stack empty or element is not a value");
425      return;      return;
426      }
427    
428    value= (*stack_head)->content.val;    value= (*stack_head)->content.val;
429    toss(stack_head);    toss(stack_head);
# Line 418  extern void def(stackitem** stack_head) Line 444  extern void def(stackitem** stack_head)
444    stackitem *temp, *value;    stackitem *temp, *value;
445    
446    if(*stack_head==NULL || (*stack_head)->next==NULL    if(*stack_head==NULL || (*stack_head)->next==NULL
447       || (*stack_head)->type!=ref)       || (*stack_head)->type!=ref) {
448        printerr("Define what?");
449      return;      return;
450      }
451    
452    temp= (*stack_head)->content.ptr;    temp= (*stack_head)->content.ptr;
453    value= (*stack_head)->next;    value= (*stack_head)->next;

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.17

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26