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.revision = 0;
48 struct lambda *make_abstraction(char *i, bool strict, struct lambda *t)
50 struct lambda *r = make_lambda();
51 r->which = lambda_abs;
52 r->data.abstraction.ident = strdup(i);
53 r->data.abstraction.revision = 0;
54 r->data.abstraction.strict = strict;
55 r->data.abstraction.expr = t;
59 struct lambda *make_application(struct lambda *t1, struct lambda *t2)
61 struct lambda *r = make_lambda();
62 r->which = lambda_app;
63 r->data.application.expr1 = t1;
64 r->data.application.expr2 = t2;
68 struct lambda *make_numeral(unsigned int i)
70 struct lambda *body = make_ident("x");
72 body = make_application(make_ident("f"), body);
73 return make_abstraction("f", false, make_abstraction("x", false, body));
76 struct lambda *make_bool(bool b)
79 ? make_abstraction("a", false, make_abstraction("b", false, make_ident("a")))
80 : make_abstraction("a", false, make_abstraction("b", false, make_ident("b")));
83 void decls_prepend(char *ident, struct lambda *value)
85 struct decllist *head = malloc(sizeof (struct decllist));
87 head->ident = strdup(ident);
92 struct lambda *decls_lookup(char *ident)
94 struct decllist *c = decls;
96 if(strcmp(c->ident, ident) == 0)
97 return copy(c->value);
100 return make_ident(ident);
112 %token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN LITERAL BANG
118 | lambda SEMICOLON program
124 lambda_reduce(&$1, &$1, &maxdepth);
125 lambda_print($1, NULL);
130 decls_prepend($1->data.identifier.ident, $2);
131 printf("%s = ", $1->data.identifier.ident);
132 lambda_print($2, NULL);
140 $$ = make_abstraction($2->data.identifier.ident, true, $3);
145 $$ = make_abstraction($1->data.identifier.ident, false, $2);
150 { $$ = make_application($1, $2); }
158 $$ = decls_lookup($1->data.identifier.ident);
168 : BANG IDENT abstraction
170 $$ = make_abstraction($2->data.identifier.ident, true, $3);
175 $$ = make_abstraction($1->data.identifier.ident, false, $2);