-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
636 lines (538 loc) · 13.8 KB
/
io.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// Package io contains WDTE functions for dealing with files and other
// types of data streams.
package io
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"github.com/DeedleFake/wdte"
"github.com/DeedleFake/wdte/auto"
"github.com/DeedleFake/wdte/std"
)
// These variables are what are returned by the corresponding
// functions in this package. If a client wants to globally redirect
// input or output, they may simply change these variables.
var (
Stdin io.Reader = os.Stdin
Stdout io.Writer = os.Stdout
Stderr io.Writer = os.Stderr
)
type stdin struct{}
func (r stdin) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return r
}
func (stdin) Read(buf []byte) (int, error) {
return Stdin.Read(buf)
}
func (stdin) String() string {
return "<reader(stdin)>"
}
func (stdin) Reflect(name string) bool { // nolint
return name == "Reader"
}
type stdout struct{}
func (w stdout) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return w
}
func (stdout) Write(data []byte) (int, error) {
return Stdout.Write(data)
}
func (stdout) String() string {
return "<writer(stdout)>"
}
func (stdout) Reflect(name string) bool { // nolint
return name == "Writer"
}
type stderr struct{}
func (w stderr) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func {
return w
}
func (stderr) Write(data []byte) (int, error) {
return Stderr.Write(data)
}
func (stderr) String() string {
return "<writer(stderr)>"
}
func (stderr) Reflect(name string) bool { // nolint
return name == "Writer"
}
type reader interface {
wdte.Func
io.Reader
}
// Reader wraps an io.Reader, allowing it to be used as a WDTE
// function.
//
// Note that using this specific type is not necessary. Any wdte.Func
// that implements io.Reader is also accepted by the functions in this
// module.
type Reader struct {
io.Reader
}
func (r Reader) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
return r
}
func (r Reader) String() string { // nolint
if inner, ok := r.Reader.(fmt.Stringer); ok {
return inner.String()
}
return "<reader>"
}
func (r Reader) Reflect(name string) bool { // nolint
return name == "Reader"
}
type writer interface {
wdte.Func
io.Writer
}
// Writer wraps an io.Writer, allowing it to be used as a WDTE
// function.
//
// Note that using this specific type is not necessary. Any wdte.Func
// that implements io.Writer is also accepted by the functions in this
// module.
type Writer struct {
io.Writer
}
func (w Writer) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
return w
}
func (w Writer) String() string { // nolint
if inner, ok := w.Writer.(fmt.Stringer); ok {
return inner.String()
}
return "<writer>"
}
func (w Writer) Reflect(name string) bool { // nolint
return name == "Writer"
}
// Seek is a WDTE function with the following signatures:
//
// seek s n w
// (seek w) s n
// (seek n w) s
//
// Returns s after seeking s to n, with a relative position denoted by
// w:
//
// If w is greater than 0, it seeks relative to the beginning of s.
//
// If w is equal to 0, it seeks relative to the current location in s.
//
// If w is less than 0, it seeks relative to the end of s.
func Seek(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("seek")
if len(args) < 3 {
return auto.SaveArgsReverse(wdte.GoFunc(Seek), args...)
}
s := args[0].Call(frame).(io.Seeker)
off := int64(args[1].Call(frame).(wdte.Number))
rel := args[2].Call(frame).(wdte.Number)
var w int
switch {
case rel < 0:
w = io.SeekEnd
case rel == 0:
w = io.SeekCurrent
case rel > 0:
w = io.SeekStart
}
_, err := s.Seek(off, w)
if err != nil {
return wdte.Error{Err: err, Frame: frame}
}
return s.(wdte.Func)
}
// Close is a WDTE function with the following signatures:
//
// close c
//
// Returns c after closing it.
func Close(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("close")
if len(args) == 0 {
return wdte.GoFunc(Close)
}
c := args[0].Call(frame).(io.Closer)
err := c.Close()
if err != nil {
return wdte.Error{Err: err, Frame: frame}
}
return c.(wdte.Func)
}
// Combine is a WDTE function with the following signatures:
//
// combine a ...
// (combine a) ...
//
// If the arguments passed are readers, it returns a reader that reads
// each until EOF before continuing to the next, and finally yielding
// EOF itself when the last reader does.
//
// If the arguments passed are writers, it returns a writer that
// writes each write to all of them in turn, only returning when they
// have all returned.
func Combine(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("combine")
if len(args) < 2 {
return auto.SaveArgs(wdte.GoFunc(Combine), args...)
}
switch a0 := args[0].Call(frame).(type) {
case reader:
r := make([]io.Reader, 1, len(args))
r[0] = a0
for _, a := range args[1:] {
r = append(r, a.Call(frame).(reader))
}
return Reader{Reader: io.MultiReader(r...)}
case writer:
w := make([]io.Writer, 1, len(args))
w[0] = a0
for _, a := range args[1:] {
w = append(w, a.Call(frame).(writer))
}
return Writer{Writer: io.MultiWriter(w...)}
default:
panic(fmt.Errorf("Unexpected argument type: %T", a0))
}
}
// Copy is a WDTE function with the following signatures:
//
// copy w r
// (copy w) r
// copy r w
// (copy r) w
//
// Copies from the reader r into the writer w until r yields EOF.
// Returns whichever argument was given second.
//
// The reason for this return discrepency is to allow both variants of
// the function to be used more easily in chains. For example, both of
// the following work:
//
// stdout -> copy stdin -> ... # Later elements will be given stdout.
// stdin -> copy stdout -> ... # Later elements will be given stdin.
func Copy(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("copy")
if len(args) < 2 {
return auto.SaveArgs(wdte.GoFunc(Copy), args...)
}
var w writer
var r reader
var a1 wdte.Func
switch a0 := args[0].Call(frame).(type) {
case writer:
w = a0
r = args[1].Call(frame).(reader)
a1 = r
case reader:
w = args[1].Call(frame).(writer)
r = a0
a1 = w
}
_, err := io.Copy(w, r)
if err != nil {
return wdte.Error{Err: err, Frame: frame}
}
return a1
}
// String is a WDTE function with the following signature:
//
// string r
//
// Reads the entirety of the reader r and returns the result as a
// string.
func String(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("string")
if len(args) == 0 {
return wdte.GoFunc(String)
}
r := args[0].Call(frame).(reader)
var buf bytes.Buffer
_, err := io.Copy(&buf, r)
if err != nil {
return wdte.Error{Err: err, Frame: frame}
}
return wdte.String(buf.String())
}
// scanner is a simple wrapper that allows a bufio.Scanner to be used
// as a stream.Stream.
type scanner struct {
s *bufio.Scanner
}
func (s scanner) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
return s
}
func (s scanner) Next(frame wdte.Frame) (wdte.Func, bool) { // nolint
ok := s.s.Scan()
if !ok {
err := s.s.Err()
if err != nil {
return wdte.Error{Err: err, Frame: frame}, false
}
return nil, false
}
return wdte.String(s.s.Text()), true
}
func (s scanner) Reflect(name string) bool { // nolint
return name == "Stream"
}
// Lines is a WDTE function with the following signature:
//
// lines r
//
// Returns a stream.Stream that yields, as strings, successive lines
// read from the reader r.
func Lines(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("lines")
if len(args) == 0 {
return wdte.GoFunc(Lines)
}
r := args[0].Call(frame).(reader)
return scanner{s: bufio.NewScanner(r)}
}
// Words is a WDTE function with the following signature:
//
// words r
//
// Returns a stream.Stream that yields, as strings, successive words
// read from the reader r.
func Words(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("words")
if len(args) == 0 {
return wdte.GoFunc(Words)
}
r := args[0].Call(frame).(reader)
s := bufio.NewScanner(r)
s.Split(bufio.ScanWords)
return scanner{s: s}
}
// Scan is a WDTE function with the following signatures:
//
// scan r sep
// (scan r) sep
// scan sep r
// (scan sep) r
//
// Returns a stream.Stream that yields sections of the reader r split
// around the separator string sep. For example,
//
// readString 'this--is--an--example' -> scan '--'
//
// will return a stream.Stream that will yield 'this', 'is', 'an', and
// 'example'.
func Scan(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("scan")
if len(args) < 2 {
return auto.SaveArgs(wdte.GoFunc(Scan), args...)
}
var r reader
var sep wdte.String
switch a0 := args[0].Call(frame).(type) {
case reader:
r = a0
sep = args[1].Call(frame).(wdte.String)
case wdte.String:
r = args[1].Call(frame).(reader)
sep = a0
}
seb := []byte(sep)
s := bufio.NewScanner(r)
s.Split(func(data []byte, eof bool) (int, []byte, error) {
start := bytes.Index(data, seb)
if start < 0 {
if eof {
if len(data) == 0 {
return 1, nil, nil
}
return len(data), data, nil
}
return 0, nil, nil
}
return start + len(seb), data[:start], nil
})
return scanner{s: s}
}
type runeStream struct {
r io.RuneReader
}
func (r runeStream) Call(frame wdte.Frame, args ...wdte.Func) wdte.Func { // nolint
return r
}
func (r runeStream) Next(frame wdte.Frame) (wdte.Func, bool) { // nolint
c, _, err := r.r.ReadRune()
if err != nil {
if err == io.EOF {
return nil, false
}
return wdte.Error{Frame: frame, Err: err}, true
}
return wdte.Number(c), true
}
func (r runeStream) Reflect(name string) bool { // nolint
return name == "Stream"
}
// Runes is a WDTE function with the following signature:
//
// runes r
//
// Returns a stream.Stream that yields individual Unicode characters
// from the reader r as numbers.
//
// TODO: Maybe it makes more sense for them to be yielded as strings
// with a length of one.
func Runes(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("runes")
if len(args) == 0 {
return wdte.GoFunc(Runes)
}
a := args[0].Call(frame)
var r io.RuneReader
switch a := a.(type) {
case io.RuneReader:
r = a
case io.Reader:
r = bufio.NewReader(a)
default:
panic(fmt.Errorf("Unexpected argument type: %T", a))
}
return runeStream{r: r}
}
func write(f func(io.Writer, interface{}) error) (gf wdte.Func) {
return wdte.GoFunc(func(frame wdte.Frame, args ...wdte.Func) wdte.Func {
if len(args) < 2 {
return auto.SaveArgsReverse(gf, args...)
}
var w writer
var d wdte.Func
switch a0 := args[0].Call(frame).(type) {
case writer:
w = a0
d = args[1].Call(frame)
case wdte.Func:
d = a0
w = args[1].Call(frame).(writer)
}
err := f(w, d)
if err != nil {
return wdte.Error{Err: err, Frame: frame}
}
return w
})
}
// Write is a WDTE function with the following signatures:
//
// write w d
// (write w) d
// write d w
// (write d) w
//
// It writes the data d to the writer w in much the same way that Go's
// fmt.Fprint does. It returns w to allow for easier chaining.
//
// If both arguments are writers, it will consider either the first
// argument or the outer argument to be w.
func Write(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("write")
return write(func(w io.Writer, v interface{}) error {
_, err := fmt.Fprint(w, v)
return err
}).Call(frame, args...)
}
// Writeln is a WDTE function with the following signatures:
//
// writeln w d
// (writeln w) d
// writeln d w
// (writeln d) w
//
// It writes the data d to the writer w in much the same way that Go's
// fmt.Fprintln does. It returns w to allow for easier chaining.
//
// If both arguments are writers, it will consider either the first
// argument or the outer argument to be w.
func Writeln(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("writeln")
return write(func(w io.Writer, v interface{}) error {
_, err := fmt.Fprintln(w, v)
return err
}).Call(frame, args...)
}
// Panic is a WDTE function with the following signatures:
//
// panic err
// panic w err
// panic desc err
// panic w desc err
//
// Note that, somewhat unusually, Panic accepts its arguments in any order.
//
// It writes the given error to w, prepending the optional
// description in the form `desc: err` and appending a newline. It
// then returns the error. If an error occurs somewhere internally,
// such as while printing, that error is returned instead.
//
// If w is not given, it defaults to Stderr.
//
// Panic is primarily intended for use with the error chain operator.
// For example:
//
// + a b -| panic 'Failed to add a and b';
func Panic(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("panic")
w := writer(stderr{})
var desc wdte.String
var e error
set := func(f wdte.Func) {
switch f := f.(type) {
case writer:
w = f
case wdte.String:
desc = f + ": "
case error:
e = f
default:
panic(fmt.Errorf("Unexpected argument type: %T", f))
}
}
n := 3
if len(args) < 3 {
n = len(args)
}
for i, arg := range args[:n] {
args[i] = arg.Call(frame)
set(args[i])
}
if e == nil {
return auto.SaveArgs(wdte.GoFunc(Panic), args...)
}
_, err := fmt.Fprintf(w, "%v%v\n", desc, e)
if err != nil {
return wdte.Error{Err: err, Frame: frame}
}
return e.(wdte.Func)
}
// Scope is a scope containing the functions in this package.
var Scope = wdte.S().Map(map[wdte.ID]wdte.Func{
"stdin": stdin{},
"stdout": stdout{},
"stderr": stderr{},
"seek": wdte.GoFunc(Seek),
"close": wdte.GoFunc(Close),
"combine": wdte.GoFunc(Combine),
"copy": wdte.GoFunc(Copy),
"string": wdte.GoFunc(String),
"lines": wdte.GoFunc(Lines),
"words": wdte.GoFunc(Words),
"scan": wdte.GoFunc(Scan),
"runes": wdte.GoFunc(Runes),
"write": wdte.GoFunc(Write),
"writeln": wdte.GoFunc(Writeln),
"panic": wdte.GoFunc(Panic),
//"panicln": wdte.GoFunc(Panicln),
})
func init() {
std.Register("io", Scope)
}