From: Mart Lubbers Date: Wed, 23 May 2018 10:03:50 +0000 (+0200) Subject: remove smart allocations, back to bruteforce X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=09ae4fb60f7e45e9dd91f46cda60c20ed0b7b488;p=lambda.git remove smart allocations, back to bruteforce --- diff --git a/lambda.h b/lambda.h index 54c23e3..d9921d6 100644 --- a/lambda.h +++ b/lambda.h @@ -9,7 +9,6 @@ enum lambda_which {lambda_ident, lambda_abs, lambda_app}; struct lambda { enum lambda_which which; - unsigned int refcount; union { struct { char *ident; diff --git a/lambda.y b/lambda.y index 3aff9c1..9cde7e1 100644 --- a/lambda.y +++ b/lambda.y @@ -31,9 +31,7 @@ int yywrap() struct lambda *make_lambda() { - struct lambda *t = malloc(sizeof (struct lambda)); - t->refcount = 1; - return t; + return malloc(sizeof (struct lambda)); } struct lambda *make_ident(char *i) @@ -148,7 +146,7 @@ lambda struct lambda *t = $1; printf(" "); for(unsigned int i = 0; i<999; i++) - if(!lambda_reduce(&t, &t, false)) + if(!lambda_reduce(&t, &t, true)) break; lambda_print(t, NULL); putchar('\n'); diff --git a/mem.c b/mem.c index 4d73188..270679b 100644 --- a/mem.c +++ b/mem.c @@ -5,24 +5,20 @@ void lambda_free(struct lambda *t) { if(t != NULL){ - if(t->refcount == 1){ - switch(t->which){ - case lambda_ident: - free(t->data.identifier.ident); - break; - case lambda_abs: - free(t->data.abstraction.ident); - lambda_free(t->data.abstraction.expr); - break; - case lambda_app: - lambda_free(t->data.application.expr1); - lambda_free(t->data.application.expr2); - break; - } - free(t); - } else { - t->refcount--; + switch(t->which){ + case lambda_ident: + free(t->data.identifier.ident); + break; + case lambda_abs: + free(t->data.abstraction.ident); + lambda_free(t->data.abstraction.expr); + break; + case lambda_app: + lambda_free(t->data.application.expr1); + lambda_free(t->data.application.expr2); + break; } + free(t); } } diff --git a/reduce.c b/reduce.c index bbe52de..7bd170c 100644 --- a/reduce.c +++ b/reduce.c @@ -16,8 +16,7 @@ void lambda_beta(struct lambda *binding, struct lambda **body, struct lambda *ta case lambda_ident: if((*body)->data.identifier.binding == binding){ lambda_free(*body); - *body = target; - target->refcount++; + *body = copy(target); } break; case lambda_abs: @@ -42,17 +41,11 @@ 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)){ - lhs(t)->refcount--; - rhs(t) = copy(rhs(t)); - } - //In this abstraction we substitute the result with the rhs lambda_print(*total, *t); lambda_beta(lhs(t), &lhs(t)->data.abstraction.expr, rhs(t), *total); - struct lambda *newt = lhs(t)->data.abstraction.expr; - lhs(t)->data.abstraction.expr->refcount++; + struct lambda *newt = copy(lhs(t)->data.abstraction.expr); lambda_free(*t); *t = newt;