-
Notifications
You must be signed in to change notification settings - Fork 1
/
StackExpression.java
131 lines (121 loc) · 3.98 KB
/
StackExpression.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package bracketAlert;
import java.util.*;
public class StackExpression implements Expression {
private List<String> expList;
public StackExpression(String ruleExpression) {
expList = ruleToExpList(ruleExpression);
}
@Override
public boolean interpret(Map<String, Long> stats) {
Deque<Boolean> boolStack = new LinkedList<>();
Deque<String> logicStack = new LinkedList<>();
logicStack.push("#");
for (String strExpr : expList) {
if (strExpr.contains(">")) {
Expression expression = new GreaterExpression(strExpr);
boolStack.push(expression.interpret(stats));
} else if (strExpr.contains("<")) {
Expression expression = new LessExpression(strExpr);
boolStack.push(expression.interpret(stats));
} else if (strExpr.contains("==")) {
Expression expression = new EqualExpression(strExpr);
boolStack.push(expression.interpret(stats));
} else {
String top = logicStack.getFirst();
int icp = icp(strExpr);
int isp = isp(top);
if (icp < isp) {
Boolean b1 = boolStack.pop();
Boolean b2 = boolStack.pop();
logicStack.pop();
boolean b3;
switch (top) {
case "||":
b3 = b1 || b2;
break;
case "&&":
b3 = b1 && b2;
break;
default:
throw new RuntimeException("invalid logic: \"" + top + "\"");
}
boolStack.push(b3);
} else if (icp == isp) {
logicStack.pop();
} else {
logicStack.push(strExpr);
}
}
}
return boolStack.pop();
}
private List<String> ruleToExpList(String ruleExpression) {
List<String> expList = new ArrayList<>();
StringBuilder sb = new StringBuilder();
char[] chars = ruleExpression.toCharArray();
int i = 0;
while (i < chars.length) {
char c = chars[i];
if (c == '(' || c == ')' || c == '&' || c == '|') {
String trim = sb.toString().trim();
if (!trim.isEmpty()) {
expList.add(trim);
sb.setLength(0);
}
if (c == '(' || c == ')') {
expList.add(String.valueOf(c));
i++;
} else if (c == '&') {
expList.add("&&");
i += 2;
} else {
expList.add("||");
i += 2;
}
} else {
sb.append(c);
i++;
}
}
String trim = sb.toString().trim();
if (!trim.isEmpty()) {
expList.add(trim);
}
expList.add("#");
return expList;
}
// 栈内优先级
private int isp(String ch) {
switch (ch) {
case "#":
return 0;
case "(":
return 1;
case "||":
return 3;
case "&&":
return 5;
case ")":
return 8;
default:
throw new RuntimeException("invalid op: \"" + ch + "\"");
}
}
// 栈外优先级
private int icp(String ch) {
switch (ch) {
case "#":
return 0;
case "(":
return 8;
case "||":
return 2;
case "&&":
return 4;
case ")":
return 1;
default:
throw new RuntimeException("invalid op: \"" + ch + "\"");
}
}
}