Skip to content

Use constant in the parse tree for minus + graphviz export handles constants #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/antlr4/Cryptator.g4
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ expression returns [ICryptaNode node] //create recursively the tree of expressio
| '\'' number '\'' {$node=new CryptaConstant($number.text);}
| '(' expression ')' {$node=$expression.node;}
| 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
| sub expression {$node=new CryptaNode($sub.text, new CryptaLeaf(), $expression.node);}
| sub expression {$node=new CryptaNode($sub.text, new CryptaConstant("0"), $expression.node);}
| e1=expression divORmul e2=expression {$node=new CryptaNode($divORmul.text, $e1.node, $e2.node);}
| e1=expression addORsub e2=expression {$node=new CryptaNode($addORsub.text, $e1.node, $e2.node);};

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/cryptator/tree/CryptaFeatures.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ public CryptaFeatures() {

private void accept(char[] word) {
final int n = word.length;
if(n > 0) {
wordCount++;
charCount += n;
if(n < minWordLength) minWordLength = n;
else if(n > maxWordLength) maxWordLength = n;
for (char c : word) {
symbols.add(c);
wordCount++;
charCount += n;
if(n < minWordLength) minWordLength = n;
else if(n > maxWordLength) maxWordLength = n;
for (char c : word) {
symbols.add(c);
}
}
}

@Override
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/cryptator/tree/CryptaLeaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public class CryptaLeaf implements ICryptaNode {

private final char[] word;

public CryptaLeaf() {
this(new char[0]);
}

public CryptaLeaf(String word) {
this(word.toCharArray());
}
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/cryptator/tree/GraphvizExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import guru.nidi.graphviz.attribute.Label;
import guru.nidi.graphviz.attribute.Records;
import guru.nidi.graphviz.attribute.Shape;
import guru.nidi.graphviz.attribute.Style;
import guru.nidi.graphviz.model.Graph;
import guru.nidi.graphviz.model.Node;

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

protected Node withZeroLabel(Node n) {
return n.with(Label.of("0"));
}

protected Node withWordLabel(Node n, ICryptaNode node) {
protected final Node withPlainWord(Node n, ICryptaNode node) {
return n.with(Label.of(new String(node.getWord())));
}

protected final Node withBoxedWord(Node n, ICryptaNode node) {
return withPlainWord(n, node).with(Shape.BOX, Style.DASHED);
}

protected Node makeWordNode(ICryptaNode node, int numNode) {
final Node n = makeNode(numNode);
if(node.getWord().length == 0) {
return withZeroLabel(n);
} else {
return withWordLabel(n, node);
if(node.isInternalNode()) return withPlainWord(n, node);
else {
if(node.isConstant()) return withBoxedWord(n, node);
else return withPlainWord(n, node);
}
}

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

protected final Node withRecord(Node n, ICryptaNode node) {
return n.with(makeRecords(node.getWord()));
}
@Override
protected Node makeWordNode(ICryptaNode node, int numNode) {
final Node n = makeNode(numNode);
if(node.getWord().length == 0) return withZeroLabel(n);
else if (node.isInternalNode()) return withWordLabel(n, node);
else return n.with(makeRecords(node.getWord()));
if(node.isInternalNode()) return withPlainWord(n, node);
else {
if(node.isConstant()) return withBoxedWord(n, node);
else return withRecord(n, node);
}
}

}
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/cryptator/tree/TreeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@

public final class TreeUtils {

public static final String ZERO = "_0";

private TreeUtils() {
}

private static void writeWord(ICryptaNode node, PrintWriter out) {
char[] w = node.getWord();
if (w.length > 0) out.write(node.toGrammarString());
else out.write(ZERO);
out.write(node.toGrammarString());
out.write(" ");
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/cryptator/ExportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ public void testExport3() throws CryptaParserException, CryptaSolutionException,
assertNotNull(GraphvizExport.exportToGraphviz(cryptarithm, solution));
}

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


}
26 changes: 13 additions & 13 deletions src/test/java/cryptator/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import org.junit.Test;

import static cryptator.TreeTest.*;
import static cryptator.tree.TreeUtils.ZERO;

public class ParserTest {

public final CryptaParserWrapper parser = new CryptaParserWrapper();
public static final String ZERO = "'0'";

public ParserTest() {
}
Expand All @@ -42,17 +42,17 @@ public void testParser2() throws CryptaParserException {
@Test
public void testParser3() throws CryptaParserException {
final ICryptaNode node = parser.parse("-send/more=money");
testPreorder("= / - " + ZERO + " send more money ", node);
testPostorder("" + ZERO + " send - more / money = ", node);
testInorder("" + ZERO + " - send / more = money ", node);
testPreorder("= / - " + ParserTest.ZERO + " send more money ", node);
testPostorder("" + ParserTest.ZERO + " send - more / money = ", node);
testInorder("" + ParserTest.ZERO + " - send / more = money ", node);
}

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

@Test
Expand Down Expand Up @@ -82,9 +82,9 @@ public void testParser7() throws CryptaParserException {
@Test
public void testParser8() throws CryptaParserException {
final ICryptaNode node = parser.parse("-send -more = -money");
testPreorder("= - - " + ZERO + " send more - " + ZERO + " money ", node);
testPostorder(ZERO + " send - more - " + ZERO + " money - = ", node);
testInorder(ZERO + " - send - more = " + ZERO + " - money ", node);
testPreorder("= - - " + ParserTest.ZERO + " send more - " + ParserTest.ZERO + " money ", node);
testPostorder(ParserTest.ZERO + " send - more - " + ParserTest.ZERO + " money - = ", node);
testInorder(ParserTest.ZERO + " - send - more = " + ParserTest.ZERO + " - money ", node);
}

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

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

}

Expand Down