-
Notifications
You must be signed in to change notification settings - Fork 15
/
pattern_set.go
142 lines (127 loc) · 3.48 KB
/
pattern_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
package rel
import (
"bytes"
"fmt"
)
type SetPattern struct {
patterns []Pattern
}
func NewSetPattern(patterns ...Pattern) SetPattern {
m := make(map[string]struct{})
for _, v := range patterns {
if _, exists := m[v.String()]; exists {
panic(fmt.Sprintf("item %s is duplicated", v))
}
m[v.String()] = struct{}{}
}
return SetPattern{patterns}
}
func (p SetPattern) Bind(local Scope, value Value) (Scope, error) {
set, is := value.(GenericSet)
if !is {
return EmptyScope, fmt.Errorf("value %s is not a set", value)
}
extraElements := make(map[int]int)
for i, ptn := range p.patterns {
if _, is := ptn.(ExtraElementPattern); is {
if len(extraElements) == 1 {
return EmptyScope, fmt.Errorf("non-deterministic pattern is not supported yet")
}
extraElements[i] = set.Count() - len(p.patterns)
continue
}
if t, is := ptn.(ExprPattern); is {
if _, is = t.Expr.(IdentExpr); is {
if len(extraElements) == 1 {
return EmptyScope, fmt.Errorf("non-deterministic pattern is not supported yet")
}
extraElements[i] = set.Count() - len(p.patterns)
}
}
}
if len(p.patterns) > set.Count()+len(extraElements) {
return EmptyScope, fmt.Errorf("length of set %s shorter than set pattern %s", set, p)
}
if len(extraElements) == 0 && len(p.patterns) < set.Count() {
return EmptyScope, fmt.Errorf("length of set %s longer than set pattern %s", set, p)
}
result := EmptyScope
for _, ptn := range p.patterns {
if _, is := ptn.(ExtraElementPattern); is {
continue
}
switch t := ptn.(type) {
case ExprPattern:
v, is := t.Expr.(Value)
if is {
if !set.Has(v) {
return EmptyScope, fmt.Errorf("item %s is not included in set %s", v, value)
}
set = set.Without(v).(GenericSet)
continue
}
if _, is := t.Expr.(IdentExpr); !is {
return EmptyScope, fmt.Errorf("item type %s is not supported yet", t)
}
case ExprsPattern:
// Support cases:
// AssertCodesEvalToSameValue(t, `{5, 6}`, `let x = 1; let y = 42; let {(x), (y), ...t} = {1, 42, 5, 6}; t`)
// AssertCodeErrors(t, "", `let x = 1; let y = 42; let {(x), (y)} = {1, 4}; 2`)
if identExpr, is := t.exprs[0].(IdentExpr); is {
v, has := local.Get(identExpr.ident)
if !has {
return EmptyScope, fmt.Errorf("%q not in scope", identExpr.ident)
}
if !set.Has(v.(Value)) {
return EmptyScope, fmt.Errorf("item %s is not included in set %s", v, value)
}
set = set.Without(v.(Value)).(GenericSet)
}
default:
return EmptyScope, fmt.Errorf("%s not supported yet", t)
}
}
for i := range extraElements {
var scope Scope
var err error
if _, is := p.patterns[i].(ExtraElementPattern); is {
scope, err = p.patterns[i].Bind(local, set)
} else {
if set.Count() != 1 {
return EmptyScope, fmt.Errorf("the length of set %s is wrong", set)
}
scope, err = p.patterns[i].Bind(local, set.set.Any().(Value))
if err != nil {
return EmptyScope, err
}
}
if err != nil {
return EmptyScope, err
}
result, err = result.MatchedUpdate(scope)
if err != nil {
return EmptyScope, err
}
}
return result, nil
}
func (p SetPattern) String() string {
elts := p.patterns
var buf bytes.Buffer
buf.WriteString("{")
for i, value := range elts {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(value.String())
}
buf.WriteString("}")
return buf.String()
}
func (p SetPattern) Bindings() []string {
bindings := make([]string, len(p.patterns))
for i, v := range p.patterns {
bindings[i] = v.String()
}
return bindings
}