Skip to content

Commit a5222c4

Browse files
committed
Add MemoryValues analysis, addressing #22
1 parent 689f374 commit a5222c4

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class Brainalyze implements BrainfuckListener {
2222
private final GroovyBFContext groovy
2323
@PackageScope final Map<Class<?>, Object> analysis = [:]
2424
private boolean memoryIndexBelowZero
25-
private int minValue
26-
private int maxValue
2725
private int maxMemory
2826

2927
public <T extends BrainfuckAnalyzer> T get(Class<T> clazz) {
@@ -60,10 +58,6 @@ class Brainalyze implements BrainfuckListener {
6058
brain.setListener(analyze)
6159
brain.run()
6260

63-
for (int i = 0; i < brain.memory.memorySize; i++) {
64-
analyze.cells[i].value = brain.memory.getMemory(i)
65-
}
66-
6761
int commandCount = brain.code.commandCount
6862
for (int i = 0; i < commandCount; i++) {
6963
BrainfuckCommand command = brain.code.getCommandAt(i)
@@ -203,12 +197,6 @@ class Brainalyze implements BrainfuckListener {
203197

204198
@Override
205199
void afterPerform(BrainfuckRunner runner, BrainfuckCommand command) {
206-
if (runner.memory.value > this.maxValue) {
207-
this.maxValue = runner.memory.value
208-
}
209-
if (runner.memory.value < this.minValue) {
210-
this.minValue = runner.memory.value
211-
}
212200
}
213201

214202
IndexCounters getWhileLoopCounts() {
@@ -251,12 +239,4 @@ class Brainalyze implements BrainfuckListener {
251239
this.memoryIndexBelowZero
252240
}
253241

254-
int getMinValue() {
255-
this.minValue
256-
}
257-
258-
int getMaxValue() {
259-
this.maxValue
260-
}
261-
262242
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class MemoryCell {
1515

1616
final int index
1717
boolean used
18-
int value
1918
IndexCounter prints = new IndexCounter()
2019
IndexCounter userInputs = new IndexCounter()
2120

@@ -44,18 +43,13 @@ class MemoryCell {
4443
}
4544

4645
String toString(GroovyBFContext groovy) {
47-
int value = this.value
4846
String hexAddress = String.format("%04X", index)
4947
String decAddress = String.format("%06d", index)
5048

51-
boolean specialChar = value >= 0 && value <= 13
52-
char chrValue = specialChar ? 32 : value;
53-
String decValue = String.format("%6d", value);
54-
5549
String analysis = analysis.values().stream().map({obj -> String.valueOf(obj)}).collect(Collectors.joining('\t'))
5650
Map<String, Integer> tagsCount = resolveTags(groovy)
5751
String tags = tagsCount.isEmpty() ? '' : tagsCount.toString()
58-
"Hex $hexAddress\tDec $decAddress\tValue $decValue '$chrValue' \t" +
52+
"Hex $hexAddress\tDec $decAddress\t" +
5953
analysis +
6054
"$tags".toString()
6155
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.analyze.MemoryCell
6+
import net.zomis.brainf.model.BrainfuckCommand
7+
import net.zomis.brainf.model.BrainfuckRunner
8+
9+
class MemoryValues implements BrainfuckAnalyzer {
10+
11+
public static class MemoryCellValue {
12+
int value
13+
14+
String toString() {
15+
int value = this.value
16+
boolean specialChar = value >= 0 && value <= 13
17+
char chrValue = specialChar ? 32 : value;
18+
String decValue = String.format("%6d", value);
19+
"Value $decValue '$chrValue'"
20+
}
21+
22+
}
23+
24+
private int minValue
25+
private int maxValue
26+
27+
@Override
28+
void afterPerform(MemoryCell cell, BrainfuckRunner runner, BrainfuckCommand command) {
29+
if (runner.memory.value > this.maxValue) {
30+
this.maxValue = runner.memory.value
31+
}
32+
if (runner.memory.value < this.minValue) {
33+
this.minValue = runner.memory.value
34+
}
35+
}
36+
37+
@Override
38+
Object createMemoryData() {
39+
return new MemoryCellValue()
40+
}
41+
42+
@Override
43+
void after(Brainalyze analyze, BrainfuckRunner runner) {
44+
for (int i = 0; i < runner.memory.memorySize; i++) {
45+
analyze.cell(i).data(this, MemoryCellValue).value = runner.memory.getMemory(i)
46+
}
47+
}
48+
49+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package net.zomis.brainf
22

33
import net.zomis.brainf.analyze.IndexCounters
44
import net.zomis.brainf.analyze.MemoryCell
5+
import net.zomis.brainf.analyze.analyzers.MemoryValues
56
import net.zomis.brainf.analyze.analyzers.ReadWriteAnalysis
67
import net.zomis.brainf.model.BrainF
78
import net.zomis.brainf.model.classic.BrainFCommand
@@ -211,7 +212,7 @@ public class BrainTest extends BrainfuckTest {
211212
public void readsAndWrites() {
212213
source.addCommands(">> +++++ [->[+>>]+[<<]>]")
213214
// distribute values from 5 downto 1 across the tape
214-
analyze(new ReadWriteAnalysis())
215+
analyze(new MemoryValues(), new ReadWriteAnalysis())
215216
analyze.print()
216217
ReadWriteAnalysis readWrite = analyze.get(ReadWriteAnalysis)
217218
assert readWrite.maxMemory == 0x0B

0 commit comments

Comments
 (0)