|
| 1 | +package edu.kit.cbc.common.corc.parsing; |
| 2 | + |
| 3 | + |
| 4 | +import edu.kit.cbc.common.corc.cbcmodel.CbCFormula; |
| 5 | +import edu.kit.cbc.common.corc.cbcmodel.Condition; |
| 6 | +import edu.kit.cbc.common.corc.cbcmodel.statements.AbstractStatement; |
| 7 | +import edu.kit.cbc.common.corc.cbcmodel.statements.CompositionStatement; |
| 8 | +import edu.kit.cbc.common.corc.cbcmodel.statements.SelectionStatement; |
| 9 | +import edu.kit.cbc.common.corc.cbcmodel.statements.SmallRepetitionStatement; |
| 10 | +import edu.kit.cbc.common.corc.cbcmodel.statements.Statement; |
| 11 | +import edu.kit.cbc.common.corc.parsing.condition.ast.ExistsTree; |
| 12 | +import edu.kit.cbc.common.corc.parsing.condition.ast.ForAllTree; |
| 13 | +import edu.kit.cbc.common.corc.parsing.condition.ast.OldTree; |
| 14 | +import edu.kit.cbc.common.corc.parsing.parser.ast.ArrayAcessTree; |
| 15 | +import edu.kit.cbc.common.corc.parsing.parser.ast.BinaryOperationTree; |
| 16 | +import edu.kit.cbc.common.corc.parsing.parser.ast.CallTree; |
| 17 | +import edu.kit.cbc.common.corc.parsing.parser.ast.IdentTree; |
| 18 | +import edu.kit.cbc.common.corc.parsing.parser.ast.LengthTree; |
| 19 | +import edu.kit.cbc.common.corc.parsing.parser.ast.Tree; |
| 20 | +import edu.kit.cbc.common.corc.parsing.parser.ast.UnaryOperationTree; |
| 21 | +import edu.kit.cbc.common.corc.parsing.program.ast.AssignTree; |
| 22 | +import edu.kit.cbc.common.corc.parsing.program.ast.BlockTree; |
| 23 | +import edu.kit.cbc.common.corc.parsing.program.ast.StatementTree; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +public class SemanticChecker { |
| 29 | + |
| 30 | + private static final java.util.List<String> IGNORED_VARIABLES = java.util.List.of("true", "false"); |
| 31 | + |
| 32 | + public static void checkVariables(CbCFormula formula) throws SemanticException { |
| 33 | + if (formula == null) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + Set<String> declaredVariables = new HashSet<>(); |
| 38 | + if (formula.getJavaVariables() != null) { |
| 39 | + declaredVariables = formula.getJavaVariables().stream() |
| 40 | + .map(v -> { |
| 41 | + String name = v.getName().trim(); |
| 42 | + String[] parts = name.split("\\s+"); |
| 43 | + String last = parts[parts.length - 1]; |
| 44 | + return last.replace("[]", ""); |
| 45 | + }) |
| 46 | + .collect(Collectors.toSet()); |
| 47 | + } |
| 48 | + |
| 49 | + if (formula.getGlobalConditions() != null) { |
| 50 | + for (Condition condition : formula.getGlobalConditions()) { |
| 51 | + checkCondition(condition, declaredVariables); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + checkStatement(formula.getStatement(), declaredVariables); |
| 56 | + } |
| 57 | + |
| 58 | + private static void checkStatement(AbstractStatement statement, Set<String> declaredVariables) |
| 59 | + throws SemanticException { |
| 60 | + if (statement == null) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + checkCondition(statement.getPreCondition(), declaredVariables); |
| 65 | + checkCondition(statement.getPostCondition(), declaredVariables); |
| 66 | + |
| 67 | + if (statement instanceof CompositionStatement comp) { |
| 68 | + checkCondition(comp.getIntermediateCondition(), declaredVariables); |
| 69 | + checkStatement(comp.getFirstStatement(), declaredVariables); |
| 70 | + checkStatement(comp.getSecondStatement(), declaredVariables); |
| 71 | + } else if (statement instanceof SelectionStatement sel) { |
| 72 | + if (sel.getGuards() != null) { |
| 73 | + for (Condition guard : sel.getGuards()) { |
| 74 | + checkCondition(guard, declaredVariables); |
| 75 | + } |
| 76 | + } |
| 77 | + if (sel.getCommands() != null) { |
| 78 | + for (AbstractStatement cmd : sel.getCommands()) { |
| 79 | + checkStatement(cmd, declaredVariables); |
| 80 | + } |
| 81 | + } |
| 82 | + } else if (statement instanceof SmallRepetitionStatement rep) { |
| 83 | + checkCondition(rep.getInvariant(), declaredVariables); |
| 84 | + checkCondition(rep.getVariant(), declaredVariables); |
| 85 | + checkCondition(rep.getGuard(), declaredVariables); |
| 86 | + checkStatement(rep.getLoopStatement(), declaredVariables); |
| 87 | + } else if (statement instanceof Statement stmt) { |
| 88 | + checkTree(stmt.getProgramTree(), declaredVariables); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static void checkCondition(Condition condition, Set<String> declaredVariables) throws SemanticException { |
| 93 | + if (condition == null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + checkTree(condition.getParsedCondition(), declaredVariables); |
| 97 | + } |
| 98 | + |
| 99 | + private static void checkTree(Tree node, Set<String> scope) throws SemanticException { |
| 100 | + if (node == null) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (node instanceof ForAllTree forAll) { |
| 105 | + Set<String> newScope = new HashSet<>(scope); |
| 106 | + if (forAll.variable() != null) { |
| 107 | + newScope.add(forAll.variable().name()); |
| 108 | + } |
| 109 | + checkTree(forAll.condition(), newScope); |
| 110 | + } else if (node instanceof ExistsTree exists) { |
| 111 | + Set<String> newScope = new HashSet<>(scope); |
| 112 | + if (exists.variable() != null) { |
| 113 | + newScope.add(exists.variable().name()); |
| 114 | + } |
| 115 | + checkTree(exists.condition(), newScope); |
| 116 | + } else if (node instanceof IdentTree id) { |
| 117 | + String name = id.name(); |
| 118 | + if (!scope.contains(name) && IGNORED_VARIABLES.stream().noneMatch(name::equalsIgnoreCase)) { |
| 119 | + throw new SemanticException("Variable '" + name + "' is used but not defined."); |
| 120 | + } |
| 121 | + } else if (node instanceof LengthTree len) { |
| 122 | + if (!scope.contains(len.variable())) { |
| 123 | + throw new SemanticException("Variable '" + len.variable() + "' is used but not defined."); |
| 124 | + } |
| 125 | + } else if (node instanceof ArrayAcessTree arr) { |
| 126 | + checkTree(arr.name(), scope); |
| 127 | + checkTree(arr.expr(), scope); |
| 128 | + } else if (node instanceof BinaryOperationTree bin) { |
| 129 | + checkTree(bin.lhs(), scope); |
| 130 | + checkTree(bin.rhs(), scope); |
| 131 | + } else if (node instanceof CallTree call) { |
| 132 | + checkTree(call.name(), scope); |
| 133 | + if (call.params() != null) { |
| 134 | + for (Tree param : call.params()) { |
| 135 | + checkTree(param, scope); |
| 136 | + } |
| 137 | + } |
| 138 | + } else if (node instanceof UnaryOperationTree un) { |
| 139 | + checkTree(un.expr(), scope); |
| 140 | + } else if (node instanceof AssignTree assign) { |
| 141 | + checkTree(assign.name(), scope); |
| 142 | + checkTree(assign.expr(), scope); |
| 143 | + } else if (node instanceof BlockTree block) { |
| 144 | + if (block.statements() != null) { |
| 145 | + for (StatementTree stmt : block.statements()) { |
| 146 | + checkTree(stmt, scope); |
| 147 | + } |
| 148 | + } |
| 149 | + } else if (node instanceof OldTree old) { |
| 150 | + checkTree(old.variable(), scope); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments