Nicened the loop code so it isn't quite as nested

This commit is contained in:
Chris Sexton 2012-11-13 22:27:18 -05:00
parent 763b188a6a
commit 3f9743f3a9
1 changed files with 15 additions and 19 deletions

View File

@ -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 == '>' {