Skip to content

Commit c7d62a7

Browse files
authored
Merge pull request #55 from arnaud-m/constMinus
Use constant in the parse tree for minus + graphviz export handles constants
2 parents e4bcd17 + 3dce2d3 commit c7d62a7

File tree

7 files changed

+46
-42
lines changed

7 files changed

+46
-42
lines changed

src/main/antlr4/Cryptator.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ expression returns [ICryptaNode node] //create recursively the tree of expressio
3838
| '\'' number '\'' {$node=new CryptaConstant($number.text);}
3939
| '(' expression ')' {$node=$expression.node;}
4040
| e1=expression modORpow e2=expression {$node=new CryptaNode($modORpow.text, $e1.node, $e2.node);} //create a node of the tree corresponding to an operation and return this node
41-
| sub expression {$node=new CryptaNode($sub.text, new CryptaLeaf(), $expression.node);}
41+
| sub expression {$node=new CryptaNode($sub.text, new CryptaConstant("0"), $expression.node);}
4242
| e1=expression divORmul e2=expression {$node=new CryptaNode($divORmul.text, $e1.node, $e2.node);}
4343
| e1=expression addORsub e2=expression {$node=new CryptaNode($addORsub.text, $e1.node, $e2.node);};
4444

src/main/java/cryptator/tree/CryptaFeatures.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,13 @@ public CryptaFeatures() {
3333

3434
private void accept(char[] word) {
3535
final int n = word.length;
36-
if(n > 0) {
37-
wordCount++;
38-
charCount += n;
39-
if(n < minWordLength) minWordLength = n;
40-
else if(n > maxWordLength) maxWordLength = n;
41-
for (char c : word) {
42-
symbols.add(c);
36+
wordCount++;
37+
charCount += n;
38+
if(n < minWordLength) minWordLength = n;
39+
else if(n > maxWordLength) maxWordLength = n;
40+
for (char c : word) {
41+
symbols.add(c);
4342
}
44-
}
4543
}
4644

4745
@Override

src/main/java/cryptator/tree/CryptaLeaf.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ public class CryptaLeaf implements ICryptaNode {
1515

1616
private final char[] word;
1717

18-
public CryptaLeaf() {
19-
this(new char[0]);
20-
}
21-
2218
public CryptaLeaf(String word) {
2319
this(word.toCharArray());
2420
}

src/main/java/cryptator/tree/GraphvizExport.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import guru.nidi.graphviz.attribute.Label;
2727
import guru.nidi.graphviz.attribute.Records;
2828
import guru.nidi.graphviz.attribute.Shape;
29+
import guru.nidi.graphviz.attribute.Style;
2930
import guru.nidi.graphviz.model.Graph;
3031
import guru.nidi.graphviz.model.Node;
3132

@@ -62,20 +63,21 @@ protected final Node makeNode(int numNode) {
6263
return node(String.valueOf(numNode));
6364
}
6465

65-
protected Node withZeroLabel(Node n) {
66-
return n.with(Label.of("0"));
67-
}
6866

69-
protected Node withWordLabel(Node n, ICryptaNode node) {
67+
protected final Node withPlainWord(Node n, ICryptaNode node) {
7068
return n.with(Label.of(new String(node.getWord())));
7169
}
70+
71+
protected final Node withBoxedWord(Node n, ICryptaNode node) {
72+
return withPlainWord(n, node).with(Shape.BOX, Style.DASHED);
73+
}
7274

7375
protected Node makeWordNode(ICryptaNode node, int numNode) {
7476
final Node n = makeNode(numNode);
75-
if(node.getWord().length == 0) {
76-
return withZeroLabel(n);
77-
} else {
78-
return withWordLabel(n, node);
77+
if(node.isInternalNode()) return withPlainWord(n, node);
78+
else {
79+
if(node.isConstant()) return withBoxedWord(n, node);
80+
else return withPlainWord(n, node);
7981
}
8082
}
8183

@@ -116,12 +118,17 @@ private final Attributes<ForNode> makeRecords(char[] word) {
116118
return Records.of(records);
117119
}
118120

121+
protected final Node withRecord(Node n, ICryptaNode node) {
122+
return n.with(makeRecords(node.getWord()));
123+
}
119124
@Override
120125
protected Node makeWordNode(ICryptaNode node, int numNode) {
121126
final Node n = makeNode(numNode);
122-
if(node.getWord().length == 0) return withZeroLabel(n);
123-
else if (node.isInternalNode()) return withWordLabel(n, node);
124-
else return n.with(makeRecords(node.getWord()));
127+
if(node.isInternalNode()) return withPlainWord(n, node);
128+
else {
129+
if(node.isConstant()) return withBoxedWord(n, node);
130+
else return withRecord(n, node);
131+
}
125132
}
126133

127134
}

src/main/java/cryptator/tree/TreeUtils.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818

1919
public final class TreeUtils {
2020

21-
public static final String ZERO = "_0";
22-
2321
private TreeUtils() {
2422
}
2523

2624
private static void writeWord(ICryptaNode node, PrintWriter out) {
27-
char[] w = node.getWord();
28-
if (w.length > 0) out.write(node.toGrammarString());
29-
else out.write(ZERO);
25+
out.write(node.toGrammarString());
3026
out.write(" ");
3127
}
3228

src/test/java/cryptator/ExportTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ public void testExport3() throws CryptaParserException, CryptaSolutionException,
4848
assertNotNull(GraphvizExport.exportToGraphviz(cryptarithm, solution));
4949
}
5050

51+
@Test
52+
public void testExport4() throws CryptaParserException, CryptaSolutionException, CryptaEvaluationException {
53+
final ICryptaNode cryptarithm = parser.parse("SEND+MORE=MONEY ; M='1'");
54+
assertNotNull(GraphvizExport.exportToGraphviz(cryptarithm));
55+
}
56+
57+
5158
}

src/test/java/cryptator/ParserTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
import org.junit.Test;
1515

1616
import static cryptator.TreeTest.*;
17-
import static cryptator.tree.TreeUtils.ZERO;
1817

1918
public class ParserTest {
2019

2120
public final CryptaParserWrapper parser = new CryptaParserWrapper();
21+
public static final String ZERO = "'0'";
2222

2323
public ParserTest() {
2424
}
@@ -42,17 +42,17 @@ public void testParser2() throws CryptaParserException {
4242
@Test
4343
public void testParser3() throws CryptaParserException {
4444
final ICryptaNode node = parser.parse("-send/more=money");
45-
testPreorder("= / - " + ZERO + " send more money ", node);
46-
testPostorder("" + ZERO + " send - more / money = ", node);
47-
testInorder("" + ZERO + " - send / more = money ", node);
45+
testPreorder("= / - " + ParserTest.ZERO + " send more money ", node);
46+
testPostorder("" + ParserTest.ZERO + " send - more / money = ", node);
47+
testInorder("" + ParserTest.ZERO + " - send / more = money ", node);
4848
}
4949

5050
@Test
5151
public void testParser4() throws CryptaParserException {
5252
final ICryptaNode node = parser.parse("send/-more<money");
53-
testPreorder("< / send - " + ZERO + " more money ", node);
54-
testPostorder("send " + ZERO + " more - / money < ", node);
55-
testInorder("send / " + ZERO + " - more < money ", node);
53+
testPreorder("< / send - " + ParserTest.ZERO + " more money ", node);
54+
testPostorder("send " + ParserTest.ZERO + " more - / money < ", node);
55+
testInorder("send / " + ParserTest.ZERO + " - more < money ", node);
5656
}
5757

5858
@Test
@@ -82,9 +82,9 @@ public void testParser7() throws CryptaParserException {
8282
@Test
8383
public void testParser8() throws CryptaParserException {
8484
final ICryptaNode node = parser.parse("-send -more = -money");
85-
testPreorder("= - - " + ZERO + " send more - " + ZERO + " money ", node);
86-
testPostorder(ZERO + " send - more - " + ZERO + " money - = ", node);
87-
testInorder(ZERO + " - send - more = " + ZERO + " - money ", node);
85+
testPreorder("= - - " + ParserTest.ZERO + " send more - " + ParserTest.ZERO + " money ", node);
86+
testPostorder(ParserTest.ZERO + " send - more - " + ParserTest.ZERO + " money - = ", node);
87+
testInorder(ParserTest.ZERO + " - send - more = " + ParserTest.ZERO + " - money ", node);
8888
}
8989

9090
@Test(expected = CryptaParserException.class)
@@ -135,9 +135,9 @@ public void testParserAND() throws CryptaParserException {
135135
public void testParserAND2() throws CryptaParserException {
136136
final ICryptaNode node = parser.parse("send+more= money; -send -more= \n -money");
137137

138-
testPreorder("; = + send more money = - - " + ZERO + " send more - " + ZERO + " money ", node);
139-
testPostorder("send more + money = " + ZERO + " send - more - " + ZERO + " money - = ; ", node);
140-
testInorder("send + more = money ; " + ZERO + " - send - more = " + ZERO + " - money ", node);
138+
testPreorder("; = + send more money = - - " + ParserTest.ZERO + " send more - " + ParserTest.ZERO + " money ", node);
139+
testPostorder("send more + money = " + ParserTest.ZERO + " send - more - " + ParserTest.ZERO + " money - = ; ", node);
140+
testInorder("send + more = money ; " + ParserTest.ZERO + " - send - more = " + ParserTest.ZERO + " - money ", node);
141141

142142
}
143143

0 commit comments

Comments
 (0)