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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* Copyright (C) 2004-2008 Sami Pietila
* Copyright (C) 2008-2011 Robert Ancell.
*
* 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 2 of the License, or (at your option) any later
* version. See http://www.gnu.org/copyleft/gpl.html the full text of the
* license.
*/
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "parser.h"
static int
variable_is_defined(ParserState *state, const char *name)
{
/* FIXME: Make more generic */
if (strcmp(name, "e") == 0 ||
strcmp(name, "i") == 0 ||
strcmp(name, "π") == 0 ||
strcmp(name, "pi") == 0 ||
strcmp(name, "c₀") == 0 ||
strcmp(name, "μ₀") == 0 ||
strcmp(name, "ε₀") == 0 ||
strcmp(name, "G") == 0 ||
strcmp(name, "h") == 0 ||
strcmp(name, "e") == 0 ||
strcmp(name, "mₑ") == 0 ||
strcmp(name, "mₚ") == 0 ||
strcmp(name, "Nₐ") == 0)
return 1;
if (state->options->variable_is_defined)
return state->options->variable_is_defined(name, state->options->callback_data);
return 0;
}
static int
get_variable(ParserState *state, const char *name, MPNumber *z)
{
int result = 1;
if (strcmp(name, "e") == 0)
mp_get_eulers(z);
else if (strcmp(name, "i") == 0)
mp_get_i(z);
else if (strcmp(name, "π") == 0 || strcmp(name, "pi") == 0)
mp_get_pi(z);
else if (strcmp(name, "c₀") == 0)
mp_set_from_string("299792458", 10, z); /* velocity of light */
else if (strcmp(name, "μ₀") == 0)
mp_set_from_string("0.0000012566370614", 10, z); /* magnetic constant */
else if (strcmp(name, "ε₀") == 0)
mp_set_from_string("0.00000000000885418782", 10, z); /* electric constant */
else if (strcmp(name, "G") == 0)
mp_set_from_string("0.0000000000667408", 10, z); /* Newtonian constant of gravitation */
else if (strcmp(name, "h") == 0)
mp_set_from_string("0.000000000000000000000000000000000662607004", 10, z); /* Planck constant */
else if (strcmp(name, "e") == 0)
mp_set_from_string("0.00000000000000000016021766208", 10, z); /* elementary charge */
else if (strcmp(name, "mₑ") == 0)
mp_set_from_string("0.000000000000000000000000000000910938356", 10, z); /* electron mass */
else if (strcmp(name, "mₚ") == 0)
mp_set_from_string("0.000000000000000000000000001672621898", 10, z); /* proton mass */
else if (strcmp(name, "Nₐ") == 0)
mp_set_from_string("602214086000000000000000", 10, z); /* Avogrado constant */
else if (state->options->get_variable)
result = state->options->get_variable(name, z, state->options->callback_data);
else
result = 0;
return result;
}
static void
set_variable(ParserState *state, const char *name, const MPNumber *x)
{
// Reserved words, e, π, mod, and, or, xor, not, abs, log, ln, sqrt, int, frac, sin, cos, ...
if (strcmp(name, "e") == 0 ||
strcmp(name, "i") == 0 ||
strcmp(name, "π") == 0 ||
strcmp(name, "pi") == 0 ||
strcmp(name, "c₀") == 0 ||
strcmp(name, "μ₀") == 0 ||
strcmp(name, "ε₀") == 0 ||
strcmp(name, "G") == 0 ||
strcmp(name, "h") == 0 ||
strcmp(name, "e") == 0 ||
strcmp(name, "mₑ") == 0 ||
strcmp(name, "mₚ") == 0 ||
strcmp(name, "Nₐ") == 0)
return; // FALSE
if (state->options->set_variable)
state->options->set_variable(name, x, state->options->callback_data);
}
// FIXME: Accept "2sin" not "2 sin", i.e. let the tokenizer collect the multiple
// Parser then distinguishes between "sin"="s*i*n" or "sin5" = "sin 5" = "sin(5)"
// i.e. numbers+letters = variable or function depending on following arg
// letters+numbers = numbers+letters+numbers = function
int
sub_atoi(const char *data)
{
int i, value = 0;
const char *digits[] = {"₀", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉", NULL};
do {
for(i = 0; digits[i] != NULL && strncmp(data, digits[i], strlen(digits[i])) != 0; i++);
if(digits[i] == NULL)
return -1;
data += strlen(digits[i]);
value = value * 10 + i;
} while(*data != '\0');
return value;
}
int
super_atoi(const char *data)
{
int i, sign = 1, value = 0;
const char *digits[11] = {"⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", NULL};
if(strncmp(data, "⁻", strlen("⁻")) == 0) {
sign = -1;
data += strlen("⁻");
}
do {
for(i = 0; digits[i] != NULL && strncmp(data, digits[i], strlen(digits[i])) != 0; i++);
if(digits[i] == NULL)
return 0;
value = value * 10 + i;
data += strlen(digits[i]);
} while(*data != '\0');
return sign * value;
}
static int
function_is_defined(ParserState *state, const char *name)
{
char *c, *lower_name;
lower_name = strdup(name);
for (c = lower_name; *c; c++)
*c = tolower(*c);
/* FIXME: Make more generic */
if (strcmp(lower_name, "log") == 0 ||
(strncmp(lower_name, "log", 3) == 0 && sub_atoi(lower_name + 3) >= 0) ||
strcmp(lower_name, "ln") == 0 ||
strcmp(lower_name, "sqrt") == 0 ||
strcmp(lower_name, "abs") == 0 ||
strcmp(lower_name, "sgn") == 0 ||
strcmp(lower_name, "arg") == 0 ||
strcmp(lower_name, "conj") == 0 ||
strcmp(lower_name, "int") == 0 ||
strcmp(lower_name, "frac") == 0 ||
strcmp(lower_name, "floor") == 0 ||
strcmp(lower_name, "ceil") == 0 ||
strcmp(lower_name, "round") == 0 ||
strcmp(lower_name, "re") == 0 ||
strcmp(lower_name, "im") == 0 ||
strcmp(lower_name, "sin") == 0 || strcmp(lower_name, "cos") == 0 || strcmp(lower_name, "tan") == 0 ||
strcmp(lower_name, "asin") == 0 || strcmp(lower_name, "acos") == 0 || strcmp(lower_name, "atan") == 0 ||
strcmp(lower_name, "sin⁻¹") == 0 || strcmp(lower_name, "cos⁻¹") == 0 || strcmp(lower_name, "tan⁻¹") == 0 ||
strcmp(lower_name, "sinh") == 0 || strcmp(lower_name, "cosh") == 0 || strcmp(lower_name, "tanh") == 0 ||
strcmp(lower_name, "sinh⁻¹") == 0 || strcmp(lower_name, "cosh⁻¹") == 0 || strcmp(lower_name, "tanh⁻¹") == 0 ||
strcmp(lower_name, "asinh") == 0 || strcmp(lower_name, "acosh") == 0 || strcmp(lower_name, "atanh") == 0 ||
strcmp(lower_name, "ones") == 0 ||
strcmp(lower_name, "twos") == 0) {
g_free (lower_name);
return 1;
}
g_free (lower_name);
if (state->options->function_is_defined)
return state->options->function_is_defined(name, state->options->callback_data);
return 0;
}
static int
get_function(ParserState *state, const char *name, const MPNumber *x, MPNumber *z)
{
char *c, *lower_name;
int result = 1;
lower_name = strdup(name);
for (c = lower_name; *c; c++)
*c = tolower(*c);
// FIXME: Re Im ?
if (strcmp(lower_name, "log") == 0)
mp_logarithm(10, x, z); // FIXME: Default to ln
else if (strncmp(lower_name, "log", 3) == 0) {
int base;
base = sub_atoi(lower_name + 3);
if (base < 0)
result = 0;
else
mp_logarithm(base, x, z);
}
else if (strcmp(lower_name, "ln") == 0)
mp_ln(x, z);
else if (strcmp(lower_name, "sqrt") == 0) // √x
mp_sqrt(x, z);
else if (strcmp(lower_name, "abs") == 0) // |x|
mp_abs(x, z);
else if (strcmp(lower_name, "sgn") == 0)
mp_sgn(x, z);
else if (strcmp(lower_name, "arg") == 0)
mp_arg(x, state->options->angle_units, z);
else if (strcmp(lower_name, "conj") == 0)
mp_conjugate(x, z);
else if (strcmp(lower_name, "int") == 0)
mp_integer_component(x, z);
else if (strcmp(lower_name, "frac") == 0)
mp_fractional_component(x, z);
else if (strcmp(lower_name, "floor") == 0)
mp_floor(x, z);
else if (strcmp(lower_name, "ceil") == 0)
mp_ceiling(x, z);
else if (strcmp(lower_name, "round") == 0)
mp_round(x, z);
else if (strcmp(lower_name, "re") == 0)
mp_real_component(x, z);
else if (strcmp(lower_name, "im") == 0)
mp_imaginary_component(x, z);
else if (strcmp(lower_name, "sin") == 0)
mp_sin(x, state->options->angle_units, z);
else if (strcmp(lower_name, "cos") == 0)
mp_cos(x, state->options->angle_units, z);
else if (strcmp(lower_name, "tan") == 0)
mp_tan(x, state->options->angle_units, z);
else if (strcmp(lower_name, "sin⁻¹") == 0 || strcmp(lower_name, "asin") == 0)
mp_asin(x, state->options->angle_units, z);
else if (strcmp(lower_name, "cos⁻¹") == 0 || strcmp(lower_name, "acos") == 0)
mp_acos(x, state->options->angle_units, z);
else if (strcmp(lower_name, "tan⁻¹") == 0 || strcmp(lower_name, "atan") == 0)
mp_atan(x, state->options->angle_units, z);
else if (strcmp(lower_name, "sinh") == 0)
mp_sinh(x, z);
else if (strcmp(lower_name, "cosh") == 0)
mp_cosh(x, z);
else if (strcmp(lower_name, "tanh") == 0)
mp_tanh(x, z);
else if (strcmp(lower_name, "sinh⁻¹") == 0 || strcmp(lower_name, "asinh") == 0)
mp_asinh(x, z);
else if (strcmp(lower_name, "cosh⁻¹") == 0 || strcmp(lower_name, "acosh") == 0)
mp_acosh(x, z);
else if (strcmp(lower_name, "tanh⁻¹") == 0 || strcmp(lower_name, "atanh") == 0)
mp_atanh(x, z);
else if (strcmp(lower_name, "ones") == 0)
mp_ones_complement(x, state->options->wordlen, z);
else if (strcmp(lower_name, "twos") == 0)
mp_twos_complement(x, state->options->wordlen, z);
else if (state->options->get_function)
result = state->options->get_function(name, x, z, state->options->callback_data);
else
result = 0;
free(lower_name);
return result;
}
static int
convert(ParserState *state, const MPNumber *x, const char *x_units, const char *z_units, MPNumber *z)
{
if (state->options->convert)
return state->options->convert(x, x_units, z_units, z, state->options->callback_data);
else
return 0;
}
MPErrorCode
mp_equation_parse(const char *expression, MPEquationOptions *options, MPNumber *result, char **error_token)
{
int ret;
int err;
ParserState* state;
state = p_create_parser (expression, options);
if (!(expression && result) || strlen(expression) == 0)
return PARSER_ERR_INVALID;
state->variable_is_defined = variable_is_defined;
state->get_variable = get_variable;
state->set_variable = set_variable;
state->function_is_defined = function_is_defined;
state->get_function = get_function;
state->convert = convert;
state->error = 0;
mp_clear_error();
ret = p_parse (state);
if (state->error_token != NULL && error_token != NULL) {
*error_token = state->error_token;
}
/* Error during parsing */
if (state->error) {
err = state->error;
p_destroy_parser (state);
return err;
}
if (mp_get_error()) {
p_destroy_parser (state);
return PARSER_ERR_MP;
}
/* Failed to parse */
if (ret) {
p_destroy_parser (state);
return PARSER_ERR_INVALID;
}
mp_set_from_mp(&state->ret, result);
p_destroy_parser (state);
return PARSER_ERR_NONE;
}
const char *
mp_error_code_to_string(MPErrorCode error_code)
{
switch(error_code)
{
case PARSER_ERR_NONE:
return "PARSER_ERR_NONE";
case PARSER_ERR_INVALID:
return "PARSER_ERR_INVALID";
case PARSER_ERR_OVERFLOW:
return "PARSER_ERR_OVERFLOW";
case PARSER_ERR_UNKNOWN_VARIABLE:
return "PARSER_ERR_UNKNOWN_VARIABLE";
case PARSER_ERR_UNKNOWN_FUNCTION:
return "PARSER_ERR_UNKNOWN_FUNCTION";
case PARSER_ERR_UNKNOWN_CONVERSION:
return "PARSER_ERR_UNKNOWN_CONVERSION";
case PARSER_ERR_MP:
return "PARSER_ERR_MP";
default:
return "Unknown parser error";
}
}
|