I’m making a Brainf**k interpreter in Golang.
But there is a strange bug in my program,
and I can’t find its cause.
func Execute(code string) {
program, err := Compile(code)
if program == nil {
panic(err)
}
for i := 0; i < len(program); i++ {
e := program[i]
switch e[0] {
case dec_ptr:
if ptr <= 0 {
ptr = mem_size - 1
break
}
ptr--
case inc_ptr:
if ptr >= mem_size-1 {
ptr = 0
break
}
ptr++
case decrease:
if memory[ptr] <= 0 {
memory[ptr] = 255
break
}
memory[ptr]--
case increase:
if memory[ptr] >= 255 {
memory[ptr] = 0
break
}
memory[ptr]++
case output:
fmt.Printf("%s", string(memory[ptr]))
case input:
case loop_start:
if memory[ptr] == 0 {
i = e[1] - 1
}
case loop_end:
if memory[ptr] != 0 {
i = e[1] - 1
}
}
// fmt.Printf("%d, %d, %d, %d, %d\n", memory[0], memory[1], memory[2], memory[3], memory[4])
}
}
Here is a main function in my program with the bug.
When I use go run .
or go build, ./<program-name>
to run my program, it doesn’t print result.
But when I use VS Code debug tool to run it, it prints result.
I can’t find the cause of that! Please help me!
Here is BF code that I used to test, the bug didn’t show another BF code.