340 |
} |
} |
341 |
|
|
342 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
343 |
void print_h(stackitem *stack_head) |
void print_h(stackitem *stack_head, int noquote) |
344 |
{ |
{ |
345 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
346 |
case integer: |
case integer: |
347 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
348 |
break; |
break; |
349 |
case string: |
case string: |
350 |
printf("%s", (char*)stack_head->item->content.ptr); |
if(noquote) |
351 |
|
printf("%s", (char*)stack_head->item->content.ptr); |
352 |
|
else |
353 |
|
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
354 |
break; |
break; |
355 |
case symb: |
case symb: |
356 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
363 |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
364 |
printf("[ "); |
printf("[ "); |
365 |
while(stack_head != NULL) { |
while(stack_head != NULL) { |
366 |
print_h(stack_head); |
print_h(stack_head, noquote); |
367 |
printf(" "); |
printf(" "); |
368 |
stack_head=stack_head->next; |
stack_head=stack_head->next; |
369 |
} |
} |
378 |
env->err=1; |
env->err=1; |
379 |
return; |
return; |
380 |
} |
} |
381 |
print_h(env->head); |
print_h(env->head, 0); |
382 |
|
nl(); |
383 |
} |
} |
384 |
|
|
385 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
390 |
toss(env); |
toss(env); |
391 |
} |
} |
392 |
|
|
393 |
|
extern void princ_(environment *env) { |
394 |
|
if(env->head==NULL) { |
395 |
|
printerr("Too Few Arguments"); |
396 |
|
env->err=1; |
397 |
|
return; |
398 |
|
} |
399 |
|
print_h(env->head, 1); |
400 |
|
} |
401 |
|
|
402 |
|
/* Prints the top element of the stack and then discards it. */ |
403 |
|
extern void princ(environment *env) |
404 |
|
{ |
405 |
|
princ_(env); |
406 |
|
if(env->err) return; |
407 |
|
toss(env); |
408 |
|
} |
409 |
|
|
410 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
411 |
void print_st(stackitem *stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
412 |
{ |
{ |
413 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
414 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
415 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
416 |
print_h(stack_head); |
print_h(stack_head, 0); |
417 |
nl(); |
nl(); |
418 |
} |
} |
419 |
|
|
421 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
422 |
{ |
{ |
423 |
if(env->head == NULL) { |
if(env->head == NULL) { |
424 |
|
printf("Stack Empty\n"); |
425 |
return; |
return; |
426 |
} |
} |
427 |
print_st(env->head, 1); |
print_st(env->head, 1); |
|
nl(); |
|
428 |
} |
} |
429 |
|
|
430 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
497 |
value* temp_val; |
value* temp_val; |
498 |
stackitem* iterator; |
stackitem* iterator; |
499 |
|
|
500 |
|
eval_start: |
501 |
|
|
502 |
if(env->head==NULL) { |
if(env->head==NULL) { |
503 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
504 |
env->err=1; |
env->err=1; |
505 |
return; |
return; |
506 |
} |
} |
507 |
|
|
|
eval_start: |
|
|
|
|
508 |
switch(env->head->item->type) { |
switch(env->head->item->type) { |
509 |
/* if it's a symbol */ |
/* if it's a symbol */ |
510 |
case symb: |
case symb: |
750 |
clear(env); |
clear(env); |
751 |
if (env->err) return; |
if (env->err) return; |
752 |
for(i= 0; i<HASHTBLSIZE; i++) { |
for(i= 0; i<HASHTBLSIZE; i++) { |
753 |
if (env->symbols[i]!= NULL) { |
while(env->symbols[i]!= NULL) { |
754 |
forget_sym(&(env->symbols[i])); |
forget_sym(&(env->symbols[i])); |
|
env->symbols[i]= NULL; |
|
755 |
} |
} |
756 |
|
env->symbols[i]= NULL; |
757 |
} |
} |
758 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
759 |
} |
} |
832 |
init_env(&myenv); |
init_env(&myenv); |
833 |
|
|
834 |
while(1) { |
while(1) { |
835 |
if(myenv.in_string==NULL) |
if(myenv.in_string==NULL) { |
836 |
|
nl(); |
837 |
printstack(&myenv); |
printstack(&myenv); |
838 |
|
printf("> "); |
839 |
|
} |
840 |
read(&myenv); |
read(&myenv); |
841 |
if(myenv.err) { |
if(myenv.err) { |
842 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
1234 |
size_t inlength; |
size_t inlength; |
1235 |
|
|
1236 |
if(env->in_string==NULL) { |
if(env->in_string==NULL) { |
1237 |
|
if(depth > 0) { |
1238 |
|
printf("]> "); |
1239 |
|
} |
1240 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
1241 |
|
|
1242 |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |