-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme_test.go
57 lines (46 loc) · 1.05 KB
/
readme_test.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
package wdte_test
import (
"fmt"
"os"
"strings"
"github.com/DeedleFake/wdte"
"github.com/DeedleFake/wdte/wdteutil"
)
const src = `
let i => import 'some/import/path/or/another';
i.print 3;
+ 5 2 -> i.print;
7 -> + 5 -> i.print;
`
func im(from string) (*wdte.Scope, error) {
return wdte.S().Map(map[wdte.ID]wdte.Func{
"print": wdteutil.Func("print", func(v interface{}) interface{} {
fmt.Println(v)
return v
}),
}), nil
}
func Sum(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("+")
if len(args) < 2 {
return wdteutil.SaveArgs(wdte.GoFunc(Sum), args...)
}
var sum wdte.Number
for _, arg := range args {
sum += arg.(wdte.Number)
}
return sum
}
func Example() {
m, err := wdte.Parse(strings.NewReader(src), wdte.ImportFunc(im), nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing script: %v\n", err)
os.Exit(1)
}
scope := wdte.S().Add("+", wdte.GoFunc(Sum))
r := m.Call(wdte.F().WithScope(scope))
if err, ok := r.(error); ok {
fmt.Fprintf(os.Stderr, "Error running script: %v\n", err)
os.Exit(1)
}
}