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
head->next = decls;
head->ident = strdup(ident);
head->value = value;
- printf("Declared %s as ", ident);
- lambda_print(value, NULL);
decls = head;
}
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;
int r = yyparse();
int maxdepth = 10000;
if(r == 0){
+ decls_print();
printf(" ");
lambda_reduce(result, result, &maxdepth);
lambda_print(result, NULL);
}
}
+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)
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);
(*maxdepth)--;
lambda_reduce(t, total, maxdepth);
}
- break;
}
}