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 struct lambda *t = malloc(sizeof (struct lambda));
39 struct lambda *make_ident(char *i)
41 struct lambda *r = make_lambda();
42 r->which = lambda_ident;
43 r->data.identifier.ident = strdup(i);
44 r->data.identifier.binding = NULL;
48 void lambda_bind(struct lambda *tob, struct lambda *binding, char *ident)
52 if(strcmp(ident, tob->data.identifier.ident) == 0 && tob->data.identifier.binding == NULL)
53 tob->data.identifier.binding = binding;
56 lambda_bind(tob->data.abstraction.expr, binding, ident);
59 lambda_bind(tob->data.application.expr1, binding, ident);
60 lambda_bind(tob->data.application.expr2, binding, ident);
65 struct lambda *make_abstraction(char *i, bool strict, struct lambda *t)
67 struct lambda *r = make_lambda();
68 r->which = lambda_abs;
69 r->data.abstraction.ident = strdup(i);
70 r->data.abstraction.strict = strict;
71 r->data.abstraction.expr = t;
76 struct lambda *make_application(struct lambda *t1, struct lambda *t2)
78 struct lambda *r = make_lambda();
79 r->which = lambda_app;
80 r->data.application.expr1 = t1;
81 r->data.application.expr2 = t2;
85 struct lambda *make_numeral(unsigned int i)
87 struct lambda *body = make_ident("x");
89 body = make_application(make_ident("f"), body);
90 return make_abstraction("f", false, make_abstraction("x", false, body));
93 struct lambda *make_bool(bool b)
96 ? make_abstraction("a", false, make_abstraction("b", false, make_ident("a")))
97 : make_abstraction("a", false, make_abstraction("b", false, make_ident("b")));
100 void decls_prepend(char *ident, struct lambda *value)
102 struct decllist *head = malloc(sizeof (struct decllist));
104 head->ident = strdup(ident);
109 struct lambda *decls_lookup(char *ident)
111 struct decllist *c = decls;
113 if(strcmp(c->ident, ident) == 0)
114 return copy(c->value);
117 return make_ident(ident);
122 setbuf(stdout, NULL);
130 %token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN LITERAL BANG
136 | lambda SEMICOLON program
140 decls_prepend($1->data.identifier.ident, $2);
141 printf("%s = ", $1->data.identifier.ident);
142 lambda_print($2, NULL);
148 struct lambda *t = $1;
150 for(unsigned int i = 0; i<999; i++)
151 if(!lambda_reduce(&t, &t, false))
153 lambda_print(t, NULL);
162 $$ = make_abstraction($2->data.identifier.ident, true, $3);
167 $$ = make_abstraction($1->data.identifier.ident, false, $2);
172 { $$ = make_application($1, $2); }
180 $$ = decls_lookup($1->data.identifier.ident);
190 : BANG IDENT abstraction
192 $$ = make_abstraction($2->data.identifier.ident, true, $3);
197 $$ = make_abstraction($1->data.identifier.ident, false, $2);