-
Notifications
You must be signed in to change notification settings - Fork 0
/
TermExp.java
48 lines (44 loc) · 1.53 KB
/
TermExp.java
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
class TermExp extends AstExpression {
private TermExp(String opCode) {
super(opCode);
}
static String[] OPCODES = {"+", "-"};
@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 "+":
return String.valueOf(left + right);
case "-":
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 parseTerm(ExpParser parser, boolean isInsideParenthesis) throws ExpressionFormatException {
AstNode res = FactorExp.parseFactor(parser, isInsideParenthesis);
while (isThisOpCode(parser.getLast())) {
AstNode newNode = new AstNode(new TermExp(parser.getLast()));
parser.moveNext();
newNode.setLeftChild(res);
newNode.setRightChild(FactorExp.parseFactor(parser, isInsideParenthesis));
res = newNode;
}
return res;
}
}