From fc1e23a8112f627e7697c8abd4a32f7924fc0cc8 Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Tue, 11 Oct 2016 12:46:39 +1100 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) https://www.partow.net/programming/exprtk/index.html --- exprtk.hpp | 252 +++++++++++++++++++++++++++++------------------------ readme.txt | 84 ++++++++++++++++-- 2 files changed, 218 insertions(+), 118 deletions(-) diff --git a/exprtk.hpp b/exprtk.hpp index 95d76f4..6aa8536 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -34923,116 +34923,6 @@ namespace exprtk return true; } - - namespace rtl - { - namespace io - { - namespace details - { - template - inline void print_type(const std::string& fmt, - const T v, - exprtk::details::numeric::details::real_type_tag) - { - printf(fmt.c_str(),v); - } - - template - struct print_impl - { - typedef typename igeneric_function::generic_type generic_type; - typedef typename igeneric_function::parameter_list_t parameter_list_t; - typedef typename generic_type::scalar_view scalar_t; - typedef typename generic_type::vector_view vector_t; - typedef typename generic_type::string_view string_t; - - static void process(const std::string& scalar_format, parameter_list_t parameters) - { - for (std::size_t i = 0; i < parameters.size(); ++i) - { - generic_type& gt = parameters[i]; - - typename exprtk::details::numeric::details::number_type::type num_type; - - switch (gt.type) - { - case generic_type::e_scalar : print_type(scalar_format,scalar_t(gt)(),num_type); - break; - - case generic_type::e_vector : { - vector_t vector(gt); - - for (std::size_t x = 0; x < vector.size(); ++x) - { - print_type(scalar_format,vector[x],num_type); - - if ((x + 1) < vector.size()) - printf(" "); - } - } - break; - - case generic_type::e_string : printf("%s",to_str(string_t(gt)).c_str()); - break; - - default : continue; - } - } - } - }; - - } // namespace exprtk::rtl::io::details - - template - struct print : public exprtk::igeneric_function - { - typedef typename igeneric_function::parameter_list_t parameter_list_t; - - using exprtk::igeneric_function::operator(); - - print(const std::string& scalar_format = "%10.5f") - : scalar_format_(scalar_format) - { - exprtk::enable_zero_parameters(*this); - } - - inline T operator()(parameter_list_t parameters) - { - details::print_impl::process(scalar_format_,parameters); - return T(0); - } - - std::string scalar_format_; - }; - - template - struct println : public exprtk::igeneric_function - { - typedef typename igeneric_function::parameter_list_t parameter_list_t; - - using exprtk::igeneric_function::operator(); - - println(const std::string& scalar_format = "%10.5f") - : scalar_format_(scalar_format) - { - exprtk::enable_zero_parameters(*this); - } - - inline T operator()(parameter_list_t parameters) - { - details::print_impl::process(scalar_format_,parameters); - printf("\n"); - return T(0); - } - - std::string scalar_format_; - }; - - - - } // namespace exprtk::rtl::io - } } #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) @@ -35052,7 +34942,6 @@ namespace exprtk namespace exprtk { - class timer { public: @@ -35148,6 +35037,142 @@ namespace exprtk } // namespace exprtk +#ifndef exprtk_disable_rtl_io +namespace exprtk +{ + namespace rtl { namespace io { namespace details + { + template + inline void print_type(const std::string& fmt, + const T v, + exprtk::details::numeric::details::real_type_tag) + { + printf(fmt.c_str(),v); + } + + template + struct print_impl + { + typedef typename igeneric_function::generic_type generic_type; + typedef typename igeneric_function::parameter_list_t parameter_list_t; + typedef typename generic_type::scalar_view scalar_t; + typedef typename generic_type::vector_view vector_t; + typedef typename generic_type::string_view string_t; + typedef typename exprtk::details::numeric::details::number_type::type num_type; + + static void process(const std::string& scalar_format, parameter_list_t parameters) + { + for (std::size_t i = 0; i < parameters.size(); ++i) + { + generic_type& gt = parameters[i]; + + switch (gt.type) + { + case generic_type::e_scalar : print(scalar_format,scalar_t(gt)); + break; + + case generic_type::e_vector : print(scalar_format,vector_t(gt)); + break; + + case generic_type::e_string : print(string_t(gt)); + break; + + default : continue; + } + } + } + + static inline void print(const std::string& scalar_format, const scalar_t& s) + { + print_type(scalar_format,s(),num_type()); + } + + static inline void print(const std::string& scalar_format, const vector_t& v) + { + for (std::size_t i = 0; i < v.size(); ++i) + { + print_type(scalar_format,v[i],num_type()); + + if ((i + 1) < v.size()) + printf(" "); + } + } + + static inline void print(const string_t& s) + { + printf("%s",to_str(s).c_str()); + } + }; + + } // namespace exprtk::rtl::io::details + + template + struct print : public exprtk::igeneric_function + { + typedef typename igeneric_function::parameter_list_t parameter_list_t; + + using exprtk::igeneric_function::operator(); + + print(const std::string& scalar_format = "%10.5f") + : scalar_format_(scalar_format) + { + exprtk::enable_zero_parameters(*this); + } + + inline T operator()(parameter_list_t parameters) + { + details::print_impl::process(scalar_format_,parameters); + return T(0); + } + + std::string scalar_format_; + }; + + template + struct println : public exprtk::igeneric_function + { + typedef typename igeneric_function::parameter_list_t parameter_list_t; + + using exprtk::igeneric_function::operator(); + + println(const std::string& scalar_format = "%10.5f") + : scalar_format_(scalar_format) + { + exprtk::enable_zero_parameters(*this); + } + + inline T operator()(parameter_list_t parameters) + { + details::print_impl::process(scalar_format_,parameters); + printf("\n"); + return T(0); + } + + std::string scalar_format_; + }; + + template + struct package + { + print p; + println pl; + + bool register_package(exprtk::symbol_table& symtab) + { + if (!symtab.add_function("print" ,p)) + return false; + else if (!symtab.add_function("println" ,pl)) + return false; + else + return true; + } + }; + + } // namespace exprtk::rtl::io + } // namespace exprtk::rtl +} // namespace exprtk +#endif + #ifndef exprtk_disable_rtl_io_file #include namespace exprtk @@ -35184,6 +35209,7 @@ namespace exprtk { file_name.clear(); delete stream; + return false; } else @@ -35199,6 +35225,7 @@ namespace exprtk { file_name.clear(); delete stream; + return false; } else @@ -35214,6 +35241,7 @@ namespace exprtk { file_name.clear(); delete stream; + return false; } else @@ -35299,8 +35327,6 @@ namespace exprtk case e_rdwrt : return (!!std::getline(*reinterpret_cast(stream_ptr),s)); default : return false; } - - return true; } bool eof() diff --git a/readme.txt b/readme.txt index d55e90d..4831116 100644 --- a/readme.txt +++ b/readme.txt @@ -2668,7 +2668,81 @@ via the 'unknown symbol resolver' mechanism. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -[20 - EXPRTK NOTES] +[20 - RUNTIME LIBRARY PACKAGES] +ExprTk contains a set of simple extensions, that provide +functionalities beyond basic numerical calculations. Currently the +available packages are: + + +---+--------------------+-----------------------------------+ + | # | Package Name | Namespace/Type | + +---+--------------------+-----------------------------------+ + | 1 | Basic I/O | exprtk::rtl::io::package | + | 2 | File I/O | exprtk::rtl::io::file::package | + | 3 | Vector Operations | exprtk::rtl::vecops::package | + +---+--------------------+-----------------------------------+ + + +In order to make the features of a specific package available within +an expression, an instance of the package must be added to the +expression's associated symbol table. In the following example, the +file I/O package is made available for the given expression: + + typedef exprtk::symbol_table symbol_table_t; + typedef exprtk::expression expression_t; + typedef exprtk::parser parser_t; + + exprtk::rtl::io::file::package fileio_package; + + std::string expression_string = + " var file_name := 'file.txt'; " + " var stream := null; " + " " + " stream := open(file_name,'w'); " + " " + " write(stream,'Hello world....\n'); " + " " + " close(stream); " + " "; + + symbol_table_t symbol_table; + symbol_table.add_package(fileio_package); + + expression_t expression; + expression.register_symbol_table(symbol_table); + + parser_t parser; + parser.compile(expression_string,expression); + + expression.value(); + + +(1) Basic I/O functions: + + (a) print + (b) println + +(2) File I/O functions: + + (a) open (b) close + (c) write (d) read + (e) getline (f) eof + +(3) Vector Operations functions: + + (a) all_true (b) all_false + (c) any_true (d) any_false + (e) count (f) copy + (g) rotate-left (h) rotate-right + (i) shift-left (j) shift-right + (k) sort (l) sumk + (m) axpy (n) axpby + (o) axpyz (p) axpbyz + (q) axpbz (r) dot + (s) dotk + + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +[21 - EXPRTK NOTES] The following is a list of facts and suggestions one may want to take into account when using ExprTk: @@ -2869,7 +2943,7 @@ into account when using ExprTk: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -[21 - SIMPLE EXPRTK EXAMPLE] +[22 - SIMPLE EXPRTK EXAMPLE] The following is a simple yet complete example demonstrating typical usage of the ExprTk Library. The example instantiates a symbol table object, adding to it three variables named x, y and z, and a custom @@ -2974,7 +3048,7 @@ int main() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -[22 - BUILD OPTIONS] +[23 - BUILD OPTIONS] When building ExprTk there are a number of defines that will enable or disable certain features and capabilities. The defines can either be part of a compiler command line switch or scoped around the include to @@ -3041,7 +3115,7 @@ error. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -[23 - FILES] +[24 - FILES] The source distribution of ExprTk is comprised of the following set of files: @@ -3071,7 +3145,7 @@ files: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -[24 - LANGUAGE STRUCTURE] +[25 - LANGUAGE STRUCTURE] +-------------------------------------------------------------+ |00 - If Statement | | |