-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfck.go
168 lines (155 loc) · 3.39 KB
/
brainfck.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
package main
import (
"fmt"
"io"
"os"
)
const memSize int = 30000
type bfOperation func()
type Brainfckr struct {
reader io.Reader
input io.Reader
writer io.Writer
stack Stack
code []byte
codePtr int
mem []byte
memPtr int
executed []byte
operations map[byte]bfOperation
}
func NewBrainfckr(reader io.Reader, input io.Reader, writer io.Writer) *Brainfckr {
bf := new(Brainfckr)
bf.opsMapSetup()
bf.mem = make([]byte, memSize)
bf.reader = reader
bf.input = input
bf.writer = writer
bf.memPtr = memSize / 2
return bf
}
func (bf *Brainfckr) opsMapSetup() {
bf.operations = make(map[byte]bfOperation)
bf.operations['+'] = func() {
bf.mem[bf.memPtr]++
bf.executed = append(bf.executed, '+')
}
bf.operations['-'] = func() {
bf.mem[bf.memPtr]--
bf.executed = append(bf.executed, '-')
}
bf.operations['>'] = func() {
if bf.memPtr == memSize-1 {
bf.memPtr = 0
} else {
bf.memPtr++
}
bf.executed = append(bf.executed, '<')
}
bf.operations['<'] = func() {
if bf.memPtr == 0 {
bf.memPtr = memSize - 1
} else {
bf.memPtr--
}
bf.executed = append(bf.executed, '<')
}
bf.operations[','] = func() {
b := []byte{0}
bf.input.Read(b)
if bf.input == os.Stdin {
bf.mem[bf.memPtr] = b[0] - 48 // account for ascii offset
bf.input.Read(b) // skip CR
}
bf.mem[bf.memPtr] = b[0]
bf.executed = append(bf.executed, ',')
}
bf.operations['.'] = func() {
bf.executed = append(bf.executed, '.')
_, _ = bf.writer.Write(bf.mem[bf.memPtr : bf.memPtr+1])
}
bf.operations['['] = func() {
bf.loop()
}
bf.operations[']'] = func() { // does nothing, just there to be a validOp
bf.executed = append(bf.executed, ']')
}
}
func (bf *Brainfckr) Interpret() error {
for op, err := bf.nextOp(); err != io.EOF; op, err = bf.nextOp() {
bf.operations[op]()
}
return io.EOF
}
func (bf *Brainfckr) loop() {
var op byte = '['
bf.code = append(bf.code, op)
for {
if op == '[' {
bf.stack = bf.stack.Push(bf.codePtr)
if bf.mem[bf.memPtr] == 0 { // skip the loop
imbalanceCount := 1
for imbalanceCount > 0 {
op = bf.nextLoopOp()
if bf.code[bf.codePtr] == ']' {
imbalanceCount--
} else if bf.code[bf.codePtr] == '[' {
imbalanceCount++
}
}
continue
}
} else if op == ']' {
var tmpCodePtr int
bf.stack, tmpCodePtr = bf.stack.Pop()
if bf.mem[bf.memPtr] > 0 {
bf.codePtr = tmpCodePtr - 1
} else if bf.stack.IsEmpty() {
break
}
} else {
bf.operations[op]()
}
op = bf.nextLoopOp()
}
bf.code = nil
bf.codePtr = 0
}
func (bf *Brainfckr) nextLoopOp() byte {
var op byte
bf.codePtr++
if bf.codePtr == len(bf.code) {
op, _ = bf.nextOp()
bf.code = append(bf.code, op)
} else {
op = bf.code[bf.codePtr]
}
return op
}
func (bf *Brainfckr) nextOp() (byte, error) {
b := make([]byte, 1)
var err error
for {
_, err = bf.reader.Read(b)
if err != nil {
if err == io.EOF {
break
}
}
if _, validOp := bf.operations[b[0]]; validOp {
break
} else if b[0] == '$' {
bf.debugOutput("\ndebug;\n")
}
}
return b[0], err
}
func (bf *Brainfckr) debugOutput(msg string) {
fmt.Printf(msg)
fmt.Printf("Stack Size %d\n", len(bf.stack))
fmt.Printf("CodePtr %d\n", bf.codePtr)
fmt.Printf("Code %s\n", bf.code)
fmt.Printf("Executed %s\n", bf.executed)
fmt.Printf("MemPtr %d\n", bf.memPtr)
fmt.Printf("Memory Content % d\n", bf.mem[:200])
}