Skip to content

Commit

Permalink
Make StepOutStrategy great again
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Dec 6, 2016
1 parent d0568ae commit 40c4cc4
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions src/main/groovy/net/zomis/brainf/model/run/StepOutStrategy.groovy
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
package net.zomis.brainf.model.run

import net.zomis.brainf.model.classic.BrainFCommand
import net.zomis.brainf.model.BrainfuckCommand
import net.zomis.brainf.model.ast.tree.SyntaxTree
import net.zomis.brainf.model.BrainfuckRunner

class StepOutStrategy implements RunStrategy {

private int loops
private int enteredSyntaxes
private SyntaxTree activeTree

@Override
boolean start(BrainfuckRunner runner) {
loops = 0
if (runner.getCode().getNextCommand() == BrainFCommand.WHILE) {
enteredSyntaxes = runner.code.enteredTrees.size()
activeTree = runner.code.currentTree.tree
if (runner.code.currentSyntax instanceof SyntaxTree) {
return false
}
int matching = runner.getCode().findMatching(BrainFCommand.END_WHILE, BrainFCommand.WHILE, 1)
return matching != -1
return enteredSyntaxes > 1
}

@Override
boolean next(BrainfuckRunner runner) {
BrainfuckCommand comm = runner.getCode().getNextCommand()
int value = runner.memory.value
if (comm == BrainFCommand.WHILE) {
loops++
runner.runSyntax()
if (runner.code.currentSyntax == activeTree) {
return true
}
if (comm == BrainFCommand.END_WHILE && value == 0) {
loops--
}
if (comm == BrainFCommand.END_WHILE && loops < 0 && value == 0) {
runner.step()
return false
}
runner.step();
return true
return runner.code.enteredTrees.size() >= enteredSyntaxes
}

}

0 comments on commit 40c4cc4

Please sign in to comment.