Fixed looping to support nested loops (oops)

The only test I have that doesn't pass is now the bubble sort test.
This commit is contained in:
Chris Sexton 2012-11-13 22:23:58 -05:00
parent be6410dbff
commit 5d7e23c9fc
1 changed files with 33 additions and 14 deletions

View File

@ -74,25 +74,44 @@ func (m *Machine) Input() {
} }
} }
func (m *Machine) value() byte {
return m.array[m.ptr]
}
// Run the whole program specified by input // Run the whole program specified by input
func (m *Machine) Run(input []byte) { func (m *Machine) Run(input []byte) {
// execute the program // execute the program
loop := -1 for ip := 0; ip < len(input); ip++ {
discard := false instr := input[ip]
for i := 0; i < len(input); i++ { if instr == '[' {
instr := input[i] // if *ptr == 0, jump to ]
if discard && instr != ']' { if m.value() == 0 {
continue // fmt.Printf("Encountered a [ with *ptr = 0, loopCount=%d\n", loopCount)
} else if instr == '[' { for lc := 1; lc > 0; {
loop = i ip += 1
if m.array[m.ptr] == 0 { if input[ip] == ']' {
discard = true // fmt.Println("]: Decrementing lc", lc)
lc -= 1
} else if input[ip] == '[' {
// fmt.Println("[: Incrementing lc=", lc)
lc += 1
}
}
} }
} else if instr == ']' { } else if instr == ']' {
if m.array[m.ptr] != 0 { // if *ptr != 0, go back to [
i = loop if m.value() != 0 {
} else { // fmt.Printf("Found a ] with *ptr != 0, loopCount=%d\n", loopCount)
discard = false for lc := 1; lc > 0; {
ip -= 1
if input[ip] == ']' {
// fmt.Println("]: decrementing lc=", lc)
lc += 1
} else if input[ip] == '[' {
// fmt.Println("[: incrementing lc=", lc)
lc -= 1
}
}
} }
} else if instr == '>' { } else if instr == '>' {
m.PtrIncr() m.PtrIncr()