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.7 |
enum {value, string, ref, func, symbol, list} type; |
15 |
masse |
1.1 |
union { |
16 |
|
|
void* ptr; |
17 |
|
|
int val; |
18 |
|
|
} content; |
19 |
|
|
|
20 |
|
|
char* id; |
21 |
|
|
struct stack_item* next; |
22 |
|
|
} stackitem; |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
typedef stackitem* hashtbl[HASHTBLSIZE]; |
26 |
|
|
typedef void (*funcp)(stackitem**); |
27 |
|
|
|
28 |
|
|
void init_hashtbl(hashtbl out_hash) |
29 |
|
|
{ |
30 |
|
|
long i; |
31 |
|
|
|
32 |
|
|
for(i= 0; i<HASHTBLSIZE; i++) |
33 |
|
|
out_hash[i]= NULL; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
stackitem** hash(hashtbl in_hashtbl, const char* in_string) |
37 |
|
|
{ |
38 |
|
|
long i= 0; |
39 |
|
|
unsigned long out_hash= 0; |
40 |
|
|
char key= 0; |
41 |
|
|
stackitem** position; |
42 |
|
|
|
43 |
|
|
while(1){ |
44 |
|
|
key= in_string[i++]; |
45 |
|
|
if(key=='\0') |
46 |
|
|
break; |
47 |
|
|
out_hash= out_hash*32+key; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
out_hash= out_hash%HASHTBLSIZE; |
51 |
|
|
position= &(in_hashtbl[out_hash]); |
52 |
|
|
|
53 |
|
|
while(1){ |
54 |
|
|
if(*position==NULL) |
55 |
|
|
return position; |
56 |
|
|
|
57 |
|
|
if(strcmp(in_string, (*position)->id)==0) |
58 |
|
|
return position; |
59 |
|
|
|
60 |
|
|
position= &((*position)->next); |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
int push(stackitem** stack_head, stackitem* in_item) |
66 |
|
|
{ |
67 |
|
|
in_item->next= *stack_head; |
68 |
|
|
*stack_head= in_item; |
69 |
|
|
return 1; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
int push_val(stackitem** stack_head, int in_val) |
73 |
|
|
{ |
74 |
|
|
stackitem* new_item= malloc(sizeof(stackitem)); |
75 |
|
|
new_item->content.val= in_val; |
76 |
|
|
new_item->type= value; |
77 |
|
|
|
78 |
|
|
push(stack_head, new_item); |
79 |
|
|
return 1; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
int push_cstring(stackitem** stack_head, const char* in_string) |
83 |
|
|
{ |
84 |
|
|
stackitem* new_item= malloc(sizeof(stackitem)); |
85 |
|
|
new_item->content.ptr= malloc(strlen(in_string)+1); |
86 |
|
|
strcpy(new_item->content.ptr, in_string); |
87 |
|
|
new_item->type= string; |
88 |
|
|
|
89 |
|
|
push(stack_head, new_item); |
90 |
|
|
return 1; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id) |
94 |
|
|
{ |
95 |
|
|
in_item->id= malloc(strlen(id)+1); |
96 |
|
|
|
97 |
|
|
strcpy(in_item->id, id); |
98 |
|
|
push(hash(in_hashtbl, id), in_item); |
99 |
|
|
|
100 |
|
|
return 1; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
104 |
|
|
{ |
105 |
|
|
stackitem* temp= malloc(sizeof(stackitem)); |
106 |
|
|
|
107 |
|
|
temp->type= func; |
108 |
|
|
temp->content.ptr= in_func; |
109 |
|
|
|
110 |
|
|
mk_hashentry(in_hashtbl, temp, id); |
111 |
|
|
} |
112 |
|
|
|
113 |
masse |
1.4 |
void def_sym(hashtbl in_hashtbl, const char* id) |
114 |
|
|
{ |
115 |
|
|
stackitem* temp= malloc(sizeof(stackitem)); |
116 |
|
|
|
117 |
|
|
temp->type= symbol; |
118 |
|
|
|
119 |
|
|
mk_hashentry(in_hashtbl, temp, id); |
120 |
|
|
} |
121 |
|
|
|
122 |
masse |
1.1 |
int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string) |
123 |
|
|
{ |
124 |
masse |
1.6 |
static void* handle= NULL; |
125 |
masse |
1.1 |
void* symbol; |
126 |
masse |
1.6 |
|
127 |
masse |
1.1 |
stackitem* new_item= malloc(sizeof(stackitem)); |
128 |
|
|
new_item->content.ptr= *hash(in_hash, in_string); |
129 |
|
|
new_item->type= ref; |
130 |
|
|
|
131 |
masse |
1.6 |
if(handle==NULL) |
132 |
|
|
handle= dlopen(NULL, RTLD_LAZY); |
133 |
|
|
|
134 |
masse |
1.1 |
if(new_item->content.ptr==NULL) { |
135 |
|
|
symbol= dlsym(handle, in_string); |
136 |
masse |
1.4 |
if(dlerror()==NULL) |
137 |
masse |
1.1 |
def_func(in_hash, symbol, in_string); |
138 |
masse |
1.4 |
else |
139 |
|
|
def_sym(in_hash, in_string); |
140 |
|
|
|
141 |
|
|
new_item->content.ptr= *hash(in_hash, in_string); |
142 |
|
|
new_item->type= ref; |
143 |
masse |
1.1 |
} |
144 |
|
|
|
145 |
|
|
push(stack_head, new_item); |
146 |
|
|
return 1; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
extern void toss(stackitem** stack_head) |
150 |
|
|
{ |
151 |
|
|
stackitem* temp= *stack_head; |
152 |
|
|
|
153 |
masse |
1.7 |
if((*stack_head)==NULL) |
154 |
|
|
return; |
155 |
|
|
|
156 |
|
|
if((*stack_head)->type==string) |
157 |
|
|
free((*stack_head)->content.ptr); |
158 |
|
|
|
159 |
masse |
1.1 |
*stack_head= (*stack_head)->next; |
160 |
|
|
free(temp); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
|
164 |
|
|
/* print_stack(stack); */ |
165 |
|
|
void print_st(stackitem* stack_head, long counter) |
166 |
|
|
{ |
167 |
|
|
if(stack_head->next != NULL) |
168 |
|
|
print_st(stack_head->next, counter+1); |
169 |
|
|
|
170 |
teddy |
1.2 |
switch(stack_head->type){ |
171 |
|
|
case value: |
172 |
masse |
1.1 |
printf("%ld: %d\n", counter, (int)stack_head->content.val); |
173 |
teddy |
1.2 |
break; |
174 |
|
|
case string: |
175 |
masse |
1.1 |
printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); |
176 |
teddy |
1.2 |
break; |
177 |
|
|
case ref: |
178 |
masse |
1.6 |
printf("%ld: %s\n", counter, ((stackitem*)stack_head->content.ptr)->id); |
179 |
|
|
break; |
180 |
teddy |
1.2 |
case func: |
181 |
masse |
1.4 |
case symbol: |
182 |
masse |
1.7 |
default: |
183 |
masse |
1.1 |
printf("%ld: %p\n", counter, stack_head->content.ptr); |
184 |
teddy |
1.2 |
break; |
185 |
|
|
} |
186 |
masse |
1.1 |
} |
187 |
|
|
|
188 |
|
|
extern void printstack(stackitem** stack_head) |
189 |
|
|
{ |
190 |
|
|
if(*stack_head != NULL) { |
191 |
|
|
print_st(*stack_head, 1); |
192 |
|
|
printf("\n"); |
193 |
|
|
} |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
|
197 |
|
|
extern void eval(stackitem** stack_head) |
198 |
|
|
{ |
199 |
|
|
funcp in_func; |
200 |
|
|
|
201 |
|
|
if((*stack_head)==NULL || (*stack_head)->type!=ref) |
202 |
|
|
return; |
203 |
|
|
|
204 |
|
|
if(((stackitem*)(*stack_head)->content.ptr)->type==func) { |
205 |
|
|
in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr; |
206 |
|
|
toss(stack_head); |
207 |
|
|
(*in_func)(stack_head); |
208 |
|
|
return; |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line) |
213 |
|
|
{ |
214 |
|
|
char *temp, *rest; |
215 |
|
|
int itemp; |
216 |
|
|
size_t inlength= strlen(in_line)+1; |
217 |
|
|
int convert= 0; |
218 |
|
|
|
219 |
|
|
temp= malloc(inlength); |
220 |
|
|
rest= malloc(inlength); |
221 |
|
|
|
222 |
|
|
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest)) >= 1) |
223 |
|
|
push_cstring(stack_head, temp); |
224 |
|
|
else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1) |
225 |
|
|
push_val(stack_head, itemp); |
226 |
|
|
else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1) |
227 |
|
|
push_ref(stack_head, in_hash, temp); |
228 |
|
|
else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1) |
229 |
|
|
if(*temp==';') |
230 |
|
|
eval(stack_head); |
231 |
|
|
|
232 |
|
|
free(temp); |
233 |
|
|
|
234 |
|
|
if(convert<2) { |
235 |
|
|
free(rest); |
236 |
|
|
return 0; |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
stack_read(stack_head, in_hash, rest); |
240 |
|
|
|
241 |
|
|
free(rest); |
242 |
|
|
return 1; |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
extern void print(stackitem** stack_head) |
246 |
|
|
{ |
247 |
|
|
if((*stack_head)==NULL) |
248 |
|
|
return; |
249 |
|
|
|
250 |
teddy |
1.5 |
switch((*stack_head)->type){ |
251 |
|
|
case value: |
252 |
masse |
1.1 |
printf("%d", (*stack_head)->content.val); |
253 |
teddy |
1.5 |
break; |
254 |
|
|
case string: |
255 |
masse |
1.1 |
printf("%s", (char*)(*stack_head)->content.ptr); |
256 |
teddy |
1.5 |
break; |
257 |
masse |
1.6 |
case ref: |
258 |
|
|
printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); |
259 |
|
|
break; |
260 |
teddy |
1.5 |
case symbol: |
261 |
|
|
default: |
262 |
masse |
1.1 |
printf("%p", (*stack_head)->content.ptr); |
263 |
teddy |
1.5 |
break; |
264 |
|
|
} |
265 |
masse |
1.1 |
|
266 |
|
|
toss(stack_head); |
267 |
|
|
} |
268 |
masse |
1.7 |
|
269 |
|
|
extern void pack(stackitem** stack_head) |
270 |
|
|
{ |
271 |
|
|
void* delimiter; |
272 |
|
|
stackitem *iterator, *temp, *pack; |
273 |
|
|
|
274 |
|
|
if((*stack_head)==NULL) |
275 |
|
|
return; |
276 |
|
|
|
277 |
|
|
delimiter= (*stack_head)->content.ptr; |
278 |
|
|
toss(stack_head); |
279 |
|
|
|
280 |
|
|
iterator= *stack_head; |
281 |
|
|
|
282 |
|
|
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
283 |
|
|
iterator= iterator->next; |
284 |
|
|
|
285 |
|
|
temp= *stack_head; |
286 |
|
|
*stack_head= iterator->next; |
287 |
|
|
iterator->next= NULL; |
288 |
|
|
|
289 |
|
|
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
290 |
|
|
toss(stack_head); |
291 |
|
|
|
292 |
|
|
pack= malloc(sizeof(stackitem)); |
293 |
|
|
pack->type= list; |
294 |
|
|
pack->content.ptr= temp; |
295 |
|
|
|
296 |
|
|
push(stack_head, pack); |
297 |
|
|
} |
298 |
|
|
|
299 |
masse |
1.1 |
|
300 |
|
|
extern void nl() |
301 |
|
|
{ |
302 |
|
|
printf("\n"); |
303 |
teddy |
1.5 |
} |
304 |
|
|
|
305 |
|
|
extern void quit() |
306 |
|
|
{ |
307 |
|
|
exit(EXIT_SUCCESS); |
308 |
masse |
1.1 |
} |
309 |
|
|
|
310 |
|
|
int main() |
311 |
|
|
{ |
312 |
|
|
stackitem* s= NULL; |
313 |
|
|
hashtbl myhash; |
314 |
|
|
char in_string[100]; |
315 |
|
|
|
316 |
|
|
init_hashtbl(myhash); |
317 |
|
|
|
318 |
|
|
printf("okidok\n "); |
319 |
|
|
|
320 |
|
|
while(fgets(in_string, 100, stdin) != NULL) { |
321 |
|
|
stack_read(&s, myhash, in_string); |
322 |
|
|
printf("okidok\n "); |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
|
326 |
|
|
return EXIT_SUCCESS; |
327 |
|
|
} |
328 |
teddy |
1.3 |
|
329 |
|
|
/* Local Variables: */ |
330 |
|
|
/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ |
331 |
|
|
/* End: */ |