| 21 |
Teddy Hogeborn <teddy@fukt.bth.se> |
Teddy Hogeborn <teddy@fukt.bth.se> |
| 22 |
*/ |
*/ |
| 23 |
|
|
| 24 |
#define CAR(X) (X->content.c->car) |
#define CAR(X) ((X)->content.c->car) |
| 25 |
#define CDR(X) (X->content.c->cdr) |
#define CDR(X) ((X)->content.c->cdr) |
| 26 |
|
|
| 27 |
/* printf, sscanf, fgets, fprintf, fopen, perror */ |
/* printf, sscanf, fgets, fprintf, fopen, perror */ |
| 28 |
#include <stdio.h> |
#include <stdio.h> |
| 62 |
env->gc_ref= NULL; |
env->gc_ref= NULL; |
| 63 |
|
|
| 64 |
env->head= new_val(env); |
env->head= new_val(env); |
|
env->head->type= empty; |
|
| 65 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
| 66 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
| 67 |
env->err= 0; |
env->err= 0; |
| 123 |
value *nval= malloc(sizeof(value)); |
value *nval= malloc(sizeof(value)); |
| 124 |
stackitem *nitem= malloc(sizeof(stackitem)); |
stackitem *nitem= malloc(sizeof(stackitem)); |
| 125 |
|
|
| 126 |
|
assert(nval != NULL); |
| 127 |
|
assert(nitem != NULL); |
| 128 |
|
|
| 129 |
nval->content.ptr= NULL; |
nval->content.ptr= NULL; |
| 130 |
nval->type= integer; |
nval->type= empty; |
| 131 |
|
|
| 132 |
nitem->item= nval; |
nitem->item= nval; |
| 133 |
nitem->next= env->gc_ref; |
nitem->next= env->gc_ref; |
| 338 |
int length= strlen(in_string)+1; |
int length= strlen(in_string)+1; |
| 339 |
|
|
| 340 |
new_value->content.ptr= malloc(length); |
new_value->content.ptr= malloc(length); |
| 341 |
|
assert(new_value != NULL); |
| 342 |
env->gc_count += length; |
env->gc_count += length; |
| 343 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
| 344 |
new_value->type= string; |
new_value->type= string; |
| 353 |
char *new_string, *current; |
char *new_string, *current; |
| 354 |
|
|
| 355 |
new_string= malloc((strlen(old_string)*2)+4); |
new_string= malloc((strlen(old_string)*2)+4); |
| 356 |
|
assert(new_string != NULL); |
| 357 |
strcpy(new_string, "sx_"); /* Stack eXternal */ |
strcpy(new_string, "sx_"); /* Stack eXternal */ |
| 358 |
current= new_string+3; |
current= new_string+3; |
| 359 |
while(old_string[0] != '\0'){ |
while(old_string[0] != '\0'){ |
| 423 |
|
|
| 424 |
/* Create a new symbol */ |
/* Create a new symbol */ |
| 425 |
(*new_symbol)= malloc(sizeof(symbol)); |
(*new_symbol)= malloc(sizeof(symbol)); |
| 426 |
|
assert((*new_symbol) != NULL); |
| 427 |
(*new_symbol)->val= NULL; /* undefined value */ |
(*new_symbol)->val= NULL; /* undefined value */ |
| 428 |
(*new_symbol)->next= NULL; |
(*new_symbol)->next= NULL; |
| 429 |
(*new_symbol)->id= malloc(strlen(in_string)+1); |
(*new_symbol)->id= malloc(strlen(in_string)+1); |
| 430 |
|
assert((*new_symbol)->id != NULL); |
| 431 |
strcpy((*new_symbol)->id, in_string); |
strcpy((*new_symbol)->id, in_string); |
| 432 |
|
|
| 433 |
/* Intern the new symbol in the hash table */ |
/* Intern the new symbol in the hash table */ |
| 505 |
} |
} |
| 506 |
|
|
| 507 |
/* Print a value */ |
/* Print a value */ |
| 508 |
void print_val(value *val, int noquote) |
void print_val(value *val, int noquote, stackitem *stack) |
| 509 |
{ |
{ |
| 510 |
|
stackitem *titem, *tstack; |
| 511 |
|
int depth; |
| 512 |
|
|
| 513 |
switch(val->type) { |
switch(val->type) { |
| 514 |
case empty: |
case empty: |
| 515 |
printf("[]"); |
printf("[]"); |
| 534 |
break; |
break; |
| 535 |
case tcons: |
case tcons: |
| 536 |
printf("[ "); |
printf("[ "); |
| 537 |
|
tstack= stack; |
| 538 |
do { |
do { |
| 539 |
print_val(CAR(val), noquote); |
titem=malloc(sizeof(stackitem)); |
| 540 |
|
assert(titem != NULL); |
| 541 |
|
titem->item=val; |
| 542 |
|
titem->next=tstack; |
| 543 |
|
tstack=titem; /* Put it on the stack */ |
| 544 |
|
/* Search a stack of values being printed to see if we are already |
| 545 |
|
printing this value */ |
| 546 |
|
titem=tstack; |
| 547 |
|
depth=0; |
| 548 |
|
while(titem != NULL && titem->item != CAR(val)){ |
| 549 |
|
titem=titem->next; |
| 550 |
|
depth++; |
| 551 |
|
} |
| 552 |
|
if(titem != NULL){ /* If we found it on the stack, */ |
| 553 |
|
printf("#%d#", depth); /* print a depth reference */ |
| 554 |
|
} else { |
| 555 |
|
print_val(CAR(val), noquote, tstack); |
| 556 |
|
} |
| 557 |
val= CDR(val); |
val= CDR(val); |
| 558 |
switch(val->type){ |
switch(val->type){ |
| 559 |
case empty: |
case empty: |
| 560 |
break; |
break; |
| 561 |
case tcons: |
case tcons: |
| 562 |
printf(" "); |
/* Search a stack of values being printed to see if we are already |
| 563 |
|
printing this value */ |
| 564 |
|
titem=tstack; |
| 565 |
|
depth=0; |
| 566 |
|
while(titem != NULL && titem->item != val){ |
| 567 |
|
titem=titem->next; |
| 568 |
|
depth++; |
| 569 |
|
} |
| 570 |
|
if(titem != NULL){ /* If we found it on the stack, */ |
| 571 |
|
printf(" . #%d#", depth); /* print a depth reference */ |
| 572 |
|
} else { |
| 573 |
|
printf(" "); |
| 574 |
|
} |
| 575 |
break; |
break; |
| 576 |
default: |
default: |
| 577 |
printf(" . "); /* Improper list */ |
printf(" . "); /* Improper list */ |
| 578 |
print_val(val, noquote); |
print_val(val, noquote, tstack); |
| 579 |
} |
} |
| 580 |
} while(val->type == tcons); |
} while(val->type == tcons && titem == NULL); |
| 581 |
|
titem=tstack; |
| 582 |
|
while(titem != stack){ |
| 583 |
|
tstack=titem->next; |
| 584 |
|
free(titem); |
| 585 |
|
titem=tstack; |
| 586 |
|
} |
| 587 |
printf(" ]"); |
printf(" ]"); |
| 588 |
break; |
break; |
| 589 |
} |
} |
| 596 |
env->err= 1; |
env->err= 1; |
| 597 |
return; |
return; |
| 598 |
} |
} |
| 599 |
print_val(CAR(env->head), 0); |
print_val(CAR(env->head), 0, NULL); |
| 600 |
nl(); |
nl(); |
| 601 |
} |
} |
| 602 |
|
|
| 615 |
env->err= 1; |
env->err= 1; |
| 616 |
return; |
return; |
| 617 |
} |
} |
| 618 |
print_val(CAR(env->head), 1); |
print_val(CAR(env->head), 1, NULL); |
| 619 |
} |
} |
| 620 |
|
|
| 621 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
| 632 |
if(CDR(stack_head)->type != empty) |
if(CDR(stack_head)->type != empty) |
| 633 |
print_st(CDR(stack_head), counter+1); |
print_st(CDR(stack_head), counter+1); |
| 634 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
| 635 |
print_val(CAR(stack_head), 0); |
print_val(CAR(stack_head), 0, NULL); |
| 636 |
nl(); |
nl(); |
| 637 |
} |
} |
| 638 |
|
|
| 809 |
|
|
| 810 |
old_head= CAR(env->head); |
old_head= CAR(env->head); |
| 811 |
new_head= new_val(env); |
new_head= new_val(env); |
|
new_head->type= empty; |
|
| 812 |
while(old_head->type != empty) { |
while(old_head->type != empty) { |
| 813 |
item= old_head; |
item= old_head; |
| 814 |
old_head= CDR(old_head); |
old_head= CDR(old_head); |
| 824 |
value *iterator, *temp, *ending; |
value *iterator, *temp, *ending; |
| 825 |
|
|
| 826 |
ending=new_val(env); |
ending=new_val(env); |
|
ending->type=empty; |
|
| 827 |
|
|
| 828 |
iterator= env->head; |
iterator= env->head; |
| 829 |
if(iterator->type == empty |
if(iterator->type == empty |
| 1169 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
| 1170 |
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
| 1171 |
new_string= malloc(len); |
new_string= malloc(len); |
| 1172 |
|
assert(new_string != NULL); |
| 1173 |
strcpy(new_string, b_val->content.ptr); |
strcpy(new_string, b_val->content.ptr); |
| 1174 |
strcat(new_string, a_val->content.ptr); |
strcat(new_string, a_val->content.ptr); |
| 1175 |
push_cstring(env, new_string); |
push_cstring(env, new_string); |
| 1769 |
} |
} |
| 1770 |
|
|
| 1771 |
env->in_string= malloc(strlen(CAR(env->head)->content.ptr)+1); |
env->in_string= malloc(strlen(CAR(env->head)->content.ptr)+1); |
| 1772 |
|
assert(env->in_string != NULL); |
| 1773 |
env->free_string= env->in_string; /* Save the original pointer */ |
env->free_string= env->in_string; /* Save the original pointer */ |
| 1774 |
strcpy(env->in_string, CAR(env->head)->content.ptr); |
strcpy(env->in_string, CAR(env->head)->content.ptr); |
| 1775 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
| 1777 |
|
|
| 1778 |
inlength= strlen(env->in_string)+1; |
inlength= strlen(env->in_string)+1; |
| 1779 |
match= malloc(inlength); |
match= malloc(inlength); |
| 1780 |
|
assert(match != NULL); |
| 1781 |
|
|
| 1782 |
if(sscanf(env->in_string, blankform, &readlength) != EOF |
if(sscanf(env->in_string, blankform, &readlength) != EOF |
| 1783 |
&& readlength != -1) { |
&& readlength != -1) { |
| 2446 |
swap(env); if(env->err) return; |
swap(env); if(env->err) return; |
| 2447 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
| 2448 |
} |
} |
| 2449 |
|
|
| 2450 |
|
/* 2: 3 => */ |
| 2451 |
|
/* 1: [ [ 1 . 2 ] [ 3 . 4 ] ] => 1: [ 3 . 4 ] */ |
| 2452 |
|
extern void assq(environment *env) |
| 2453 |
|
{ |
| 2454 |
|
assocgen(env, eq); |
| 2455 |
|
} |
| 2456 |
|
|
| 2457 |
|
|
| 2458 |
|
/* General assoc function */ |
| 2459 |
|
void assocgen(environment *env, funcp eqfunc) |
| 2460 |
|
{ |
| 2461 |
|
value *key, *item; |
| 2462 |
|
|
| 2463 |
|
/* Needs two values on the stack, the top one must be an association |
| 2464 |
|
list */ |
| 2465 |
|
if(env->head->type==empty || CDR(env->head)->type==empty) { |
| 2466 |
|
printerr("Too Few Arguments"); |
| 2467 |
|
env->err= 1; |
| 2468 |
|
return; |
| 2469 |
|
} |
| 2470 |
|
|
| 2471 |
|
if(CAR(env->head)->type!=tcons) { |
| 2472 |
|
printerr("Bad Argument Type"); |
| 2473 |
|
env->err= 2; |
| 2474 |
|
return; |
| 2475 |
|
} |
| 2476 |
|
|
| 2477 |
|
key=CAR(CDR(env->head)); |
| 2478 |
|
item=CAR(env->head); |
| 2479 |
|
|
| 2480 |
|
while(item->type == tcons){ |
| 2481 |
|
if(CAR(item)->type != tcons){ |
| 2482 |
|
printerr("Bad Argument Type"); |
| 2483 |
|
env->err= 2; |
| 2484 |
|
return; |
| 2485 |
|
} |
| 2486 |
|
push_val(env, key); |
| 2487 |
|
push_val(env, CAR(CAR(item))); |
| 2488 |
|
eqfunc(env); if(env->err) return; |
| 2489 |
|
|
| 2490 |
|
/* Check the result of 'eqfunc' */ |
| 2491 |
|
if(env->head->type==empty) { |
| 2492 |
|
printerr("Too Few Arguments"); |
| 2493 |
|
env->err= 1; |
| 2494 |
|
return; |
| 2495 |
|
} |
| 2496 |
|
if(CAR(env->head)->type!=integer) { |
| 2497 |
|
printerr("Bad Argument Type"); |
| 2498 |
|
env->err= 2; |
| 2499 |
|
return; |
| 2500 |
|
} |
| 2501 |
|
|
| 2502 |
|
if(CAR(env->head)->content.i){ |
| 2503 |
|
toss(env); if(env->err) return; |
| 2504 |
|
break; |
| 2505 |
|
} |
| 2506 |
|
toss(env); if(env->err) return; |
| 2507 |
|
|
| 2508 |
|
if(item->type!=tcons) { |
| 2509 |
|
printerr("Bad Argument Type"); |
| 2510 |
|
env->err= 2; |
| 2511 |
|
return; |
| 2512 |
|
} |
| 2513 |
|
|
| 2514 |
|
item=CDR(item); |
| 2515 |
|
} |
| 2516 |
|
|
| 2517 |
|
if(item->type == tcons){ /* A match was found */ |
| 2518 |
|
push_val(env, CAR(item)); |
| 2519 |
|
} else { |
| 2520 |
|
push_int(env, 0); |
| 2521 |
|
} |
| 2522 |
|
swap(env); if(env->err) return; |
| 2523 |
|
toss(env); if(env->err) return; |
| 2524 |
|
swap(env); if(env->err) return; |
| 2525 |
|
toss(env); |
| 2526 |
|
} |