| 58 |
typedef struct { |
typedef struct { |
| 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 */ |
| 62 |
} environment; |
} environment; |
| 63 |
|
|
| 64 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
| 179 |
new_symbol= hash(env->symbols, in_string); |
new_symbol= hash(env->symbols, in_string); |
| 180 |
new_value->content.ptr= *new_symbol; |
new_value->content.ptr= *new_symbol; |
| 181 |
|
|
| 182 |
if(new_value->content.ptr==NULL) { /* If symbol was undefined */ |
if(*new_symbol==NULL) { /* If symbol was undefined */ |
| 183 |
|
|
| 184 |
/* Create a new symbol */ |
/* Create a new symbol */ |
| 185 |
*new_symbol= malloc(sizeof(symbol)); |
(*new_symbol)= malloc(sizeof(symbol)); |
| 186 |
(*new_symbol)->val= NULL; /* undefined value */ |
(*new_symbol)->val= NULL; /* undefined value */ |
| 187 |
(*new_symbol)->next= NULL; |
(*new_symbol)->next= NULL; |
| 188 |
(*new_symbol)->id= malloc(strlen(in_string)+1); |
(*new_symbol)->id= malloc(strlen(in_string)+1); |
| 255 |
} |
} |
| 256 |
|
|
| 257 |
/* Print newline. */ |
/* Print newline. */ |
| 258 |
extern void nl() |
extern void nl(environment *env) |
| 259 |
{ |
{ |
| 260 |
printf("\n"); |
printf("\n"); |
| 261 |
} |
} |
| 349 |
return out_item; |
return out_item; |
| 350 |
} |
} |
| 351 |
|
|
| 352 |
|
/* Recall a value from a symbol, if bound */ |
| 353 |
|
extern void rcl(environment *env) |
| 354 |
|
{ |
| 355 |
|
value *val; |
| 356 |
|
|
| 357 |
|
if(env->head == NULL) { |
| 358 |
|
printerr("Stack empty"); |
| 359 |
|
return; |
| 360 |
|
} |
| 361 |
|
|
| 362 |
|
if(env->head->item->type!=symb) { |
| 363 |
|
printerr("Not a symbol"); |
| 364 |
|
return; |
| 365 |
|
} |
| 366 |
|
val=((symbol *)(env->head->item->content.ptr))->val; |
| 367 |
|
if(val == NULL){ |
| 368 |
|
printerr("Unbound variable"); |
| 369 |
|
return; |
| 370 |
|
} |
| 371 |
|
toss(env); /* toss the symbol */ |
| 372 |
|
push_val(&(env->head), val); /* Return its bound value */ |
| 373 |
|
} |
| 374 |
|
|
| 375 |
/* 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 |
| 376 |
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 |
return; |
return; |
| 416 |
} |
} |
| 417 |
|
|
|
/* push(&(env->head), copy(env->head)); */ |
|
|
/* swap(env); */ |
|
|
/* toss(env); */ |
|
| 418 |
} |
} |
| 419 |
|
|
| 420 |
/* Make a list. */ |
/* Make a list. */ |
| 644 |
toss(env); |
toss(env); |
| 645 |
} |
} |
| 646 |
|
|
| 647 |
|
/* List all defined words */ |
| 648 |
|
extern void words(environment *env) |
| 649 |
|
{ |
| 650 |
|
symbol *temp; |
| 651 |
|
int i; |
| 652 |
|
|
| 653 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
| 654 |
|
temp= env->symbols[i]; |
| 655 |
|
while(temp!=NULL) { |
| 656 |
|
printf("%s\n", temp->id); |
| 657 |
|
temp= temp->next; |
| 658 |
|
} |
| 659 |
|
} |
| 660 |
|
} |
| 661 |
|
|
| 662 |
int main() |
int main() |
| 663 |
{ |
{ |
| 664 |
environment myenv; |
environment myenv; |