Commit 298861e0 authored by Sarah Grebing's avatar Sarah Grebing

Added DefaultCase AstNode to enable listeners in interpreter

parent fbc0c764
...@@ -131,6 +131,12 @@ public class ASTChanger extends DefaultASTVisitor<ASTNode> { ...@@ -131,6 +131,12 @@ public class ASTChanger extends DefaultASTVisitor<ASTNode> {
return closesCase; return closesCase;
} }
@Override
public ASTNode visit(DefaultCaseStatement defCase) {
defCase.getBody().accept(this);
return defCase;
}
@Override @Override
public CaseStatement visit(CaseStatement caseStatement) { public CaseStatement visit(CaseStatement caseStatement) {
//caseStatement.getGuard().accept(this); //caseStatement.getGuard().accept(this);
...@@ -189,4 +195,6 @@ public class ASTChanger extends DefaultASTVisitor<ASTNode> { ...@@ -189,4 +195,6 @@ public class ASTChanger extends DefaultASTVisitor<ASTNode> {
e.setExpression((Expression) e.getExpression().accept(this)); e.setExpression((Expression) e.getExpression().accept(this));
return e; return e;
} }
} }
...@@ -188,4 +188,10 @@ public interface ASTTraversal<T> extends Visitor<T> { ...@@ -188,4 +188,10 @@ public interface ASTTraversal<T> extends Visitor<T> {
closesCase.getBody().accept(this); closesCase.getBody().accept(this);
return null; return null;
} }
@Override
default T visit(DefaultCaseStatement defCase) {
defCase.getBody().accept(this);
return null;
}
} }
...@@ -87,6 +87,11 @@ public class DefaultASTVisitor<T> implements Visitor<T> { ...@@ -87,6 +87,11 @@ public class DefaultASTVisitor<T> implements Visitor<T> {
return defaultVisit(casesStatement); return defaultVisit(casesStatement);
} }
@Override
public T visit(DefaultCaseStatement defCase) {
return defaultVisit(defCase);
}
@Override @Override
public T visit(CaseStatement caseStatement) { public T visit(CaseStatement caseStatement) {
return defaultVisit(caseStatement); return defaultVisit(caseStatement);
......
...@@ -23,13 +23,13 @@ package edu.kit.iti.formal.psdbg.parser; ...@@ -23,13 +23,13 @@ package edu.kit.iti.formal.psdbg.parser;
*/ */
import edu.kit.iti.formal.psdbg.parser.ast.*;
import edu.kit.iti.formal.psdbg.parser.types.Type; import edu.kit.iti.formal.psdbg.parser.types.Type;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import edu.kit.iti.formal.psdbg.parser.ast.*;
/** /**
...@@ -144,12 +144,19 @@ public class PrettyPrinter extends DefaultASTVisitor<Void> { ...@@ -144,12 +144,19 @@ public class PrettyPrinter extends DefaultASTVisitor<Void> {
c.accept(this); c.accept(this);
nl(); nl();
} }
if (casesStatement.getDefaultCase() != null) { /* if (casesStatement.getDefaultCase() != null) {
s.append("default {"); s.append("default {");
casesStatement.getDefaultCase().accept(this); casesStatement.getDefaultCase().accept(this);
cl(); cl();
s.append("}"); s.append("}");
}*/
if (casesStatement.getDefCaseStmt() != null) {
s.append("default {");
casesStatement.getDefCaseStmt().accept(this);
cl();
s.append("}");
} }
decrementIndent(); decrementIndent();
cl(); cl();
s.append("}"); s.append("}");
......
...@@ -312,9 +312,14 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -312,9 +312,14 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
CasesStatement cases = new CasesStatement(); CasesStatement cases = new CasesStatement();
ctx.casesList().forEach(c -> cases.getCases().add((CaseStatement) c.accept(this))); ctx.casesList().forEach(c -> cases.getCases().add((CaseStatement) c.accept(this)));
if (ctx.DEFAULT() != null) { if (ctx.DEFAULT() != null) {
cases.setDefaultCase((Statements) /* cases.setDefaultCase((Statements)
ctx.defList.accept(this) ctx.defList.accept(this)
); );*/
DefaultCaseStatement defCase = new DefaultCaseStatement();
defCase.setRuleContext(ctx.defList);
defCase.setBody((Statements) ctx.defList.accept(this));
// defCase.setClosedStmt(false);
cases.setDefCaseStmt(defCase);
} }
cases.setRuleContext(ctx); cases.setRuleContext(ctx);
return cases; return cases;
......
...@@ -51,6 +51,8 @@ public interface Visitor<T> { ...@@ -51,6 +51,8 @@ public interface Visitor<T> {
T visit(CasesStatement cases); T visit(CasesStatement cases);
T visit(DefaultCaseStatement defCase);
//T visit(CaseStatement case_); //T visit(CaseStatement case_);
T visit(CallStatement call); T visit(CallStatement call);
......
...@@ -39,7 +39,9 @@ import java.util.List; ...@@ -39,7 +39,9 @@ import java.util.List;
@Data @Data
public class CasesStatement extends Statement<ScriptLanguageParser.CasesStmtContext> { public class CasesStatement extends Statement<ScriptLanguageParser.CasesStmtContext> {
@NonNull private final List<CaseStatement> cases = new ArrayList<>(); @NonNull private final List<CaseStatement> cases = new ArrayList<>();
@NonNull private Statements defaultCase = new Statements(); // @NonNull private Statements defaultCase = new Statements();
@NonNull
private DefaultCaseStatement defCaseStmt = new DefaultCaseStatement();
/** /**
* {@inheritDoc} * {@inheritDoc}
...@@ -54,8 +56,10 @@ public class CasesStatement extends Statement<ScriptLanguageParser.CasesStmtCont ...@@ -54,8 +56,10 @@ public class CasesStatement extends Statement<ScriptLanguageParser.CasesStmtCont
@Override public CasesStatement copy() { @Override public CasesStatement copy() {
CasesStatement c = new CasesStatement(); CasesStatement c = new CasesStatement();
cases.forEach(caseStatement -> c.cases.add(caseStatement.copy())); cases.forEach(caseStatement -> c.cases.add(caseStatement.copy()));
if (defaultCase != null) // if (defaultCase != null)
c.defaultCase = defaultCase.copy(); // c.defaultCase = defaultCase.copy();
if (defCaseStmt != null)
c.defCaseStmt = defCaseStmt.copy();
return c; return c;
} }
} }
package edu.kit.iti.formal.psdbg.parser.ast;
import edu.kit.iti.formal.psdbg.parser.ScriptLanguageParser;
import edu.kit.iti.formal.psdbg.parser.Visitor;
import lombok.Data;
@Data
public class DefaultCaseStatement extends Statement<ScriptLanguageParser.StmtListContext> {
protected Statements body;
public DefaultCaseStatement() {
this.body = new Statements();
}
public DefaultCaseStatement(Statements body) {
this.body = body;
}
/**
* {@inheritDoc}
*/
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
/**
* {@inheritDoc}
*/
@Override
public DefaultCaseStatement copy() {
return new DefaultCaseStatement(body.copy());
}
}
...@@ -223,12 +223,15 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -223,12 +223,15 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
} }
//for all remaining goals execute default //for all remaining goals execute default case
//we need an entry/exit listener for default case
if (!toMatch.isEmpty()) { if (!toMatch.isEmpty()) {
VariableAssignment va = new VariableAssignment(); VariableAssignment va = new VariableAssignment();
Statements defaultCase = casesStatement.getDefaultCase(); //Statements defaultCase = casesStatement.getDefaultCase();
DefaultCaseStatement defCaseStmt = casesStatement.getDefCaseStmt();
for (GoalNode<T> goal : toMatch) { for (GoalNode<T> goal : toMatch) {
resultingGoals.addAll(executeBody(defaultCase, goal, va).getGoals()); resultingGoals.addAll(executeDefaultCase(defCaseStmt, goal, va).getGoals());
// resultingGoals.addAll(executeBody(defaultCase, goal, va).getGoals());
} }
} }
...@@ -253,6 +256,13 @@ public class Interpreter<T> extends DefaultASTVisitor<Object> ...@@ -253,6 +256,13 @@ public class Interpreter<T> extends DefaultASTVisitor<Object>
return null; return null;
} }
private State<T> executeDefaultCase(DefaultCaseStatement defCaseStmt, GoalNode<T> goal, VariableAssignment va) {
enterScope(defCaseStmt);
State<T> newState = executeBody(defCaseStmt.getBody(), goal, va);
exitScope(defCaseStmt);
return newState;
}
@Override @Override
public Object visit(SimpleCaseStatement simpleCaseStatement) { public Object visit(SimpleCaseStatement simpleCaseStatement) {
Expression matchExpression = simpleCaseStatement.getGuard(); Expression matchExpression = simpleCaseStatement.getGuard();
......
...@@ -214,8 +214,19 @@ public class ControlFlowVisitor extends DefaultASTVisitor<Void> { ...@@ -214,8 +214,19 @@ public class ControlFlowVisitor extends DefaultASTVisitor<Void> {
graph.putEdgeValue(lastNode, currentNode, EdgeTypes.STEP_RETURN); graph.putEdgeValue(lastNode, currentNode, EdgeTypes.STEP_RETURN);
// } // }
} }
if (casesStatement.getDefaultCase() != null) { //casesStatement.getDefaultCase()
Statements defaultCase = casesStatement.getDefaultCase(); if (casesStatement.getDefCaseStmt() != null) {
DefaultCaseStatement defCase = casesStatement.getDefCaseStmt();
ControlFlowNode caseNode = new ControlFlowNode(defCase);
mappingOfNodes.put(defCase, caseNode);
graph.addNode(caseNode);
//System.out.println("\n" + caseNode + "\n");
graph.putEdgeValue(currentNode, caseNode, EdgeTypes.STEP_OVER); //??is this right?
graph.putEdgeValue(caseNode, currentNode, EdgeTypes.STEP_BACK);
lastNode = caseNode;
defCase.getBody().accept(this);
graph.putEdgeValue(lastNode, currentNode, EdgeTypes.STEP_RETURN);
/*Statements defaultCase = defCase.getBody();
ControlFlowNode caseNode = new ControlFlowNode(defaultCase); ControlFlowNode caseNode = new ControlFlowNode(defaultCase);
mappingOfNodes.put(defaultCase, caseNode); mappingOfNodes.put(defaultCase, caseNode);
...@@ -225,7 +236,7 @@ public class ControlFlowVisitor extends DefaultASTVisitor<Void> { ...@@ -225,7 +236,7 @@ public class ControlFlowVisitor extends DefaultASTVisitor<Void> {
graph.putEdgeValue(caseNode, currentNode, EdgeTypes.STEP_BACK); graph.putEdgeValue(caseNode, currentNode, EdgeTypes.STEP_BACK);
lastNode = caseNode; lastNode = caseNode;
defaultCase.accept(this); defaultCase.accept(this);
graph.putEdgeValue(lastNode, currentNode, EdgeTypes.STEP_RETURN); graph.putEdgeValue(lastNode, currentNode, EdgeTypes.STEP_RETURN);*/
} }
lastNode = currentNode; lastNode = currentNode;
return null; return null;
......
...@@ -74,7 +74,8 @@ public class StateGraphWrapper<T> { ...@@ -74,7 +74,8 @@ public class StateGraphWrapper<T> {
this.cfgVisitor = cfgVisitor; this.cfgVisitor = cfgVisitor;
this.mainScript = mainScript; this.mainScript = mainScript;
// createRootNode(null);
createRootNode(this.mainScript);
} }
...@@ -294,7 +295,9 @@ public class StateGraphWrapper<T> { ...@@ -294,7 +295,9 @@ public class StateGraphWrapper<T> {
if (root.get() == null) { if (root.get() == null) {
createRootNode(proofScript); createRootNode(proofScript);
} else { } else {
createNewNode(proofScript); if (!root.get().getScriptstmt().equals(proofScript)) {
createNewNode(proofScript);
}
} }
return null; return null;
} }
...@@ -349,9 +352,15 @@ public class StateGraphWrapper<T> { ...@@ -349,9 +352,15 @@ public class StateGraphWrapper<T> {
public Void visit(ClosesCase closesCase) { public Void visit(ClosesCase closesCase) {
return createNewNode(closesCase); return createNewNode(closesCase);
} }
@Override
public Void visit(DefaultCaseStatement defCase) {
return createNewNode(defCase);
}
} }
private class ExitListener extends DefaultASTVisitor<Void> { private class ExitListener extends DefaultASTVisitor<Void> {
@Override @Override
public Void visit(AssignmentStatement assignment) { public Void visit(AssignmentStatement assignment) {
return addState(assignment); return addState(assignment);
...@@ -367,6 +376,11 @@ public class StateGraphWrapper<T> { ...@@ -367,6 +376,11 @@ public class StateGraphWrapper<T> {
return addState(caseStatement); return addState(caseStatement);
}*/ }*/
@Override
public Void visit(DefaultCaseStatement defCase) {
return addState(defCase);
}
@Override @Override
public Void visit(CallStatement call) { public Void visit(CallStatement call) {
return addState(call); return addState(call);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment