mirror of
https://github.com/ArashPartow/exprtk.git
synced 2025-06-22 16:28:57 +00:00
C++ Mathematical Expression Library (ExprTk) https://www.partow.net/programming/exprtk/index.html
This commit is contained in:
166
readme.txt
166
readme.txt
@ -115,7 +115,7 @@ Free use of the C++ Mathematical Expression Toolkit Library is
|
||||
permitted under the guidelines and in accordance with the most current
|
||||
version of the MIT License.
|
||||
|
||||
http://www.opensource.org/licenses/MIT
|
||||
https://www.opensource.org/licenses/MIT
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@ -707,7 +707,7 @@ the expression instance and to also allow the expression to be
|
||||
evaluated using the current value of the element.
|
||||
|
||||
Note: Any variable reference provided to a given symbol_table
|
||||
instance, must have a life time at least as long as the life-time of
|
||||
instance, must have a life-time at least as long as the life-time of
|
||||
the symbol_table instance. In the event the variable reference is
|
||||
invalidated before the symbol_table or any dependent expression
|
||||
instances have been destructed, then any associated expression
|
||||
@ -868,6 +868,66 @@ a false result due to one or more of the following reasons:
|
||||
6. The symbol_table instance is in an invalid state
|
||||
|
||||
|
||||
A further property of symbol tables is that they can be classified at
|
||||
instantiation as either being mutable (by default) or immutable. The
|
||||
following demonstrates construction of an immutable symbol table
|
||||
instance:
|
||||
|
||||
symbol_table_t immutable_symbol_table
|
||||
(symbol_table_t::symtab_mutability_type::e_immutable);
|
||||
|
||||
|
||||
When a symbol table, that has been constructed as being immutable, is
|
||||
registered with an expression, any statements in the expression string
|
||||
that modify the variables that are managed by the immutable symbol
|
||||
table will result in a compilation error. The operations that trigger
|
||||
the mutability constraint are the following assignment operators:
|
||||
|
||||
1. Assignment: :=
|
||||
2. Assign operation: +=, -=, *=, /= , %=
|
||||
|
||||
|
||||
The main reason for this functionality is that, one may want the
|
||||
immutability properties that come with constness of a variable such as
|
||||
scalars, vectors and strings, but not necessarily the accompanying
|
||||
compile time const-folding optimisations, that would result in the
|
||||
value of the variables being retrieved only once at compile time,
|
||||
causing external updates to the variables to not be part of the
|
||||
expression evaluation.
|
||||
|
||||
symbol_table_t immutable_symbol_table
|
||||
(symbol_table_t::symtab_mutability_type::e_immutable);
|
||||
|
||||
T x = 0.0;
|
||||
|
||||
const std::string expression_str = "x + (y + y)";
|
||||
|
||||
immutable_symbol_table.add_variable("x" , x );
|
||||
immutable_symbol_table.add_constant("y" , 123.0);
|
||||
|
||||
expression_t expression;
|
||||
expression.register_symbol_table(immutabile_symbol_table);
|
||||
|
||||
parser_t parser;
|
||||
parser.compile(expression_str, expression)
|
||||
|
||||
for (; x < 10.0; ++x)
|
||||
{
|
||||
const auto expected_value = x + (123.0 + 123.0);
|
||||
const auto result_value = expression.value();
|
||||
assert(expression.value() != expected_value);
|
||||
}
|
||||
|
||||
|
||||
In the above example, there are two variables X and Y. Where Y is a
|
||||
constant and X is a normal variable. Both are registered with a symbol
|
||||
table that is immutable. The expression when compiled will result in
|
||||
the "(y + y)" part being const-folded at compile time to the literal
|
||||
value of 246. Whereas the current value of X, being updated via the
|
||||
for-loop, externally to the expression and the symbol table will be
|
||||
available to the expression upon each evaluation.
|
||||
|
||||
|
||||
(2) Expression
|
||||
A structure that holds an Abstract Syntax Tree or AST for a specified
|
||||
expression and is used to evaluate said expression. Evaluation of the
|
||||
@ -1020,7 +1080,7 @@ may include user defined variables or functions. These are embedded as
|
||||
references into the expression's AST. When copying an expression, said
|
||||
references need to also be copied. If the references are blindly
|
||||
copied, it will then result in two or more identical expressions
|
||||
utilizing the exact same references for variables. This obviously is
|
||||
utilising the exact same references for variables. This obviously is
|
||||
not the default assumed scenario and will give rise to non-obvious
|
||||
behaviours when using the expressions in various contexts such as
|
||||
multi-threading et al.
|
||||
@ -1110,6 +1170,7 @@ enabled by default. The options and their explanations are as follows:
|
||||
(5) Sequence Check
|
||||
(6) Commutative Check
|
||||
(7) Strength Reduction Check
|
||||
(8) Stack And Node Depth Check
|
||||
|
||||
|
||||
(1) Replacer (e_replacer)
|
||||
@ -1218,6 +1279,43 @@ desired over the strength reduced form. In these situations it is best
|
||||
to turn off strength reduction optimisations or to use a type with a
|
||||
larger numerical bound.
|
||||
|
||||
|
||||
(8) Stack And Node Depth Check
|
||||
ExprTk incorporates a recursive descent parser. When parsing
|
||||
expressions comprising inner sub-expressions, the recursive nature of
|
||||
the parsing process causes the stack to grow. If the expression causes
|
||||
the stack to grow beyond the stack size limit, this would lead to a
|
||||
stackoverflow and its associated stack corruption and security
|
||||
vulnerability issues.
|
||||
|
||||
Similarly to parsing, evaluating an expression may cause the stack to
|
||||
grow. Such things like user defined functions, composite functions and
|
||||
the general nature of the AST being evaluated can cause the stack to
|
||||
grow, and may result in potential stackoverflow issues as denoted
|
||||
above.
|
||||
|
||||
ExprTk provides a set of checks that prevent both of the above denoted
|
||||
problems at compile time. These check rely on two specific limits
|
||||
being set on the parser instance, these limits are:
|
||||
|
||||
1. max_stack_depth (default: 400)
|
||||
2. max_node_depth (default: 10000)
|
||||
|
||||
|
||||
The following demonstrates how these two parser parameters can be set:
|
||||
|
||||
parser_t parser;
|
||||
|
||||
parser.set_max_stack_depth(100);
|
||||
parser.set_max_node_depth(200);
|
||||
|
||||
|
||||
In the above code, during parsing if the stack depth reaches or
|
||||
exceeds 100 levels, the parsing process will immediately halt and
|
||||
return with a failure. Similarly, during synthesizing the AST nodes,
|
||||
if the compilation process detects an AST tree depth exceeding 200
|
||||
levels the parsing process will halt and return a parsing failure.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
[SECTION 12 - EXPRESSION STRUCTURES]
|
||||
@ -1780,7 +1878,7 @@ value is not a vector but rather a single value.
|
||||
sum(x > 0 and x < 5) == x[]
|
||||
|
||||
|
||||
When utilizing external user defined vectors via the symbol table as
|
||||
When utilising external user defined vectors via the symbol table as
|
||||
opposed to expression local defined vectors, the typical 'add_vector'
|
||||
method from the symbol table will register the entirety of the vector
|
||||
that is passed. The following example attempts to evaluate the sum of
|
||||
@ -3489,7 +3587,7 @@ ExprTk reserved words, the add_function call will fail.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
[SECTION 20 - EXPRESSION RETURN VALUES]
|
||||
ExprTk expressions can return immediately from any point by utilizing
|
||||
ExprTk expressions can return immediately from any point by utilising
|
||||
the return call. Furthermore the return call can be used to transfer
|
||||
out multiple return values from within the expression.
|
||||
|
||||
@ -4591,52 +4689,58 @@ part of a compiler command line switch or scoped around the include to
|
||||
the ExprTk header. The defines are as follows:
|
||||
|
||||
(01) exprtk_enable_debugging
|
||||
(02) exprtk_disable_comments
|
||||
(03) exprtk_disable_break_continue
|
||||
(04) exprtk_disable_sc_andor
|
||||
(05) exprtk_disable_return_statement
|
||||
(06) exprtk_disable_enhanced_features
|
||||
(07) exprtk_disable_string_capabilities
|
||||
(08) exprtk_disable_superscalar_unroll
|
||||
(09) exprtk_disable_rtl_io_file
|
||||
(10) exprtk_disable_rtl_vecops
|
||||
(11) exprtk_disable_caseinsensitivity
|
||||
|
||||
(02) exprtk_disable_cardinal_pow_optimisation
|
||||
(03) exprtk_disable_comments
|
||||
(04) exprtk_disable_break_continue
|
||||
(05) exprtk_disable_sc_andor
|
||||
(06) exprtk_disable_return_statement
|
||||
(07) exprtk_disable_enhanced_features
|
||||
(08) exprtk_disable_string_capabilities
|
||||
(09) exprtk_disable_superscalar_unroll
|
||||
(10) exprtk_disable_rtl_io
|
||||
(11) exprtk_disable_rtl_io_file
|
||||
(12) exprtk_disable_rtl_vecops
|
||||
(13) exprtk_disable_caseinsensitivity
|
||||
(14) exprtk_enable_range_runtime_checks
|
||||
|
||||
(01) exprtk_enable_debugging
|
||||
This define will enable printing of debug information to stdout during
|
||||
the compilation process.
|
||||
|
||||
(02) exprtk_disable_comments
|
||||
(02) exprtk_disable_cardinal_pow_optimisation
|
||||
This define will disable the optimisation invoked when constant
|
||||
integers are used as powers in exponentiation expressions (eg: x^7).
|
||||
|
||||
(03) exprtk_disable_comments
|
||||
This define will disable the ability for expressions to have comments.
|
||||
Expressions that have comments when parsed with a build that has this
|
||||
option, will result in a compilation failure.
|
||||
|
||||
(03) exprtk_disable_break_continue
|
||||
(04) exprtk_disable_break_continue
|
||||
This define will disable the loop-wise 'break' and 'continue'
|
||||
capabilities. Any expression that contains those keywords will result
|
||||
in a compilation failure.
|
||||
|
||||
(04) exprtk_disable_sc_andor
|
||||
(05) exprtk_disable_sc_andor
|
||||
This define will disable the short-circuit '&' (and) and '|' (or)
|
||||
operators
|
||||
|
||||
(05) exprtk_disable_return_statement
|
||||
(06) exprtk_disable_return_statement
|
||||
This define will disable use of return statements within expressions.
|
||||
|
||||
(06) exprtk_disable_enhanced_features
|
||||
(07) exprtk_disable_enhanced_features
|
||||
This define will disable all enhanced features such as strength
|
||||
reduction and special function optimisations and expression specific
|
||||
type instantiations. This feature will reduce compilation times and
|
||||
binary sizes but will also result in massive performance degradation
|
||||
of expression evaluations.
|
||||
|
||||
(07) exprtk_disable_string_capabilities
|
||||
(08) exprtk_disable_string_capabilities
|
||||
This define will disable all string processing capabilities. Any
|
||||
expression that contains a string or string related syntax will result
|
||||
in a compilation failure.
|
||||
|
||||
(08) exprtk_disable_superscalar_unroll
|
||||
(09) exprtk_disable_superscalar_unroll
|
||||
This define will set the loop unroll batch size to 4 operations per
|
||||
loop instead of the default 8 operations. This define is used in
|
||||
operations that involve vectors and aggregations over vectors. When
|
||||
@ -4644,22 +4748,32 @@ targeting non-superscalar architectures, it may be recommended to
|
||||
build using this particular option if efficiency of evaluations is of
|
||||
concern.
|
||||
|
||||
(09) exprtk_disable_rtl_io_file
|
||||
(10) exprtk_disable_rtl_io
|
||||
This define will disable all of basic IO RTL package features. When
|
||||
present, any attempt to register the basic IO RTL package with a given
|
||||
symbol table will fail causing a compilation error.
|
||||
|
||||
(11) exprtk_disable_rtl_io_file
|
||||
This define will disable the file I/O RTL package features. When
|
||||
present, any attempts to register the file I/O package with a given
|
||||
symbol table will fail causing a compilation error.
|
||||
|
||||
(10) exprtk_disable_rtl_vecops
|
||||
(12) exprtk_disable_rtl_vecops
|
||||
This define will disable the extended vector operations RTL package
|
||||
features. When present, any attempts to register the vector operations
|
||||
package with a given symbol table will fail causing a compilation
|
||||
error.
|
||||
|
||||
(11) exprtk_disable_caseinsensitivity
|
||||
(13) exprtk_disable_caseinsensitivity
|
||||
This define will disable case-insensitivity when matching variables
|
||||
and functions. Furthermore all reserved and keywords will only be
|
||||
acknowledged when in all lower-case.
|
||||
|
||||
(14) exprtk_enable_range_runtime_checks
|
||||
This define will enable run-time checks pertaining to vector indexing
|
||||
operations used in any of the vector-to-vector and vector-to-scalar
|
||||
operations.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
[SECTION 28 - FILES]
|
||||
|
Reference in New Issue
Block a user