Commit c6f8d090 authored by Sarah Grebing's avatar Sarah Grebing
Browse files

Cases Statement (partially)

parent 0521f0df
Pipeline #10607 failed with stage
in 3 minutes and 29 seconds
...@@ -135,30 +135,67 @@ public class Interpreter<T> extends DefaultASTVisitor<T> { ...@@ -135,30 +135,67 @@ public class Interpreter<T> extends DefaultASTVisitor<T> {
for (GoalNode node : allGoalsBeforeCases) { for (GoalNode node : allGoalsBeforeCases) {
node.enterNewVarScope(); node.enterNewVarScope();
} }
List<GoalNode> goalsAfterCases = new ArrayList<>();
//copy the list of goal nodes for keeping track of goals //copy the list of goal nodes for keeping track of goals
List<GoalNode> copiedList = new ArrayList<>(); Set<GoalNode> copiedList = new HashSet<>();
for (GoalNode goalNode : allGoalsBeforeCases) { for (GoalNode goalNode : allGoalsBeforeCases) {
copiedList.add(goalNode); copiedList.add(goalNode);
} }
//handle cases TODO //handle cases
List<CaseStatement> cases = casesStatement.getCases(); List<CaseStatement> cases = casesStatement.getCases();
Iterator<CaseStatement> casesIter = cases.iterator(); Iterator<CaseStatement> casesIter = cases.iterator();
while (casesIter.hasNext()) { while (casesIter.hasNext()) {
CaseStatement currentCase = casesIter.next(); CaseStatement currentCase = casesIter.next();
currentCase.getGuard(); Expression guard = currentCase.getGuard();
Statements body = currentCase.getBody();
Iterator<GoalNode> goalIter = copiedList.iterator();
Set<GoalNode> forCase = new HashSet<>();
while (goalIter.hasNext()) {
GoalNode g = goalIter.next();
Evaluator goalEval = new Evaluator(g);
Value eval = goalEval.eval(guard);
if (eval.getData().equals(Value.TRUE)) {
forCase.add(g);
//copiedList.remove(g);
}
}
copiedList.removeAll(forCase);
Iterator<GoalNode> caseGoals = forCase.iterator();
while (caseGoals.hasNext()) {
GoalNode current = caseGoals.next();
List<GoalNode> goalList = new ArrayList<>();
goalList.add(current);
State s = new State(goalList, current);
stateStack.push(s);
visit(body);
State aftercase = (State) stateStack.pop();
goalsAfterCases.addAll(aftercase.getGoals());
}
//jetzt body auswerten mit der Liste der Ziele
} }
casesStatement.getDefaultCase(); casesStatement.getDefaultCase();
//exit scope //exit scope
State aftercases = (State) stateStack.pop();
List<GoalNode> goalsAfterCases = aftercases.getGoals(); State newStateAfterCases;
if (!goalsAfterCases.isEmpty()) { if (!goalsAfterCases.isEmpty()) {
for (GoalNode goalAfterCases : goalsAfterCases) { for (GoalNode goalAfterCases : goalsAfterCases) {
goalAfterCases.exitNewVarScope(); goalAfterCases.exitNewVarScope();
} }
if (goalsAfterCases.size() == 1) {
newStateAfterCases = new State(goalsAfterCases, goalsAfterCases.get(0));
} else {
newStateAfterCases = new State(goalsAfterCases, null);
}
stateStack.push(newStateAfterCases);
} }
return null; return null;
} }
......
...@@ -40,11 +40,11 @@ public class Value<T> { ...@@ -40,11 +40,11 @@ public class Value<T> {
} }
public static Value<Boolean> from(BooleanLiteral b) { public static Value<Boolean> from(BooleanLiteral b) {
return new Value<>(Type.BOOL, b.isValue()); return new Value(Type.BOOL, (b.isValue() ? Value.TRUE : Value.FALSE));
} }
public static Value<Boolean> from(boolean equals) { public static Value<Boolean> from(boolean equals) {
return new Value<>(Type.BOOL, equals); return new Value(Type.BOOL, (equals ? Value.TRUE : Value.FALSE));
} }
public static Value<BigInteger> from(BigInteger apply) { public static Value<BigInteger> from(BigInteger apply) {
......
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