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

Diff of /stack/stack.c

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

revision 1.4 by masse, Mon Jan 7 23:35:37 2002 UTC revision 1.18 by teddy, Fri Feb 1 23:30:55 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    
14  typedef struct stack_item  typedef struct stack_item
15  {  {
16    enum {value, string, ref, func, symbol} type;    enum {
17        value,                      /* Integer */
18        string,
19        ref,                        /* Reference (to an element in the
20                                       hash table) */
21        func,                       /* Function pointer */
22        symbol,
23        list
24      } type;                       /* Type of stack element */
25    
26    union {    union {
27      void* ptr;      void* ptr;                  /* Pointer to the content */
28      int val;      int val;                    /* ...or an integer */
29    } content;    } content;                    /* Stores a pointer or an integer */
30    
31    char* id;    char* id;                     /* Symbol name */
32    struct stack_item* next;    struct stack_item* next;      /* Next element */
33  } stackitem;  } stackitem;
34    
35    
36  typedef stackitem* hashtbl[HASHTBLSIZE];  typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */
37  typedef void (*funcp)(stackitem**);  typedef void (*funcp)(stackitem**); /* funcp is a pointer to a
38                                           void function (stackitem **) */
39    
40    /* 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 33  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. */
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;
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){    while(1){                     /* Hash in_string */
58      key= in_string[i++];      key= in_string[i++];
59      if(key=='\0')      if(key=='\0')
60        break;        break;
# Line 50  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){    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)      if(strcmp(in_string, (*position)->id)==0) /* If match */
72        return position;        return position;
73    
74      position= &((*position)->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. */
81  int push(stackitem** stack_head, stackitem* in_item)  int push(stackitem** stack_head, stackitem* in_item)
82  {  {
83    in_item->next= *stack_head;    in_item->next= *stack_head;
# Line 69  int push(stackitem** stack_head, stackit Line 85  int push(stackitem** stack_head, stackit
85    return 1;    return 1;
86  }  }
87    
88    /* Push a value on the stack. */
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 79  int push_val(stackitem** stack_head, int Line 97  int push_val(stackitem** stack_head, int
97    return 1;    return 1;
98  }  }
99    
100    /* Copy a string onto the stack. */
101  int push_cstring(stackitem** stack_head, const char* in_string)  int push_cstring(stackitem** stack_head, const char* in_string)
102  {  {
103    stackitem* new_item= malloc(sizeof(stackitem));    stackitem* new_item= malloc(sizeof(stackitem));
# Line 90  int push_cstring(stackitem** stack_head, Line 109  int push_cstring(stackitem** stack_head,
109    return 1;    return 1;
110  }  }
111    
112    /* Create a new hash entry. */
113  int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id)  int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id)
114  {  {
115    in_item->id= malloc(strlen(id)+1);    in_item->id= malloc(strlen(id)+1);
# Line 100  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 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 110  void def_func(hashtbl in_hashtbl, funcp Line 131  void def_func(hashtbl in_hashtbl, funcp
131    mk_hashentry(in_hashtbl, temp, id);    mk_hashentry(in_hashtbl, temp, id);
132  }  }
133    
134    /* Define a new symbol in the hash table. */
135  void def_sym(hashtbl in_hashtbl, const char* id)  void def_sym(hashtbl in_hashtbl, const char* id)
136  {  {
137    stackitem* temp= malloc(sizeof(stackitem));    stackitem* temp= malloc(sizeof(stackitem));
138        
139    temp->type= symbol;    temp->type= symbol;
   
140    mk_hashentry(in_hashtbl, temp, id);    mk_hashentry(in_hashtbl, temp, id);
141  }  }
142    
143    /* Push a reference to an entry in the hash table onto the stack. */
144  int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string)  int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string)
145  {  {
146    void* handle;    static void* handle= NULL;
147    void* symbol;    void* symbol;
148      
149    stackitem* new_item= malloc(sizeof(stackitem));    stackitem* new_item= malloc(sizeof(stackitem));
150    new_item->content.ptr= *hash(in_hash, in_string);    new_item->content.ptr= *hash(in_hash, in_string);
151    new_item->type= ref;    new_item->type= ref;
152    
153    if(new_item->content.ptr==NULL) {    if(new_item->content.ptr==NULL) { /* If hash entry empty */
154      handle= dlopen(NULL, RTLD_LAZY);      if(handle==NULL)            /* If no handle */
155      symbol= dlsym(handle, in_string);        handle= dlopen(NULL, RTLD_LAZY);    
156      if(dlerror()==NULL)  
157        def_func(in_hash, symbol, in_string);      symbol= dlsym(handle, in_string); /* Get function pointer */
158        if(dlerror()==NULL)         /* If existing function pointer */
159          def_func(in_hash, symbol, in_string); /* Store function pointer */
160      else      else
161        def_sym(in_hash, in_string);        def_sym(in_hash, in_string); /* Make symbol */
162                
163      new_item->content.ptr= *hash(in_hash, in_string);      new_item->content.ptr= *hash(in_hash, in_string); /* XXX */
164      new_item->type= ref;      new_item->type= ref;
165    }    }
166    
# Line 144  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. */
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) {
181        printerr("Stack empty");
182        return;
183      }
184      
185      if((*stack_head)->type==string)
186        free((*stack_head)->content.ptr);
187    
188    *stack_head= (*stack_head)->next;    *stack_head= (*stack_head)->next;
189    free(temp);    free(temp);
190  }  }
191    
192    /* Print newline. */
193    extern void nl()
194    {
195      printf("\n");
196    }
197    
198  /* print_stack(stack); */  /* Prints the top element of the stack. */
199  void print_st(stackitem* stack_head, long counter)  void print_(stackitem** stack_head)
200  {  {
201    if(stack_head->next != NULL)    if((*stack_head)==NULL) {
202      print_st(stack_head->next, counter+1);      printerr("Stack empty");
203        return;
204      }
205    
206    switch(stack_head->type){    switch((*stack_head)->type) {
207    case value:    case value:
208      printf("%ld: %d\n", counter, (int)stack_head->content.val);      printf("%d", (*stack_head)->content.val);
209      break;      break;
210    case string:    case string:
211      printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);      printf("%s", (char*)(*stack_head)->content.ptr);
212      break;      break;
213    case ref:    case ref:
214    case func:      printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);
215        break;
216    case symbol:    case symbol:
217      printf("%ld: %p\n", counter, stack_head->content.ptr);    default:
218        printf("%p", (*stack_head)->content.ptr);
219      break;      break;
220    }    }
221  }  }
222    
223    /* Prints the top element of the stack and then discards it. */
224    extern void print(stackitem** stack_head)
225    {
226      print_(stack_head);
227      toss(stack_head);
228    }
229    
230    /* Only to be called by function printstack. */
231    void print_st(stackitem* stack_head, long counter)
232    {
233      if(stack_head->next != NULL)
234        print_st(stack_head->next, counter+1);
235    
236      printf("%ld: ", counter);
237      print_(&stack_head);
238      nl();
239    }
240    
241    /* Prints the stack. */
242  extern void printstack(stackitem** stack_head)  extern void printstack(stackitem** stack_head)
243  {  {
244    if(*stack_head != NULL) {    if(*stack_head != NULL) {
245      print_st(*stack_head, 1);      print_st(*stack_head, 1);
246      printf("\n");      printf("\n");
247      } else {
248        printerr("Stack empty");
249    }    }
250  }  }
251    
252    /* If the top element is a reference, determine if it's a reference to a
253       function, and if it is, toss the reference and execute the function. */
254  extern void eval(stackitem** stack_head)  extern void eval(stackitem** stack_head)
255  {  {
256    funcp in_func;    funcp in_func;
257    
258    if((*stack_head)==NULL || (*stack_head)->type!=ref)    if((*stack_head)==NULL || (*stack_head)->type!=ref) {
259        printerr("Stack empty or not a reference");
260      return;      return;
261      }
262    
263    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {
264      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;
265      toss(stack_head);      toss(stack_head);
266      (*in_func)(stack_head);      (*in_func)(stack_head);
267      return;      return;
268    }    } else
269        printerr("Not a function");
270  }  }
271    
272    /* Parse input. */
273  int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)  int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)
274  {  {
275    char *temp, *rest;    char *temp, *rest;
# Line 208  int stack_read(stackitem** stack_head, h Line 280  int stack_read(stackitem** stack_head, h
280    temp= malloc(inlength);    temp= malloc(inlength);
281    rest= malloc(inlength);    rest= malloc(inlength);
282    
283    if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest)) >= 1)    do {
284      push_cstring(stack_head, temp);      /* If string */
285    else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1)      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
286      push_val(stack_head, itemp);        push_cstring(stack_head, temp);
287    else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1)        break;
288      push_ref(stack_head, in_hash, temp);      }
289    else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1)      /* If value */
290      if(*temp==';')      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
291        eval(stack_head);        push_val(stack_head, itemp);
292          break;
293        }
294        /* Escape ';' with '\' */
295        if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
296          temp[1]= '\0';
297          push_ref(stack_head, in_hash, temp);
298          break;
299        }
300        /* If symbol */
301        if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) {
302            push_ref(stack_head, in_hash, temp);
303            break;
304        }
305        /* If ';' */
306        if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') {
307          eval(stack_head);         /* Evaluate top element */
308          break;
309        }
310      } while(0);
311    
312    
313    free(temp);    free(temp);
314    
# Line 231  int stack_read(stackitem** stack_head, h Line 323  int stack_read(stackitem** stack_head, h
323    return 1;    return 1;
324  }  }
325    
326  extern void print(stackitem** stack_head)  /* Make a list. */
327    extern void pack(stackitem** stack_head)
328  {  {
329    if((*stack_head)==NULL)    void* delimiter;
330      stackitem *iterator, *temp, *pack;
331    
332      if((*stack_head)==NULL) {
333        printerr("Stack empty");
334      return;      return;
335      }
336    
337    if((*stack_head)->type==value)    delimiter= (*stack_head)->content.ptr; /* Get delimiter */
338      printf("%d", (*stack_head)->content.val);    toss(stack_head);
339    else if((*stack_head)->type==string)  
340      printf("%s", (char*)(*stack_head)->content.ptr);    iterator= *stack_head;
341    else  
342      printf("%p", (*stack_head)->content.ptr);    /* Search for first delimiter */
343      while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)
344        iterator= iterator->next;
345    
346      /* Extract list */
347      temp= *stack_head;
348      *stack_head= iterator->next;
349      iterator->next= NULL;
350      
351      if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)
352        toss(stack_head);
353    
354      /* Push list */
355      pack= malloc(sizeof(stackitem));
356      pack->type= list;
357      pack->content.ptr= temp;
358    
359      push(stack_head, pack);
360    }
361    
362    /* Relocate elements of the list on the stack. */
363    extern void expand(stackitem** stack_head)
364    {
365      stackitem *temp, *new_head;
366    
367      /* Is top element a list? */
368      if((*stack_head)==NULL || (*stack_head)->type!=list) {
369        printerr("Stack empty or not a list");
370        return;
371      }
372    
373      /* The first list element is the new stack head */
374      new_head= temp= (*stack_head)->content.ptr;
375    toss(stack_head);    toss(stack_head);
376    
377      /* Search the end of the list */
378      while(temp->next!=NULL)
379        temp= temp->next;
380    
381      /* Connect the the tail of the list with the old stack head */
382      temp->next= *stack_head;
383      *stack_head= new_head;        /* ...and voila! */
384  }  }
385    
386  extern void nl()  /* Swap the two top elements on the stack. */
387    extern void swap(stackitem** stack_head)
388  {  {
389    printf("\n");    stackitem* temp= (*stack_head);
390      
391      if((*stack_head)==NULL) {
392        printerr("Stack empty");
393        return;
394      }
395    
396      if((*stack_head)->next==NULL)
397        return;
398    
399      *stack_head= (*stack_head)->next;
400      temp->next= (*stack_head)->next;
401      (*stack_head)->next= temp;
402    }
403    
404    /* Compares two elements by reference. */
405    extern void eq(stackitem** stack_head)
406    {
407      void *left, *right;
408      int result;
409    
410      if((*stack_head)==NULL || (*stack_head)->next==NULL) {
411        printerr("Not enough elements to compare");
412        return;
413      }
414    
415      left= (*stack_head)->content.ptr;
416      swap(stack_head);
417      right= (*stack_head)->content.ptr;
418      result= (left==right);
419      
420      toss(stack_head); toss(stack_head);
421      push_val(stack_head, (left==right));
422    }
423    
424    /* Negates the top element on the stack. */
425    extern void not(stackitem** stack_head)
426    {
427      int value;
428    
429      if((*stack_head)==NULL || (*stack_head)->type!=value) {
430        printerr("Stack empty or element is not a value");
431        return;
432      }
433    
434      value= (*stack_head)->content.val;
435      toss(stack_head);
436      push_val(stack_head, !value);
437    }
438    
439    /* Compares the two top elements on the stack and return 0 if they're the
440       same. */
441    extern void neq(stackitem** stack_head)
442    {
443      eq(stack_head);
444      not(stack_head);
445    }
446    
447    /* Give a symbol some content. */
448    extern void def(stackitem** stack_head)
449    {
450      stackitem *temp, *value;
451    
452      if(*stack_head==NULL || (*stack_head)->next==NULL
453         || (*stack_head)->type!=ref) {
454        printerr("Define what?");
455        return;
456      }
457    
458      temp= (*stack_head)->content.ptr;
459      value= (*stack_head)->next;
460      temp->content= value->content;
461      value->content.ptr=NULL;
462      temp->type= value->type;
463    
464      toss(stack_head); toss(stack_head);
465    }
466    
467    /* Quit stack. */
468    extern void quit()
469    {
470      exit(EXIT_SUCCESS);
471  }  }
472    
473  int main()  int main()
# Line 266  int main() Line 485  int main()
485      printf("okidok\n ");      printf("okidok\n ");
486    }    }
487    
488      exit(EXIT_SUCCESS);
   return EXIT_SUCCESS;  
489  }  }
490    
491  /* Local Variables: */  /* Local Variables: */

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26