From 3f9743f3a99f93a25d9b1aa47ac58536ec784bfe Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 13 Nov 2012 22:27:18 -0500 Subject: [PATCH] Nicened the loop code so it isn't quite as nested --- brainfuck.go | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/brainfuck.go b/brainfuck.go index e96c046..e55f114 100644 --- a/brainfuck.go +++ b/brainfuck.go @@ -83,28 +83,24 @@ func (m *Machine) Run(input []byte) { // execute the program for ip := 0; ip < len(input); ip++ { instr := input[ip] - if instr == '[' { - // if *ptr == 0, jump to ] - if m.value() == 0 { - for lc := 1; lc > 0; { - ip += 1 - if input[ip] == ']' { - lc -= 1 - } else if input[ip] == '[' { - lc += 1 - } + // if *ptr == 0, jump to ] + if instr == '[' && m.value() == 0 { + for lc := 1; lc > 0; { + ip += 1 + if input[ip] == ']' { + lc -= 1 + } else if input[ip] == '[' { + lc += 1 } } - } else if instr == ']' { + } else if instr == ']' && m.value() != 0 { // if *ptr != 0, go back to [ - if m.value() != 0 { - for lc := 1; lc > 0; { - ip -= 1 - if input[ip] == ']' { - lc += 1 - } else if input[ip] == '[' { - lc -= 1 - } + for lc := 1; lc > 0; { + ip -= 1 + if input[ip] == ']' { + lc += 1 + } else if input[ip] == '[' { + lc -= 1 } } } else if instr == '>' {