remove memory leaks, fix function inferring
authorMart Lubbers <mart@martlubbers.net>
Wed, 24 Feb 2021 11:54:36 +0000 (12:54 +0100)
committerMart Lubbers <mart@martlubbers.net>
Wed, 24 Feb 2021 11:54:36 +0000 (12:54 +0100)
in.txt [deleted file]
sem.c
sem/hm.c
sem/hm/gamma.c
sem/hm/scheme.c
type.c

diff --git a/in.txt b/in.txt
deleted file mode 100644 (file)
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 (file)
--- 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:
index ebc21dd..8de1088 100644 (file)
--- 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; i<fundecl->nargs; i++)
-               fundecl->atypes[i] = subst_apply_t(s, fundecl->atypes[i]);
+       for (int i = 0; i<fundecl->nargs; 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;
 }
index 4202154..a5b46c5 100644 (file)
@@ -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;
index 33ff121..8d65075 100644 (file)
@@ -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 (file)
--- 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: