| 6 |
#include <stddef.h> |
#include <stddef.h> |
| 7 |
/* dlopen, dlsym, dlerror */ |
/* dlopen, dlsym, dlerror */ |
| 8 |
#include <dlfcn.h> |
#include <dlfcn.h> |
| 9 |
|
/* assert */ |
| 10 |
|
#include <assert.h> |
| 11 |
|
|
| 12 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 65536 |
| 13 |
|
|
| 15 |
{ |
{ |
| 16 |
enum { |
enum { |
| 17 |
value, /* Integer */ |
value, /* Integer */ |
| 18 |
string, |
string, |
| 19 |
ref, /* Reference (to an element in the hash table) */ |
ref, /* Reference (to an element in the |
| 20 |
|
hash table) */ |
| 21 |
func, /* Function pointer */ |
func, /* Function pointer */ |
| 22 |
symbol, |
symbol, |
| 23 |
list |
list |
| 24 |
} type; /* Tells what kind of stack element */ |
} type; /* Type of stack element */ |
| 25 |
|
|
| 26 |
union { |
union { |
| 27 |
void* ptr; /* Pointer to the content */ |
void* ptr; /* Pointer to the content */ |
| 28 |
int val; /* ...or an integer */ |
int val; /* ...or an integer */ |
| 34 |
|
|
| 35 |
|
|
| 36 |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
| 37 |
typedef void (*funcp)(stackitem**); /* Function pointer declaration */ |
typedef void (*funcp)(stackitem**); /* funcp is a pointer to a |
| 38 |
|
void function (stackitem **) */ |
| 39 |
|
|
| 40 |
/* Initiates a newly created hash table. */ |
/* Initialize a newly created hash table. */ |
| 41 |
void init_hashtbl(hashtbl out_hash) |
void init_hashtbl(hashtbl out_hash) |
| 42 |
{ |
{ |
| 43 |
long i; |
long i; |
| 51 |
{ |
{ |
| 52 |
long i= 0; |
long i= 0; |
| 53 |
unsigned long out_hash= 0; |
unsigned long out_hash= 0; |
| 54 |
char key= 0; |
char key= '\0'; |
| 55 |
stackitem** position; |
stackitem** position; |
| 56 |
|
|
| 57 |
while(1){ /* Hash in_string */ |
while(1){ /* Hash in_string */ |
| 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(1){ /* Return position if empty */ |
while(position != NULL){ |
| 68 |
if(*position==NULL) |
if(*position==NULL) /* If empty */ |
| 69 |
return position; |
return position; |
| 70 |
|
|
| 71 |
if(strcmp(in_string, (*position)->id)==0) /* Return position if match */ |
if(strcmp(in_string, (*position)->id)==0) /* If match */ |
| 72 |
return position; |
return position; |
| 73 |
|
|
| 74 |
position= &((*position)->next); /* Try next */ |
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. */ |
/* Generic push function. */ |
| 89 |
int push_val(stackitem** stack_head, int in_val) |
int push_val(stackitem** stack_head, int in_val) |
| 90 |
{ |
{ |
| 91 |
stackitem* new_item= malloc(sizeof(stackitem)); |
stackitem* new_item= malloc(sizeof(stackitem)); |
| 92 |
|
assert(new_item != NULL); |
| 93 |
new_item->content.val= in_val; |
new_item->content.val= in_val; |
| 94 |
new_item->type= value; |
new_item->type= value; |
| 95 |
|
|
| 120 |
return 1; |
return 1; |
| 121 |
} |
} |
| 122 |
|
|
| 123 |
/* Define a function a new function in the hash table. */ |
/* Define a new function in the hash table. */ |
| 124 |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
| 125 |
{ |
{ |
| 126 |
stackitem* temp= malloc(sizeof(stackitem)); |
stackitem* temp= malloc(sizeof(stackitem)); |
| 168 |
return 1; |
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. */ |
/* Discard the top element of the stack. */ |
| 176 |
extern void toss(stackitem** stack_head) |
extern void toss(stackitem** stack_head) |
| 177 |
{ |
{ |
| 178 |
stackitem* temp= *stack_head; |
stackitem* temp= *stack_head; |
| 179 |
|
|
| 180 |
if((*stack_head)==NULL) |
if((*stack_head)==NULL) { |
| 181 |
|
printerr("Stack empty"); |
| 182 |
return; |
return; |
| 183 |
|
} |
| 184 |
|
|
| 185 |
if((*stack_head)->type==string) |
if((*stack_head)->type==string) |
| 186 |
free((*stack_head)->content.ptr); |
free((*stack_head)->content.ptr); |
| 198 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
| 199 |
void print_(stackitem** stack_head) |
void print_(stackitem** stack_head) |
| 200 |
{ |
{ |
| 201 |
if((*stack_head)==NULL) |
if((*stack_head)==NULL) { |
| 202 |
|
printerr("Stack empty"); |
| 203 |
return; |
return; |
| 204 |
|
} |
| 205 |
|
|
| 206 |
switch((*stack_head)->type) { |
switch((*stack_head)->type) { |
| 207 |
case value: |
case value: |
| 244 |
if(*stack_head != NULL) { |
if(*stack_head != NULL) { |
| 245 |
print_st(*stack_head, 1); |
print_st(*stack_head, 1); |
| 246 |
printf("\n"); |
printf("\n"); |
| 247 |
|
} else { |
| 248 |
|
printerr("Stack empty"); |
| 249 |
} |
} |
| 250 |
} |
} |
| 251 |
|
|
| 255 |
{ |
{ |
| 256 |
funcp in_func; |
funcp in_func; |
| 257 |
|
|
| 258 |
if((*stack_head)==NULL || (*stack_head)->type!=ref) |
if((*stack_head)==NULL || (*stack_head)->type!=ref) { |
| 259 |
|
printerr("Stack empty or not a reference"); |
| 260 |
return; |
return; |
| 261 |
|
} |
| 262 |
|
|
| 263 |
if(((stackitem*)(*stack_head)->content.ptr)->type==func) { |
if(((stackitem*)(*stack_head)->content.ptr)->type==func) { |
| 264 |
in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr; |
in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr; |
| 265 |
toss(stack_head); |
toss(stack_head); |
| 266 |
(*in_func)(stack_head); |
(*in_func)(stack_head); |
| 267 |
return; |
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. */ |
/* Parse input. */ |
| 334 |
break; |
break; |
| 335 |
} |
} |
| 336 |
/* If symbol */ |
/* If symbol */ |
| 337 |
if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%[^] ;\n\r]%[^\n\r]", temp, rest))) { |
| 338 |
push_ref(stack_head, in_hash, temp); |
push_ref(stack_head, in_hash, temp); |
| 339 |
break; |
break; |
| 340 |
} |
} |
| 341 |
/* If ';' */ |
/* If single char */ |
| 342 |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') { |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
| 343 |
eval(stack_head); /* Evaluate top element */ |
if(*temp==';') { |
| 344 |
break; |
eval(stack_head); /* Evaluate top element */ |
| 345 |
|
break; |
| 346 |
|
} |
| 347 |
|
|
| 348 |
|
if(*temp==']') { |
| 349 |
|
push_ref(stack_head, in_hash, "["); |
| 350 |
|
pack(stack_head); |
| 351 |
|
break; |
| 352 |
|
} |
| 353 |
} |
} |
| 354 |
} while(0); |
} while(0); |
| 355 |
|
|
| 367 |
return 1; |
return 1; |
| 368 |
} |
} |
| 369 |
|
|
|
/* Make a list. */ |
|
|
extern void pack(stackitem** stack_head) |
|
|
{ |
|
|
void* delimiter; |
|
|
stackitem *iterator, *temp, *pack; |
|
|
|
|
|
if((*stack_head)==NULL) /* No delimiter */ |
|
|
return; |
|
|
|
|
|
delimiter= (*stack_head)->content.ptr; /* Get delimiter */ |
|
|
toss(stack_head); |
|
|
|
|
|
iterator= *stack_head; |
|
|
|
|
|
/* Search for first delimiter */ |
|
|
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
|
|
iterator= iterator->next; |
|
|
|
|
|
/* Extract list */ |
|
|
temp= *stack_head; |
|
|
*stack_head= iterator->next; |
|
|
iterator->next= NULL; |
|
|
|
|
|
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
|
|
toss(stack_head); |
|
|
|
|
|
/* Push list */ |
|
|
pack= malloc(sizeof(stackitem)); |
|
|
pack->type= list; |
|
|
pack->content.ptr= temp; |
|
|
|
|
|
push(stack_head, pack); |
|
|
} |
|
|
|
|
| 370 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
| 371 |
extern void expand(stackitem** stack_head) |
extern void expand(stackitem** stack_head) |
| 372 |
{ |
{ |
| 373 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
| 374 |
|
|
| 375 |
/* Is top element a list? */ |
/* Is top element a list? */ |
| 376 |
if((*stack_head)==NULL || (*stack_head)->type!=list) |
if((*stack_head)==NULL || (*stack_head)->type!=list) { |
| 377 |
|
printerr("Stack empty or not a list"); |
| 378 |
return; |
return; |
| 379 |
|
} |
| 380 |
|
|
| 381 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
| 382 |
new_head= temp= (*stack_head)->content.ptr; |
new_head= temp= (*stack_head)->content.ptr; |
| 396 |
{ |
{ |
| 397 |
stackitem* temp= (*stack_head); |
stackitem* temp= (*stack_head); |
| 398 |
|
|
| 399 |
if((*stack_head)==NULL || (*stack_head)->next==NULL) |
if((*stack_head)==NULL) { |
| 400 |
|
printerr("Stack empty"); |
| 401 |
|
return; |
| 402 |
|
} |
| 403 |
|
|
| 404 |
|
if((*stack_head)->next==NULL) |
| 405 |
return; |
return; |
| 406 |
|
|
| 407 |
*stack_head= (*stack_head)->next; |
*stack_head= (*stack_head)->next; |
| 415 |
void *left, *right; |
void *left, *right; |
| 416 |
int result; |
int result; |
| 417 |
|
|
| 418 |
if((*stack_head)==NULL || (*stack_head)->next==NULL) |
if((*stack_head)==NULL || (*stack_head)->next==NULL) { |
| 419 |
|
printerr("Not enough elements to compare"); |
| 420 |
return; |
return; |
| 421 |
|
} |
| 422 |
|
|
| 423 |
left= (*stack_head)->content.ptr; |
left= (*stack_head)->content.ptr; |
| 424 |
swap(stack_head); |
swap(stack_head); |
| 434 |
{ |
{ |
| 435 |
int value; |
int value; |
| 436 |
|
|
| 437 |
if((*stack_head)==NULL || (*stack_head)->type!=value) |
if((*stack_head)==NULL || (*stack_head)->type!=value) { |
| 438 |
|
printerr("Stack empty or element is not a value"); |
| 439 |
return; |
return; |
| 440 |
|
} |
| 441 |
|
|
| 442 |
value= (*stack_head)->content.val; |
value= (*stack_head)->content.val; |
| 443 |
toss(stack_head); |
toss(stack_head); |
| 458 |
stackitem *temp, *value; |
stackitem *temp, *value; |
| 459 |
|
|
| 460 |
if(*stack_head==NULL || (*stack_head)->next==NULL |
if(*stack_head==NULL || (*stack_head)->next==NULL |
| 461 |
|| (*stack_head)->type!=ref) |
|| (*stack_head)->type!=ref) { |
| 462 |
|
printerr("Define what?"); |
| 463 |
return; |
return; |
| 464 |
|
} |
| 465 |
|
|
| 466 |
temp= (*stack_head)->content.ptr; |
temp= (*stack_head)->content.ptr; |
| 467 |
value= (*stack_head)->next; |
value= (*stack_head)->next; |