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, 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.expr = t;
56 struct lambda *make_application(struct lambda *t1, struct lambda *t2)
58 struct lambda *r = make_lambda();
59 r->which = lambda_app;
60 r->data.application.expr1 = t1;
61 r->data.application.expr2 = t2;
65 struct lambda *make_numeral(unsigned int i)
67 struct lambda *body = make_ident("x");
69 body = make_application(make_ident("f"), body);
70 return make_abstraction("f", make_abstraction("x", body));
73 struct lambda *make_bool(bool b)
76 ? make_abstraction("a", make_abstraction("b", make_ident("a")))
77 : make_abstraction("a", make_abstraction("b", make_ident("b")));
80 void decls_prepend(char *ident, struct lambda *value)
82 struct decllist *head = malloc(sizeof (struct decllist));
84 head->ident = strdup(ident);
89 struct lambda *decls_lookup(char *ident)
91 struct decllist *c = decls;
93 if(strcmp(c->ident, ident) == 0)
94 return copy(c->value);
97 return make_ident(ident);
109 %token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN LITERAL
115 | lambda SEMICOLON program
121 lambda_reduce($1, $1, &maxdepth);
122 lambda_print($1, NULL);
127 decls_prepend($1->data.identifier.ident, $3);
128 printf("%s = ", $1->data.identifier.ident);
129 lambda_print($3, NULL);
134 { $$ = make_application($1, $2); }
142 $$ = decls_lookup($1->data.identifier.ident);
147 $$ = make_ident($1->data.identifier.ident);
157 $$ = make_abstraction($1->data.identifier.ident, $2);