From d0cce0e640ddf4aced3cd0786667c6669343f9d7 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Wed, 23 May 2018 10:40:49 +0200 Subject: [PATCH] fix memory leak --- lambda.y | 2 +- reduce.c | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lambda.y b/lambda.y index b0fe73a..3aff9c1 100644 --- a/lambda.y +++ b/lambda.y @@ -148,7 +148,7 @@ lambda struct lambda *t = $1; printf(" "); for(unsigned int i = 0; i<999; i++) - if(!lambda_reduce(&t, &t, true)) + if(!lambda_reduce(&t, &t, false)) break; lambda_print(t, NULL); putchar('\n'); diff --git a/reduce.c b/reduce.c index 91299fc..bbe52de 100644 --- a/reduce.c +++ b/reduce.c @@ -6,20 +6,15 @@ #include "print.h" #include "mem.h" -#define binder(t) (*t)->data.identifier.binding #define lhs(t) (*t)->data.application.expr1 #define rhs(t) (*t)->data.application.expr2 #define bdy(t) (*t)->data.abstraction.expr void lambda_beta(struct lambda *binding, struct lambda **body, struct lambda *target, struct lambda *total) { -// lambda_print(*body, NULL); -// printf(" [%s=", binding->data.abstraction.ident); -// lambda_print(target, NULL); -// printf("]\n"); switch((*body)->which){ case lambda_ident: - if(binder(body) == binding){ + if((*body)->data.identifier.binding == binding){ lambda_free(*body); *body = target; target->refcount++; @@ -47,8 +42,10 @@ bool lambda_reduce(struct lambda **t, struct lambda **total, bool applicative) if(lhs(t)->data.abstraction.strict) lambda_reduce(&rhs(t), total, true); - if(lhs(t) == rhs(t)) + if(lhs(t) == rhs(t)){ + lhs(t)->refcount--; rhs(t) = copy(rhs(t)); + } //In this abstraction we substitute the result with the rhs lambda_print(*total, *t); -- 2.20.1