remove smart allocations, back to bruteforce
[lambda.git] / print.c
1 #include <stdio.h>
2 #include "lambda.h"
3
4 #define PRINT_ABS(t) {\
5 if(t->data.abstraction.strict)\
6 putchar('!');\
7 printf("%s", t->data.abstraction.ident);\
8 }
9
10
11 void term_print(struct lambda *t, struct lambda *mark)
12 {
13 if(t == mark)
14 putchar('|');
15 switch(t->which){
16 case lambda_ident:
17 // printf("%s(%p)", t->data.identifier.ident, (void *)t->data.identifier.binding);
18 printf("%s", t->data.identifier.ident);
19 break;
20 case lambda_abs:
21 printf("λ");
22 PRINT_ABS(t);
23 while((t = t->data.abstraction.expr)->which == lambda_abs){
24 putchar(' ');
25 PRINT_ABS(t);
26 }
27 putchar('.');
28 term_print(t, mark);
29 break;
30 case lambda_app:
31 if(t->data.application.expr1->which == lambda_abs)
32 putchar('(');
33 term_print(t->data.application.expr1, mark);
34 if(t->data.application.expr1->which == lambda_abs)
35 putchar(')');
36
37 putchar(' ');
38
39 if(t->data.application.expr2->which == lambda_app)
40 putchar('(');
41 term_print(t->data.application.expr2, mark);
42 if(t->data.application.expr2->which == lambda_app)
43 putchar(')');
44 break;
45 }
46 if(t == mark)
47 putchar('|');
48 }
49
50 void lambda_print(struct lambda *t, struct lambda *mark)
51 {
52 term_print(t, mark);
53 printf(";");
54 }