From 36ef9e611fb9bb421b52ff67bab899065ba0d6ae Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Mon, 27 Oct 2014 21:57:30 +1100 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html --- Makefile | 105 +++++------------------------------ exprtk.hpp | 43 ++++++-------- exprtk_benchmark.cpp | 2 +- exprtk_simple_example_04.cpp | 2 +- 4 files changed, 32 insertions(+), 120 deletions(-) diff --git a/Makefile b/Makefile index 13edf45..e7702ed 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ COMPILER = -c++ #COMPILER = -clang OPTIMIZATION_OPT = -O1 BASE_OPTIONS = -pedantic-errors -Wall -Wextra -Werror -Wno-long-long -OPTIONS = $(BASE_OPTIONS) $(OPTIMIZATION_OPT) -o +OPTIONS = $(BASE_OPTIONS) $(OPTIMIZATION_OPT) LINKER_OPT = -L/usr/lib -lstdc++ -lm BUILD_LIST+=exprtk_test @@ -41,106 +41,27 @@ BUILD_LIST+=exprtk_simple_example_14 BUILD_LIST+=exprtk_simple_example_15 BUILD_LIST+=exprtk_simple_example_16 + all: $(BUILD_LIST) -exprtk_test: exprtk_test.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_test exprtk_test.cpp $(LINKER_OPT) +$(BUILD_LIST) : %: %.cpp exprtk.hpp + $(COMPILER) $(OPTIONS) -o $@ $@.cpp $(LINKER_OPT) -exprtk_benchmark: exprtk_benchmark.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_benchmark exprtk_benchmark.cpp $(LINKER_OPT) +strip_bin : + @for f in $(BUILD_LIST); do if [ -f $$f ]; then strip -s $$f; echo $$f; fi done; -exprtk_simple_example_01: exprtk_simple_example_01.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_01 exprtk_simple_example_01.cpp $(LINKER_OPT) - -exprtk_simple_example_02: exprtk_simple_example_02.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_02 exprtk_simple_example_02.cpp $(LINKER_OPT) - -exprtk_simple_example_03: exprtk_simple_example_03.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_03 exprtk_simple_example_03.cpp $(LINKER_OPT) - -exprtk_simple_example_04: exprtk_simple_example_04.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_04 exprtk_simple_example_04.cpp $(LINKER_OPT) - -exprtk_simple_example_05: exprtk_simple_example_05.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_05 exprtk_simple_example_05.cpp $(LINKER_OPT) - -exprtk_simple_example_06: exprtk_simple_example_06.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_06 exprtk_simple_example_06.cpp $(LINKER_OPT) - -exprtk_simple_example_07: exprtk_simple_example_07.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_07 exprtk_simple_example_07.cpp $(LINKER_OPT) - -exprtk_simple_example_08: exprtk_simple_example_08.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_08 exprtk_simple_example_08.cpp $(LINKER_OPT) - -exprtk_simple_example_09: exprtk_simple_example_09.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_09 exprtk_simple_example_09.cpp $(LINKER_OPT) - -exprtk_simple_example_10: exprtk_simple_example_10.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_10 exprtk_simple_example_10.cpp $(LINKER_OPT) - -exprtk_simple_example_11: exprtk_simple_example_11.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_11 exprtk_simple_example_11.cpp $(LINKER_OPT) - -exprtk_simple_example_12: exprtk_simple_example_12.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_12 exprtk_simple_example_12.cpp $(LINKER_OPT) - -exprtk_simple_example_13: exprtk_simple_example_13.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_13 exprtk_simple_example_13.cpp $(LINKER_OPT) - -exprtk_simple_example_14: exprtk_simple_example_14.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_14 exprtk_simple_example_14.cpp $(LINKER_OPT) - -exprtk_simple_example_15: exprtk_simple_example_15.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_15 exprtk_simple_example_15.cpp $(LINKER_OPT) - -exprtk_simple_example_16: exprtk_simple_example_16.cpp exprtk.hpp - $(COMPILER) $(OPTIONS) exprtk_simple_example_16 exprtk_simple_example_16.cpp $(LINKER_OPT) +valgrind : + @for f in $(BUILD_LIST); do \ + if [ -f $$f ]; then \ + cmd="valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=$$f.log -v ./$$f"; \ + echo $$cmd; \ + $$cmd; \ + fi done; pgo: exprtk_benchmark.cpp exprtk.hpp $(COMPILER) $(BASE_OPTIONS) -O3 -march=native -fprofile-generate -o exprtk_benchmark exprtk_benchmark.cpp $(LINKER_OPT) ./exprtk_benchmark $(COMPILER) $(BASE_OPTIONS) -O3 -march=native -fprofile-use -o exprtk_benchmark exprtk_benchmark.cpp $(LINKER_OPT) -strip_bin: - strip -s exprtk_test - strip -s exprtk_benchmark - strip -s exprtk_simple_example_01 - strip -s exprtk_simple_example_02 - strip -s exprtk_simple_example_03 - strip -s exprtk_simple_example_04 - strip -s exprtk_simple_example_05 - strip -s exprtk_simple_example_06 - strip -s exprtk_simple_example_07 - strip -s exprtk_simple_example_08 - strip -s exprtk_simple_example_09 - strip -s exprtk_simple_example_10 - strip -s exprtk_simple_example_11 - strip -s exprtk_simple_example_12 - strip -s exprtk_simple_example_13 - strip -s exprtk_simple_example_14 - strip -s exprtk_simple_example_15 - strip -s exprtk_simple_example_16 - -valgrind_check: - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_test_valgrind.log -v ./exprtk_test - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_benchmark_valgrind.log -v ./exprtk_benchmark - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_01_valgrind.log -v ./exprtk_simple_example_01 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_02_valgrind.log -v ./exprtk_simple_example_02 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_03_valgrind.log -v ./exprtk_simple_example_03 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_04_valgrind.log -v ./exprtk_simple_example_04 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_05_valgrind.log -v ./exprtk_simple_example_05 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_06_valgrind.log -v ./exprtk_simple_example_06 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_07_valgrind.log -v ./exprtk_simple_example_07 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_08_valgrind.log -v ./exprtk_simple_example_08 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_09_valgrind.log -v ./exprtk_simple_example_09 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_10_valgrind.log -v ./exprtk_simple_example_10 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_11_valgrind.log -v ./exprtk_simple_example_11 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_12_valgrind.log -v ./exprtk_simple_example_12 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_13_valgrind.log -v ./exprtk_simple_example_13 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_14_valgrind.log -v ./exprtk_simple_example_14 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_15_valgrind.log -v ./exprtk_simple_example_15 - valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_simple_example_16_valgrind.log -v ./exprtk_simple_example_16 - clean: rm -f core.* *~ *.o *.bak *stackdump gmon.out *.gcda *.gcno *.gcnor *.gch diff --git a/exprtk.hpp b/exprtk.hpp index 58058b3..27458b9 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -2542,8 +2542,8 @@ namespace exprtk { lexer::token t = generator[i]; printf("Token[%02d] @ %03d %6s --> '%s'\n", - static_cast(i), - static_cast(t.position), + static_cast(i), + static_cast(t.position), t.to_str(t.type).c_str(), t.value.c_str()); } @@ -3981,7 +3981,6 @@ namespace exprtk private: - operator_type operation_; expression_ptr branch_; bool branch_deletable_; bool equality_; @@ -6888,7 +6887,6 @@ namespace exprtk private: vector_node* vec_node_ptr_; - vector_node* vec1_node_ptr_; }; template @@ -6960,7 +6958,6 @@ namespace exprtk private: vector_node* vec_node_ptr_; - vector_node* vec1_node_ptr_; }; template @@ -12667,6 +12664,12 @@ namespace exprtk struct type { + type() + : mode(parser_error::e_unknown), + line_no(0), + column_no(0) + {} + lexer::token token; error_mode mode; std::string diagnostic; @@ -12681,13 +12684,11 @@ namespace exprtk t.mode = mode; t.token.type = lexer::token::e_error; t.diagnostic = diagnostic; - t.line_no = 0; - t.column_no = 0; exprtk_debug(((diagnostic + "\n").c_str())); return t; } - inline type make_error(error_mode mode, const lexer::token tk, const std::string& diagnostic = "") + inline type make_error(error_mode mode, const lexer::token& tk, const std::string& diagnostic = "") { type t; t.mode = mode; @@ -12756,7 +12757,7 @@ namespace exprtk inline void dump_error(const type& error) { printf("Position: %02d Type: [%s] Msg: %s\n", - static_cast(error.token.position), + static_cast(error.token.position), exprtk::parser_error::to_str(error.mode).c_str(), error.diagnostic.c_str()); } @@ -12937,7 +12938,8 @@ namespace exprtk typedef parser parser_t; scope_element_manager(parser& p) - : parser_(p) + : parser_(p), + input_param_cnt_(0) {} inline std::size_t size() const @@ -14350,7 +14352,6 @@ namespace exprtk } } - std::string symbol = current_token_.value; if (result) { if (details::imatch(current_token_.value,"else")) @@ -14942,8 +14943,6 @@ namespace exprtk std::vector arg_list; expression_node_ptr result = error_node(); - const std::string symbol = current_token_.value; - if (!details::imatch(current_token_.value,"switch")) { set_error( @@ -15077,8 +15076,6 @@ namespace exprtk std::vector arg_list; expression_node_ptr result = error_node(); - const std::string symbol = current_token_.value; - if (!details::imatch(current_token_.value,"[*]")) { set_error( @@ -15813,8 +15810,6 @@ namespace exprtk std::vector arg_list; expression_node_ptr result = error_node(); - const std::string symbol = current_token_.value; - scoped_vec_delete sdd(*this,arg_list); next_token(); @@ -16255,7 +16250,7 @@ namespace exprtk exprtk_debug(("parse_define_vector_statement() - INFO - Added new local vector: %s[%d]\n", nse.name.c_str(), - static_cast(nse.size))); + static_cast(nse.size))); } expression_node_ptr result = @@ -16503,8 +16498,6 @@ namespace exprtk return error_node(); } - var_node = nse.var_node; - exprtk_debug(("parse_uninitialised_var_statement() - INFO - Added new local variable: %s\n",nse.name.c_str())); } @@ -16734,8 +16727,8 @@ namespace exprtk if (vararg_function) { - expression_node_ptr vararg_func_node = error_node(); - vararg_func_node = parse_vararg_function_call(vararg_function,symbol); + expression_node_ptr vararg_func_node = + parse_vararg_function_call(vararg_function,symbol); if (vararg_func_node) return vararg_func_node; @@ -21576,7 +21569,7 @@ namespace exprtk return (synthesis_result) ? result : error_node(); } // (v0 * c0) / (v1 / c1) --> (covov) (c0 * c1) * (v0 / v1) - else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_mul == o2)) + else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_div == o2)) { const bool synthesis_result = synthesize_sf3ext_expression:: @@ -21921,7 +21914,7 @@ namespace exprtk return (synthesis_result) ? result : error_node(); } // (v0 * c0) / (c1 / v1) --> (covov) (c0 / c1) * (v0 * v1) - else if ((details::e_mul == o0) && (details::e_mul == o1) && (details::e_mul == o2)) + else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_div == o2)) { const bool synthesis_result = synthesize_sf3ext_expression:: @@ -25270,8 +25263,6 @@ namespace exprtk const Sequence& var_list) { const std::size_t n = var_list.size(); - std::vector v(n,0); - std::vector sv(n); if (expr_map_.end() != expr_map_.find(name)) return false; diff --git a/exprtk_benchmark.cpp b/exprtk_benchmark.cpp index 1d41be0..d086ab3 100644 --- a/exprtk_benchmark.cpp +++ b/exprtk_benchmark.cpp @@ -511,7 +511,7 @@ void perform_file_based_benchmark(const std::string& file_name, const std::size_ static_cast(i + 1), static_cast(expression_list.size()), (timer.time() * 1000000000.0) / (1.0 * rounds), - static_cast(timer.time() * 1000000000.0), + static_cast(timer.time() * 1000000000.0), sum, expr_str_list[i].c_str()); diff --git a/exprtk_simple_example_04.cpp b/exprtk_simple_example_04.cpp index e3d0d13..e87cf09 100644 --- a/exprtk_simple_example_04.cpp +++ b/exprtk_simple_example_04.cpp @@ -71,7 +71,7 @@ void fibonacci() x = i; T result = expression.value(); printf("fibonacci(%3d) = %10.0f\n", - static_cast(i), + static_cast(i), result); } }