#include "mem.h"
struct lambda *result;
+struct decllist *decls = NULL;
extern int yylex();
int yydebug=1;
return r;
}
+void decls_prepend(char *ident, struct lambda *value)
+{
+ printf("add: %s\n", ident);
+ struct decllist *head = malloc(sizeof (struct decllist));
+ head->next = decls;
+ head->ident = ident;
+ head->value = value;
+ decls = head;
+}
+
+struct lambda *decls_lookup(char *ident)
+{
+ printf("lookup: %s\n", ident);
+ struct decllist *c = decls;
+ while(c != NULL){
+ if(strcmp(c->ident, ident) == 0)
+ return copy(c->value);
+ c = c->next;
+ }
+ printf("Notfound\n");
+ return make_ident(ident);
+}
+
%}
-%token LAMBDA DOT OBRACE CBRACE IDENT I K S T F
+%token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN
%%
lambda
- : term
+ : decl lambda
+ | term
{ result = $$; }
+decl
+ : FUNC ASSIGN term SEMICOLON
+ { decls_prepend($1->data.identifier, $3); }
term
: term appterm
- {
- $$ = make_application($1, $2);
- }
+ { $$ = make_application($1, $2); }
| appterm
{ $$ = $1; }
appterm
- : IDENT
+ : FUNC
+ {
+ $$ = decls_lookup($1->data.identifier);
+ lambda_free($1);
+ }
+ | IDENT
{
$$ = make_ident($1->data.identifier);
lambda_free($1);
}
| OBRACE term CBRACE
{ $$ = $2; }
- | I
- { $$ = make_abstraction("x", make_ident("x")); }
- | K
- { $$ = make_abstraction("x", make_abstraction("y", make_ident("x"))); }
- | S
- { $$ = make_abstraction("x", make_abstraction("y", make_abstraction("z",
- make_application(make_application(make_ident("x"), make_ident("y")),
- make_application(make_ident("y"), make_ident("z"))))));
- }
- | T
- { $$ = make_abstraction("x", make_abstraction("y", make_ident("x"))); }
- | F
- { $$ = make_abstraction("x", make_abstraction("y", make_ident("y"))); }
}
free(t);
}
+
+struct lambda *copy(struct lambda *t)
+{
+ struct lambda *c = malloc(sizeof (struct lambda));
+ c->which = t->which;
+ switch(t->which){
+ case lambda_ident:
+ c->data.identifier = strdup(t->data.identifier);
+ break;
+ case lambda_abs:
+ c->data.abstraction.ident = strdup(t->data.abstraction.ident);
+ c->data.abstraction.expr = copy(t->data.abstraction.expr);
+ break;
+ case lambda_app:
+ c->data.application.expr1 = copy(t->data.application.expr1);
+ c->data.application.expr2 = copy(t->data.application.expr2);
+ break;
+ }
+ return c;
+}
+
#include "print.h"
#include "mem.h"
-struct lambda *copy(struct lambda *t)
-{
- struct lambda *c = malloc(sizeof (struct lambda));
- c->which = t->which;
- switch(t->which){
- case lambda_ident:
- c->data.identifier = strdup(t->data.identifier);
- break;
- case lambda_abs:
- c->data.abstraction.ident = strdup(t->data.abstraction.ident);
- c->data.abstraction.expr = copy(t->data.abstraction.expr);
- break;
- case lambda_app:
- c->data.application.expr1 = copy(t->data.application.expr1);
- c->data.application.expr2 = copy(t->data.application.expr2);
- break;
- }
- return c;
-}
-
void subst(char *ident, struct lambda *t1, struct lambda *t2, struct lambda *total)
{
struct lambda cpy;
(*maxdepth)--;
if(total == t)
*total = *t1->data.abstraction.expr;
+ *t = *t1->data.abstraction.expr;
printf("-> ");
lambda_print(total);
lambda_reduce(t, total, maxdepth);