Skip to content

Commit 034519a

Browse files
committed
Extract GroovyCommandAnalysis for #22
1 parent 6b04088 commit 034519a

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

src/main/groovy/net/zomis/brainf/analyze/Brainalyze.groovy

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Brainalyze implements BrainfuckListener {
1717
private final int[] actionsPerCommand
1818
private final int[] codeCommands
1919
private final MemoryCell[] cells
20-
private final List<Map> problematicCommands = []
2120
private final GroovyBFContext groovy
2221
@PackageScope final Map<Class<?>, Object> analysis = [:]
2322
private boolean memoryIndexBelowZero
@@ -64,18 +63,6 @@ class Brainalyze implements BrainfuckListener {
6463
BrainFCommand cmd = command as BrainFCommand
6564
analyze.codeCommands[cmd.ordinal()]++
6665
}
67-
if (command instanceof GroovyBFContext.SpecialCommand) {
68-
GroovyBFContext.SpecialCommand cmd = command as GroovyBFContext.SpecialCommand
69-
for (int codeIndex = 0; codeIndex < cmd.code.length(); codeIndex++) {
70-
char ch = cmd.code.charAt(codeIndex)
71-
BrainFCommand bfCommand = BrainFCommand.getCommand(ch)
72-
if (bfCommand != BrainFCommand.NONE) {
73-
Map<String, Object> map = [index: i, codeIndex: codeIndex, command: bfCommand]
74-
analyze.problematicCommands << map
75-
break;
76-
}
77-
}
78-
}
7966
}
8067

8168
for (int i = analyze.cells.length - 1; i >= 0; i--) {
@@ -112,9 +99,6 @@ class Brainalyze implements BrainfuckListener {
11299
println()
113100
this.analysis.values().forEach({it.print()})
114101
println()
115-
for (Map problem : problematicCommands) {
116-
println "Problematic command: $problem"
117-
}
118102
}
119103

120104
static void printCommands(int[] ints) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.zomis.brainf.analyze.analyzers
2+
3+
import net.zomis.brainf.analyze.Brainalyze
4+
import net.zomis.brainf.analyze.BrainfuckAnalyzer
5+
import net.zomis.brainf.model.BrainfuckRunner
6+
import net.zomis.brainf.model.classic.BrainFCommand
7+
import net.zomis.brainf.model.groovy.GroovyBFContext
8+
9+
class GroovyCommandAnalysis implements BrainfuckAnalyzer {
10+
11+
private final List<Map> problematicCommands = []
12+
13+
@Override
14+
void after(Brainalyze analyze, BrainfuckRunner runner) {
15+
for (int i = 0; i < runner.code.commandCount; i++) {
16+
def command = runner.code.getCommandAt(i)
17+
if (command instanceof GroovyBFContext.SpecialCommand) {
18+
GroovyBFContext.SpecialCommand cmd = command as GroovyBFContext.SpecialCommand
19+
for (int codeIndex = 0; codeIndex < cmd.code.length(); codeIndex++) {
20+
char ch = cmd.code.charAt(codeIndex)
21+
BrainFCommand bfCommand = BrainFCommand.getCommand(ch)
22+
if (bfCommand != BrainFCommand.NONE) {
23+
Map<String, Object> map = [index: i, codeIndex: codeIndex, command: bfCommand]
24+
problematicCommands << map
25+
break;
26+
}
27+
}
28+
}
29+
}
30+
}
31+
32+
@Override
33+
void print() {
34+
for (Map problem : problematicCommands) {
35+
println "Problematic command: $problem"
36+
}
37+
}
38+
39+
}

0 commit comments

Comments
 (0)