Nicened the loop code so it isn't quite as nested
This commit is contained in:
parent
763b188a6a
commit
3f9743f3a9
34
brainfuck.go
34
brainfuck.go
|
@ -83,28 +83,24 @@ func (m *Machine) Run(input []byte) {
|
||||||
// execute the program
|
// execute the program
|
||||||
for ip := 0; ip < len(input); ip++ {
|
for ip := 0; ip < len(input); ip++ {
|
||||||
instr := input[ip]
|
instr := input[ip]
|
||||||
if instr == '[' {
|
// if *ptr == 0, jump to ]
|
||||||
// if *ptr == 0, jump to ]
|
if instr == '[' && m.value() == 0 {
|
||||||
if m.value() == 0 {
|
for lc := 1; lc > 0; {
|
||||||
for lc := 1; lc > 0; {
|
ip += 1
|
||||||
ip += 1
|
if input[ip] == ']' {
|
||||||
if input[ip] == ']' {
|
lc -= 1
|
||||||
lc -= 1
|
} else if input[ip] == '[' {
|
||||||
} else if input[ip] == '[' {
|
lc += 1
|
||||||
lc += 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if instr == ']' {
|
} else if instr == ']' && m.value() != 0 {
|
||||||
// if *ptr != 0, go back to [
|
// if *ptr != 0, go back to [
|
||||||
if m.value() != 0 {
|
for lc := 1; lc > 0; {
|
||||||
for lc := 1; lc > 0; {
|
ip -= 1
|
||||||
ip -= 1
|
if input[ip] == ']' {
|
||||||
if input[ip] == ']' {
|
lc += 1
|
||||||
lc += 1
|
} else if input[ip] == '[' {
|
||||||
} else if input[ip] == '[' {
|
lc -= 1
|
||||||
lc -= 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if instr == '>' {
|
} else if instr == '>' {
|
||||||
|
|
Loading…
Reference in New Issue