C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_mpfr_repl.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk MPFR REPL Interface *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <algorithm>
21#include <cstdio>
22#include <deque>
23#include <fstream>
24#include <iostream>
25#include <numeric>
26#include <string>
27
28#include <mpreal.h>
30#include "exprtk.hpp"
31
32
33template <typename T>
34struct putch : public exprtk::ifunction<T>
35{
36 using exprtk::ifunction<T>::operator();
37
38 putch() : exprtk::ifunction<T>(1) {}
39
40 inline T operator()(const T& v)
41 {
42 printf("%c",static_cast<int>(v.toLong()));
43 return T(0);
44 }
45};
46
47template <typename T>
48struct putint : public exprtk::ifunction<T>
49{
50 using exprtk::ifunction<T>::operator();
51
52 putint() : exprtk::ifunction<T>(1) {}
53
54 inline T operator()(const T& v)
55 {
56 printf("%d",static_cast<int>(v.toLong()));
57 return T(0);
58 }
59};
60
61template <typename T>
62struct rnd_01 : public exprtk::ifunction<T>
63{
64 using exprtk::ifunction<T>::operator();
65
67 { ::srand(static_cast<unsigned int>(time(NULL))); }
68
69 inline T operator()()
70 {
71 // Note: Do not use this in production
72 // Result is in the interval [0,1)
73 return T(::rand() / T(RAND_MAX + 1.0));
74 }
75};
76
77template <typename T>
79{
80public:
81
90
91 typedef typename parser_t::dependent_entity_collector::symbol_t symbol_t;
92 typedef std::vector<symbol_t> symbol_list_t;
93
95 : persist_symbol_table_ (false),
96 symbol_dump_ (false),
97 assignment_dump_ (false),
98 display_total_time_ (false),
100 enable_usr_ (false),
101 disable_local_vardef_ (false),
102 batch_runs_cnt_ (0),
104 #ifdef exprtk_enable_repl_variables
105 ,s0_("abcdefghijk"),
106 s1_("abcdefghijk0123456789"),
107 s2_("012345678901234567890123456789"),
108 v0_({1,1,1}),
109 v1_({2,2,2,2,2}),
110 v2_({3,3,3,3,3,3,3}),
111 v3_({4,4,4,4,4,4,4,4}),
112 vv_(exprtk::make_vector_view(v2_,v2_.size()))
113 #endif
114 {
116
123
136
137 #ifdef exprtk_enable_repl_variables
141 symbol_table_.add_vector ("v0",v0_);
142 symbol_table_.add_vector ("v1",v1_);
143 symbol_table_.add_vector ("v2",v2_);
144 symbol_table_.add_vector ("v3",v3_);
145
146 vv_ = exprtk::make_vector_view(v2_,v2_.size());
147 symbol_table_.add_vector ("vv",vv_);
148 #endif
149
151
158
165
174
176 }
177
182
184 {
186 }
187
189 {
190 return symbol_dump_;
191 }
192
194 {
195 return assignment_dump_;
196 }
197
199 {
200 return display_total_time_;
201 }
202
207
209 {
210 return enable_usr_;
211 }
212
214 {
216 }
217
227
228 void process(std::string program)
229 {
230 program = trim_whitespace(program);
231
232 if (program.empty())
233 return;
234
236
237 expression_t expression;
240
241 exprtk::timer compile_timer;
242 compile_timer.start();
243
244 if (enable_usr_)
246 else
248
251 else
253
256
258
259 if (!parser_.compile(program,expression))
260 {
261 printf("Error: %s\tExpression:%c%s\n",
262 parser_.error().c_str(),
263 ((std::string::npos != program.find_first_of('\n')) ? '\n' : ' '),
264 ((program.size() < 200) ? program.c_str() : "....."));
265
266 for (std::size_t i = 0; i < parser_.error_count(); ++i)
267 {
268 error_t error = parser_.get_error(i);
269
270 printf("Err No.: %02d Pos: %02d Type: [%14s] Msg: %s\n",
271 static_cast<unsigned int>(i),
272 static_cast<unsigned int>(error.token.position),
273 exprtk::parser_error::to_str(error.mode).c_str(),
274 error.diagnostic.c_str());
275
276 if (
277 (0 == i) &&
279 )
280 {
281 printf("Error (line: %d column: %d)\n",
282 static_cast<unsigned int>(error.line_no),
283 static_cast<unsigned int>(error.column_no));
284
285 printf("%s \n",error.error_line.c_str());
286 printf("%s^\n",std::string(error.column_no,'~').c_str());
287 }
288 }
289
290 return;
291 }
292
293 compile_timer.stop();
294
296 {
297 printf("\nCompile time: %6.3fms\n",compile_timer.time() * 1000.0);
298 }
299
300 if (batch_runs_cnt_)
301 {
302 std::vector<double> timings(batch_runs_cnt_,0.0);
303
304 exprtk::timer total_timer;
305 exprtk::timer timer;
306
307 T result = T(0);
308
309 total_timer.start();
310
311 for (std::size_t i = 0; i < batch_runs_cnt_; ++i)
312 {
313 timer.start();
314
315 result = expression.value();
316
317 timer.stop();
318
319 timings[i] = timer.time() * 1000.0;
320 }
321
322 total_timer.stop();
323
324 printf("\nresult: %s\n",result.toString().c_str());
325
326 std::sort(timings.begin(),timings.end());
327
328 printf("\nRuns: %4d Time min %7.3fms\tmax: %7.3fms\tavg: %7.3fms\ttot: %7.3fms 90%%:%7.3fms\n",
329 static_cast<unsigned int>(batch_runs_cnt_),
330 timings.front(),
331 timings.back (),
332 std::accumulate(timings.begin(),timings.end(),0.0) / timings.size(),
333 total_timer.time() * 1000.0,
334 timings[static_cast<int>(timings.size() * 0.90)]);
335
336 return;
337 }
338
339 exprtk::timer timer;
340 timer.start();
341
342 T result = expression.value();
343
344 timer.stop();
345
346 if (expression.results().count())
347 {
348 print_results(expression.results());
349 }
350
351 printf("\nresult: %s\n",result.toString().c_str());
352
354 {
355 printf("\nTotal time: %6.3fms\n",timer.time() * 1000.0);
356 }
357
358 if (symbol_dump_)
359 {
360 symbol_list_t symbol_list;
361
362 parser_.dec().symbols(symbol_list);
363
364 printf("------ Symbols ------\n");
365 perform_symbol_dump(symbol_list);
366 printf("---------------------\n");
367 }
368
370 {
371 symbol_list_t assignment_list;
372
373 parser_.dec().assignment_symbols(assignment_list);
374
375 printf("---- Assignments ----\n");
376 perform_symbol_dump(assignment_list);
377 printf("---------------------\n");
378 }
379 }
380
381 void process_from_file(const std::string& file_name)
382 {
383 if (file_name.empty())
384 return;
385
386 std::ifstream stream(file_name.c_str());
387
388 if (!stream)
389 {
390 printf("ERROR: Failed to open file: %s\n\n",file_name.c_str());
391 return;
392 }
393
394 std::string program(
395 (std::istreambuf_iterator<char>(stream)),
396 (std::istreambuf_iterator<char>())
397 );
398
399 process_function_definition(program,false);
400 }
401
402 void process_directive(std::string expression)
403 {
404 expression = trim_whitespace(expression);
405
406 if ('$' != expression[0])
407 return;
408 else if ("$enable_cache" == expression)
409 persist_symbol_table() = true;
410 else if ("$disable_cache" == expression)
411 persist_symbol_table() = false;
412 else if ("$enable_symbol_dump" == expression)
413 symbol_dump() = true;
414 else if ("$disable_symbol_dump" == expression)
415 symbol_dump() = false;
416 else if ("$enable_assignment_dump" == expression)
417 assignment_dump() = true;
418 else if ("$disable_assignment_dump" == expression)
419 assignment_dump() = false;
420 else if ("$enable_timer" == expression)
421 display_total_time() = true;
422 else if ("$enable_compile_timer" == expression)
424 else if ("$disable_timer" == expression)
425 display_total_time() = false;
426 else if ("$disable_compile_timer" == expression)
428 else if ("$enable_usr" == expression)
429 enable_usr() = true;
430 else if ("$disable_usr" == expression)
431 enable_usr() = false;
432 else if ("$enable_local_vardef" == expression)
433 disable_local_vardef() = false;
434 else if ("$disable_local_vardef" == expression)
435 disable_local_vardef() = true;
436 else if ("$list_vars" == expression)
437 list_symbols();
438 else if ("$clear_functions" == expression)
440 else if ((0 == expression.find("$set_mpfr_prec ")) && (expression.size() > 15))
441 mpfr::mpreal::set_default_prec(atoi(expression.substr(14,expression.size() - 14).c_str()));
442 else if ((0 == expression.find("$batch_run ")) && (expression.size() > 12))
443 process_batch_run(expression.substr(11,expression.size() - 11));
444 else if ((0 == expression.find("$load ")) && (expression.size() > 7))
445 process_from_file(expression.substr(6,expression.size() - 6));
446 else if ((0 == expression.find("$disable arithmetic ")) && (expression.size() >= 21))
447 process_disable_arithmetic(expression.substr(20,expression.size() - 20));
448 else if ((0 == expression.find("$disable assignment ")) && (expression.size() >= 21))
449 process_disable_assignment(expression.substr(20,expression.size() - 20));
450 else if ((0 == expression.find("$disable inequality ")) && (expression.size() >= 21))
451 process_disable_inequality(expression.substr(20,expression.size() - 20));
452 else if ((0 == expression.find("$enable arithmetic ")) && (expression.size() >= 20))
453 process_enable_arithmetic(expression.substr(19,expression.size() - 19));
454 else if ((0 == expression.find("$enable assignment ")) && (expression.size() >= 20))
455 process_enable_assignment(expression.substr(19,expression.size() - 19));
456 else if ((0 == expression.find("$enable inequality ")) && (expression.size() >= 20))
457 process_enable_inequality(expression.substr(19,expression.size() - 19));
458 else if ("$begin" == expression)
460 else if (0 == expression.find("$function"))
461 process_function_definition(expression);
462 else
463 printf("\nERROR - Invalid directive: %s\n",expression.c_str());
464 }
465
466private:
467
469 {
470 typedef exprtk::results_context<T> results_context_t;
471 typedef typename results_context_t::type_store_t type_t;
472 typedef typename type_t::scalar_view scalar_t;
473 typedef typename type_t::vector_view vector_t;
474 typedef typename type_t::string_view string_t;
475
477
478 printf("%s\n",std::string(10,'-').c_str());
479 printf("Return Results (#%d)\n",static_cast<int>(results.count()));
480
481 for (std::size_t i = 0; i < results.count(); ++i)
482 {
483 printf("[%02d] ",static_cast<int>(i));
484
485 type_t t = results[i];
486
487 switch (t.type)
488 {
489 case type_t::e_scalar : printf("Scalar\t");
490 exprtk::rtl::io::details::print_type("%10.5f",scalar_t(t)(),num_type);
491 break;
492
493 case type_t::e_vector : {
494 printf("Vector\t");
495 vector_t vector(t);
496
497 for (std::size_t x = 0; x < vector.size(); ++x)
498 {
499 exprtk::rtl::io::details::print_type("%10.5f",vector[x],num_type);
500
501 if ((x + 1) < vector.size())
502 printf(" ");
503 }
504 }
505 break;
506
507 case type_t::e_string : printf("String\t");
508 printf("%s",to_str(string_t(t)).c_str());
509 break;
510
511 default : continue;
512 }
513
514 printf("\n");
515 }
516
517 printf("%s\n",std::string(10,'-').c_str());
518 }
519
520 void perform_symbol_dump(const symbol_list_t& variable_list) const
521 {
522 for (std::size_t i = 0; i < variable_list.size(); ++i)
523 {
524 const symbol_t& symbol = variable_list[i];
525
526 switch (symbol.second)
527 {
528 case parser_t::e_st_variable : printf("[%02d] Variable %s\n",
529 static_cast<int>(i),symbol.first.c_str());
530 break;
531
532 case parser_t::e_st_vector : printf("[%02d] Vector %s\n",
533 static_cast<int>(i),symbol.first.c_str());
534 break;
535
536 case parser_t::e_st_string : printf("[%02d] String %s\n",
537 static_cast<int>(i),symbol.first.c_str());
538 break;
539
540 case parser_t::e_st_function : printf("[%02d] Function %s\n",
541 static_cast<int>(i),symbol.first.c_str());
542 break;
543
545 : printf("[%02d] LocalVar %s\n",
546 static_cast<int>(i),symbol.first.c_str());
547 break;
548
550 : printf("[%02d] LocalVec %s\n",
551 static_cast<int>(i),symbol.first.c_str());
552 break;
553
555 : printf("[%02d] LocalStr %s\n",
556 static_cast<int>(i),symbol.first.c_str());
557 break;
558
559 default : break;
560 }
561 }
562 }
563
564 void process_batch_run(const std::string& batch_runs_cnt)
565 {
566 batch_runs_cnt_ = atoi(batch_runs_cnt.c_str());
567 }
568
570 {
571 std::string program;
572
573 for ( ; ; )
574 {
575 std::string line;
576
577 std::cout << ">> ";
578 std::getline(std::cin,line);
579
580 line = trim_whitespace(line);
581
582 if (line.empty())
583 continue;
584 else if ("$end" == line)
585 break;
586 else
587 program += (line + "\n");
588 }
589
590 process(program);
591 }
592
594 {
595 std::string name;
596 std::string body;
597 std::vector<std::string> var_list;
598
599 void clear()
600 {
601 name .clear();
602 body .clear();
603 var_list.clear();
604 }
605 };
606
615
617 {
619 {
620 if (!init(func_def))
621 return e_parse_lexfail;
622
623 if (!token_is(token_t::e_symbol,"function"))
624 return e_parse_notfunc;
625
627 return e_parse_partial;
628
629 fd.name = current_token().value;
630
631 next_token();
632
634 return e_parse_partial;
635
637 {
638 std::vector<std::string> var_list;
639
640 for ( ; ; )
641 {
642 // (x,y,z,....w)
644 return e_parse_partial;
645
646 var_list.push_back(current_token().value);
647
648 next_token();
649
651 break;
652
654 return e_parse_partial;
655 }
656
657 var_list.swap(fd.var_list);
658 }
659
660 std::size_t body_begin = current_token().position;
661 std::size_t body_end = current_token().position;
662
663 int bracket_stack = 0;
664
666 return e_parse_partial;
667
668 for ( ; ; )
669 {
670 body_end = current_token().position;
671
673 bracket_stack++;
675 {
676 if (0 == --bracket_stack)
677 break;
678 }
679 else
680 {
681 if (lexer().finished())
682 return e_parse_partial;
683
684 next_token();
685 }
686 }
687
688 std::size_t size = body_end - body_begin + 1;
689
690 fd.body = func_def.substr(body_begin,size);
691
692 const std::size_t index = body_begin + size;
693
694 if (index < func_def.size())
695 func_def = func_def.substr(index,func_def.size() - index);
696 else
697 func_def = "";
698
699 return e_parse_success;
700 }
701 };
702
704 {
706 return parser.process(func_def,cf);
707 }
708
709 std::string read_from_stdin()
710 {
711 std::string input;
712
713 for ( ; ; )
714 {
715 std::string line;
716
717 std::cout << ">> ";
718 std::getline(std::cin,line);
719
720 if (line.empty())
721 continue;
722 else if ("$end" == line)
723 break;
724 else
725 input += (line + "\n");
726 }
727
728 if (!input.empty())
729 input.erase(input.end() - 1);
730
731 return input;
732 }
733
734 void process_function_definition(const std::string& func_def_header, bool read_stdin = true)
735 {
736 std::string func_def = func_def_header;
737
738 if (read_stdin)
739 {
740 func_def += read_from_stdin();
741
742 if (!func_def.empty() && ('$' == func_def[0]))
743 func_def.erase(func_def.begin());
744 }
745
746 do
747 {
749
750 func_parse_result fp_result = parse_function_definition(func_def,fd);
751
752 if (e_parse_success == fp_result)
753 {
754 std::string vars;
755
756 for (std::size_t i = 0; i < fd.var_list.size(); ++i)
757 {
758 vars += fd.var_list[i] + ((i < fd.var_list.size() - 1) ? "," : "");
759 }
760
761 function_t f(fd.name);
762
763 for (std::size_t i = 0; i < fd.var_list.size(); ++i)
764 {
765 f.var(fd.var_list[i]);
766 }
767
768 f.expression(fd.body);
769
771 {
773
774 for (std::size_t i = 0; i < func_def_list_.size(); ++i)
775 {
777 {
778 func_def_list_.erase(func_def_list_.begin() + i);
779
780 break;
781 }
782 }
783 }
784
785 if (!compositor_.add(f,true))
786 {
788
789 printf("Error - Failed to add function: %s\n",fd.name.c_str());
790
791 return;
792 }
793
794 printf("Function[%02d]\n",static_cast<int>(func_def_list_.size()));
795 printf("Name: %s \n",fd.name.c_str() );
796 printf("Vars: (%s) \n",vars.c_str() );
797 printf("------------------------------------------------------\n");
798
799 func_def_list_.push_back(fd);
800 }
801 else if (e_parse_notfunc != fp_result)
802 {
803 printf("Error - Critical parsing error - partial parse occured\n");
804 return;
805 }
806 else
807 break;
808 }
809 while (!func_def.empty());
810
811 if (!func_def.empty())
812 {
813 process(func_def);
814 }
815 }
816
818 {
819 std::deque<std::pair<std::string,T> > variable_list;
820 symbol_table_.get_variable_list(variable_list);
821
822 std::size_t max_varname_length = 0;
823
824 for (std::size_t i = 0; i < variable_list.size(); ++i)
825 {
826 max_varname_length = std::max(max_varname_length,variable_list[i].first.size());
827 }
828
829 for (std::size_t i = 0; i < variable_list.size(); ++i)
830 {
831 int pad_length = 0;
832
833 if (max_varname_length > variable_list[i].first.size())
834 {
835 pad_length = static_cast<int>(max_varname_length - variable_list[i].first.size());
836 }
837
838 printf("%02d %s%*.*s %s\n",
839 static_cast<unsigned int>(i),
840 variable_list[i].first.c_str(),
841 pad_length,
842 pad_length,
843 std::string(max_varname_length,' ').c_str(),
844 variable_list[i].second.toString().c_str());
845 }
846 }
847
849 {
850 func_def_list_.clear();
852 }
853
854 std::string trim_whitespace(std::string s)
855 {
856 static const std::string whitespace(" \n\r\t\b\v\f");
857
858 if (!s.empty())
859 {
860 s.erase(0,s.find_first_not_of(whitespace));
861
862 if (!s.empty())
863 {
864 std::size_t index = s.find_last_not_of(whitespace);
865
866 if (std::string::npos != index)
867 s.erase(index + 1);
868 else
869 s.clear();
870 }
871 }
872
873 return s;
874 }
875
876 void process_disable_arithmetic(const std::string& arithmetic)
877 {
878 typename std::map<std::string,typename settings_store_t::settings_arithmetic_opr>::iterator itr;
879
880 if (arith_opr_.end() != (itr = arith_opr_.find(arithmetic)))
881 {
883 .disable_arithmetic_operation(itr->second);
884 }
885 }
886
887 void process_disable_assignment(const std::string& assignment)
888 {
889 typename std::map<std::string,typename settings_store_t::settings_assignment_opr>::iterator itr;
890
891 if (assign_opr_.end() != (itr = assign_opr_.find(assignment)))
892 {
894 .disable_assignment_operation(itr->second);
895 }
896 }
897
898 void process_disable_inequality(const std::string& inequality)
899 {
900 typename std::map<std::string,typename settings_store_t::settings_inequality_opr>::iterator itr;
901
902 if (inequality_opr_.end() != (itr = inequality_opr_.find(inequality)))
903 {
905 .disable_inequality_operation(itr->second);
906 }
907 }
908
909 void process_enable_arithmetic(const std::string& arithmetic)
910 {
911 typename std::map<std::string,typename settings_store_t::settings_arithmetic_opr>::iterator itr;
912
913 if (arith_opr_.end() != (itr = arith_opr_.find(arithmetic)))
914 {
916 .enable_arithmetic_operation(itr->second);
917 }
918 }
919
920 void process_enable_assignment(const std::string& assignment)
921 {
922 typename std::map<std::string,typename settings_store_t::settings_assignment_opr>::iterator itr;
923
924 if (assign_opr_.end() != (itr = assign_opr_.find(assignment)))
925 {
927 .enable_assignment_operation(itr->second);
928 }
929 }
930
931 void process_enable_inequality(const std::string& inequality)
932 {
933 typename std::map<std::string,typename settings_store_t::settings_inequality_opr>::iterator itr;
934
935 if (inequality_opr_.end() != (itr = inequality_opr_.find(inequality)))
936 {
938 .enable_inequality_operation(itr->second);
939 }
940 }
941
942
943private:
944
952 std::size_t batch_runs_cnt_;
953
958
959 putch <T> putch_;
960 putint <T> putint_;
961 rnd_01 <T> rnd_01_;
965
978
979 std::vector<function_definition> func_def_list_;
980
981 std::map<std::string,typename settings_store_t::settings_arithmetic_opr> arith_opr_;
982 std::map<std::string,typename settings_store_t::settings_assignment_opr> assign_opr_;
983 std::map<std::string,typename settings_store_t::settings_inequality_opr> inequality_opr_;
984
985 #ifdef exprtk_enable_repl_variables
986 std::string s0_;
987 std::string s1_;
988 std::string s2_;
989 std::vector<T> v0_;
990 std::vector<T> v1_;
991 std::vector<T> v2_;
992 std::vector<T> v3_;
994 #endif
995};
996
997template <typename T>
998void repl(int argc, char* argv[])
999{
1000 expression_processor<T> processor;
1001
1002 if (argc > 1)
1003 {
1004 for (int i = 1; i < argc; ++i)
1005 {
1006 processor.process_from_file(argv[i]);
1007 }
1008 }
1009 else
1010 {
1011 for ( ; ; )
1012 {
1013 std::string expression;
1014
1015 std::cout << ">> ";
1016 std::getline(std::cin,expression);
1017
1018 if (expression.empty())
1019 continue;
1020 else if ("exit" == expression)
1021 break;
1022 else if ("quit" == expression)
1023 break;
1024 else if ('$' == expression[0])
1025 processor.process_directive(expression);
1026 else
1027 processor.process(expression);
1028 }
1029 }
1030}
1031
1032int main(int argc, char* argv[])
1033{
1034 mpfr::mpreal::set_default_prec(1024);
1035 repl<mpfr::mpreal>(argc,argv);
1036 return 0;
1037}
1038
1039
1040/*
1041
1042REPL commands:
1043
1044 $enable_cache/$disable_cache
1045 Enable or disable caching of variables.
1046
1047 $enable_symbol_dump/$disable_symbol_dump
1048 Enable or disable dumping of symbols found in expression during
1049 compilation process.
1050
1051 $enable_assignment_dump/$disable_assignment_dump
1052 Enable or disable dumping of symbols that undergo assignment in
1053 expression.
1054
1055 $enable_timer/$disable_timer
1056 Enable or disable expression evaluation timer.
1057
1058 $enable_compile_timer/$disable_compile_timer
1059 Enable or disable compilation timer
1060
1061 $enable_usr/$disable_usr
1062 Enable or disable unknown symbol resolver.
1063
1064 $list_vars
1065 List variables found in the global symbol table.
1066
1067 $clear_functions
1068 Clear all functions found in the global function symbol table.
1069
1070 $load <program file name>
1071 Load the file as a complete program and execute.
1072
1073 $begin/$end
1074 Pre/post-ambles for multiline expressions.
1075
1076
1077Example REPL Instructions:
1078 Step 1.1 Enter: var x := 3; var y := 4; var z := x + y; println(z / 2);
1079 Step 1.2 Enter: var x := 3; var y := 4; var z := x - y; putint(z / 2);
1080
1081
1082 Step 2.Enter the following multi-line program:
1083 --- snip ---
1084
1085$begin
1086var x := 3;
1087var y := 4;
1088var z := sin(x / pi) * cos(y * pi);
1089println(z / 2);
1090$end
1091
1092 --- snip ---
1093
1094
1095 Step 3.1 Create a new file called mandelbrot.txt
1096 Step 3.2 Copy the contents between the snip lines into the file
1097 Step 3.3 Execute: ./exprtk_repl mandelbrot.txt
1098
1099
1100 Step 4.1 Execute: ./exprtk_repl
1101 Step 4.2 Enter: $load mandelbrot.txt
1102
1103---- snip ----
1104
1105var width := 118;
1106var height := 41;
1107var imag_max := +1;
1108var imag_min := -1;
1109var real_max := +1;
1110var real_min := -2.5;
1111var x_step := (real_max - real_min) / width;
1112var y_step := (imag_max - imag_min) / height;
1113
1114for (var y := 0; y < height; y += 1)
1115{
1116 var imag := imag_min + (y_step * y);
1117
1118 for (var x := 0; x < width; x += 1)
1119 {
1120 var real := real_min + x_step * x;
1121 var z_real := real;
1122 var z_imag := imag;
1123 var plot_value := 0;
1124
1125 for (var n := 0; n < 30; n += 1)
1126 {
1127 var a := z_real^2;
1128 var b := z_imag^2;
1129
1130 plot_value := n;
1131
1132 if ((a + b) < 4)
1133 {
1134 z_imag := 2 * z_real * z_imag + imag;
1135 z_real := a - b + real;
1136 }
1137 else
1138 break;
1139 };
1140
1141 putch(61 - plot_value);
1142
1143 };
1144
1145 println()
1146}
1147
1148---- snip ----
1149
1150
1151 Step 5.1 Copy into the REPL the contents of the snippet below:
1152---- snip ----
1153$function is_prime(x)
1154{
1155 if (x == 1)
1156 return [false];
1157 else if (x == 2)
1158 return [true];
1159 else if ((x % 2) == 0)
1160 return [false];
1161
1162 var upper_bound := sqrt(x) + 1;
1163
1164 for (var i := 3; i <= upper_bound; i += 2)
1165 {
1166 if (0 == (x % i))
1167 {
1168 return[false];
1169 }
1170 };
1171
1172 return [true];
1173}
1174$end
1175
1176$begin
1177for (var i := 1; i < 50; i += 1)
1178{
1179 if (is_prime(i))
1180 {
1181 println(i);
1182 }
1183}
1184$end
1185
1186---- snip ----
1187
1188
1189 Step 6.1 Copy into the REPL the contents of the snippet below:
1190---- snip ----
1191
1192$begin
1193var s := 'abcdefghijkl';
1194for (var i := 0; i < (s[] / 2); i+= 1)
1195{
1196 var j := s[] - i - 1;
1197 s[i:i + 1] <=> s[j:j + 1];
1198};
1199
1200println(s)
1201$end
1202
1203---- snip ----
1204
1205
1206 Step 7.1 Copy into the REPL the contents of the snippet below:
1207---- snip ----
1208$begin
1209var vec[11] := { -1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1 };
1210var zero := 0;
1211var max_sum := 0;
1212var max_start := 0;
1213var max_end := 0;
1214var curr_sum := 0;
1215var curr_start := 0;
1216
1217for (var i := 0; i < vec[]; i += 1)
1218{
1219 curr_sum += vec[i];
1220
1221 if (curr_sum < zero)
1222 {
1223 curr_sum := 0;
1224 curr_start := i + 1;
1225 }
1226 else if (curr_sum > max_sum)
1227 {
1228 max_sum := curr_sum;
1229 max_start := curr_start;
1230 max_end := i;
1231 }
1232}
1233
1234println('vec: ',vec);
1235
1236println('Max sum: ', max_sum );
1237println('Start index: ', max_start);
1238println('End index: ', max_end );
1239
1240for (var i := max_start; i <= max_end; i += 1)
1241{
1242 print(vec[i],' ');
1243}
1244$end
1245
1246---- snip ----
1247
1248
1249 Step 8.1 Copy into the REPL the contents of the snippet below:
1250---- snip ----
1251
1252$function is_prime(x)
1253{
1254 if (x <= 0)
1255 return [false];
1256 else if (frac(x) != 0)
1257 return [false];
1258 else
1259 {
1260 switch
1261 {
1262 case 1 == x : return [false];
1263 case 2 == x : return [true ];
1264 default :
1265 {
1266 var prime_lut[160] :=
1267 {
1268 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
1269 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
1270 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
1271 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
1272 179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
1273 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
1274 283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
1275 353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
1276 419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
1277 467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
1278 547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
1279 607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
1280 661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
1281 739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
1282 811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
1283 877, 881, 883, 887, 907, 911, 919, 929, 937, 941
1284 };
1285
1286 var upper_bound := min(x - 1,trunc(sqrt(x)) + 1);
1287
1288 for (var i := 0; i < prime_lut[]; i += 1)
1289 {
1290 if (prime_lut[i] >= upper_bound)
1291 return [true];
1292 else if ((x % prime_lut[i]) == 0)
1293 return [false];
1294 };
1295
1296 var lower_bound := prime_lut[prime_lut[] - 1] + 2;
1297
1298 for (var i := lower_bound; i < upper_bound; i += 2)
1299 {
1300 if ((x % i) == 0)
1301 {
1302 return [false];
1303 }
1304 }
1305 };
1306 }
1307 };
1308
1309 return [true];
1310}
1311
1312var prime_count := 0;
1313
1314for (var i := 1; i < 10^6; i += 1)
1315{
1316 if (is_prime(i))
1317 {
1318 prime_count += 1;
1319 }
1320};
1321
1322prime_count
1323$end
1324---- snip ----
1325
1326
1327Step 9.1 Copy into the REPL the contents of the snippet below:
1328---- snip ----
1329$begin
1330var file := open('file.txt','w');
1331var s := 'Hello world...\n';
1332
1333for (var i := 0; i < 10; i += 1)
1334{
1335 write(file,s);
1336};
1337
1338close(file);
1339
1340println('~~~~~~~~~~~~~~~~~~~~~~');
1341
1342file := open('file.txt','r');
1343var i := 0;
1344
1345while (not(eof(file)))
1346{
1347 s := getline(file);
1348
1349 if (s[] > 0)
1350 {
1351 println('[',i+=1,'] - ',s);
1352 }
1353};
1354
1355close(file);
1356$end
1357---- snip ----
1358
1359
1360Step 10.1 Copy into the REPL the contents of the snippet below:
1361---- snip ----
1362$begin
1363var v0[1000] := [rnd_01];
1364var v1[1000] := [0];
1365
1366~{
1367 var file := open('data.dat','w');
1368
1369 if (not(write(file,v0)))
1370 {
1371 println('failed to write vector 0\n');
1372 return [false];
1373 };
1374
1375 close(file);
1376 };
1377
1378~{
1379 var file := open('data.dat','r');
1380
1381 if (not(read(file,v1)))
1382 {
1383 println('failed to read vector 1\n');
1384 return [false];
1385 };
1386
1387 close(file);
1388 };
1389
1390if (sum(v0 != v1) > 0)
1391 println('error: v0 != v1');
1392else
1393{
1394 println('success: v0 == v1');
1395 println('sum(v0) = ',sum(v0),' avg(v0) = ',avg(v0));
1396 println('sum(v1) = ',sum(v1),' avg(v1) = ',avg(v1));
1397}
1398$end
1399---- snip ----
1400
1401
1402Step 11.1 Copy into the REPL the contents of the snippet below:
1403---- snip ----
1404$function fibonacci(x)
1405{
1406 switch
1407 {
1408 case x == 0 : 0;
1409 case x == 1 : 1;
1410 default :
1411 {
1412 var prev := 0;
1413 var curr := 1;
1414 while ((x -= 1) > 0)
1415 {
1416 curr += (curr <=> prev);
1417 };
1418 };
1419 }
1420}
1421
1422for (var i := 0; i < 20; i += 1)
1423{
1424 println(i, ' = ', fibonacci(i));
1425};
1426
1427$end
1428---- snip ----
1429
1430*/
void print_results(const exprtk::results_context< T > &results)
exprtk::rtl::io::file::package< T > fileio_package_
exprtk::polynomial< T, 6 > poly06_
void process_enable_inequality(const std::string &inequality)
exprtk::expression< T > expression_t
exprtk::polynomial< T, 5 > poly05_
std::vector< function_definition > func_def_list_
void process_disable_inequality(const std::string &inequality)
std::map< std::string, typename settings_store_t::settings_arithmetic_opr > arith_opr_
parser_t::dependent_entity_collector::symbol_t symbol_t
exprtk::polynomial< T, 7 > poly07_
exprtk::rtl::vecops::package< T > vecops_package_
void process_enable_assignment(const std::string &assignment)
exprtk::polynomial< T, 4 > poly04_
exprtk::polynomial< T, 8 > poly08_
parser_t::settings_store settings_store_t
void perform_symbol_dump(const symbol_list_t &variable_list) const
void process_batch_run(const std::string &batch_runs_cnt)
std::vector< symbol_t > symbol_list_t
std::map< std::string, typename settings_store_t::settings_assignment_opr > assign_opr_
void process_disable_arithmetic(const std::string &arithmetic)
func_parse_result parse_function_definition(std::string &func_def, function_definition &cf)
exprtk::polynomial< T, 3 > poly03_
compositor_t::function function_t
exprtk::polynomial< T, 9 > poly09_
std::string trim_whitespace(std::string s)
exprtk::parser_error::type error_t
exprtk::symbol_table< T > symbol_table_t
void process_disable_assignment(const std::string &assignment)
exprtk::function_compositor< T > compositor_t
exprtk::lexer::parser_helper prsrhlpr_t
std::map< std::string, typename settings_store_t::settings_inequality_opr > inequality_opr_
exprtk::polynomial< T, 2 > poly02_
exprtk::parser< T > parser_t
void process_function_definition(const std::string &func_def_header, bool read_stdin=true)
exprtk::polynomial< T, 12 > poly12_
symbol_table_t function_symbol_table_
exprtk::rtl::io::package< T > io_package_
exprtk::polynomial< T, 11 > poly11_
exprtk::polynomial< T, 10 > poly10_
exprtk::polynomial< T, 1 > poly01_
void process_from_file(const std::string &file_name)
void process_directive(std::string expression)
void process(std::string program)
void process_enable_arithmetic(const std::string &arithmetic)
bool register_symbol_table(symbol_table< T > &st)
Definition exprtk.hpp:21726
const results_context_t & results() const
Definition exprtk.hpp:21757
void add_auxiliary_symtab(symbol_table_t &symtab)
Definition exprtk.hpp:43103
bool add(const std::string &name, const std::string &expression, const Sequence< std::string, Allocator > &var_list, const bool override=false)
Definition exprtk.hpp:43032
ifunction(const std::size_t &pc)
Definition exprtk.hpp:19545
bool init(const std::string &str)
Definition exprtk.hpp:4353
const token_t & current_token() const
Definition exprtk.hpp:4394
bool token_is(const token_t::token_type &ttype, const token_advance_mode mode=e_advance)
Definition exprtk.hpp:4418
std::size_t assignment_symbols(Sequence< symbol_t, Allocator > &assignment_list)
Definition exprtk.hpp:23539
std::size_t symbols(Sequence< symbol_t, Allocator > &symbols_list)
Definition exprtk.hpp:23513
settings_store & enable_arithmetic_operation(const settings_arithmetic_opr arithmetic)
Definition exprtk.hpp:24145
settings_store & disable_arithmetic_operation(const settings_arithmetic_opr arithmetic)
Definition exprtk.hpp:24052
settings_store & disable_assignment_operation(const settings_assignment_opr assignment)
Definition exprtk.hpp:24065
settings_store & enable_assignment_operation(const settings_assignment_opr assignment)
Definition exprtk.hpp:24163
settings_store & disable_local_vardef()
Definition exprtk.hpp:23870
settings_store & disable_inequality_operation(const settings_inequality_opr inequality)
Definition exprtk.hpp:24078
settings_store & enable_local_vardef()
Definition exprtk.hpp:23798
settings_store & enable_inequality_operation(const settings_inequality_opr inequality)
Definition exprtk.hpp:24181
bool compile(const std::string &expression_string, expression< T > &expr)
Definition exprtk.hpp:24443
void enable_unknown_symbol_resolver(unknown_symbol_resolver *usr=reinterpret_cast< unknown_symbol_resolver * >(0))
Definition exprtk.hpp:24762
dependent_entity_collector & dec()
Definition exprtk.hpp:24737
parser_error::type get_error(const std::size_t &index) const
Definition exprtk.hpp:24712
std::string error() const
Definition exprtk.hpp:24722
std::size_t error_count() const
Definition exprtk.hpp:24732
void disable_unknown_symbol_resolver()
Definition exprtk.hpp:24777
settings_store & settings()
Definition exprtk.hpp:24707
std::size_t count() const
Definition exprtk.hpp:4896
bool add_package(Package &package)
Definition exprtk.hpp:21098
bool add_constant(const std::string &constant_name, const T &value)
Definition exprtk.hpp:20782
void clear_variables(const bool delete_node=true)
Definition exprtk.hpp:20508
bool add_function(const std::string &function_name, function_t &function)
Definition exprtk.hpp:20811
function_ptr get_function(const std::string &function_name) const
Definition exprtk.hpp:20628
std::size_t get_variable_list(Sequence< std::pair< std::string, T >, Allocator > &vlist) const
Definition exprtk.hpp:21105
bool add_vector(const std::string &vector_name, T(&v)[N])
Definition exprtk.hpp:20974
bool add_stringvar(const std::string &stringvar_name, std::string &s, const bool is_constant=false)
Definition exprtk.hpp:20798
bool remove_function(const std::string &function_name)
Definition exprtk.hpp:21047
double time() const
Definition exprtk.hpp:43502
void repl(int argc, char *argv[])
bool imatch(const char_t c1, const char_t c2)
Definition exprtk.hpp:190
bool update_error(type &error, const std::string &expression)
Definition exprtk.hpp:22114
std::string to_str(error_mode mode)
Definition exprtk.hpp:22098
void print_type(const std::string &fmt, const T v, exprtk::details::numeric::details::real_type_tag)
Definition exprtk.hpp:43547
vector_view< T > make_vector_view(T *data, const std::size_t size, const std::size_t offset=0)
Definition exprtk.hpp:4660
func_parse_result process(std::string &func_def, function_definition &fd)
function & expression(const std::string &e)
Definition exprtk.hpp:42516
function & var(const std::string &v)
Definition exprtk.hpp:42522
std::size_t position
Definition exprtk.hpp:2400
std::string value
Definition exprtk.hpp:2399
T operator()(const T &v)
T operator()(const T &v)