1 |
/* printf */ |
2 |
#include <stdio.h> |
3 |
/* EXIT_SUCCESS */ |
4 |
#include <stdlib.h> |
5 |
/* NULL */ |
6 |
#include <stddef.h> |
7 |
/* dlopen, dlsym, dlerror */ |
8 |
#include <dlfcn.h> |
9 |
/* assert */ |
10 |
#include <assert.h> |
11 |
|
12 |
#define HASHTBLSIZE 65536 |
13 |
|
14 |
typedef struct stack_item |
15 |
{ |
16 |
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 { |
27 |
void* ptr; /* Pointer to the content */ |
28 |
int val; /* ...or an integer */ |
29 |
} content; /* Stores a pointer or an integer */ |
30 |
|
31 |
char* id; /* Symbol name */ |
32 |
struct stack_item* next; /* Next element */ |
33 |
} stackitem; |
34 |
|
35 |
|
36 |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
37 |
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) |
42 |
{ |
43 |
long i; |
44 |
|
45 |
for(i= 0; i<HASHTBLSIZE; i++) |
46 |
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) |
51 |
{ |
52 |
long i= 0; |
53 |
unsigned long out_hash= 0; |
54 |
char key= '\0'; |
55 |
stackitem** position; |
56 |
|
57 |
while(1){ /* Hash in_string */ |
58 |
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 |
while(position != NULL){ |
68 |
if(*position==NULL) /* If empty */ |
69 |
return position; |
70 |
|
71 |
if(strcmp(in_string, (*position)->id)==0) /* If match */ |
72 |
return position; |
73 |
|
74 |
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) |
82 |
{ |
83 |
in_item->next= *stack_head; |
84 |
*stack_head= in_item; |
85 |
return 1; |
86 |
} |
87 |
|
88 |
/* Push a value on the stack. */ |
89 |
int push_val(stackitem** stack_head, int in_val) |
90 |
{ |
91 |
stackitem* new_item= malloc(sizeof(stackitem)); |
92 |
assert(new_item != NULL); |
93 |
new_item->content.val= in_val; |
94 |
new_item->type= value; |
95 |
|
96 |
push(stack_head, new_item); |
97 |
return 1; |
98 |
} |
99 |
|
100 |
/* Copy a string onto the stack. */ |
101 |
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 |
/* Create a new hash entry. */ |
113 |
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 |
/* Define a new function in the hash table. */ |
124 |
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 |
/* 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) |
145 |
{ |
146 |
static void* handle= NULL; |
147 |
void* symbol; |
148 |
|
149 |
stackitem* new_item= malloc(sizeof(stackitem)); |
150 |
new_item->content.ptr= *hash(in_hash, in_string); |
151 |
new_item->type= ref; |
152 |
|
153 |
if(new_item->content.ptr==NULL) { /* If hash entry empty */ |
154 |
if(handle==NULL) /* If no handle */ |
155 |
handle= dlopen(NULL, RTLD_LAZY); |
156 |
|
157 |
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 |
161 |
def_sym(in_hash, in_string); /* Make symbol */ |
162 |
|
163 |
new_item->content.ptr= *hash(in_hash, in_string); /* XXX */ |
164 |
new_item->type= ref; |
165 |
} |
166 |
|
167 |
push(stack_head, new_item); |
168 |
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) |
177 |
{ |
178 |
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; |
189 |
free(temp); |
190 |
} |
191 |
|
192 |
/* Print newline. */ |
193 |
extern void nl() |
194 |
{ |
195 |
printf("\n"); |
196 |
} |
197 |
|
198 |
/* Prints the top element of the stack. */ |
199 |
void print_(stackitem** stack_head) |
200 |
{ |
201 |
if((*stack_head)==NULL) { |
202 |
printerr("Stack empty"); |
203 |
return; |
204 |
} |
205 |
|
206 |
switch((*stack_head)->type) { |
207 |
case value: |
208 |
printf("%d", (*stack_head)->content.val); |
209 |
break; |
210 |
case string: |
211 |
printf("\"%s\"", (char*)(*stack_head)->content.ptr); |
212 |
break; |
213 |
case ref: |
214 |
printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); |
215 |
break; |
216 |
case symbol: |
217 |
default: |
218 |
printf("%p", (*stack_head)->content.ptr); |
219 |
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) |
243 |
{ |
244 |
if(*stack_head != NULL) { |
245 |
print_st(*stack_head, 1); |
246 |
nl(); |
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) |
255 |
{ |
256 |
funcp in_func; |
257 |
|
258 |
if((*stack_head)==NULL || (*stack_head)->type!=ref) { |
259 |
printerr("Stack empty or not a reference"); |
260 |
return; |
261 |
} |
262 |
|
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 |
} else |
269 |
printerr("Not a function"); |
270 |
} |
271 |
|
272 |
/* 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 |
/* Parse input. */ |
309 |
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 |
static int non_eval_flag= 0; |
316 |
|
317 |
temp= malloc(inlength); |
318 |
rest= malloc(inlength); |
319 |
|
320 |
do { |
321 |
/* If string */ |
322 |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
323 |
push_cstring(stack_head, temp); |
324 |
break; |
325 |
} |
326 |
/* If value */ |
327 |
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
328 |
push_val(stack_head, itemp); |
329 |
break; |
330 |
} |
331 |
/* Escape ';' with '\' */ |
332 |
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 |
/* If symbol */ |
338 |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
339 |
push_ref(stack_head, in_hash, temp); |
340 |
break; |
341 |
} |
342 |
/* If single char */ |
343 |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
344 |
if(*temp==';') { |
345 |
if(!non_eval_flag) { |
346 |
eval(stack_head); /* Evaluate top element */ |
347 |
break; |
348 |
} |
349 |
|
350 |
push_ref(stack_head, in_hash, ";"); |
351 |
break; |
352 |
} |
353 |
|
354 |
if(*temp==']') { |
355 |
push_ref(stack_head, in_hash, "["); |
356 |
pack(stack_head); |
357 |
if(non_eval_flag!=0) |
358 |
non_eval_flag--; |
359 |
break; |
360 |
} |
361 |
|
362 |
if(*temp=='[') { |
363 |
push_ref(stack_head, in_hash, "["); |
364 |
non_eval_flag++; |
365 |
break; |
366 |
} |
367 |
} |
368 |
} while(0); |
369 |
|
370 |
|
371 |
free(temp); |
372 |
|
373 |
if(convert<2) { |
374 |
free(rest); |
375 |
return 0; |
376 |
} |
377 |
|
378 |
stack_read(stack_head, in_hash, rest); |
379 |
|
380 |
free(rest); |
381 |
return 1; |
382 |
} |
383 |
|
384 |
/* Relocate elements of the list on the stack. */ |
385 |
extern void expand(stackitem** stack_head) |
386 |
{ |
387 |
stackitem *temp, *new_head; |
388 |
|
389 |
/* Is top element a list? */ |
390 |
if((*stack_head)==NULL || (*stack_head)->type!=list) { |
391 |
printerr("Stack empty or not a list"); |
392 |
return; |
393 |
} |
394 |
|
395 |
/* The first list element is the new stack head */ |
396 |
new_head= temp= (*stack_head)->content.ptr; |
397 |
toss(stack_head); |
398 |
|
399 |
/* Search the end of the list */ |
400 |
while(temp->next!=NULL) |
401 |
temp= temp->next; |
402 |
|
403 |
/* Connect the the tail of the list with the old stack head */ |
404 |
temp->next= *stack_head; |
405 |
*stack_head= new_head; /* ...and voila! */ |
406 |
} |
407 |
|
408 |
/* Swap the two top elements on the stack. */ |
409 |
extern void swap(stackitem** stack_head) |
410 |
{ |
411 |
stackitem* temp= (*stack_head); |
412 |
|
413 |
if((*stack_head)==NULL) { |
414 |
printerr("Stack empty"); |
415 |
return; |
416 |
} |
417 |
|
418 |
if((*stack_head)->next==NULL) |
419 |
return; |
420 |
|
421 |
*stack_head= (*stack_head)->next; |
422 |
temp->next= (*stack_head)->next; |
423 |
(*stack_head)->next= temp; |
424 |
} |
425 |
|
426 |
/* Compares two elements by reference. */ |
427 |
extern void eq(stackitem** stack_head) |
428 |
{ |
429 |
void *left, *right; |
430 |
int result; |
431 |
|
432 |
if((*stack_head)==NULL || (*stack_head)->next==NULL) { |
433 |
printerr("Not enough elements to compare"); |
434 |
return; |
435 |
} |
436 |
|
437 |
left= (*stack_head)->content.ptr; |
438 |
swap(stack_head); |
439 |
right= (*stack_head)->content.ptr; |
440 |
result= (left==right); |
441 |
|
442 |
toss(stack_head); toss(stack_head); |
443 |
push_val(stack_head, (left==right)); |
444 |
} |
445 |
|
446 |
/* Negates the top element on the stack. */ |
447 |
extern void not(stackitem** stack_head) |
448 |
{ |
449 |
int value; |
450 |
|
451 |
if((*stack_head)==NULL || (*stack_head)->type!=value) { |
452 |
printerr("Stack empty or element is not a value"); |
453 |
return; |
454 |
} |
455 |
|
456 |
value= (*stack_head)->content.val; |
457 |
toss(stack_head); |
458 |
push_val(stack_head, !value); |
459 |
} |
460 |
|
461 |
/* Compares the two top elements on the stack and return 0 if they're the |
462 |
same. */ |
463 |
extern void neq(stackitem** stack_head) |
464 |
{ |
465 |
eq(stack_head); |
466 |
not(stack_head); |
467 |
} |
468 |
|
469 |
/* Give a symbol some content. */ |
470 |
extern void def(stackitem** stack_head) |
471 |
{ |
472 |
stackitem *temp, *value; |
473 |
|
474 |
if(*stack_head==NULL || (*stack_head)->next==NULL |
475 |
|| (*stack_head)->type!=ref) { |
476 |
printerr("Define what?"); |
477 |
return; |
478 |
} |
479 |
|
480 |
temp= (*stack_head)->content.ptr; |
481 |
value= (*stack_head)->next; |
482 |
temp->content= value->content; |
483 |
value->content.ptr=NULL; |
484 |
temp->type= value->type; |
485 |
|
486 |
toss(stack_head); toss(stack_head); |
487 |
} |
488 |
|
489 |
/* Quit stack. */ |
490 |
extern void quit() |
491 |
{ |
492 |
exit(EXIT_SUCCESS); |
493 |
} |
494 |
|
495 |
int main() |
496 |
{ |
497 |
stackitem* s= NULL; |
498 |
hashtbl myhash; |
499 |
char in_string[100]; |
500 |
|
501 |
init_hashtbl(myhash); |
502 |
|
503 |
printf("okidok\n "); |
504 |
|
505 |
while(fgets(in_string, 100, stdin) != NULL) { |
506 |
stack_read(&s, myhash, in_string); |
507 |
printf("okidok\n "); |
508 |
} |
509 |
|
510 |
exit(EXIT_SUCCESS); |
511 |
} |
512 |
|
513 |
/* Local Variables: */ |
514 |
/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ |
515 |
/* End: */ |