-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinference.h
86 lines (82 loc) · 1.58 KB
/
inference.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* requires stdint.h */
#define MAX_ARGS 2
typedef enum {
OK = 0,
UNHANDLED_SYNTAX_NODE = -1,
UNDEFINED_SYMBOL = -2,
RECURSIVE_UNIFICATION = -3,
TYPE_MISMATCH = -4,
UNIFY_ERROR = -5,
LOCAL_SCOPE_EXCEEDED = -6,
OUT_OF_TYPES = -7,
MAX_RECURSION_EXCEEDED = -8,
} error_t;
typedef enum {
VARIABLE = 0,
OPERATOR = 1,
} type_t;
typedef struct Type {
char *name;
union {
struct Type *instance;
struct {
struct Type *types[MAX_ARGS];
uint32_t args : 2;
};
};
uint32_t generic : 1;
uint32_t type : 1;
uint32_t id : 28;
} Type;
typedef enum {
IDENTIFIER = 0,
APPLY = 1,
LAMBDA = 2,
LET = 3,
LETREC = 4
} term_t;
typedef struct Term {
union {
struct {
char *name;
};
struct {
struct Term *fn;
struct Term *arg;
};
struct {
char *v;
struct Term *defn;
struct Term *body;
};
};
term_t type;
} Term;
typedef struct Env {
char *name;
Type *node;
struct Env *next;
} Env;
typedef struct Inferencer {
Type *types;
Type *result;
char *error_msg;
error_t error;
int use;
int cap;
int locals;
} Inferencer;
error_t analyze(Inferencer *ctx, Term *node, Env *env);
Type *get_result(Inferencer *ctx);
Inferencer make_ctx(Type *, int); /* TODO: Fix return type to be int */
Type *make_type(Inferencer *);
Type *Function(Inferencer *, Type *, Type *);
Type *Var(Inferencer *);
Type *Err(Inferencer *, type_t, char *);
Type *Integer(Inferencer *);
Type *Bool(Inferencer *);
Type *Apply(Inferencer *);
Type *copy_generic(Inferencer *ctx, Type *v);
void var_is(Inferencer *ctx, Type *v, Type *i);
void print(Term *, Type *);
void print_error(Term *, error_t, char *);