-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
set.go
314 lines (255 loc) · 8.8 KB
/
set.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package containers
import (
"github.com/inoxlang/inox/internal/commonfmt"
"github.com/inoxlang/inox/internal/core/symbolic"
"github.com/inoxlang/inox/internal/globals/containers/common"
pprint "github.com/inoxlang/inox/internal/prettyprint"
"github.com/inoxlang/inox/internal/utils"
)
var (
SET_PROPNAMES = []string{"has", "add", "remove", "get"}
SET_CONFIG_ELEMENT_PATTERN_PROP_KEY = "element"
SET_CONFIG_UNIQUE_PROP_KEY = "unique"
SET_ADD_METHOD_PARAM_NAMES = []string{"element"}
SET_HAS_METHOD_PARAM_NAMES = []string{"element"}
SET_GET_METHOD_PARAM_NAMES = []string{"key"}
ANY_SET = NewSetWithPattern(symbolic.ANY_PATTERN, nil)
ANY_SET_PATTERN = NewSetPatternWithElementPatternAndUniqueness(symbolic.ANY_PATTERN, nil)
_ = []symbolic.Iterable{(*Set)(nil)}
_ = []symbolic.Collection{(*Set)(nil)}
_ = []symbolic.Serializable{(*Set)(nil)}
_ = []symbolic.PotentiallySharable{(*Set)(nil)}
_ = []symbolic.UrlHolder{(*Set)(nil)}
_ = []symbolic.Watchable{(*Set)(nil)}
_ = []symbolic.PotentiallyConcretizable{(*SetPattern)(nil)}
_ = []symbolic.MigrationInitialValueCapablePattern{(*SetPattern)(nil)}
)
type Set struct {
elementPattern symbolic.Pattern
element symbolic.Value //cache
uniqueness *common.UniquenessConstraint //if nil any uniqueness is matched
shared bool
url *symbolic.URL //can be nil
symbolic.UnassignablePropsMixin
symbolic.SerializableMixin
symbolic.CollectionMixin
}
func NewSet(ctx *symbolic.Context, elements symbolic.Iterable, config *symbolic.OptionalParam[*symbolic.Object]) *Set {
var patt symbolic.Pattern = symbolic.ANY_PATTERN
var uniqueness *common.UniquenessConstraint = &common.UniquenessConstraint{
Type: common.UniqueRepr,
}
if config.Value != nil {
configObject := *config.Value
val, _, hasElemPattern := configObject.GetProperty(SET_CONFIG_ELEMENT_PATTERN_PROP_KEY)
if hasElemPattern {
pattern, ok := val.(symbolic.Pattern)
if !ok {
err := commonfmt.FmtInvalidValueForPropXOfArgY(SET_CONFIG_ELEMENT_PATTERN_PROP_KEY, "configuration", "a pattern is expected")
ctx.AddSymbolicGoFunctionError(err.Error())
} else {
patt = pattern
}
}
val, _, hasUniquenessConstraint := configObject.GetProperty(SET_CONFIG_UNIQUE_PROP_KEY)
if hasUniquenessConstraint {
u, err := common.UniquenessConstraintFromSymbolicValue(val, patt)
if err != nil {
err := commonfmt.FmtInvalidValueForPropXOfArgY(SET_CONFIG_UNIQUE_PROP_KEY, "configuration", err.Error())
ctx.AddSymbolicGoFunctionError(err.Error())
} else if u.Type == common.UniqueTransientID {
ctx.AddSymbolicGoFunctionError("uniqueness based on transient ID is not supported by Set")
} else {
uniqueness = &u
}
}
}
return NewSetWithPattern(patt, uniqueness)
}
func NewSetWithPattern(elementPattern symbolic.Pattern, uniqueness *common.UniquenessConstraint) *Set {
set := &Set{elementPattern: elementPattern, uniqueness: uniqueness}
set.element = elementPattern.SymbolicValue()
return set
}
func (s *Set) Test(v symbolic.Value, state symbolic.RecTestCallState) bool {
state.StartCall()
defer state.FinishCall()
otherSet, ok := v.(*Set)
if !ok || !s.elementPattern.Test(otherSet.elementPattern, state) {
return false
}
return s.uniqueness == nil || (otherSet.uniqueness != nil && *s.uniqueness == *otherSet.uniqueness)
}
func (s *Set) IsSharable() (bool, string) {
return true, ""
}
func (s *Set) Share(originState *symbolic.State) symbolic.PotentiallySharable {
shared := *s
shared.shared = true
if psharablbe, ok := shared.element.(symbolic.PotentiallySharable); ok {
shared.element = psharablbe.Share(originState)
}
return &shared
}
func (s *Set) IsShared() bool {
return s.shared
}
func (set *Set) WatcherElement() symbolic.Value {
return symbolic.ANY
}
func (s *Set) WithURL(url *symbolic.URL) symbolic.UrlHolder {
copy := *s
copy.url = url
mv, ok := copy.element.(*symbolic.Multivalue)
elementURL := copy.url.WithAdditionalPathPatternSegment("*")
if ok {
copy.element = mv.TransformsValues(func(v symbolic.Value) symbolic.Value {
if urlHolder, ok := v.(symbolic.UrlHolder); ok {
return urlHolder.WithURL(elementURL)
}
return v
})
} else if urlHolder, ok := copy.element.(symbolic.UrlHolder); ok {
copy.element = urlHolder.WithURL(elementURL)
}
return ©
}
func (s *Set) URL() (*symbolic.URL, bool) {
if s.url != nil {
return s.url, true
}
return nil, false
}
func (s *Set) GetGoMethod(name string) (*symbolic.GoFunction, bool) {
switch name {
case "has":
return symbolic.WrapGoMethod(s.Has), true
case "add":
return symbolic.WrapGoMethod(s.Add), true
case "remove":
return symbolic.WrapGoMethod(s.Remove), true
case "get":
return symbolic.WrapGoMethod(s.Get), true
}
return nil, false
}
func (s *Set) Prop(name string) symbolic.Value {
return symbolic.GetGoMethodOrPanic(name, s)
}
func (*Set) PropertyNames() []string {
return SET_PROPNAMES
}
func (s *Set) Has(ctx *symbolic.Context, v symbolic.Serializable) *symbolic.Bool {
ctx.SetSymbolicGoFunctionParameters(&[]symbolic.Value{
s.element,
}, SET_HAS_METHOD_PARAM_NAMES)
return symbolic.ANY_BOOL
}
func (s *Set) Contains(value symbolic.Serializable) (yes bool, possible bool) {
return false, s.element.Test(value, symbolic.RecTestCallState{})
}
func (s *Set) Add(ctx *symbolic.Context, v symbolic.Serializable) {
ctx.SetSymbolicGoFunctionParameters(&[]symbolic.Value{
s.element,
}, SET_ADD_METHOD_PARAM_NAMES)
}
func (s *Set) Remove(ctx *symbolic.Context, v symbolic.Serializable) {
ctx.SetSymbolicGoFunctionParameters(&[]symbolic.Value{
s.element,
}, SET_ADD_METHOD_PARAM_NAMES)
}
func (s *Set) Get(ctx *symbolic.Context, k symbolic.StringLike) (symbolic.Value, *symbolic.Bool) {
return s.element, symbolic.ANY_BOOL
}
func (s *Set) PrettyPrint(w pprint.PrettyPrintWriter, config *pprint.PrettyPrintConfig) {
w.WriteName("Set(")
s.element.PrettyPrint(w, config)
if s.uniqueness != nil {
w.WriteByte(',')
s.uniqueness.ToSymbolicValue().PrettyPrint(w.ZeroIndent(), config)
}
w.WriteByte(')')
}
func (*Set) IteratorElementKey() symbolic.Value {
return symbolic.ANY_STR_LIKE
}
func (s *Set) IteratorElementValue() symbolic.Value {
return s.element
}
func (*Set) WidestOfType() symbolic.Value {
return ANY_SET
}
type SetPattern struct {
symbolic.UnassignablePropsMixin
elementPattern symbolic.Pattern
uniqueness *common.UniquenessConstraint //if nil any uniqueness is matched
symbolic.NotCallablePatternMixin
symbolic.SerializableMixin
}
func NewSetPatternWithElementPatternAndUniqueness(elementPattern symbolic.Pattern, uniqueness *common.UniquenessConstraint) *SetPattern {
return &SetPattern{elementPattern: elementPattern, uniqueness: uniqueness}
}
func (p *SetPattern) MigrationInitialValue() (symbolic.Serializable, bool) {
return symbolic.EMPTY_LIST, true
}
func (p *SetPattern) Test(v symbolic.Value, state symbolic.RecTestCallState) bool {
state.StartCall()
defer state.FinishCall()
otherPattern, ok := v.(*SetPattern)
if !ok || !p.elementPattern.Test(otherPattern.elementPattern, state) {
return false
}
return p.uniqueness == nil || (otherPattern.uniqueness != nil && *p.uniqueness == *otherPattern.uniqueness)
}
func (p *SetPattern) TestValue(v symbolic.Value, state symbolic.RecTestCallState) bool {
state.StartCall()
defer state.FinishCall()
set, ok := v.(*Set)
if !ok {
return false
}
return p.elementPattern.Test(set.elementPattern, state) &&
(p.uniqueness == nil || (set.uniqueness != nil && *p.uniqueness == *set.uniqueness))
//TODO: test nodes's value
}
func (p *SetPattern) IsConcretizable() bool {
if p.uniqueness == nil {
return false
}
potentiallyConcretizable, ok := p.elementPattern.(symbolic.PotentiallyConcretizable)
return ok && potentiallyConcretizable.IsConcretizable()
}
func (p *SetPattern) Concretize(ctx symbolic.ConcreteContext) any {
if !p.IsConcretizable() {
panic(symbolic.ErrNotConcretizable)
}
concreteElementPattern := utils.Must(symbolic.Concretize(p.elementPattern, ctx))
return externalData.CreateConcreteSetPattern(*p.uniqueness, concreteElementPattern)
}
func (p *SetPattern) HasUnderlyingPattern() bool {
return true
}
func (p *SetPattern) StringPattern() (symbolic.StringPattern, bool) {
return nil, false
}
func (p *SetPattern) SymbolicValue() symbolic.Value {
return NewSetWithPattern(p.elementPattern, p.uniqueness)
}
func (p *SetPattern) PrettyPrint(w pprint.PrettyPrintWriter, config *pprint.PrettyPrintConfig) {
w.WriteName("set-pattern(")
p.elementPattern.SymbolicValue().PrettyPrint(w, config)
if p.uniqueness != nil {
w.WriteByte(',')
p.uniqueness.ToSymbolicValue().PrettyPrint(w.ZeroIndent(), config)
}
w.WriteByte(')')
}
func (*SetPattern) IteratorElementKey() symbolic.Value {
return symbolic.ANY
}
func (*SetPattern) IteratorElementValue() symbolic.Value {
return symbolic.ANY
}
func (*SetPattern) WidestOfType() symbolic.Value {
return ANY_SET_PATTERN
}