1 %define parse.error verbose
4 #include "lambda.tab.h"
9 struct decllist *decls = NULL;
13 void yyerror(const char *str)
15 fprintf(stderr, "parse error: %s\n", str);
23 struct lambda *make_lambda()
25 return malloc(sizeof (struct lambda));
28 struct lambda *make_ident(char *i)
30 struct lambda *r = make_lambda();
31 r->which = lambda_ident;
32 r->data.identifier.ident = strdup(i);
33 r->data.identifier.revision = 0;
37 struct lambda *make_abstraction(char *i, struct lambda *t)
39 struct lambda *r = make_lambda();
40 r->which = lambda_abs;
41 r->data.abstraction.ident = strdup(i);
42 r->data.abstraction.revision = 0;
43 r->data.abstraction.expr = t;
47 struct lambda *make_application(struct lambda *t1, struct lambda *t2)
49 struct lambda *r = make_lambda();
50 r->which = lambda_app;
51 r->data.application.expr1 = t1;
52 r->data.application.expr2 = t2;
56 struct lambda *make_numeral(unsigned int i)
58 struct lambda *body = make_ident("x");
60 body = make_application(make_ident("f"), body);
61 return make_abstraction("f", make_abstraction("x", body));
64 struct lambda *make_bool(bool b)
67 ? make_abstraction("a", make_abstraction("b", make_ident("a")))
68 : make_abstraction("a", make_abstraction("b", make_ident("b")));
71 void decls_prepend(char *ident, struct lambda *value)
73 struct decllist *head = malloc(sizeof (struct decllist));
75 head->ident = strdup(ident);
80 struct lambda *decls_lookup(char *ident)
82 struct decllist *c = decls;
84 if(strcmp(c->ident, ident) == 0)
85 return copy(c->value);
88 return make_ident(ident);
93 struct decllist *c = decls;
94 unsigned int maxlen = 0, len;
96 len = strlen(c->ident);
97 maxlen = maxlen < len ? len : maxlen;
103 printf("%s ", c->ident);
104 len = strlen(c->ident);
105 for(unsigned int i = 1; i<maxlen-len; i++)
108 lambda_print(c->value, NULL);
116 while(decls != NULL){
118 lambda_free(decls->value);
127 %token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN LITERAL
136 : FUNC ASSIGN term SEMICOLON
138 decls_prepend($1->data.identifier.ident, $3);
143 { $$ = make_application($1, $2); }
151 $$ = decls_lookup($1->data.identifier.ident);
156 $$ = make_ident($1->data.identifier.ident);
166 $$ = make_abstraction($1->data.identifier.ident, $2);