-
Notifications
You must be signed in to change notification settings - Fork 25
/
pdmanager.go
137 lines (124 loc) · 3.62 KB
/
pdmanager.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
package matcher
import (
"bytes"
"sort"
"strings"
"github.com/corywalker/expreduce/expreduce/atoms"
"github.com/corywalker/expreduce/pkg/expreduceapi"
)
type PDManager struct {
patternDefined map[string]expreduceapi.Ex
}
func EmptyPD() *PDManager {
return &PDManager{nil}
}
func copyPD(orig *PDManager) (dest *PDManager) {
dest = EmptyPD()
// We do not care that this iterates in a random order.
if (*orig).len() > 0 {
dest.lazyMakeMap()
for k, v := range (*orig).patternDefined {
(*dest).patternDefined[k] = v
}
}
return
}
func (pm *PDManager) lazyMakeMap() {
if pm.patternDefined == nil {
pm.patternDefined = make(map[string]expreduceapi.Ex)
}
}
func (pm *PDManager) Define(name string, val expreduceapi.Ex) {
pm.lazyMakeMap()
pm.patternDefined[name] = val
}
func (pm *PDManager) update(toAdd *PDManager) {
if (*toAdd).len() > 0 {
pm.lazyMakeMap()
}
// We do not care that this iterates in a random order.
for k, v := range (*toAdd).patternDefined {
(*pm).patternDefined[k] = v
}
}
func (pm *PDManager) len() int {
if pm.patternDefined == nil {
return 0
}
return len(pm.patternDefined)
}
//nolint:unused
func (pm *PDManager) string(es expreduceapi.EvalStateInterface) string {
var buffer bytes.Buffer
buffer.WriteString("{")
// We sort the keys here such that converting identical PDManagers always
// produces the same string.
keys := []string{}
for k := range pm.patternDefined {
keys = append(keys, k)
}
sort.Strings(keys)
context := atoms.NewString("Global`")
contextPath := atoms.NewExpression([]expreduceapi.Ex{
atoms.NewSymbol("System`List"),
atoms.NewString("System`"),
})
for _, k := range keys {
v := pm.patternDefined[k]
buffer.WriteString(k)
buffer.WriteString("_: ")
buffer.WriteString(v.StringForm(expreduceapi.ToStringParams{Form: "InputForm", Context: context, ContextPath: contextPath, Esi: es}))
buffer.WriteString(", ")
}
if strings.HasSuffix(buffer.String(), ", ") {
buffer.Truncate(buffer.Len() - 2)
}
buffer.WriteString("}")
return buffer.String()
}
func (pm *PDManager) Expression() expreduceapi.Ex {
res := atoms.NewExpression([]expreduceapi.Ex{atoms.NewSymbol("System`List")})
// We sort the keys here such that converting identical PDManagers always
// produces the same string.
keys := []string{}
for k := range pm.patternDefined {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := pm.patternDefined[k]
res.AppendEx(atoms.NewExpression([]expreduceapi.Ex{
atoms.NewSymbol("System`Rule"),
atoms.NewString(k),
v,
}))
}
return res
}
func defineSequence(lhs parsedForm, sequence []expreduceapi.Ex, pm *PDManager, sequenceHead string, es expreduceapi.EvalStateInterface) bool {
var attemptDefine expreduceapi.Ex
if lhs.hasPat {
sequenceHeadSym := atoms.NewSymbol(sequenceHead)
oneIdent := sequenceHeadSym.Attrs(es.GetDefinedMap()).OneIdentity
if len(sequence) == 1 && (lhs.isBlank || oneIdent || lhs.isOptional) {
attemptDefine = sequence[0]
} else if len(sequence) == 0 && lhs.isOptional && lhs.defaultExpr != nil {
attemptDefine = lhs.defaultExpr
} else if lhs.isImpliedBs {
attemptDefine = atoms.NewExpression(append([]expreduceapi.Ex{sequenceHeadSym}, sequence...))
} else {
head := atoms.NewSymbol("System`Sequence")
attemptDefine = atoms.NewExpression(append([]expreduceapi.Ex{head}, sequence...))
}
if pm.patternDefined != nil {
defined, ispd := pm.patternDefined[lhs.patSym.Name]
if ispd && !atoms.IsSameQ(defined, attemptDefine) {
es.Debugf("patterns do not match! continuing.")
return false
}
}
pm.lazyMakeMap()
pm.patternDefined[lhs.patSym.Name] = attemptDefine
}
return true
}