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
|
// Don't run these on Windows, because newline handling means they don't pass.
//go:build !windows
// +build !windows
package interp_test
import (
"fmt"
"strings"
"github.com/benhoyt/goawk/interp"
"github.com/benhoyt/goawk/parser"
)
func Example() {
input := strings.NewReader("foo bar\n\nbaz buz")
err := interp.Exec("$0 { print $1 }", " ", input, nil)
if err != nil {
fmt.Println(err)
return
}
// Output:
// foo
// baz
}
func Example_fieldsep() {
// Use ',' as the field separator
input := strings.NewReader("1,2\n3,4")
err := interp.Exec("{ print $1, $2 }", ",", input, nil)
if err != nil {
fmt.Println(err)
return
}
// Output:
// 1 2
// 3 4
}
func Example_program() {
src := "{ print NR, tolower($0) }"
input := "A\naB\nAbC"
prog, err := parser.ParseProgram([]byte(src), nil)
if err != nil {
fmt.Println(err)
return
}
config := &interp.Config{
Stdin: strings.NewReader(input),
Vars: []string{"OFS", ":"},
}
_, err = interp.ExecProgram(prog, config)
if err != nil {
fmt.Println(err)
return
}
// Output:
// 1:a
// 2:ab
// 3:abc
}
func Example_funcs() {
src := `BEGIN { print sum(), sum(1), sum(2, 3, 4), repeat("xyz", 3) }`
parserConfig := &parser.ParserConfig{
Funcs: map[string]interface{}{
"sum": func(args ...float64) float64 {
sum := 0.0
for _, a := range args {
sum += a
}
return sum
},
"repeat": strings.Repeat,
},
}
prog, err := parser.ParseProgram([]byte(src), parserConfig)
if err != nil {
fmt.Println(err)
return
}
interpConfig := &interp.Config{
Funcs: parserConfig.Funcs,
}
_, err = interp.ExecProgram(prog, interpConfig)
if err != nil {
fmt.Println(err)
return
}
// Output:
// 0 1 9 xyzxyzxyz
}
func Example_new() {
// We'll execute this program multiple times on different inputs.
src := `{ print $1, x, $3; x++ }`
// Parse the program and set up the interpreter.
prog, err := parser.ParseProgram([]byte(src), nil)
if err != nil {
fmt.Println(err)
return
}
interpreter, err := interp.New(prog)
if err != nil {
fmt.Println(err)
return
}
// Run it once on one input.
_, err = interpreter.Execute(&interp.Config{
Stdin: strings.NewReader("one two three"),
Environ: []string{}, // avoid calling os.Environ each time
})
if err != nil {
fmt.Println(err)
return
}
// Reset variables and run it again efficiently on a different input (this
// could be from a completely different data source).
interpreter.ResetVars()
_, err = interpreter.Execute(&interp.Config{
Stdin: strings.NewReader("a b c\nd e f\n"),
Environ: []string{},
})
if err != nil {
fmt.Println(err)
return
}
// Run it on another input, this time without resetting variables.
_, err = interpreter.Execute(&interp.Config{
Stdin: strings.NewReader("x y z"),
Environ: []string{},
})
if err != nil {
fmt.Println(err)
return
}
// Output:
// one three
// a c
// d 1 f
// x 2 z
}
func Example_csv() {
src := `{ total += @"amount" } END { print total }`
input := `# comment
name,amount
Bob,17.50
Jill,20
"Boba Fett",100.00
`
prog, err := parser.ParseProgram([]byte(src), nil)
if err != nil {
fmt.Println(err)
return
}
config := &interp.Config{
Stdin: strings.NewReader(input),
InputMode: interp.CSVMode,
CSVInput: interp.CSVInputConfig{Comment: '#', Header: true},
}
_, err = interp.ExecProgram(prog, config)
if err != nil {
fmt.Println(err)
return
}
// Output:
// 137.5
}
|