/
script.go
81 lines (75 loc) · 1.52 KB
/
script.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
package turtle
import (
"bufio"
"regexp"
"strconv"
"strings"
)
var reComment = regexp.MustCompile(`#.*$`)
var reSpaces = regexp.MustCompile(`\s+`)
var reCommands = regexp.MustCompile(`([a-zA-Z]+)(?:(-?\d+)(?:,(-?\d+))?)?`) // "CMD-12,-5" "CMD3" or "CMD"
// DrawScript - draw with the script in the buffer
// U12 G 1,-1 # up 12 times, and goto to (1,-1)
func (t *Turtle) DrawScript(script string) *Turtle {
scanner := bufio.NewScanner(strings.NewReader(script))
for scanner.Scan() {
line := scanner.Text()
line = reSpaces.ReplaceAllString(reComment.ReplaceAllString(line, ""), "")
for _, cmd := range reCommands.FindAllStringSubmatch(line, -1) {
if len(cmd) != 4 {
continue
}
name, p1, p2 := cmd[1], 0, 0
var err error
if cmd[2] != "" {
if p1, err = strconv.Atoi(cmd[2]); err != nil {
continue
}
}
if cmd[3] != "" {
if p2, err = strconv.Atoi(cmd[3]); err != nil {
continue
}
}
switch strings.ToUpper(name) {
case "S":
t.Set()
case "C":
t.SetColor(p1)
case "N":
t.Raise()
case "Y":
t.Put()
case "U":
t.Up(p1)
case "D":
t.Down(p1)
case "R":
t.Right(p1)
case "L":
t.Left(p1)
case "UR":
t.UpRight(p1)
case "UL":
t.UpLeft(p1)
case "DR":
t.DownRight(p1)
case "DL":
t.DownLeft(p1)
case "G":
t.GoTo(p1, p2)
case "GA":
t.GoToAbs(p1, p2)
case "LT":
t.LineTo(p1, p2)
case "RT":
t.RectTo(p1, p2)
case "F":
t.Fill()
case "CI":
t.Circle(float64(p1))
}
}
}
return t
}