%define parse.error verbose %{ #include "lambda.h" #include "lambda.tab.h" #include "mem.h" #include "print.h" struct lambda *result; struct decllist *decls = NULL; extern int yylex(); int yydebug=1; void yyerror(const char *str) { fprintf(stderr, "parse error: %s\n", str); } int yywrap() { return 1; } struct lambda *make_lambda() { return malloc(sizeof (struct lambda)); } struct lambda *make_ident(char *i) { struct lambda *r = make_lambda(); r->which = lambda_ident; r->data.identifier.ident = strdup(i); r->data.identifier.revision = 0; return r; } struct lambda *make_abstraction(char *i, struct lambda *t) { struct lambda *r = make_lambda(); r->which = lambda_abs; r->data.abstraction.ident = strdup(i); r->data.abstraction.revision = 0; r->data.abstraction.expr = t; return r; } struct lambda *make_application(struct lambda *t1, struct lambda *t2) { struct lambda *r = make_lambda(); r->which = lambda_app; r->data.application.expr1 = t1; r->data.application.expr2 = t2; return r; } struct lambda *make_numeral(unsigned int i) { struct lambda *body = make_ident("x"); while(i-- > 0) body = make_application(make_ident("f"), body); return make_abstraction("f", make_abstraction("x", body)); } struct lambda *make_bool(bool b) { return b ? make_abstraction("a", make_abstraction("b", make_ident("a"))) : make_abstraction("a", make_abstraction("b", make_ident("b"))); } void decls_prepend(char *ident, struct lambda *value) { struct decllist *head = malloc(sizeof (struct decllist)); head->next = decls; head->ident = strdup(ident); head->value = value; decls = head; } struct lambda *decls_lookup(char *ident) { struct decllist *c = decls; while(c != NULL){ if(strcmp(c->ident, ident) == 0) return copy(c->value); c = c->next; } 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 ", c->ident); len = strlen(c->ident); for(unsigned int i = 1; i