-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogicalExp.java
More file actions
50 lines (46 loc) · 1.64 KB
/
LogicalExp.java
File metadata and controls
50 lines (46 loc) · 1.64 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
class LogicalExp extends AstExpression {
private LogicalExp(String opCode) {
super(opCode);
}
static String[] OPCODES = {"and", "or", "xor"};
@Override
String performAction(String[] args) throws ExpressionFormatException {
if (args.length != 2) {
throw new IllegalArgumentException("Wrong number of arguments");
}
long left = Long.parseLong(args[0]);
long right = Long.parseLong(args[1]);
switch (content) {
case "and":
return String.valueOf(left & right);
case "or":
return String.valueOf(left | right);
case "xor":
return String.valueOf(left ^ right);
default:
throw new ExpressionFormatException("Wrong operator of action");
}
}
static boolean isThisOpCode(String opCode) {
if (opCode == null || opCode.isEmpty()) {
return false;
}
for (String curOp : OPCODES) {
if (opCode.equals(curOp)) {
return true;
}
}
return false;
}
static AstNode parseLogical(ExpParser parser, boolean isInsideParenthesis) throws ExpressionFormatException {
AstNode res = RelationExp.parseRelation(parser, isInsideParenthesis);
while (isThisOpCode(parser.getLast())) {
AstNode newNode = new AstNode(new LogicalExp(parser.getLast()));
parser.moveNext();
newNode.setLeftChild(res);
newNode.setRightChild(RelationExp.parseRelation(parser, isInsideParenthesis));
res = newNode;
}
return res;
}
}