Added support for any continue statements, fixed empty for loops (set condition to true if empty)

This commit is contained in:
Carlos Galindo 2019-04-04 18:00:16 +02:00
parent 96847b7a8a
commit daa1c2d90a
Signed by untrusted user who does not match committer: kauron
GPG key ID: 83E68706DEE119A3
3 changed files with 82 additions and 20 deletions

View file

@ -17,6 +17,10 @@ public class CFG {
private final Map<Node, String> labelMap = new HashMap<>(); private final Map<Node, String> labelMap = new HashMap<>();
private final Map<SimpleName, List<BreakStmt>> breakMap = new HashMap<>(); private final Map<SimpleName, List<BreakStmt>> breakMap = new HashMap<>();
private final Stack<List<BreakStmt>> breakStack = new Stack<>(); private final Stack<List<BreakStmt>> breakStack = new Stack<>();
private final Map<SimpleName, Node> continueMap = new HashMap<>();
private final Stack<Node> continueStack = new Stack<>();
private SimpleName lastLabel;
private void registerNode(Node stmt) { private void registerNode(Node stmt) {
nodes.add(stmt); nodes.add(stmt);
@ -122,14 +126,14 @@ public class CFG {
breakStack.push(new LinkedList<>()); breakStack.push(new LinkedList<>());
} }
public void registerBreak(BreakStmt stmt) { public void connectBreak(BreakStmt stmt) {
List<BreakStmt> list; connectTo(stmt);
if (stmt.getLabel().isPresent()) { if (stmt.getLabel().isPresent()) {
list = breakMap.get(stmt.getLabel().get()); breakMap.get(stmt.getLabel().get()).add(stmt);
} else { } else {
list = breakStack.peek(); breakStack.peek().add(stmt);
} }
list.add(stmt); clearNode();
} }
public void endBreakSection(SimpleName name) { public void endBreakSection(SimpleName name) {
@ -140,6 +144,38 @@ public class CFG {
nodeList.addAll(breakStack.pop()); nodeList.addAll(breakStack.pop());
} }
public void beginContinueSection(SimpleName name) {
assert lastLabel == null;
lastLabel = name;
}
public void beginContinueSection(Node node) {
continueStack.push(node);
if (lastLabel != null) {
assert !continueMap.containsKey(lastLabel);
continueMap.put(lastLabel, node);
lastLabel = null;
}
}
public void connectContinue(ContinueStmt stmt) {
connectTo(stmt);
if (stmt.getLabel().isPresent()) {
connectTo(continueMap.get(stmt.getLabel().get()));
} else {
connectTo(continueStack.peek());
}
clearNode();
}
public void endContinueSection(SimpleName name) {
continueMap.remove(name);
}
public void endContinueSection() {
continueStack.pop();
}
public List<String> toStringList(GraphViz gv) { public List<String> toStringList(GraphViz gv) {
List<String> res = new LinkedList<>(); List<String> res = new LinkedList<>();
res.add(gv.start_graph()); res.add(gv.start_graph());

View file

@ -3,6 +3,7 @@ package grafos;
import com.github.javaparser.ast.Node; import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.Expression; import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.stmt.*; import com.github.javaparser.ast.stmt.*;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
@ -53,7 +54,9 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
@Override @Override
public void visit(LabeledStmt n, CFG graph) { public void visit(LabeledStmt n, CFG graph) {
graph.beginBreakSection(n.getLabel()); graph.beginBreakSection(n.getLabel());
graph.beginContinueSection(n.getLabel());
super.visit(n, graph); super.visit(n, graph);
graph.endContinueSection(n.getLabel());
graph.endBreakSection(n.getLabel()); graph.endBreakSection(n.getLabel());
} }
@ -62,11 +65,13 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
Node whileStart = n.getCondition(); Node whileStart = n.getCondition();
graph.beginBlock(n, whileStart); graph.beginBlock(n, whileStart);
graph.beginBreakSection(); graph.beginBreakSection();
graph.beginContinueSection(whileStart);
graph.connectTo(whileStart); graph.connectTo(whileStart);
graph.setNextLabel(true); graph.setNextLabel(true);
n.getBody().accept(this, graph); n.getBody().accept(this, graph);
graph.connectTo(whileStart); graph.connectTo(whileStart);
graph.setNextLabel(false); graph.setNextLabel(false);
graph.endContinueSection();
graph.endBreakSection(); graph.endBreakSection();
graph.endBlock(); graph.endBlock();
} }
@ -76,10 +81,12 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
Node condition = n.getCondition(); Node condition = n.getCondition();
graph.beginBlock(n, condition); graph.beginBlock(n, condition);
graph.beginBreakSection(); graph.beginBreakSection();
graph.beginContinueSection(condition);
graph.appendNode(condition); graph.appendNode(condition);
n.getBody().accept(this, graph); n.getBody().accept(this, graph);
graph.connectTo(condition); graph.connectTo(condition);
graph.setNextLabel(false); graph.setNextLabel(false);
graph.endContinueSection();
graph.endBreakSection(); graph.endBreakSection();
graph.endBlock(); graph.endBlock();
} }
@ -96,10 +103,12 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
} }
// Comparison expression // Comparison expression
// TODO: shortcut conditions (||, &&) // TODO: shortcut conditions (||, &&)
if (n.getCompare().isPresent()) { assert n.getTokenRange().isPresent();
graph.connectTo(n.getCompare().get()); if (!n.getCompare().isPresent())
graph.setNextLabel(true); n.setCompare(new BooleanLiteralExpr(n.getTokenRange().get(), true));
} graph.beginContinueSection(n.getCompare().get());
graph.connectTo(n.getCompare().get());
graph.setNextLabel(true);
// Loop body // Loop body
n.getBody().accept(this, graph); n.getBody().accept(this, graph);
// Update expressions // Update expressions
@ -108,14 +117,9 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
graph.connectTo(e); graph.connectTo(e);
// Connect to comparison expression // Connect to comparison expression
// Set comparison as last possible statement // Set comparison as last possible statement
if (n.getCompare().isPresent()) { graph.connectTo(n.getCompare().get());
graph.connectTo(n.getCompare().get()); graph.setNextLabel(false);
graph.setNextLabel(false); graph.endContinueSection();
} else {
// TODO: connect last update / end of loop with first element of loop body
// There is no comparison, can't exit the loop TODO implement break and continue as way to exit
graph.clearNode();
}
graph.endBreakSection(); graph.endBreakSection();
graph.endBlock(); graph.endBlock();
} }
@ -125,11 +129,13 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
ForeachStmt copy = new ForeachStmt(n.getTokenRange().orElse(null), n.getVariable(), n.getIterable(), new EmptyStmt()); ForeachStmt copy = new ForeachStmt(n.getTokenRange().orElse(null), n.getVariable(), n.getIterable(), new EmptyStmt());
graph.beginBlock(n, copy); graph.beginBlock(n, copy);
graph.beginBreakSection(); graph.beginBreakSection();
graph.beginContinueSection(copy);
graph.connectTo(copy); graph.connectTo(copy);
graph.setNextLabel(true); graph.setNextLabel(true);
n.getBody().accept(this, graph); n.getBody().accept(this, graph);
graph.connectTo(copy); graph.connectTo(copy);
graph.setNextLabel(false); graph.setNextLabel(false);
graph.endContinueSection();
graph.endBreakSection(); graph.endBreakSection();
graph.endBlock(); graph.endBlock();
} }
@ -168,9 +174,12 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
@Override @Override
public void visit(BreakStmt n, CFG graph) { public void visit(BreakStmt n, CFG graph) {
graph.connectTo(n, false); graph.connectBreak(n);
graph.clearNode(); }
graph.registerBreak(n);
@Override
public void visit(ContinueStmt n, CFG graph) {
graph.connectContinue(n);
} }
@Override @Override

View file

@ -0,0 +1,17 @@
package mytest;
public class BasicContinue {
public static void main(String[] args) {
int x = 0;
bucle:
while (x < 20) {
x++;
for (int y = 0; y < 10; y++) {
if (y == x) continue;
if (y * 2 == x) continue bucle;
}
if (x > 10) continue;
x++;
}
}
}