-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
246 lines (227 loc) · 4.17 KB
/
runner.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package befunge
import (
"fmt"
"io"
"math/rand"
"os"
"strconv"
)
// Runner befunge runner.
type Runner struct {
Scanner
stack []int
input io.Reader
output io.Writer
step func()
debug bool
errors []error
randFunc func(n int) int
}
// NewRunner create a new befunge codes runner.
func NewRunner(s []byte) *Runner {
return &Runner{
Scanner: *NewScanner(s),
output: os.Stdout,
input: os.Stdin,
randFunc: rand.Intn,
}
}
// Stack returns stack.
func (r *Runner) Stack() []int {
return r.stack
}
// SetInput sets input.
func (r *Runner) SetInput(input io.Reader) {
r.input = input
}
// SetOutput sets output.
func (r *Runner) SetOutput(output io.Writer) {
r.output = output
}
// SetRandFunc sets randFunc.
func (r *Runner) SetRandFunc(randFunc func(n int) int) {
r.randFunc = randFunc
}
// SetStep sets step.
func (r *Runner) SetStep(f func()) {
r.step = f
}
// SetDebug sets debug.
func (r *Runner) SetDebug(e bool) {
r.debug = e
}
// Put value to the stack.
func (r *Runner) Put(i int) {
r.stack = append(r.stack, i)
}
// Get value from of the stack.
func (r *Runner) Get() int {
if len(r.stack) == 0 {
return 0
}
v := r.stack[len(r.stack)-1]
r.stack = r.stack[:len(r.stack)-1]
return v
}
// Swap two values on top of the stack.
func (r *Runner) Swap() {
switch len(r.stack) {
case 0:
return
case 1:
r.stack = append(r.stack, 0)
}
r.stack[len(r.stack)-1], r.stack[len(r.stack)-2] = r.stack[len(r.stack)-2], r.stack[len(r.stack)-1]
}
// Duplicate value on top of the stack.
func (r *Runner) Duplicate() {
if len(r.stack) == 0 {
r.stack = append(r.stack, 0)
return
}
r.stack = append(r.stack, r.stack[len(r.stack)-1])
return
}
func (r *Runner) runStep() (bool, error) {
val, ch := r.Scan()
switch ch {
case OpOther:
r.Put(val)
case OpAdd:
a := r.Get()
b := r.Get()
r.Put(a + b)
case OpSub:
a := r.Get()
b := r.Get()
r.Put(b - a)
case OpMult:
a := r.Get()
b := r.Get()
r.Put(a * b)
case OpDiv:
a := r.Get()
b := r.Get()
r.Put(b / a)
case OpMod:
a := r.Get()
b := r.Get()
r.Put(b % a)
case OpNot:
if r.Get() == 0 {
r.Put(1)
} else {
r.Put(0)
}
case OpGreaterThan:
a := r.Get()
b := r.Get()
if b > a {
r.Put(1)
} else {
r.Put(0)
}
case OpIfHoriz:
if r.Get() == 0 {
r.SetRudder(OpModRight)
} else {
r.SetRudder(OpMovLeft)
}
case OpIfVert:
if r.Get() == 0 {
r.SetRudder(OpMovDown)
} else {
r.SetRudder(OpMovUp)
}
case OpDup:
r.Duplicate()
case OpSwap:
r.Swap()
case OpPop:
r.Get()
case OpPutCode:
y := r.Get()
x := r.Get()
v := r.Get()
r.PutCode(x, y, v)
case OpGetCode:
y := r.Get()
x := r.Get()
v := r.GetCode(x, y)
r.Put(v)
case OpOutInt:
r.Output(strconv.FormatInt(int64(r.Get()), 10))
case OpOutRune:
r.Output(string([]byte{byte(r.Get())}))
case OpInInt:
v := 0
info := fmt.Sprintf("\n(Enter a number '%s'): ", string([]byte{ch}))
for {
r.Output(info)
_, err := fmt.Fscanf(r.input, "%d\n", &v)
if err == nil {
break
}
r.errors = append(r.errors, err)
}
r.Put(v)
case OpInRune:
char := 0
info := fmt.Sprintf("\n(Enter a character '%s'): ", string([]byte{ch}))
for {
r.Output(info)
_, err := fmt.Fscanf(r.input, "%c\n", &char)
if err == nil {
break
}
r.errors = append(r.errors, err)
}
r.Put(char)
case OpModRight, OpMovLeft, OpMovUp, OpMovDown:
r.SetRudder(ch)
case OpBridge:
r.Next(1)
case OpMovRandom:
randSwitch := []byte{OpModRight, OpMovLeft, OpMovUp, OpMovDown}
ru := randSwitch[r.randFunc(len(randSwitch))]
r.SetRudder(ru)
case OpEnd:
return false, nil
case OpBlank, OpNone:
default:
x, y := r.Point()
err := fmt.Errorf("Error in %d, %d undefined: %s", x, y, CodeText(int(ch)))
r.errors = append(r.errors, err)
}
return true, nil
}
// Output print
func (r *Runner) Output(s string) {
if r.output != nil {
io.WriteString(r.output, s)
}
if r.step != nil {
r.step()
}
}
// Errors of the Runner
func (r *Runner) Errors() []error {
return r.errors
}
// Run befunge code.
func (r *Runner) Run() error {
for {
r.Next(1)
if r.step != nil {
r.step()
}
ok, err := r.runStep()
if err != nil {
return err
}
if !ok {
break
}
}
return nil
}