This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
283 lines (261 loc) · 6.11 KB
/
type.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package typ
import (
"encoding/json"
"strconv"
"strings"
"github.com/mb0/xelf/bfr"
"github.com/mb0/xelf/cor"
)
// Type represents the full type details. It consists of a kind and additional information.
type Type struct {
Kind Kind
*Info
}
func (t Type) Typ() Type { return Typ }
// Info represents the reference name and type parameters or constants.
type Info struct {
Ref string `json:"ref,omitempty"`
Params []Param `json:"params,omitempty"`
Consts Consts `json:"consts,omitempty"`
}
// Key returns the lowercase ref key.
func (a *Info) Key() string {
if a != nil {
return cor.Keyed(a.Ref)
}
return ""
}
func (a *Info) HasRef() bool { return a != nil && len(a.Ref) > 0 }
func (a *Info) HasParams() bool { return a != nil && len(a.Params) > 0 }
func (a *Info) HasConsts() bool { return a != nil && len(a.Consts) > 0 }
func (a *Info) ParamLen() int {
if a == nil {
return 0
}
return len(a.Params)
}
// ParamByIdx returns a pointer to the field at idx or an error.
func (a *Info) ParamByIdx(idx int) (*Param, error) {
if a != nil && idx >= 0 && idx < len(a.Params) {
return &a.Params[idx], nil
}
return nil, cor.Errorf("no param with idx %d", idx)
}
// ParamByKey returns a pointer to the field and its idex at key or an error.
func (a *Info) ParamByKey(key string) (*Param, int, error) {
if a != nil {
for i, f := range a.Params {
if f.Key() == key {
return &a.Params[i], i, nil
}
}
}
return nil, -1, cor.Errorf("no param with key %s", key)
}
// Param represents an type parameter with a name and type.
type Param struct {
Name string `json:"name,omitempty"`
Type `json:"typ,omitempty"`
}
// Opt returns true if the param is optional, indicated by its name ending in a question mark.
func (p Param) Opt() bool { n := p.Name; return n != "" && n[len(n)-1] == '?' }
// Key returns the lowercase param key.
func (p Param) Key() string { return cor.LastKey(p.Name) }
func (t Type) IsZero() bool { return t.Kind == 0 && t.Info.IsZero() }
func (a *Info) IsZero() bool {
return a == nil || a.Ref == "" && len(a.Params) == 0 && len(a.Consts) == 0
}
type infoPair = struct{ a, b *Info }
func (t Type) Equal(o Type) bool { return t.equal(o, nil) }
func (t Type) equal(o Type, hist []infoPair) bool {
return t.Kind == o.Kind && t.Info.equal(o.Info, t.Kind&KindCtx != 0, hist)
}
func (a *Info) Equal(b *Info) bool { return a.equal(b, false, nil) }
func (a *Info) equal(b *Info, ref bool, hist []infoPair) bool {
if a == b {
return true
}
if a.IsZero() {
return b.IsZero()
}
if b.IsZero() ||
a.Ref != b.Ref && a.Key() != b.Key() {
return false
}
if ref {
return true
}
if len(a.Params) != len(b.Params) ||
len(a.Consts) != len(b.Consts) {
return false
}
for i, av := range a.Consts {
if av != b.Consts[i] {
return false
}
}
p := infoPair{a, b}
for _, h := range hist {
if h == p {
return true
}
}
for i, af := range a.Params {
if !af.equal(b.Params[i], append(hist, p)) {
return false
}
}
return true
}
func (p Param) Equal(o Param) bool { return p.equal(o, nil) }
func (p Param) equal(o Param, hist []infoPair) bool {
return (p.Name == o.Name || p.Key() == o.Key()) && p.Type.equal(o.Type, hist)
}
func (t Type) String() string { return bfr.String(t) }
func (t Type) MarshalJSON() ([]byte, error) { return bfr.JSON(t) }
func (t *Type) UnmarshalJSON(raw []byte) error {
var tmp struct{ Typ string }
err := json.Unmarshal(raw, &tmp)
if err != nil {
return err
}
r, err := Read(strings.NewReader(tmp.Typ))
if err != nil {
return err
}
*t = r
return nil
}
func (t Type) WriteBfr(b *bfr.Ctx) error {
if b.JSON {
b.WriteString(`{"typ":"`)
bb := *b
bb.JSON = false
err := t.writeBfr(&bb, nil, nil, false)
b.WriteString(`"}`)
return err
}
return t.writeBfr(b, nil, nil, false)
}
func (t Type) writeBfr(b *bfr.Ctx, pre *strings.Builder, hist []*Info, qual bool) error {
switch t.Kind & MaskRef {
case KindRec, KindObj:
for i := 0; i < len(hist); i++ {
h := hist[len(hist)-1-i]
if t.Info == h {
writeRef(b, pre, '~', strconv.Itoa(i), t)
return nil
}
}
case KindCont, KindIdxr, KindKeyr, KindList, KindDict:
if pre == nil {
pre = &strings.Builder{}
} else {
pre.WriteByte('|')
}
pre.WriteString(t.Kind.String())
return t.Elem().writeBfr(b, pre, hist, false)
}
var detail bool
switch t.Kind & MaskRef {
case KindVar:
n := t.ParamLen()
if n == 0 {
break
}
if n > 1 {
b.WriteByte('<')
}
err := writePre(b, pre, t, qual)
if err != nil {
return err
}
if n == 1 {
b.WriteByte('|')
c := t.Params[0].Type
return c.writeBfr(b, nil, nil, false)
}
b.WriteString("|alt")
err = t.Info.writeXelf(b, true, hist)
b.WriteByte('>')
return err
case KindRef, KindSch:
ref := ""
if t.HasRef() {
ref = t.Ref
}
var x byte = '@'
if t.Kind&KindSch == KindSch {
x = '~'
}
writeRef(b, pre, x, ref, t)
return nil
case KindRec, KindFunc, KindForm, KindAlt:
detail = true
fallthrough
case KindBits, KindEnum, KindObj:
b.WriteByte('<')
err := writePre(b, pre, t, qual)
if err != nil {
return err
}
err = t.Info.writeXelf(b, detail, append(hist, t.Info))
b.WriteByte('>')
return err
}
return writePre(b, pre, t, qual)
}
func writePre(b *bfr.Ctx, pre *strings.Builder, t Type, qual bool) error {
if qual {
b.WriteByte('~')
}
if pre != nil {
b.WriteString(pre.String())
if t == Any {
return nil
}
b.WriteByte('|')
}
return t.Kind.WriteBfr(b)
}
func writeRef(b *bfr.Ctx, pre *strings.Builder, x byte, ref string, t Type) {
if pre != nil {
b.WriteString(pre.String())
b.WriteByte('|')
}
b.WriteByte(x)
b.WriteString(ref)
if t.Kind&KindOpt != 0 {
b.WriteByte('?')
}
}
func (a *Info) writeXelf(b *bfr.Ctx, detail bool, hist []*Info) error {
if a == nil {
return nil
}
if a.Ref != "" {
b.WriteByte(' ')
b.WriteString(a.Ref)
}
if !detail {
return nil
}
var i int
for ; i < len(a.Params); i++ {
b.WriteByte(' ')
f := a.Params[i]
if f.Name != "" {
b.WriteString(f.Name)
if f.Type == Void {
b.WriteByte(';')
continue
}
b.WriteByte(':')
}
err := f.Type.writeBfr(b, nil, hist, false)
if err != nil {
return err
}
}
return nil
}