/[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.22 by masse, Sat Feb 2 18:11:54 2002 UTC
# Line 6  Line 6 
6  #include <stddef.h>  #include <stddef.h>
7  /* dlopen, dlsym, dlerror */  /* dlopen, dlsym, dlerror */
8  #include <dlfcn.h>  #include <dlfcn.h>
9    /* assert */
10    #include <assert.h>
11    
12  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 65536
13    
# Line 13  typedef struct stack_item Line 15  typedef struct stack_item
15  {  {
16    enum {    enum {
17      value,                      /* Integer */      value,                      /* Integer */
18      string,      string,
19      ref,            /* Reference (to an element in the hash table) */      ref,                        /* Reference (to an element in the
20                                       hash table) */
21      func,                       /* Function pointer */      func,                       /* Function pointer */
22      symbol,      symbol,
23      list      list
24    } type;                       /* Tells what kind of stack element */    } type;                       /* Type of stack element */
25      
26    union {    union {
27      void* ptr;                  /* Pointer to the content */      void* ptr;                  /* Pointer to the content */
28      int val;                    /* ...or an integer */      int val;                    /* ...or an integer */
# Line 31  typedef struct stack_item Line 34  typedef struct stack_item
34    
35    
36  typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */  typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */
37  typedef void (*funcp)(stackitem**); /* Function pointer declaration */  typedef void (*funcp)(stackitem**); /* funcp is a pointer to a
38                                           void function (stackitem **) */
39    
40  /* Initiates a newly created hash table. */  /* Initialize a newly created hash table. */
41  void init_hashtbl(hashtbl out_hash)  void init_hashtbl(hashtbl out_hash)
42  {  {
43    long i;    long i;
# Line 48  stackitem** hash(hashtbl in_hashtbl, con Line 51  stackitem** hash(hashtbl in_hashtbl, con
51  {  {
52    long i= 0;    long i= 0;
53    unsigned long out_hash= 0;    unsigned long out_hash= 0;
54    char key= 0;    char key= '\0';
55    stackitem** position;    stackitem** position;
56        
57    while(1){                     /* Hash in_string */    while(1){                     /* Hash in_string */
# Line 61  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(1){                     /* Return position if empty */    while(position != NULL){
68      if(*position==NULL)      if(*position==NULL)         /* If empty */
69        return position;        return position;
70            
71      if(strcmp(in_string, (*position)->id)==0) /* Return position if match */      if(strcmp(in_string, (*position)->id)==0) /* If match */
72        return position;        return position;
73    
74      position= &((*position)->next); /* Try next */      position= &((*position)->next); /* Try next */
75    }    }
76      return NULL;                  /* end of list reached without finding
77                                       an empty position */
78  }  }
79    
80  /* Generic push function. */  /* Generic push function. */
# Line 84  int push(stackitem** stack_head, stackit Line 89  int push(stackitem** stack_head, stackit
89  int push_val(stackitem** stack_head, int in_val)  int push_val(stackitem** stack_head, int in_val)
90  {  {
91    stackitem* new_item= malloc(sizeof(stackitem));    stackitem* new_item= malloc(sizeof(stackitem));
92      assert(new_item != NULL);
93    new_item->content.val= in_val;    new_item->content.val= in_val;
94    new_item->type= value;    new_item->type= value;
95    
# Line 114  int mk_hashentry(hashtbl in_hashtbl, sta Line 120  int mk_hashentry(hashtbl in_hashtbl, sta
120    return 1;    return 1;
121  }  }
122    
123  /* Define a function a new function in the hash table. */  /* Define a new function in the hash table. */
124  void def_func(hashtbl in_hashtbl, funcp in_func, const char* id)  void def_func(hashtbl in_hashtbl, funcp in_func, const char* id)
125  {  {
126    stackitem* temp= malloc(sizeof(stackitem));    stackitem* temp= malloc(sizeof(stackitem));
# Line 162  int push_ref(stackitem** stack_head, has Line 168  int push_ref(stackitem** stack_head, has
168    return 1;    return 1;
169  }  }
170    
171    void printerr(const char* in_string) {
172      fprintf(stderr, "Err: %s\n", in_string);
173    }
174    
175  /* Discard the top element of the stack. */  /* Discard the top element of the stack. */
176  extern void toss(stackitem** stack_head)  extern void toss(stackitem** stack_head)
177  {  {
178    stackitem* temp= *stack_head;    stackitem* temp= *stack_head;
179    
180    if((*stack_head)==NULL)    if((*stack_head)==NULL) {
181        printerr("Stack empty");
182      return;      return;
183      }
184        
185    if((*stack_head)->type==string)    if((*stack_head)->type==string)
186      free((*stack_head)->content.ptr);      free((*stack_head)->content.ptr);
# Line 186  extern void nl() Line 198  extern void nl()
198  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
199  void print_(stackitem** stack_head)  void print_(stackitem** stack_head)
200  {  {
201    if((*stack_head)==NULL)    if((*stack_head)==NULL) {
202        printerr("Stack empty");
203      return;      return;
204      }
205    
206    switch((*stack_head)->type) {    switch((*stack_head)->type) {
207    case value:    case value:
208      printf("%d", (*stack_head)->content.val);      printf("%d", (*stack_head)->content.val);
209      break;      break;
210    case string:    case string:
211      printf("%s", (char*)(*stack_head)->content.ptr);      printf("\"%s\"", (char*)(*stack_head)->content.ptr);
212      break;      break;
213    case ref:    case ref:
214      printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);      printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);
# Line 229  extern void printstack(stackitem** stack Line 243  extern void printstack(stackitem** stack
243  {  {
244    if(*stack_head != NULL) {    if(*stack_head != NULL) {
245      print_st(*stack_head, 1);      print_st(*stack_head, 1);
246      printf("\n");      nl();
247      } else {
248        printerr("Stack empty");
249    }    }
250  }  }
251    
# Line 238  extern void printstack(stackitem** stack Line 254  extern void printstack(stackitem** stack
254  extern void eval(stackitem** stack_head)  extern void eval(stackitem** stack_head)
255  {  {
256    funcp in_func;    funcp in_func;
257      stackitem* temp= *stack_head;
258    
259    if((*stack_head)==NULL || (*stack_head)->type!=ref)    if(temp==NULL) {
260        printerr("Stack empty");
261      return;      return;
262      }
263    
264    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {    while(temp->type==ref)
265      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;      temp= temp->content.ptr;
266    
267      if(temp->type==func) {
268        in_func= (funcp)(temp->content.ptr);
269      toss(stack_head);      toss(stack_head);
270      (*in_func)(stack_head);      (*in_func)(stack_head);
271      return;      return;
272      }
273        
274      printerr("Couldn't evaluate");
275    }
276    
277    /* Make a list. */
278    extern void pack(stackitem** stack_head)
279    {
280      void* delimiter;
281      stackitem *iterator, *temp, *pack;
282    
283      if((*stack_head)==NULL) {
284        printerr("Stack empty");
285        return;
286    }    }
287    
288      delimiter= (*stack_head)->content.ptr; /* Get delimiter */
289      toss(stack_head);
290    
291      iterator= *stack_head;
292    
293      /* Search for first delimiter */
294      while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)
295        iterator= iterator->next;
296    
297      /* Extract list */
298      temp= *stack_head;
299      *stack_head= iterator->next;
300      iterator->next= NULL;
301      
302      if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)
303        toss(stack_head);
304    
305      /* Push list */
306      pack= malloc(sizeof(stackitem));
307      pack->type= list;
308      pack->content.ptr= temp;
309    
310      push(stack_head, pack);
311  }  }
312    
313  /* Parse input. */  /* Parse input. */
# Line 257  int stack_read(stackitem** stack_head, h Line 317  int stack_read(stackitem** stack_head, h
317    int itemp;    int itemp;
318    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
319    int convert= 0;    int convert= 0;
320      static int non_eval_flag= 0;
321    
322    temp= malloc(inlength);    temp= malloc(inlength);
323    rest= malloc(inlength);    rest= malloc(inlength);
# Line 279  int stack_read(stackitem** stack_head, h Line 340  int stack_read(stackitem** stack_head, h
340        break;        break;
341      }      }
342      /* If symbol */      /* If symbol */
343      if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
344          push_ref(stack_head, in_hash, temp);          push_ref(stack_head, in_hash, temp);
345          break;          break;
346      }      }
347      /* If ';' */      /* If single char */
348      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
349        eval(stack_head);         /* Evaluate top element */        if(*temp==';') {
350        break;          if(!non_eval_flag) {
351              eval(stack_head);             /* Evaluate top element */
352              break;
353            }
354            
355            push_ref(stack_head, in_hash, ";");
356            break;
357          }
358    
359          if(*temp==']') {
360            push_ref(stack_head, in_hash, "[");
361            pack(stack_head);
362            if(non_eval_flag!=0)
363              non_eval_flag--;
364            break;
365          }
366    
367          if(*temp=='[') {
368            push_ref(stack_head, in_hash, "[");
369            non_eval_flag++;
370            break;
371          }
372      }      }
373    } while(0);    } while(0);
374    
# Line 304  int stack_read(stackitem** stack_head, h Line 386  int stack_read(stackitem** stack_head, h
386    return 1;    return 1;
387  }  }
388    
 /* Make a list. */  
 extern void pack(stackitem** stack_head)  
 {  
   void* delimiter;  
   stackitem *iterator, *temp, *pack;  
   
   if((*stack_head)==NULL)       /* No delimiter */  
     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);  
 }  
   
389  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
390  extern void expand(stackitem** stack_head)  extern void expand(stackitem** stack_head)
391  {  {
392    stackitem *temp, *new_head;    stackitem *temp, *new_head;
393    
394    /* Is top element a list? */    /* Is top element a list? */
395    if((*stack_head)==NULL || (*stack_head)->type!=list)    if((*stack_head)==NULL || (*stack_head)->type!=list) {
396        printerr("Stack empty or not a list");
397      return;      return;
398      }
399    
400    /* The first list element is the new stack head */    /* The first list element is the new stack head */
401    new_head= temp= (*stack_head)->content.ptr;    new_head= temp= (*stack_head)->content.ptr;
# Line 365  extern void swap(stackitem** stack_head) Line 415  extern void swap(stackitem** stack_head)
415  {  {
416    stackitem* temp= (*stack_head);    stackitem* temp= (*stack_head);
417        
418    if((*stack_head)==NULL || (*stack_head)->next==NULL)    if((*stack_head)==NULL) {
419        printerr("Stack empty");
420        return;
421      }
422    
423      if((*stack_head)->next==NULL)
424      return;      return;
425    
426    *stack_head= (*stack_head)->next;    *stack_head= (*stack_head)->next;
# Line 379  extern void eq(stackitem** stack_head) Line 434  extern void eq(stackitem** stack_head)
434    void *left, *right;    void *left, *right;
435    int result;    int result;
436    
437    if((*stack_head)==NULL || (*stack_head)->next==NULL)    if((*stack_head)==NULL || (*stack_head)->next==NULL) {
438        printerr("Not enough elements to compare");
439      return;      return;
440      }
441    
442    left= (*stack_head)->content.ptr;    left= (*stack_head)->content.ptr;
443    swap(stack_head);    swap(stack_head);
# Line 396  extern void not(stackitem** stack_head) Line 453  extern void not(stackitem** stack_head)
453  {  {
454    int value;    int value;
455    
456    if((*stack_head)==NULL || (*stack_head)->type!=value)    if((*stack_head)==NULL || (*stack_head)->type!=value) {
457        printerr("Stack empty or element is not a value");
458      return;      return;
459      }
460    
461    value= (*stack_head)->content.val;    value= (*stack_head)->content.val;
462    toss(stack_head);    toss(stack_head);
# Line 418  extern void def(stackitem** stack_head) Line 477  extern void def(stackitem** stack_head)
477    stackitem *temp, *value;    stackitem *temp, *value;
478    
479    if(*stack_head==NULL || (*stack_head)->next==NULL    if(*stack_head==NULL || (*stack_head)->next==NULL
480       || (*stack_head)->type!=ref)       || (*stack_head)->type!=ref) {
481        printerr("Define what?");
482      return;      return;
483      }
484    
485    temp= (*stack_head)->content.ptr;    temp= (*stack_head)->content.ptr;
486    value= (*stack_head)->next;    value= (*stack_head)->next;

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26