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

Diff of /stack/stack.c

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

revision 1.18 by teddy, Fri Feb 1 23:30:55 2002 UTC revision 1.25 by masse, Sat Feb 2 20:06:11 2002 UTC
# Line 64  stackitem** hash(hashtbl in_hashtbl, con Line 64  stackitem** hash(hashtbl in_hashtbl, con
64    out_hash= out_hash%HASHTBLSIZE;    out_hash= out_hash%HASHTBLSIZE;
65    position= &(in_hashtbl[out_hash]);    position= &(in_hashtbl[out_hash]);
66    
67    while(position != NULL){    while(1){
68      if(*position==NULL)         /* If empty */      if(*position==NULL)         /* If empty */
69        return position;        return position;
70            
# Line 73  stackitem** hash(hashtbl in_hashtbl, con Line 73  stackitem** hash(hashtbl in_hashtbl, con
73    
74      position= &((*position)->next); /* Try next */      position= &((*position)->next); /* Try next */
75    }    }
   return NULL;                  /* end of list reached without finding  
                                    an empty position */  
76  }  }
77    
78  /* Generic push function. */  /* Generic push function. */
# Line 160  int push_ref(stackitem** stack_head, has Line 158  int push_ref(stackitem** stack_head, has
158      else      else
159        def_sym(in_hash, in_string); /* Make symbol */        def_sym(in_hash, in_string); /* Make symbol */
160                
161      new_item->content.ptr= *hash(in_hash, in_string); /* XXX */      new_item->content.ptr= *hash(in_hash, in_string); /* The new reference
162                                                             shouldn't point at
163                                                             NULL */
164      new_item->type= ref;      new_item->type= ref;
165    }    }
166    
# Line 196  extern void nl() Line 196  extern void nl()
196  }  }
197    
198  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
199  void print_(stackitem** stack_head)  extern void print_(stackitem** stack_head)
200  {  {
201    if((*stack_head)==NULL) {    stackitem* temp= *stack_head;
202    
203      if(temp==NULL) {
204      printerr("Stack empty");      printerr("Stack empty");
205      return;      return;
206    }    }
207    
208    switch((*stack_head)->type) {    while(temp->type==ref)
209        temp= temp->content.ptr;
210    
211      switch(temp->type) {
212    case value:    case value:
213      printf("%d", (*stack_head)->content.val);      printf("%d", temp->content.val);
214      break;      break;
215    case string:    case string:
216      printf("%s", (char*)(*stack_head)->content.ptr);      printf("\"%s\"", (char*)temp->content.ptr);
     break;  
   case ref:  
     printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);  
217      break;      break;
218    case symbol:    case symbol:
219      case func:
220        printf("%s", temp->id);
221        break;
222    default:    default:
223      printf("%p", (*stack_head)->content.ptr);      printf("%p", temp->content.ptr);
224      break;      break;
225    }    }
226  }  }
# Line 243  extern void printstack(stackitem** stack Line 248  extern void printstack(stackitem** stack
248  {  {
249    if(*stack_head != NULL) {    if(*stack_head != NULL) {
250      print_st(*stack_head, 1);      print_st(*stack_head, 1);
251      printf("\n");      nl();
252    } else {    } else {
253      printerr("Stack empty");      printerr("Stack empty");
254    }    }
# Line 254  extern void printstack(stackitem** stack Line 259  extern void printstack(stackitem** stack
259  extern void eval(stackitem** stack_head)  extern void eval(stackitem** stack_head)
260  {  {
261    funcp in_func;    funcp in_func;
262      stackitem* temp= *stack_head;
263    
264    if((*stack_head)==NULL || (*stack_head)->type!=ref) {    if(temp==NULL) {
265      printerr("Stack empty or not a reference");      printerr("Stack empty");
266      return;      return;
267    }    }
268    
269    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {    while(temp->type==ref)
270      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;      temp= temp->content.ptr;
271    
272      if(temp->type==func) {
273        in_func= (funcp)(temp->content.ptr);
274      toss(stack_head);      toss(stack_head);
275      (*in_func)(stack_head);      (*in_func)(stack_head);
276      return;      return;
277    } else    }
278      printerr("Not a function");      
279      printerr("Couldn't evaluate");
280    }
281    
282    /* Make a list. */
283    extern void pack(stackitem** stack_head)
284    {
285      void* delimiter;
286      stackitem *iterator, *temp, *pack;
287    
288      delimiter= (*stack_head)->content.ptr; /* Get delimiter */
289      toss(stack_head);
290    
291      iterator= *stack_head;
292    
293      if(iterator==NULL || iterator->content.ptr==delimiter) {
294        temp= NULL;
295        toss(stack_head);
296      } else {
297        /* Search for first delimiter */
298        while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)
299          iterator= iterator->next;
300        
301        /* Extract list */
302        temp= *stack_head;
303        *stack_head= iterator->next;
304        iterator->next= NULL;
305        
306        if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)
307          toss(stack_head);
308      }
309    
310      /* Push list */
311      pack= malloc(sizeof(stackitem));
312      pack->type= list;
313      pack->content.ptr= temp;
314    
315      push(stack_head, pack);
316  }  }
317    
318  /* Parse input. */  /* Parse input. */
# Line 276  int stack_read(stackitem** stack_head, h Line 322  int stack_read(stackitem** stack_head, h
322    int itemp;    int itemp;
323    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
324    int convert= 0;    int convert= 0;
325      static int non_eval_flag= 0;
326    
327    temp= malloc(inlength);    temp= malloc(inlength);
328    rest= malloc(inlength);    rest= malloc(inlength);
# Line 298  int stack_read(stackitem** stack_head, h Line 345  int stack_read(stackitem** stack_head, h
345        break;        break;
346      }      }
347      /* If symbol */      /* If symbol */
348      if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
349          push_ref(stack_head, in_hash, temp);          push_ref(stack_head, in_hash, temp);
350          break;          break;
351      }      }
352      /* If ';' */      /* If single char */
353      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
354        eval(stack_head);         /* Evaluate top element */        if(*temp==';') {
355        break;          if(!non_eval_flag) {
356              eval(stack_head);             /* Evaluate top element */
357              break;
358            }
359            
360            push_ref(stack_head, in_hash, ";");
361            break;
362          }
363    
364          if(*temp==']') {
365            push_ref(stack_head, in_hash, "[");
366            pack(stack_head);
367            if(non_eval_flag!=0)
368              non_eval_flag--;
369            break;
370          }
371    
372          if(*temp=='[') {
373            push_ref(stack_head, in_hash, "[");
374            non_eval_flag++;
375            break;
376          }
377      }      }
378    } while(0);    } while(0);
379    
# Line 323  int stack_read(stackitem** stack_head, h Line 391  int stack_read(stackitem** stack_head, h
391    return 1;    return 1;
392  }  }
393    
 /* Make a list. */  
 extern void pack(stackitem** stack_head)  
 {  
   void* delimiter;  
   stackitem *iterator, *temp, *pack;  
   
   if((*stack_head)==NULL) {  
     printerr("Stack empty");  
     return;  
   }  
   
   delimiter= (*stack_head)->content.ptr; /* Get delimiter */  
   toss(stack_head);  
   
   iterator= *stack_head;  
   
   /* Search for first delimiter */  
   while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)  
     iterator= iterator->next;  
   
   /* Extract list */  
   temp= *stack_head;  
   *stack_head= iterator->next;  
   iterator->next= NULL;  
     
   if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)  
     toss(stack_head);  
   
   /* Push list */  
   pack= malloc(sizeof(stackitem));  
   pack->type= list;  
   pack->content.ptr= temp;  
   
   push(stack_head, pack);  
 }  
   
394  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
395  extern void expand(stackitem** stack_head)  extern void expand(stackitem** stack_head)
396  {  {
# Line 374  extern void expand(stackitem** stack_hea Line 406  extern void expand(stackitem** stack_hea
406    new_head= temp= (*stack_head)->content.ptr;    new_head= temp= (*stack_head)->content.ptr;
407    toss(stack_head);    toss(stack_head);
408    
409      if(temp==NULL)
410        return;
411    
412    /* Search the end of the list */    /* Search the end of the list */
413    while(temp->next!=NULL)    while(temp->next!=NULL)
414      temp= temp->next;      temp= temp->next;
# Line 470  extern void quit() Line 505  extern void quit()
505    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
506  }  }
507    
508    /* Clear stack */
509    extern void clear(stackitem** stack_head)
510    {
511      while(*stack_head!=NULL)
512        toss(stack_head);
513    }
514    
515  int main()  int main()
516  {  {
517    stackitem* s= NULL;    stackitem* s= NULL;

Legend:
Removed from v.1.18  
changed lines
  Added in v.1.25

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26