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

Diff of /stack/stack.c

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

revision 1.3 by teddy, Mon Jan 7 23:31:34 2002 UTC revision 1.23 by masse, Sat Feb 2 18:36:19 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} 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)
136    {
137      stackitem* temp= malloc(sizeof(stackitem));
138      
139      temp->type= symbol;
140      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        new_item->content.ptr= *hash(in_hash, in_string);      if(dlerror()==NULL)         /* If existing function pointer */
159        new_item->type= ref;        def_func(in_hash, symbol, in_string); /* Store function pointer */
160      }      else
161          def_sym(in_hash, in_string); /* Make symbol */
162          
163        new_item->content.ptr= *hash(in_hash, in_string); /* The new reference
164                                                             shouldn't point at
165                                                             NULL */
166        new_item->type= ref;
167    }    }
168    
169    push(stack_head, new_item);    push(stack_head, new_item);
170    return 1;    return 1;
171  }  }
172    
173    void printerr(const char* in_string) {
174      fprintf(stderr, "Err: %s\n", in_string);
175    }
176    
177    /* Discard the top element of the stack. */
178  extern void toss(stackitem** stack_head)  extern void toss(stackitem** stack_head)
179  {  {
180    stackitem* temp= *stack_head;    stackitem* temp= *stack_head;
181    
182      if((*stack_head)==NULL) {
183        printerr("Stack empty");
184        return;
185      }
186      
187      if((*stack_head)->type==string)
188        free((*stack_head)->content.ptr);
189    
190    *stack_head= (*stack_head)->next;    *stack_head= (*stack_head)->next;
191    free(temp);    free(temp);
192  }  }
193    
194    /* Print newline. */
195    extern void nl()
196    {
197      printf("\n");
198    }
199    
200    /* Prints the top element of the stack. */
201    extern void print_(stackitem** stack_head)
202    {
203      stackitem* temp= *stack_head;
204    
205  /* print_stack(stack); */    if(temp==NULL) {
206  void print_st(stackitem* stack_head, long counter)      printerr("Stack empty");
207  {      return;
208    if(stack_head->next != NULL)    }
     print_st(stack_head->next, counter+1);  
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("%ld: %d\n", counter, (int)stack_head->content.val);      printf("%d", temp->content.val);
216      break;      break;
217    case string:    case string:
218      printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);      printf("\"%s\"", (char*)temp->content.ptr);
219        break;
220      case symbol:
221        printf("%s", temp->id);
222      break;      break;
223    case ref:    default:
224    case func:      printf("%p", temp->content.ptr);
     printf("%ld: %p\n", counter, stack_head->content.ptr);  
225      break;      break;
226    }    }
227  }  }
228    
229    /* Prints the top element of the stack and then discards it. */
230    extern void print(stackitem** stack_head)
231    {
232      print_(stack_head);
233      toss(stack_head);
234    }
235    
236    /* Only to be called by function printstack. */
237    void print_st(stackitem* stack_head, long counter)
238    {
239      if(stack_head->next != NULL)
240        print_st(stack_head->next, counter+1);
241    
242      printf("%ld: ", counter);
243      print_(&stack_head);
244      nl();
245    }
246    
247    /* Prints the stack. */
248  extern void printstack(stackitem** stack_head)  extern void printstack(stackitem** stack_head)
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 {
254        printerr("Stack empty");
255    }    }
256  }  }
257    
258    /* If the top element is a reference, determine if it's a reference to a
259       function, and if it is, toss the reference and execute the function. */
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");
267      return;      return;
268      }
269    
270      while(temp->type==ref)
271        temp= temp->content.ptr;
272    
273    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {    if(temp->type==func) {
274      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;      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      }
279        
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      if((*stack_head)==NULL) {
290        printerr("Stack empty");
291        return;
292    }    }
293    
294      delimiter= (*stack_head)->content.ptr; /* Get delimiter */
295      toss(stack_head);
296    
297      iterator= *stack_head;
298    
299      /* Search for first delimiter */
300      while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)
301        iterator= iterator->next;
302    
303      /* Extract list */
304      temp= *stack_head;
305      *stack_head= iterator->next;
306      iterator->next= NULL;
307      
308      if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)
309        toss(stack_head);
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. */
320  int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)  int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)
321  {  {
322    char *temp, *rest;    char *temp, *rest;
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);
330    
331    if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest)) >= 1)    do {
332      push_cstring(stack_head, temp);      /* If string */
333    else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1)      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
334      push_val(stack_head, itemp);        push_cstring(stack_head, temp);
335    else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1)        break;
336      push_ref(stack_head, in_hash, temp);      }
337    else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1)      /* If value */
338      if(*temp==';')      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
339        eval(stack_head);        push_val(stack_head, itemp);
340          break;
341        }
342        /* Escape ';' with '\' */
343        if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
344          temp[1]= '\0';
345          push_ref(stack_head, in_hash, temp);
346          break;
347        }
348        /* If symbol */
349        if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
350            push_ref(stack_head, in_hash, temp);
351            break;
352        }
353        /* If single char */
354        if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
355          if(*temp==';') {
356            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);
380    
381    
382    free(temp);    free(temp);
383    
# Line 219  int stack_read(stackitem** stack_head, h Line 392  int stack_read(stackitem** stack_head, h
392    return 1;    return 1;
393  }  }
394    
395  extern void print(stackitem** stack_head)  /* Relocate elements of the list on the stack. */
396    extern void expand(stackitem** stack_head)
397  {  {
398    if((*stack_head)==NULL)    stackitem *temp, *new_head;
399    
400      /* Is top element a list? */
401      if((*stack_head)==NULL || (*stack_head)->type!=list) {
402        printerr("Stack empty or not a list");
403        return;
404      }
405    
406      /* The first list element is the new stack head */
407      new_head= temp= (*stack_head)->content.ptr;
408      toss(stack_head);
409    
410      /* Search the end of the list */
411      while(temp->next!=NULL)
412        temp= temp->next;
413    
414      /* Connect the the tail of the list with the old stack head */
415      temp->next= *stack_head;
416      *stack_head= new_head;        /* ...and voila! */
417    }
418    
419    /* Swap the two top elements on the stack. */
420    extern void swap(stackitem** stack_head)
421    {
422      stackitem* temp= (*stack_head);
423      
424      if((*stack_head)==NULL) {
425        printerr("Stack empty");
426      return;      return;
427      }
428    
429    if((*stack_head)->type==value)    if((*stack_head)->next==NULL)
430      printf("%d", (*stack_head)->content.val);      return;
   else if((*stack_head)->type==string)  
     printf("%s", (char*)(*stack_head)->content.ptr);  
   else  
     printf("%p", (*stack_head)->content.ptr);  
431    
432      *stack_head= (*stack_head)->next;
433      temp->next= (*stack_head)->next;
434      (*stack_head)->next= temp;
435    }
436    
437    /* Compares two elements by reference. */
438    extern void eq(stackitem** stack_head)
439    {
440      void *left, *right;
441      int result;
442    
443      if((*stack_head)==NULL || (*stack_head)->next==NULL) {
444        printerr("Not enough elements to compare");
445        return;
446      }
447    
448      left= (*stack_head)->content.ptr;
449      swap(stack_head);
450      right= (*stack_head)->content.ptr;
451      result= (left==right);
452      
453      toss(stack_head); toss(stack_head);
454      push_val(stack_head, (left==right));
455    }
456    
457    /* Negates the top element on the stack. */
458    extern void not(stackitem** stack_head)
459    {
460      int value;
461    
462      if((*stack_head)==NULL || (*stack_head)->type!=value) {
463        printerr("Stack empty or element is not a value");
464        return;
465      }
466    
467      value= (*stack_head)->content.val;
468    toss(stack_head);    toss(stack_head);
469      push_val(stack_head, !value);
470  }  }
471    
472  extern void nl()  /* Compares the two top elements on the stack and return 0 if they're the
473       same. */
474    extern void neq(stackitem** stack_head)
475  {  {
476    printf("\n");    eq(stack_head);
477      not(stack_head);
478    }
479    
480    /* Give a symbol some content. */
481    extern void def(stackitem** stack_head)
482    {
483      stackitem *temp, *value;
484    
485      if(*stack_head==NULL || (*stack_head)->next==NULL
486         || (*stack_head)->type!=ref) {
487        printerr("Define what?");
488        return;
489      }
490    
491      temp= (*stack_head)->content.ptr;
492      value= (*stack_head)->next;
493      temp->content= value->content;
494      value->content.ptr=NULL;
495      temp->type= value->type;
496    
497      toss(stack_head); toss(stack_head);
498    }
499    
500    /* Quit stack. */
501    extern void quit()
502    {
503      exit(EXIT_SUCCESS);
504  }  }
505    
506  int main()  int main()
# Line 254  int main() Line 518  int main()
518      printf("okidok\n ");      printf("okidok\n ");
519    }    }
520    
521      exit(EXIT_SUCCESS);
   return EXIT_SUCCESS;  
522  }  }
523    
524  /* Local Variables: */  /* Local Variables: */

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.23

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26