Skip to content

Commit 0d09c51

Browse files
committed
Add MemoryIndexAnalysis, for #22
1 parent df91799 commit 0d09c51

File tree

4 files changed

+68
-17
lines changed

4 files changed

+68
-17
lines changed

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Brainalyze implements BrainfuckListener {
1616
private final MemoryCell[] cells
1717
private final GroovyBFContext groovy
1818
@PackageScope final Map<Class<?>, Object> analysis = [:]
19-
private boolean memoryIndexBelowZero
2019
private int maxMemory
2120

2221
public <T extends BrainfuckAnalyzer> T get(Class<T> clazz) {
@@ -58,10 +57,6 @@ class Brainalyze implements BrainfuckListener {
5857

5958
println 'Brainfuck Analyze'
6059
println '-----------------'
61-
if (this.memoryIndexBelowZero) {
62-
println 'WARNING: Memory index goes below zero'
63-
}
64-
println()
6560
println 'Tape summary'
6661
for (int i = 0; i <= maxMemory; i++) {
6762
MemoryCell cell = cells[i]
@@ -80,10 +75,6 @@ class Brainalyze implements BrainfuckListener {
8075
BrainFCommand command = (BrainFCommand) cmd
8176
MemoryCell cell = cells[runner.memory.memoryIndex]
8277

83-
if (command == BrainFCommand.PREVIOUS && runner.memory.memoryIndex == 0) {
84-
this.memoryIndexBelowZero = true
85-
}
86-
8778
switch (command) {
8879
case BrainFCommand.ADD:
8980
case BrainFCommand.SUBTRACT:
@@ -129,8 +120,4 @@ class Brainalyze implements BrainfuckListener {
129120
result
130121
}
131122

132-
boolean isMemoryIndexBelowZero() {
133-
this.memoryIndexBelowZero
134-
}
135-
136123
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package net.zomis.brainf.analyze.analyzers
2+
3+
import net.zomis.brainf.analyze.BrainfuckAnalyzer
4+
import net.zomis.brainf.analyze.MemoryCell
5+
import net.zomis.brainf.model.BrainfuckCommand
6+
import net.zomis.brainf.model.BrainfuckRunner
7+
import net.zomis.brainf.model.classic.BrainFCommand
8+
9+
class MemoryIndexAnalysis implements BrainfuckAnalyzer {
10+
11+
private boolean memoryIndexBelowZero
12+
private boolean memoryIndexAboveMax
13+
private int rightmostMemory
14+
15+
@Override
16+
void print() {
17+
if (this.memoryIndexBelowZero) {
18+
println 'WARNING: Memory index goes below zero'
19+
}
20+
if (this.memoryIndexAboveMax) {
21+
println 'WARNING: Memory index goes above maximum'
22+
}
23+
if (this.rightmostMemory > 30_000) {
24+
println 'WARNING: Memory index goes above 30 000'
25+
} else if (this.rightmostMemory > 9_999) {
26+
println 'WARNING: Memory index goes above 9 999'
27+
}
28+
println "Rightmost memory accessed is $rightmostMemory"
29+
println()
30+
}
31+
32+
@Override
33+
void beforePerform(MemoryCell cell, BrainfuckRunner runner, BrainfuckCommand command) {
34+
if (command == BrainFCommand.PREVIOUS && runner.memory.memoryIndex == 0) {
35+
this.memoryIndexBelowZero = true
36+
}
37+
if (command == BrainFCommand.NEXT && runner.memory.memoryIndex == runner.memory.size - 1) {
38+
this.memoryIndexAboveMax = true
39+
}
40+
}
41+
42+
@Override
43+
void afterPerform(MemoryCell cell, BrainfuckRunner runner, BrainfuckCommand command) {
44+
this.@rightmostMemory = Math.max(this.@rightmostMemory, runner.memory.memoryIndex)
45+
}
46+
47+
boolean isMemoryIndexBelowZero() {
48+
this.@memoryIndexBelowZero
49+
}
50+
51+
boolean isMemoryIndexAboveMax() {
52+
this.@memoryIndexAboveMax
53+
}
54+
55+
int getRightmostMemory() {
56+
this.@rightmostMemory
57+
}
58+
59+
}

src/test/groovy/net/zomis/brainf/BrainfuckTest.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import net.zomis.brainf.analyze.BrainfuckAnalyzer
66
import net.zomis.brainf.analyze.analyzers.CommandCountAnalysis
77
import net.zomis.brainf.analyze.analyzers.GroovyCommandAnalysis
88
import net.zomis.brainf.analyze.analyzers.IOAnalysis
9+
import net.zomis.brainf.analyze.analyzers.MemoryIndexAnalysis
910
import net.zomis.brainf.analyze.analyzers.MemoryValues
1011
import net.zomis.brainf.analyze.analyzers.ReadWriteAnalysis
1112
import net.zomis.brainf.analyze.analyzers.WhileLoopAnalysis
@@ -50,6 +51,7 @@ class BrainfuckTest {
5051
new ReadWriteAnalysis(),
5152
new WhileLoopAnalysis(),
5253
new CommandCountAnalysis(),
54+
new MemoryIndexAnalysis(),
5355
]
5456
analyze(analyzers)
5557
}

src/test/groovy/net/zomis/brainf/ValidateTest.groovy

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.zomis.brainf
22

3+
import net.zomis.brainf.analyze.analyzers.MemoryIndexAnalysis
34
import net.zomis.brainf.analyze.analyzers.MemoryValues
45
import org.junit.Test
56

@@ -8,15 +9,17 @@ class ValidateTest extends BrainfuckTest {
89
@Test
910
void moveNegativeIndex() {
1011
source.addCommands('+<<++')
11-
analyze()
12-
assert analyze.isMemoryIndexBelowZero()
12+
analyze(new MemoryIndexAnalysis())
13+
assert analyze.get(MemoryIndexAnalysis).isMemoryIndexBelowZero()
14+
assert !analyze.get(MemoryIndexAnalysis).isMemoryIndexAboveMax()
1315
}
1416

1517
@Test
1618
void stayPositiveIndex() {
1719
source.addCommands('+>+<++')
18-
analyze()
19-
assert !analyze.isMemoryIndexBelowZero()
20+
analyze(new MemoryIndexAnalysis())
21+
assert !analyze.get(MemoryIndexAnalysis).isMemoryIndexBelowZero()
22+
assert !analyze.get(MemoryIndexAnalysis).isMemoryIndexAboveMax()
2023
}
2124

2225
@Test

0 commit comments

Comments
 (0)