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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/******************************************************************************
* Copyright (C) 2011 Michael Hofmann <mh21@piware.de> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
******************************************************************************/
Quark expression_error_quark() {
return Quark.from_string("expression-error-quark");
}
public class ExpressionParser {
Data[] datas;
public ExpressionParser(Data[] datas) {
this.datas = datas;
}
private static void expandtoken(char *current,
ref char *last) {
if (last == null)
last = current;
// stderr.printf("Expanding token to '%s'\n", strndup(last, current - last + 1));
}
private static string[] savetoken(char *current,
ref char *last, string[] result) {
string[] r = result;
if (last != null) {
var token = strndup(last, current - last);
// stderr.printf("Saving token '%s'\n", token);
r += token;
last = null;
} else {
// stderr.printf("Not saving empty token\n");
}
return r;
}
private static string[] addtoken(char current,
string[] result) {
string[] r = result;
var token = current.to_string();
// stderr.printf("Adding token '%s'\n", token);
r += token;
return r;
}
private static bool isspace(char current) {
return current == ' ';
}
private static bool isvariable(char current) {
return
current >= 'a' && current <= 'z' ||
current >= '0' && current <= '9' ||
current == '.';
}
public string[] tokenize(string expression) {
string[] result = null;
char *last = null;
char *current = expression;
int level = 0;
bool inexpression = false;
for (; *current != '\0'; current = current + 1) {
if (!inexpression) {
if (*current == '$') {
result = savetoken(current, ref last, result);
result = addtoken(*current, result);
inexpression = true;
} else {
expandtoken(current, ref last);
}
} else {
if (level == 0) {
if (isvariable(*current)) {
expandtoken(current, ref last);
} else if (last == null && *current == '(') {
result = addtoken(*current, result);
++level;
} else {
result = addtoken('(', result);
result = savetoken(current, ref last, result);
result = addtoken(')', result);
expandtoken(current, ref last);
inexpression = false;
}
} else {
if (*current == '(') {
result = savetoken(current, ref last, result);
result = addtoken(*current, result);
++level;
} else if (*current == ')') {
result = savetoken(current, ref last, result);
result = addtoken(*current, result);
--level;
if (level == 0)
inexpression = false;
} else if (isspace(*current)) {
result = savetoken(current, ref last, result);
} else if (!isvariable(*current)) {
result = savetoken(current, ref last, result);
result = addtoken(*current, result);
} else {
expandtoken(current, ref last);
}
}
}
}
result = savetoken(current, ref last, result);
return result;
}
private Error error(uint index, string message) {
return new Error(expression_error_quark(), (int)index, "%s", message);
}
private string evaluate_expression(string[] tokens, ref uint index) throws Error {
if (index >= tokens.length)
throw error(index, "empty expression");
if (tokens[index] == "(")
return evaluate_expression_parens(tokens, ref index);
return evaluate_expression_name(tokens, ref index);
}
private string evaluate_expression_times(string[] tokens, ref uint index) throws Error {
string result = null;
bool div = false;
for (;;) {
if (index >= tokens.length)
throw error(index, "times: expected expression");
var value = evaluate_expression(tokens, ref index);
if (result == null)
result = value;
else if (!div)
result = (double.parse(result) * double.parse(value)).to_string();
else
result = (double.parse(result) / double.parse(value)).to_string();
if (index >= tokens.length)
return result;
switch (tokens[index]) {
case "*":
div = false;
++index;
continue;
case "/":
div = true;
++index;
continue;
default:
return result;
}
}
}
private string evaluate_expression_plus(string[] tokens, ref uint index) throws Error {
string result = null;
bool minus = false;
for (;;) {
if (index >= tokens.length)
throw error(index, "plus: expected expression");
var value = evaluate_expression_times(tokens, ref index);
if (result == null)
result = value;
else if (!minus)
result = (double.parse(result) + double.parse(value)).to_string();
else
result = (double.parse(result) - double.parse(value)).to_string();
if (index >= tokens.length)
return result;
switch (tokens[index]) {
case "+":
minus = false;
++index;
continue;
case "-":
minus = true;
++index;
continue;
default:
return result;
}
}
}
private string evaluate_expression_parens(string[] tokens, ref uint index) throws Error {
if (index >= tokens.length || tokens[index] != "(")
throw error(index, "parens: expected '('");
++index;
var result = evaluate_expression_plus(tokens, ref index);
if (index >= tokens.length || tokens[index] != ")")
throw error(index, "parens: expected ')'");
++index;
return result;
}
private string[] evaluate_expression_params(string[] tokens, ref uint index) throws Error {
string[] result = null;
if (index >= tokens.length || tokens[index] != "(")
throw error(index, "params: expected '('");
++index;
for (;;) {
result += evaluate_expression_plus(tokens, ref index);
if (index >= tokens.length)
throw error(index, "params: expected ')'");
if (tokens[index] != ",")
break;
++index;
}
if (index >= tokens.length || tokens[index] != ")")
throw error(index, "params: expected ')'");
++index;
return result;
}
private string evaluate_expression_name(string[] tokens, ref uint index) throws Error {
if (index >= tokens.length)
throw error(index, "name: expected identifier");
double sign = 1;
if (tokens[index] == "+") {
++index;
if (index >= tokens.length)
throw error(index, "name: expected identifier");
} else if (tokens[index] == "-") {
sign = -1.0;
++index;
if (index >= tokens.length)
throw error(index, "name: expected identifier");
}
var token = tokens[index];
if (token.length > 0 && (token[0] >= '0' && token[0] <= '9' || token[0] == '.')) {
++index;
if (sign == -1)
return "-" + token;
return token;
}
var varparts = token.split(".");
var nameindex = index;
++index;
switch (varparts.length) {
case 1:
var function = varparts[0];
var parameters = evaluate_expression_params(tokens, ref index);
switch (function) {
case "decimals":
if (parameters.length != 2)
throw error(index, "name: two parameters expected");
return "%.*f".printf(int.parse(parameters[1]), sign * double.parse(parameters[0]));
case "size":
if (parameters.length != 1)
throw error(index, "name: one parameter expected");
return Utils.format_size(sign * double.parse(parameters[0]));
case "speed":
if (parameters.length != 1)
throw error(index, "name: one parameter expected");
return Utils.format_speed(sign * double.parse(parameters[0]));
case "percent":
if (parameters.length != 1)
throw error(index, "name: one parameter expected");
return _("%u%%").printf
((uint)Math.round(100 * sign * double.parse(parameters[0])));
default:
throw error(nameindex, "name: unknown function");
}
case 2:
foreach (var data in this.datas) {
if (data.id != varparts[0])
continue;
for (uint j = 0, jsize = data.keys.length; j < jsize; ++j) {
if (data.keys[j] != varparts[1])
continue;
return (sign * data.values[j]).to_string();
}
}
throw error(nameindex, "name: unknown variable");
default:
throw error(nameindex, "name: too many identifier parts");
}
}
private string evaluate_text(string[] tokens, ref uint index) throws Error {
string[] result = null;
while (index < tokens.length) {
string current = tokens[index];
if (current == "$") {
++index;
result += evaluate_expression(tokens, ref index);
} else {
result += current;
++index;
}
}
return string.joinv("", result);
}
public string evaluate(string[] tokens) {
uint index = 0;
try {
return evaluate_text(tokens, ref index);
} catch (Error e) {
stderr.printf("Expression error at token %i: %s\n", e.code, e.message);
foreach (var token in tokens)
stderr.printf(" '%s'", token);
stderr.printf("\n");
return "";
}
}
}
|