-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.java
More file actions
181 lines (144 loc) · 3.6 KB
/
Copy pathToken.java
File metadata and controls
181 lines (144 loc) · 3.6 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* This class describes tokens and contains methods for working with tokens.
* For example isOperator - is token operator or not, isFunction - is token
* function name.
*
* @author pbsphp
*/
public class Token {
/**
* Constructor of Token.
*
* @param token as string, e.g. "+", "10", "(".
*/
public Token(String tokenAsString) {
this.tokenAsString = tokenAsString;
}
/**
* Converts token to string. (for new Token(T) it returns T)
*
* @return token as string
*/
public String toString() {
return this.tokenAsString;
}
/**
* Tests if token is operator
*
* @return is token operator
*/
public boolean isOperator() {
return contains(operators, tokenAsString);
}
/**
* Tests if token is number
*
* @return is token number
*/
public boolean isNumber() {
char fst = tokenAsString.charAt(0);
return Character.isDigit(fst);
}
/**
* Tests if token is function name
*
* @return is token function name
*/
public boolean isFunction() {
char fst = tokenAsString.charAt(0);
return Character.isAlphabetic(fst);
}
/**
* Tests if token is separator in function arguments default is ','
*
* @return is token separator
*/
public boolean isSeparator() {
return tokenAsString.equals(",");
}
/**
* Tests if token is left brace
*
* @return is token '('
*/
public boolean isLeftBracket() {
return tokenAsString.equals("(");
}
/**
* Tests if token is right brace
*
* @return is token ')'
*/
public boolean isRightBracket() {
return tokenAsString.equals(")");
}
/**
* Tests if token is left associative operator
*
* @return is token left associative
*/
public boolean isLeftAssoc() {
return !this.isRightAssoc();
}
/**
* Tests if token is right associative operator
*
* @return is token right associative
*/
public boolean isRightAssoc() {
return contains(rightAssocOperators, tokenAsString);
}
/**
* Tests if token is binary operator like '+' in 'A + B'
*
* @return is token binary operator
*/
public boolean isBinary() {
return !this.isUnary();
}
/**
* Tests if token is unary operator like '-' in 'A * -B'
*
* @return is token unary operator
*/
public boolean isUnary() {
return this.isOperator() && tokenAsString.startsWith("u");
}
/**
* Returns priority of operator.
* For example priority of + is lower than priority of *
*
* @return priority
*/
public int getPriority() {
for (int i = 0; i < priorityTable.length; ++i) {
if (contains(priorityTable[i], tokenAsString)) {
return i;
}
}
return 0;
}
/**
* Converts token to number (now is Double)
* Token must be number!
*
* @bug bad conversion of not-numeric tokens
*
* @return token as number
*/
public double toNumber() {
return Double.parseDouble(tokenAsString);
}
protected boolean contains(String[] strings, String str) {
for (String el : strings) {
if (el.equals(str)) {
return true;
}
}
return false;
}
String tokenAsString;
String[] operators = { "+", "-", "*", "/" };
String[] rightAssocOperators = { };
String[][] priorityTable = {{ "+", "-" }, { "*", "/" }};
}