-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expression.java
121 lines (93 loc) · 4.06 KB
/
Expression.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
import java.util.Stack;
import java.util.StringTokenizer;
/**
*Rezolva expresia
*/
public class Expression {
public static OperatorFactory operatorFactory = null;
static String[] arrayOfWords = new String[10000];
/**
* Creaza un nou string inlocuind continutul parantezelor cu valori de adevar
*/
public static String evaluate_inside_parenthesis(String helper, String name, String value) {
operatorFactory = OperatorFactory.getInstance();
StringTokenizer Tok = new StringTokenizer(helper);
int n = 0;
while(Tok.hasMoreElements()) {
arrayOfWords[n] = Tok.nextToken();
n++;
}
String expressionToBeProced = new String("");
for (int i = 0; i < n; i++) {
if(arrayOfWords[i].equals("eq") || arrayOfWords[i].equals("ne")) {
Operator operator = operatorFactory.makeOperator(arrayOfWords[i]);
if(arrayOfWords[i+1].equals("name")) {
operator.setType("name");
operator.setRef(arrayOfWords[i+2]);
operator.setArgument(name);
expressionToBeProced = expressionToBeProced + (char) (operator.apply() + 48) + " ";
i += 2;
}
else {
if(arrayOfWords[i+1].equals("value")) {
operator.setType("value");
operator.setRef(arrayOfWords[i + 2]);
operator.setArgument(value);
expressionToBeProced = expressionToBeProced + (char) (operator.apply() + 48) + " ";
i += 2;
}
}
} else if (arrayOfWords[i].equals("lt") || arrayOfWords[i].equals("le") || arrayOfWords[i].equals("gt") ||
arrayOfWords[i].equals("ge")) {
Operator operator = operatorFactory.makeOperator(arrayOfWords[i]);
operator.setType("value");
operator.setRef(arrayOfWords[i + 2]);
operator.setArgument(value);
expressionToBeProced = expressionToBeProced + (char) (operator.apply() + 48) + " ";
i += 2;
} else {
expressionToBeProced = expressionToBeProced + arrayOfWords[i] + " ";
}
}
return expressionToBeProced;
}
/**
* Creaza arborele si intoarce valoarea 1 in caz de succes si 0 in caz de fail
*/
public static int success(Observer observer, String name, String value) {
String expression = observer.getExpression();
if (expression.equals("nil")) {
return 1;
}
String expressionToBeProced = Expression.evaluate_inside_parenthesis(expression, name, value);
Stack<String> expressionProced = SYA.takeInput(expressionToBeProced);
if (expressionProced.size() == 1) {
return Integer.parseInt(expressionProced.pop());
}
OrNode or;
AndNode and;
int result = 0;
Stack<Integer> intermmediar = new Stack<Integer>();
while(!expressionProced.isEmpty()) {
String last = expressionProced.firstElement();
expressionProced.removeElement(last);
if(last.equals("0") || last.equals("1")) {
intermmediar.push(Integer.parseInt(last));
}
else {
OperandNode n1 = new OperandNode(intermmediar.pop());
OperandNode n2 = new OperandNode(intermmediar.pop());
if(last.equals("||")) {
or = new OrNode(n1, n2);
intermmediar.push(or.accept(new CalculatorVisitor()));
}
if(last.equals("&&")) {
and = new AndNode(n1, n2);
intermmediar.push(and.accept(new CalculatorVisitor()));
}
}
}
result = intermmediar.pop();
return result;
}
}