From: Mart Lubbers Date: Wed, 17 Mar 2021 15:08:06 +0000 (+0100) Subject: fix some minor bugs in type checking X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=55e60995dfe5cb2595eb48d77757127c83a8d303;p=ccc.git fix some minor bugs in type checking --- diff --git a/sem.c b/sem.c index 181ea84..59e6339 100644 --- a/sem.c +++ b/sem.c @@ -25,6 +25,9 @@ struct ast *sem(struct ast *ast) //Check whether all globals are constant sem_check_constant(ast); + // Move all vardecls to the top of the function + sem_check_vardecls(ast); + // Check that all functions return and mark void sem_check_return(ast); @@ -34,8 +37,5 @@ struct ast *sem(struct ast *ast) // Check that a main function exists with the correct type sem_check_main(ast); - // Move all vardecls to the top of the function - sem_check_vardecls(ast); - return ast; } diff --git a/sem/hm.c b/sem/hm.c index 097010d..7e85878 100644 --- a/sem/hm.c +++ b/sem/hm.c @@ -68,7 +68,7 @@ static struct subst *infer_binop(struct gamma *gamma, struct expr *l, struct exp struct type *a1, struct type *a2, struct type *rt, struct type *sigma) { struct subst *s1 = infer_expr(gamma, l, a1); - struct subst *s2 = infer_expr(subst_apply_g(s1, gamma), r, a2); + struct subst *s2 = infer_expr(subst_apply_g(s1, gamma), r, subst_apply_t(s1, a2)); struct subst *s3 = subst_union(s2, s1); struct subst *s4 = unify(l->loc, subst_apply_t(s3, sigma), rt); return subst_union(s4, s3); @@ -78,7 +78,7 @@ static struct subst *infer_unop(struct gamma *gamma, struct expr *e, struct type *a, struct type *rt, struct type *sigma) { struct subst *s1 = infer_expr(gamma, e, a); - struct subst *s2 = unify(e->loc, subst_apply_t(s1, sigma), rt); + struct subst *s2 = unify(e->loc, subst_apply_t(s1, sigma), subst_apply_t(s1, rt)); return subst_union(s2, s1); } @@ -298,7 +298,10 @@ struct subst *infer_stmt(struct gamma *gamma, struct stmt *stmt, struct type *ty return s0; case svardecl: f1 = gamma_fresh(gamma); - s0 = infer_expr(gamma, stmt->data.svardecl->expr, f1); + if (stmt->data.svardecl->expr != NULL) + s0 = infer_expr(gamma, stmt->data.svardecl->expr, f1); + else + s0 = subst_id(); if (stmt->data.svardecl->type != NULL) { s1 = unify(stmt->loc, f1, stmt->data.svardecl->type); type_free(f1); diff --git a/sem/type.c b/sem/type.c index 5e04324..e540d5f 100644 --- a/sem/type.c +++ b/sem/type.c @@ -107,6 +107,7 @@ static struct vardecl *type_vardecl(struct gamma *gamma, struct vardecl *vardecl vardecl->type = subst_apply_t(s, t); gamma_insert(gamma, ident_str(vardecl->ident), scheme_create(vardecl->type)); + patch_overload_expr(s, vardecl->expr); subst_free(s); return vardecl;