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)
93 return make_abstraction("a", false,
94 make_abstraction("b", false, make_ident(b ? "a" : "b")));
97 void decls_prepend(char *ident, struct lambda *value)
99 struct decllist *head = malloc(sizeof (struct decllist));
101 head->ident = strdup(ident);
106 struct lambda *decls_lookup(char *ident)
108 for(struct decllist *c = decls; c != NULL; c = c->next)
109 if(strcmp(c->ident, ident) == 0)
110 return copy(c->value);
111 return make_ident(ident);
123 %token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN LITERAL BANG
129 | lambda SEMICOLON program
133 decls_prepend($1->data.identifier.ident, $2);
134 printf("%s = ", $1->data.identifier.ident);
135 lambda_print($2, NULL);
140 struct lambda *t = $1;
142 for(unsigned int i = 0; i<999; i++)
143 if(!lambda_reduce(&t, &t, false))
145 lambda_print(t, NULL);
153 $$ = make_abstraction($2->data.identifier.ident, true, $3);
158 $$ = make_abstraction($1->data.identifier.ident, false, $2);
163 { $$ = make_application($1, $2); }
171 $$ = decls_lookup($1->data.identifier.ident);
181 : BANG IDENT abstraction
183 $$ = make_abstraction($2->data.identifier.ident, true, $3);
188 $$ = make_abstraction($1->data.identifier.ident, false, $2);