C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html

This commit is contained in:
Arash Partow 2016-05-28 17:53:52 +10:00
parent 654bf59c36
commit 5300e5485c
2 changed files with 69 additions and 56 deletions

View File

@ -3753,24 +3753,35 @@ namespace exprtk
return current_token_; return current_token_;
} }
inline bool token_is(const token_t::token_type& ttype, const bool advance_token = true) enum token_advance_mode
{
e_hold = 0,
e_advance = 1
};
inline void advance_token(const token_advance_mode mode)
{
if (e_advance == mode)
{
next_token();
}
}
inline bool token_is(const token_t::token_type& ttype, const token_advance_mode mode = e_advance)
{ {
if (current_token().type != ttype) if (current_token().type != ttype)
{ {
return false; return false;
} }
if (advance_token) advance_token(mode);
{
next_token();
}
return true; return true;
} }
inline bool token_is(const token_t::token_type& ttype, inline bool token_is(const token_t::token_type& ttype,
const std::string& value, const std::string& value,
const bool advance_token = true) const token_advance_mode mode = e_advance)
{ {
if ( if (
(current_token().type != ttype) || (current_token().type != ttype) ||
@ -3780,17 +3791,14 @@ namespace exprtk
return false; return false;
} }
if (advance_token) advance_token(mode);
{
next_token();
}
return true; return true;
} }
inline bool token_is_then_assign(const token_t::token_type& ttype, inline bool token_is_then_assign(const token_t::token_type& ttype,
std::string& token, std::string& token,
const bool advance_token = true) const token_advance_mode mode = e_advance)
{ {
if (current_token_.type != ttype) if (current_token_.type != ttype)
{ {
@ -3799,10 +3807,7 @@ namespace exprtk
token = current_token_.value; token = current_token_.value;
if (advance_token) advance_token(mode);
{
next_token();
}
return true; return true;
} }
@ -16439,6 +16444,8 @@ namespace exprtk
typedef results_context<T> results_context_t; typedef results_context<T> results_context_t;
typedef parser_helper prsrhlpr_t;
struct scope_element struct scope_element
{ {
enum element_type enum element_type
@ -18498,7 +18505,7 @@ namespace exprtk
if (lexer().finished()) if (lexer().finished())
break; break;
else if (token_is(token_t::e_eof,false)) else if (token_is(token_t::e_eof,prsrhlpr_t::e_hold))
{ {
if (lexer().finished()) if (lexer().finished())
break; break;
@ -18774,7 +18781,10 @@ namespace exprtk
{ {
expression = new_expression; expression = new_expression;
if (token_is(token_t::e_ternary,false) && (precedence == e_level00)) if (
token_is(token_t::e_ternary,prsrhlpr_t::e_hold) &&
(precedence == e_level00)
)
{ {
expression = parse_ternary_conditional_statement(expression); expression = parse_ternary_conditional_statement(expression);
} }
@ -19342,7 +19352,7 @@ namespace exprtk
bool result = true; bool result = true;
if (token_is(token_t::e_lcrlbracket,false)) if (token_is(token_t::e_lcrlbracket,prsrhlpr_t::e_hold))
{ {
if (0 == (consequent = parse_multi_sequence("if-statement-01"))) if (0 == (consequent = parse_multi_sequence("if-statement-01")))
{ {
@ -19357,7 +19367,7 @@ namespace exprtk
{ {
if ( if (
settings_.commutative_check_enabled() && settings_.commutative_check_enabled() &&
token_is(token_t::e_mul,false) token_is(token_t::e_mul,prsrhlpr_t::e_hold)
) )
{ {
next_token(); next_token();
@ -19390,7 +19400,7 @@ namespace exprtk
{ {
next_token(); next_token();
if (token_is(token_t::e_lcrlbracket,false)) if (token_is(token_t::e_lcrlbracket,prsrhlpr_t::e_hold))
{ {
if (0 == (alternative = parse_multi_sequence("else-statement-01"))) if (0 == (alternative = parse_multi_sequence("else-statement-01")))
{ {
@ -19495,7 +19505,7 @@ namespace exprtk
return error_node(); return error_node();
} }
else if (token_is(token_t::e_comma,false)) else if (token_is(token_t::e_comma,prsrhlpr_t::e_hold))
{ {
// if (x,y,z) // if (x,y,z)
return parse_conditional_statement_01(condition); return parse_conditional_statement_01(condition);
@ -19873,13 +19883,13 @@ namespace exprtk
if (!token_is(token_t::e_eof)) if (!token_is(token_t::e_eof))
{ {
if ( if (
!token_is(token_t::e_symbol,false) && !token_is(token_t::e_symbol,prsrhlpr_t::e_hold) &&
details::imatch(current_token().value,"var") details::imatch(current_token().value,"var")
) )
{ {
next_token(); next_token();
if (!token_is(token_t::e_symbol,false)) if (!token_is(token_t::e_symbol,prsrhlpr_t::e_hold))
{ {
set_error( set_error(
make_error(parser_error::e_syntax, make_error(parser_error::e_syntax,
@ -20161,7 +20171,7 @@ namespace exprtk
expression_node_ptr default_statement = error_node(); expression_node_ptr default_statement = error_node();
if (token_is(token_t::e_lcrlbracket,false)) if (token_is(token_t::e_lcrlbracket,prsrhlpr_t::e_hold))
default_statement = parse_multi_sequence("switch-default"); default_statement = parse_multi_sequence("switch-default");
else else
default_statement = parse_expression(); default_statement = parse_expression();
@ -20290,7 +20300,7 @@ namespace exprtk
arg_list.push_back(consequent); arg_list.push_back(consequent);
} }
if (token_is(token_t::e_rcrlbracket,false)) if (token_is(token_t::e_rcrlbracket,prsrhlpr_t::e_hold))
{ {
break; break;
} }
@ -20352,6 +20362,7 @@ namespace exprtk
lodge_symbol(symbol,e_st_function); lodge_symbol(symbol,e_st_function);
next_token(); next_token();
if (!token_is(token_t::e_lbracket)) if (!token_is(token_t::e_lbracket))
{ {
set_error( set_error(
@ -20453,8 +20464,8 @@ namespace exprtk
(0 != expression) && (0 != expression) &&
(i++ < max_rangesize_parses) && (i++ < max_rangesize_parses) &&
error_list_.empty() && error_list_.empty() &&
token_is(token_t::e_lsqrbracket,false) && is_generally_string_node(expression) &&
is_generally_string_node(expression) token_is(token_t::e_lsqrbracket,prsrhlpr_t::e_hold)
) )
{ {
expression = parse_string_range_statement(expression); expression = parse_string_range_statement(expression);
@ -21798,9 +21809,9 @@ namespace exprtk
} }
if ( if (
!token_is(token_t::e_rbracket ,false) && !token_is(token_t::e_rbracket ,prsrhlpr_t::e_hold) &&
!token_is(token_t::e_rcrlbracket,false) && !token_is(token_t::e_rcrlbracket,prsrhlpr_t::e_hold) &&
!token_is(token_t::e_rsqrbracket,false) !token_is(token_t::e_rsqrbracket,prsrhlpr_t::e_hold)
) )
{ {
if (!token_is(token_t::e_eof)) if (!token_is(token_t::e_eof))
@ -22043,11 +22054,11 @@ namespace exprtk
return error_node(); return error_node();
} }
else if (token_is(token_t::e_lsqrbracket,false)) else if (token_is(token_t::e_lsqrbracket,prsrhlpr_t::e_hold))
{ {
return parse_define_vector_statement(var_name); return parse_define_vector_statement(var_name);
} }
else if (token_is(token_t::e_lcrlbracket,false)) else if (token_is(token_t::e_lcrlbracket,prsrhlpr_t::e_hold))
{ {
return parse_uninitialised_var_statement(var_name); return parse_uninitialised_var_statement(var_name);
} }
@ -22065,12 +22076,12 @@ namespace exprtk
} }
if ( if (
!token_is(token_t::e_rbracket ,false) && !token_is(token_t::e_rbracket ,prsrhlpr_t::e_hold) &&
!token_is(token_t::e_rcrlbracket,false) && !token_is(token_t::e_rcrlbracket,prsrhlpr_t::e_hold) &&
!token_is(token_t::e_rsqrbracket,false) !token_is(token_t::e_rsqrbracket,prsrhlpr_t::e_hold)
) )
{ {
if (!token_is(token_t::e_eof,false)) if (!token_is(token_t::e_eof,prsrhlpr_t::e_hold))
{ {
set_error( set_error(
make_error(parser_error::e_syntax, make_error(parser_error::e_syntax,
@ -22173,7 +22184,7 @@ namespace exprtk
return error_node(); return error_node();
} }
else if (!token_is(token_t::e_eof,false)) else if (!token_is(token_t::e_eof,prsrhlpr_t::e_hold))
{ {
set_error( set_error(
make_error(parser_error::e_syntax, make_error(parser_error::e_syntax,
@ -22268,7 +22279,7 @@ namespace exprtk
const std::string var0_name = current_token().value; const std::string var0_name = current_token().value;
if (!token_is(token_t::e_symbol,false)) if (!token_is(token_t::e_symbol,prsrhlpr_t::e_hold))
{ {
set_error( set_error(
make_error(parser_error::e_syntax, make_error(parser_error::e_syntax,
@ -22341,7 +22352,7 @@ namespace exprtk
const std::string var1_name = current_token().value; const std::string var1_name = current_token().value;
if (!token_is(token_t::e_symbol,false)) if (!token_is(token_t::e_symbol,prsrhlpr_t::e_hold))
{ {
set_error( set_error(
make_error(parser_error::e_syntax, make_error(parser_error::e_syntax,
@ -22604,21 +22615,23 @@ namespace exprtk
if (is_generally_string_node(branch)) if (is_generally_string_node(branch))
return true; return true;
const lexer::parser_helper::token_advance_mode hold = prsrhlpr_t::e_hold;
switch (token) switch (token)
{ {
case token_t::e_lcrlbracket : implied_mul = token_is(token_t::e_lbracket ,false) || case token_t::e_lcrlbracket : implied_mul = token_is(token_t::e_lbracket ,hold) ||
token_is(token_t::e_lcrlbracket,false) || token_is(token_t::e_lcrlbracket,hold) ||
token_is(token_t::e_lsqrbracket,false) ; token_is(token_t::e_lsqrbracket,hold) ;
break; break;
case token_t::e_lbracket : implied_mul = token_is(token_t::e_lbracket ,false) || case token_t::e_lbracket : implied_mul = token_is(token_t::e_lbracket ,hold) ||
token_is(token_t::e_lcrlbracket,false) || token_is(token_t::e_lcrlbracket,hold) ||
token_is(token_t::e_lsqrbracket,false) ; token_is(token_t::e_lsqrbracket,hold) ;
break; break;
case token_t::e_lsqrbracket : implied_mul = token_is(token_t::e_lbracket ,false) || case token_t::e_lsqrbracket : implied_mul = token_is(token_t::e_lbracket ,hold) ||
token_is(token_t::e_lcrlbracket,false) || token_is(token_t::e_lcrlbracket,hold) ||
token_is(token_t::e_lsqrbracket,false) ; token_is(token_t::e_lsqrbracket,hold) ;
break; break;
default : return true; default : return true;
@ -23151,7 +23164,7 @@ namespace exprtk
if ( if (
branch && branch &&
(e_level00 == precedence) && (e_level00 == precedence) &&
token_is(token_t::e_ternary,false) token_is(token_t::e_ternary,prsrhlpr_t::e_hold)
) )
{ {
branch = parse_ternary_conditional_statement(branch); branch = parse_ternary_conditional_statement(branch);
@ -33724,8 +33737,8 @@ namespace exprtk
namespace information namespace information
{ {
static const char* library = "Mathematical Expression Toolkit"; static const char* library = "Mathematical Expression Toolkit";
static const char* version = "2.718281828459045235360287471352662" static const char* version = "2.7182818284590452353602874713526624"
"49775724709369995957496696762772407"; "977572470936999595749669676277240766";
static const char* date = "20160606"; static const char* date = "20160606";
static inline std::string data() static inline std::string data()

View File

@ -291,7 +291,7 @@ of C++ compilers:
| nequal | Not-equal test between x and y using normalized epsilon | | nequal | Not-equal test between x and y using normalized epsilon |
+----------+---------------------------------------------------------+ +----------+---------------------------------------------------------+
| root | Nth-Root of x. where n is a positive integer. | | root | Nth-Root of x. where n is a positive integer. |
| | (eg: root(x,3)) | | | (eg: root(x,3) == x^(1/3)) |
+----------+---------------------------------------------------------+ +----------+---------------------------------------------------------+
| round | Round x to the nearest integer. (eg: round(x)) | | round | Round x to the nearest integer. (eg: round(x)) |
+----------+---------------------------------------------------------+ +----------+---------------------------------------------------------+
@ -302,7 +302,7 @@ of C++ compilers:
| sgn | Sign of x, -1 where x < 0, +1 where x > 0, else zero. | | sgn | Sign of x, -1 where x < 0, +1 where x > 0, else zero. |
| | (eg: sgn(x)) | | | (eg: sgn(x)) |
+----------+---------------------------------------------------------+ +----------+---------------------------------------------------------+
| sqrt | Square root of x, where x > 0. (eg: sqrt(x)) | | sqrt | Square root of x, where x >= 0. (eg: sqrt(x)) |
+----------+---------------------------------------------------------+ +----------+---------------------------------------------------------+
| sum | Sum of all the inputs. | | sum | Sum of all the inputs. |
| | (eg: sum(x,y,z,w,u,v,t) == (x + y + z + w + u + v + t)) | | | (eg: sum(x,y,z,w,u,v,t) == (x + y + z + w + u + v + t)) |