Fix reduction, add declarations
authorMart Lubbers <mart@martlubbers.net>
Wed, 16 May 2018 11:50:03 +0000 (13:50 +0200)
committerMart Lubbers <mart@martlubbers.net>
Wed, 16 May 2018 11:50:03 +0000 (13:50 +0200)
lambda.h
lambda.l
lambda.y
lambda.yy.h
main.c
mem.c
mem.h
reduce.c

index 2495f9d..a92a2bb 100644 (file)
--- a/lambda.h
+++ b/lambda.h
@@ -21,6 +21,12 @@ struct lambda {
        } data;
 };
 
+struct decllist {
+       struct decllist *next;
+       char *ident;
+       struct lambda *value;
+};
+
 struct lambda *make_ident(char *);
 struct lambda *make_abstraction(char *, struct lambda *);
 struct lambda *make_application(struct lambda *, struct lambda *);
index b37d888..9fe4ef4 100644 (file)
--- a/lambda.l
+++ b/lambda.l
 %%
 
 [ \t\n]
+=         return ASSIGN;
+;         return SEMICOLON;
 (\\|λ)    return LAMBDA;
 \.        return DOT;
 \(        return OBRACE;
 \)        return CBRACE;
-I         return I;
-K         return K;
-S         return S;
-F         return F;
-T         return T;
-[a-zA-Z]+ yylval = make_ident(strdup(yytext)); return IDENT;
+[a-z]+    yylval = make_ident(strdup(yytext)); return IDENT;
+[A-Z]+    yylval = make_ident(strdup(yytext)); return FUNC;
 
 %%
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"))); }
index 63c1936..f0efc72 100644 (file)
@@ -466,7 +466,7 @@ extern int yylex (void);
 #undef yyTABLES_NAME
 #endif
 
-#line 24 "lambda.l"
+#line 22 "lambda.l"
 
 
 #line 473 "lambda.yy.h"
diff --git a/main.c b/main.c
index 1899d67..1c0be0f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -9,7 +9,7 @@
 
 extern struct lambda *result;
 
-int main(int argc, char **argv)
+int main()
 {
        int r = yyparse();
        int maxdepth = 1000;
diff --git a/mem.c b/mem.c
index 19c5c8e..1e112a8 100644 (file)
--- a/mem.c
+++ b/mem.c
@@ -20,3 +20,24 @@ void lambda_free(struct lambda *t)
        }
        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;
+}
+
diff --git a/mem.h b/mem.h
index 0cbce0d..1964654 100644 (file)
--- a/mem.h
+++ b/mem.h
@@ -4,4 +4,5 @@
 #include "lambda.h"
 
 void lambda_free(struct lambda *);
+struct lambda *copy(struct lambda *);
 #endif
index 79cde99..8537a20 100644 (file)
--- a/reduce.c
+++ b/reduce.c
@@ -5,26 +5,6 @@
 #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;
@@ -72,6 +52,7 @@ void lambda_reduce(struct lambda *t, struct lambda *total, int *maxdepth)
                        (*maxdepth)--;
                        if(total == t)
                                *total = *t1->data.abstraction.expr;
+                       *t = *t1->data.abstraction.expr;
                        printf("-> ");
                        lambda_print(total);
                        lambda_reduce(t, total, maxdepth);