add fancy printing
authorMart Lubbers <mart@martlubbers.net>
Thu, 17 May 2018 14:13:33 +0000 (16:13 +0200)
committerMart Lubbers <mart@martlubbers.net>
Thu, 17 May 2018 14:13:33 +0000 (16:13 +0200)
lambda.h
lambda.y
main.c
print.c
reduce.c

index 3409e7f..a1b2ff3 100644 (file)
--- a/lambda.h
+++ b/lambda.h
@@ -35,5 +35,6 @@ struct lambda *make_ident(char *);
 struct lambda *make_abstraction(char *, struct lambda *);
 struct lambda *make_application(struct lambda *, struct lambda *);
 void decls_free();
+void decls_print();
 #define YYSTYPE struct lambda *
 #endif
index 1dbcaaf..040d834 100644 (file)
--- a/lambda.y
+++ b/lambda.y
@@ -59,8 +59,6 @@ void decls_prepend(char *ident, struct lambda *value)
        head->next = decls;
        head->ident = strdup(ident);
        head->value = value;
-       printf("Declared %s as ", ident);
-       lambda_print(value, NULL);
        decls = head;
 }
 
@@ -75,6 +73,24 @@ struct lambda *decls_lookup(char *ident)
        return make_ident(ident);
 }
 
+void decls_print()
+{
+       struct decllist *c = decls;
+       unsigned int maxlen = 0, len;
+       while(c != NULL){
+               len = strlen(c->ident);
+               maxlen = maxlen < len ? len : maxlen;
+               c = c->next;
+       }
+
+       c = decls;
+       while(c != NULL){
+               printf("% -*s = ", maxlen, c->ident);
+               lambda_print(c->value, NULL);
+               c = c->next;
+       }
+}
+
 void decls_free()
 {
        struct decllist *t;
diff --git a/main.c b/main.c
index 22e4d66..4269ff4 100644 (file)
--- a/main.c
+++ b/main.c
@@ -14,6 +14,7 @@ int main()
        int r = yyparse();
        int maxdepth = 10000;
        if(r == 0){
+               decls_print();
                printf("     ");
                lambda_reduce(result, result, &maxdepth);
                lambda_print(result, NULL);
diff --git a/print.c b/print.c
index 33e7104..6a2cba5 100644 (file)
--- a/print.c
+++ b/print.c
@@ -11,29 +11,49 @@ void print_apos(unsigned int revision)
        }
 }
 
+void print_ident(char *ident, unsigned int revision)
+{
+       printf("%s", ident);
+       while(revision > 2){
+               putchar('\"');
+               revision -= 2;
+       }
+       if(revision == 1)
+               putchar('\'');
+}
+
 void term_print(struct lambda *t, struct lambda *mark)
 {
        if(t == mark)
                putchar('|');
        switch(t->which){
        case lambda_ident:
-               printf("%s", t->data.identifier.ident);
-               print_apos(t->data.identifier.revision);
+               print_ident(t->data.identifier.ident, t->data.identifier.revision);
                break;
        case lambda_abs:
-               printf("(λ");
-               printf("%s", t->data.abstraction.ident);
-               print_apos(t->data.abstraction.revision);
+               printf("λ");
+               print_ident(t->data.abstraction.ident, t->data.abstraction.revision);
+               while((t = t->data.abstraction.expr)->which == lambda_abs){
+                       putchar(' ');
+                       print_ident(t->data.abstraction.ident, t->data.abstraction.revision);
+               }
                putchar('.');
-               term_print(t->data.abstraction.expr, mark);
-               putchar(')');
+               term_print(t, mark);
                break;
        case lambda_app:
-               putchar('(');
+               if(t->data.application.expr1->which == lambda_abs)
+                       putchar('(');
                term_print(t->data.application.expr1, mark);
+               if(t->data.application.expr1->which == lambda_abs)
+                       putchar(')');
+
                putchar(' ');
+
+               if(t->data.application.expr2->which == lambda_app)
+                       putchar('(');
                term_print(t->data.application.expr2, mark);
-               putchar(')');
+               if(t->data.application.expr2->which == lambda_app)
+                       putchar(')');
                break;
        }
        if(t == mark)
index 7106743..65610e5 100644 (file)
--- a/reduce.c
+++ b/reduce.c
@@ -76,13 +76,7 @@ void lambda_reduce(struct lambda *t, struct lambda *total, int *maxdepth)
        if(*maxdepth == 0)
                return;
        struct lambda *t1, *t2;
-       switch(t->which){
-       case lambda_ident:
-               break;
-       case lambda_abs:
-//             lambda_reduce(t->data.abstraction.expr, total, maxdepth);
-               break;
-       case lambda_app:
+       if(t->which == lambda_app){
                t1 = t->data.application.expr1;
                t2 = t->data.application.expr2;
                lambda_reduce(t1, total, maxdepth);
@@ -100,6 +94,5 @@ void lambda_reduce(struct lambda *t, struct lambda *total, int *maxdepth)
                        (*maxdepth)--;
                        lambda_reduce(t, total, maxdepth);
                }
-               break;
        }
 }