/
string.go
190 lines (179 loc) · 4.57 KB
/
string.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
package expreduce
import (
"bytes"
"github.com/corywalker/expreduce/expreduce/atoms"
"github.com/corywalker/expreduce/expreduce/parser/parens"
"github.com/corywalker/expreduce/pkg/expreduceapi"
)
func toStringInfix(
parts []expreduceapi.Ex,
delim string,
thisHead string,
p expreduceapi.ToStringParams,
) (bool, string) {
if p.Form != "InputForm" && p.Form != "OutputForm" && p.Form != "TeXForm" {
return false, ""
}
if len(parts) < 2 {
return false, ""
}
addParens := parens.NeedsParens(thisHead, p.PreviousHead)
var buffer bytes.Buffer
if addParens {
if p.Form == "TeXForm" {
buffer.WriteString("{\\left(")
} else {
buffer.WriteString("(")
}
}
nextParams := p
nextParams.PreviousHead = thisHead
for i := 0; i < len(parts); i++ {
buffer.WriteString(parts[i].StringForm(nextParams))
if i != len(parts)-1 {
buffer.WriteString(delim)
}
}
if addParens {
if p.Form == "TeXForm" {
buffer.WriteString("\\right)}")
} else {
buffer.WriteString(")")
}
}
return true, buffer.String()
}
func toStringInfixFn(
this expreduceapi.ExpressionInterface,
p expreduceapi.ToStringParams,
) (bool, string) {
if len(this.GetParts()) != 3 {
return false, ""
}
expr, isExpr := this.GetParts()[1].(expreduceapi.ExpressionInterface)
delim, delimIsStr := this.GetParts()[2].(*atoms.String)
if !isExpr || !delimIsStr {
return false, ""
}
return toStringInfix(expr.GetParts()[1:], delim.Val, "", p)
}
// TODO(corywalker): Remove start, end. No users of these values.
func toStringInfixAdvanced(
parts []expreduceapi.Ex,
delim string,
thisHead string,
surroundEachArg bool,
start string,
end string,
params expreduceapi.ToStringParams,
) (bool, string) {
if params.Form != "InputForm" && params.Form != "OutputForm" &&
params.Form != "TeXForm" {
return false, ""
}
if len(parts) < 2 {
return false, ""
}
var buffer bytes.Buffer
addParens := parens.NeedsParens(thisHead, params.PreviousHead)
if addParens {
if params.Form == "TeXForm" {
buffer.WriteString("{\\left(")
} else {
buffer.WriteString("(")
}
}
if !surroundEachArg {
buffer.WriteString(start)
}
nextParams := params
for i := 0; i < len(parts); i++ {
useTeXGrouping := false
if thisHead == "System`Power" && params.Form == "TeXForm" && i == 1 {
nextParams.PreviousHead = "<TOPLEVEL>"
useTeXGrouping = true
} else {
nextParams.PreviousHead = thisHead
}
if useTeXGrouping {
buffer.WriteString("{")
}
if surroundEachArg {
buffer.WriteString("(")
buffer.WriteString(parts[i].StringForm(nextParams))
buffer.WriteString(")")
} else {
buffer.WriteString(parts[i].StringForm(nextParams))
}
if useTeXGrouping {
buffer.WriteString("}")
}
if i != len(parts)-1 {
buffer.WriteString(delim)
}
}
if !surroundEachArg {
buffer.WriteString(end)
}
if addParens {
if params.Form == "TeXForm" {
buffer.WriteString("\\right)}")
} else {
buffer.WriteString(")")
}
}
return true, buffer.String()
}
func definitionComplexityStringFormArgs() (*atoms.String, expreduceapi.ExpressionInterface) {
// This was created because the "Private`" names in the blanks were
// indicating greater complexity than they deserved.
return atoms.NewString("Global`"), atoms.NewExpression([]expreduceapi.Ex{
atoms.NewSymbol("System`List"),
atoms.NewString("System`"),
atoms.NewString("Private`"),
})
}
func actualStringFormArgs(
es expreduceapi.EvalStateInterface,
) (*atoms.String, expreduceapi.ExpressionInterface) {
return atoms.NewString(
es.GetStringDef("System`$Context", "Global`"),
), es.GetListDef(
"System`$ContextPath",
)
}
func ActualStringFormArgsFull(
form string,
es expreduceapi.EvalStateInterface,
) expreduceapi.ToStringParams {
return expreduceapi.ToStringParams{
Form: form,
Context: atoms.NewString(
es.GetStringDef("System`$Context", "Global`"),
),
ContextPath: es.GetListDef("System`$ContextPath"),
PreviousHead: "<TOPLEVEL>",
Esi: es,
}
}
func simpleTeXToString(
fnName string,
) func(expreduceapi.ExpressionInterface, expreduceapi.ToStringParams) (bool, string) {
return (func(this expreduceapi.ExpressionInterface, params expreduceapi.ToStringParams) (bool, string) {
if params.Form != "TeXForm" {
return false, ""
}
var buffer bytes.Buffer
buffer.WriteString("\\" + fnName + " \\left(")
nextParams := params
nextParams.PreviousHead = "<TOPLEVEL>"
for i := 1; i < len(this.GetParts()); i++ {
buffer.WriteString(this.GetParts()[i].StringForm(nextParams))
if i != len(this.GetParts())-1 {
buffer.WriteString(",")
}
}
buffer.WriteString("\\right)")
return true, buffer.String()
})
}