-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfmt.go
More file actions
277 lines (265 loc) · 7.62 KB
/
Copy pathfmt.go
File metadata and controls
277 lines (265 loc) · 7.62 KB
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
package apl
import (
"fmt"
"io"
"reflect"
"strings"
"text/tabwriter"
"unicode"
"github.com/ktye/iv/apl/scan"
)
// SetPP is called when a value is assigned to Quad-PP.
// If R is an integer, PP is set to this value.
// If R is a dict that maps from values to string, the format strings of the types are set.
// If R is the empty array, all format strings are removed and PP is reset.
//
// PP >= 0 sets the precision when printing floating point numbers.
// When nothing else is set, the default format string is %.(prec)G.
// PP < 0 always uses full precision for numbers and quoted format for strings (%q).
// Additionally, there are several special interpretations for PP < 0:
// -1: arrays formatted in a table
// -2: arrays formatted in a single line of json
// -3: arrays formatted in a single line compatible with matlab
// -8: integers formatted as octal numbers with 0 prefix
// -16: integers formatted as hexadecimal numbers with 0x prefix, floats with %b (-123456p-78)
func (a *Apl) SetPP(R Value) error {
if _, ok := R.(EmptyArray); ok {
a.Format.PP = 0
a.Format.Fmt = make(map[reflect.Type]string)
return nil
} else if d, ok := R.(Object); ok {
keys := d.Keys()
for _, k := range keys {
v := d.At(k)
if v != nil {
if s, ok := v.(String); ok {
a.Format.Fmt[reflect.TypeOf(k)] = string(s)
}
}
}
} else if n, ok := R.(Number); ok {
if i, ok := n.ToIndex(); ok {
a.Format.PP = i
return nil
}
}
return fmt.Errorf("illegal type for PP: %T", R)
}
// ArrayString can be used by an array implementation.
// It formats an n-dimensional array using a tabwriter for PP>=-1.
// Each dimension is terminated by k newlines, where k is the dimension index.
// For PP==-2, it uses a single line json notation with nested brackets and
// for PP==-3, it formats in a single line matlab syntax (rank <= 2).
func ArrayString(f Format, v Array) string {
if f.PP == -2 {
return jsonArray(f, v)
} else if f.PP == -3 {
return matArray(f, v)
}
shape := v.Shape()
if len(shape) == 0 {
return ""
} else if len(shape) == 1 {
s := make([]string, shape[0])
for i := 0; i < shape[0]; i++ {
s[i] = v.At(i).String(f)
}
return strings.Join(s, " ")
}
size := 1
for _, n := range shape {
size *= n
}
idx := make([]int, len(shape))
inc := func() int {
for i := 0; i < len(idx); i++ {
k := len(idx) - 1 - i
idx[k]++
if idx[k] == shape[k] {
idx[k] = 0
} else {
return i
}
}
return -1 // should not happen
}
var buf strings.Builder
tw := tabwriter.NewWriter(&buf, 1, 0, 1, ' ', tabwriter.AlignRight) // tabwriter.AlignRight)
for i := 0; i < size; i++ {
fmt.Fprintf(tw, "%s\t", v.At(i).String(f))
if term := inc(); term > 0 {
for k := 0; k < term; k++ {
fmt.Fprintln(tw)
}
} else if term == -1 {
fmt.Fprintln(tw)
}
}
tw.Flush()
s := buf.String()
if len(s) > 0 && s[len(s)-1] == '\n' {
// Don't print the final newline.
return s[:len(s)-1]
}
return s
}
// stringArray converts the array to a string array of the same shape.
// All elements are printed with the current PP.
func stringArray(f Format, v Array) StringArray {
sa := StringArray{Dims: CopyShape(v), Strings: make([]string, v.Size())}
for i := range sa.Strings {
sa.Strings[i] = v.At(i).String(f)
}
return sa
}
// jsonArray is used for PP=-2
func jsonArray(f Format, v Array) string {
sa := stringArray(f, v)
var vector func(v StringArray) string
vector = func(S StringArray) string {
if len(S.Dims) == 1 {
return "[" + strings.Join(S.Strings[:S.Dims[0]], ",") + "]"
}
vec := make([]string, S.Dims[0])
inc := Prod(S.Dims[1:])
for i := 0; i < S.Dims[0]; i++ {
sub := StringArray{Dims: S.Dims[1:], Strings: S.Strings[i*inc:]}
vec[i] = vector(sub)
}
return "[" + strings.Join(vec, ",") + "]"
}
return vector(sa)
}
// matArray is used for PP=-3. It only supported for rank 1 and 2.
func matArray(f Format, v Array) string {
sa := stringArray(f, v)
if len(sa.Dims) == 1 {
return "[" + strings.Join(sa.Strings, ",") + "]"
} else if len(sa.Dims) != 2 {
return "[rank error]"
}
var b strings.Builder
b.WriteString("[")
off := 0
for i := 0; i < sa.Dims[0]; i++ {
b.WriteString(strings.Join(sa.Strings[off:off+sa.Dims[1]], ","))
if i < sa.Dims[0]-1 {
b.WriteString(";")
}
off += sa.Dims[1]
}
b.WriteString("]")
return b.String()
}
// ParseArray parses a rectangular n-dimensional array from a string representation.
// The result will have the same type as the prototype, or an error is returned.
// If the prototype is nil, a mixed array is returned.
// The function can parse arrays that have been formatted with ¯1⍕, ¯2⍕ and ¯3⍕.
// Json arrays (¯2⍕) can only be parsed, if they don't contain complex numbers.
func (a *Apl) ParseArray(prototype Value, s string) (Value, error) {
v, err := a.ScanRankArray(strings.NewReader(s), -1)
if err != nil {
return nil, fmt.Errorf("parse array: %s", err)
}
if prototype != nil {
if u, ok := prototype.(Uniform); ok {
res, ok := a.Unify(v.(Array), true)
if ok == false {
return nil, fmt.Errorf("parse array: array has no uniform type")
}
if reflect.TypeOf(res) != reflect.TypeOf(u) {
return nil, fmt.Errorf("parse array: result has wrong type %T != %T", res, u)
}
return res, nil
}
}
return v, nil
}
// ScanRankArray returns the next sub-array from a RuneScanner of a given rank.
// If rank is 0, it returns a Value that is not an array.
// If the rank is negative, it is not restricted.
// If result may have a smaller rank than requested without an error.
// The format is the same as for ParseArray.
func (a *Apl) ScanRankArray(s io.RuneScanner, rank int) (Value, error) {
var values []Value
c := 0
var shape []int
for {
r, _, err := s.ReadRune()
if err == io.EOF {
break
} else if r == '\n' || r == ';' || r == ']' {
if len(values) == 0 {
continue
}
c++
if c == rank {
break
} else if c > len(shape) {
if shape == nil {
shape = []int{len(values)}
} else {
p := Prod(shape)
shape = append([]int{len(values) / p}, shape...)
}
}
} else if unicode.IsSpace(r) || r == ',' || r == '[' || r == '(' || r == ')' {
continue
} else if r == '"' { // Parse a string.
c = 0
s.UnreadRune()
if str, err := scan.ReadString(s); err != nil {
return nil, fmt.Errorf("parse array: %s", err)
} else {
if rank == 0 {
return String(str), nil
}
values = append(values, String(str))
}
} else { // Parse an number.
c = 0
s.UnreadRune()
num, err := scan.ScanNumber(s)
if err != nil {
return nil, fmt.Errorf("parse array: %s", err)
}
if n, err := a.Tower.Parse(num); err != nil {
return nil, fmt.Errorf("parse array: %s", err)
} else {
if rank == 0 {
return n, nil
}
values = append(values, n.Number)
}
}
}
// The algorithm does not check if the array is uniform in between.
// We just test at the end, if the size matches the shape. This may include false positives.
if len(values) == 0 {
return nil, io.EOF
}
if rank < 0 {
// For rank < 0, we read everything. Data could be closed or not.
rank = len(shape)
if Prod(shape) == len(values) {
rank = len(shape) - 1
}
}
for i := 0; i <= rank-len(shape); i++ {
p := Prod(shape)
if len(shape) == 0 {
p = 1
} else if p == 0 {
return nil, fmt.Errorf("parse array: divide by zero: values: %v shape: %v", values, shape)
}
shape = append([]int{len(values) / p}, shape...)
if Prod(shape) != len(values) {
return nil, fmt.Errorf("parse array: array is not rectangular: ×/%v ≠ %v", shape, len(values))
}
// Continue and fill leading 1s if the rank is higher than data.
}
return MixedArray{
Dims: shape,
Values: values,
}, nil
}