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

Annotation of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (hide annotations)
Sat Feb 2 18:11:54 2002 UTC (22 years, 3 months ago) by masse
Branch: MAIN
Changes since 1.21: +11 -6 lines
File MIME type: text/plain
Cleanup of function "eval".

1 masse 1.1 /* printf */
2     #include <stdio.h>
3     /* EXIT_SUCCESS */
4     #include <stdlib.h>
5     /* NULL */
6     #include <stddef.h>
7 teddy 1.3 /* dlopen, dlsym, dlerror */
8 masse 1.1 #include <dlfcn.h>
9 teddy 1.18 /* assert */
10     #include <assert.h>
11 masse 1.1
12     #define HASHTBLSIZE 65536
13    
14     typedef struct stack_item
15     {
16 masse 1.16 enum {
17     value, /* Integer */
18 teddy 1.18 string,
19     ref, /* Reference (to an element in the
20     hash table) */
21 masse 1.16 func, /* Function pointer */
22     symbol,
23     list
24 teddy 1.18 } type; /* Type of stack element */
25    
26 masse 1.1 union {
27 masse 1.16 void* ptr; /* Pointer to the content */
28     int val; /* ...or an integer */
29     } content; /* Stores a pointer or an integer */
30 masse 1.1
31 masse 1.16 char* id; /* Symbol name */
32     struct stack_item* next; /* Next element */
33 masse 1.1 } stackitem;
34    
35    
36 masse 1.16 typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */
37 teddy 1.18 typedef void (*funcp)(stackitem**); /* funcp is a pointer to a
38     void function (stackitem **) */
39 masse 1.14
40 teddy 1.18 /* Initialize a newly created hash table. */
41 masse 1.1 void init_hashtbl(hashtbl out_hash)
42     {
43     long i;
44    
45     for(i= 0; i<HASHTBLSIZE; i++)
46     out_hash[i]= NULL;
47     }
48    
49 masse 1.14 /* Returns a pointer to an element in the hash table. */
50 masse 1.1 stackitem** hash(hashtbl in_hashtbl, const char* in_string)
51     {
52     long i= 0;
53     unsigned long out_hash= 0;
54 teddy 1.18 char key= '\0';
55 masse 1.1 stackitem** position;
56    
57 masse 1.16 while(1){ /* Hash in_string */
58 masse 1.1 key= in_string[i++];
59     if(key=='\0')
60     break;
61     out_hash= out_hash*32+key;
62     }
63    
64     out_hash= out_hash%HASHTBLSIZE;
65     position= &(in_hashtbl[out_hash]);
66    
67 teddy 1.18 while(position != NULL){
68     if(*position==NULL) /* If empty */
69 masse 1.1 return position;
70    
71 teddy 1.18 if(strcmp(in_string, (*position)->id)==0) /* If match */
72 masse 1.1 return position;
73    
74 masse 1.16 position= &((*position)->next); /* Try next */
75 masse 1.1 }
76 teddy 1.18 return NULL; /* end of list reached without finding
77     an empty position */
78 masse 1.1 }
79    
80 masse 1.14 /* Generic push function. */
81 masse 1.1 int push(stackitem** stack_head, stackitem* in_item)
82     {
83     in_item->next= *stack_head;
84     *stack_head= in_item;
85     return 1;
86     }
87    
88 masse 1.14 /* Push a value on the stack. */
89 masse 1.1 int push_val(stackitem** stack_head, int in_val)
90     {
91     stackitem* new_item= malloc(sizeof(stackitem));
92 teddy 1.18 assert(new_item != NULL);
93 masse 1.1 new_item->content.val= in_val;
94     new_item->type= value;
95    
96     push(stack_head, new_item);
97     return 1;
98     }
99    
100 masse 1.14 /* Copy a string onto the stack. */
101 masse 1.1 int push_cstring(stackitem** stack_head, const char* in_string)
102     {
103     stackitem* new_item= malloc(sizeof(stackitem));
104     new_item->content.ptr= malloc(strlen(in_string)+1);
105     strcpy(new_item->content.ptr, in_string);
106     new_item->type= string;
107    
108     push(stack_head, new_item);
109     return 1;
110     }
111    
112 masse 1.14 /* Create a new hash entry. */
113 masse 1.1 int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id)
114     {
115     in_item->id= malloc(strlen(id)+1);
116    
117     strcpy(in_item->id, id);
118     push(hash(in_hashtbl, id), in_item);
119    
120     return 1;
121     }
122    
123 teddy 1.18 /* Define a new function in the hash table. */
124 masse 1.1 void def_func(hashtbl in_hashtbl, funcp in_func, const char* id)
125     {
126     stackitem* temp= malloc(sizeof(stackitem));
127    
128     temp->type= func;
129     temp->content.ptr= in_func;
130    
131     mk_hashentry(in_hashtbl, temp, id);
132     }
133    
134 masse 1.14 /* Define a new symbol in the hash table. */
135 masse 1.4 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 masse 1.14 /* Push a reference to an entry in the hash table onto the stack. */
144 masse 1.1 int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string)
145     {
146 masse 1.6 static void* handle= NULL;
147 masse 1.1 void* symbol;
148 masse 1.6
149 masse 1.1 stackitem* new_item= malloc(sizeof(stackitem));
150     new_item->content.ptr= *hash(in_hash, in_string);
151     new_item->type= ref;
152    
153 masse 1.16 if(new_item->content.ptr==NULL) { /* If hash entry empty */
154     if(handle==NULL) /* If no handle */
155 masse 1.9 handle= dlopen(NULL, RTLD_LAZY);
156 masse 1.6
157 masse 1.16 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 masse 1.4 else
161 masse 1.16 def_sym(in_hash, in_string); /* Make symbol */
162 masse 1.4
163 masse 1.16 new_item->content.ptr= *hash(in_hash, in_string); /* XXX */
164 masse 1.4 new_item->type= ref;
165 masse 1.1 }
166    
167     push(stack_head, new_item);
168     return 1;
169     }
170    
171 masse 1.17 void printerr(const char* in_string) {
172     fprintf(stderr, "Err: %s\n", in_string);
173     }
174    
175 masse 1.14 /* Discard the top element of the stack. */
176 masse 1.1 extern void toss(stackitem** stack_head)
177     {
178     stackitem* temp= *stack_head;
179    
180 masse 1.17 if((*stack_head)==NULL) {
181     printerr("Stack empty");
182 masse 1.7 return;
183 masse 1.17 }
184 masse 1.7
185     if((*stack_head)->type==string)
186     free((*stack_head)->content.ptr);
187    
188 masse 1.1 *stack_head= (*stack_head)->next;
189     free(temp);
190     }
191    
192 masse 1.14 /* Print newline. */
193 masse 1.8 extern void nl()
194     {
195     printf("\n");
196     }
197 masse 1.1
198 masse 1.14 /* Prints the top element of the stack. */
199 masse 1.11 void print_(stackitem** stack_head)
200 masse 1.8 {
201 masse 1.17 if((*stack_head)==NULL) {
202     printerr("Stack empty");
203 masse 1.8 return;
204 masse 1.17 }
205 masse 1.1
206 masse 1.8 switch((*stack_head)->type) {
207 teddy 1.2 case value:
208 masse 1.8 printf("%d", (*stack_head)->content.val);
209 teddy 1.2 break;
210     case string:
211 masse 1.21 printf("\"%s\"", (char*)(*stack_head)->content.ptr);
212 teddy 1.2 break;
213     case ref:
214 masse 1.8 printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);
215 masse 1.6 break;
216 masse 1.4 case symbol:
217 masse 1.7 default:
218 masse 1.8 printf("%p", (*stack_head)->content.ptr);
219 teddy 1.2 break;
220     }
221 masse 1.1 }
222    
223 masse 1.14 /* Prints the top element of the stack and then discards it. */
224 masse 1.8 extern void print(stackitem** stack_head)
225     {
226 masse 1.11 print_(stack_head);
227 masse 1.8 toss(stack_head);
228     }
229    
230 masse 1.14 /* Only to be called by function printstack. */
231 masse 1.8 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 masse 1.11 print_(&stack_head);
238 masse 1.8 nl();
239     }
240    
241 masse 1.14 /* Prints the stack. */
242 masse 1.1 extern void printstack(stackitem** stack_head)
243     {
244     if(*stack_head != NULL) {
245     print_st(*stack_head, 1);
246 masse 1.21 nl();
247 masse 1.17 } else {
248     printerr("Stack empty");
249 masse 1.1 }
250     }
251    
252 masse 1.14 /* If the top element is a reference, determine if it's a reference to a
253 masse 1.16 function, and if it is, toss the reference and execute the function. */
254 masse 1.1 extern void eval(stackitem** stack_head)
255     {
256     funcp in_func;
257 masse 1.22 stackitem* temp= *stack_head;
258 masse 1.1
259 masse 1.22 if(temp==NULL) {
260     printerr("Stack empty");
261 masse 1.1 return;
262 masse 1.17 }
263 masse 1.1
264 masse 1.22 while(temp->type==ref)
265     temp= temp->content.ptr;
266    
267     if(temp->type==func) {
268     in_func= (funcp)(temp->content.ptr);
269 masse 1.1 toss(stack_head);
270     (*in_func)(stack_head);
271     return;
272 masse 1.22 }
273    
274     printerr("Couldn't evaluate");
275 masse 1.1 }
276    
277 masse 1.19 /* 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 masse 1.14 /* Parse input. */
314 masse 1.1 int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)
315     {
316     char *temp, *rest;
317     int itemp;
318     size_t inlength= strlen(in_line)+1;
319     int convert= 0;
320 masse 1.20 static int non_eval_flag= 0;
321 masse 1.1
322     temp= malloc(inlength);
323     rest= malloc(inlength);
324    
325 masse 1.15 do {
326 masse 1.16 /* If string */
327 masse 1.15 if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
328     push_cstring(stack_head, temp);
329     break;
330     }
331 masse 1.16 /* If value */
332 masse 1.15 if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
333     push_val(stack_head, itemp);
334     break;
335     }
336 masse 1.16 /* Escape ';' with '\' */
337 masse 1.15 if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
338     temp[1]= '\0';
339     push_ref(stack_head, in_hash, temp);
340     break;
341     }
342 masse 1.16 /* If symbol */
343 masse 1.21 if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
344 masse 1.15 push_ref(stack_head, in_hash, temp);
345     break;
346     }
347 masse 1.19 /* If single char */
348     if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
349     if(*temp==';') {
350 masse 1.21 if(!non_eval_flag) {
351 masse 1.20 eval(stack_head); /* Evaluate top element */
352     break;
353     }
354    
355     push_ref(stack_head, in_hash, ";");
356 masse 1.19 break;
357     }
358    
359     if(*temp==']') {
360     push_ref(stack_head, in_hash, "[");
361     pack(stack_head);
362 masse 1.21 if(non_eval_flag!=0)
363     non_eval_flag--;
364 masse 1.20 break;
365     }
366    
367     if(*temp=='[') {
368     push_ref(stack_head, in_hash, "[");
369     non_eval_flag++;
370 masse 1.19 break;
371     }
372 masse 1.15 }
373     } while(0);
374    
375 masse 1.1
376     free(temp);
377    
378     if(convert<2) {
379     free(rest);
380     return 0;
381     }
382    
383     stack_read(stack_head, in_hash, rest);
384    
385     free(rest);
386     return 1;
387 masse 1.7 }
388 masse 1.1
389 masse 1.16 /* Relocate elements of the list on the stack. */
390 masse 1.8 extern void expand(stackitem** stack_head)
391 masse 1.1 {
392 masse 1.8 stackitem *temp, *new_head;
393    
394 masse 1.16 /* Is top element a list? */
395 masse 1.17 if((*stack_head)==NULL || (*stack_head)->type!=list) {
396     printerr("Stack empty or not a list");
397 masse 1.8 return;
398 masse 1.17 }
399 masse 1.8
400 masse 1.16 /* The first list element is the new stack head */
401 masse 1.8 new_head= temp= (*stack_head)->content.ptr;
402     toss(stack_head);
403    
404 masse 1.16 /* Search the end of the list */
405 masse 1.8 while(temp->next!=NULL)
406     temp= temp->next;
407    
408 masse 1.16 /* Connect the the tail of the list with the old stack head */
409 masse 1.8 temp->next= *stack_head;
410 masse 1.16 *stack_head= new_head; /* ...and voila! */
411 teddy 1.5 }
412    
413 masse 1.14 /* Swap the two top elements on the stack. */
414 masse 1.10 extern void swap(stackitem** stack_head)
415     {
416     stackitem* temp= (*stack_head);
417    
418 masse 1.17 if((*stack_head)==NULL) {
419     printerr("Stack empty");
420     return;
421     }
422    
423     if((*stack_head)->next==NULL)
424 masse 1.10 return;
425    
426     *stack_head= (*stack_head)->next;
427     temp->next= (*stack_head)->next;
428     (*stack_head)->next= temp;
429     }
430 masse 1.11
431 masse 1.14 /* Compares two elements by reference. */
432 masse 1.11 extern void eq(stackitem** stack_head)
433     {
434     void *left, *right;
435     int result;
436    
437 masse 1.17 if((*stack_head)==NULL || (*stack_head)->next==NULL) {
438     printerr("Not enough elements to compare");
439 masse 1.11 return;
440 masse 1.17 }
441 masse 1.11
442     left= (*stack_head)->content.ptr;
443 masse 1.12 swap(stack_head);
444     right= (*stack_head)->content.ptr;
445 masse 1.11 result= (left==right);
446    
447     toss(stack_head); toss(stack_head);
448     push_val(stack_head, (left==right));
449     }
450    
451 masse 1.14 /* Negates the top element on the stack. */
452 masse 1.11 extern void not(stackitem** stack_head)
453     {
454     int value;
455    
456 masse 1.17 if((*stack_head)==NULL || (*stack_head)->type!=value) {
457     printerr("Stack empty or element is not a value");
458 masse 1.11 return;
459 masse 1.17 }
460 masse 1.11
461     value= (*stack_head)->content.val;
462     toss(stack_head);
463     push_val(stack_head, !value);
464     }
465    
466 masse 1.14 /* Compares the two top elements on the stack and return 0 if they're the
467     same. */
468 masse 1.11 extern void neq(stackitem** stack_head)
469     {
470     eq(stack_head);
471     not(stack_head);
472     }
473 masse 1.12
474 masse 1.14 /* Give a symbol some content. */
475 masse 1.12 extern void def(stackitem** stack_head)
476     {
477     stackitem *temp, *value;
478    
479     if(*stack_head==NULL || (*stack_head)->next==NULL
480 masse 1.17 || (*stack_head)->type!=ref) {
481     printerr("Define what?");
482 masse 1.12 return;
483 masse 1.17 }
484 masse 1.12
485     temp= (*stack_head)->content.ptr;
486     value= (*stack_head)->next;
487     temp->content= value->content;
488     value->content.ptr=NULL;
489     temp->type= value->type;
490    
491     toss(stack_head); toss(stack_head);
492     }
493 masse 1.10
494 masse 1.14 /* Quit stack. */
495 teddy 1.5 extern void quit()
496     {
497     exit(EXIT_SUCCESS);
498 masse 1.1 }
499    
500     int main()
501     {
502     stackitem* s= NULL;
503     hashtbl myhash;
504     char in_string[100];
505    
506     init_hashtbl(myhash);
507    
508     printf("okidok\n ");
509    
510     while(fgets(in_string, 100, stdin) != NULL) {
511     stack_read(&s, myhash, in_string);
512     printf("okidok\n ");
513     }
514    
515 masse 1.10 exit(EXIT_SUCCESS);
516 masse 1.1 }
517 teddy 1.3
518     /* Local Variables: */
519     /* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */
520     /* End: */

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26