1 %define parse.error verbose
4 #include "lambda.tab.h"
10 struct decllist *decls = NULL;
14 void yyerror(const char *str)
16 fprintf(stderr, "parse error: %s\n", str);
24 lambda_free(decls->value);
32 struct lambda *make_lambda()
34 return malloc(sizeof (struct lambda));
37 struct lambda *make_ident(char *i)
39 struct lambda *r = make_lambda();
40 r->which = lambda_ident;
41 r->data.identifier.ident = strdup(i);
42 r->data.identifier.binding = NULL;
46 void lambda_bind(struct lambda *tob, struct lambda *binding, char *ident)
50 if(strcmp(ident, tob->data.identifier.ident) == 0 && tob->data.identifier.binding == NULL)
51 tob->data.identifier.binding = binding;
54 lambda_bind(tob->data.abstraction.expr, binding, ident);
57 lambda_bind(tob->data.application.expr1, binding, ident);
58 lambda_bind(tob->data.application.expr2, binding, ident);
63 struct lambda *make_abstraction(char *i, bool strict, struct lambda *t)
65 struct lambda *r = make_lambda();
66 r->which = lambda_abs;
67 r->data.abstraction.ident = strdup(i);
68 r->data.abstraction.strict = strict;
69 r->data.abstraction.expr = t;
74 struct lambda *make_application(struct lambda *t1, struct lambda *t2)
76 struct lambda *r = make_lambda();
77 r->which = lambda_app;
78 r->data.application.expr1 = t1;
79 r->data.application.expr2 = t2;
83 struct lambda *make_numeral(unsigned int i)
85 struct lambda *body = make_ident("x");
87 body = make_application(make_ident("f"), body);
88 return make_abstraction("f", false, make_abstraction("x", false, body));
91 struct lambda *make_bool(bool b)
94 ? make_abstraction("a", false, make_abstraction("b", false, make_ident("a")))
95 : make_abstraction("a", false, make_abstraction("b", false, make_ident("b")));
98 void decls_prepend(char *ident, struct lambda *value)
100 struct decllist *head = malloc(sizeof (struct decllist));
102 head->ident = strdup(ident);
107 struct lambda *decls_lookup(char *ident)
109 struct decllist *c = decls;
111 if(strcmp(c->ident, ident) == 0)
112 return copy(c->value);
115 return make_ident(ident);
120 setbuf(stdout, NULL);
128 %token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN LITERAL BANG
134 | lambda SEMICOLON program
138 decls_prepend($1->data.identifier.ident, $2);
139 printf("%s = ", $1->data.identifier.ident);
140 lambda_print($2, NULL);
146 struct lambda *t = $1;
148 for(unsigned int i = 0; i<999; i++)
149 if(!lambda_reduce(&t, &t, true))
151 lambda_print(t, NULL);
160 $$ = make_abstraction($2->data.identifier.ident, true, $3);
165 $$ = make_abstraction($1->data.identifier.ident, false, $2);
170 { $$ = make_application($1, $2); }
178 $$ = decls_lookup($1->data.identifier.ident);
188 : BANG IDENT abstraction
190 $$ = make_abstraction($2->data.identifier.ident, true, $3);
195 $$ = make_abstraction($1->data.identifier.ident, false, $2);