-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInterpreter.java
More file actions
68 lines (59 loc) · 2.3 KB
/
Interpreter.java
File metadata and controls
68 lines (59 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.Map;
public class Interpreter implements Sentence.Visitor<Boolean> {
private final Map<String, Boolean> truthAssignment;
private final Map<String, Boolean> intermediateResults;
public Interpreter(Map<String, Boolean> truthAssignment, Map<String, Boolean> intermediateResults) {
this.truthAssignment = truthAssignment;
this.intermediateResults = intermediateResults;
}
// if the sentence is TRUE, FALSE, P, Q, S, then just get the value
// Note that intermediate results is not used here because this are the initial values
@Override
public Boolean visitAtomicSentence(Sentence.AtomicSentence sentence) {
return truthAssignment.get(sentence.value);
}
// if the sentence is NOT sentence, get the value of the right which is the sentence then invert
@Override
public Boolean visitUnarySentence(Sentence.Unary sentence) {
Boolean right = sentence.right.accept(this);
Boolean result = !right;
String rightString = sentence.right.getString();
rightString = rightString.replaceAll("\\(([^()]*)\\)", "$1");
intermediateResults.put("NOT " + rightString, result);
return result;
}
// if the sentence is binary, get the value of the left and right then do the operation according to type
@Override
public Boolean visitBinarySentence(Sentence.Binary sentence) {
Boolean left = sentence.left.accept(this);
Boolean right = sentence.right.accept(this);
Boolean result;
switch (sentence.operator.getType()) {
case AND:
result = left && right;
break;
case OR:
result = left || right;
break;
case IMPLIES:
result = !left || right;
break;
case EQUIVALENT:
result = left == right;
break;
default: // default may not be achieved because of the scanner first
result = false;
break;
}
String rightString = sentence.right.getString();
rightString = rightString.replaceAll("\\(([^()]*)\\)", "$1");
String key = sentence.left.getString() + " " + sentence.operator.getLexeme() + " " + rightString;
intermediateResults.put(key, result);
return result;
}
// we just get the sentence passed in the grouping
@Override
public Boolean visitGroupingSentence(Sentence.Grouping sentence) {
return sentence.expression.accept(this);
}
}