From dd4d699dd805d6c6e68b788e054ada7213262310 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 17 May 2018 12:53:23 +0200 Subject: [PATCH] Started with actually freeing terms that are not used anymore --- lambda.h | 1 + lambda.l | 4 ++-- lambda.y | 20 ++++++++++++++++++-- main.c | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lambda.h b/lambda.h index 60bc284..3409e7f 100644 --- a/lambda.h +++ b/lambda.h @@ -34,5 +34,6 @@ struct decllist { struct lambda *make_ident(char *); struct lambda *make_abstraction(char *, struct lambda *); struct lambda *make_application(struct lambda *, struct lambda *); +void decls_free(); #define YYSTYPE struct lambda * #endif diff --git a/lambda.l b/lambda.l index 9fe4ef4..689b5a4 100644 --- a/lambda.l +++ b/lambda.l @@ -16,7 +16,7 @@ \. return DOT; \( return OBRACE; \) return CBRACE; -[a-z]+ yylval = make_ident(strdup(yytext)); return IDENT; -[A-Z]+ yylval = make_ident(strdup(yytext)); return FUNC; +[a-z]+ yylval = make_ident(yytext); return IDENT; +[A-Z]+ yylval = make_ident(yytext); return FUNC; %% diff --git a/lambda.y b/lambda.y index eda3c59..cb8259a 100644 --- a/lambda.y +++ b/lambda.y @@ -38,6 +38,7 @@ struct lambda *make_abstraction(char *i, struct lambda *t) struct lambda *r = make_lambda(); r->which = lambda_abs; r->data.abstraction.ident = strdup(i); + r->data.abstraction.revision = 0; r->data.abstraction.expr = t; return r; } @@ -55,7 +56,7 @@ void decls_prepend(char *ident, struct lambda *value) { struct decllist *head = malloc(sizeof (struct decllist)); head->next = decls; - head->ident = ident; + head->ident = strdup(ident); head->value = value; printf("Declared %s as ", ident); lambda_print(value, NULL); @@ -73,6 +74,18 @@ struct lambda *decls_lookup(char *ident) return make_ident(ident); } +void decls_free() +{ + struct decllist *t; + while(decls != NULL){ + free(decls->ident); + lambda_free(decls->value); + t = decls->next; + free(decls); + decls = t; + } +} + %} %token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN @@ -85,7 +98,10 @@ lambda { result = $$; } decl : FUNC ASSIGN term SEMICOLON - { decls_prepend($1->data.identifier.ident, $3); } + { + decls_prepend($1->data.identifier.ident, $3); + lambda_free($1); + } term : term appterm { $$ = make_application($1, $2); } diff --git a/main.c b/main.c index 907336b..ac9bea3 100644 --- a/main.c +++ b/main.c @@ -20,5 +20,6 @@ int main() lambda_free(result); } yylex_destroy(); + decls_free(); return r; } -- 2.20.1