From e677a675b057fedcd0acd41f04f5c3749bcd2af0 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 17 May 2018 15:33:39 +0200 Subject: [PATCH] Fix memory leaks and verbosify reduction --- lambda.y | 1 + main.c | 4 ++-- reduce.c | 21 +++++++++++++-------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lambda.y b/lambda.y index cb8259a..1dbcaaf 100644 --- a/lambda.y +++ b/lambda.y @@ -1,3 +1,4 @@ +%define parse.error verbose %{ #include "lambda.h" #include "lambda.tab.h" diff --git a/main.c b/main.c index ac9bea3..22e4d66 100644 --- a/main.c +++ b/main.c @@ -12,14 +12,14 @@ extern struct lambda *result; int main() { int r = yyparse(); - int maxdepth = 100; + int maxdepth = 10000; if(r == 0){ printf(" "); lambda_reduce(result, result, &maxdepth); lambda_print(result, NULL); - lambda_free(result); } yylex_destroy(); + lambda_free(result); decls_free(); return r; } diff --git a/reduce.c b/reduce.c index 429b038..7106743 100644 --- a/reduce.c +++ b/reduce.c @@ -41,17 +41,18 @@ void lambda_alpha(struct lambda *expr, struct lambda *ident) void lambda_beta(struct lambda *ident, struct lambda *body, struct lambda *target, struct lambda *total) { - struct lambda cpy; + struct lambda *cpy; switch(body->which){ case lambda_ident: if(lambda_ident_eq(body, ident)){ lambda_print(total, NULL); printf("β -> "); - cpy = *copy(target); + cpy = copy(target); free(body->data.identifier.ident); if(total == body) - *total = cpy; - *body = cpy; + *total = *cpy; + *body = *cpy; + free(cpy); } break; case lambda_abs: @@ -79,7 +80,7 @@ void lambda_reduce(struct lambda *t, struct lambda *total, int *maxdepth) case lambda_ident: break; case lambda_abs: - lambda_reduce(t->data.abstraction.expr, total, maxdepth); +// lambda_reduce(t->data.abstraction.expr, total, maxdepth); break; case lambda_app: t1 = t->data.application.expr1; @@ -89,10 +90,14 @@ void lambda_reduce(struct lambda *t, struct lambda *total, int *maxdepth) lambda_print(total, t); printf("β -> "); lambda_beta(t1, t1->data.abstraction.expr, t2, total); - (*maxdepth)--; + lambda_free(t2); + t2 = copy(t1->data.abstraction.expr); if(total == t) - *total = *t1->data.abstraction.expr; - *t = *t1->data.abstraction.expr; + *total = *t2; + lambda_free(t1); + *t = *t2; + free(t2); + (*maxdepth)--; lambda_reduce(t, total, maxdepth); } break; -- 2.20.1