-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
150 lines (138 loc) · 2.76 KB
/
command.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package brainfun
import (
"errors"
"io"
"math"
)
func Inc(state *State) error {
if state.Skip {
return nil
}
if state.DataPointer >= uint64(len(state.Data)) {
return errors.New("array out of bound")
}
val := state.Data[state.DataPointer]
if val == math.MaxUint8 {
val = 0
} else {
val++
}
state.Data[state.DataPointer] = val
return nil
}
func Dec(state *State) error {
if state.Skip {
return nil
}
if state.DataPointer >= uint64(len(state.Data)) {
return errors.New("array out of bound")
}
val := state.Data[state.DataPointer]
if val == 0 {
val = math.MaxUint8
} else {
val--
}
state.Data[state.DataPointer] = val
return nil
}
func MoveLeft(state *State) error {
if state.Skip {
return nil
}
val := state.DataPointer
if val == 0 {
val = uint64(len(state.Data))
}
val--
state.DataPointer = val
return nil
}
func MoveRight(state *State) error {
if state.Skip {
return nil
}
val := state.DataPointer
if val >= uint64(len(state.Data))-1 {
val = 0
} else {
val++
}
state.DataPointer = val
return nil
}
func LoopStart(state *State) error {
if state.Skip {
val, ok := state.Stack.Pop() // bracket counter, not the link to the start of the loop
if !ok {
return errors.New("wrong start of the loop: no beginning")
}
state.Stack.Push(val + 1)
return nil
}
if state.DataPointer >= uint64(len(state.Data)) {
return errors.New("array out of bound")
}
val := state.Data[state.DataPointer]
if val == 0 {
state.Skip = true
state.Stack.Push(0) // bracket counter, not the link to the start
return nil
}
state.Stack.Push(state.HistoryPointer - 1)
return nil
}
func LoopEnd(state *State) error {
if state.Stack.Size() == 0 {
return errors.New("wrong end of the loop: no beginning")
}
stackVal, _ := state.Stack.Pop()
if state.Skip {
if stackVal == 0 {
// bracket counter equals zero, can stop skipping
state.Skip = false
return nil
}
state.Stack.Push(stackVal - 1)
return nil
}
val := state.Data[state.DataPointer]
if val > 0 {
state.HistoryPointer = stackVal
return nil
}
return nil
}
func PrintWith(writer io.Writer) Transform {
return func(state *State) error {
if state.Skip {
return nil
}
if state.DataPointer >= uint64(len(state.Data)) {
return errors.New("array out of bound")
}
val := state.Data[state.DataPointer]
_, err := writer.Write([]byte(string(val)))
return err
}
}
func ReadWith(reader io.Reader) Transform {
return func(state *State) error {
if state.Skip {
return nil
}
if state.DataPointer >= uint64(len(state.Data)) {
return errors.New("array out of bound")
}
buf := make([]byte, 1)
_, err := reader.Read(buf)
if err == io.EOF {
return nil
}
if err != nil {
return err
}
state.Data[state.DataPointer] = buf[0]
return nil
}
}