64 |
out_hash= out_hash%HASHTBLSIZE; |
out_hash= out_hash%HASHTBLSIZE; |
65 |
position= &(in_hashtbl[out_hash]); |
position= &(in_hashtbl[out_hash]); |
66 |
|
|
67 |
while(position != NULL){ |
while(1){ |
68 |
if(*position==NULL) /* If empty */ |
if(*position==NULL) /* If empty */ |
69 |
return position; |
return position; |
70 |
|
|
73 |
|
|
74 |
position= &((*position)->next); /* Try next */ |
position= &((*position)->next); /* Try next */ |
75 |
} |
} |
|
return NULL; /* end of list reached without finding |
|
|
an empty position */ |
|
76 |
} |
} |
77 |
|
|
78 |
/* Generic push function. */ |
/* Generic push function. */ |
205 |
return; |
return; |
206 |
} |
} |
207 |
|
|
208 |
while(temp->type==ref) |
while(temp->type==ref) { |
209 |
temp= temp->content.ptr; |
temp= temp->content.ptr; |
210 |
|
|
211 |
|
if(temp->type!=ref) { |
212 |
|
printf("ref-> %s", temp->id); |
213 |
|
return; |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
switch(temp->type) { |
switch(temp->type) { |
218 |
case value: |
case value: |
219 |
printf("%d", temp->content.val); |
printf("%d", temp->content.val); |
222 |
printf("\"%s\"", (char*)temp->content.ptr); |
printf("\"%s\"", (char*)temp->content.ptr); |
223 |
break; |
break; |
224 |
case symbol: |
case symbol: |
225 |
|
case func: |
226 |
printf("%s", temp->id); |
printf("%s", temp->id); |
227 |
break; |
break; |
228 |
default: |
default: |
260 |
} |
} |
261 |
} |
} |
262 |
|
|
263 |
|
/* Swap the two top elements on the stack. */ |
264 |
|
extern void swap(stackitem** stack_head) |
265 |
|
{ |
266 |
|
stackitem* temp= (*stack_head); |
267 |
|
|
268 |
|
if((*stack_head)==NULL) { |
269 |
|
printerr("Stack empty"); |
270 |
|
return; |
271 |
|
} |
272 |
|
|
273 |
|
if((*stack_head)->next==NULL) |
274 |
|
return; |
275 |
|
|
276 |
|
*stack_head= (*stack_head)->next; |
277 |
|
temp->next= (*stack_head)->next; |
278 |
|
(*stack_head)->next= temp; |
279 |
|
} |
280 |
|
|
281 |
|
stackitem* copy(stackitem* in_item) |
282 |
|
{ |
283 |
|
stackitem* out_item= malloc(sizeof(stackitem)); |
284 |
|
|
285 |
|
memcpy(out_item, in_item, sizeof(stackitem)); |
286 |
|
out_item->next= NULL; |
287 |
|
|
288 |
|
return out_item; |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
/* If the top element is a reference, determine if it's a reference to a |
/* If the top element is a reference, determine if it's a reference to a |
293 |
function, and if it is, toss the reference and execute the function. */ |
function, and if it is, toss the reference and execute the function. */ |
294 |
extern void eval(stackitem** stack_head) |
extern void eval(stackitem** stack_head) |
309 |
toss(stack_head); |
toss(stack_head); |
310 |
(*in_func)(stack_head); |
(*in_func)(stack_head); |
311 |
return; |
return; |
312 |
} |
} |
313 |
|
|
314 |
printerr("Couldn't evaluate"); |
push(stack_head, copy(temp)); |
315 |
|
swap(stack_head); |
316 |
|
toss(stack_head); |
317 |
} |
} |
318 |
|
|
319 |
/* Make a list. */ |
/* Make a list. */ |
322 |
void* delimiter; |
void* delimiter; |
323 |
stackitem *iterator, *temp, *pack; |
stackitem *iterator, *temp, *pack; |
324 |
|
|
|
if((*stack_head)==NULL) { |
|
|
printerr("Stack empty"); |
|
|
return; |
|
|
} |
|
|
|
|
325 |
delimiter= (*stack_head)->content.ptr; /* Get delimiter */ |
delimiter= (*stack_head)->content.ptr; /* Get delimiter */ |
326 |
toss(stack_head); |
toss(stack_head); |
327 |
|
|
328 |
iterator= *stack_head; |
iterator= *stack_head; |
329 |
|
|
330 |
/* Search for first delimiter */ |
if(iterator==NULL || iterator->content.ptr==delimiter) { |
331 |
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
temp= NULL; |
|
iterator= iterator->next; |
|
|
|
|
|
/* Extract list */ |
|
|
temp= *stack_head; |
|
|
*stack_head= iterator->next; |
|
|
iterator->next= NULL; |
|
|
|
|
|
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
|
332 |
toss(stack_head); |
toss(stack_head); |
333 |
|
} else { |
334 |
|
/* Search for first delimiter */ |
335 |
|
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
336 |
|
iterator= iterator->next; |
337 |
|
|
338 |
|
/* Extract list */ |
339 |
|
temp= *stack_head; |
340 |
|
*stack_head= iterator->next; |
341 |
|
iterator->next= NULL; |
342 |
|
|
343 |
|
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
344 |
|
toss(stack_head); |
345 |
|
} |
346 |
|
|
347 |
/* Push list */ |
/* Push list */ |
348 |
pack= malloc(sizeof(stackitem)); |
pack= malloc(sizeof(stackitem)); |
443 |
new_head= temp= (*stack_head)->content.ptr; |
new_head= temp= (*stack_head)->content.ptr; |
444 |
toss(stack_head); |
toss(stack_head); |
445 |
|
|
446 |
|
if(temp==NULL) |
447 |
|
return; |
448 |
|
|
449 |
/* Search the end of the list */ |
/* Search the end of the list */ |
450 |
while(temp->next!=NULL) |
while(temp->next!=NULL) |
451 |
temp= temp->next; |
temp= temp->next; |
455 |
*stack_head= new_head; /* ...and voila! */ |
*stack_head= new_head; /* ...and voila! */ |
456 |
} |
} |
457 |
|
|
|
/* Swap the two top elements on the stack. */ |
|
|
extern void swap(stackitem** stack_head) |
|
|
{ |
|
|
stackitem* temp= (*stack_head); |
|
|
|
|
|
if((*stack_head)==NULL) { |
|
|
printerr("Stack empty"); |
|
|
return; |
|
|
} |
|
|
|
|
|
if((*stack_head)->next==NULL) |
|
|
return; |
|
|
|
|
|
*stack_head= (*stack_head)->next; |
|
|
temp->next= (*stack_head)->next; |
|
|
(*stack_head)->next= temp; |
|
|
} |
|
|
|
|
458 |
/* Compares two elements by reference. */ |
/* Compares two elements by reference. */ |
459 |
extern void eq(stackitem** stack_head) |
extern void eq(stackitem** stack_head) |
460 |
{ |
{ |
524 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
525 |
} |
} |
526 |
|
|
527 |
|
/* Clear stack */ |
528 |
|
extern void clear(stackitem** stack_head) |
529 |
|
{ |
530 |
|
while(*stack_head!=NULL) |
531 |
|
toss(stack_head); |
532 |
|
} |
533 |
|
|
534 |
int main() |
int main() |
535 |
{ |
{ |
536 |
stackitem* s= NULL; |
stackitem* s= NULL; |