Fix reduction, add declarations
[lambda.git] / lambda.y
index 4d47f9b..e65a644 100644 (file)
--- a/lambda.y
+++ b/lambda.y
@@ -4,6 +4,7 @@
 #include "mem.h"
 
 struct lambda *result;
+struct decllist *decls = NULL;
 extern int yylex();
 
 int yydebug=1;
@@ -48,24 +49,54 @@ struct lambda *make_application(struct lambda *t1, struct lambda *t2)
        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);
@@ -77,16 +108,3 @@ appterm
                }
        | 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"))); }