From: Mart Lubbers Date: Wed, 2 Feb 2022 10:39:55 +0000 (+0100) Subject: cleanup imports X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=refs%2Fheads%2Fmain;p=ccc.git cleanup imports --- diff --git a/src/Makefile b/src/Makefile index b43dc7c..b1da661 100644 --- a/src/Makefile +++ b/src/Makefile @@ -6,7 +6,7 @@ LFLAGS+=--header-file=scan.h OBJECTS:=array.o scan.o parse.o ast.o type.o util.o ident.o\ $(addprefix gen,.o /c.o /ssm.o)\ $(addprefix sem,.o /main.o /constant.o /return.o /scc.o /type.o /vardecl.o\ - $(addprefix /hm, .o /gamma.o /subst.o /scheme.o)) + $(addprefix /hm, .o /gamma.o /subst.o)) all: splc splc: $(OBJECTS) diff --git a/src/ast.c b/src/ast.c index dca36d4..1350bd5 100644 --- a/src/ast.c +++ b/src/ast.c @@ -1,12 +1,6 @@ -#include -#include #include -#include "array.h" -#include "util.h" #include "ast.h" -#include "type.h" -#include "parse.h" const char *binop_str[] = { [binor] = "||", [binand] = "&&", [eq] = "==", [neq] = "!=", diff --git a/src/gen.h b/src/gen.h index dc9c723..a19f6fa 100644 --- a/src/gen.h +++ b/src/gen.h @@ -4,6 +4,7 @@ #include #include "ast.h" +#include "type.h" /** Datatype that stores the overloading information */ struct overload { diff --git a/src/gen/c.c b/src/gen/c.c index 0e0340b..01241f8 100644 --- a/src/gen/c.c +++ b/src/gen/c.c @@ -2,9 +2,7 @@ #include #include -#include "../ast.h" -#include "../sem.h" -#include "../gen.h" +#include "c.h" static const char *fun_name(const char *name) { diff --git a/src/parse.y b/src/parse.y index 7e29cde..4fa0a20 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1,7 +1,6 @@ %{ #include -#include "array.h" #include "ast.h" #include "parse.h" diff --git a/src/sem/hm.c b/src/sem/hm.c index e9cef4d..a9cf6fc 100644 --- a/src/sem/hm.c +++ b/src/sem/hm.c @@ -2,11 +2,7 @@ #include #include "hm.h" -#include "hm/subst.h" -#include "hm/gamma.h" -#include "hm/scheme.h" #include "../sem.h" -#include "../ast.h" static bool occurs_check(struct ident ident, struct type *r) { diff --git a/src/sem/hm.h b/src/sem/hm.h index fe7bedd..17ed193 100644 --- a/src/sem/hm.h +++ b/src/sem/hm.h @@ -4,7 +4,6 @@ #include "../ast.h" #include "hm/gamma.h" #include "hm/subst.h" -#include "hm/scheme.h" #include "../ident.h" /** diff --git a/src/sem/hm/gamma.c b/src/sem/hm/gamma.c index 9134dca..6a884a0 100644 --- a/src/sem/hm/gamma.c +++ b/src/sem/hm/gamma.c @@ -1,7 +1,10 @@ #include #include -#include "../hm.h" +#include "gamma.h" +#include "subst.h" +#include "../../util.h" +#include "../../type.h" #define IN_CAP 50 @@ -137,3 +140,82 @@ void gamma_free(struct gamma *gamma) free(gamma->entries); free(gamma); } + +struct type *scheme_instantiate(struct gamma *gamma, struct scheme *sch) +{ + struct subst *s = subst_id(); + struct type *t; + for (int i = 0; invar; i++) { + t = gamma_fresh(gamma); + subst_insert(s, sch->var[i], t); + type_free(t); + } + + t = subst_apply_t(s, type_dup(sch->type)); + subst_free(s); + return t; +} + +struct scheme *scheme_create(struct type *t) +{ + struct scheme *s = xalloc(1, struct scheme); + s->type = type_dup(t); + s->nvar = 0; + s->var = NULL; + return s; +} + +struct scheme *scheme_generalise(struct gamma *gamma, struct type *t) +{ + struct scheme *s = xalloc(1, struct scheme); + int nftv = 0; + struct ident *ftv = NULL; + type_ftv(t, &nftv, &ftv); + + s->type = type_dup(t); + s->nvar = 0; + s->var = xalloc(nftv, struct ident); + for (int i = 0; invar++; + s->var[i] = ident_dup(ftv[i]); + } + free(ftv); + return s; +} + +bool scheme_free_in(struct scheme *scheme, struct ident ident) +{ + for (int i = 0; invar; i++) + if (ident_cmp(scheme->var[i], ident) == 0) + return false; + return type_free_in(scheme->type, ident); +} + +void scheme_print(struct scheme *scheme, FILE *out) +{ + if (scheme == NULL) { + safe_fprintf(out, "NULLSCHEME"); + return; + } + if (scheme->nvar > 0) { + safe_fprintf(out, "A."); + for (int i = 0; invar; i++) { + if (i > 0) + safe_fprintf(out, " "); + ident_print(scheme->var[i], out); + } + safe_fprintf(out, ": "); + } + type_print(scheme->type, out); +} + +void scheme_free(struct scheme *scheme) +{ + type_free(scheme->type); + for (int i = 0; invar; i++) + ident_free(scheme->var[i]); + free(scheme->var); + free(scheme); +} diff --git a/src/sem/hm/gamma.h b/src/sem/hm/gamma.h index 08424a2..9a46d39 100644 --- a/src/sem/hm/gamma.h +++ b/src/sem/hm/gamma.h @@ -2,16 +2,24 @@ #define SEM_HM_GAMMA_H #include - -#include "scheme.h" -#include "../hm.h" -#include "../../ident.h" +#include /** abstract type representing the environment */ struct gamma; + /** abstract type representing an entry in the environment */ struct gamma_entry; +/** Definition of a type scheme */ +struct scheme { + struct type *type; /** Type */ + int nvar; /** Number of quantified type variables */ + struct ident *var; /** array of quantified type variables */ +}; + +#include "../../ident.h" + + /** * Initialise an empty environment * @@ -97,4 +105,51 @@ void gamma_print(struct gamma *gamma, FILE *out); */ void gamma_free(struct gamma *gamma); +/** + * Instantiate a type scheme, i.e. replace the quantified type variables by + * fresh type variables. + * + * @param gamma environment + * @param s scheme + * @result type + */ +struct type *scheme_instantiate(struct gamma *gamma, struct scheme *s); +/** + * Create a scheme from a type and assume no type variable is quantified. + * + * @param t type + * @result scheme + */ +struct scheme *scheme_create(struct type *t); +/** + * Create a scheme by generalising a type, i.e. quantify all free type variables + * that are not free in gamma. + * + * @param gamma environment + * @param t type + * @result scheme + */ +struct scheme *scheme_generalise(struct gamma *gamma, struct type *t); +/** + * Check whether \p ident is free in the type scheme. + * + * @param scheme type scheme + * @param ident type variable + * @result result + */ +bool scheme_free_in(struct scheme *scheme, struct ident ident); +/** + * Print a type scheme. + * + * @param scheme type scheme + * @param out output stream + */ +void scheme_print(struct scheme *scheme, FILE *out); +/** + * Free a type scheme. + * + * @param scheme type scheme + */ +void scheme_free(struct scheme *scheme); + #endif diff --git a/src/sem/hm/scheme.c b/src/sem/hm/scheme.c deleted file mode 100644 index abb28b9..0000000 --- a/src/sem/hm/scheme.c +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include - -#include "../hm.h" - -struct type *scheme_instantiate(struct gamma *gamma, struct scheme *sch) -{ - struct subst *s = subst_id(); - struct type *t; - for (int i = 0; invar; i++) { - t = gamma_fresh(gamma); - subst_insert(s, sch->var[i], t); - type_free(t); - } - - t = subst_apply_t(s, type_dup(sch->type)); - subst_free(s); - return t; -} - -struct scheme *scheme_create(struct type *t) -{ - struct scheme *s = xalloc(1, struct scheme); - s->type = type_dup(t); - s->nvar = 0; - s->var = NULL; - return s; -} - -struct scheme *scheme_generalise(struct gamma *gamma, struct type *t) -{ - struct scheme *s = xalloc(1, struct scheme); - int nftv = 0; - struct ident *ftv = NULL; - type_ftv(t, &nftv, &ftv); - - s->type = type_dup(t); - s->nvar = 0; - s->var = xalloc(nftv, struct ident); - for (int i = 0; invar++; - s->var[i] = ident_dup(ftv[i]); - } - free(ftv); - return s; -} - -bool scheme_free_in(struct scheme *scheme, struct ident ident) -{ - for (int i = 0; invar; i++) - if (ident_cmp(scheme->var[i], ident) == 0) - return false; - return type_free_in(scheme->type, ident); -} - -void scheme_print(struct scheme *scheme, FILE *out) -{ - if (scheme == NULL) { - safe_fprintf(out, "NULLSCHEME"); - return; - } - if (scheme->nvar > 0) { - safe_fprintf(out, "A."); - for (int i = 0; invar; i++) { - if (i > 0) - safe_fprintf(out, " "); - ident_print(scheme->var[i], out); - } - safe_fprintf(out, ": "); - } - type_print(scheme->type, out); -} - -void scheme_free(struct scheme *scheme) -{ - type_free(scheme->type); - for (int i = 0; invar; i++) - ident_free(scheme->var[i]); - free(scheme->var); - free(scheme); -} diff --git a/src/sem/hm/scheme.h b/src/sem/hm/scheme.h deleted file mode 100644 index 57dc0d7..0000000 --- a/src/sem/hm/scheme.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef SEM_HM_SCHEME_H -#define SEM_HM_SCHEME_H - -#include "gamma.h" -struct gamma; - -#include "../hm.h" -#include "../../ident.h" - -/** Definition of a type scheme */ -struct scheme { - struct type *type; /** Type */ - int nvar; /** Number of quantified type variables */ - struct ident *var; /** array of quantified type variables */ -}; - -/** - * Instantiate a type scheme, i.e. replace the quantified type variables by - * fresh type variables. - * - * @param gamma environment - * @param s scheme - * @result type - */ -struct type *scheme_instantiate(struct gamma *gamma, struct scheme *s); -/** - * Create a scheme from a type and assume no type variable is quantified. - * - * @param t type - * @result scheme - */ -struct scheme *scheme_create(struct type *t); -/** - * Create a scheme by generalising a type, i.e. quantify all free type variables - * that are not free in gamma. - * - * @param gamma environment - * @param t type - * @result scheme - */ -struct scheme *scheme_generalise(struct gamma *gamma, struct type *t); -/** - * Check whether \p ident is free in the type scheme. - * - * @param scheme type scheme - * @param ident type variable - * @result result - */ -bool scheme_free_in(struct scheme *scheme, struct ident ident); -/** - * Print a type scheme. - * - * @param scheme type scheme - * @param out output stream - */ -void scheme_print(struct scheme *scheme, FILE *out); -/** - * Free a type scheme. - * - * @param scheme type scheme - */ -void scheme_free(struct scheme *scheme); - -#endif diff --git a/src/sem/hm/subst.c b/src/sem/hm/subst.c index c46756f..6dadebc 100644 --- a/src/sem/hm/subst.c +++ b/src/sem/hm/subst.c @@ -2,8 +2,9 @@ #include #include -#include "../hm.h" -#include "../../list.h" +#include "subst.h" +#include "gamma.h" +#include "../../util.h" struct subst { size_t nvar; diff --git a/src/sem/hm/subst.h b/src/sem/hm/subst.h index 101dbff..6b53d62 100644 --- a/src/sem/hm/subst.h +++ b/src/sem/hm/subst.h @@ -1,9 +1,8 @@ #ifndef SEM_HM_SUBST_H #define SEM_HM_SUBST_H -#include "../../ast.h" -#include "../hm.h" #include "../../ident.h" +#include "../../type.h" /** Abstract type representing a substitution, a map from type variable to type */ struct subst; diff --git a/src/sem/scc.c b/src/sem/scc.c index cffc7e0..41e7082 100644 --- a/src/sem/scc.c +++ b/src/sem/scc.c @@ -3,7 +3,6 @@ #include #include "../ast.h" -#include "../list.h" #include "../sem.h" #ifndef min diff --git a/src/type.h b/src/type.h index 35a0d12..7cbaaa0 100644 --- a/src/type.h +++ b/src/type.h @@ -2,7 +2,7 @@ #define TYPE_H #include -#include "ast.h" +#include #include "ident.h" /**