-
Notifications
You must be signed in to change notification settings - Fork 2
/
script_module.cpp
executable file
·449 lines (419 loc) · 13.4 KB
/
script_module.cpp
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* Copyright (C) 2000, 2013 Thierry Crozat
*
* 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, see <http://www.gnu.org/licenses/>.
*
* Contact: criezy01@gmail.com
*/
#include "script_parser.h"
#include "redirect_output.h"
#include "modules.h"
#include "map.h"
#include <math.h>
#include <ctype.h>
#include <stdio.h>
void printScriptModuleHelp(int mode) {
switch (mode) {
case 1:
case 2:
case 3:
printEquationModuleHelp(mode);
break;
case 4:
printf("A script consists of one or more mathematical expressions separated\n");
printf("by a ';'. It can also contain conditional statements to control the\n");
printf("flow of the execution. You can also use variables.\n");
printf("Example:\n");
printf(" variable1 = variable2 * 1.56325 + 17.4;\n");
printf(" if (variable1 >= 0) {\n");
printf(" variable1 = sqrt(variable1);\n");
printf(" }\n");
printf(" // This is a valid comment.\n");
printf(" # This is another valid comment.\n");
printf(" variable4 = 0;\n");
printf(" if (variable1 > 1) {\n");
printf(" variable3 = 1 / sqrt(variable1 - 1);\n");
printf(" variable4 = variable3 + 1;\n");
printf(" } else if (variable1 > 0) {\n");
printf(" variable3 = 1 / sqrt(variable1);\n");
printf(" variable4 = variable3 - 1;\n");
printf(" } else {\n");
printf(" if (variable2 != 0) {\n");
printf(" variable3 = 1 / variable2;\n");
printf(" }\n");
printf(" }\n");
printf(" while (variable4 != 0.0) {\n");
printf(" variable3 = sqrt(variable3);\n");
printf(" variable4 = variable3 - 1;\n");
printf(" if (variable4 < 0) {\n");
printf(" }\n");
printf(" }\n");
break;
default:
printf("Evaluates C-like script.\n");
printf("The following commands are recognized:\n");
printf(" - 'start [name]' Start defining a script. Optionaly a name can be given for the script.\n");
printf(" Type 'end' to finish the script definition. The script will consists of\n");
printf(" everything you have typed between 'start' and 'end'.\n");
printf(" - 'scripts' List all the defined scripts.\n");
printf(" - 'clear [name]' Clear the script with the given name.\n");
printf(" - 'script [name]' Print the previously defined script with the given name.\n");
printf(" - 'script [name] < file' Initialise the script with the given name using the content of the\n");
printf(" given file.\n");
printf(" - 'script [name] > file' Save the script with the given name to the given file\n");
printf(" - 'variables' Print the list of variables in the previously defined scripts.\n");
printf(" - 'run [name]' Run the previously defined script with the given name.\n");
printf(" - 'run [name] > file' Run the previously defined script with the given name and redirect.\n");
printf(" output to file.\n");
#ifdef PARSER_TREE_DEBUG
printf(" - 'tree [name]' Display the parser tree for the previously defined script with the given.\n");
printf(" name. This can be useful to debug issues with the parser.\n");
printf(" - 'tree [name] > file' Print the parser tree to the specified file.\n");
#endif
printf(" - 'help [topic]' Print this help or help on a specific topic. Topics are:\n");
printf(" 'constants', 'functions', 'operators' and 'script'.\n");
printf(" - 'quit' or 'exit' Quit the program.\n");
break;
}
}
void removeScript(
const String& name,
Map<String, String>& scripts,
StringList& variables,
double*& var_values
) {
// Remove this script
if (!scripts.remove(name))
return;
// Update list of variables
const StringList& names = scripts.keys();
StringList new_vars;
ScriptParser parser;
for (int i = 0 ; i < names.size() ; ++i) {
StringList script_vars = parser.getVariablesList(scripts[names[i]]);
for (int v = 0 ; v < script_vars.size() ; ++v) {
if (!new_vars.contains(script_vars[v]))
new_vars << script_vars[v];
}
}
if (new_vars.size() == variables.size())
return; // No variables was removed
// Update value array
double* new_values = NULL;
if (!new_vars.isEmpty()) {
new_values = new double[new_vars.size()];
for (int i = 0 ; i < new_vars.size() ; ++i) {
int index = variables.indexOf(new_vars[i]);
if (index == -1)
new_values[i] = 0.0;
else
new_values[i] = var_values[index];
}
}
delete [] var_values;
var_values = new_values;
variables = new_vars;
}
void addScript(
const String& script,
const String& name,
Map<String, String>& scripts,
StringList& variables,
double*& var_values
) {
if (script.isEmpty()) {
removeScript(name, scripts, variables, var_values);
return;
}
// Check the script is valid
ScriptParser parser;
StringList new_vars = parser.getVariablesList(script);
if (parser.nbErrors() > 0) {
printf("The script contains %d error(s):\n", parser.nbErrors());
for (int error = 0 ; error < parser.nbErrors() ; ++error)
printf(" %d: %s\n", error+1, parser.getError(error).c_str());
removeScript(name, scripts, variables, var_values);
return;
}
// Update list of variables
scripts.remove(name);
const StringList& names = scripts.keys();
for (int i = 0 ; i < names.size() ; ++i) {
StringList script_vars = parser.getVariablesList(scripts[names[i]]);
for (int v = 0 ; v < script_vars.size() ; ++v) {
if (!new_vars.contains(script_vars[v]))
new_vars << script_vars[v];
}
}
scripts[name] = script;
// Update value array
double* new_values = NULL;
if (!new_vars.isEmpty()) {
new_values = new double[new_vars.size()];
for (int i = 0 ; i < new_vars.size() ; ++i) {
int index = variables.indexOf(new_vars[i]);
if (index == -1)
new_values[i] = 0.0;
else
new_values[i] = var_values[index];
}
}
delete [] var_values;
var_values = new_values;
variables = new_vars;
}
String breakLine(const String& line, String& argument, String& input_file, String& output_file) {
String cmd = line.trimmed();
argument.clear();
input_file.clear();
output_file.clear();
if (cmd.isEmpty())
return cmd;
// Check if we have an input file
int index = cmd.findChar('<');
if (index != -1) {
input_file = cmd.right(index + 1).trimmed();
cmd = cmd.left(index - 1).trimmed();
}
// Check if we have an output file
index = cmd.findChar('>');
if (index != -1) {
output_file = cmd.right(index + 1).trimmed();
cmd = cmd.left(index - 1).trimmed();
} else {
index = input_file.findChar('>');
if (index != -1) {
output_file = input_file.right(index + 1).trimmed();
input_file = input_file.left(index - 1).trimmed();
}
}
// break what remains of the command between a command and an argument if there is a space
index = 1;
while (index < cmd.length() && !isspace(cmd[index]))
++ index;
if (index < cmd.length()) {
argument = cmd.right(index+1).trimmed();
cmd = cmd.left(index-1);
}
return cmd;
}
void runScriptModule(const String& s) {
Map<String, String> scripts;
ScriptParser parser;
StringList variables;
String cur_script, cur_name, input_file, output_file;
double* var_values = NULL;
if (!s.isEmpty())
addScript(s, String(), scripts, variables, var_values);
bool script_edition = false;
printf("Starting script mode.\nType 'help' to get some help.\n");
while (1) {
// Script edition
if (script_edition) {
String line = readLine(NULL, false);
if (line == "end\n") {
script_edition = false;
addScript(cur_script, cur_name, scripts, variables, var_values);
} else
cur_script += line;
continue;
}
// Read a line
String line = readLine("> ");
String cmd = breakLine(line, cur_name, input_file, output_file);
if (cmd.isEmpty())
continue;
// Check if it is exit
if (cmd == "exit" || cmd == "quit")
break;
// Check if it is help
if (cmd == "help") {
int t = 0;
if (cur_name == "constants")
t = 1;
else if (cur_name == "functions")
t = 2;
else if (cur_name == "operators")
t = 3;
else if (cur_name == "script")
t = 4;
printScriptModuleHelp(t);
continue;
}
// start
if (cmd == "start") {
script_edition = true;
cur_script.clear();
continue;
}
// scripts
if (cmd == "scripts") {
if (scripts.isEmpty()) {
printf("No script is currently defined.\n");
printf("Type 'start [name]' to define a script and 'end' when you have finished.\n");
} else {
const StringList& names = scripts.keys();
printf("There are %d scripts defined:\n", names.size());
for (int i = 0 ; i < names.size() ; ++i)
printf(" %s\n", names[i].c_str());
}
continue;
}
// clear
if (cmd == "clear") {
removeScript(cur_name, scripts, variables, var_values);
continue;
}
// script
if (cmd == "script") {
if (!input_file.isEmpty()) {
FILE* file = fopen(input_file.c_str(), "r");
if (file == NULL)
printf("Cannot open file '%s'\n", input_file.c_str());
else {
char buffer[256];
cur_script.clear();
while (fgets(buffer, 256, file) != NULL)
cur_script += buffer;
fclose(file);
addScript(cur_script, cur_name, scripts, variables, var_values);
}
}
if (!scripts.contains(cur_name)) {
printf("The script '%s' is not defined.\n", cur_name.c_str());
printf("Type 'scripts' to get a list of defined scripts.\n");
} else {
if (!output_file.isEmpty()) {
FILE* file = fopen(output_file.c_str(), "w");
if (file == NULL)
printf("Failed to save script to file '%s'.\n", output_file.c_str());
else {
fprintf(file, "%s", scripts[cur_name].c_str());
fclose(file);
}
} else
printf("%s\n", scripts[cur_name].c_str());
}
continue;
}
// tree
if (cmd == "tree") {
#ifdef PARSER_TREE_DEBUG
if (!scripts.contains(cur_name)) {
printf("The script '%s' is not defined.\n", cur_name.c_str());
printf("Type 'scripts' to get a list of defined scripts.\n");
} else {
parser.parse(scripts[cur_name], variables);
bool redirected = false;
if (!output_file.isEmpty())
redirected = redirect_output(output_file);
EquationParser::debugPrint(parser.getParserTreeDescription());
if (redirected)
close_redirect_output();
}
#else
printf("The 'tree' command is not available as it was not enabled when compiling this executable.\n");
#endif
continue;
}
// variables
if (cmd == "variables") {
if (scripts.isEmpty()) {
printf("No script is currently defined.\n");
printf("Type 'start [name]' to define a script and 'end' when you have finished.\n");
} else {
for (int i = 0 ; i < variables.size() ; ++i)
printf("%s = %.12g\n", variables[i].c_str(), var_values[i]);
}
continue;
}
// run
if (cur_name.isEmpty() && scripts.contains(cmd)) {
// Only the name of a script was typed. Run it.
cur_name = cmd;
cmd = "run";
}
if (cmd == "run") {
if (!scripts.contains(cur_name)) {
printf("The script '%s' is not defined.\n", cur_name.c_str());
printf("Type 'scripts' to get a list of defined scripts.\n");
} else {
bool redirected = false;
if (!output_file.isEmpty())
redirected = redirect_output(output_file);
parser.parse(scripts[cur_name], variables);
if (!input_file.isEmpty()) {
FILE* var_file = fopen(input_file.c_str(), "r");
if (var_file == NULL)
printf("Cannot open file %s\n", input_file.c_str());
else {
String line = readLine(true, var_file).trimmed();
List<int> column_mapping;
while (!line.isEmpty()) {
int space_i = line.findSpace();
String var;
if (space_i == -1) {
var = line;
line.clear();
} else {
var = line.left(space_i - 1);
line = line.right(space_i + 1).trimmed();
}
column_mapping << variables.indexOf(var);
if (column_mapping.last() == -1)
printf("Warning: variable %s ignored as it is not used in any script.\n", var.c_str());
}
while (!feof(var_file)) {
String line = readLine(true, var_file).trimmed();
int var_i = 0;
while (!line.isEmpty()) {
int space_i = line.findSpace();
String value;
if (space_i == -1) {
value = line;
line.clear();
} else {
value = line.left(space_i - 1);
line = line.right(space_i + 1).trimmed();
}
if (column_mapping[var_i] != -1)
sscanf(value.c_str(), "%lf", var_values + column_mapping[var_i]);
++var_i;
}
parser.evaluate(var_values);
}
fclose(var_file);
}
} else
parser.evaluate(var_values);
if (redirected)
close_redirect_output();
}
continue;
}
// Treat it as a one-line script
if (parser.parse(line, variables))
parser.evaluate(var_values);
else {
if (parser.nbErrors() == 0)
printf("Syntax error...\n");
else {
printf("Equation contains %d error(s):\n", parser.nbErrors());
for (int error = 0 ; error < parser.nbErrors() ; ++error)
printf(" %d: %s\n", error+1, parser.getError(error).c_str());
}
}
}
delete [] var_values;
}