-
Notifications
You must be signed in to change notification settings - Fork 152
/
annotator.go
43 lines (39 loc) · 961 Bytes
/
annotator.go
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
package semantic
// Annotate walks a graph and assigns each expression a type variable annotation.
func Annotate(node Node) Annotator {
annotator := Annotator{
f: new(fresher),
annotations: make(map[Node]annotation),
}
Walk(annotator, node)
return annotator
}
// Annotator implements Visitor to walk the graph.
type Annotator struct {
f *fresher
annotations map[Node]annotation
}
// annotation is associated with a Node.
type annotation struct {
// Var is the type variable assigned to the node
Var Tvar
// Type is the type of the node if known
Type PolyType
// Err is any error encountered while performing type inference.
Err error
}
func (v Annotator) Visit(node Node) Visitor {
switch n := node.(type) {
case *FunctionBlock,
*FunctionParameter,
*Property,
*Block,
*ReturnStatement,
Expression:
v.annotations[n] = annotation{
Var: v.f.Fresh(),
}
}
return v
}
func (v Annotator) Done(node Node) {}