8 |
#include <dlfcn.h> |
#include <dlfcn.h> |
9 |
/* assert */ |
/* assert */ |
10 |
#include <assert.h> |
#include <assert.h> |
11 |
|
/* strcat */ |
12 |
|
#include <string.h> |
13 |
|
|
14 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 65536 |
15 |
|
|
59 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
60 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
61 |
int err; /* Error flag */ |
int err; /* Error flag */ |
62 |
|
int non_eval_flag; |
63 |
} environment; |
} environment; |
64 |
|
|
65 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
69 |
/* Initialize a newly created environment */ |
/* Initialize a newly created environment */ |
70 |
void init_env(environment *env) |
void init_env(environment *env) |
71 |
{ |
{ |
72 |
long i; |
int i; |
73 |
|
|
74 |
env->err=0; |
env->err= 0; |
75 |
|
env->non_eval_flag= 0; |
76 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
77 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
78 |
} |
} |
80 |
/* Returns a pointer to a pointer to an element in the hash table. */ |
/* Returns a pointer to a pointer to an element in the hash table. */ |
81 |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
82 |
{ |
{ |
83 |
long i= 0; |
int i= 0; |
84 |
unsigned long out_hash= 0; |
unsigned int out_hash= 0; |
85 |
char key= '\0'; |
char key= '\0'; |
86 |
symbol **position; |
symbol **position; |
87 |
|
|
305 |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
306 |
break; |
break; |
307 |
case symb: |
case symb: |
308 |
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
309 |
break; |
break; |
310 |
case func: |
case func: |
311 |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
354 |
nl(); |
nl(); |
355 |
} |
} |
356 |
|
|
|
|
|
|
|
|
357 |
/* Prints the stack. */ |
/* Prints the stack. */ |
358 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
359 |
{ |
{ |
369 |
{ |
{ |
370 |
stackitem *temp= env->head; |
stackitem *temp= env->head; |
371 |
|
|
372 |
if((env->head)==NULL) { |
if(env->head==NULL || env->head->next==NULL) { |
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
|
|
|
if(env->head->next==NULL) { |
|
373 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
374 |
env->err=1; |
env->err=1; |
375 |
return; |
return; |
380 |
env->head->next= temp; |
env->head->next= temp; |
381 |
} |
} |
382 |
|
|
|
stackitem* copy(stackitem* in_item) |
|
|
{ |
|
|
stackitem *out_item= malloc(sizeof(stackitem)); |
|
|
|
|
|
memcpy(out_item, in_item, sizeof(stackitem)); |
|
|
out_item->next= NULL; |
|
|
|
|
|
return out_item; |
|
|
} |
|
|
|
|
383 |
/* Recall a value from a symbol, if bound */ |
/* Recall a value from a symbol, if bound */ |
384 |
extern void rcl(environment *env) |
extern void rcl(environment *env) |
385 |
{ |
{ |
408 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(&(env->head), val); /* Return its bound value */ |
409 |
} |
} |
410 |
|
|
411 |
|
void stack_read(environment*, char*); |
412 |
|
|
413 |
/* If the top element is a symbol, determine if it's bound to a |
/* If the top element is a symbol, determine if it's bound to a |
414 |
function value, and if it is, toss the symbol and execute the |
function value, and if it is, toss the symbol and execute the |
415 |
function. */ |
function. */ |
416 |
extern void eval(environment *env) |
extern void eval(environment *env) |
417 |
{ |
{ |
418 |
funcp in_func; |
funcp in_func; |
419 |
|
value* temp_val; |
420 |
|
stackitem* iterator; |
421 |
|
char* temp_string; |
422 |
|
|
423 |
if(env->head==NULL) { |
if(env->head==NULL) { |
424 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
425 |
env->err=1; |
env->err=1; |
426 |
return; |
return; |
427 |
} |
} |
428 |
|
|
429 |
/* if it's a symbol */ |
switch(env->head->item->type) { |
430 |
if(env->head->item->type==symb) { |
/* if it's a symbol */ |
431 |
|
case symb: |
432 |
rcl(env); /* get its contents */ |
rcl(env); /* get its contents */ |
433 |
if(env->err) return; |
if(env->err) return; |
434 |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
435 |
eval(env); /* evaluate the value */ |
eval(env); /* evaluate the value */ |
436 |
return; |
return; |
437 |
} |
} |
438 |
} |
break; |
439 |
|
|
440 |
/* If it's a lone function value, run it */ |
/* If it's a lone function value, run it */ |
441 |
if(env->head->item->type==func) { |
case func: |
442 |
in_func= (funcp)(env->head->item->content.ptr); |
in_func= (funcp)(env->head->item->content.ptr); |
443 |
toss(env); |
toss(env); |
444 |
if(env->err) return; |
if(env->err) return; |
445 |
(*in_func)(env); |
(*in_func)(env); |
446 |
|
break; |
447 |
|
|
448 |
|
/* If it's a list */ |
449 |
|
case list: |
450 |
|
temp_val= env->head->item; |
451 |
|
env->head->item->refcount++; |
452 |
|
toss(env); |
453 |
|
if(env->err) return; |
454 |
|
iterator= (stackitem*)temp_val->content.ptr; |
455 |
|
while(iterator!=NULL && iterator->item!=NULL) { |
456 |
|
push_val(&(env->head), iterator->item); |
457 |
|
if(env->head->item->type==symb |
458 |
|
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
459 |
|
toss(env); |
460 |
|
if(env->err) return; |
461 |
|
eval(env); |
462 |
|
if(env->err) return; |
463 |
|
} |
464 |
|
iterator= iterator->next; |
465 |
|
} |
466 |
|
free_val(temp_val); |
467 |
|
break; |
468 |
|
|
469 |
|
/* If it's a string */ |
470 |
|
case string: |
471 |
|
temp_val= env->head->item; |
472 |
|
env->head->item->refcount++; |
473 |
|
toss(env); |
474 |
|
if(env->err) return; |
475 |
|
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
476 |
|
strcat(temp_string, "[ "); |
477 |
|
strcat(temp_string, (char*)temp_val->content.ptr); |
478 |
|
strcat(temp_string, " ]"); |
479 |
|
stack_read(env, temp_string); |
480 |
|
eval(env); |
481 |
|
if(env->err) return; |
482 |
|
free_val(temp_val); |
483 |
|
free(temp_string); |
484 |
|
break; |
485 |
|
|
486 |
|
default: |
487 |
} |
} |
488 |
} |
} |
489 |
|
|
490 |
/* Reverse a list */ |
/* Reverse (flip) a list */ |
491 |
extern void rev(environment *env){ |
extern void rev(environment *env){ |
492 |
stackitem *old_head, *new_head, *item; |
stackitem *old_head, *new_head, *item; |
493 |
|
|
564 |
int itemp; |
int itemp; |
565 |
size_t inlength= strlen(in_line)+1; |
size_t inlength= strlen(in_line)+1; |
566 |
int convert= 0; |
int convert= 0; |
|
static int non_eval_flag= 0; |
|
567 |
|
|
568 |
temp= malloc(inlength); |
temp= malloc(inlength); |
569 |
rest= malloc(inlength); |
rest= malloc(inlength); |
599 |
/* If single char */ |
/* If single char */ |
600 |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
601 |
if(*temp==';') { |
if(*temp==';') { |
602 |
if(!non_eval_flag) { |
if(!env->non_eval_flag) { |
603 |
eval(env); /* Evaluate top element */ |
eval(env); /* Evaluate top element */ |
604 |
break; |
break; |
605 |
} |
} |
611 |
if(*temp==']') { |
if(*temp==']') { |
612 |
push_sym(env, "["); |
push_sym(env, "["); |
613 |
pack(env); |
pack(env); |
614 |
if(non_eval_flag!=0) |
if(env->non_eval_flag) |
615 |
non_eval_flag--; |
env->non_eval_flag--; |
616 |
break; |
break; |
617 |
} |
} |
618 |
|
|
619 |
if(*temp=='[') { |
if(*temp=='[') { |
620 |
push_sym(env, "["); |
push_sym(env, "["); |
621 |
non_eval_flag++; |
env->non_eval_flag++; |
622 |
break; |
break; |
623 |
} |
} |
624 |
} |
} |