-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.ha
53 lines (48 loc) · 1.3 KB
/
context.ha
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
// slightly more freeform. It does not own any of the useful information it is
// used to retrieve. It also forms a linked list of contexts, allowing for
// nesting and overriding.
type TypContext = struct {
ident_map: [](IdentId, nullable *Typ),
pool: *TypPool,
outer: nullable *TypContext,
};
fn drop_context(context: *TypContext) void = {
free(context.ident_map);
return;
};
fn lookup(context: *TypContext, ident: IdentId) nullable *Typ = {
for (let i : IdentId = 0; i < len(&context.ident_map); i += 1) {
if (ident != context.ident_map[i].0) {
continue;
};
return context.ident_map[i].1;
};
match (context.outer) {
case null =>
return null;
case let x : *TypContext =>
return lookup(x, ident);
};
};
type RedefinitionError = !void;
fn insert_context(context: *TypContext, ident: IdentId, typ: *Typ)
(void | RedefinitionError) = {
for (let i : size = 0; i < len(&context.ident_map); i += 1) {
let slot = context.ident_map[i];
if (slot.0 == ident) {
return RedefinitionError;
};
};
append(context.ident_map, (ident, typ));
return;
};
fn remove_context(context: *TypContext, ident: IdentId) void = {
for (let i : size = 0; i < len(&context.ident_map); i += 1) {
let slot = context.ident_map[i];
if (slot.0 == ident) {
delete(context.ident_map[i]);
break;
};
};
return;
};