-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.go
56 lines (51 loc) · 1.43 KB
/
interpreter.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
package main
func StartInterpreter (script string) {
// 数据域
memory := NewMemory()
cmdList := []rune(string(script))
var loopMark []int
for cmdIndex := 0; cmdIndex < len(cmdList); cmdIndex ++ {
cmdItem := cmdList[cmdIndex]
if runeEquals(cmdItem, "⌚") {
memory.ToRight()
} else if runeEquals(cmdItem, "👓️") {
memory.ToLeft()
} else if runeEquals(cmdItem, "➕") {
memory.Add()
} else if runeEquals(cmdItem, "📹️") {
memory.Minus()
} else if runeEquals(cmdItem, "🎸") {
memory.ReadNum()
} else if runeEquals(cmdItem, "🤓") {
memory.PrintChar()
} else if runeEquals(cmdItem, "🧙") {
if memory.Get() != 0 {
loopMark = append(loopMark, cmdIndex)
} else {
nextCloseLoopIndex := findNextLoopClose(cmdList, cmdIndex, "🐶")
if nextCloseLoopIndex < 0 {
panic("[Error] 做膜法师🧙的人,必然需要言之有🐶啊,你缺🐶了知道吗?")
} else {
cmdIndex = nextCloseLoopIndex
}
}
} else if runeEquals(cmdItem, "🐶") {
if memory.Get() != 0 {
cmdIndex = loopMark[len(loopMark) - 1]
} else {
loopMark = loopMark[:len(loopMark) - 1]
}
}
}
}
func findNextLoopClose(cmdList []rune, pCurrent int, closeMark string) int {
for i := pCurrent; i < len(cmdList); i++ {
if runeEquals(cmdList[i], closeMark) {
return i
}
}
return -1
}
func runeEquals (runeChar rune, runeStr string) bool {
return runeChar == []rune(runeStr)[0]
}