diff --git a/Makefile b/Makefile index d2b94e6..ed07b04 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,7 @@ BUILD_LIST+=exprtk_simple_example_09 BUILD_LIST+=exprtk_simple_example_10 BUILD_LIST+=exprtk_simple_example_11 BUILD_LIST+=exprtk_simple_example_12 +BUILD_LIST+=exprtk_simple_example_13 all: $(BUILD_LIST) @@ -81,6 +82,9 @@ exprtk_simple_example_11: exprtk_simple_example_11.cpp exprtk.hpp 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) + pgo: exprtk_test.cpp exprtk_benchmark.cpp exprtk.hpp $(COMPILER) $(BASE_OPTIONS) -O3 -march=native -fprofile-generate -o exprtk_benchmark exprtk_benchmark.cpp $(LINKER_OPT) ./exprtk_benchmark @@ -101,6 +105,7 @@ strip_bin: strip -s exprtk_simple_example_10 strip -s exprtk_simple_example_11 strip -s exprtk_simple_example_12 + strip -s exprtk_simple_example_13 valgrind_check: valgrind --leak-check=full --show-reachable=yes --track-origins=yes --log-file=exprtk_test_valgrind.log -v ./exprtk_test @@ -117,6 +122,7 @@ valgrind_check: 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 clean: rm -f core.* *~ *.o *.bak *stackdump gmon.out *.gcda *.gcno *.gcnor *.gch diff --git a/exprtk.hpp b/exprtk.hpp index d426fb5..7322a55 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -13030,7 +13030,11 @@ namespace exprtk next_token(); break; } - else if (!token_is(seperator)) + + bool is_next_until = peek_token_is(token_t::e_symbol) && + peek_token_is("until"); + + if (!token_is(seperator) && is_next_until) { set_error( make_error(parser_error::e_syntax, @@ -15324,6 +15328,11 @@ namespace exprtk return (lexer_.peek_next_token().type == ttype); } + inline bool peek_token_is(const std::string& s) + { + return (details::imatch(lexer_.peek_next_token().value,s)); + } + template class expression_generator { diff --git a/exprtk_simple_example_01.cpp b/exprtk_simple_example_01.cpp index 33d4c5d..259eebc 100644 --- a/exprtk_simple_example_01.cpp +++ b/exprtk_simple_example_01.cpp @@ -36,7 +36,7 @@ void trig_function() exprtk::parser parser; parser.compile(expression_string,expression); - for (x = T(-5.0); x <= T(+5.0); x += 0.001) + for (x = T(-5); x <= T(+5); x += T(0.001)) { T y = expression.value(); printf("%19.15f\t%19.15f\n",x,y); diff --git a/exprtk_simple_example_04.cpp b/exprtk_simple_example_04.cpp index 99c7df6..e3d0d13 100644 --- a/exprtk_simple_example_04.cpp +++ b/exprtk_simple_example_04.cpp @@ -32,12 +32,15 @@ void fibonacci() compositor_t compositor; compositor - .add("fibonacci_impl", - "switch " - "{ " - " case x == 0 : 0; " - " case x == 1 : 1; " - " default : " + .add("fibonacci", + " var w := 0; " + " var y := 0; " + " var z := 1; " + " switch " + " { " + " case x == 0 : 0; " + " case x == 1 : 1; " + " default : " " while ((x -= 1) > 0) " " { " " w := z; " @@ -45,12 +48,7 @@ void fibonacci() " y := w; " " z " " }; " - "} ", - "x","y","z","w"); - - compositor - .add("fibonacci", - "fibonacci_impl(x,0,1,0)", + " } ", "x"); T x = T(0); diff --git a/exprtk_simple_example_10.cpp b/exprtk_simple_example_10.cpp index 76dcf8e..3af2fc2 100644 --- a/exprtk_simple_example_10.cpp +++ b/exprtk_simple_example_10.cpp @@ -39,29 +39,25 @@ void newton_sqrt() compositor_t compositor(symbol_table); - compositor - .add("newton_sqrt_impl", - "switch " - "{ " - " case x < 0 : -inf; " - " case x == 0 : 0; " - " case x == 1 : 1; " - " default: " - " ~{ " - " z := 100; " - " y := x / 2; " - " repeat " - " y := (1 / 2) * (y + (x / y)); " - " if (equal(y * y,x)) " - " break[y]; " - " until ((z -= 1) <= 0); " - " }; " - "} ", - "x","y","z"); - compositor .add("newton_sqrt", - "newton_sqrt_impl(x,0,0)","x"); + " switch " + " { " + " case x < 0 : -inf; " + " case x == 0 : 0; " + " case x == 1 : 1; " + " default: " + " ~{ " + " var z := 100; " + " var y := x / 2; " + " repeat " + " y := (1 / 2) * (y + (x / y)); " + " if (equal(y * y,x)) " + " break[y]; " + " until ((z -= 1) <= 0); " + " }; " + " } ", + "x"); std::string expression_str = "newton_sqrt(x)"; diff --git a/exprtk_simple_example_11.cpp b/exprtk_simple_example_11.cpp index ceb5f69..e6d74a8 100644 --- a/exprtk_simple_example_11.cpp +++ b/exprtk_simple_example_11.cpp @@ -29,7 +29,7 @@ void square_wave2() typedef exprtk::parser parser_t; std::string wave_program = - " r := 0; " + " var r := 0; " " for(i := 0; i < 1000; i += 1) " " { " " r += (1 / (2i + 1)) * sin((4i + 2) * pi * f * t); " @@ -52,7 +52,6 @@ void square_wave2() expression.register_symbol_table(symbol_table); parser_t parser; - parser.enable_unknown_symbol_resolver(); parser.compile(wave_program,expression); diff --git a/exprtk_simple_example_13.cpp b/exprtk_simple_example_13.cpp new file mode 100644 index 0000000..192096d --- /dev/null +++ b/exprtk_simple_example_13.cpp @@ -0,0 +1,98 @@ +/* + ************************************************************** + * C++ Mathematical Expression Toolkit Library * + * * + * Simple Example 13 * + * Author: Arash Partow (1999-2014) * + * URL: http://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 Common Public License. * + * http://www.opensource.org/licenses/cpl1.0.php * + * * + ************************************************************** +*/ + + +#include +#include +#include +#include +#include "exprtk.hpp" + + +template +void savitzky_golay_filter() +{ + typedef exprtk::symbol_table symbol_table_t; + typedef exprtk::expression expression_t; + typedef exprtk::parser parser_t; + + std::string gsfilter_program = + " var weight[9] := " + " { " + " -21, 14, 39, " + " 54, 59, 54, " + " 39, 14, -21 " + " }; " + " " + " var lower_bound := trunc(weight[] / 2); " + " var upper_bound := v_in[] - lower_bound; " + " " + " if (v_in[] >= weight[]) " + " { " + " for (i := lower_bound; i < upper_bound; i += 1) " + " { " + " v_out[i] := 0; " + " for (j := 0; j < weight[]; j += 1) " + " { " + " v_out[i] += weight[j] * v_in[i + j]; " + " }; " + " v_out[i] /= 231; " + " }; " + " } "; + + const std::size_t n = 1024; + + std::vector v_in; + std::vector v_out; + + const T pi = T(3.141592653589793238462); + + srand(time(0)); + + // Generate a signal with noise. + for (T t = T(-5); t <= T(+5); t += T(10.0 / n)) + { + T noise = T(0.5 * (rand() / (RAND_MAX + 1.0) - 0.5)); + v_in.push_back(sin(2.0 * pi * t) + noise); + } + + v_out.resize(v_in.size()); + + symbol_table_t symbol_table; + symbol_table.add_vector("v_in" , v_in); + symbol_table.add_vector("v_out",v_out); + + expression_t expression; + expression.register_symbol_table(symbol_table); + + parser_t parser; + + parser.compile(gsfilter_program,expression); + + expression.value(); + + for (std::size_t i = 0; i < v_out.size(); ++i) + { + printf("%10.6f\t%10.6f\n",v_in[i],v_out[i]); + } +} + +int main() +{ + savitzky_golay_filter(); + return 0; +} diff --git a/readme.txt b/readme.txt index 14e26b6..748df97 100644 --- a/readme.txt +++ b/readme.txt @@ -968,7 +968,7 @@ example defines a vararg function called 'boo': For the above denoted custom functions to be used in an expression, an instance of each function needs to be registered with a symbol_table that has been associated with the expression instance. The following -demonstrations how all the pieces are put together: +demonstrates how all the pieces are put together: typedef exprtk::symbol_table symbol_table_t; typedef exprtk::expression expression_t; @@ -1242,6 +1242,7 @@ int main() (14) exprtk_simple_example_10.cpp (15) exprtk_simple_example_11.cpp (16) exprtk_simple_example_12.cpp +(17) exprtk_simple_example_13.cpp