-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
106 lines (92 loc) · 2.31 KB
/
Copy pathutil.go
File metadata and controls
106 lines (92 loc) · 2.31 KB
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
package analysis
import (
"cmp"
"fmt"
"github.com/ProCode-Software/klar/internal/klarerrs"
)
func sortByOrder(a, b *Object) int { return cmp.Compare(a.Order, b.Order) }
type mapObject map[string]*Object
func (m *mapObject) Insert(obj *Object) (existing *Object) {
if *m == nil {
*m = make(mapObject)
}
if existing = (*m)[obj.Name]; existing != nil {
return
}
(*m)[obj.Name] = obj
return nil
}
func (m *mapObject) Set(name string, obj *Object) {
if *m == nil {
*m = make(mapObject)
}
(*m)[name] = obj
}
func (m *mapObject) ContainsName(name string) bool {
if *m == nil {
return false
}
_, ok := (*m)[name]
return ok
}
func quote(s string) string { return klarerrs.Quote(s) }
func repeatWithItem[T any](item T, count int) []T {
result := make([]T, count)
for i := range result {
result[i] = item
}
return result
}
func indexBuiltin(builtin, f string, t *Expr) *klarerrs.Error {
builtinObj := builtinModule.Context.Lookup(builtin)
if builtinObj == nil {
panic("invalid builtin: " + builtin)
}
if builtinObj.IsTypeName() {
if bt, ok := builtinObj.TypeName().Type.(*bootstrapType); ok {
indexer, ok := bt.asDeclared.(Indexer)
if !ok {
panic("builtin " + builtin + " does not implement Indexer")
}
return indexer.Index(f, t)
}
}
// TODO: Is this reachable?
indexer, ok := Underlying(builtinObj.Type).(Indexer)
if !ok {
panic("builtin " + builtin + " does not implement Indexer")
}
return indexer.Index(f, t)
}
func quoteAka(t Type) string {
ts := t.String()
if und := UnderlyingTypeName(t, false).String(); und != ts {
return fmt.Sprintf("%s (aka %s)", quote(ts), quote(und))
}
return quote(ts)
}
func isTODO(t Type) bool {
if t.Kind() != KindFunction {
return false
}
builtinTODO := BuiltInContext.Lookup("TODO")
if builtinTODO != nil {
return t == builtinTODO.Type
}
// If currently bootstrapping
return t == builtinModule.Context.Lookup("TODO").Type
}
func isCloneBuiltin(t Type) bool {
if t.Kind() != KindFunction {
return false
}
builtinClone := BuiltInContext.Lookup("clone")
if builtinClone != nil {
return t == builtinClone.Type
}
// If currently bootstrapping
return t == builtinModule.Context.Lookup("clone").Type
}
func hintWithDiff(err *klarerrs.Error, hint string, edits ...klarerrs.DiffEdit) {
err.HintWithDiff(hint, klarerrs.NewDiff("", edits...))
}