-
Notifications
You must be signed in to change notification settings - Fork 2
/
funcs.go
86 lines (74 loc) · 1.8 KB
/
funcs.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
// Copyright (c) 2013 Ostap Cherkashin. You can use this source code
// under the terms of the MIT License found in the LICENSE file.
package main
import (
"math"
"strings"
)
type Func struct {
Name string
Type FuncType
Eval func(s *Stack)
}
func FuncTrunc() *Func {
t := FuncType{ScalarType(0), []Type{ScalarType(0)}}
return &Func{"trunc", t, func(s *Stack) {
val := s.PopNum()
val = math.Trunc(val)
s.PushNum(val)
}}
}
func FuncDist() *Func {
t := FuncType{ScalarType(0), []Type{ScalarType(0), ScalarType(0), ScalarType(0), ScalarType(0)}}
return &Func{"dist", t, func(s *Stack) {
lat1 := s.PopNum()
lon1 := s.PopNum()
lat2 := s.PopNum()
lon2 := s.PopNum()
val := Dist(lat1, lon1, lat2, lon2)
s.PushNum(val)
}}
}
func FuncTrim() *Func {
t := FuncType{ScalarType(0), []Type{ScalarType(0)}}
return &Func{"trim", t, func(s *Stack) {
str := s.PopStr()
str = strings.Trim(str, " \t\r\n")
s.PushStr(str)
}}
}
func FuncLower() *Func {
t := FuncType{ScalarType(0), []Type{ScalarType(0)}}
return &Func{"lower", t, func(s *Stack) {
str := s.PopStr()
str = strings.ToLower(str)
s.PushStr(str)
}}
}
func FuncUpper() *Func {
t := FuncType{ScalarType(0), []Type{ScalarType(0)}}
return &Func{"upper", t, func(s *Stack) {
str := s.PopStr()
str = strings.ToUpper(str)
s.PushStr(str)
}}
}
func FuncFuzzy() *Func {
t := FuncType{ScalarType(0), []Type{ScalarType(0), ScalarType(0)}}
return &Func{"fuzzy", t, func(s *Stack) {
se := s.PopStr()
te := s.PopStr()
val := Fuzzy(se, te)
s.PushNum(val)
}}
}
func FuncReplace() *Func {
t := FuncType{ScalarType(0), []Type{ScalarType(0), ScalarType(0), ScalarType(0)}}
return &Func{"replace", t, func(s *Stack) {
str := s.PopStr()
from := s.PopStr()
to := s.PopStr()
str = strings.Replace(str, from, to, -1)
s.PushStr(str)
}}
}