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)
-#include <stdlib.h>
-#include <stdio.h>
#include <string.h>
-#include "array.h"
-#include "util.h"
#include "ast.h"
-#include "type.h"
-#include "parse.h"
const char *binop_str[] = {
[binor] = "||", [binand] = "&&", [eq] = "==", [neq] = "!=",
#include <stdio.h>
#include "ast.h"
+#include "type.h"
/** Datatype that stores the overloading information */
struct overload {
#include <string.h>
#include <stdlib.h>
-#include "../ast.h"
-#include "../sem.h"
-#include "../gen.h"
+#include "c.h"
static const char *fun_name(const char *name)
{
%{
#include <stdio.h>
-#include "array.h"
#include "ast.h"
#include "parse.h"
#include <string.h>
#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)
{
#include "../ast.h"
#include "hm/gamma.h"
#include "hm/subst.h"
-#include "hm/scheme.h"
#include "../ident.h"
/**
#include <stdlib.h>
#include <string.h>
-#include "../hm.h"
+#include "gamma.h"
+#include "subst.h"
+#include "../../util.h"
+#include "../../type.h"
#define IN_CAP 50
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; i<sch->nvar; 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; i<nftv; i++) {
+ if (gamma_free_in(gamma, ftv[i]))
+ continue;
+ s->nvar++;
+ 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; i<scheme->nvar; 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; i<scheme->nvar; 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; i<scheme->nvar; i++)
+ ident_free(scheme->var[i]);
+ free(scheme->var);
+ free(scheme);
+}
#define SEM_HM_GAMMA_H
#include <stdlib.h>
-
-#include "scheme.h"
-#include "../hm.h"
-#include "../../ident.h"
+#include <stdbool.h>
/** 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
*
*/
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
+++ /dev/null
-#include <string.h>
-#include <stdlib.h>
-
-#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; i<sch->nvar; 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; i<nftv; i++) {
- if (gamma_free_in(gamma, ftv[i]))
- continue;
- s->nvar++;
- 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; i<scheme->nvar; 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; i<scheme->nvar; 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; i<scheme->nvar; i++)
- ident_free(scheme->var[i]);
- free(scheme->var);
- free(scheme);
-}
+++ /dev/null
-#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
#include <stdlib.h>
#include <stdint.h>
-#include "../hm.h"
-#include "../../list.h"
+#include "subst.h"
+#include "gamma.h"
+#include "../../util.h"
struct subst {
size_t nvar;
#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;
#include <stddef.h>
#include "../ast.h"
-#include "../list.h"
#include "../sem.h"
#ifndef min
#define TYPE_H
#include <stdio.h>
-#include "ast.h"
+#include <stdbool.h>
#include "ident.h"
/**