-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
84 lines (69 loc) · 1.22 KB
/
parser.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
package parser
import (
"strconv"
"strings"
)
type Strategy interface {
SetAutoTilt(tilt bool)
MoveUp()
MoveDown()
TiltUp()
TiltDown()
Move(x, y float32)
}
type Command string
const (
AutoTilt Command = "auto_tilt"
TiltUp Command = "tilt_up"
TiltDown Command = "tilt_down"
MoveUp Command = "move_up"
MoveDown Command = "move_down"
Move Command = "move"
)
const Delimeter = "|"
func Parse(input string, strategy Strategy) {
args := strings.Split(input, Delimeter)
if len(args) == 0 {
return
}
command := Command(args[0])
args = args[1:]
switch command {
case MoveUp:
if len(args) == 0 {
strategy.MoveUp()
}
case MoveDown:
if len(args) == 0 {
strategy.MoveDown()
}
case AutoTilt:
if len(args) == 1 {
autoTilt, err := strconv.ParseBool(args[0])
if err != nil {
return
}
strategy.SetAutoTilt(autoTilt)
}
case TiltDown:
if len(args) == 0 {
strategy.TiltDown()
}
case TiltUp:
if len(args) == 0 {
strategy.TiltUp()
}
case Move:
if len(args) == 2 {
x, err := strconv.ParseFloat(args[0], 32)
if err != nil {
return
}
y, err := strconv.ParseFloat(args[1], 32)
if err != nil {
return
}
strategy.Move(float32(x), float32(y))
}
}
}