generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
typeresolver.go
176 lines (154 loc) 路 4.02 KB
/
typeresolver.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package schema
import (
"fmt"
"strings"
"github.com/alecthomas/types/optional"
)
// Resolver may be implemented be a node in the AST to resolve references within itself.
type Resolver interface {
// Resolve a reference to a symbol declaration or nil.
Resolve(ref Ref) *ModuleDecl
}
// Scoped is implemented by nodes that wish to introduce a new scope.
type Scoped interface {
Scope() Scope
}
// Scope maps relative names to fully qualified types.
type Scope map[string]ModuleDecl
// ModuleDecl is a declaration associated with a module.
type ModuleDecl struct {
Module optional.Option[*Module]
Symbol Symbol
}
func (s Scope) String() string {
out := &strings.Builder{}
for name, decl := range s {
fmt.Fprintf(out, "%s: %T\n", name, decl.Symbol)
}
return out.String()
}
// ResolveTypeAs resolves a [Type] to a concrete symbol and declaration.
func ResolveTypeAs[S Symbol](scopes Scopes, t Type) (symbol S, decl *ModuleDecl) {
decl = scopes.ResolveType(t)
if decl == nil {
return
}
var ok bool
symbol, ok = decl.Symbol.(S)
if !ok {
return
}
return symbol, decl
}
// ResolveAs resolves a [Ref] to a concrete symbol and declaration.
func ResolveAs[S Symbol](scopes Scopes, ref Ref) (symbol S, decl *ModuleDecl) {
decl = scopes.Resolve(ref)
if decl == nil {
return
}
var ok bool
symbol, ok = decl.Symbol.(S)
if !ok {
return
}
return symbol, decl
}
// Scopes to search during type resolution.
type Scopes []Scope
// NewScopes constructs a new type resolution stack with builtins pre-populated.
func NewScopes() Scopes {
builtins := Builtins()
// Empty scope tail for builtins.
scopes := Scopes{primitivesScope, Scope{}}
if err := scopes.Add(optional.None[*Module](), builtins.Name, builtins); err != nil {
panic(err)
}
for _, decl := range builtins.Decls {
if err := scopes.Add(optional.Some(builtins), decl.GetName(), decl); err != nil {
panic(err)
}
}
// Push an empty scope for other modules to be added to.
scopes = scopes.Push()
return scopes
}
var _ Resolver = Scopes{}
func (s Scopes) String() string {
out := &strings.Builder{}
for i, scope := range s {
if i != 0 {
fmt.Fprintln(out)
}
fmt.Fprintf(out, "Scope %d:\n", i)
for name, decl := range scope {
fmt.Fprintf(out, " %s: %T\n", name, decl.Symbol)
}
}
return out.String()
}
func (s Scopes) PushScope(scope Scope) Scopes {
out := make(Scopes, 0, len(s)+1)
out = append(out, s...)
out = append(out, scope)
return out
}
// Push a new Scope onto the stack.
//
// This contains references to previous Scopes so that updates are preserved.
func (s Scopes) Push() Scopes {
return s.PushScope(Scope{})
}
// Add a declaration to the current scope.
func (s *Scopes) Add(owner optional.Option[*Module], name string, symbol Symbol) error {
end := len(*s) - 1
if prev, ok := (*s)[end][name]; ok {
return fmt.Errorf("%s: duplicate declaration of %q at %s", symbol.Position(), name, prev.Symbol.Position())
}
(*s)[end][name] = ModuleDecl{owner, symbol}
return nil
}
func (s Scopes) ResolveType(t Type) *ModuleDecl {
switch t := t.(type) {
case Named:
return s.Resolve(Ref{Name: t.GetName()})
case *Ref:
return s.Resolve(*t)
case Symbol:
return &ModuleDecl{optional.None[*Module](), t}
default:
return nil
}
}
// Resolve a reference to a symbol declaration or nil.
func (s Scopes) Resolve(ref Ref) *ModuleDecl {
if ref.Module == "" {
for i := len(s) - 1; i >= 0; i-- {
scope := s[i]
if decl, ok := scope[ref.Name]; ok {
return &decl
}
}
return nil
}
// If a module is provided, try to resolve it, then resolve the reference through the module.
for i := len(s) - 1; i >= 0; i-- {
scope := s[i]
if mdecl, ok := scope[ref.Module]; ok {
if resolver, ok := mdecl.Symbol.(Resolver); ok {
if decl := resolver.Resolve(ref); decl != nil {
// Holy nested if statement Batman.
return decl
}
} else {
if module, ok := mdecl.Module.Get(); ok {
for _, d := range module.Decls {
if d.GetName() == ref.Name {
return &ModuleDecl{mdecl.Module, d}
}
}
}
}
}
}
return nil
}