-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
11 additions
and
20 deletions.
There are no files selected for viewing
31 changes: 11 additions & 20 deletions
31
src/main/groovy/net/zomis/brainf/model/run/StepOutStrategy.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |