add reference counter for future smarter allocations
[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 print_apos(t->data.abstraction.revision);\
9 }
10
11
12 void print_apos(unsigned int revision)
13 {
14 if(revision == 1)
15 printf("\'");
16 if(revision > 2){
17 printf("\"");
18 print_apos(revision - 2);
19 }
20 }
21
22 void term_print(struct lambda *t, struct lambda *mark)
23 {
24 if(t == mark)
25 putchar('|');
26 switch(t->which){
27 case lambda_ident:
28 printf("%s", t->data.identifier.ident);
29 print_apos(t->data.identifier.revision);
30 break;
31 case lambda_abs:
32 printf("λ");
33 PRINT_ABS(t);
34 while((t = t->data.abstraction.expr)->which == lambda_abs){
35 putchar(' ');
36 PRINT_ABS(t);
37 }
38 putchar('.');
39 term_print(t, mark);
40 break;
41 case lambda_app:
42 if(t->data.application.expr1->which == lambda_abs)
43 putchar('(');
44 term_print(t->data.application.expr1, mark);
45 if(t->data.application.expr1->which == lambda_abs)
46 putchar(')');
47
48 putchar(' ');
49
50 if(t->data.application.expr2->which == lambda_app)
51 putchar('(');
52 term_print(t->data.application.expr2, mark);
53 if(t->data.application.expr2->which == lambda_app)
54 putchar(')');
55 break;
56 }
57 if(t == mark)
58 putchar('|');
59 }
60
61 void lambda_print(struct lambda *t, struct lambda *mark)
62 {
63 term_print(t, mark);
64 printf(";\n");
65 }