/
functions.go
725 lines (681 loc) · 17.9 KB
/
functions.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
// Evaluate builtin and user-defined function calls
package interp
import (
"bytes"
"errors"
"fmt"
"io"
"math"
"os/exec"
"reflect"
"sort"
"strconv"
"strings"
"syscall"
"time"
"unicode/utf8"
. "github.com/benhoyt/goawk/internal/ast"
. "github.com/benhoyt/goawk/lexer"
)
// Call builtin function specified by "op" with given args
func (p *interp) callBuiltin(op Token, argExprs []Expr) (value, error) {
// split() has an array arg (not evaluated) and [g]sub() have an
// lvalue arg, so handle them as special cases
switch op {
case F_SPLIT:
strValue, err := p.eval(argExprs[0])
if err != nil {
return null(), err
}
str := p.toString(strValue)
var fieldSep string
if len(argExprs) == 3 {
sepValue, err := p.eval(argExprs[2])
if err != nil {
return null(), err
}
fieldSep = p.toString(sepValue)
} else {
fieldSep = p.fieldSep
}
arrayExpr := argExprs[1].(*ArrayExpr)
n, err := p.split(str, arrayExpr.Scope, arrayExpr.Index, fieldSep)
if err != nil {
return null(), err
}
return num(float64(n)), nil
case F_SUB, F_GSUB:
regexValue, err := p.eval(argExprs[0])
if err != nil {
return null(), err
}
regex := p.toString(regexValue)
replValue, err := p.eval(argExprs[1])
if err != nil {
return null(), err
}
repl := p.toString(replValue)
var in string
if len(argExprs) == 3 {
inValue, err := p.eval(argExprs[2])
if err != nil {
return null(), err
}
in = p.toString(inValue)
} else {
in = p.line
}
out, n, err := p.sub(regex, repl, in, op == F_GSUB)
if err != nil {
return null(), err
}
if len(argExprs) == 3 {
err := p.assign(argExprs[2], str(out))
if err != nil {
return null(), err
}
} else {
p.setLine(out)
}
return num(float64(n)), nil
}
// Now evaluate the argExprs (calls with up to 7 args don't
// require heap allocation)
args := make([]value, 0, 7)
for _, a := range argExprs {
arg, err := p.eval(a)
if err != nil {
return null(), err
}
args = append(args, arg)
}
// Then switch on the function for the ordinary functions
switch op {
case F_LENGTH:
switch len(args) {
case 0:
return num(float64(len(p.line))), nil
default:
return num(float64(len(p.toString(args[0])))), nil
}
case F_MATCH:
re, err := p.compileRegex(p.toString(args[1]))
if err != nil {
return null(), err
}
loc := re.FindStringIndex(p.toString(args[0]))
if loc == nil {
p.matchStart = 0
p.matchLength = -1
return num(0), nil
}
p.matchStart = loc[0] + 1
p.matchLength = loc[1] - loc[0]
return num(float64(p.matchStart)), nil
case F_SUBSTR:
s := p.toString(args[0])
pos := int(args[1].num())
if pos > len(s) {
pos = len(s) + 1
}
if pos < 1 {
pos = 1
}
maxLength := len(s) - pos + 1
length := maxLength
if len(args) == 3 {
length = int(args[2].num())
if length < 0 {
length = 0
}
if length > maxLength {
length = maxLength
}
}
return str(s[pos-1 : pos-1+length]), nil
case F_SPRINTF:
s, err := p.sprintf(p.toString(args[0]), args[1:])
if err != nil {
return null(), err
}
return str(s), nil
case F_INDEX:
s := p.toString(args[0])
substr := p.toString(args[1])
return num(float64(strings.Index(s, substr) + 1)), nil
case F_TOLOWER:
return str(strings.ToLower(p.toString(args[0]))), nil
case F_TOUPPER:
return str(strings.ToUpper(p.toString(args[0]))), nil
case F_ATAN2:
return num(math.Atan2(args[0].num(), args[1].num())), nil
case F_COS:
return num(math.Cos(args[0].num())), nil
case F_EXP:
return num(math.Exp(args[0].num())), nil
case F_INT:
return num(float64(int(args[0].num()))), nil
case F_LOG:
return num(math.Log(args[0].num())), nil
case F_SQRT:
return num(math.Sqrt(args[0].num())), nil
case F_RAND:
return num(p.random.Float64()), nil
case F_SIN:
return num(math.Sin(args[0].num())), nil
case F_SRAND:
prevSeed := p.randSeed
switch len(args) {
case 0:
p.random.Seed(time.Now().UnixNano())
case 1:
p.randSeed = args[0].num()
p.random.Seed(int64(math.Float64bits(p.randSeed)))
}
return num(prevSeed), nil
case F_SYSTEM:
if p.noExec {
return null(), newError("can't call system() due to NoExec")
}
cmdline := p.toString(args[0])
cmd := exec.Command("sh", "-c", cmdline)
cmd.Stdout = p.output
cmd.Stderr = p.errorOutput
err := cmd.Start()
if err != nil {
fmt.Fprintln(p.errorOutput, err)
return num(-1), nil
}
err = cmd.Wait()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
return num(float64(status.ExitStatus())), nil
} else {
fmt.Fprintf(p.errorOutput, "couldn't get exit status for %q: %v\n", cmdline, err)
return num(-1), nil
}
} else {
fmt.Fprintf(p.errorOutput, "unexpected error running command %q: %v\n", cmdline, err)
return num(-1), nil
}
}
return num(0), nil
case F_CLOSE:
name := p.toString(args[0])
var c io.Closer = p.inputStreams[name]
if c != nil {
// Close input stream
delete(p.inputStreams, name)
err := c.Close()
if err != nil {
return num(-1), nil
}
return num(0), nil
}
c = p.outputStreams[name]
if c != nil {
// Close output stream
delete(p.outputStreams, name)
err := c.Close()
if err != nil {
return num(-1), nil
}
return num(0), nil
}
// Nothing to close
return num(-1), nil
case F_FFLUSH:
var name string
if len(args) > 0 {
name = p.toString(args[0])
}
var ok bool
if name != "" {
// Flush a single, named output stream
ok = p.flushStream(name)
} else {
// fflush() or fflush("") flushes all output streams
ok = p.flushAll()
}
if !ok {
return num(-1), nil
}
return num(0), nil
default:
// Shouldn't happen
panic(fmt.Sprintf("unexpected function: %s", op))
}
}
// Call user-defined function with given index and arguments, return
// its return value (or null value if it doesn't return anything)
func (p *interp) callUser(index int, args []Expr) (value, error) {
f := p.program.Functions[index]
if p.callDepth >= maxCallDepth {
return null(), newError("calling %q exceeded maximum call depth of %d", f.Name, maxCallDepth)
}
// Evaluate the arguments and push them onto the locals stack
oldFrame := p.frame
newFrameStart := len(p.stack)
var arrays []int
for i, arg := range args {
if f.Arrays[i] {
a := arg.(*VarExpr)
arrays = append(arrays, p.getArrayIndex(a.Scope, a.Index))
} else {
argValue, err := p.eval(arg)
if err != nil {
return null(), err
}
p.stack = append(p.stack, argValue)
}
}
// Push zero value for any additional parameters (it's valid to
// call a function with fewer arguments than it has parameters)
oldArraysLen := len(p.arrays)
for i := len(args); i < len(f.Params); i++ {
if f.Arrays[i] {
arrays = append(arrays, len(p.arrays))
p.arrays = append(p.arrays, make(map[string]value))
} else {
p.stack = append(p.stack, null())
}
}
p.frame = p.stack[newFrameStart:]
p.localArrays = append(p.localArrays, arrays)
// Execute the function!
p.callDepth++
err := p.executes(f.Body)
p.callDepth--
// Pop the locals off the stack
p.stack = p.stack[:newFrameStart]
p.frame = oldFrame
p.localArrays = p.localArrays[:len(p.localArrays)-1]
p.arrays = p.arrays[:oldArraysLen]
if r, ok := err.(returnValue); ok {
return r.Value, nil
}
if err != nil {
return null(), err
}
return null(), nil
}
// Call native-defined function with given name and arguments, return
// return value (or null value if it doesn't return anything).
func (p *interp) callNative(index int, args []Expr) (value, error) {
f := p.nativeFuncs[index]
minIn := len(f.in) // Mininum number of args we should pass
var variadicType reflect.Type
if f.isVariadic {
variadicType = f.in[len(f.in)-1].Elem()
minIn--
}
// Build list of args to pass to function
values := make([]reflect.Value, 0, 7) // up to 7 args won't require heap allocation
for i, arg := range args {
a, err := p.eval(arg)
if err != nil {
return null(), err
}
var argType reflect.Type
if !f.isVariadic || i < len(f.in)-1 {
argType = f.in[i]
} else {
// Final arg(s) when calling a variadic are all of this type
argType = variadicType
}
values = append(values, p.toNative(a, argType))
}
// Use zero value for any unspecified args
for i := len(args); i < minIn; i++ {
values = append(values, reflect.Zero(f.in[i]))
}
// Call Go function, determine return value
outs := f.value.Call(values)
switch len(outs) {
case 0:
// No return value, return null value to AWK
return null(), nil
case 1:
// Single return value
return fromNative(outs[0]), nil
case 2:
// Two-valued return of (scalar, error)
if !outs[1].IsNil() {
return null(), outs[1].Interface().(error)
}
return fromNative(outs[0]), nil
default:
// Should never happen (checked at parse time)
panic(fmt.Sprintf("unexpected number of return values: %d", len(outs)))
}
}
// Convert from an AWK value to a native Go value
func (p *interp) toNative(v value, typ reflect.Type) reflect.Value {
switch typ.Kind() {
case reflect.Bool:
return reflect.ValueOf(v.boolean())
case reflect.Int:
return reflect.ValueOf(int(v.num()))
case reflect.Int8:
return reflect.ValueOf(int8(v.num()))
case reflect.Int16:
return reflect.ValueOf(int16(v.num()))
case reflect.Int32:
return reflect.ValueOf(int32(v.num()))
case reflect.Int64:
return reflect.ValueOf(int64(v.num()))
case reflect.Uint:
return reflect.ValueOf(uint(v.num()))
case reflect.Uint8:
return reflect.ValueOf(uint8(v.num()))
case reflect.Uint16:
return reflect.ValueOf(uint16(v.num()))
case reflect.Uint32:
return reflect.ValueOf(uint32(v.num()))
case reflect.Uint64:
return reflect.ValueOf(uint64(v.num()))
case reflect.Float32:
return reflect.ValueOf(float32(v.num()))
case reflect.Float64:
return reflect.ValueOf(v.num())
case reflect.String:
return reflect.ValueOf(p.toString(v))
case reflect.Slice:
if typ.Elem().Kind() != reflect.Uint8 {
// Shouldn't happen: prevented by checkNativeFunc
panic(fmt.Sprintf("unexpected argument slice: %s", typ.Elem().Kind()))
}
return reflect.ValueOf([]byte(p.toString(v)))
default:
// Shouldn't happen: prevented by checkNativeFunc
panic(fmt.Sprintf("unexpected argument type: %s", typ.Kind()))
}
}
// Convert from a native Go value to an AWK value
func fromNative(v reflect.Value) value {
switch v.Kind() {
case reflect.Bool:
return boolean(v.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return num(float64(v.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return num(float64(v.Uint()))
case reflect.Float32, reflect.Float64:
return num(v.Float())
case reflect.String:
return str(v.String())
case reflect.Slice:
if b, ok := v.Interface().([]byte); ok {
return str(string(b))
}
// Shouldn't happen: prevented by checkNativeFunc
panic(fmt.Sprintf("unexpected return slice: %s", v.Type().Elem().Kind()))
default:
// Shouldn't happen: prevented by checkNativeFunc
panic(fmt.Sprintf("unexpected return type: %s", v.Kind()))
}
}
// Used for caching native function type information on init
type nativeFunc struct {
isVariadic bool
in []reflect.Type
value reflect.Value
}
// Check and initialize native functions
func (p *interp) initNativeFuncs(funcs map[string]interface{}) error {
for name, f := range funcs {
err := checkNativeFunc(name, f)
if err != nil {
return err
}
}
// Sort functions by name, then use those indexes to build slice
// (this has to match how the parser sets the indexes).
names := make([]string, 0, len(funcs))
for name := range funcs {
names = append(names, name)
}
sort.Strings(names)
p.nativeFuncs = make([]nativeFunc, len(names))
for i, name := range names {
f := funcs[name]
typ := reflect.TypeOf(f)
in := make([]reflect.Type, typ.NumIn())
for j := 0; j < len(in); j++ {
in[j] = typ.In(j)
}
p.nativeFuncs[i] = nativeFunc{
isVariadic: typ.IsVariadic(),
in: in,
value: reflect.ValueOf(f),
}
}
return nil
}
// Got this trick from the Go stdlib text/template source
var errorType = reflect.TypeOf((*error)(nil)).Elem()
// Check that native function with given name is okay to call from
// AWK, return a *interp.Error if not. This checks that f is actually
// a function, and that its parameter and return types are good.
func checkNativeFunc(name string, f interface{}) error {
if KeywordToken(name) != ILLEGAL {
return newError("can't use keyword %q as native function name", name)
}
typ := reflect.TypeOf(f)
if typ.Kind() != reflect.Func {
return newError("native function %q is not a function", name)
}
for i := 0; i < typ.NumIn(); i++ {
param := typ.In(i)
if typ.IsVariadic() && i == typ.NumIn()-1 {
param = param.Elem()
}
if !validNativeType(param) {
return newError("native function %q param %d is not int or string", name, i)
}
}
switch typ.NumOut() {
case 0:
// No return value is fine
case 1:
// Single scalar return value is fine
if !validNativeType(typ.Out(0)) {
return newError("native function %q return value is not int or string", name)
}
case 2:
// Returning (scalar, error) is handled too
if !validNativeType(typ.Out(0)) {
return newError("native function %q first return value is not int or string", name)
}
if typ.Out(1) != errorType {
return newError("native function %q second return value is not an error", name)
}
default:
return newError("native function %q returns more than two values", name)
}
return nil
}
// Return true if typ is a valid parameter or return type.
func validNativeType(typ reflect.Type) bool {
switch typ.Kind() {
case reflect.Bool:
return true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
case reflect.Float32, reflect.Float64:
return true
case reflect.String:
return true
case reflect.Slice:
// Only allow []byte (convert to string in AWK)
return typ.Elem().Kind() == reflect.Uint8
default:
return false
}
}
// Guts of the split() function
func (p *interp) split(s string, scope VarScope, index int, fs string) (int, error) {
var parts []string
if fs == " " {
parts = strings.Fields(s)
} else if s == "" {
// NF should be 0 on empty line
} else if utf8.RuneCountInString(fs) <= 1 {
parts = strings.Split(s, fs)
} else {
re, err := p.compileRegex(fs)
if err != nil {
return 0, err
}
parts = re.Split(s, -1)
}
array := make(map[string]value, len(parts))
for i, part := range parts {
array[strconv.Itoa(i+1)] = numStr(part)
}
p.arrays[p.getArrayIndex(scope, index)] = array
return len(array), nil
}
// Guts of the sub() and gsub() functions
func (p *interp) sub(regex, repl, in string, global bool) (out string, num int, err error) {
re, err := p.compileRegex(regex)
if err != nil {
return "", 0, err
}
count := 0
out = re.ReplaceAllStringFunc(in, func(s string) string {
// Only do the first replacement for sub(), or all for gsub()
if !global && count > 0 {
return s
}
count++
// Handle & (ampersand) properly in replacement string
r := make([]byte, 0, 64) // Up to 64 byte replacement won't require heap allocation
for i := 0; i < len(repl); i++ {
switch repl[i] {
case '&':
r = append(r, s...)
case '\\':
i++
if i < len(repl) {
switch repl[i] {
case '&':
r = append(r, '&')
case '\\':
r = append(r, '\\')
default:
r = append(r, '\\', repl[i])
}
} else {
r = append(r, '\\')
}
default:
r = append(r, repl[i])
}
}
return string(r)
})
return out, count, nil
}
type cachedFormat struct {
format string
types []byte
}
// Parse given sprintf format string into Go format string, along with
// type conversion specifiers. Output is memoized in a simple cache
// for performance.
func (p *interp) parseFmtTypes(s string) (format string, types []byte, err error) {
if item, ok := p.formatCache[s]; ok {
return item.format, item.types, nil
}
out := []byte(s)
for i := 0; i < len(s); i++ {
if s[i] == '%' {
i++
if i >= len(s) {
return "", nil, errors.New("expected type specifier after %")
}
if s[i] == '%' {
continue
}
for i < len(s) && bytes.IndexByte([]byte(" .-+*#0123456789"), s[i]) >= 0 {
if s[i] == '*' {
types = append(types, 'd')
}
i++
}
if i >= len(s) {
return "", nil, errors.New("expected type specifier after %")
}
var t byte
switch s[i] {
case 's':
t = 's'
case 'd', 'i', 'o', 'x', 'X':
t = 'd'
case 'f', 'e', 'E', 'g', 'G':
t = 'f'
case 'u':
t = 'u'
out[i] = 'd'
case 'c':
t = 'c'
out[i] = 's'
default:
return "", nil, fmt.Errorf("invalid format type %q", s[i])
}
types = append(types, t)
}
}
// Dumb, non-LRU cache: just cache the first N formats
format = string(out)
if len(p.formatCache) < maxCachedFormats {
p.formatCache[s] = cachedFormat{format, types}
}
return format, types, nil
}
// Guts of sprintf() function (also used by "printf" statement)
func (p *interp) sprintf(format string, args []value) (string, error) {
format, types, err := p.parseFmtTypes(format)
if err != nil {
return "", newError("format error: %s", err)
}
if len(types) > len(args) {
return "", newError("format error: got %d args, expected %d", len(args), len(types))
}
converted := make([]interface{}, len(types))
for i, t := range types {
a := args[i]
var v interface{}
switch t {
case 's':
v = p.toString(a)
case 'd':
v = int(a.num())
case 'f':
v = a.num()
case 'u':
v = uint32(a.num())
case 'c':
var c []byte
if a.isTrueStr() {
s := p.toString(a)
if len(s) > 0 {
c = []byte{s[0]}
} else {
c = []byte{0}
}
} else {
// Follow the behaviour of awk and mawk, where %c
// operates on bytes (0-255), not Unicode codepoints
c = []byte{byte(a.num())}
}
v = c
}
converted[i] = v
}
return fmt.Sprintf(format, converted...), nil
}