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> {
return closesCase;
}
@Override
public ASTNode visit(DefaultCaseStatement defCase) {
defCase.getBody().accept(this);
return defCase;
}
@Override
public CaseStatement visit(CaseStatement caseStatement) {
//caseStatement.getGuard().accept(this);
......@@ -189,4 +195,6 @@ public class ASTChanger extends DefaultASTVisitor<ASTNode> {
e.setExpression((Expression) e.getExpression().accept(this));
return e;
}
}
......@@ -188,4 +188,10 @@ public interface ASTTraversal<T> extends Visitor<T> {
closesCase.getBody().accept(this);
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> {
return defaultVisit(casesStatement);
}
@Override
public T visit(DefaultCaseStatement defCase) {
return defaultVisit(defCase);
}
@Override
public T visit(CaseStatement caseStatement) {
return defaultVisit(caseStatement);
......
......@@ -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 lombok.Getter;
import lombok.Setter;
import java.util.Iterator;
import java.util.Map;
import edu.kit.iti.formal.psdbg.parser.ast.*;
/**
......@@ -144,12 +144,19 @@ public class PrettyPrinter extends DefaultASTVisitor<Void> {
c.accept(this);
nl();
}
if (casesStatement.getDefaultCase() != null) {
/* if (casesStatement.getDefaultCase() != null) {
s.append("default {");
casesStatement.getDefaultCase().accept(this);
cl();
s.append("}");
}*/
if (casesStatement.getDefCaseStmt() != null) {
s.append("default {");
casesStatement.getDefCaseStmt().accept(this);
cl();
s.append("}");
}
decrementIndent();
cl();
s.append("}");
......
......@@ -312,9 +312,14 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
CasesStatement cases = new CasesStatement();
ctx.casesList().forEach(c -> cases.getCases().add((CaseStatement) c.accept(this)));
if (ctx.DEFAULT() != null) {
cases.setDefaultCase((Statements)
/* cases.setDefaultCase((Statements)
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);
return cases;
......
......@@ -51,6 +51,8 @@ public interface Visitor<T> {
T visit(CasesStatement cases);
T visit(DefaultCaseStatement defCase);
//T visit(CaseStatement case_);
T visit(CallStatement call);
......
......@@ -39,7 +39,9 @@ import java.util.List;
@Data
public class CasesStatement extends Statement<ScriptLanguageParser.CasesStmtContext> {
@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}
......@@ -54,8 +56,10 @@ public class CasesStatement extends Statement<ScriptLanguageParser.CasesStmtCont
@Override public CasesStatement copy() {
CasesStatement c = new CasesStatement();
cases.forEach(caseStatement -> c.cases.add(caseStatement.copy()));
if (defaultCase != null)
c.defaultCase = defaultCase.copy();
// if (defaultCase != null)
// c.defaultCase = defaultCase.copy();
if (defCaseStmt != null)
c.defCaseStmt = defCaseStmt.copy();
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>
}
//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()) {
VariableAssignment va = new VariableAssignment();
Statements defaultCase = casesStatement.getDefaultCase();
//Statements defaultCase = casesStatement.getDefaultCase();
DefaultCaseStatement defCaseStmt = casesStatement.getDefCaseStmt();
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>
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
public Object visit(SimpleCaseStatement simpleCaseStatement) {
Expression matchExpression = simpleCaseStatement.getGuard();
......
......@@ -214,8 +214,19 @@ public class ControlFlowVisitor extends DefaultASTVisitor<Void> {
graph.putEdgeValue(lastNode, currentNode, EdgeTypes.STEP_RETURN);
// }
}
if (casesStatement.getDefaultCase() != null) {
Statements defaultCase = casesStatement.getDefaultCase();
//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);
mappingOfNodes.put(defaultCase, caseNode);
......@@ -225,7 +236,7 @@ public class ControlFlowVisitor extends DefaultASTVisitor<Void> {
graph.putEdgeValue(caseNode, currentNode, EdgeTypes.STEP_BACK);
lastNode = caseNode;
defaultCase.accept(this);
graph.putEdgeValue(lastNode, currentNode, EdgeTypes.STEP_RETURN);
graph.putEdgeValue(lastNode, currentNode, EdgeTypes.STEP_RETURN);*/
}
lastNode = currentNode;
return null;
......
......@@ -74,7 +74,8 @@ public class StateGraphWrapper<T> {
this.cfgVisitor = cfgVisitor;
this.mainScript = mainScript;
// createRootNode(null);
createRootNode(this.mainScript);
}
......@@ -294,7 +295,9 @@ public class StateGraphWrapper<T> {
if (root.get() == null) {
createRootNode(proofScript);
} else {
createNewNode(proofScript);
if (!root.get().getScriptstmt().equals(proofScript)) {
createNewNode(proofScript);
}
}
return null;
}
......@@ -349,9 +352,15 @@ public class StateGraphWrapper<T> {
public Void visit(ClosesCase closesCase) {
return createNewNode(closesCase);
}
@Override
public Void visit(DefaultCaseStatement defCase) {
return createNewNode(defCase);
}
}
private class ExitListener extends DefaultASTVisitor<Void> {
@Override
public Void visit(AssignmentStatement assignment) {
return addState(assignment);
......@@ -367,6 +376,11 @@ public class StateGraphWrapper<T> {
return addState(caseStatement);
}*/
@Override
public Void visit(DefaultCaseStatement defCase) {
return addState(defCase);
}
@Override
public Void visit(CallStatement 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