| 58 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
| 59 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
| 60 |
int err; /* Error flag */ |
int err; /* Error flag */ |
|
int non_eval_flag; |
|
| 61 |
char *in_string; /* Input pending to be read */ |
char *in_string; /* Input pending to be read */ |
| 62 |
|
char *free_string; /* Free this string when all input is |
| 63 |
|
read from in_string */ |
| 64 |
} environment; |
} environment; |
| 65 |
|
|
| 66 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
| 74 |
|
|
| 75 |
env->in_string= NULL; |
env->in_string= NULL; |
| 76 |
env->err= 0; |
env->err= 0; |
|
env->non_eval_flag= 0; |
|
| 77 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
| 78 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
| 79 |
} |
} |
| 100 |
free(item); /* free the stackitem */ |
free(item); /* free the stackitem */ |
| 101 |
item=temp; /* go to next stackitem */ |
item=temp; /* go to next stackitem */ |
| 102 |
} |
} |
|
free(val); /* Free the actual list value */ |
|
| 103 |
break; |
break; |
| 104 |
case integer: |
case integer: |
| 105 |
case func: |
case func: |
| 106 |
case symb: |
case symb: |
| 107 |
break; |
break; |
| 108 |
} |
} |
| 109 |
|
if(val->id!=NULL) |
| 110 |
|
free(val->id); |
| 111 |
|
free(val); /* Free the actual list value */ |
| 112 |
} |
} |
| 113 |
} |
} |
| 114 |
|
|
| 174 |
|
|
| 175 |
new_value->content.val= in_val; |
new_value->content.val= in_val; |
| 176 |
new_value->type= integer; |
new_value->type= integer; |
| 177 |
new_value->refcount=1; |
new_value->refcount= 1; |
| 178 |
|
new_value->id= NULL; |
| 179 |
|
|
| 180 |
push_val(env, new_value); |
push_val(env, new_value); |
| 181 |
} |
} |
| 188 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
| 189 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
| 190 |
new_value->type= string; |
new_value->type= string; |
| 191 |
new_value->refcount=1; |
new_value->refcount= 1; |
| 192 |
|
new_value->id= NULL; |
| 193 |
|
|
| 194 |
push_val(env, new_value); |
push_val(env, new_value); |
| 195 |
} |
} |
| 215 |
} |
} |
| 216 |
|
|
| 217 |
extern void mangle(environment *env){ |
extern void mangle(environment *env){ |
|
value *new_value; |
|
| 218 |
char *new_string; |
char *new_string; |
| 219 |
|
|
| 220 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
| 234 |
toss(env); |
toss(env); |
| 235 |
if(env->err) return; |
if(env->err) return; |
| 236 |
|
|
| 237 |
new_value= malloc(sizeof(value)); |
push_cstring(env, new_string); |
|
new_value->content.ptr= new_string; |
|
|
new_value->type= string; |
|
|
new_value->refcount=1; |
|
|
|
|
|
push_val(env, new_value); |
|
| 238 |
} |
} |
| 239 |
|
|
| 240 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
| 336 |
} |
} |
| 337 |
|
|
| 338 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
| 339 |
void print_h(stackitem *stack_head) |
void print_h(stackitem *stack_head, int noquote) |
| 340 |
{ |
{ |
| 341 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
| 342 |
case integer: |
case integer: |
| 343 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
| 344 |
break; |
break; |
| 345 |
case string: |
case string: |
| 346 |
printf("%s", (char*)stack_head->item->content.ptr); |
if(noquote) |
| 347 |
|
printf("%s", (char*)stack_head->item->content.ptr); |
| 348 |
|
else |
| 349 |
|
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
| 350 |
break; |
break; |
| 351 |
case symb: |
case symb: |
| 352 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
| 359 |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
| 360 |
printf("[ "); |
printf("[ "); |
| 361 |
while(stack_head != NULL) { |
while(stack_head != NULL) { |
| 362 |
print_h(stack_head); |
print_h(stack_head, noquote); |
| 363 |
printf(" "); |
printf(" "); |
| 364 |
stack_head=stack_head->next; |
stack_head=stack_head->next; |
| 365 |
} |
} |
| 374 |
env->err=1; |
env->err=1; |
| 375 |
return; |
return; |
| 376 |
} |
} |
| 377 |
print_h(env->head); |
print_h(env->head, 0); |
| 378 |
|
nl(); |
| 379 |
} |
} |
| 380 |
|
|
| 381 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
| 386 |
toss(env); |
toss(env); |
| 387 |
} |
} |
| 388 |
|
|
| 389 |
|
extern void princ_(environment *env) { |
| 390 |
|
if(env->head==NULL) { |
| 391 |
|
printerr("Too Few Arguments"); |
| 392 |
|
env->err=1; |
| 393 |
|
return; |
| 394 |
|
} |
| 395 |
|
print_h(env->head, 1); |
| 396 |
|
} |
| 397 |
|
|
| 398 |
|
/* Prints the top element of the stack and then discards it. */ |
| 399 |
|
extern void princ(environment *env) |
| 400 |
|
{ |
| 401 |
|
princ_(env); |
| 402 |
|
if(env->err) return; |
| 403 |
|
toss(env); |
| 404 |
|
} |
| 405 |
|
|
| 406 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
| 407 |
void print_st(stackitem *stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
| 408 |
{ |
{ |
| 409 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
| 410 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
| 411 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
| 412 |
print_h(stack_head); |
print_h(stack_head, 0); |
| 413 |
nl(); |
nl(); |
| 414 |
} |
} |
| 415 |
|
|
| 417 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
| 418 |
{ |
{ |
| 419 |
if(env->head == NULL) { |
if(env->head == NULL) { |
| 420 |
|
printf("Stack Empty\n"); |
| 421 |
return; |
return; |
| 422 |
} |
} |
| 423 |
print_st(env->head, 1); |
print_st(env->head, 1); |
|
nl(); |
|
| 424 |
} |
} |
| 425 |
|
|
| 426 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
| 493 |
value* temp_val; |
value* temp_val; |
| 494 |
stackitem* iterator; |
stackitem* iterator; |
| 495 |
|
|
| 496 |
|
eval_start: |
| 497 |
|
|
| 498 |
if(env->head==NULL) { |
if(env->head==NULL) { |
| 499 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 500 |
env->err=1; |
env->err=1; |
| 501 |
return; |
return; |
| 502 |
} |
} |
| 503 |
|
|
|
eval_start: |
|
|
|
|
| 504 |
switch(env->head->item->type) { |
switch(env->head->item->type) { |
| 505 |
/* if it's a symbol */ |
/* if it's a symbol */ |
| 506 |
case symb: |
case symb: |
| 735 |
toss(env); toss(env); |
toss(env); toss(env); |
| 736 |
} |
} |
| 737 |
|
|
| 738 |
|
extern void clear(environment *); |
| 739 |
|
void forget_sym(symbol **); |
| 740 |
|
|
| 741 |
/* Quit stack. */ |
/* Quit stack. */ |
| 742 |
extern void quit(environment *env) |
extern void quit(environment *env) |
| 743 |
{ |
{ |
| 744 |
|
long i; |
| 745 |
|
|
| 746 |
|
clear(env); |
| 747 |
|
if (env->err) return; |
| 748 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
| 749 |
|
while(env->symbols[i]!= NULL) { |
| 750 |
|
forget_sym(&(env->symbols[i])); |
| 751 |
|
} |
| 752 |
|
env->symbols[i]= NULL; |
| 753 |
|
} |
| 754 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
| 755 |
} |
} |
| 756 |
|
|
| 776 |
} |
} |
| 777 |
} |
} |
| 778 |
|
|
| 779 |
|
/* Internal forget function */ |
| 780 |
|
void forget_sym(symbol **hash_entry) { |
| 781 |
|
symbol *temp; |
| 782 |
|
|
| 783 |
|
temp= *hash_entry; |
| 784 |
|
*hash_entry= (*hash_entry)->next; |
| 785 |
|
|
| 786 |
|
if(temp->val!=NULL) { |
| 787 |
|
free_val(temp->val); |
| 788 |
|
} |
| 789 |
|
free(temp->id); |
| 790 |
|
free(temp); |
| 791 |
|
} |
| 792 |
|
|
| 793 |
/* Forgets a symbol (remove it from the hash table) */ |
/* Forgets a symbol (remove it from the hash table) */ |
| 794 |
extern void forget(environment *env) |
extern void forget(environment *env) |
| 795 |
{ |
{ |
| 796 |
char* sym_id; |
char* sym_id; |
| 797 |
stackitem *stack_head= env->head; |
stackitem *stack_head= env->head; |
|
symbol **hash_entry, *temp; |
|
| 798 |
|
|
| 799 |
if(stack_head==NULL) { |
if(stack_head==NULL) { |
| 800 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
| 811 |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
| 812 |
toss(env); |
toss(env); |
| 813 |
|
|
| 814 |
hash_entry= hash(env->symbols, sym_id); |
return forget_sym(hash(env->symbols, sym_id)); |
|
temp= *hash_entry; |
|
|
*hash_entry= (*hash_entry)->next; |
|
|
|
|
|
if(temp->val!=NULL) { |
|
|
free_val(temp->val); |
|
|
} |
|
|
free(temp->id); |
|
|
free(temp); |
|
| 815 |
} |
} |
| 816 |
|
|
| 817 |
/* Returns the current error number to the stack */ |
/* Returns the current error number to the stack */ |
| 828 |
init_env(&myenv); |
init_env(&myenv); |
| 829 |
|
|
| 830 |
while(1) { |
while(1) { |
| 831 |
if(myenv.in_string==NULL) |
if(myenv.in_string==NULL) { |
| 832 |
|
nl(); |
| 833 |
printstack(&myenv); |
printstack(&myenv); |
| 834 |
|
printf("> "); |
| 835 |
|
} |
| 836 |
read(&myenv); |
read(&myenv); |
| 837 |
if(myenv.err) { |
if(myenv.err) { |
| 838 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
| 1216 |
|
|
| 1217 |
/* Read a value and place on stack */ |
/* Read a value and place on stack */ |
| 1218 |
extern void read(environment *env) { |
extern void read(environment *env) { |
| 1219 |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%[\001-\377]"; |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
| 1220 |
const char strform[]= "\"%[^\"]\"%[\001-\377]"; |
const char strform[]= "\"%[^\"]\"%n"; |
| 1221 |
const char intform[]= "%i%[\001-\377]"; |
const char intform[]= "%i%n"; |
| 1222 |
const char blankform[]= "%*[ \t]%[\001-\377]"; |
const char blankform[]= "%*[ \t]%n"; |
| 1223 |
const char ebrackform[]= "%*1[]]%[\001-\377]"; |
const char ebrackform[]= "%*1[]]%n"; |
| 1224 |
const char semicform[]= "%*1[;]%[\001-\377]"; |
const char semicform[]= "%*1[;]%n"; |
| 1225 |
const char bbrackform[]= "%*1[[]%[\001-\377]"; |
const char bbrackform[]= "%*1[[]%n"; |
| 1226 |
|
|
| 1227 |
int itemp; |
int itemp, readlength= -1; |
| 1228 |
static int depth= 0; |
static int depth= 0; |
| 1229 |
char *rest, *match; |
char *rest, *match; |
| 1230 |
size_t inlength; |
size_t inlength; |
| 1231 |
|
|
| 1232 |
if(env->in_string==NULL) { |
if(env->in_string==NULL) { |
| 1233 |
|
if(depth > 0) { |
| 1234 |
|
printf("]> "); |
| 1235 |
|
} |
| 1236 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
| 1237 |
|
|
| 1238 |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
| 1239 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
| 1240 |
strcpy(env->in_string, env->head->item->content.ptr); |
strcpy(env->in_string, env->head->item->content.ptr); |
| 1241 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
| 1242 |
} |
} |
| 1245 |
match= malloc(inlength); |
match= malloc(inlength); |
| 1246 |
rest= malloc(inlength); |
rest= malloc(inlength); |
| 1247 |
|
|
| 1248 |
if(sscanf(env->in_string, blankform, rest)) { |
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
| 1249 |
|
&& readlength != -1) { |
| 1250 |
; |
; |
| 1251 |
} else if(sscanf(env->in_string, intform, &itemp, rest) > 0) { |
} else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF |
| 1252 |
|
&& readlength != -1) { |
| 1253 |
push_int(env, itemp); |
push_int(env, itemp); |
| 1254 |
} else if(sscanf(env->in_string, strform, match, rest) > 0) { |
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
| 1255 |
|
&& readlength != -1) { |
| 1256 |
push_cstring(env, match); |
push_cstring(env, match); |
| 1257 |
} else if(sscanf(env->in_string, symbform, match, rest) > 0) { |
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
| 1258 |
|
&& readlength != -1) { |
| 1259 |
push_sym(env, match); |
push_sym(env, match); |
| 1260 |
} else if(sscanf(env->in_string, ebrackform, rest) > 0) { |
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
| 1261 |
|
&& readlength != -1) { |
| 1262 |
pack(env); if(env->err) return; |
pack(env); if(env->err) return; |
| 1263 |
if(depth!=0) depth--; |
if(depth != 0) depth--; |
| 1264 |
} else if(sscanf(env->in_string, semicform, rest) > 0) { |
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
| 1265 |
|
&& readlength != -1) { |
| 1266 |
push_sym(env, ";"); |
push_sym(env, ";"); |
| 1267 |
} else if(sscanf(env->in_string, bbrackform, rest) > 0) { |
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
| 1268 |
|
&& readlength != -1) { |
| 1269 |
push_sym(env, "["); |
push_sym(env, "["); |
| 1270 |
depth++; |
depth++; |
| 1271 |
} else { |
} else { |
| 1272 |
free(rest); |
free(env->free_string); |
| 1273 |
rest= NULL; |
env->in_string = env->free_string = NULL; |
| 1274 |
|
free(match); |
| 1275 |
|
} |
| 1276 |
|
if ( env->in_string != NULL) { |
| 1277 |
|
env->in_string += readlength; |
| 1278 |
} |
} |
|
|
|
|
free(env->in_string); |
|
|
free(match); |
|
|
|
|
|
env->in_string= rest; |
|
| 1279 |
|
|
| 1280 |
if(depth) |
if(depth) |
| 1281 |
return read(env); |
return read(env); |