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.revision = 0;
46 struct lambda *make_abstraction(char *i, bool strict, struct lambda *t)
48 struct lambda *r = make_lambda();
49 r->which = lambda_abs;
50 r->data.abstraction.ident = strdup(i);
51 r->data.abstraction.revision = 0;
52 r->data.abstraction.strict = strict;
53 r->data.abstraction.expr = t;
57 struct lambda *make_application(struct lambda *t1, struct lambda *t2)
59 struct lambda *r = make_lambda();
60 r->which = lambda_app;
61 r->data.application.expr1 = t1;
62 r->data.application.expr2 = t2;
66 struct lambda *make_numeral(unsigned int i)
68 struct lambda *body = make_ident("x");
70 body = make_application(make_ident("f"), body);
71 return make_abstraction("f", false, make_abstraction("x", false, body));
74 struct lambda *make_bool(bool b)
77 ? make_abstraction("a", false, make_abstraction("b", false, make_ident("a")))
78 : make_abstraction("a", false, make_abstraction("b", false, make_ident("b")));
81 void decls_prepend(char *ident, struct lambda *value)
83 struct decllist *head = malloc(sizeof (struct decllist));
85 head->ident = strdup(ident);
90 struct lambda *decls_lookup(char *ident)
92 struct decllist *c = decls;
94 if(strcmp(c->ident, ident) == 0)
95 return copy(c->value);
98 return make_ident(ident);
110 %token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN LITERAL BANG
116 | lambda SEMICOLON program
122 lambda_reduce($1, $1, &maxdepth);
123 lambda_print($1, NULL);
128 decls_prepend($1->data.identifier.ident, $2);
129 printf("%s = ", $1->data.identifier.ident);
130 lambda_print($2, NULL);
138 $$ = make_abstraction($2->data.identifier.ident, true, $3);
143 $$ = make_abstraction($1->data.identifier.ident, false, $2);
148 { $$ = make_application($1, $2); }
156 $$ = decls_lookup($1->data.identifier.ident);
166 : BANG IDENT abstraction
168 $$ = make_abstraction($2->data.identifier.ident, true, $3);
173 $$ = make_abstraction($1->data.identifier.ident, false, $2);