-
Notifications
You must be signed in to change notification settings - Fork 11
/
exprtk_repl.cpp
1425 lines (1132 loc) · 37.8 KB
/
exprtk_repl.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* ExprTk REPL (Read Evaluate Print Loop) Interface *
* Author: Arash Partow (1999-2024) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* SPDX-License-Identifier: MIT *
* *
**************************************************************
*/
#include <algorithm>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <numeric>
#include <string>
#include "exprtk.hpp"
template <typename T>
struct putch : public exprtk::ifunction<T>
{
using exprtk::ifunction<T>::operator();
putch() : exprtk::ifunction<T>(1) {}
inline T operator()(const T& v)
{
printf("%c",static_cast<int>(v));
return T(0);
}
};
template <typename T>
struct putint : public exprtk::ifunction<T>
{
using exprtk::ifunction<T>::operator();
putint() : exprtk::ifunction<T>(1) {}
inline T operator()(const T& v)
{
printf("%d",static_cast<int>(v));
return T(0);
}
};
template <typename T>
struct rnd_01 : public exprtk::ifunction<T>
{
using exprtk::ifunction<T>::operator();
rnd_01() : exprtk::ifunction<T>(0)
{ ::srand(static_cast<unsigned int>(time(NULL))); }
inline T operator()()
{
// Note: Do not use this in production
// Result is in the interval [0,1)
return T(::rand() / T(RAND_MAX + 1.0));
}
};
template <typename T>
class expression_processor
{
public:
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
typedef exprtk::parser_error::type error_t;
typedef exprtk::function_compositor<T> compositor_t;
typedef typename compositor_t::function function_t;
typedef typename parser_t::settings_store settings_store_t;
typedef exprtk::lexer::parser_helper prsrhlpr_t;
typedef typename parser_t::dependent_entity_collector::symbol_t symbol_t;
typedef std::vector<symbol_t> symbol_list_t;
expression_processor()
: persist_symbol_table_ (false)
, symbol_dump_ (false)
, assignment_dump_ (false)
, display_total_time_ (false)
, display_total_compile_time_(false)
, enable_usr_ (false)
, disable_local_vardef_ (false)
, batch_runs_cnt_ (0 )
, compositor_(function_symbol_table_)
#ifdef exprtk_enable_repl_variables
, s0_("abcdefghijk")
, s1_("abcdefghijk0123456789")
, s2_("012345678901234567890123456789")
, v0_({ 1, 1, 1 })
, v1_({ 2, 2, 2, 2, 2})
, v2_({ 3, 3, 3, 3, 3, 3, 3})
, v3_({ 4, 4, 4, 4, 4, 4, 4, 4})
, vv_(exprtk::make_vector_view(v2_,v2_.size()))
#endif
{
symbol_table_.add_constants();
symbol_table_.add_function("putch" , putch_ );
symbol_table_.add_function("putint" , putint_ );
symbol_table_.add_function("rnd_01" , rnd_01_ );
symbol_table_.add_package (fileio_package_ );
symbol_table_.add_package (vecops_package_ );
symbol_table_.add_package (io_package_ );
symbol_table_.add_function("poly01", poly01_);
symbol_table_.add_function("poly02", poly02_);
symbol_table_.add_function("poly03", poly03_);
symbol_table_.add_function("poly04", poly04_);
symbol_table_.add_function("poly05", poly05_);
symbol_table_.add_function("poly06", poly06_);
symbol_table_.add_function("poly07", poly07_);
symbol_table_.add_function("poly08", poly08_);
symbol_table_.add_function("poly09", poly09_);
symbol_table_.add_function("poly10", poly10_);
symbol_table_.add_function("poly11", poly11_);
symbol_table_.add_function("poly12", poly12_);
#ifdef exprtk_enable_repl_variables
symbol_table_.add_stringvar("s0", s0_);
symbol_table_.add_stringvar("s1", s1_);
symbol_table_.add_stringvar("s2", s2_);
symbol_table_.add_vector ("v0", v0_);
symbol_table_.add_vector ("v1", v1_);
symbol_table_.add_vector ("v2", v2_);
symbol_table_.add_vector ("v3", v3_);
vv_ = exprtk::make_vector_view(v2_,v2_.size());
symbol_table_.add_vector ("vv", vv_);
#endif
compositor_.add_auxiliary_symtab(symbol_table_);
arith_opr_["+"] = settings_store_t::e_arith_add;
arith_opr_["-"] = settings_store_t::e_arith_sub;
arith_opr_["*"] = settings_store_t::e_arith_mul;
arith_opr_["/"] = settings_store_t::e_arith_div;
arith_opr_["%"] = settings_store_t::e_arith_mod;
arith_opr_["^"] = settings_store_t::e_arith_pow;
assign_opr_[":="] = settings_store_t::e_assign_assign;
assign_opr_["+="] = settings_store_t::e_assign_addass;
assign_opr_["-="] = settings_store_t::e_assign_subass;
assign_opr_["*="] = settings_store_t::e_assign_mulass;
assign_opr_["/="] = settings_store_t::e_assign_divass;
assign_opr_["%="] = settings_store_t::e_assign_modass;
inequality_opr_[ "<"] = settings_store_t::e_ineq_lt;
inequality_opr_["<="] = settings_store_t::e_ineq_lte;
inequality_opr_["=="] = settings_store_t::e_ineq_eq;
inequality_opr_[ "="] = settings_store_t::e_ineq_equal;
inequality_opr_["!="] = settings_store_t::e_ineq_ne;
inequality_opr_["<>"] = settings_store_t::e_ineq_nequal;
inequality_opr_[">="] = settings_store_t::e_ineq_gte;
inequality_opr_[ ">"] = settings_store_t::e_ineq_gt;
clear_functions();
}
~expression_processor()
{
clear_functions();
}
bool& persist_symbol_table()
{
return persist_symbol_table_;
}
bool& symbol_dump()
{
return symbol_dump_;
}
bool& assignment_dump()
{
return assignment_dump_;
}
bool& display_total_time()
{
return display_total_time_;
}
bool& display_total_compile_time()
{
return display_total_compile_time_;
}
bool& enable_usr()
{
return enable_usr_;
}
bool& disable_local_vardef()
{
return disable_local_vardef_;
}
void setup_symbol_table()
{
if (!persist_symbol_table_)
{
symbol_table_.clear_variables();
symbol_table_.add_constants ();
symbol_table_.add_constant ("e", exprtk::details::numeric::constant::e);
}
}
void process(std::string program)
{
program = trim_whitespace(program);
if (program.empty())
return;
setup_symbol_table();
expression_t expression;
expression.register_symbol_table(symbol_table_);
expression.register_symbol_table(function_symbol_table_);
exprtk::timer compile_timer;
compile_timer.start();
if (enable_usr_)
parser_.enable_unknown_symbol_resolver();
else
parser_.disable_unknown_symbol_resolver();
if (disable_local_vardef_)
parser_.settings().disable_local_vardef();
else
parser_.settings().enable_local_vardef();
parser_.dec().collect_variables() = symbol_dump_;
parser_.dec().collect_functions() = symbol_dump_;
parser_.dec().collect_assignments() = assignment_dump_;
if (!parser_.compile(program,expression))
{
printf("Error: %s\tExpression:%c%s\n",
parser_.error().c_str(),
((std::string::npos != program.find_first_of('\n')) ? '\n' : ' '),
((program.size() < 200) ? program.c_str() : "....."));
for (std::size_t i = 0; i < parser_.error_count(); ++i)
{
error_t error = parser_.get_error(i);
printf("Err No.: %02d Pos: %02d Type: [%14s] Msg: %s\n",
static_cast<unsigned int>(i),
static_cast<unsigned int>(error.token.position),
exprtk::parser_error::to_str(error.mode).c_str(),
error.diagnostic.c_str());
if (
(0 == i) &&
exprtk::parser_error::update_error(error,program)
)
{
printf("Error (line: %d column: %d)\n",
static_cast<unsigned int>(error.line_no),
static_cast<unsigned int>(error.column_no));
printf("%s \n",error.error_line.c_str());
printf("%s^\n",std::string(error.column_no,'~').c_str());
}
}
return;
}
compile_timer.stop();
if (display_total_compile_time_)
{
printf("\nCompile time: %6.3fms\n",compile_timer.time() * 1000.0);
}
if (batch_runs_cnt_)
{
std::vector<double> timings(batch_runs_cnt_,0.0);
exprtk::timer total_timer;
exprtk::timer timer;
T result = T(0);
total_timer.start();
for (std::size_t i = 0; i < batch_runs_cnt_; ++i)
{
timer.start();
result = expression.value();
timer.stop();
timings[i] = timer.time() * 1000.0;
}
total_timer.stop();
printf("\nResult: %15.9f\n",result);
std::sort(timings.begin(),timings.end());
printf("\nRuns: %4d Time min: %7.3fms max: %7.3fms avg: %7.3fms tot: %7.3fms 90%%:%7.3fms\n",
static_cast<unsigned int>(batch_runs_cnt_),
timings.front(),
timings.back (),
std::accumulate(timings.begin(),timings.end(),0.0) / timings.size(),
total_timer.time() * 1000.0,
timings[static_cast<int>(timings.size() * 0.90)]);
return;
}
exprtk::timer timer;
timer.start();
const T result = expression.value();
timer.stop();
if (expression.results().count())
{
print_results(expression.results());
}
printf("\nResult: %15.9f\n",result);
if (display_total_time_)
{
printf("\nTotal time: %6.3fms\n",timer.time() * 1000.0);
}
if (symbol_dump_)
{
symbol_list_t symbol_list;
parser_.dec().symbols(symbol_list);
printf("------ Symbols ------\n");
perform_symbol_dump(symbol_list);
printf("---------------------\n");
}
if (assignment_dump_)
{
symbol_list_t assignment_list;
parser_.dec().assignment_symbols(assignment_list);
printf("---- Assignments ----\n");
perform_symbol_dump(assignment_list);
printf("---------------------\n");
}
}
void process_from_file(const std::string& file_name)
{
if (file_name.empty())
return;
std::ifstream stream(file_name.c_str());
if (!stream)
{
printf("ERROR: Failed to open file: %s\n\n",file_name.c_str());
return;
}
std::string program(
(std::istreambuf_iterator<char>(stream)),
(std::istreambuf_iterator<char>())
);
process_function_definition(program,false);
}
void process_directive(std::string expression)
{
expression = trim_whitespace(expression);
if ('$' != expression[0])
return;
else if ("$enable_cache" == expression)
persist_symbol_table() = true;
else if ("$disable_cache" == expression)
persist_symbol_table() = false;
else if ("$enable_symbol_dump" == expression)
symbol_dump() = true;
else if ("$disable_symbol_dump" == expression)
symbol_dump() = false;
else if ("$enable_assignment_dump" == expression)
assignment_dump() = true;
else if ("$disable_assignment_dump" == expression)
assignment_dump() = false;
else if ("$enable_timer" == expression)
display_total_time() = true;
else if ("$enable_compile_timer" == expression)
display_total_compile_time() = true;
else if ("$disable_timer" == expression)
display_total_time() = false;
else if ("$disable_compile_timer" == expression)
display_total_compile_time() = false;
else if ("$enable_usr" == expression)
enable_usr() = true;
else if ("$disable_usr" == expression)
enable_usr() = false;
else if ("$enable_local_vardef" == expression)
disable_local_vardef() = false;
else if ("$disable_local_vardef" == expression)
disable_local_vardef() = true;
else if ("$list_vars" == expression)
list_symbols();
else if ("$clear_functions" == expression)
clear_functions();
else if ((0 == expression.find("$batch_run ")) && (expression.size() >= 12))
process_batch_run(expression.substr(11,expression.size() - 11));
else if ((0 == expression.find("$load ")) && (expression.size() > 7))
process_from_file(expression.substr(6,expression.size() - 6));
else if ((0 == expression.find("$disable arithmetic ")) && (expression.size() >= 21))
process_disable_arithmetic(expression.substr(20,expression.size() - 20));
else if ((0 == expression.find("$disable assignment ")) && (expression.size() >= 21))
process_disable_assignment(expression.substr(20,expression.size() - 20));
else if ((0 == expression.find("$disable inequality ")) && (expression.size() >= 21))
process_disable_inequality(expression.substr(20,expression.size() - 20));
else if ((0 == expression.find("$enable arithmetic ")) && (expression.size() >= 20))
process_enable_arithmetic(expression.substr(19,expression.size() - 19));
else if ((0 == expression.find("$enable assignment ")) && (expression.size() >= 20))
process_enable_assignment(expression.substr(19,expression.size() - 19));
else if ((0 == expression.find("$enable inequality ")) && (expression.size() >= 20))
process_enable_inequality(expression.substr(19,expression.size() - 19));
else if ("$begin" == expression)
process_multiline();
else if (0 == expression.find("$function"))
process_function_definition(expression);
else
printf("\nERROR - Invalid directive: %s\n",expression.c_str());
}
private:
void print_results(const exprtk::results_context<T>& results)
{
typedef exprtk::results_context<T> results_context_t;
typedef typename results_context_t::type_store_t type_t;
typedef typename type_t::scalar_view scalar_t;
typedef typename type_t::vector_view vector_t;
typedef typename type_t::string_view string_t;
typename exprtk::details::numeric::details::number_type<T>::type num_type;
printf("%s\n",std::string(10,'-').c_str());
printf("Return Results (#%d)\n",static_cast<int>(results.count()));
for (std::size_t i = 0; i < results.count(); ++i)
{
printf("[%02d] ",static_cast<int>(i));
type_t t = results[i];
switch (t.type)
{
case type_t::e_scalar : printf("Scalar\t");
exprtk::rtl::io::details::print_type("%10.5f",scalar_t(t)(),num_type);
break;
case type_t::e_vector : {
printf("Vector\t");
vector_t vector(t);
for (std::size_t x = 0; x < vector.size(); ++x)
{
exprtk::rtl::io::details::print_type("%10.5f",vector[x],num_type);
if ((x + 1) < vector.size())
printf(" ");
}
}
break;
case type_t::e_string : printf("String\t");
printf("%s",to_str(string_t(t)).c_str());
break;
default : continue;
}
printf("\n");
}
printf("%s\n",std::string(10,'-').c_str());
}
void perform_symbol_dump(const symbol_list_t& variable_list) const
{
for (std::size_t i = 0; i < variable_list.size(); ++i)
{
const symbol_t& symbol = variable_list[i];
switch (symbol.second)
{
case parser_t::e_st_variable : printf("[%02d] Variable %s\n",
static_cast<int>(i),symbol.first.c_str());
break;
case parser_t::e_st_vector : printf("[%02d] Vector %s\n",
static_cast<int>(i),symbol.first.c_str());
break;
case parser_t::e_st_string : printf("[%02d] String %s\n",
static_cast<int>(i),symbol.first.c_str());
break;
case parser_t::e_st_function : printf("[%02d] Function %s\n",
static_cast<int>(i),symbol.first.c_str());
break;
case parser_t::e_st_local_variable
: printf("[%02d] LocalVar %s\n",
static_cast<int>(i),symbol.first.c_str());
break;
case parser_t::e_st_local_vector
: printf("[%02d] LocalVec %s\n",
static_cast<int>(i),symbol.first.c_str());
break;
case parser_t::e_st_local_string
: printf("[%02d] LocalStr %s\n",
static_cast<int>(i),symbol.first.c_str());
break;
default : break;
}
}
}
void process_batch_run(const std::string& batch_runs_cnt)
{
batch_runs_cnt_ = atoi(batch_runs_cnt.c_str());
}
void process_multiline()
{
std::string program;
for ( ; ; )
{
std::string line;
std::cout << ">> ";
std::getline(std::cin,line);
line = trim_whitespace(line);
if (line.empty())
continue;
else if ("$end" == line)
break;
else
program += (line + "\n");
}
process(program);
}
struct function_definition
{
std::string name;
std::string body;
std::vector<std::string> var_list;
void clear()
{
name .clear();
body .clear();
var_list.clear();
}
};
enum func_parse_result
{
e_parse_unknown = 0,
e_parse_success = 1,
e_parse_partial = 2,
e_parse_lexfail = 4,
e_parse_notfunc = 8
};
struct parse_function_definition_impl : public exprtk::lexer::parser_helper
{
func_parse_result process(std::string& func_def, function_definition& fd)
{
if (!init(func_def))
return e_parse_lexfail;
if (!token_is(token_t::e_symbol,"function"))
return e_parse_notfunc;
if (!token_is(token_t::e_symbol,prsrhlpr_t::e_hold))
return e_parse_partial;
fd.name = current_token().value;
next_token();
if (!token_is(token_t::e_lbracket))
return e_parse_partial;
if (!token_is(token_t::e_rbracket))
{
std::vector<std::string> var_list;
for ( ; ; )
{
// (x,y,z,....w)
if (!token_is(token_t::e_symbol,prsrhlpr_t::e_hold))
return e_parse_partial;
var_list.push_back(current_token().value);
next_token();
if (token_is(token_t::e_rbracket))
break;
if (!token_is(token_t::e_comma))
return e_parse_partial;
}
var_list.swap(fd.var_list);
}
const std::size_t body_begin = current_token().position;
std::size_t body_end = current_token().position;
int bracket_stack = 0;
if (!token_is(token_t::e_lcrlbracket,prsrhlpr_t::e_hold))
return e_parse_partial;
for ( ; ; )
{
body_end = current_token().position;
if (token_is(token_t::e_lcrlbracket))
bracket_stack++;
else if (token_is(token_t::e_rcrlbracket))
{
if (0 == --bracket_stack)
break;
}
else
{
if (lexer().finished())
return e_parse_partial;
next_token();
}
}
const std::size_t size = body_end - body_begin + 1;
fd.body = func_def.substr(body_begin,size);
const std::size_t index = body_begin + size;
if (index < func_def.size())
func_def = func_def.substr(index,func_def.size() - index);
else
func_def = "";
return e_parse_success;
}
};
func_parse_result parse_function_definition(std::string& func_def, function_definition& cf)
{
parse_function_definition_impl parser;
return parser.process(func_def,cf);
}
std::string read_from_stdin()
{
std::string input;
for ( ; ; )
{
std::string line;
std::cout << ">> ";
std::getline(std::cin,line);
if (line.empty())
continue;
else if ("$end" == line)
break;
else
input += (line + "\n");
}
if (!input.empty())
input.erase(input.end() - 1);
return input;
}
void process_function_definition(const std::string& func_def_header, bool read_stdin = true)
{
std::string func_def = func_def_header;
if (read_stdin)
{
func_def += read_from_stdin();
if (!func_def.empty() && ('$' == func_def[0]))
func_def.erase(func_def.begin());
}
do
{
function_definition fd;
func_parse_result fp_result = parse_function_definition(func_def,fd);
if (e_parse_success == fp_result)
{
std::string vars;
for (std::size_t i = 0; i < fd.var_list.size(); ++i)
{
vars += fd.var_list[i] + ((i < fd.var_list.size() - 1) ? "," : "");
}
function_t f(fd.name);
for (std::size_t i = 0; i < fd.var_list.size(); ++i)
{
f.var(fd.var_list[i]);
}
f.expression(fd.body);
if (function_symbol_table_.get_function(fd.name))
{
function_symbol_table_.remove_function(fd.name);
for (std::size_t i = 0; i < func_def_list_.size(); ++i)
{
if (exprtk::details::imatch(fd.name, func_def_list_[i].name))
{
func_def_list_.erase(func_def_list_.begin() + i);
break;
}
}
}
if (!compositor_.add(f,true))
{
function_symbol_table_.remove_function(fd.name);
printf("Error - Failed to add function: %s\n",fd.name.c_str());
return;
}
printf("Function[%02d]\n",static_cast<int>(func_def_list_.size()));
printf("Name: %s \n",fd.name.c_str() );
printf("Vars: (%s) \n",vars.c_str() );
printf("------------------------------------------------------\n");
func_def_list_.push_back(fd);
}
else if (e_parse_notfunc != fp_result)
{
printf("Error - Critical parsing error - partial parse occurred\n");
return;
}
else
break;
}
while (!func_def.empty());
if (!func_def.empty())
{
process(func_def);
}
}
void list_symbols()
{
std::deque<std::pair<std::string,T> > variable_list;
symbol_table_.get_variable_list(variable_list);
std::size_t max_varname_length = 0;
for (std::size_t i = 0; i < variable_list.size(); ++i)
{
max_varname_length = std::max(max_varname_length,variable_list[i].first.size());
}
for (std::size_t i = 0; i < variable_list.size(); ++i)
{
int pad_length = 0;
if (max_varname_length > variable_list[i].first.size())
{
pad_length = static_cast<int>(max_varname_length - variable_list[i].first.size());
}
printf("%02d %s%*.*s %25.10f\n",
static_cast<unsigned int>(i),
variable_list[i].first.c_str(),
pad_length,
pad_length,
std::string(max_varname_length,' ').c_str(),
variable_list[i].second);
}
}
void clear_functions()
{
func_def_list_.clear();
function_symbol_table_.clear();
}
std::string trim_whitespace(std::string s)
{
static const std::string whitespace(" \n\r\t\b\v\f");
if (!s.empty())
{
s.erase(0,s.find_first_not_of(whitespace));
if (!s.empty())
{
std::size_t index = s.find_last_not_of(whitespace);
if (std::string::npos != index)
s.erase(index + 1);
else
s.clear();
}
}
return s;
}
void process_disable_arithmetic(const std::string& arithmetic)
{
typename std::map<std::string,typename settings_store_t::settings_arithmetic_opr>::iterator itr;
if (arith_opr_.end() != (itr = arith_opr_.find(arithmetic)))
{
parser_.settings()
.disable_arithmetic_operation(itr->second);
}
}
void process_disable_assignment(const std::string& assignment)
{
typename std::map<std::string,typename settings_store_t::settings_assignment_opr>::iterator itr;
if (assign_opr_.end() != (itr = assign_opr_.find(assignment)))
{
parser_.settings()
.disable_assignment_operation(itr->second);
}
}
void process_disable_inequality(const std::string& inequality)
{
typename std::map<std::string,typename settings_store_t::settings_inequality_opr>::iterator itr;
if (inequality_opr_.end() != (itr = inequality_opr_.find(inequality)))
{
parser_.settings()
.disable_inequality_operation(itr->second);
}
}
void process_enable_arithmetic(const std::string& arithmetic)
{
typename std::map<std::string,typename settings_store_t::settings_arithmetic_opr>::iterator itr;
if (arith_opr_.end() != (itr = arith_opr_.find(arithmetic)))
{
parser_.settings()
.enable_arithmetic_operation(itr->second);
}
}
void process_enable_assignment(const std::string& assignment)
{
typename std::map<std::string,typename settings_store_t::settings_assignment_opr>::iterator itr;
if (assign_opr_.end() != (itr = assign_opr_.find(assignment)))
{
parser_.settings()
.enable_assignment_operation(itr->second);
}
}
void process_enable_inequality(const std::string& inequality)
{
typename std::map<std::string,typename settings_store_t::settings_inequality_opr>::iterator itr;
if (inequality_opr_.end() != (itr = inequality_opr_.find(inequality)))
{
parser_.settings()
.enable_inequality_operation(itr->second);
}
}
private:
bool persist_symbol_table_;
bool symbol_dump_;
bool assignment_dump_;
bool display_total_time_;
bool display_total_compile_time_;
bool enable_usr_;
bool disable_local_vardef_;
std::size_t batch_runs_cnt_;
symbol_table_t symbol_table_;
symbol_table_t function_symbol_table_;
parser_t parser_;
compositor_t compositor_;
putch <T> putch_;
putint <T> putint_;
rnd_01 <T> rnd_01_;
exprtk::rtl::io::file::package<T> fileio_package_;
exprtk::rtl::vecops::package<T> vecops_package_;
exprtk::rtl::io::package<T> io_package_;
exprtk::polynomial<T, 1> poly01_;
exprtk::polynomial<T, 2> poly02_;
exprtk::polynomial<T, 3> poly03_;
exprtk::polynomial<T, 4> poly04_;
exprtk::polynomial<T, 5> poly05_;
exprtk::polynomial<T, 6> poly06_;
exprtk::polynomial<T, 7> poly07_;
exprtk::polynomial<T, 8> poly08_;
exprtk::polynomial<T, 9> poly09_;
exprtk::polynomial<T,10> poly10_;
exprtk::polynomial<T,11> poly11_;
exprtk::polynomial<T,12> poly12_;
std::vector<function_definition> func_def_list_;
std::map<std::string,typename settings_store_t::settings_arithmetic_opr> arith_opr_;
std::map<std::string,typename settings_store_t::settings_assignment_opr> assign_opr_;
std::map<std::string,typename settings_store_t::settings_inequality_opr> inequality_opr_;
#ifdef exprtk_enable_repl_variables
std::string s0_;
std::string s1_;
std::string s2_;
std::vector<T> v0_;
std::vector<T> v1_;
std::vector<T> v2_;
std::vector<T> v3_;
exprtk::vector_view<T> vv_;
#endif
};
template <typename T>
void repl(int argc, char* argv[])
{
expression_processor<T> processor;
if (argc > 1)
{
for (int i = 1; i < argc; ++i)