/[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.27 by teddy, Sat Feb 2 22:22:04 2002 UTC
# Line 46  void init_hashtbl(hashtbl out_hash) Line 46  void init_hashtbl(hashtbl out_hash)
46      out_hash[i]= NULL;      out_hash[i]= NULL;
47  }  }
48    
49  /* Returns a pointer to an element in the hash table. */  /* Returns a pointer to a pointer to an element in the hash table. */
50  stackitem** hash(hashtbl in_hashtbl, const char* in_string)  stackitem** hash(hashtbl in_hashtbl, const char* in_string)
51  {  {
52    long i= 0;    long i= 0;
# 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        if(temp->type!=ref) {
212          printf("ref-> %s", temp->id);
213          return;
214        }
215      }
216    
217      switch(temp->type) {
218    case value:    case value:
219      printf("%d", (*stack_head)->content.val);      printf("%d", temp->content.val);
220      break;      break;
221    case string:    case string:
222      printf("%s", (char*)(*stack_head)->content.ptr);      printf("\"%s\"", (char*)temp->content.ptr);
     break;  
   case ref:  
     printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);  
223      break;      break;
224    case symbol:    case symbol:
225      case func:
226        printf("%s", temp->id);
227        break;
228    default:    default:
229      printf("%p", (*stack_head)->content.ptr);      printf("%p", temp->content.ptr);
230      break;      break;
231    }    }
232  }  }
# Line 243  extern void printstack(stackitem** stack Line 254  extern void printstack(stackitem** stack
254  {  {
255    if(*stack_head != NULL) {    if(*stack_head != NULL) {
256      print_st(*stack_head, 1);      print_st(*stack_head, 1);
257      printf("\n");      nl();
258    } else {    } else {
259      printerr("Stack empty");      printerr("Stack empty");
260    }    }
261  }  }
262    
263    /* Swap the two top elements on the stack. */
264    extern void swap(stackitem** stack_head)
265    {
266      stackitem* temp= (*stack_head);
267      
268      if((*stack_head)==NULL) {
269        printerr("Stack empty");
270        return;
271      }
272    
273      if((*stack_head)->next==NULL)
274        return;
275    
276      *stack_head= (*stack_head)->next;
277      temp->next= (*stack_head)->next;
278      (*stack_head)->next= temp;
279    }
280    
281    stackitem* copy(stackitem* in_item)
282    {
283      stackitem* out_item= malloc(sizeof(stackitem));
284    
285      memcpy(out_item, in_item, sizeof(stackitem));
286      out_item->next= NULL;
287    
288      return out_item;
289    }
290    
291    
292  /* If the top element is a reference, determine if it's a reference to a  /* If the top element is a reference, determine if it's a reference to a
293     function, and if it is, toss the reference and execute the function. */     function, and if it is, toss the reference and execute the function. */
294  extern void eval(stackitem** stack_head)  extern void eval(stackitem** stack_head)
295  {  {
296    funcp in_func;    funcp in_func;
297      stackitem* temp= *stack_head;
298    
299    if((*stack_head)==NULL || (*stack_head)->type!=ref) {    if(temp==NULL) {
300      printerr("Stack empty or not a reference");      printerr("Stack empty");
301      return;      return;
302    }    }
303    
304    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {    while(temp->type==ref)
305      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;      temp= temp->content.ptr;
306    
307      if(temp->type==func) {
308        in_func= (funcp)(temp->content.ptr);
309      toss(stack_head);      toss(stack_head);
310      (*in_func)(stack_head);      (*in_func)(stack_head);
311      return;      return;
312    } else    }
313      printerr("Not a function");  
314      push(stack_head, copy(temp));
315      swap(stack_head);
316      toss(stack_head);
317    }
318    
319    /* Make a list. */
320    extern void pack(stackitem** stack_head)
321    {
322      void* delimiter;
323      stackitem *iterator, *temp, *pack;
324    
325      delimiter= (*stack_head)->content.ptr; /* Get delimiter */
326      toss(stack_head);
327    
328      iterator= *stack_head;
329    
330      if(iterator==NULL || iterator->content.ptr==delimiter) {
331        temp= NULL;
332        toss(stack_head);
333      } else {
334        /* Search for first delimiter */
335        while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)
336          iterator= iterator->next;
337        
338        /* Extract list */
339        temp= *stack_head;
340        *stack_head= iterator->next;
341        iterator->next= NULL;
342        
343        if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)
344          toss(stack_head);
345      }
346    
347      /* Push list */
348      pack= malloc(sizeof(stackitem));
349      pack->type= list;
350      pack->content.ptr= temp;
351    
352      push(stack_head, pack);
353  }  }
354    
355  /* Parse input. */  /* Parse input. */
# Line 276  int stack_read(stackitem** stack_head, h Line 359  int stack_read(stackitem** stack_head, h
359    int itemp;    int itemp;
360    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
361    int convert= 0;    int convert= 0;
362      static int non_eval_flag= 0;
363    
364    temp= malloc(inlength);    temp= malloc(inlength);
365    rest= malloc(inlength);    rest= malloc(inlength);
# Line 298  int stack_read(stackitem** stack_head, h Line 382  int stack_read(stackitem** stack_head, h
382        break;        break;
383      }      }
384      /* If symbol */      /* If symbol */
385      if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
386          push_ref(stack_head, in_hash, temp);          push_ref(stack_head, in_hash, temp);
387          break;          break;
388      }      }
389      /* If ';' */      /* If single char */
390      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
391        eval(stack_head);         /* Evaluate top element */        if(*temp==';') {
392        break;          if(!non_eval_flag) {
393              eval(stack_head);             /* Evaluate top element */
394              break;
395            }
396            
397            push_ref(stack_head, in_hash, ";");
398            break;
399          }
400    
401          if(*temp==']') {
402            push_ref(stack_head, in_hash, "[");
403            pack(stack_head);
404            if(non_eval_flag!=0)
405              non_eval_flag--;
406            break;
407          }
408    
409          if(*temp=='[') {
410            push_ref(stack_head, in_hash, "[");
411            non_eval_flag++;
412            break;
413          }
414      }      }
415    } while(0);    } while(0);
416    
# Line 323  int stack_read(stackitem** stack_head, h Line 428  int stack_read(stackitem** stack_head, h
428    return 1;    return 1;
429  }  }
430    
 /* 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);  
 }  
   
431  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
432  extern void expand(stackitem** stack_head)  extern void expand(stackitem** stack_head)
433  {  {
# Line 374  extern void expand(stackitem** stack_hea Line 443  extern void expand(stackitem** stack_hea
443    new_head= temp= (*stack_head)->content.ptr;    new_head= temp= (*stack_head)->content.ptr;
444    toss(stack_head);    toss(stack_head);
445    
446      if(temp==NULL)
447        return;
448    
449    /* Search the end of the list */    /* Search the end of the list */
450    while(temp->next!=NULL)    while(temp->next!=NULL)
451      temp= temp->next;      temp= temp->next;
# Line 383  extern void expand(stackitem** stack_hea Line 455  extern void expand(stackitem** stack_hea
455    *stack_head= new_head;        /* ...and voila! */    *stack_head= new_head;        /* ...and voila! */
456  }  }
457    
 /* Swap the two top elements on the stack. */  
 extern void swap(stackitem** stack_head)  
 {  
   stackitem* temp= (*stack_head);  
     
   if((*stack_head)==NULL) {  
     printerr("Stack empty");  
     return;  
   }  
   
   if((*stack_head)->next==NULL)  
     return;  
   
   *stack_head= (*stack_head)->next;  
   temp->next= (*stack_head)->next;  
   (*stack_head)->next= temp;  
 }  
   
458  /* Compares two elements by reference. */  /* Compares two elements by reference. */
459  extern void eq(stackitem** stack_head)  extern void eq(stackitem** stack_head)
460  {  {
# Line 470  extern void quit() Line 524  extern void quit()
524    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
525  }  }
526    
527    /* Clear stack */
528    extern void clear(stackitem** stack_head)
529    {
530      while(*stack_head!=NULL)
531        toss(stack_head);
532    }
533    
534  int main()  int main()
535  {  {
536    stackitem* s= NULL;    stackitem* s= NULL;
# Line 487  int main() Line 548  int main()
548    
549    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
550  }  }
   
 /* Local Variables: */  
 /* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */  
 /* End: */  

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26