| 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 |
|
|
| 14 |
typedef struct stack_item |
typedef struct stack_item |
| 15 |
{ |
{ |
| 16 |
enum {value, string, ref, func, symbol, list} type; |
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 { |
union { |
| 27 |
void* ptr; |
void* ptr; /* Pointer to the content */ |
| 28 |
int val; |
int val; /* ...or an integer */ |
| 29 |
} content; |
} content; /* Stores a pointer or an integer */ |
| 30 |
|
|
| 31 |
char* id; |
char* id; /* Symbol name */ |
| 32 |
struct stack_item* next; |
struct stack_item* next; /* Next element */ |
| 33 |
} stackitem; |
} stackitem; |
| 34 |
|
|
| 35 |
|
|
| 36 |
typedef stackitem* hashtbl[HASHTBLSIZE]; |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
| 37 |
typedef void (*funcp)(stackitem**); |
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){ |
while(1){ /* Hash in_string */ |
| 58 |
key= in_string[i++]; |
key= in_string[i++]; |
| 59 |
if(key=='\0') |
if(key=='\0') |
| 60 |
break; |
break; |
| 65 |
position= &(in_hashtbl[out_hash]); |
position= &(in_hashtbl[out_hash]); |
| 66 |
|
|
| 67 |
while(1){ |
while(1){ |
| 68 |
if(*position==NULL) |
if(*position==NULL) /* If empty */ |
| 69 |
return position; |
return position; |
| 70 |
|
|
| 71 |
if(strcmp(in_string, (*position)->id)==0) |
if(strcmp(in_string, (*position)->id)==0) /* If match */ |
| 72 |
return position; |
return position; |
| 73 |
|
|
| 74 |
position= &((*position)->next); |
position= &((*position)->next); /* Try next */ |
| 75 |
} |
} |
| 76 |
} |
} |
| 77 |
|
|
| 87 |
int push_val(stackitem** stack_head, int in_val) |
int push_val(stackitem** stack_head, int in_val) |
| 88 |
{ |
{ |
| 89 |
stackitem* new_item= malloc(sizeof(stackitem)); |
stackitem* new_item= malloc(sizeof(stackitem)); |
| 90 |
|
assert(new_item != NULL); |
| 91 |
new_item->content.val= in_val; |
new_item->content.val= in_val; |
| 92 |
new_item->type= value; |
new_item->type= value; |
| 93 |
|
|
| 118 |
return 1; |
return 1; |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
/* Define a function a new function in the hash table. */ |
/* Define a new function in the hash table. */ |
| 122 |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
| 123 |
{ |
{ |
| 124 |
stackitem* temp= malloc(sizeof(stackitem)); |
stackitem* temp= malloc(sizeof(stackitem)); |
| 135 |
stackitem* temp= malloc(sizeof(stackitem)); |
stackitem* temp= malloc(sizeof(stackitem)); |
| 136 |
|
|
| 137 |
temp->type= symbol; |
temp->type= symbol; |
|
|
|
| 138 |
mk_hashentry(in_hashtbl, temp, id); |
mk_hashentry(in_hashtbl, temp, id); |
| 139 |
} |
} |
| 140 |
|
|
| 148 |
new_item->content.ptr= *hash(in_hash, in_string); |
new_item->content.ptr= *hash(in_hash, in_string); |
| 149 |
new_item->type= ref; |
new_item->type= ref; |
| 150 |
|
|
| 151 |
if(new_item->content.ptr==NULL) { |
if(new_item->content.ptr==NULL) { /* If hash entry empty */ |
| 152 |
if(handle==NULL) |
if(handle==NULL) /* If no handle */ |
| 153 |
handle= dlopen(NULL, RTLD_LAZY); |
handle= dlopen(NULL, RTLD_LAZY); |
| 154 |
|
|
| 155 |
symbol= dlsym(handle, in_string); |
symbol= dlsym(handle, in_string); /* Get function pointer */ |
| 156 |
if(dlerror()==NULL) |
if(dlerror()==NULL) /* If existing function pointer */ |
| 157 |
def_func(in_hash, symbol, in_string); |
def_func(in_hash, symbol, in_string); /* Store function pointer */ |
| 158 |
else |
else |
| 159 |
def_sym(in_hash, in_string); |
def_sym(in_hash, in_string); /* Make symbol */ |
| 160 |
|
|
| 161 |
new_item->content.ptr= *hash(in_hash, in_string); |
new_item->content.ptr= *hash(in_hash, in_string); /* The new reference |
| 162 |
|
shouldn't point at |
| 163 |
|
NULL */ |
| 164 |
new_item->type= ref; |
new_item->type= ref; |
| 165 |
} |
} |
| 166 |
|
|
| 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); |
| 196 |
} |
} |
| 197 |
|
|
| 198 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
| 199 |
void print_(stackitem** stack_head) |
extern void print_(stackitem** stack_head) |
| 200 |
{ |
{ |
| 201 |
if((*stack_head)==NULL) |
stackitem* temp= *stack_head; |
| 202 |
|
|
| 203 |
|
if(temp==NULL) { |
| 204 |
|
printerr("Stack empty"); |
| 205 |
return; |
return; |
| 206 |
|
} |
| 207 |
|
|
| 208 |
|
while(temp->type==ref) |
| 209 |
|
temp= temp->content.ptr; |
| 210 |
|
|
| 211 |
switch((*stack_head)->type) { |
switch(temp->type) { |
| 212 |
case value: |
case value: |
| 213 |
printf("%d", (*stack_head)->content.val); |
printf("%d", temp->content.val); |
| 214 |
break; |
break; |
| 215 |
case string: |
case string: |
| 216 |
printf("%s", (char*)(*stack_head)->content.ptr); |
printf("\"%s\"", (char*)temp->content.ptr); |
|
break; |
|
|
case ref: |
|
|
printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); |
|
| 217 |
break; |
break; |
| 218 |
case symbol: |
case symbol: |
| 219 |
|
case func: |
| 220 |
|
printf("%s", temp->id); |
| 221 |
|
break; |
| 222 |
default: |
default: |
| 223 |
printf("%p", (*stack_head)->content.ptr); |
printf("%p", temp->content.ptr); |
| 224 |
break; |
break; |
| 225 |
} |
} |
| 226 |
} |
} |
| 248 |
{ |
{ |
| 249 |
if(*stack_head != NULL) { |
if(*stack_head != NULL) { |
| 250 |
print_st(*stack_head, 1); |
print_st(*stack_head, 1); |
| 251 |
printf("\n"); |
nl(); |
| 252 |
|
} else { |
| 253 |
|
printerr("Stack empty"); |
| 254 |
} |
} |
| 255 |
} |
} |
| 256 |
|
|
| 257 |
/* 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 |
| 258 |
function, and if it is execute and pop the reference and execute the |
function, and if it is, toss the reference and execute the function. */ |
|
function. */ |
|
| 259 |
extern void eval(stackitem** stack_head) |
extern void eval(stackitem** stack_head) |
| 260 |
{ |
{ |
| 261 |
funcp in_func; |
funcp in_func; |
| 262 |
|
stackitem* temp= *stack_head; |
| 263 |
|
|
| 264 |
if((*stack_head)==NULL || (*stack_head)->type!=ref) |
if(temp==NULL) { |
| 265 |
|
printerr("Stack empty"); |
| 266 |
return; |
return; |
| 267 |
|
} |
| 268 |
|
|
| 269 |
|
while(temp->type==ref) |
| 270 |
|
temp= temp->content.ptr; |
| 271 |
|
|
| 272 |
if(((stackitem*)(*stack_head)->content.ptr)->type==func) { |
if(temp->type==func) { |
| 273 |
in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr; |
in_func= (funcp)(temp->content.ptr); |
| 274 |
toss(stack_head); |
toss(stack_head); |
| 275 |
(*in_func)(stack_head); |
(*in_func)(stack_head); |
| 276 |
return; |
return; |
| 277 |
|
} |
| 278 |
|
|
| 279 |
|
printerr("Couldn't evaluate"); |
| 280 |
|
} |
| 281 |
|
|
| 282 |
|
/* Make a list. */ |
| 283 |
|
extern void pack(stackitem** stack_head) |
| 284 |
|
{ |
| 285 |
|
void* delimiter; |
| 286 |
|
stackitem *iterator, *temp, *pack; |
| 287 |
|
|
| 288 |
|
delimiter= (*stack_head)->content.ptr; /* Get delimiter */ |
| 289 |
|
toss(stack_head); |
| 290 |
|
|
| 291 |
|
iterator= *stack_head; |
| 292 |
|
|
| 293 |
|
if(iterator==NULL || iterator->content.ptr==delimiter) { |
| 294 |
|
temp= NULL; |
| 295 |
|
toss(stack_head); |
| 296 |
|
} else { |
| 297 |
|
/* Search for first delimiter */ |
| 298 |
|
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
| 299 |
|
iterator= iterator->next; |
| 300 |
|
|
| 301 |
|
/* Extract list */ |
| 302 |
|
temp= *stack_head; |
| 303 |
|
*stack_head= iterator->next; |
| 304 |
|
iterator->next= NULL; |
| 305 |
|
|
| 306 |
|
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
| 307 |
|
toss(stack_head); |
| 308 |
} |
} |
| 309 |
|
|
| 310 |
|
/* Push list */ |
| 311 |
|
pack= malloc(sizeof(stackitem)); |
| 312 |
|
pack->type= list; |
| 313 |
|
pack->content.ptr= temp; |
| 314 |
|
|
| 315 |
|
push(stack_head, pack); |
| 316 |
} |
} |
| 317 |
|
|
| 318 |
/* Parse input. */ |
/* Parse input. */ |
| 322 |
int itemp; |
int itemp; |
| 323 |
size_t inlength= strlen(in_line)+1; |
size_t inlength= strlen(in_line)+1; |
| 324 |
int convert= 0; |
int convert= 0; |
| 325 |
|
static int non_eval_flag= 0; |
| 326 |
|
|
| 327 |
temp= malloc(inlength); |
temp= malloc(inlength); |
| 328 |
rest= malloc(inlength); |
rest= malloc(inlength); |
| 329 |
|
|
| 330 |
do { |
do { |
| 331 |
|
/* If string */ |
| 332 |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
| 333 |
push_cstring(stack_head, temp); |
push_cstring(stack_head, temp); |
| 334 |
break; |
break; |
| 335 |
} |
} |
| 336 |
|
/* If value */ |
| 337 |
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
| 338 |
push_val(stack_head, itemp); |
push_val(stack_head, itemp); |
| 339 |
break; |
break; |
| 340 |
} |
} |
| 341 |
|
/* Escape ';' with '\' */ |
| 342 |
if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { |
| 343 |
temp[1]= '\0'; |
temp[1]= '\0'; |
| 344 |
push_ref(stack_head, in_hash, temp); |
push_ref(stack_head, in_hash, temp); |
| 345 |
break; |
break; |
| 346 |
} |
} |
| 347 |
|
/* If symbol */ |
| 348 |
if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
| 349 |
push_ref(stack_head, in_hash, temp); |
push_ref(stack_head, in_hash, temp); |
| 350 |
break; |
break; |
| 351 |
} |
} |
| 352 |
|
/* If single char */ |
| 353 |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') { |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
| 354 |
eval(stack_head); |
if(*temp==';') { |
| 355 |
break; |
if(!non_eval_flag) { |
| 356 |
|
eval(stack_head); /* Evaluate top element */ |
| 357 |
|
break; |
| 358 |
|
} |
| 359 |
|
|
| 360 |
|
push_ref(stack_head, in_hash, ";"); |
| 361 |
|
break; |
| 362 |
|
} |
| 363 |
|
|
| 364 |
|
if(*temp==']') { |
| 365 |
|
push_ref(stack_head, in_hash, "["); |
| 366 |
|
pack(stack_head); |
| 367 |
|
if(non_eval_flag!=0) |
| 368 |
|
non_eval_flag--; |
| 369 |
|
break; |
| 370 |
|
} |
| 371 |
|
|
| 372 |
|
if(*temp=='[') { |
| 373 |
|
push_ref(stack_head, in_hash, "["); |
| 374 |
|
non_eval_flag++; |
| 375 |
|
break; |
| 376 |
|
} |
| 377 |
} |
} |
| 378 |
} while(0); |
} while(0); |
| 379 |
|
|
| 391 |
return 1; |
return 1; |
| 392 |
} |
} |
| 393 |
|
|
| 394 |
/* Make a list. */ |
/* Relocate elements of the list on the stack. */ |
|
extern void pack(stackitem** stack_head) |
|
|
{ |
|
|
void* delimiter; |
|
|
stackitem *iterator, *temp, *pack; |
|
|
|
|
|
if((*stack_head)==NULL) |
|
|
return; |
|
|
|
|
|
delimiter= (*stack_head)->content.ptr; |
|
|
toss(stack_head); |
|
|
|
|
|
iterator= *stack_head; |
|
|
|
|
|
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
|
|
iterator= iterator->next; |
|
|
|
|
|
temp= *stack_head; |
|
|
*stack_head= iterator->next; |
|
|
iterator->next= NULL; |
|
|
|
|
|
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
|
|
toss(stack_head); |
|
|
|
|
|
pack= malloc(sizeof(stackitem)); |
|
|
pack->type= list; |
|
|
pack->content.ptr= temp; |
|
|
|
|
|
push(stack_head, pack); |
|
|
} |
|
|
|
|
|
/* Push elements of the list on the stack. */ |
|
| 395 |
extern void expand(stackitem** stack_head) |
extern void expand(stackitem** stack_head) |
| 396 |
{ |
{ |
| 397 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
| 398 |
|
|
| 399 |
if((*stack_head)==NULL || (*stack_head)->type!=list) |
/* Is top element a list? */ |
| 400 |
|
if((*stack_head)==NULL || (*stack_head)->type!=list) { |
| 401 |
|
printerr("Stack empty or not a list"); |
| 402 |
return; |
return; |
| 403 |
|
} |
| 404 |
|
|
| 405 |
|
/* The first list element is the new stack head */ |
| 406 |
new_head= temp= (*stack_head)->content.ptr; |
new_head= temp= (*stack_head)->content.ptr; |
| 407 |
toss(stack_head); |
toss(stack_head); |
| 408 |
|
|
| 409 |
|
if(temp==NULL) |
| 410 |
|
return; |
| 411 |
|
|
| 412 |
|
/* Search the end of the list */ |
| 413 |
while(temp->next!=NULL) |
while(temp->next!=NULL) |
| 414 |
temp= temp->next; |
temp= temp->next; |
| 415 |
|
|
| 416 |
|
/* Connect the the tail of the list with the old stack head */ |
| 417 |
temp->next= *stack_head; |
temp->next= *stack_head; |
| 418 |
*stack_head= new_head; |
*stack_head= new_head; /* ...and voila! */ |
| 419 |
} |
} |
| 420 |
|
|
| 421 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
| 423 |
{ |
{ |
| 424 |
stackitem* temp= (*stack_head); |
stackitem* temp= (*stack_head); |
| 425 |
|
|
| 426 |
if((*stack_head)==NULL || (*stack_head)->next==NULL) |
if((*stack_head)==NULL) { |
| 427 |
|
printerr("Stack empty"); |
| 428 |
|
return; |
| 429 |
|
} |
| 430 |
|
|
| 431 |
|
if((*stack_head)->next==NULL) |
| 432 |
return; |
return; |
| 433 |
|
|
| 434 |
*stack_head= (*stack_head)->next; |
*stack_head= (*stack_head)->next; |
| 442 |
void *left, *right; |
void *left, *right; |
| 443 |
int result; |
int result; |
| 444 |
|
|
| 445 |
if((*stack_head)==NULL || (*stack_head)->next==NULL) |
if((*stack_head)==NULL || (*stack_head)->next==NULL) { |
| 446 |
|
printerr("Not enough elements to compare"); |
| 447 |
return; |
return; |
| 448 |
|
} |
| 449 |
|
|
| 450 |
left= (*stack_head)->content.ptr; |
left= (*stack_head)->content.ptr; |
| 451 |
swap(stack_head); |
swap(stack_head); |
| 461 |
{ |
{ |
| 462 |
int value; |
int value; |
| 463 |
|
|
| 464 |
if((*stack_head)==NULL) |
if((*stack_head)==NULL || (*stack_head)->type!=value) { |
| 465 |
|
printerr("Stack empty or element is not a value"); |
| 466 |
return; |
return; |
| 467 |
|
} |
| 468 |
|
|
| 469 |
value= (*stack_head)->content.val; |
value= (*stack_head)->content.val; |
| 470 |
toss(stack_head); |
toss(stack_head); |
| 485 |
stackitem *temp, *value; |
stackitem *temp, *value; |
| 486 |
|
|
| 487 |
if(*stack_head==NULL || (*stack_head)->next==NULL |
if(*stack_head==NULL || (*stack_head)->next==NULL |
| 488 |
|| (*stack_head)->type!=ref) |
|| (*stack_head)->type!=ref) { |
| 489 |
|
printerr("Define what?"); |
| 490 |
return; |
return; |
| 491 |
|
} |
| 492 |
|
|
| 493 |
temp= (*stack_head)->content.ptr; |
temp= (*stack_head)->content.ptr; |
| 494 |
value= (*stack_head)->next; |
value= (*stack_head)->next; |
| 505 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
| 506 |
} |
} |
| 507 |
|
|
| 508 |
|
/* Clear stack */ |
| 509 |
|
extern void clear(stackitem** stack_head) |
| 510 |
|
{ |
| 511 |
|
while(*stack_head!=NULL) |
| 512 |
|
toss(stack_head); |
| 513 |
|
} |
| 514 |
|
|
| 515 |
int main() |
int main() |
| 516 |
{ |
{ |
| 517 |
stackitem* s= NULL; |
stackitem* s= NULL; |