From 537fb5b20744f4e94695e2f606b3a4dec9a1a28b Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Wed, 24 Feb 2021 12:54:36 +0100 Subject: [PATCH] remove memory leaks, fix function inferring --- in.txt | 1 - sem.c | 10 ++++++---- sem/hm.c | 14 +++++++++++--- sem/hm/gamma.c | 5 +++-- sem/hm/scheme.c | 3 ++- type.c | 4 +--- 6 files changed, 23 insertions(+), 14 deletions(-) delete mode 100644 in.txt diff --git a/in.txt b/in.txt deleted file mode 100644 index 564415e..0000000 --- a/in.txt +++ /dev/null @@ -1 +0,0 @@ -fun (x) { return fun(x); } diff --git a/sem.c b/sem.c index 7fd0c7a..9df5bbf 100644 --- a/sem.c +++ b/sem.c @@ -69,13 +69,15 @@ struct ast *sem(struct ast *ast) break; case dfundecl: { struct type *f1 = gamma_fresh(gamma); - struct subst *s = infer_fundecl(gamma, ast->decls[i]->data.dfun, f1); + struct subst *s = infer_fundecl(gamma, + ast->decls[i]->data.dfun, f1); f1 = subst_apply_t(s, f1); - gamma_insert(gamma, ast->decls[i]->data.dfun->ident, scheme_generalise(gamma, subst_apply_t(s, f1))); -// type_free(f1); - subst_free(s); + gamma_insert(gamma, ast->decls[i]->data.dfun->ident, + scheme_generalise(gamma, f1)); gamma_print(gamma, stderr); fprintf(stderr, "done\n"); + subst_free(s); + type_free(f1); break; } case dcomp: diff --git a/sem/hm.c b/sem/hm.c index ebc21dd..8de1088 100644 --- a/sem/hm.c +++ b/sem/hm.c @@ -243,13 +243,16 @@ struct subst *infer_stmt(struct gamma *gamma, struct stmt *stmt, struct type *ty type_print(type, stderr); fprintf(stderr, "\n"); -// struct subst *s1, *s2; + struct subst *s1;//, *s2; // struct type *f1, *f2, *f3; // struct scheme *s; switch (stmt->type) { case sassign: break; case sif: + s1 = infer_expr(gamma, stmt->data.sif.pred, &tybool); + subst_apply_g(s1, gamma); + subst_free(s1); break; case sreturn: return infer_expr(gamma, stmt->data.sreturn, type); @@ -319,11 +322,16 @@ struct subst *infer_fundecl(struct gamma *gamma, struct fundecl *fundecl, struct subst_free(s2); } - for (int i = 0; inargs; i++) - fundecl->atypes[i] = subst_apply_t(s, fundecl->atypes[i]); + for (int i = 0; inargs; i++) { + struct type *t = subst_apply_t(s, fundecl->atypes[i]); + type_free(fundecl->atypes[i]); + fundecl->atypes[i] = t; + } fundecl->rtype = subst_apply_t(s, fundecl->rtype); + ftype = subst_apply_t(s, ftype); struct subst *r = unify(fundecl->loc, fty, ftype); type_free(ftype); + subst_free(s); return r; } diff --git a/sem/hm/gamma.c b/sem/hm/gamma.c index 4202154..a5b46c5 100644 --- a/sem/hm/gamma.c +++ b/sem/hm/gamma.c @@ -23,8 +23,9 @@ void gamma_insert(struct gamma *gamma, char *ident, struct scheme *scheme) } } gamma->nschemes++; - gamma->vars = realloc(gamma->vars, gamma->nschemes*sizeof(char *)); - gamma->schemes = realloc(gamma->schemes, + gamma->vars = safe_realloc(gamma->vars, + gamma->nschemes*sizeof(char *)); + gamma->schemes = safe_realloc(gamma->schemes, gamma->nschemes*sizeof(struct scheme *)); gamma->vars[gamma->nschemes-1] = safe_strdup(ident); gamma->schemes[gamma->nschemes-1] = scheme; diff --git a/sem/hm/scheme.c b/sem/hm/scheme.c index 33ff121..8d65075 100644 --- a/sem/hm/scheme.c +++ b/sem/hm/scheme.c @@ -43,8 +43,9 @@ struct scheme *scheme_generalise(struct gamma *gamma, struct type *t) if (skip) continue; s->nvar++; - s->var[i] = ftv[i]; + s->var[i] = safe_strdup(ftv[i]); } + free(ftv); return s; } diff --git a/type.c b/type.c index 39d67b5..18c8151 100644 --- a/type.c +++ b/type.c @@ -178,9 +178,7 @@ void type_ftv(struct type *r, int *nftv, char ***ftv) for (int i = 0; i<*nftv; i++) if (strcmp((*ftv)[i], r->data.tvar) == 0) return; - *ftv = realloc(*ftv, (*nftv+1)*sizeof(char *)); - if (*ftv == NULL) - perror("realloc"); + *ftv = safe_realloc(*ftv, (*nftv+1)*sizeof(char *)); (*ftv)[(*nftv)++] = r->data.tvar; break; default: -- 2.20.1