#include "list.h"
#include "sem/scc.h"
-#include "sem/hm.h"
+#include "sem/hm/scheme.h"
+#include "sem/hm/gamma.h"
#include "ast.h"
void type_error(YYLTYPE l, bool d, const char *msg, ...)
#define IN_CAP 50
+struct gamma {
+ int capacity;
+ int fresh;
+ int scope;
+ int nentries;
+ struct gamma_entry *entries;
+};
+struct gamma_entry {
+ int scope;
+ struct ident var;
+ struct scheme *scheme;
+};
+
struct gamma *gamma_init()
{
struct gamma *gamma = xalloc(1, struct gamma);
gamma->scope--;
}
+void gamma_iter(struct gamma *gamma, void *st, void (*iter)(struct ident, struct scheme *, void *))
+{
+ for (int i = 0; i<gamma->nentries; i++)
+ iter(gamma->entries[i].var, gamma->entries[i].scheme, st);
+}
+
struct scheme *gamma_lookup(struct gamma *gamma, struct ident ident)
{
struct scheme *res = NULL;
#include <stdlib.h>
+#include "scheme.h"
#include "../hm.h"
#include "../../ident.h"
-struct gamma {
- int capacity;
- int fresh;
- int scope;
- int nentries;
- struct gamma_entry {
- int scope;
- struct ident var;
- struct scheme *scheme;
- } *entries;
-};
+struct gamma;
+struct gamma_entry;
struct gamma *gamma_init();
void gamma_insert(struct gamma *gamma, struct ident ident, struct scheme *scheme);
void gamma_increment_scope(struct gamma *gamma);
void gamma_decrement_scope(struct gamma *gamma);
+void gamma_iter(struct gamma *gamma, void *st, void (*iter)(struct ident, struct scheme *, void *));
+
struct scheme *gamma_lookup(struct gamma *gamma, struct ident ident);
struct type *gamma_fresh(struct gamma *gamma);
s->nvar = 0;
s->var = xalloc(nftv, struct ident);
for (int i = 0; i<nftv; i++) {
- bool skip = false;
- for (int j = 0; j<gamma->nentries; j++)
- if (ident_cmp(gamma->entries[j].var, ftv[i]) == 0)
- skip = true;
- if (skip)
+ if (gamma_lookup(gamma, ftv[i]) != NULL)
continue;
s->nvar++;
s->var[i] = ident_dup(ftv[i]);
#ifndef SEM_HM_SCHEME_H
#define SEM_HM_SCHEME_H
+#include "gamma.h"
+struct gamma;
+
#include "../hm.h"
#include "../../ident.h"
return scheme;
}
+
+static void giter(struct ident ident, struct scheme *s, void *st)
+{
+ subst_apply_s((struct subst *)st, s);
+ (void)ident;
+}
+
struct gamma *subst_apply_g(struct subst *subst, struct gamma *gamma)
{
- for (int i = 0; i<gamma->nentries; i++)
- subst_apply_s(subst, gamma->entries[i].scheme);
+ gamma_iter(gamma, (void *)subst, giter);
return gamma;
}