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