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

Diff of /stack/stack.c

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

revision 1.5 by teddy, Tue Jan 8 00:03:16 2002 UTC revision 1.15 by masse, Thu Jan 31 21:47:20 2002 UTC
# Line 11  Line 11 
11    
12  typedef struct stack_item  typedef struct stack_item
13  {  {
14    enum {value, string, ref, func, symbol} type;    enum {value, string, ref, func, symbol, list} type;
15    union {    union {
16      void* ptr;      void* ptr;
17      int val;      int val;
# Line 25  typedef struct stack_item Line 25  typedef struct stack_item
25  typedef stackitem* hashtbl[HASHTBLSIZE];  typedef stackitem* hashtbl[HASHTBLSIZE];
26  typedef void (*funcp)(stackitem**);  typedef void (*funcp)(stackitem**);
27    
28    
29    /* Initiates a newly created hash table. */
30  void init_hashtbl(hashtbl out_hash)  void init_hashtbl(hashtbl out_hash)
31  {  {
32    long i;    long i;
# Line 33  void init_hashtbl(hashtbl out_hash) Line 35  void init_hashtbl(hashtbl out_hash)
35      out_hash[i]= NULL;      out_hash[i]= NULL;
36  }  }
37    
38    /* Returns a pointer to an element in the hash table. */
39  stackitem** hash(hashtbl in_hashtbl, const char* in_string)  stackitem** hash(hashtbl in_hashtbl, const char* in_string)
40  {  {
41    long i= 0;    long i= 0;
# Line 61  stackitem** hash(hashtbl in_hashtbl, con Line 64  stackitem** hash(hashtbl in_hashtbl, con
64    }    }
65  }  }
66    
67    /* Generic push function. */
68  int push(stackitem** stack_head, stackitem* in_item)  int push(stackitem** stack_head, stackitem* in_item)
69  {  {
70    in_item->next= *stack_head;    in_item->next= *stack_head;
# Line 69  int push(stackitem** stack_head, stackit Line 72  int push(stackitem** stack_head, stackit
72    return 1;    return 1;
73  }  }
74    
75    /* Push a value on the stack. */
76  int push_val(stackitem** stack_head, int in_val)  int push_val(stackitem** stack_head, int in_val)
77  {  {
78    stackitem* new_item= malloc(sizeof(stackitem));    stackitem* new_item= malloc(sizeof(stackitem));
# Line 79  int push_val(stackitem** stack_head, int Line 83  int push_val(stackitem** stack_head, int
83    return 1;    return 1;
84  }  }
85    
86    /* Copy a string onto the stack. */
87  int push_cstring(stackitem** stack_head, const char* in_string)  int push_cstring(stackitem** stack_head, const char* in_string)
88  {  {
89    stackitem* new_item= malloc(sizeof(stackitem));    stackitem* new_item= malloc(sizeof(stackitem));
# Line 90  int push_cstring(stackitem** stack_head, Line 95  int push_cstring(stackitem** stack_head,
95    return 1;    return 1;
96  }  }
97    
98    /* Create a new hash entry. */
99  int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id)  int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id)
100  {  {
101    in_item->id= malloc(strlen(id)+1);    in_item->id= malloc(strlen(id)+1);
# Line 100  int mk_hashentry(hashtbl in_hashtbl, sta Line 106  int mk_hashentry(hashtbl in_hashtbl, sta
106    return 1;    return 1;
107  }  }
108    
109    /* Define a function a new function in the hash table. */
110  void def_func(hashtbl in_hashtbl, funcp in_func, const char* id)  void def_func(hashtbl in_hashtbl, funcp in_func, const char* id)
111  {  {
112    stackitem* temp= malloc(sizeof(stackitem));    stackitem* temp= malloc(sizeof(stackitem));
# Line 110  void def_func(hashtbl in_hashtbl, funcp Line 117  void def_func(hashtbl in_hashtbl, funcp
117    mk_hashentry(in_hashtbl, temp, id);    mk_hashentry(in_hashtbl, temp, id);
118  }  }
119    
120    /* Define a new symbol in the hash table. */
121  void def_sym(hashtbl in_hashtbl, const char* id)  void def_sym(hashtbl in_hashtbl, const char* id)
122  {  {
123    stackitem* temp= malloc(sizeof(stackitem));    stackitem* temp= malloc(sizeof(stackitem));
# Line 119  void def_sym(hashtbl in_hashtbl, const c Line 127  void def_sym(hashtbl in_hashtbl, const c
127    mk_hashentry(in_hashtbl, temp, id);    mk_hashentry(in_hashtbl, temp, id);
128  }  }
129    
130    /* Push a reference to an entry in the hash table onto the stack. */
131  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)
132  {  {
133    void* handle;    static void* handle= NULL;
134    void* symbol;    void* symbol;
135      
136    stackitem* new_item= malloc(sizeof(stackitem));    stackitem* new_item= malloc(sizeof(stackitem));
137    new_item->content.ptr= *hash(in_hash, in_string);    new_item->content.ptr= *hash(in_hash, in_string);
138    new_item->type= ref;    new_item->type= ref;
139    
140    if(new_item->content.ptr==NULL) {    if(new_item->content.ptr==NULL) {
141      handle= dlopen(NULL, RTLD_LAZY);      if(handle==NULL)
142          handle= dlopen(NULL, RTLD_LAZY);    
143    
144      symbol= dlsym(handle, in_string);      symbol= dlsym(handle, in_string);
145      if(dlerror()==NULL)      if(dlerror()==NULL)
146        def_func(in_hash, symbol, in_string);        def_func(in_hash, symbol, in_string);
# Line 144  int push_ref(stackitem** stack_head, has Line 155  int push_ref(stackitem** stack_head, has
155    return 1;    return 1;
156  }  }
157    
158    /* Discard the top element of the stack. */
159  extern void toss(stackitem** stack_head)  extern void toss(stackitem** stack_head)
160  {  {
161    stackitem* temp= *stack_head;    stackitem* temp= *stack_head;
162    
163      if((*stack_head)==NULL)
164        return;
165      
166      if((*stack_head)->type==string)
167        free((*stack_head)->content.ptr);
168    
169    *stack_head= (*stack_head)->next;    *stack_head= (*stack_head)->next;
170    free(temp);    free(temp);
171  }  }
172    
173    /* Print newline. */
174    extern void nl()
175    {
176      printf("\n");
177    }
178    
179  /* print_stack(stack); */  /* Prints the top element of the stack. */
180  void print_st(stackitem* stack_head, long counter)  void print_(stackitem** stack_head)
181  {  {
182    if(stack_head->next != NULL)    if((*stack_head)==NULL)
183      print_st(stack_head->next, counter+1);      return;
184    
185    switch(stack_head->type){    switch((*stack_head)->type) {
186    case value:    case value:
187      printf("%ld: %d\n", counter, (int)stack_head->content.val);      printf("%d", (*stack_head)->content.val);
188      break;      break;
189    case string:    case string:
190      printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);      printf("%s", (char*)(*stack_head)->content.ptr);
191      break;      break;
192    case ref:    case ref:
193    case func:      printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);
194        break;
195    case symbol:    case symbol:
196      printf("%ld: %p\n", counter, stack_head->content.ptr);    default:
197        printf("%p", (*stack_head)->content.ptr);
198      break;      break;
199    }    }
200  }  }
201    
202    /* Prints the top element of the stack and then discards it. */
203    extern void print(stackitem** stack_head)
204    {
205      print_(stack_head);
206      toss(stack_head);
207    }
208    
209    /* Only to be called by function printstack. */
210    void print_st(stackitem* stack_head, long counter)
211    {
212      if(stack_head->next != NULL)
213        print_st(stack_head->next, counter+1);
214    
215      printf("%ld: ", counter);
216      print_(&stack_head);
217      nl();
218    }
219    
220    /* Prints the stack. */
221  extern void printstack(stackitem** stack_head)  extern void printstack(stackitem** stack_head)
222  {  {
223    if(*stack_head != NULL) {    if(*stack_head != NULL) {
# Line 182  extern void printstack(stackitem** stack Line 226  extern void printstack(stackitem** stack
226    }    }
227  }  }
228    
229    /* If the top element is a reference, determine if it's a reference to a
230       function, and if it is execute and pop the reference and execute the
231       function. */
232  extern void eval(stackitem** stack_head)  extern void eval(stackitem** stack_head)
233  {  {
234    funcp in_func;    funcp in_func;
# Line 198  extern void eval(stackitem** stack_head) Line 244  extern void eval(stackitem** stack_head)
244    }    }
245  }  }
246    
247    /* Parse input. */
248  int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)  int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)
249  {  {
250    char *temp, *rest;    char *temp, *rest;
# Line 208  int stack_read(stackitem** stack_head, h Line 255  int stack_read(stackitem** stack_head, h
255    temp= malloc(inlength);    temp= malloc(inlength);
256    rest= malloc(inlength);    rest= malloc(inlength);
257    
258    if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest)) >= 1)    do {
259      push_cstring(stack_head, temp);      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
260    else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1)        push_cstring(stack_head, temp);
261      push_val(stack_head, itemp);        break;
262    else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1)      }
263      push_ref(stack_head, in_hash, temp);  
264    else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1)      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
265      if(*temp==';')        push_val(stack_head, itemp);
266          break;
267        }
268    
269        if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
270          temp[1]= '\0';
271          push_ref(stack_head, in_hash, temp);
272          break;
273        }
274          
275        if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) {
276            push_ref(stack_head, in_hash, temp);
277            break;
278        }
279      
280        if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') {
281        eval(stack_head);        eval(stack_head);
282          break;
283        }
284      } while(0);
285    
286    
287    free(temp);    free(temp);
288    
# Line 231  int stack_read(stackitem** stack_head, h Line 297  int stack_read(stackitem** stack_head, h
297    return 1;    return 1;
298  }  }
299    
300  extern void print(stackitem** stack_head)  /* Make a list. */
301    extern void pack(stackitem** stack_head)
302  {  {
303      void* delimiter;
304      stackitem *iterator, *temp, *pack;
305    
306    if((*stack_head)==NULL)    if((*stack_head)==NULL)
307      return;      return;
308    
309    switch((*stack_head)->type){    delimiter= (*stack_head)->content.ptr;
310    case value:    toss(stack_head);
311      printf("%d", (*stack_head)->content.val);  
312      break;    iterator= *stack_head;
313    case string:  
314      printf("%s", (char*)(*stack_head)->content.ptr);    while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)
315      break;      iterator= iterator->next;
316    case symbol:  
317      printf("%s", (*stack_head)->id);    temp= *stack_head;
318    case ref:    *stack_head= iterator->next;
319    default:    iterator->next= NULL;
320      printf("%p", (*stack_head)->content.ptr);    
321      break;    if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)
322    }      toss(stack_head);
323    
324      pack= malloc(sizeof(stackitem));
325      pack->type= list;
326      pack->content.ptr= temp;
327    
328      push(stack_head, pack);
329    }
330    
331    /* Push elements of the list on the stack. */
332    extern void expand(stackitem** stack_head)
333    {
334      stackitem *temp, *new_head;
335    
336      if((*stack_head)==NULL || (*stack_head)->type!=list)
337        return;
338    
339      new_head= temp= (*stack_head)->content.ptr;
340    toss(stack_head);    toss(stack_head);
341    
342      while(temp->next!=NULL)
343        temp= temp->next;
344    
345      temp->next= *stack_head;
346      *stack_head= new_head;
347  }  }
348    
349  extern void nl()  /* Swap the two top elements on the stack. */
350    extern void swap(stackitem** stack_head)
351  {  {
352    printf("\n");    stackitem* temp= (*stack_head);
353      
354      if((*stack_head)==NULL || (*stack_head)->next==NULL)
355        return;
356    
357      *stack_head= (*stack_head)->next;
358      temp->next= (*stack_head)->next;
359      (*stack_head)->next= temp;
360    }
361    
362    /* Compares two elements by reference. */
363    extern void eq(stackitem** stack_head)
364    {
365      void *left, *right;
366      int result;
367    
368      if((*stack_head)==NULL || (*stack_head)->next==NULL)
369        return;
370    
371      left= (*stack_head)->content.ptr;
372      swap(stack_head);
373      right= (*stack_head)->content.ptr;
374      result= (left==right);
375      
376      toss(stack_head); toss(stack_head);
377      push_val(stack_head, (left==right));
378    }
379    
380    /* Negates the top element on the stack. */
381    extern void not(stackitem** stack_head)
382    {
383      int value;
384    
385      if((*stack_head)==NULL)
386        return;
387    
388      value= (*stack_head)->content.val;
389      toss(stack_head);
390      push_val(stack_head, !value);
391  }  }
392    
393    /* Compares the two top elements on the stack and return 0 if they're the
394       same. */
395    extern void neq(stackitem** stack_head)
396    {
397      eq(stack_head);
398      not(stack_head);
399    }
400    
401    /* Give a symbol some content. */
402    extern void def(stackitem** stack_head)
403    {
404      stackitem *temp, *value;
405    
406      if(*stack_head==NULL || (*stack_head)->next==NULL
407         || (*stack_head)->type!=ref)
408        return;
409    
410      temp= (*stack_head)->content.ptr;
411      value= (*stack_head)->next;
412      temp->content= value->content;
413      value->content.ptr=NULL;
414      temp->type= value->type;
415    
416      toss(stack_head); toss(stack_head);
417    }
418    
419    /* Quit stack. */
420  extern void quit()  extern void quit()
421  {  {
422    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
# Line 279  int main() Line 437  int main()
437      printf("okidok\n ");      printf("okidok\n ");
438    }    }
439    
440      exit(EXIT_SUCCESS);
   return EXIT_SUCCESS;  
441  }  }
442    
443  /* Local Variables: */  /* Local Variables: */

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.15

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26