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

Annotation of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (hide annotations)
Sat Feb 2 15:12:56 2002 UTC (22 years, 3 months ago) by masse
Branch: MAIN
Changes since 1.19: +14 -1 lines
File MIME type: text/plain
Now it's possible to make lists with ';' as list elements,
i.e. [ "Hey!" print; ].

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.8 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     printf("\n");
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    
258 masse 1.17 if((*stack_head)==NULL || (*stack_head)->type!=ref) {
259     printerr("Stack empty or not a reference");
260 masse 1.1 return;
261 masse 1.17 }
262 masse 1.1
263     if(((stackitem*)(*stack_head)->content.ptr)->type==func) {
264     in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;
265     toss(stack_head);
266     (*in_func)(stack_head);
267     return;
268 masse 1.17 } else
269     printerr("Not a function");
270 masse 1.1 }
271    
272 masse 1.19 /* Make a list. */
273     extern void pack(stackitem** stack_head)
274     {
275     void* delimiter;
276     stackitem *iterator, *temp, *pack;
277    
278     if((*stack_head)==NULL) {
279     printerr("Stack empty");
280     return;
281     }
282    
283     delimiter= (*stack_head)->content.ptr; /* Get delimiter */
284     toss(stack_head);
285    
286     iterator= *stack_head;
287    
288     /* Search for first delimiter */
289     while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)
290     iterator= iterator->next;
291    
292     /* Extract list */
293     temp= *stack_head;
294     *stack_head= iterator->next;
295     iterator->next= NULL;
296    
297     if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)
298     toss(stack_head);
299    
300     /* Push list */
301     pack= malloc(sizeof(stackitem));
302     pack->type= list;
303     pack->content.ptr= temp;
304    
305     push(stack_head, pack);
306     }
307    
308 masse 1.14 /* Parse input. */
309 masse 1.1 int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)
310     {
311     char *temp, *rest;
312     int itemp;
313     size_t inlength= strlen(in_line)+1;
314     int convert= 0;
315 masse 1.20 static int non_eval_flag= 0;
316 masse 1.1
317     temp= malloc(inlength);
318     rest= malloc(inlength);
319    
320 masse 1.15 do {
321 masse 1.16 /* If string */
322 masse 1.15 if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
323     push_cstring(stack_head, temp);
324     break;
325     }
326 masse 1.16 /* If value */
327 masse 1.15 if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
328     push_val(stack_head, itemp);
329     break;
330     }
331 masse 1.16 /* Escape ';' with '\' */
332 masse 1.15 if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
333     temp[1]= '\0';
334     push_ref(stack_head, in_hash, temp);
335     break;
336     }
337 masse 1.16 /* If symbol */
338 masse 1.19 if((convert= sscanf(in_line, "%[^] ;\n\r]%[^\n\r]", temp, rest))) {
339 masse 1.15 push_ref(stack_head, in_hash, temp);
340     break;
341     }
342 masse 1.19 /* If single char */
343     if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
344     if(*temp==';') {
345 masse 1.20 if(non_eval_flag) {
346     eval(stack_head); /* Evaluate top element */
347     break;
348     }
349    
350     push_ref(stack_head, in_hash, ";");
351 masse 1.19 break;
352     }
353    
354     if(*temp==']') {
355     push_ref(stack_head, in_hash, "[");
356     pack(stack_head);
357 masse 1.20 non_eval_flag--;
358     break;
359     }
360    
361     if(*temp=='[') {
362     push_ref(stack_head, in_hash, "[");
363     non_eval_flag++;
364 masse 1.19 break;
365     }
366 masse 1.15 }
367     } while(0);
368    
369 masse 1.1
370     free(temp);
371    
372     if(convert<2) {
373     free(rest);
374     return 0;
375     }
376    
377     stack_read(stack_head, in_hash, rest);
378    
379     free(rest);
380     return 1;
381 masse 1.7 }
382 masse 1.1
383 masse 1.16 /* Relocate elements of the list on the stack. */
384 masse 1.8 extern void expand(stackitem** stack_head)
385 masse 1.1 {
386 masse 1.8 stackitem *temp, *new_head;
387    
388 masse 1.16 /* Is top element a list? */
389 masse 1.17 if((*stack_head)==NULL || (*stack_head)->type!=list) {
390     printerr("Stack empty or not a list");
391 masse 1.8 return;
392 masse 1.17 }
393 masse 1.8
394 masse 1.16 /* The first list element is the new stack head */
395 masse 1.8 new_head= temp= (*stack_head)->content.ptr;
396     toss(stack_head);
397    
398 masse 1.16 /* Search the end of the list */
399 masse 1.8 while(temp->next!=NULL)
400     temp= temp->next;
401    
402 masse 1.16 /* Connect the the tail of the list with the old stack head */
403 masse 1.8 temp->next= *stack_head;
404 masse 1.16 *stack_head= new_head; /* ...and voila! */
405 teddy 1.5 }
406    
407 masse 1.14 /* Swap the two top elements on the stack. */
408 masse 1.10 extern void swap(stackitem** stack_head)
409     {
410     stackitem* temp= (*stack_head);
411    
412 masse 1.17 if((*stack_head)==NULL) {
413     printerr("Stack empty");
414     return;
415     }
416    
417     if((*stack_head)->next==NULL)
418 masse 1.10 return;
419    
420     *stack_head= (*stack_head)->next;
421     temp->next= (*stack_head)->next;
422     (*stack_head)->next= temp;
423     }
424 masse 1.11
425 masse 1.14 /* Compares two elements by reference. */
426 masse 1.11 extern void eq(stackitem** stack_head)
427     {
428     void *left, *right;
429     int result;
430    
431 masse 1.17 if((*stack_head)==NULL || (*stack_head)->next==NULL) {
432     printerr("Not enough elements to compare");
433 masse 1.11 return;
434 masse 1.17 }
435 masse 1.11
436     left= (*stack_head)->content.ptr;
437 masse 1.12 swap(stack_head);
438     right= (*stack_head)->content.ptr;
439 masse 1.11 result= (left==right);
440    
441     toss(stack_head); toss(stack_head);
442     push_val(stack_head, (left==right));
443     }
444    
445 masse 1.14 /* Negates the top element on the stack. */
446 masse 1.11 extern void not(stackitem** stack_head)
447     {
448     int value;
449    
450 masse 1.17 if((*stack_head)==NULL || (*stack_head)->type!=value) {
451     printerr("Stack empty or element is not a value");
452 masse 1.11 return;
453 masse 1.17 }
454 masse 1.11
455     value= (*stack_head)->content.val;
456     toss(stack_head);
457     push_val(stack_head, !value);
458     }
459    
460 masse 1.14 /* Compares the two top elements on the stack and return 0 if they're the
461     same. */
462 masse 1.11 extern void neq(stackitem** stack_head)
463     {
464     eq(stack_head);
465     not(stack_head);
466     }
467 masse 1.12
468 masse 1.14 /* Give a symbol some content. */
469 masse 1.12 extern void def(stackitem** stack_head)
470     {
471     stackitem *temp, *value;
472    
473     if(*stack_head==NULL || (*stack_head)->next==NULL
474 masse 1.17 || (*stack_head)->type!=ref) {
475     printerr("Define what?");
476 masse 1.12 return;
477 masse 1.17 }
478 masse 1.12
479     temp= (*stack_head)->content.ptr;
480     value= (*stack_head)->next;
481     temp->content= value->content;
482     value->content.ptr=NULL;
483     temp->type= value->type;
484    
485     toss(stack_head); toss(stack_head);
486     }
487 masse 1.10
488 masse 1.14 /* Quit stack. */
489 teddy 1.5 extern void quit()
490     {
491     exit(EXIT_SUCCESS);
492 masse 1.1 }
493    
494     int main()
495     {
496     stackitem* s= NULL;
497     hashtbl myhash;
498     char in_string[100];
499    
500     init_hashtbl(myhash);
501    
502     printf("okidok\n ");
503    
504     while(fgets(in_string, 100, stdin) != NULL) {
505     stack_read(&s, myhash, in_string);
506     printf("okidok\n ");
507     }
508    
509 masse 1.10 exit(EXIT_SUCCESS);
510 masse 1.1 }
511 teddy 1.3
512     /* Local Variables: */
513     /* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */
514     /* End: */

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26