/[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.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 243  extern void printstack(stackitem** stack Line 249  extern void printstack(stackitem** stack
249  {  {
250    if(*stack_head != NULL) {    if(*stack_head != NULL) {
251      print_st(*stack_head, 1);      print_st(*stack_head, 1);
252      printf("\n");      nl();
253    } else {    } else {
254      printerr("Stack empty");      printerr("Stack empty");
255    }    }
# 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. */
284    extern void pack(stackitem** stack_head)
285    {
286      void* delimiter;
287      stackitem *iterator, *temp, *pack;
288    
289      delimiter= (*stack_head)->content.ptr; /* Get delimiter */
290      toss(stack_head);
291    
292      iterator= *stack_head;
293    
294      if(iterator==NULL || iterator->content.ptr==delimiter) {
295        temp= NULL;
296        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 */
312      pack= malloc(sizeof(stackitem));
313      pack->type= list;
314      pack->content.ptr= temp;
315    
316      push(stack_head, pack);
317  }  }
318    
319  /* Parse input. */  /* Parse input. */
# Line 276  int stack_read(stackitem** stack_head, h Line 323  int stack_read(stackitem** stack_head, h
323    int itemp;    int itemp;
324    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
325    int convert= 0;    int convert= 0;
326      static int non_eval_flag= 0;
327    
328    temp= malloc(inlength);    temp= malloc(inlength);
329    rest= malloc(inlength);    rest= malloc(inlength);
# Line 298  int stack_read(stackitem** stack_head, h Line 346  int stack_read(stackitem** stack_head, h
346        break;        break;
347      }      }
348      /* If symbol */      /* If symbol */
349      if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
350          push_ref(stack_head, in_hash, temp);          push_ref(stack_head, in_hash, temp);
351          break;          break;
352      }      }
353      /* If ';' */      /* If single char */
354      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
355        eval(stack_head);         /* Evaluate top element */        if(*temp==';') {
356        break;          if(!non_eval_flag) {
357              eval(stack_head);             /* Evaluate top element */
358              break;
359            }
360            
361            push_ref(stack_head, in_hash, ";");
362            break;
363          }
364    
365          if(*temp==']') {
366            push_ref(stack_head, in_hash, "[");
367            pack(stack_head);
368            if(non_eval_flag!=0)
369              non_eval_flag--;
370            break;
371          }
372    
373          if(*temp=='[') {
374            push_ref(stack_head, in_hash, "[");
375            non_eval_flag++;
376            break;
377          }
378      }      }
379    } while(0);    } while(0);
380    
# Line 323  int stack_read(stackitem** stack_head, h Line 392  int stack_read(stackitem** stack_head, h
392    return 1;    return 1;
393  }  }
394    
 /* 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);  
 }  
   
395  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
396  extern void expand(stackitem** stack_head)  extern void expand(stackitem** stack_head)
397  {  {
# Line 374  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 470  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.18  
changed lines
  Added in v.1.24

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26