/exprtk.hpp

http://nanocalc.codeplex.com · C++ Header · 32723 lines · 27008 code · 5320 blank · 395 comment · 4550 complexity · 304dd65fa1664a3781c0e72ef5a01b4d MD5 · raw file

  1. /*
  2. ******************************************************************
  3. * C++ Mathematical Expression Toolkit Library *
  4. * *
  5. * Author: Arash Partow (1999-2015) *
  6. * URL: http://www.partow.net/programming/exprtk/index.html *
  7. * *
  8. * Copyright notice: *
  9. * Free use of the C++ Mathematical Expression Toolkit Library is *
  10. * permitted under the guidelines and in accordance with the most *
  11. * current version of the Common Public License. *
  12. * http://www.opensource.org/licenses/cpl1.0.php *
  13. * *
  14. * Example expressions: *
  15. * (00) (y + x / y) * (x - y / x) *
  16. * (01) (x^2 / sin(2 * pi / y)) - x / 2 *
  17. * (02) sqrt(1 - (x^2)) *
  18. * (03) 1 - sin(2 * x) + cos(pi / y) *
  19. * (04) a * exp(2 * t) + c *
  20. * (05) if(((x + 2) == 3) and ((y + 5) <= 9),1 + w, 2 / z) *
  21. * (06) (avg(x,y) <= x + y ? x - y : x * y) + 2 * pi / x *
  22. * (07) z := x + sin(2 * pi / y) *
  23. * (08) u := 2 * (pi * z) / (w := x + cos(y / pi)) *
  24. * (09) clamp(-1,sin(2 * pi * x) + cos(y / 2 * pi),+1) *
  25. * (10) inrange(-2,m,+2) == if(({-2 <= m} and [m <= +2]),1,0) *
  26. * (11) (2sin(x)cos(2y)7 + 1) == (2 * sin(x) * cos(2*y) * 7 + 1) *
  27. * (12) (x ilike 's*ri?g') and [y < (3 z^7 + w)] *
  28. * *
  29. ******************************************************************
  30. */
  31. #ifndef INCLUDE_EXPRTK_HPP
  32. #define INCLUDE_EXPRTK_HPP
  33. #include <algorithm>
  34. #include <cctype>
  35. #include <cmath>
  36. #include <complex>
  37. #include <cstdio>
  38. #include <cstdlib>
  39. #include <deque>
  40. #include <exception>
  41. #include <functional>
  42. #include <iterator>
  43. #include <limits>
  44. #include <list>
  45. #include <map>
  46. #include <set>
  47. #include <stack>
  48. #include <stdexcept>
  49. #include <string>
  50. #include <utility>
  51. #include <vector>
  52. namespace exprtk
  53. {
  54. #if exprtk_enable_debugging
  55. #define exprtk_debug(params) printf params
  56. #else
  57. #define exprtk_debug(params) (void)0
  58. #endif
  59. namespace details
  60. {
  61. inline bool is_whitespace(const char c)
  62. {
  63. return (' ' == c) || ('\n' == c) ||
  64. ('\r' == c) || ('\t' == c) ||
  65. ('\b' == c) || ('\v' == c) ||
  66. ('\f' == c) ;
  67. }
  68. inline bool is_operator_char(const char c)
  69. {
  70. return ('+' == c) || ('-' == c) ||
  71. ('*' == c) || ('/' == c) ||
  72. ('^' == c) || ('<' == c) ||
  73. ('>' == c) || ('=' == c) ||
  74. (',' == c) || ('!' == c) ||
  75. ('(' == c) || (')' == c) ||
  76. ('[' == c) || (']' == c) ||
  77. ('{' == c) || ('}' == c) ||
  78. ('%' == c) || (':' == c) ||
  79. ('?' == c) || ('&' == c) ||
  80. ('|' == c) || (';' == c) ;
  81. }
  82. inline bool is_letter(const char c)
  83. {
  84. return (('a' <= c) && (c <= 'z')) ||
  85. (('A' <= c) && (c <= 'Z')) ;
  86. }
  87. inline bool is_digit(const char c)
  88. {
  89. return ('0' <= c) && (c <= '9');
  90. }
  91. inline bool is_letter_or_digit(const char c)
  92. {
  93. return is_letter(c) || is_digit(c);
  94. }
  95. inline bool is_left_bracket(const char c)
  96. {
  97. return ('(' == c) || ('[' == c) || ('{' == c);
  98. }
  99. inline bool is_right_bracket(const char c)
  100. {
  101. return (')' == c) || (']' == c) || ('}' == c);
  102. }
  103. inline bool is_bracket(const char c)
  104. {
  105. return is_left_bracket(c) || is_right_bracket(c);
  106. }
  107. inline bool is_sign(const char c)
  108. {
  109. return ('+' == c) || ('-' == c);
  110. }
  111. inline bool is_invalid(const char c)
  112. {
  113. return !is_whitespace (c) &&
  114. !is_operator_char(c) &&
  115. !is_letter (c) &&
  116. !is_digit (c) &&
  117. ('.' != c) &&
  118. ('_' != c) &&
  119. ('$' != c) &&
  120. ('~' != c) &&
  121. ('\'' != c);
  122. }
  123. inline bool imatch(const char c1, const char c2)
  124. {
  125. return std::tolower(c1) == std::tolower(c2);
  126. }
  127. inline bool imatch(const std::string& s1, const std::string& s2)
  128. {
  129. if (s1.size() == s2.size())
  130. {
  131. for (std::size_t i = 0; i < s1.size(); ++i)
  132. {
  133. if (std::tolower(s1[i]) != std::tolower(s2[i]))
  134. {
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. return false;
  141. }
  142. inline bool is_valid_sf_symbol(const std::string& symbol)
  143. {
  144. // Special function: $f12 or $F34
  145. return (4 == symbol.size()) &&
  146. ('$' == symbol[0]) &&
  147. imatch('f',symbol[1]) &&
  148. is_digit(symbol[2]) &&
  149. is_digit(symbol[3]);
  150. }
  151. inline const char& front(const std::string& s)
  152. {
  153. return s[0];
  154. }
  155. inline const char& back(const std::string& s)
  156. {
  157. return s[s.size() - 1];
  158. }
  159. inline std::string to_str(int i)
  160. {
  161. if (0 == i)
  162. return std::string("0");
  163. std::string result;
  164. if (i < 0)
  165. {
  166. for ( ; i; i /= 10)
  167. {
  168. result += '0' + char(-(i % 10));
  169. }
  170. result += '-';
  171. }
  172. else
  173. {
  174. for ( ; i; i /= 10)
  175. {
  176. result += '0' + char(i % 10);
  177. }
  178. }
  179. std::reverse(result.begin(), result.end());
  180. return result;
  181. }
  182. inline bool is_hex_digit(const std::string::value_type digit)
  183. {
  184. return (('0' <= digit) && (digit <= '9')) ||
  185. (('A' <= digit) && (digit <= 'F')) ||
  186. (('a' <= digit) && (digit <= 'f')) ;
  187. }
  188. inline unsigned char hex_to_bin(unsigned char h)
  189. {
  190. if (('0' <= h) && (h <= '9'))
  191. return (h - '0');
  192. else
  193. return (std::toupper(h) - 'A');
  194. }
  195. template <typename Iterator>
  196. inline void parse_hex(Iterator& itr, Iterator end, std::string::value_type& result)
  197. {
  198. if (
  199. (end != (itr )) &&
  200. (end != (itr + 1)) &&
  201. (end != (itr + 2)) &&
  202. (end != (itr + 3)) &&
  203. ('0' == *(itr )) &&
  204. (
  205. ('x' == *(itr + 1)) ||
  206. ('X' == *(itr + 1))
  207. ) &&
  208. (is_hex_digit(*(itr + 2))) &&
  209. (is_hex_digit(*(itr + 3)))
  210. )
  211. {
  212. result = hex_to_bin(*(itr + 2)) << 4 | hex_to_bin(*(itr + 3));
  213. itr += 3;
  214. }
  215. else
  216. result = '\0';
  217. }
  218. inline void cleanup_escapes(std::string& s)
  219. {
  220. typedef std::string::iterator str_itr_t;
  221. str_itr_t itr1 = s.begin();
  222. str_itr_t itr2 = s.begin();
  223. str_itr_t end = s.end ();
  224. std::size_t removal_count = 0;
  225. while (end != itr1)
  226. {
  227. if ('\\' == (*itr1))
  228. {
  229. ++removal_count;
  230. if (end == ++itr1)
  231. break;
  232. else if ('\\' != (*itr1))
  233. {
  234. switch (*itr1)
  235. {
  236. case 'n' : (*itr1) = '\n'; break;
  237. case 'r' : (*itr1) = '\r'; break;
  238. case 't' : (*itr1) = '\t'; break;
  239. case '0' : parse_hex(itr1, end, (*itr1));
  240. removal_count += 3;
  241. break;
  242. }
  243. continue;
  244. }
  245. }
  246. if (itr1 != itr2)
  247. {
  248. (*itr2) = (*itr1);
  249. }
  250. ++itr1;
  251. ++itr2;
  252. }
  253. s.resize(s.size() - removal_count);
  254. }
  255. class build_string
  256. {
  257. public:
  258. build_string(const std::size_t& initial_size = 64)
  259. {
  260. data_.reserve(initial_size);
  261. }
  262. inline build_string& operator << (const std::string& s)
  263. {
  264. data_ += s;
  265. return (*this);
  266. }
  267. inline build_string& operator << (const char* s)
  268. {
  269. data_ += std::string(s);
  270. return (*this);
  271. }
  272. inline operator std::string () const
  273. {
  274. return data_;
  275. }
  276. inline std::string as_string() const
  277. {
  278. return data_;
  279. }
  280. private:
  281. std::string data_;
  282. };
  283. struct ilesscompare
  284. {
  285. inline bool operator()(const std::string& s1, const std::string& s2) const
  286. {
  287. const std::size_t length = std::min(s1.size(),s2.size());
  288. for (std::size_t i = 0; i < length; ++i)
  289. {
  290. const char c1 = static_cast<char>(std::tolower(s1[i]));
  291. const char c2 = static_cast<char>(std::tolower(s2[i]));
  292. if (c1 > c2)
  293. return false;
  294. else if (c1 < c2)
  295. return true;
  296. }
  297. return s1.size() < s2.size();
  298. }
  299. };
  300. static const std::string reserved_words[] =
  301. {
  302. "break", "case", "continue", "default", "false", "for",
  303. "if", "else", "ilike", "in", "like", "and", "nand", "nor",
  304. "not", "null", "or", "repeat", "return", "shl", "shr",
  305. "swap", "switch", "true", "until", "var", "while", "xnor",
  306. "xor", "&", "|"
  307. };
  308. static const std::size_t reserved_words_size = sizeof(reserved_words) / sizeof(std::string);
  309. static const std::string reserved_symbols[] =
  310. {
  311. "abs", "acos", "acosh", "and", "asin", "asinh", "atan",
  312. "atanh", "atan2", "avg", "break", "case", "ceil", "clamp",
  313. "continue", "cos", "cosh", "cot", "csc", "default",
  314. "deg2grad", "deg2rad", "equal", "erf", "erfc", "exp",
  315. "expm1", "false", "floor", "for", "frac", "grad2deg",
  316. "hypot", "iclamp", "if", "else", "ilike", "in", "inrange",
  317. "like", "log", "log10", "log2", "logn", "log1p", "mand",
  318. "max", "min", "mod", "mor", "mul", "ncdf", "nand", "nor",
  319. "not", "not_equal", "null", "or", "pow", "rad2deg",
  320. "repeat", "return", "root", "round", "roundn", "sec", "sgn",
  321. "shl", "shr", "sin", "sinc", "sinh", "sqrt", "sum", "swap",
  322. "switch", "tan", "tanh", "true", "trunc", "until", "var",
  323. "while", "xnor", "xor", "&", "|"
  324. };
  325. static const std::size_t reserved_symbols_size = sizeof(reserved_symbols) / sizeof(std::string);
  326. static const std::string base_function_list[] =
  327. {
  328. "abs", "acos", "acosh", "asin", "asinh", "atan", "atanh",
  329. "atan2", "avg", "ceil", "clamp", "cos", "cosh", "cot",
  330. "csc", "equal", "erf", "erfc", "exp", "expm1", "floor",
  331. "frac", "hypot", "iclamp", "like", "log", "log10", "log2",
  332. "logn", "log1p", "mand", "max", "min", "mod", "mor", "mul",
  333. "ncdf", "pow", "root", "round", "roundn", "sec", "sgn",
  334. "sin", "sinc", "sinh", "sqrt", "sum", "swap", "tan", "tanh",
  335. "trunc", "not_equal", "inrange", "deg2grad", "deg2rad",
  336. "rad2deg", "grad2deg"
  337. };
  338. static const std::size_t base_function_list_size = sizeof(base_function_list) / sizeof(std::string);
  339. static const std::string logic_ops_list[] =
  340. {
  341. "and", "nand", "nor", "not", "or", "xnor", "xor", "&", "|"
  342. };
  343. static const std::size_t logic_ops_list_size = sizeof(logic_ops_list) / sizeof(std::string);
  344. static const std::string cntrl_struct_list[] =
  345. {
  346. "for", "if", "repeat", "switch", "while"
  347. };
  348. static const std::size_t cntrl_struct_list_size = sizeof(cntrl_struct_list) / sizeof(std::string);
  349. inline bool is_reserved_word(const std::string& symbol)
  350. {
  351. for (std::size_t i = 0; i < reserved_words_size; ++i)
  352. {
  353. if (imatch(symbol,reserved_words[i]))
  354. {
  355. return true;
  356. }
  357. }
  358. return false;
  359. }
  360. inline bool is_reserved_symbol(const std::string& symbol)
  361. {
  362. for (std::size_t i = 0; i < reserved_symbols_size; ++i)
  363. {
  364. if (imatch(symbol,reserved_symbols[i]))
  365. {
  366. return true;
  367. }
  368. }
  369. return false;
  370. }
  371. inline bool is_base_function(const std::string& function_name)
  372. {
  373. for (std::size_t i = 0; i < base_function_list_size; ++i)
  374. {
  375. if (imatch(function_name,base_function_list[i]))
  376. {
  377. return true;
  378. }
  379. }
  380. return false;
  381. }
  382. inline bool is_control_struct(const std::string& cntrl_strct)
  383. {
  384. for (std::size_t i = 0; i < cntrl_struct_list_size; ++i)
  385. {
  386. if (imatch(cntrl_strct,cntrl_struct_list[i]))
  387. {
  388. return true;
  389. }
  390. }
  391. return false;
  392. }
  393. inline bool is_logic_opr(const std::string& lgc_opr)
  394. {
  395. for (std::size_t i = 0; i < cntrl_struct_list_size; ++i)
  396. {
  397. if (imatch(lgc_opr,logic_ops_list[i]))
  398. {
  399. return true;
  400. }
  401. }
  402. return false;
  403. }
  404. struct cs_match
  405. {
  406. static inline bool cmp(const char c0, const char c1)
  407. {
  408. return (c0 == c1);
  409. }
  410. };
  411. struct cis_match
  412. {
  413. static inline bool cmp(const char c0, const char c1)
  414. {
  415. return (std::tolower(c0) == std::tolower(c1));
  416. }
  417. };
  418. template <typename Iterator, typename Compare>
  419. inline bool match_impl(const Iterator pattern_begin,
  420. const Iterator pattern_end,
  421. const Iterator data_begin,
  422. const Iterator data_end,
  423. const typename std::iterator_traits<Iterator>::value_type& zero_or_more,
  424. const typename std::iterator_traits<Iterator>::value_type& zero_or_one)
  425. {
  426. if (0 == std::distance(data_begin,data_end))
  427. {
  428. return false;
  429. }
  430. Iterator d_itr = data_begin;
  431. Iterator p_itr = pattern_begin;
  432. Iterator c_itr = data_begin;
  433. Iterator m_itr = data_begin;
  434. while ((data_end != d_itr) && (zero_or_more != (*p_itr)))
  435. {
  436. if ((!Compare::cmp((*p_itr),(*d_itr))) && (zero_or_one != (*p_itr)))
  437. {
  438. return false;
  439. }
  440. ++p_itr;
  441. ++d_itr;
  442. }
  443. while (data_end != d_itr)
  444. {
  445. if (zero_or_more == (*p_itr))
  446. {
  447. if (pattern_end == (++p_itr))
  448. {
  449. return true;
  450. }
  451. m_itr = p_itr;
  452. c_itr = d_itr;
  453. ++c_itr;
  454. }
  455. else if ((Compare::cmp((*p_itr),(*d_itr))) || (zero_or_one == (*p_itr)))
  456. {
  457. ++p_itr;
  458. ++d_itr;
  459. }
  460. else
  461. {
  462. p_itr = m_itr;
  463. d_itr = c_itr++;
  464. }
  465. }
  466. while ((p_itr != pattern_end) && (zero_or_more == (*p_itr))) { ++p_itr; }
  467. return (p_itr == pattern_end);
  468. }
  469. inline bool wc_match(const std::string& wild_card,
  470. const std::string& str)
  471. {
  472. return match_impl<const char*,cs_match>(wild_card.data(),
  473. wild_card.data() + wild_card.size(),
  474. str.data(),
  475. str.data() + str.size(),
  476. '*',
  477. '?');
  478. }
  479. inline bool wc_imatch(const std::string& wild_card,
  480. const std::string& str)
  481. {
  482. return match_impl<const char*,cis_match>(wild_card.data(),
  483. wild_card.data() + wild_card.size(),
  484. str.data(),
  485. str.data() + str.size(),
  486. '*',
  487. '?');
  488. }
  489. inline bool sequence_match(const std::string& pattern,
  490. const std::string& str,
  491. std::size_t& diff_index,
  492. char& diff_value)
  493. {
  494. if (str.empty() || pattern.empty())
  495. return false;
  496. else if ('*' == pattern[0])
  497. return false;
  498. typedef std::string::const_iterator itr_t;
  499. itr_t p_itr = pattern.begin();
  500. itr_t s_itr = str .begin();
  501. itr_t p_end = pattern.end();
  502. itr_t s_end = str .end();
  503. while ((s_end != s_itr) && (p_end != p_itr))
  504. {
  505. if ('*' == (*p_itr))
  506. {
  507. const char target = std::toupper(*(p_itr - 1));
  508. if ('*' == target)
  509. {
  510. diff_index = std::distance(str.begin(),s_itr);
  511. diff_value = std::toupper(*p_itr);
  512. return false;
  513. }
  514. else
  515. ++p_itr;
  516. while (s_itr != s_end)
  517. {
  518. if (target != std::toupper(*s_itr))
  519. break;
  520. else
  521. ++s_itr;
  522. }
  523. continue;
  524. }
  525. else if (
  526. ('?' != *p_itr) &&
  527. std::toupper(*p_itr) != std::toupper(*s_itr)
  528. )
  529. {
  530. diff_index = std::distance(str.begin(),s_itr);
  531. diff_value = std::toupper(*p_itr);
  532. return false;
  533. }
  534. ++p_itr;
  535. ++s_itr;
  536. }
  537. return (
  538. (s_end == s_itr) &&
  539. (
  540. (p_end == p_itr) ||
  541. ('*' == *p_itr)
  542. )
  543. );
  544. }
  545. static const double pow10[] = {
  546. 1.0,
  547. 1.0E+001, 1.0E+002, 1.0E+003, 1.0E+004,
  548. 1.0E+005, 1.0E+006, 1.0E+007, 1.0E+008,
  549. 1.0E+009, 1.0E+010, 1.0E+011, 1.0E+012,
  550. 1.0E+013, 1.0E+014, 1.0E+015, 1.0E+016
  551. };
  552. static const std::size_t pow10_size = sizeof(pow10) / sizeof(double);
  553. namespace numeric
  554. {
  555. namespace constant
  556. {
  557. static const double e = 2.718281828459045235360;
  558. static const double pi = 3.141592653589793238462;
  559. static const double pi_2 = 1.570796326794896619231;
  560. static const double pi_4 = 0.785398163397448309616;
  561. static const double pi_180 = 0.017453292519943295769;
  562. static const double _1_pi = 0.318309886183790671538;
  563. static const double _2_pi = 0.636619772367581343076;
  564. static const double _180_pi = 57.295779513082320876798;
  565. static const double log2 = 0.693147180559945309417;
  566. static const double sqrt2 = 1.414213562373095048801;
  567. }
  568. namespace details
  569. {
  570. struct unknown_type_tag {};
  571. struct real_type_tag {};
  572. struct complex_type_tag {};
  573. struct int_type_tag {};
  574. template <typename T>
  575. struct number_type { typedef unknown_type_tag type; };
  576. #define exprtk_register_real_type_tag(T) \
  577. template<> struct number_type<T> { typedef real_type_tag type; }; \
  578. #define exprtk_register_complex_type_tag(T) \
  579. template<> struct number_type<std::complex<T> > \
  580. { typedef complex_type_tag type; }; \
  581. #define exprtk_register_int_type_tag(T) \
  582. template<> struct number_type<T> { typedef int_type_tag type; }; \
  583. exprtk_register_real_type_tag(double )
  584. exprtk_register_real_type_tag(long double)
  585. exprtk_register_real_type_tag(float )
  586. exprtk_register_complex_type_tag(double )
  587. exprtk_register_complex_type_tag(long double)
  588. exprtk_register_complex_type_tag(float )
  589. exprtk_register_int_type_tag(short )
  590. exprtk_register_int_type_tag(int )
  591. exprtk_register_int_type_tag(long long int )
  592. exprtk_register_int_type_tag(unsigned short )
  593. exprtk_register_int_type_tag(unsigned int )
  594. exprtk_register_int_type_tag(unsigned long long int)
  595. #undef exprtk_register_real_type_tag
  596. #undef exprtk_register_int_type_tag
  597. template <typename T>
  598. struct epsilon_type
  599. {
  600. static inline T value()
  601. {
  602. const T epsilon = T(0.0000000001);
  603. return epsilon;
  604. }
  605. };
  606. template <>
  607. struct epsilon_type <float>
  608. {
  609. static inline float value()
  610. {
  611. const float epsilon = float(0.000001f);
  612. return epsilon;
  613. }
  614. };
  615. template <>
  616. struct epsilon_type <long double>
  617. {
  618. static inline long double value()
  619. {
  620. const long double epsilon = (long double)(0.000000000001);
  621. return epsilon;
  622. }
  623. };
  624. template <typename T>
  625. inline bool is_nan_impl(const T v, real_type_tag)
  626. {
  627. return std::not_equal_to<T>()(v,v);
  628. }
  629. template <typename T>
  630. inline int to_int32_impl(const T v, real_type_tag)
  631. {
  632. return static_cast<int>(v);
  633. }
  634. template <typename T>
  635. inline long long int to_int64_impl(const T v, real_type_tag)
  636. {
  637. return static_cast<long long int>(v);
  638. }
  639. template <typename T>
  640. inline bool is_true_impl(const T v)
  641. {
  642. return std::not_equal_to<T>()(T(0),v);
  643. }
  644. template <typename T>
  645. inline bool is_false_impl(const T v)
  646. {
  647. return std::equal_to<T>()(T(0),v);
  648. }
  649. template <typename T>
  650. inline T abs_impl(const T v, real_type_tag)
  651. {
  652. return ((v >= T(0)) ? v : -v);
  653. }
  654. template <typename T>
  655. inline T min_impl(const T v0, const T v1, real_type_tag)
  656. {
  657. return std::min<T>(v0,v1);
  658. }
  659. template <typename T>
  660. inline T max_impl(const T v0, const T v1, real_type_tag)
  661. {
  662. return std::max<T>(v0,v1);
  663. }
  664. template <typename T>
  665. inline T equal_impl(const T v0, const T v1, real_type_tag)
  666. {
  667. const T epsilon = epsilon_type<T>::value();
  668. return (abs_impl(v0 - v1,real_type_tag()) <= (std::max(T(1),std::max(abs_impl(v0,real_type_tag()),abs_impl(v1,real_type_tag()))) * epsilon)) ? T(1) : T(0);
  669. }
  670. inline float equal_impl(const float v0, const float v1, real_type_tag)
  671. {
  672. const float epsilon = epsilon_type<float>::value();
  673. return (abs_impl(v0 - v1,real_type_tag()) <= (std::max(1.0f,std::max(abs_impl(v0,real_type_tag()),abs_impl(v1,real_type_tag()))) * epsilon)) ? 1.0f : 0.0f;
  674. }
  675. template <typename T>
  676. inline T equal_impl(const T v0, const T v1, int_type_tag)
  677. {
  678. return (v0 == v1) ? 1 : 0;
  679. }
  680. template <typename T>
  681. inline T expm1_impl(const T v, real_type_tag)
  682. {
  683. // return std::expm1<T>(v);
  684. if (abs_impl(v,real_type_tag()) < T(0.00001))
  685. return v + (T(0.5) * v * v);
  686. else
  687. return std::exp(v) - T(1);
  688. }
  689. template <typename T>
  690. inline T expm1_impl(const T v, int_type_tag)
  691. {
  692. return T(std::exp<double>(v)) - T(1);
  693. }
  694. template <typename T>
  695. inline T nequal_impl(const T v0, const T v1, real_type_tag)
  696. {
  697. typedef real_type_tag rtg;
  698. const T epsilon = epsilon_type<T>::value();
  699. return (abs_impl(v0 - v1,rtg()) > (std::max(T(1),std::max(abs_impl(v0,rtg()),abs_impl(v1,rtg()))) * epsilon)) ? T(1) : T(0);
  700. }
  701. inline float nequal_impl(const float v0, const float v1, real_type_tag)
  702. {
  703. typedef real_type_tag rtg;
  704. const float epsilon = epsilon_type<float>::value();
  705. return (abs_impl(v0 - v1,rtg()) > (std::max(1.0f,std::max(abs_impl(v0,rtg()),abs_impl(v1,rtg()))) * epsilon)) ? 1.0f : 0.0f;
  706. }
  707. template <typename T>
  708. inline T nequal_impl(const T v0, const T v1, int_type_tag)
  709. {
  710. return (v0 != v1) ? 1 : 0;
  711. }
  712. template <typename T>
  713. inline T modulus_impl(const T v0, const T v1, real_type_tag)
  714. {
  715. return std::fmod(v0,v1);
  716. }
  717. template <typename T>
  718. inline T modulus_impl(const T v0, const T v1, int_type_tag)
  719. {
  720. return v0 % v1;
  721. }
  722. template <typename T>
  723. inline T pow_impl(const T v0, const T v1, real_type_tag)
  724. {
  725. return std::pow(v0,v1);
  726. }
  727. template <typename T>
  728. inline T pow_impl(const T v0, const T v1, int_type_tag)
  729. {
  730. return std::pow(static_cast<double>(v0),static_cast<double>(v1));
  731. }
  732. template <typename T>
  733. inline T logn_impl(const T v0, const T v1, real_type_tag)
  734. {
  735. return std::log(v0) / std::log(v1);
  736. }
  737. template <typename T>
  738. inline T logn_impl(const T v0, const T v1, int_type_tag)
  739. {
  740. return static_cast<T>(logn_impl<double>(static_cast<double>(v0),static_cast<double>(v1),real_type_tag()));
  741. }
  742. template <typename T>
  743. inline T log1p_impl(const T v, real_type_tag)
  744. {
  745. if (v > T(-1))
  746. {
  747. if (abs_impl(v,real_type_tag()) > T(0.0001))
  748. {
  749. return std::log(T(1) + v);
  750. }
  751. else
  752. return (T(-0.5) * v + T(1)) * v;
  753. }
  754. else
  755. return std::numeric_limits<T>::quiet_NaN();
  756. }
  757. template <typename T>
  758. inline T log1p_impl(const T v, int_type_tag)
  759. {
  760. if (v > T(-1))
  761. {
  762. return std::log(T(1) + v);
  763. }
  764. else
  765. return std::numeric_limits<T>::quiet_NaN();
  766. }
  767. template <typename T>
  768. inline T root_impl(const T v0, const T v1, real_type_tag)
  769. {
  770. return std::pow(v0,T(1) / v1);
  771. }
  772. template <typename T>
  773. inline T root_impl(const T v0, const T v1, int_type_tag)
  774. {
  775. return root_impl<double>(static_cast<double>(v0),static_cast<double>(v1),real_type_tag());
  776. }
  777. template <typename T>
  778. inline T round_impl(const T v, real_type_tag)
  779. {
  780. return ((v < T(0)) ? std::ceil(v - T(0.5)) : std::floor(v + T(0.5)));
  781. }
  782. template <typename T>
  783. inline T roundn_impl(const T v0, const T v1, real_type_tag)
  784. {
  785. const int index = std::max<int>(0, std::min<int>(pow10_size - 1, (int)std::floor(v1)));
  786. const T p10 = T(pow10[index]);
  787. if (v0 < T(0))
  788. return T(std::ceil ((v0 * p10) - T(0.5)) / p10);
  789. else
  790. return T(std::floor((v0 * p10) + T(0.5)) / p10);
  791. }
  792. template <typename T>
  793. inline T roundn_impl(const T v0, const T, int_type_tag)
  794. {
  795. return v0;
  796. }
  797. template <typename T>
  798. inline T hypot_impl(const T v0, const T v1, real_type_tag)
  799. {
  800. return std::sqrt((v0 * v0) + (v1 * v1));
  801. }
  802. template <typename T>
  803. inline T hypot_impl(const T v0, const T v1, int_type_tag)
  804. {
  805. return static_cast<T>(std::sqrt(static_cast<double>((v0 * v0) + (v1 * v1))));
  806. }
  807. template <typename T>
  808. inline T atan2_impl(const T v0, const T v1, real_type_tag)
  809. {
  810. return std::atan2(v0,v1);
  811. }
  812. template <typename T>
  813. inline T atan2_impl(const T, const T, int_type_tag)
  814. {
  815. return 0;
  816. }
  817. template <typename T>
  818. inline T shr_impl(const T v0, const T v1, real_type_tag)
  819. {
  820. return v0 * (T(1) / std::pow(T(2),static_cast<T>(static_cast<int>(v1))));
  821. }
  822. template <typename T>
  823. inline T shr_impl(const T v0, const T v1, int_type_tag)
  824. {
  825. return v0 >> v1;
  826. }
  827. template <typename T>
  828. inline T shl_impl(const T v0, const T v1, real_type_tag)
  829. {
  830. return v0 * std::pow(T(2),static_cast<T>(static_cast<int>(v1)));
  831. }
  832. template <typename T>
  833. inline T shl_impl(const T v0, const T v1, int_type_tag)
  834. {
  835. return v0 << v1;
  836. }
  837. template <typename T>
  838. inline T sgn_impl(const T v, real_type_tag)
  839. {
  840. if (v > T(0)) return T(+1);
  841. else if (v < T(0)) return T(-1);
  842. else return T( 0);
  843. }
  844. template <typename T>
  845. inline T sgn_impl(const T v, int_type_tag)
  846. {
  847. if (v > T(0)) return T(+1);
  848. else if (v < T(0)) return T(-1);
  849. else return T( 0);
  850. }
  851. template <typename T>
  852. inline T and_impl(const T v0, const T v1, real_type_tag)
  853. {
  854. return (is_true_impl(v0) && is_true_impl(v1)) ? T(1) : T(0);
  855. }
  856. template <typename T>
  857. inline T and_impl(const T v0, const T v1, int_type_tag)
  858. {
  859. return v0 && v1;
  860. }
  861. template <typename T>
  862. inline T nand_impl(const T v0, const T v1, real_type_tag)
  863. {
  864. return (is_false_impl(v0) || is_false_impl(v1)) ? T(1) : T(0);
  865. }
  866. template <typename T>
  867. inline T nand_impl(const T v0, const T v1, int_type_tag)
  868. {
  869. return !(v0 && v1);
  870. }
  871. template <typename T>
  872. inline T or_impl(const T v0, const T v1, real_type_tag)
  873. {
  874. return (is_true_impl(v0) || is_true_impl(v1)) ? T(1) : T(0);
  875. }
  876. template <typename T>
  877. inline T or_impl(const T v0, const T v1, int_type_tag)
  878. {
  879. return (v0 || v1);
  880. }
  881. template <typename T>
  882. inline T nor_impl(const T v0, const T v1, real_type_tag)
  883. {
  884. return (is_false_impl(v0) && is_false_impl(v1)) ? T(1) : T(0);
  885. }
  886. template <typename T>
  887. inline T nor_impl(const T v0, const T v1, int_type_tag)
  888. {
  889. return !(v0 || v1);
  890. }
  891. template <typename T>
  892. inline T xor_impl(const T v0, const T v1, real_type_tag)
  893. {
  894. return (is_false_impl(v0) != is_false_impl(v1)) ? T(1) : T(0);
  895. }
  896. template <typename T>
  897. inline T xor_impl(const T v0, const T v1, int_type_tag)
  898. {
  899. return v0 ^ v1;
  900. }
  901. template <typename T>
  902. inline T xnor_impl(const T v0, const T v1, real_type_tag)
  903. {
  904. const bool v0_true = is_true_impl(v0);
  905. const bool v1_true = is_true_impl(v1);
  906. if ((v0_true && v1_true) || (!v0_true && !v1_true))
  907. return T(1);
  908. else
  909. return T(0);
  910. }
  911. template <typename T>
  912. inline T xnor_impl(const T v0, const T v1, int_type_tag)
  913. {
  914. const bool v0_true = is_true_impl(v0);
  915. const bool v1_true = is_true_impl(v1);
  916. if ((v0_true && v1_true) || (!v0_true && !v1_true))
  917. return T(1);
  918. else
  919. return T(0);
  920. }
  921. template <typename T>
  922. inline T erf_impl(T v, real_type_tag)
  923. {
  924. #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  925. // Credits: Abramowitz & Stegun Equations 7.1.25-28
  926. const T t = T(1) / (T(1) + T(0.5) * abs_impl(v,real_type_tag()));
  927. static const T c[] = {
  928. T( 1.26551223), T(1.00002368),
  929. T( 0.37409196), T(0.09678418),
  930. T(-0.18628806), T(0.27886807),
  931. T(-1.13520398), T(1.48851587),
  932. T(-0.82215223), T(0.17087277)
  933. };
  934. T result = T(1) - t * std::exp((-v * v) -
  935. c[0] + t * (c[1] + t *
  936. (c[2] + t * (c[3] + t *
  937. (c[4] + t * (c[5] + t *
  938. (c[6] + t * (c[7] + t *
  939. (c[8] + t * (c[9]))))))))));
  940. return (v >= T(0)) ? result : -result;
  941. #else
  942. return ::erf(v);
  943. #endif
  944. }
  945. template <typename T>
  946. inline T erf_impl(T v, int_type_tag)
  947. {
  948. return erf_impl(static_cast<double>(v),real_type_tag());
  949. }
  950. template <typename T>
  951. inline T erfc_impl(T v, real_type_tag)
  952. {
  953. #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  954. return T(1) - erf_impl(v,real_type_tag());
  955. #else
  956. return ::erfc(v);
  957. #endif
  958. }
  959. template <typename T>
  960. inline T erfc_impl(T v, int_type_tag)
  961. {
  962. return erfc_impl(static_cast<double>(v),real_type_tag());
  963. }
  964. template <typename T>
  965. inline T ncdf_impl(T v, real_type_tag)
  966. {
  967. T cnd = T(0.5) * (T(1) + erf_impl(
  968. abs_impl(v,real_type_tag()) /
  969. T(numeric::constant::sqrt2),real_type_tag()));
  970. return (v < T(0)) ? (T(1) - cnd) : cnd;
  971. }
  972. template <typename T>
  973. inline T ncdf_impl(T v, int_type_tag)
  974. {
  975. return ncdf_impl(static_cast<double>(v),real_type_tag());
  976. }
  977. template <typename T>
  978. inline T sinc_impl(T v, real_type_tag)
  979. {
  980. if (std::abs(v) >= std::numeric_limits<T>::epsilon())
  981. return(std::sin(v) / v);
  982. else
  983. return T(1);
  984. }
  985. template <typename T>
  986. inline T sinc_impl(T v, int_type_tag)
  987. {
  988. return sinc_impl(static_cast<double>(v),real_type_tag());
  989. }
  990. template <typename T> inline T acos_impl(const T v, real_type_tag) { return std::acos (v); }
  991. template <typename T> inline T acosh_impl(const T v, real_type_tag) { return std::log(v + std::sqrt((v * v) - T(1))); }
  992. template <typename T> inline T asin_impl(const T v, real_type_tag) { return std::asin (v); }
  993. template <typename T> inline T asinh_impl(const T v, real_type_tag) { return std::log(v + std::sqrt((v * v) + T(1))); }
  994. template <typename T> inline T atan_impl(const T v, real_type_tag) { return std::atan (v); }
  995. template <typename T> inline T atanh_impl(const T v, real_type_tag) { return (std::log(T(1) + v) - log(T(1) - v)) / T(2); }
  996. template <typename T> inline T ceil_impl(const T v, real_type_tag) { return std::ceil (v); }
  997. template <typename T> inline T cos_impl(const T v, real_type_tag) { return std::cos (v); }
  998. template <typename T> inline T cosh_impl(const T v, real_type_tag) { return std::cosh (v); }
  999. template <typename T> inline T exp_impl(const T v, real_type_tag) { return std::exp (v); }
  1000. template <typename T> inline T floor_impl(const T v, real_type_tag) { return std::floor(v); }
  1001. template <typename T> inline T log_impl(const T v, real_type_tag) { return std::log (v); }
  1002. template <typename T> inline T log10_impl(const T v, real_type_tag) { return std::log10(v); }
  1003. template <typename T> inline T log2_impl(const T v, real_type_tag) { return std::log(v)/T(numeric::constant::log2); }
  1004. template <typename T> inline T neg_impl(const T v, real_type_tag) { return -v; }
  1005. template <typename T> inline T pos_impl(const T v, real_type_tag) { return +v; }
  1006. template <typename T> inline T sin_impl(const T v, real_type_tag) { return std::sin (v); }
  1007. template <typename T> inline T sinh_impl(const T v, real_type_tag) { return std::sinh (v); }
  1008. template <typename T> inline T sqrt_impl(const T v, real_type_tag) { return std::sqrt (v); }
  1009. template <typename T> inline T tan_impl(const T v, real_type_tag) { return std::tan (v); }
  1010. template <typename T> inline T tanh_impl(const T v, real_type_tag) { return std::tanh (v); }
  1011. template <typename T> inline T cot_impl(const T v, real_type_tag) { return T(1) / std::tan(v); }
  1012. template <typename T> inline T sec_impl(const T v, real_type_tag) { return T(1) / std::cos(v); }
  1013. template <typename T> inline T csc_impl(const T v, real_type_tag) { return T(1) / std::sin(v); }
  1014. template <typename T> inline T r2d_impl(const T v, real_type_tag) { return (v * T(numeric::constant::_180_pi)); }
  1015. template <typename T> inline T d2r_impl(const T v, real_type_tag) { return (v * T(numeric::constant::pi_180)); }
  1016. template <typename T> inline T d2g_impl(const T v, real_type_tag) { return (v * T(20.0/9.0)); }
  1017. template <typename T> inline T g2d_impl(const T v, real_type_tag) { return (v * T(9.0/20.0)); }
  1018. template <typename T> inline T notl_impl(const T v, real_type_tag) { return (std::not_equal_to<T>()(T(0),v) ? T(0) : T(1)); }
  1019. template <typename T> inline T frac_impl(const T v, real_type_tag) { return (v - static_cast<long long>(v)); }
  1020. template <typename T> inline T trunc_impl(const T v, real_type_tag) { return T(static_cast<long long>(v)); }
  1021. template <typename T> inline T abs_impl(const T v, int_type_tag) { return ((v >= T(0)) ? v : -v); }
  1022. template <typename T> inline T exp_impl(const T v, int_type_tag) { return std::exp (v); }
  1023. template <typename T> inline T log_impl(const T v, int_type_tag) { return std::log (v); }
  1024. template <typename T> inline T log10_impl(const T v, int_type_tag) { return std::log10(v); }
  1025. template <typename T> inline T log2_impl(const T v, int_type_tag) { return std::log(v)/T(numeric::constant::log2); }
  1026. template <typename T> inline T neg_impl(const T v, int_type_tag) { return -v; }
  1027. template <typename T> inline T pos_impl(const T v, int_type_tag) { return +v; }
  1028. template <typename T> inline T ceil_impl(const T v, int_type_tag) { return v; }
  1029. template <typename T> inline T floor_impl(const T v, int_type_tag) { return v; }
  1030. template <typename T> inline T round_impl(const T v, int_type_tag) { return v; }
  1031. template <typename T> inline T notl_impl(const T v, int_type_tag) { return !v; }
  1032. template <typename T> inline T sqrt_impl(const T v, int_type_tag) { return std::sqrt (v); }
  1033. template <typename T> inline T frac_impl(const T , int_type_tag) { return T(0); }
  1034. template <typename T> inline T trunc_impl(const T v, int_type_tag) { return v; }
  1035. template <typename T> inline T acos_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1036. template <typename T> inline T acosh_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1037. template <typename T> inline T asin_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1038. template <typename T> inline T asinh_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1039. template <typename T> inline T atan_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1040. template <typename T> inline T atanh_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1041. template <typename T> inline T cos_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1042. template <typename T> inline T cosh_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1043. template <typename T> inline T sin_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1044. template <typename T> inline T sinh_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1045. template <typename T> inline T tan_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1046. template <typename T> inline T tanh_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1047. template <typename T> inline T cot_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1048. template <typename T> inline T sec_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1049. template <typename T> inline T csc_impl(const T , int_type_tag) { return std::numeric_limits<T>::quiet_NaN(); }
  1050. template <typename T>
  1051. inline bool is_integer_impl(const T& v, real_type_tag)
  1052. {
  1053. return std::equal_to<T>()(T(0),std::fmod(v,T(1)));
  1054. }
  1055. template <typename T>
  1056. inline bool is_integer_impl(const T&, int_type_tag)
  1057. {
  1058. return true;
  1059. }
  1060. }
  1061. template <typename Type>
  1062. struct numeric_info { enum { length = 0, size = 32, bound_length = 0, min_exp = 0, max_exp = 0 }; };
  1063. template<> struct numeric_info<int> { enum { length = 10, size = 16, bound_length = 9}; };
  1064. template<> struct numeric_info<float> { enum { min_exp = -38, max_exp = +38}; };
  1065. template<> struct numeric_info<double> { enum { min_exp = -308, max_exp = +308}; };
  1066. template<> struct numeric_info<long double> { enum { min_exp = -308, max_exp = +308}; };
  1067. template <typename T>
  1068. inline int to_int32(const T v)
  1069. {
  1070. typename details::number_type<T>::type num_type;
  1071. return to_int32_impl(v,num_type);
  1072. }
  1073. template <typename T>
  1074. inline long long int to_int64(const T v)
  1075. {
  1076. typename details::number_type<T>::type num_type;
  1077. return to_int64_impl(v,num_type);
  1078. }
  1079. template <typename T>
  1080. inline bool is_nan(const T v)
  1081. {
  1082. typename details::number_type<T>::type num_type;
  1083. return is_nan_impl(v,num_type);
  1084. }
  1085. template <typename T>
  1086. inline T min(const T v0, const T v1)
  1087. {
  1088. typename details::number_type<T>::type num_type;
  1089. return min_impl(v0,v1,num_type);
  1090. }
  1091. template <typename T>
  1092. inline T max(const T v0, const T v1)
  1093. {
  1094. typename details::number_type<T>::type num_type;
  1095. return max_impl(v0,v1,num_type);
  1096. }
  1097. template <typename T>
  1098. inline T equal(const T v0, const T v1)
  1099. {
  1100. typename details::number_type<T>::type num_type;
  1101. return equal_impl(v0,v1,num_type);
  1102. }
  1103. template <typename T>
  1104. inline T nequal(const T v0, const T v1)
  1105. {
  1106. typename details::number_type<T>::type num_type;
  1107. return nequal_impl(v0,v1,num_type);
  1108. }
  1109. template <typename T>
  1110. inline T modulus(const T v0, const T v1)
  1111. {
  1112. typename details::number_type<T>::type num_type;
  1113. return modulus_impl(v0,v1,num_type);
  1114. }
  1115. template <typename T>
  1116. inline T pow(const T v0, const T v1)
  1117. {
  1118. typename details::number_type<T>::type num_type;
  1119. return pow_impl(v0,v1,num_type);
  1120. }
  1121. template <typename T>
  1122. inline T logn(const T v0, const T v1)
  1123. {
  1124. typename details::number_type<T>::type num_type;
  1125. return logn_impl(v0,v1,num_type);
  1126. }
  1127. template <typename T>
  1128. inline T root(const T v0, const T v1)
  1129. {
  1130. typename details::number_type<T>::type num_type;
  1131. return root_impl(v0,v1,num_type);
  1132. }
  1133. template <typename T>
  1134. inline T roundn(const T v0, const T v1)
  1135. {
  1136. typename details::number_type<T>::type num_type;
  1137. return roundn_impl(v0,v1,num_type);
  1138. }
  1139. template <typename T>
  1140. inline T hypot(const T v0, const T v1)
  1141. {
  1142. typename details::number_type<T>::type num_type;
  1143. return hypot_impl(v0,v1,num_type);
  1144. }
  1145. template <typename T>
  1146. inline T atan2(const T v0, const T v1)
  1147. {
  1148. typename details::number_type<T>::type num_type;
  1149. return atan2_impl(v0,v1,num_type);
  1150. }
  1151. template <typename T>
  1152. inline T shr(const T v0, const T v1)
  1153. {
  1154. typename details::number_type<T>::type num_type;
  1155. return shr_impl(v0,v1,num_type);
  1156. }
  1157. template <typename T>
  1158. inline T shl(const T v0, const T v1)
  1159. {
  1160. typename details::number_type<T>::type num_type;
  1161. return shl_impl(v0,v1,num_type);
  1162. }
  1163. template <typename T>
  1164. inline T and_opr(const T v0, const T v1)
  1165. {
  1166. typename details::number_type<T>::type num_type;
  1167. return and_impl(v0,v1,num_type);
  1168. }
  1169. template <typename T>
  1170. inline T nand_opr(const T v0, const T v1)
  1171. {
  1172. typename details::number_type<T>::type num_type;
  1173. return nand_impl(v0,v1,num_type);
  1174. }
  1175. template <typename T>
  1176. inline T or_opr(const T v0, const T v1)
  1177. {
  1178. typename details::number_type<T>::type num_type;
  1179. return or_impl(v0,v1,num_type);
  1180. }
  1181. template <typename T>
  1182. inline T nor_opr(const T v0, const T v1)
  1183. {
  1184. typename details::number_type<T>::type num_type;
  1185. return nor_impl(v0,v1,num_type);
  1186. }
  1187. template <typename T>
  1188. inline T xor_opr(const T v0, const T v1)
  1189. {
  1190. typename details::number_type<T>::type num_type;
  1191. return xor_impl(v0,v1,num_type);
  1192. }
  1193. template <typename T>
  1194. inline T xnor_opr(const T v0, const T v1)
  1195. {
  1196. typename details::number_type<T>::type num_type;
  1197. return xnor_impl(v0,v1,num_type);
  1198. }
  1199. template <typename T>
  1200. inline bool is_integer(const T v)
  1201. {
  1202. typename details::number_type<T>::type num_type;
  1203. return is_integer_impl(v,num_type);
  1204. }
  1205. template <typename T, unsigned int N>
  1206. struct fast_exp
  1207. {
  1208. static inline T result(T v)
  1209. {
  1210. unsigned int k = N;
  1211. T l = T(1);
  1212. while (k)
  1213. {
  1214. if (k & 1)
  1215. {
  1216. l *= v;
  1217. --k;
  1218. }
  1219. v *= v;
  1220. k >>= 1;
  1221. }
  1222. return l;
  1223. }
  1224. };
  1225. template <typename T> struct fast_exp<T,10> { static inline T result(T v) { T v_5 = fast_exp<T,5>::result(v); return v_5 * v_5; } };
  1226. template <typename T> struct fast_exp<T, 9> { static inline T result(T v) { return fast_exp<T,8>::result(v) * v; } };
  1227. template <typename T> struct fast_exp<T, 8> { static inline T result(T v) { T v_4 = fast_exp<T,4>::result(v); return v_4 * v_4; } };
  1228. template <typename T> struct fast_exp<T, 7> { static inline T result(T v) { return fast_exp<T,6>::result(v) * v; } };
  1229. template <typename T> struct fast_exp<T, 6> { static inline T result(T v) { T v_3 = fast_exp<T,3>::result(v); return v_3 * v_3; } };
  1230. template <typename T> struct fast_exp<T, 5> { static inline T result(T v) { return fast_exp<T,4>::result(v) * v; } };
  1231. template <typename T> struct fast_exp<T, 4> { static inline T result(T v) { T v_2 = v * v; return v_2 * v_2; } };
  1232. template <typename T> struct fast_exp<T, 3> { static inline T result(T v) { return v * v * v; } };
  1233. template <typename T> struct fast_exp<T, 2> { static inline T result(T v) { return v * v; } };
  1234. template <typename T> struct fast_exp<T, 1> { static inline T result(T v) { return v; } };
  1235. template <typename T> struct fast_exp<T, 0> { static inline T result(T ) { return T(1); } };
  1236. #define exprtk_define_unary_function(FunctionName) \
  1237. template <typename T> \
  1238. inline T FunctionName (const T v) \
  1239. { \
  1240. typename details::number_type<T>::type num_type; \
  1241. return FunctionName##_impl(v,num_type); \
  1242. } \
  1243. exprtk_define_unary_function(abs )
  1244. exprtk_define_unary_function(acos )
  1245. exprtk_define_unary_function(acosh)
  1246. exprtk_define_unary_function(asin )
  1247. exprtk_define_unary_function(asinh)
  1248. exprtk_define_unary_function(atan )
  1249. exprtk_define_unary_function(atanh)
  1250. exprtk_define_unary_function(ceil )
  1251. exprtk_define_unary_function(cos )
  1252. exprtk_define_unary_function(cosh )
  1253. exprtk_define_unary_function(exp )
  1254. exprtk_define_unary_function(expm1)
  1255. exprtk_define_unary_function(floor)
  1256. exprtk_define_unary_function(log )
  1257. exprtk_define_unary_function(log10)
  1258. exprtk_define_unary_function(log2 )
  1259. exprtk_define_unary_function(log1p)
  1260. exprtk_define_unary_function(neg )
  1261. exprtk_define_unary_function(pos )
  1262. exprtk_define_unary_function(round)
  1263. exprtk_define_unary_function(sin )
  1264. exprtk_define_unary_function(sinc )
  1265. exprtk_define_unary_function(sinh )
  1266. exprtk_define_unary_function(sqrt )
  1267. exprtk_define_unary_function(tan )
  1268. exprtk_define_unary_function(tanh )
  1269. exprtk_define_unary_function(cot )
  1270. exprtk_define_unary_function(sec )
  1271. exprtk_define_unary_function(csc )
  1272. exprtk_define_unary_function(r2d )
  1273. exprtk_define_unary_function(d2r )
  1274. exprtk_define_unary_function(d2g )
  1275. exprtk_define_unary_function(g2d )
  1276. exprtk_define_unary_function(notl )
  1277. exprtk_define_unary_function(sgn )
  1278. exprtk_define_unary_function(erf )
  1279. exprtk_define_unary_function(erfc )
  1280. exprtk_define_unary_function(ncdf )
  1281. exprtk_define_unary_function(frac )
  1282. exprtk_define_unary_function(trunc)
  1283. #undef exprtk_define_unary_function
  1284. }
  1285. template <typename T>
  1286. inline T compute_pow10(T d, const int exponent)
  1287. {
  1288. static const double fract10[] =
  1289. {
  1290. 0.0,
  1291. 1.0E+001, 1.0E+002, 1.0E+003, 1.0E+004, 1.0E+005, 1.0E+006, 1.0E+007, 1.0E+008, 1.0E+009, 1.0E+010,
  1292. 1.0E+011, 1.0E+012, 1.0E+013, 1.0E+014, 1.0E+015, 1.0E+016, 1.0E+017, 1.0E+018, 1.0E+019, 1.0E+020,
  1293. 1.0E+021, 1.0E+022, 1.0E+023, 1.0E+024, 1.0E+025, 1.0E+026, 1.0E+027, 1.0E+028, 1.0E+029, 1.0E+030,
  1294. 1.0E+031, 1.0E+032, 1.0E+033, 1.0E+034, 1.0E+035, 1.0E+036, 1.0E+037, 1.0E+038, 1.0E+039, 1.0E+040,
  1295. 1.0E+041, 1.0E+042, 1.0E+043, 1.0E+044, 1.0E+045, 1.0E+046, 1.0E+047, 1.0E+048, 1.0E+049, 1.0E+050,
  1296. 1.0E+051, 1.0E+052, 1.0E+053, 1.0E+054, 1.0E+055, 1.0E+056, 1.0E+057, 1.0E+058, 1.0E+059, 1.0E+060,
  1297. 1.0E+061, 1.0E+062, 1.0E+063, 1.0E+064, 1.0E+065, 1.0E+066, 1.0E+067, 1.0E+068, 1.0E+069, 1.0E+070,
  1298. 1.0E+071, 1.0E+072, 1.0E+073, 1.0E+074, 1.0E+075, 1.0E+076, 1.0E+077, 1.0E+078, 1.0E+079, 1.0E+080,
  1299. 1.0E+081, 1.0E+082, 1.0E+083, 1.0E+084, 1.0E+085, 1.0E+086, 1.0E+087, 1.0E+088, 1.0E+089, 1.0E+090,
  1300. 1.0E+091, 1.0E+092, 1.0E+093, 1.0E+094, 1.0E+095, 1.0E+096, 1.0E+097, 1.0E+098, 1.0E+099, 1.0E+100,
  1301. 1.0E+101, 1.0E+102, 1.0E+103, 1.0E+104, 1.0E+105, 1.0E+106, 1.0E+107, 1.0E+108, 1.0E+109, 1.0E+110,
  1302. 1.0E+111, 1.0E+112, 1.0E+113, 1.0E+114, 1.0E+115, 1.0E+116, 1.0E+117, 1.0E+118, 1.0E+119, 1.0E+120,
  1303. 1.0E+121, 1.0E+122, 1.0E+123, 1.0E+124, 1.0E+125, 1.0E+126, 1.0E+127, 1.0E+128, 1.0E+129, 1.0E+130,
  1304. 1.0E+131, 1.0E+132, 1.0E+133, 1.0E+134, 1.0E+135, 1.0E+136, 1.0E+137, 1.0E+138, 1.0E+139, 1.0E+140,
  1305. 1.0E+141, 1.0E+142, 1.0E+143, 1.0E+144, 1.0E+145, 1.0E+146, 1.0E+147, 1.0E+148, 1.0E+149, 1.0E+150,
  1306. 1.0E+151, 1.0E+152, 1.0E+153, 1.0E+154, 1.0E+155, 1.0E+156, 1.0E+157, 1.0E+158, 1.0E+159, 1.0E+160,
  1307. 1.0E+161, 1.0E+162, 1.0E+163, 1.0E+164, 1.0E+165, 1.0E+166, 1.0E+167, 1.0E+168, 1.0E+169, 1.0E+170,
  1308. 1.0E+171, 1.0E+172, 1.0E+173, 1.0E+174, 1.0E+175, 1.0E+176, 1.0E+177, 1.0E+178, 1.0E+179, 1.0E+180,
  1309. 1.0E+181, 1.0E+182, 1.0E+183, 1.0E+184, 1.0E+185, 1.0E+186, 1.0E+187, 1.0E+188, 1.0E+189, 1.0E+190,
  1310. 1.0E+191, 1.0E+192, 1.0E+193, 1.0E+194, 1.0E+195, 1.0E+196, 1.0E+197, 1.0E+198, 1.0E+199, 1.0E+200,
  1311. 1.0E+201, 1.0E+202, 1.0E+203, 1.0E+204, 1.0E+205, 1.0E+206, 1.0E+207, 1.0E+208, 1.0E+209, 1.0E+210,
  1312. 1.0E+211, 1.0E+212, 1.0E+213, 1.0E+214, 1.0E+215, 1.0E+216, 1.0E+217, 1.0E+218, 1.0E+219, 1.0E+220,
  1313. 1.0E+221, 1.0E+222, 1.0E+223, 1.0E+224, 1.0E+225, 1.0E+226, 1.0E+227, 1.0E+228, 1.0E+229, 1.0E+230,
  1314. 1.0E+231, 1.0E+232, 1.0E+233, 1.0E+234, 1.0E+235, 1.0E+236, 1.0E+237, 1.0E+238, 1.0E+239, 1.0E+240,
  1315. 1.0E+241, 1.0E+242, 1.0E+243, 1.0E+244, 1.0E+245, 1.0E+246, 1.0E+247, 1.0E+248, 1.0E+249, 1.0E+250,
  1316. 1.0E+251, 1.0E+252, 1.0E+253, 1.0E+254, 1.0E+255, 1.0E+256, 1.0E+257, 1.0E+258, 1.0E+259, 1.0E+260,
  1317. 1.0E+261, 1.0E+262, 1.0E+263, 1.0E+264, 1.0E+265, 1.0E+266, 1.0E+267, 1.0E+268, 1.0E+269, 1.0E+270,
  1318. 1.0E+271, 1.0E+272, 1.0E+273, 1.0E+274, 1.0E+275, 1.0E+276, 1.0E+277, 1.0E+278, 1.0E+279, 1.0E+280,
  1319. 1.0E+281, 1.0E+282, 1.0E+283, 1.0E+284, 1.0E+285, 1.0E+286, 1.0E+287, 1.0E+288, 1.0E+289, 1.0E+290,
  1320. 1.0E+291, 1.0E+292, 1.0E+293, 1.0E+294, 1.0E+295, 1.0E+296, 1.0E+297, 1.0E+298, 1.0E+299, 1.0E+300,
  1321. 1.0E+301, 1.0E+302, 1.0E+303, 1.0E+304, 1.0E+305, 1.0E+306, 1.0E+307, 1.0E+308
  1322. };
  1323. static const int fract10_size = static_cast<int>(sizeof(fract10) / sizeof(double));
  1324. const int e = std::abs(exponent);
  1325. if (exponent >= std::numeric_limits<T>::min_exponent10)
  1326. {
  1327. if (e < fract10_size)
  1328. {
  1329. if (exponent > 0)
  1330. return T(d * fract10[e]);
  1331. else
  1332. return T(d / fract10[e]);
  1333. }
  1334. else
  1335. return T(d * std::pow(10.0, 10.0 * exponent));
  1336. }
  1337. else
  1338. {
  1339. d /= T(fract10[ -std::numeric_limits<T>::min_exponent10]);
  1340. return T(d / fract10[-exponent + std::numeric_limits<T>::min_exponent10]);
  1341. }
  1342. }
  1343. template <typename Iterator, typename T>
  1344. inline bool string_to_type_converter_impl_ref(Iterator& itr, const Iterator end, T& result)
  1345. {
  1346. if (itr == end)
  1347. return false;
  1348. bool negative = ('-' == (*itr));
  1349. if (negative || ('+' == (*itr)))
  1350. {
  1351. if (end == ++itr)
  1352. return false;
  1353. }
  1354. while ((end != itr) && ('0' == (*itr))) ++itr;
  1355. bool return_result = true;
  1356. unsigned int digit = 0;
  1357. const std::size_t length = std::distance(itr,end);
  1358. if (length <= 4)
  1359. {
  1360. switch (length)
  1361. {
  1362. #ifdef exprtk_use_lut
  1363. #define exprtk_process_digit \
  1364. if ((digit = details::digit_table[(int)*itr++]) < 10) result = result * 10 + (digit); else { return_result = false; break; }
  1365. #else
  1366. #define exprtk_process_digit \
  1367. if ((digit = (*itr++ - '0')) < 10) result = result * 10 + (digit); else { return_result = false; break; }
  1368. #endif
  1369. case 4 : exprtk_process_digit
  1370. case 3 : exprtk_process_digit
  1371. case 2 : exprtk_process_digit
  1372. case 1 : if ((digit = (*itr - '0'))>= 10) { digit = 0; return_result = false; }
  1373. #undef exprtk_process_digit
  1374. }
  1375. }
  1376. else
  1377. return_result = false;
  1378. if (length && return_result)
  1379. {
  1380. result = result * 10 + static_cast<T>(digit);
  1381. ++itr;
  1382. }
  1383. result = negative ? -result : result;
  1384. return return_result;
  1385. }
  1386. template <typename Iterator, typename T>
  1387. static inline bool parse_nan(Iterator& itr, const Iterator end, T& t)
  1388. {
  1389. typedef typename std::iterator_traits<Iterator>::value_type type;
  1390. static const std::size_t nan_length = 3;
  1391. if (std::distance(itr,end) != static_cast<int>(nan_length))
  1392. return false;
  1393. if (static_cast<type>('n') == (*itr))
  1394. {
  1395. if (
  1396. (static_cast<type>('a') != *(itr + 1)) ||
  1397. (static_cast<type>('n') != *(itr + 2))
  1398. )
  1399. {
  1400. return false;
  1401. }
  1402. }
  1403. else if (
  1404. (static_cast<type>('A') != *(itr + 1)) ||
  1405. (static_cast<type>('N') != *(itr + 2))
  1406. )
  1407. {
  1408. return false;
  1409. }
  1410. t = std::numeric_limits<T>::quiet_NaN();
  1411. return true;
  1412. }
  1413. template <typename Iterator, typename T>
  1414. static inline bool parse_inf(Iterator& itr, const Iterator end, T& t, bool negative)
  1415. {
  1416. static const char inf_uc[] = "INFINITY";
  1417. static const char inf_lc[] = "infinity";
  1418. static const std::size_t inf_length = 8;
  1419. const std::size_t length = std::distance(itr,end);
  1420. if ((3 != length) && (inf_length != length))
  1421. return false;
  1422. const char* inf_itr = ('i' == (*itr)) ? inf_lc : inf_uc;
  1423. while (end != itr)
  1424. {
  1425. if (*inf_itr == static_cast<char>(*itr))
  1426. {
  1427. ++itr;
  1428. ++inf_itr;
  1429. continue;
  1430. }
  1431. else
  1432. return false;
  1433. }
  1434. if (negative)
  1435. t = -std::numeric_limits<T>::infinity();
  1436. else
  1437. t = std::numeric_limits<T>::infinity();
  1438. return true;
  1439. }
  1440. template <typename Iterator, typename T>
  1441. inline bool string_to_real(Iterator& itr_external, const Iterator end, T& t, numeric::details::real_type_tag)
  1442. {
  1443. if (end == itr_external) return false;
  1444. Iterator itr = itr_external;
  1445. T d = T(0);
  1446. bool negative = ('-' == (*itr));
  1447. if (negative || '+' == (*itr))
  1448. {
  1449. if (end == ++itr)
  1450. return false;
  1451. }
  1452. bool instate = false;
  1453. #define parse_digit_1(d) \
  1454. if ((digit = (*itr - '0')) < 10) { d = d * T(10) + digit; } else break; if (end == ++itr) break; \
  1455. #define parse_digit_2(d) \
  1456. if ((digit = (*itr - '0')) < 10) { d = d * T(10) + digit; } else break; ++itr; \
  1457. if ('.' != (*itr))
  1458. {
  1459. const Iterator curr = itr;
  1460. while ((end != itr) && ('0' == (*itr))) ++itr;
  1461. unsigned int digit;
  1462. while (end != itr)
  1463. {
  1464. // Note: For 'physical' superscalar architectures it
  1465. // is advised that the following loop be: 4xPD1 and 1xPD2
  1466. #ifdef exprtk_enable_superscalar
  1467. parse_digit_1(d)
  1468. parse_digit_1(d)
  1469. #endif
  1470. parse_digit_1(d)
  1471. parse_digit_1(d)
  1472. parse_digit_2(d)
  1473. }
  1474. if (curr != itr) instate = true;
  1475. }
  1476. int exponent = 0;
  1477. if (end != itr)
  1478. {
  1479. if ('.' == (*itr))
  1480. {
  1481. const Iterator curr = ++itr;
  1482. unsigned int digit;
  1483. T tmp_d = T(0);
  1484. while (end != itr)
  1485. {
  1486. #ifdef exprtk_enable_superscalar
  1487. parse_digit_1(tmp_d)
  1488. parse_digit_1(tmp_d)
  1489. parse_digit_1(tmp_d)
  1490. #endif
  1491. parse_digit_1(tmp_d)
  1492. parse_digit_1(tmp_d)
  1493. parse_digit_2(tmp_d)
  1494. }
  1495. if (curr != itr)
  1496. {
  1497. instate = true;
  1498. d += compute_pow10(tmp_d,-std::distance(curr,itr));
  1499. }
  1500. #undef parse_digit_1
  1501. #undef parse_digit_2
  1502. }
  1503. if (end != itr)
  1504. {
  1505. typename std::iterator_traits<Iterator>::value_type c = (*itr);
  1506. if (('e' == c) || ('E' == c))
  1507. {
  1508. int exp = 0;
  1509. if (!details::string_to_type_converter_impl_ref(++itr,end,exp))
  1510. {
  1511. if (end == itr)
  1512. return false;
  1513. else
  1514. c = (*itr);
  1515. }
  1516. exponent += exp;
  1517. }
  1518. if (end != itr)
  1519. {
  1520. if (('f' == c) || ('F' == c) || ('l' == c) || ('L' == c))
  1521. ++itr;
  1522. else if ('#' == c)
  1523. {
  1524. if (end == ++itr)
  1525. return false;
  1526. else if (('I' <= (*itr)) && ((*itr) <= 'n'))
  1527. {
  1528. if (('i' == (*itr)) || ('I' == (*itr)))
  1529. {
  1530. return parse_inf(itr,end,t,negative);
  1531. }
  1532. else if (('n' == (*itr)) || ('N' == (*itr)))
  1533. {
  1534. return parse_nan(itr,end,t);
  1535. }
  1536. else
  1537. return false;
  1538. }
  1539. else
  1540. return false;
  1541. }
  1542. else if (('I' <= (*itr)) && ((*itr) <= 'n'))
  1543. {
  1544. if (('i' == (*itr)) || ('I' == (*itr)))
  1545. {
  1546. return parse_inf(itr,end,t,negative);
  1547. }
  1548. else if (('n' == (*itr)) || ('N' == (*itr)))
  1549. {
  1550. return parse_nan(itr,end,t);
  1551. }
  1552. else
  1553. return false;
  1554. }
  1555. else
  1556. return false;
  1557. }
  1558. }
  1559. }
  1560. if ((end != itr) || (!instate))
  1561. return false;
  1562. else if (exponent)
  1563. d = compute_pow10(d,exponent);
  1564. t = static_cast<T>((negative) ? -d : d);
  1565. return true;
  1566. }
  1567. template <typename T>
  1568. inline bool string_to_real(const std::string& s, T& t)
  1569. {
  1570. const char* begin = s.data();
  1571. const char* end = s.data() + s.size();
  1572. typename numeric::details::number_type<T>::type num_type;
  1573. return string_to_real(begin,end,t,num_type);
  1574. }
  1575. template <typename T>
  1576. struct functor_t
  1577. {
  1578. /*
  1579. Note: The following definitions for Type, may require tweaking
  1580. based on the compiler and target architecture. The benchmark
  1581. should provide enough information to make the right choice.
  1582. */
  1583. //typedef T Type;
  1584. //typedef const T Type;
  1585. typedef const T& Type;
  1586. typedef T (*qfunc_t)(Type t0, Type t1, Type t2, Type t3);
  1587. typedef T (*tfunc_t)(Type t0, Type t1, Type t2);
  1588. typedef T (*bfunc_t)(Type t0, Type t1);
  1589. typedef T (*ufunc_t)(Type t0);
  1590. };
  1591. } // namespace details
  1592. namespace lexer
  1593. {
  1594. struct token
  1595. {
  1596. enum token_type
  1597. {
  1598. e_none = 0, e_error = 1, e_err_symbol = 2,
  1599. e_err_number = 3, e_err_string = 4, e_err_sfunc = 5,
  1600. e_eof = 6, e_number = 7, e_symbol = 8,
  1601. e_string = 9, e_assign = 10, e_addass = 11,
  1602. e_subass = 12, e_mulass = 13, e_divass = 14,
  1603. e_modass = 15, e_shr = 16, e_shl = 17,
  1604. e_lte = 18, e_ne = 19, e_gte = 20,
  1605. e_swap = 21, e_lt = '<', e_gt = '>',
  1606. e_eq = '=', e_rbracket = ')', e_lbracket = '(',
  1607. e_rsqrbracket = ']', e_lsqrbracket = '[', e_rcrlbracket = '}',
  1608. e_lcrlbracket = '{', e_comma = ',', e_add = '+',
  1609. e_sub = '-', e_div = '/', e_mul = '*',
  1610. e_mod = '%', e_pow = '^', e_colon = ':',
  1611. e_ternary = '?'
  1612. };
  1613. token()
  1614. : type(e_none),
  1615. value(""),
  1616. position(std::numeric_limits<std::size_t>::max())
  1617. {}
  1618. void clear()
  1619. {
  1620. type = e_none;
  1621. value = "";
  1622. position = std::numeric_limits<std::size_t>::max();
  1623. }
  1624. template <typename Iterator>
  1625. inline token& set_operator(const token_type tt, const Iterator begin, const Iterator end, const Iterator base_begin = Iterator(0))
  1626. {
  1627. type = tt;
  1628. value.assign(begin,end);
  1629. if (base_begin)
  1630. position = std::distance(base_begin,begin);
  1631. return *this;
  1632. }
  1633. template <typename Iterator>
  1634. inline token& set_symbol(const Iterator begin, const Iterator end, const Iterator base_begin = Iterator(0))
  1635. {
  1636. type = e_symbol;
  1637. value.assign(begin,end);
  1638. if (base_begin)
  1639. position = std::distance(base_begin,begin);
  1640. return *this;
  1641. }
  1642. template <typename Iterator>
  1643. inline token& set_numeric(const Iterator begin, const Iterator end, const Iterator base_begin = Iterator(0))
  1644. {
  1645. type = e_number;
  1646. value.assign(begin,end);
  1647. if (base_begin)
  1648. position = std::distance(base_begin,begin);
  1649. return *this;
  1650. }
  1651. template <typename Iterator>
  1652. inline token& set_string(const Iterator begin, const Iterator end, const Iterator base_begin = Iterator(0))
  1653. {
  1654. type = e_string;
  1655. value.assign(begin,end);
  1656. if (base_begin)
  1657. position = std::distance(base_begin,begin);
  1658. return *this;
  1659. }
  1660. inline token& set_string(const std::string& s, const std::size_t p)
  1661. {
  1662. type = e_string;
  1663. value = s;
  1664. position = p;
  1665. return *this;
  1666. }
  1667. template <typename Iterator>
  1668. inline token& set_error(const token_type et, const Iterator begin, const Iterator end, const Iterator base_begin = Iterator(0))
  1669. {
  1670. if (
  1671. (e_error == et) ||
  1672. (e_err_symbol == et) ||
  1673. (e_err_number == et) ||
  1674. (e_err_string == et) ||
  1675. (e_err_sfunc == et)
  1676. )
  1677. {
  1678. type = et;
  1679. }
  1680. else
  1681. type = e_error;
  1682. value.assign(begin,end);
  1683. if (base_begin)
  1684. position = std::distance(base_begin,begin);
  1685. return *this;
  1686. }
  1687. static inline std::string to_str(token_type t)
  1688. {
  1689. switch (t)
  1690. {
  1691. case e_none : return "NONE";
  1692. case e_error : return "ERROR";
  1693. case e_err_symbol : return "ERROR_SYMBOL";
  1694. case e_err_number : return "ERROR_NUMBER";
  1695. case e_err_string : return "ERROR_STRING";
  1696. case e_eof : return "EOF";
  1697. case e_number : return "NUMBER";
  1698. case e_symbol : return "SYMBOL";
  1699. case e_string : return "STRING";
  1700. case e_assign : return ":=";
  1701. case e_addass : return "+=";
  1702. case e_subass : return "-=";
  1703. case e_mulass : return "*=";
  1704. case e_divass : return "/=";
  1705. case e_modass : return "%=";
  1706. case e_shr : return ">>";
  1707. case e_shl : return "<<";
  1708. case e_lte : return "<=";
  1709. case e_ne : return "!=";
  1710. case e_gte : return ">=";
  1711. case e_lt : return "<";
  1712. case e_gt : return ">";
  1713. case e_eq : return "=";
  1714. case e_rbracket : return ")";
  1715. case e_lbracket : return "(";
  1716. case e_rsqrbracket : return "]";
  1717. case e_lsqrbracket : return "[";
  1718. case e_rcrlbracket : return "}";
  1719. case e_lcrlbracket : return "{";
  1720. case e_comma : return ",";
  1721. case e_add : return "+";
  1722. case e_sub : return "-";
  1723. case e_div : return "/";
  1724. case e_mul : return "*";
  1725. case e_mod : return "%";
  1726. case e_pow : return "^";
  1727. case e_colon : return ":";
  1728. case e_ternary : return "?";
  1729. case e_swap : return "<=>";
  1730. default : return "UNKNOWN";
  1731. }
  1732. }
  1733. inline bool is_error() const
  1734. {
  1735. return (
  1736. (e_error == type) ||
  1737. (e_err_symbol == type) ||
  1738. (e_err_number == type) ||
  1739. (e_err_string == type) ||
  1740. (e_err_sfunc == type)
  1741. );
  1742. }
  1743. token_type type;
  1744. std::string value;
  1745. std::size_t position;
  1746. };
  1747. class generator
  1748. {
  1749. public:
  1750. typedef token token_t;
  1751. typedef std::vector<token_t> token_list_t;
  1752. typedef std::vector<token_t>::iterator token_list_itr_t;
  1753. generator()
  1754. : base_itr_(0),
  1755. s_itr_ (0),
  1756. s_end_ (0)
  1757. {
  1758. clear();
  1759. }
  1760. inline void clear()
  1761. {
  1762. base_itr_ = 0;
  1763. s_itr_ = 0;
  1764. s_end_ = 0;
  1765. token_list_.clear();
  1766. token_itr_ = token_list_.end();
  1767. store_token_itr_ = token_list_.end();
  1768. }
  1769. inline bool process(const std::string& str)
  1770. {
  1771. base_itr_ = str.data();
  1772. s_itr_ = str.data();
  1773. s_end_ = str.data() + str.size();
  1774. eof_token_.set_operator(token_t::e_eof,s_end_,s_end_,base_itr_);
  1775. token_list_.clear();
  1776. while (!is_end(s_itr_))
  1777. {
  1778. scan_token();
  1779. if (token_list_.empty())
  1780. return true;
  1781. else if (token_list_.back().is_error())
  1782. {
  1783. return false;
  1784. }
  1785. }
  1786. return true;
  1787. }
  1788. inline bool empty() const
  1789. {
  1790. return token_list_.empty();
  1791. }
  1792. inline std::size_t size() const
  1793. {
  1794. return token_list_.size();
  1795. }
  1796. inline void begin()
  1797. {
  1798. token_itr_ = token_list_.begin();
  1799. store_token_itr_ = token_list_.begin();
  1800. }
  1801. inline void store()
  1802. {
  1803. store_token_itr_ = token_itr_;
  1804. }
  1805. inline void restore()
  1806. {
  1807. token_itr_ = store_token_itr_;
  1808. }
  1809. inline token_t& next_token()
  1810. {
  1811. if (token_list_.end() != token_itr_)
  1812. {
  1813. return *token_itr_++;
  1814. }
  1815. else
  1816. return eof_token_;
  1817. }
  1818. inline token_t& peek_next_token()
  1819. {
  1820. if (token_list_.end() != token_itr_)
  1821. {
  1822. return *token_itr_;
  1823. }
  1824. else
  1825. return eof_token_;
  1826. }
  1827. inline token_t& operator[](const std::size_t& index)
  1828. {
  1829. if (index < token_list_.size())
  1830. return token_list_[index];
  1831. else
  1832. return eof_token_;
  1833. }
  1834. inline token_t operator[](const std::size_t& index) const
  1835. {
  1836. if (index < token_list_.size())
  1837. return token_list_[index];
  1838. else
  1839. return eof_token_;
  1840. }
  1841. inline bool finished() const
  1842. {
  1843. return (token_list_.end() == token_itr_);
  1844. }
  1845. inline void insert_front(token_t::token_type tk_type)
  1846. {
  1847. if (
  1848. !token_list_.empty() &&
  1849. (token_list_.end() != token_itr_)
  1850. )
  1851. {
  1852. token_t t = *token_itr_;
  1853. t.type = tk_type;
  1854. token_itr_ = token_list_.insert(token_itr_,t);
  1855. }
  1856. }
  1857. inline std::string substr(const std::size_t& begin, const std::size_t& end)
  1858. {
  1859. const char* begin_itr = ((base_itr_ + begin) < s_end_) ? (base_itr_ + begin) : s_end_;
  1860. const char* end_itr = ((base_itr_ + end) < s_end_) ? (base_itr_ + end) : s_end_;
  1861. return std::string(begin_itr,end_itr);
  1862. }
  1863. inline std::string remaining() const
  1864. {
  1865. if (finished())
  1866. return "";
  1867. else if (token_list_.begin() != token_itr_)
  1868. return std::string(base_itr_ + (token_itr_ - 1)->position,s_end_);
  1869. else
  1870. return std::string(base_itr_ + token_itr_->position,s_end_);
  1871. }
  1872. private:
  1873. inline bool is_end(const char* itr)
  1874. {
  1875. return (s_end_ == itr);
  1876. }
  1877. inline void skip_whitespace()
  1878. {
  1879. while (!is_end(s_itr_) && details::is_whitespace(*s_itr_))
  1880. {
  1881. ++s_itr_;
  1882. }
  1883. }
  1884. inline void skip_comments()
  1885. {
  1886. #ifndef exprtk_disable_comments
  1887. // The following comment styles are supported:
  1888. // 1. // .... \n
  1889. // 2. # .... \n
  1890. // 3. /* .... */
  1891. struct test
  1892. {
  1893. static inline bool comment_start(const char c0, const char c1, int& mode, int& incr)
  1894. {
  1895. mode = 0;
  1896. if ('#' == c0) { mode = 1; incr = 1; }
  1897. else if ('/' == c0)
  1898. {
  1899. if ('/' == c1) { mode = 1; incr = 2; }
  1900. else if ('*' == c1) { mode = 2; incr = 2; }
  1901. }
  1902. return (0 != mode);
  1903. }
  1904. static inline bool comment_end(const char c0, const char c1, const int mode)
  1905. {
  1906. return (
  1907. ((1 == mode) && ('\n' == c0)) ||
  1908. ((2 == mode) && ( '*' == c0) && ('/' == c1))
  1909. );
  1910. }
  1911. };
  1912. int mode = 0;
  1913. int increment = 0;
  1914. if (is_end(s_itr_) || is_end((s_itr_ + 1)))
  1915. return;
  1916. else if (!test::comment_start(*s_itr_,*(s_itr_ + 1),mode,increment))
  1917. return;
  1918. s_itr_ += increment;
  1919. while (!is_end(s_itr_) && !test::comment_end(*s_itr_,*(s_itr_ + 1),mode))
  1920. {
  1921. ++s_itr_;
  1922. }
  1923. if (!is_end(s_itr_))
  1924. {
  1925. s_itr_ += mode;
  1926. skip_whitespace();
  1927. skip_comments();
  1928. }
  1929. #endif
  1930. }
  1931. inline void scan_token()
  1932. {
  1933. skip_whitespace();
  1934. skip_comments();
  1935. if (is_end(s_itr_))
  1936. {
  1937. return;
  1938. }
  1939. else if (details::is_operator_char(*s_itr_))
  1940. {
  1941. scan_operator();
  1942. return;
  1943. }
  1944. else if (details::is_letter(*s_itr_))
  1945. {
  1946. scan_symbol();
  1947. return;
  1948. }
  1949. else if (details::is_digit((*s_itr_)) || ('.' == (*s_itr_)))
  1950. {
  1951. scan_number();
  1952. return;
  1953. }
  1954. else if ('$' == (*s_itr_))
  1955. {
  1956. scan_special_function();
  1957. return;
  1958. }
  1959. #ifndef exprtk_disable_string_capabilities
  1960. else if ('\'' == (*s_itr_))
  1961. {
  1962. scan_string();
  1963. return;
  1964. }
  1965. #endif
  1966. else if ('~' == (*s_itr_))
  1967. {
  1968. token_t t;
  1969. t.set_symbol(s_itr_,s_itr_ + 1,base_itr_);
  1970. token_list_.push_back(t);
  1971. ++s_itr_;
  1972. return;
  1973. }
  1974. else
  1975. {
  1976. token_t t;
  1977. t.set_error(token::e_error,s_itr_,s_itr_ + 2,base_itr_);
  1978. token_list_.push_back(t);
  1979. ++s_itr_;
  1980. }
  1981. }
  1982. inline void scan_operator()
  1983. {
  1984. token_t t;
  1985. const char c0 = s_itr_[0];
  1986. if (!is_end(s_itr_ + 1))
  1987. {
  1988. const char c1 = s_itr_[1];
  1989. if (!is_end(s_itr_ + 2))
  1990. {
  1991. const char c2 = s_itr_[2];
  1992. if ((c0 == '<') && (c1 == '=') && (c2 == '>'))
  1993. {
  1994. t.set_operator(token_t::e_swap,s_itr_,s_itr_ + 3,base_itr_);
  1995. token_list_.push_back(t);
  1996. s_itr_ += 3;
  1997. return;
  1998. }
  1999. }
  2000. token_t::token_type ttype = token_t::e_none;
  2001. if ((c0 == '<') && (c1 == '=')) ttype = token_t::e_lte;
  2002. else if ((c0 == '>') && (c1 == '=')) ttype = token_t::e_gte;
  2003. else if ((c0 == '<') && (c1 == '>')) ttype = token_t::e_ne;
  2004. else if ((c0 == '!') && (c1 == '=')) ttype = token_t::e_ne;
  2005. else if ((c0 == '=') && (c1 == '=')) ttype = token_t::e_eq;
  2006. else if ((c0 == ':') && (c1 == '=')) ttype = token_t::e_assign;
  2007. else if ((c0 == '<') && (c1 == '<')) ttype = token_t::e_shl;
  2008. else if ((c0 == '>') && (c1 == '>')) ttype = token_t::e_shr;
  2009. else if ((c0 == '+') && (c1 == '=')) ttype = token_t::e_addass;
  2010. else if ((c0 == '-') && (c1 == '=')) ttype = token_t::e_subass;
  2011. else if ((c0 == '*') && (c1 == '=')) ttype = token_t::e_mulass;
  2012. else if ((c0 == '/') && (c1 == '=')) ttype = token_t::e_divass;
  2013. else if ((c0 == '%') && (c1 == '=')) ttype = token_t::e_modass;
  2014. if (token_t::e_none != ttype)
  2015. {
  2016. t.set_operator(ttype,s_itr_,s_itr_ + 2,base_itr_);
  2017. token_list_.push_back(t);
  2018. s_itr_ += 2;
  2019. return;
  2020. }
  2021. }
  2022. if ('<' == c0)
  2023. t.set_operator(token_t::e_lt ,s_itr_,s_itr_ + 1,base_itr_);
  2024. else if ('>' == c0)
  2025. t.set_operator(token_t::e_gt ,s_itr_,s_itr_ + 1,base_itr_);
  2026. else if (';' == c0)
  2027. t.set_operator(token_t::e_eof,s_itr_,s_itr_ + 1,base_itr_);
  2028. else if ('&' == c0)
  2029. t.set_symbol(s_itr_,s_itr_ + 1,base_itr_);
  2030. else if ('|' == c0)
  2031. t.set_symbol(s_itr_,s_itr_ + 1,base_itr_);
  2032. else
  2033. t.set_operator(token_t::token_type(c0),s_itr_,s_itr_ + 1,base_itr_);
  2034. token_list_.push_back(t);
  2035. ++s_itr_;
  2036. }
  2037. inline void scan_symbol()
  2038. {
  2039. const char* initial_itr = s_itr_;
  2040. while (
  2041. (!is_end(s_itr_)) &&
  2042. (details::is_letter_or_digit(*s_itr_) || ((*s_itr_) == '_'))
  2043. )
  2044. {
  2045. ++s_itr_;
  2046. }
  2047. token_t t;
  2048. t.set_symbol(initial_itr,s_itr_,base_itr_);
  2049. token_list_.push_back(t);
  2050. }
  2051. inline void scan_number()
  2052. {
  2053. /*
  2054. Attempt to match a valid numeric value in one of the following formats:
  2055. 1. 123456
  2056. 2. 123.456
  2057. 3. 123.456e3
  2058. 4. 123.456E3
  2059. 5. 123.456e+3
  2060. 6. 123.456E+3
  2061. 7. 123.456e-3
  2062. 8. 123.456E-3
  2063. */
  2064. const char* initial_itr = s_itr_;
  2065. bool dot_found = false;
  2066. bool e_found = false;
  2067. bool post_e_sign_found = false;
  2068. token_t t;
  2069. while (!is_end(s_itr_))
  2070. {
  2071. if ('.' == (*s_itr_))
  2072. {
  2073. if (dot_found)
  2074. {
  2075. t.set_error(token::e_err_number,initial_itr,s_itr_,base_itr_);
  2076. token_list_.push_back(t);
  2077. return;
  2078. }
  2079. dot_found = true;
  2080. ++s_itr_;
  2081. continue;
  2082. }
  2083. else if (details::imatch('e',(*s_itr_)))
  2084. {
  2085. const char& c = *(s_itr_ + 1);
  2086. if (is_end(s_itr_ + 1))
  2087. {
  2088. t.set_error(token::e_err_number,initial_itr,s_itr_,base_itr_);
  2089. token_list_.push_back(t);
  2090. return;
  2091. }
  2092. else if (
  2093. ('+' != c) &&
  2094. ('-' != c) &&
  2095. !details::is_digit(c)
  2096. )
  2097. {
  2098. t.set_error(token::e_err_number,initial_itr,s_itr_,base_itr_);
  2099. token_list_.push_back(t);
  2100. return;
  2101. }
  2102. e_found = true;
  2103. ++s_itr_;
  2104. continue;
  2105. }
  2106. else if (e_found && details::is_sign(*s_itr_))
  2107. {
  2108. if (post_e_sign_found)
  2109. {
  2110. t.set_error(token::e_err_number,initial_itr,s_itr_,base_itr_);
  2111. token_list_.push_back(t);
  2112. return;
  2113. }
  2114. post_e_sign_found = true;
  2115. ++s_itr_;
  2116. continue;
  2117. }
  2118. else if (('.' != (*s_itr_)) && !details::is_digit(*s_itr_))
  2119. break;
  2120. else
  2121. ++s_itr_;
  2122. }
  2123. t.set_numeric(initial_itr,s_itr_,base_itr_);
  2124. token_list_.push_back(t);
  2125. return;
  2126. }
  2127. inline void scan_special_function()
  2128. {
  2129. const char* initial_itr = s_itr_;
  2130. token_t t;
  2131. // $fdd(x,x,x) = at least 11 chars
  2132. if (std::distance(s_itr_,s_end_) < 11)
  2133. {
  2134. t.set_error(token::e_err_sfunc,initial_itr,s_itr_,base_itr_);
  2135. token_list_.push_back(t);
  2136. return;
  2137. }
  2138. if (
  2139. !(('$' == *s_itr_) &&
  2140. (details::imatch ('f',*(s_itr_ + 1))) &&
  2141. (details::is_digit(*(s_itr_ + 2))) &&
  2142. (details::is_digit(*(s_itr_ + 3))))
  2143. )
  2144. {
  2145. t.set_error(token::e_err_sfunc,initial_itr,s_itr_,base_itr_);
  2146. token_list_.push_back(t);
  2147. return;
  2148. }
  2149. s_itr_ += 4; // $fdd = 4chars
  2150. t.set_symbol(initial_itr,s_itr_,base_itr_);
  2151. token_list_.push_back(t);
  2152. return;
  2153. }
  2154. #ifndef exprtk_disable_string_capabilities
  2155. inline void scan_string()
  2156. {
  2157. const char* initial_itr = s_itr_ + 1;
  2158. token_t t;
  2159. if (std::distance(s_itr_,s_end_) < 2)
  2160. {
  2161. t.set_error(token::e_err_string,s_itr_,s_end_,base_itr_);
  2162. token_list_.push_back(t);
  2163. return;
  2164. }
  2165. ++s_itr_;
  2166. bool escaped_found = false;
  2167. bool escaped = false;
  2168. while (!is_end(s_itr_))
  2169. {
  2170. if (!escaped && ('\\' == *s_itr_))
  2171. {
  2172. escaped_found = true;
  2173. escaped = true;
  2174. ++s_itr_;
  2175. continue;
  2176. }
  2177. else if (!escaped)
  2178. {
  2179. if ('\'' == *s_itr_)
  2180. break;
  2181. }
  2182. else if (escaped)
  2183. {
  2184. if (!is_end(s_itr_) && ('0' == *(s_itr_)))
  2185. {
  2186. /*
  2187. Note: The following 'awkward' conditional is
  2188. due to various broken msvc compilers.
  2189. */
  2190. #if _MSC_VER == 1600
  2191. const bool within_range = !is_end(s_itr_ + 2) &&
  2192. !is_end(s_itr_ + 3) ;
  2193. #else
  2194. const bool within_range = !is_end(s_itr_ + 1) &&
  2195. !is_end(s_itr_ + 2) &&
  2196. !is_end(s_itr_ + 3) ;
  2197. #endif
  2198. const bool x_seperator = ('x' == *(s_itr_ + 1)) ||
  2199. ('X' == *(s_itr_ + 1)) ;
  2200. const bool both_digits = details::is_hex_digit(*(s_itr_ + 2)) &&
  2201. details::is_hex_digit(*(s_itr_ + 3)) ;
  2202. if (!within_range || !x_seperator || !both_digits)
  2203. {
  2204. t.set_error(token::e_err_string,initial_itr,s_itr_,base_itr_);
  2205. token_list_.push_back(t);
  2206. return;
  2207. }
  2208. else
  2209. s_itr_ += 3;
  2210. }
  2211. escaped = false;
  2212. }
  2213. ++s_itr_;
  2214. }
  2215. if (is_end(s_itr_))
  2216. {
  2217. t.set_error(token::e_err_string,initial_itr,s_itr_,base_itr_);
  2218. token_list_.push_back(t);
  2219. return;
  2220. }
  2221. if (!escaped_found)
  2222. t.set_string(initial_itr,s_itr_,base_itr_);
  2223. else
  2224. {
  2225. std::string parsed_string(initial_itr,s_itr_);
  2226. details::cleanup_escapes(parsed_string);
  2227. t.set_string(parsed_string, std::distance(base_itr_,initial_itr));
  2228. }
  2229. token_list_.push_back(t);
  2230. ++s_itr_;
  2231. return;
  2232. }
  2233. #endif
  2234. private:
  2235. token_list_t token_list_;
  2236. token_list_itr_t token_itr_;
  2237. token_list_itr_t store_token_itr_;
  2238. token_t eof_token_;
  2239. const char* base_itr_;
  2240. const char* s_itr_;
  2241. const char* s_end_;
  2242. friend class token_scanner;
  2243. friend class token_modifier;
  2244. friend class token_inserter;
  2245. friend class token_joiner;
  2246. };
  2247. class helper_interface
  2248. {
  2249. public:
  2250. virtual void init() { }
  2251. virtual void reset() { }
  2252. virtual bool result() { return true; }
  2253. virtual std::size_t process(generator&) { return 0; }
  2254. virtual ~helper_interface() { }
  2255. };
  2256. class token_scanner : public helper_interface
  2257. {
  2258. public:
  2259. virtual ~token_scanner()
  2260. {}
  2261. explicit token_scanner(const std::size_t& stride)
  2262. : stride_(stride)
  2263. {
  2264. if (stride > 4)
  2265. {
  2266. throw std::invalid_argument("token_scanner() - Invalid stride value");
  2267. }
  2268. }
  2269. inline std::size_t process(generator& g)
  2270. {
  2271. if (g.token_list_.size() >= stride_)
  2272. {
  2273. for (std::size_t i = 0; i < (g.token_list_.size() - stride_ + 1); ++i)
  2274. {
  2275. token t;
  2276. switch (stride_)
  2277. {
  2278. case 1 :
  2279. {
  2280. const token& t0 = g.token_list_[i];
  2281. if (!operator()(t0))
  2282. {
  2283. return i;
  2284. }
  2285. }
  2286. break;
  2287. case 2 :
  2288. {
  2289. const token& t0 = g.token_list_[i ];
  2290. const token& t1 = g.token_list_[i + 1];
  2291. if (!operator()(t0,t1))
  2292. {
  2293. return i;
  2294. }
  2295. }
  2296. break;
  2297. case 3 :
  2298. {
  2299. const token& t0 = g.token_list_[i ];
  2300. const token& t1 = g.token_list_[i + 1];
  2301. const token& t2 = g.token_list_[i + 2];
  2302. if (!operator()(t0,t1,t2))
  2303. {
  2304. return i;
  2305. }
  2306. }
  2307. break;
  2308. case 4 :
  2309. {
  2310. const token& t0 = g.token_list_[i ];
  2311. const token& t1 = g.token_list_[i + 1];
  2312. const token& t2 = g.token_list_[i + 2];
  2313. const token& t3 = g.token_list_[i + 3];
  2314. if (!operator()(t0,t1,t2,t3))
  2315. {
  2316. return i;
  2317. }
  2318. }
  2319. break;
  2320. }
  2321. }
  2322. }
  2323. return (g.token_list_.size() - stride_ + 1);
  2324. }
  2325. virtual bool operator()(const token&)
  2326. {
  2327. return false;
  2328. }
  2329. virtual bool operator()(const token&, const token&)
  2330. {
  2331. return false;
  2332. }
  2333. virtual bool operator()(const token&, const token&, const token&)
  2334. {
  2335. return false;
  2336. }
  2337. virtual bool operator()(const token&, const token&, const token&, const token&)
  2338. {
  2339. return false;
  2340. }
  2341. private:
  2342. std::size_t stride_;
  2343. };
  2344. class token_modifier : public helper_interface
  2345. {
  2346. public:
  2347. inline std::size_t process(generator& g)
  2348. {
  2349. std::size_t changes = 0;
  2350. for (std::size_t i = 0; i < g.token_list_.size(); ++i)
  2351. {
  2352. if (modify(g.token_list_[i])) changes++;
  2353. }
  2354. return changes;
  2355. }
  2356. virtual bool modify(token& t) = 0;
  2357. };
  2358. class token_inserter : public helper_interface
  2359. {
  2360. public:
  2361. explicit token_inserter(const std::size_t& stride)
  2362. : stride_(stride)
  2363. {
  2364. if (stride > 5)
  2365. {
  2366. throw std::invalid_argument("token_inserter() - Invalid stride value");
  2367. }
  2368. }
  2369. inline std::size_t process(generator& g)
  2370. {
  2371. if (g.token_list_.empty())
  2372. return 0;
  2373. else if (g.token_list_.size() < stride_)
  2374. return 0;
  2375. std::size_t changes = 0;
  2376. for (std::size_t i = 0; i < (g.token_list_.size() - stride_ + 1); ++i)
  2377. {
  2378. int insert_index = -1;
  2379. token t;
  2380. switch (stride_)
  2381. {
  2382. case 1 : insert_index = insert(g.token_list_[i],t);
  2383. break;
  2384. case 2 : insert_index = insert(g.token_list_[i],g.token_list_[i + 1],t);
  2385. break;
  2386. case 3 : insert_index = insert(g.token_list_[i],g.token_list_[i + 1],g.token_list_[i + 2],t);
  2387. break;
  2388. case 4 : insert_index = insert(g.token_list_[i],g.token_list_[i + 1],g.token_list_[i + 2],g.token_list_[i + 3],t);
  2389. break;
  2390. case 5 : insert_index = insert(g.token_list_[i],g.token_list_[i + 1],g.token_list_[i + 2],g.token_list_[i + 3],g.token_list_[i + 4],t);
  2391. break;
  2392. }
  2393. if ((insert_index >= 0) && (insert_index <= (static_cast<int>(stride_) + 1)))
  2394. {
  2395. g.token_list_.insert(g.token_list_.begin() + (i + insert_index),t);
  2396. changes++;
  2397. }
  2398. }
  2399. return changes;
  2400. }
  2401. inline virtual int insert(const token&, token& )
  2402. {
  2403. return -1;
  2404. }
  2405. inline virtual int insert(const token&, const token&, token&)
  2406. {
  2407. return -1;
  2408. }
  2409. inline virtual int insert(const token&, const token&, const token&, token&)
  2410. {
  2411. return -1;
  2412. }
  2413. inline virtual int insert(const token&, const token&, const token&, const token&, token&)
  2414. {
  2415. return -1;
  2416. }
  2417. inline virtual int insert(const token&, const token&, const token&, const token&, const token&, token&)
  2418. {
  2419. return -1;
  2420. }
  2421. private:
  2422. std::size_t stride_;
  2423. };
  2424. class token_joiner : public helper_interface
  2425. {
  2426. public:
  2427. token_joiner(const std::size_t& stride)
  2428. : stride_(stride)
  2429. {}
  2430. inline std::size_t process(generator& g)
  2431. {
  2432. if (g.token_list_.empty())
  2433. return 0;
  2434. switch (stride_)
  2435. {
  2436. case 2 : return process_stride_2(g);
  2437. case 3 : return process_stride_3(g);
  2438. default : return 0;
  2439. }
  2440. }
  2441. virtual bool join(const token&, const token&, token&) { return false; }
  2442. virtual bool join(const token&, const token&, const token&, token&) { return false; }
  2443. private:
  2444. inline std::size_t process_stride_2(generator& g)
  2445. {
  2446. if (g.token_list_.size() < 2)
  2447. return 0;
  2448. std::size_t changes = 0;
  2449. for (std::size_t i = 0; i < g.token_list_.size() - 1; ++i)
  2450. {
  2451. token t;
  2452. while (join(g.token_list_[i],g.token_list_[i + 1],t))
  2453. {
  2454. g.token_list_[i] = t;
  2455. g.token_list_.erase(g.token_list_.begin() + (i + 1));
  2456. ++changes;
  2457. }
  2458. }
  2459. return changes;
  2460. }
  2461. inline std::size_t process_stride_3(generator& g)
  2462. {
  2463. if (g.token_list_.size() < 3)
  2464. return 0;
  2465. std::size_t changes = 0;
  2466. for (std::size_t i = 0; i < g.token_list_.size() - 2; ++i)
  2467. {
  2468. token t;
  2469. while (join(g.token_list_[i],g.token_list_[i + 1],g.token_list_[i + 2],t))
  2470. {
  2471. g.token_list_[i] = t;
  2472. g.token_list_.erase(g.token_list_.begin() + (i + 1),
  2473. g.token_list_.begin() + (i + 3));
  2474. ++changes;
  2475. }
  2476. }
  2477. return changes;
  2478. }
  2479. std::size_t stride_;
  2480. };
  2481. namespace helper
  2482. {
  2483. inline void dump(lexer::generator& generator)
  2484. {
  2485. for (std::size_t i = 0; i < generator.size(); ++i)
  2486. {
  2487. lexer::token t = generator[i];
  2488. printf("Token[%02d] @ %03d %6s --> '%s'\n",
  2489. static_cast<int>(i),
  2490. static_cast<int>(t.position),
  2491. t.to_str(t.type).c_str(),
  2492. t.value.c_str());
  2493. }
  2494. }
  2495. class commutative_inserter : public lexer::token_inserter
  2496. {
  2497. public:
  2498. commutative_inserter()
  2499. : lexer::token_inserter(2)
  2500. {}
  2501. inline void ignore_symbol(const std::string& symbol)
  2502. {
  2503. ignore_set_.insert(symbol);
  2504. }
  2505. inline int insert(const lexer::token& t0, const lexer::token& t1, lexer::token& new_token)
  2506. {
  2507. bool match = false;
  2508. new_token.type = lexer::token::e_mul;
  2509. new_token.value = "*";
  2510. new_token.position = t1.position;
  2511. if (t0.type == lexer::token::e_symbol)
  2512. {
  2513. if (ignore_set_.end() != ignore_set_.find(t0.value))
  2514. {
  2515. return -1;
  2516. }
  2517. else if (!t0.value.empty() && ('$' == t0.value[0]))
  2518. {
  2519. return -1;
  2520. }
  2521. }
  2522. if (t1.type == lexer::token::e_symbol)
  2523. {
  2524. if (ignore_set_.end() != ignore_set_.find(t1.value))
  2525. {
  2526. return -1;
  2527. }
  2528. }
  2529. if ((t0.type == lexer::token::e_number ) && (t1.type == lexer::token::e_symbol )) match = true;
  2530. else if ((t0.type == lexer::token::e_number ) && (t1.type == lexer::token::e_lbracket )) match = true;
  2531. else if ((t0.type == lexer::token::e_number ) && (t1.type == lexer::token::e_lcrlbracket)) match = true;
  2532. else if ((t0.type == lexer::token::e_number ) && (t1.type == lexer::token::e_lsqrbracket)) match = true;
  2533. else if ((t0.type == lexer::token::e_symbol ) && (t1.type == lexer::token::e_number )) match = true;
  2534. else if ((t0.type == lexer::token::e_rbracket ) && (t1.type == lexer::token::e_number )) match = true;
  2535. else if ((t0.type == lexer::token::e_rcrlbracket) && (t1.type == lexer::token::e_number )) match = true;
  2536. else if ((t0.type == lexer::token::e_rsqrbracket) && (t1.type == lexer::token::e_number )) match = true;
  2537. else if ((t0.type == lexer::token::e_rbracket ) && (t1.type == lexer::token::e_symbol )) match = true;
  2538. else if ((t0.type == lexer::token::e_rcrlbracket) && (t1.type == lexer::token::e_symbol )) match = true;
  2539. else if ((t0.type == lexer::token::e_rsqrbracket) && (t1.type == lexer::token::e_symbol )) match = true;
  2540. return (match) ? 1 : -1;
  2541. }
  2542. private:
  2543. std::set<std::string,details::ilesscompare> ignore_set_;
  2544. };
  2545. class operator_joiner : public token_joiner
  2546. {
  2547. public:
  2548. operator_joiner(const std::size_t& stride)
  2549. : token_joiner(stride)
  2550. {}
  2551. inline bool join(const lexer::token& t0, const lexer::token& t1, lexer::token& t)
  2552. {
  2553. // ': =' --> ':='
  2554. if ((t0.type == lexer::token::e_colon) && (t1.type == lexer::token::e_eq))
  2555. {
  2556. t.type = lexer::token::e_assign;
  2557. t.value = ":=";
  2558. t.position = t0.position;
  2559. return true;
  2560. }
  2561. // '+ =' --> '+='
  2562. else if ((t0.type == lexer::token::e_add) && (t1.type == lexer::token::e_eq))
  2563. {
  2564. t.type = lexer::token::e_addass;
  2565. t.value = "+=";
  2566. t.position = t0.position;
  2567. return true;
  2568. }
  2569. // '- =' --> '-='
  2570. else if ((t0.type == lexer::token::e_sub) && (t1.type == lexer::token::e_eq))
  2571. {
  2572. t.type = lexer::token::e_subass;
  2573. t.value = "-=";
  2574. t.position = t0.position;
  2575. return true;
  2576. }
  2577. // '* =' --> '*='
  2578. else if ((t0.type == lexer::token::e_mul) && (t1.type == lexer::token::e_eq))
  2579. {
  2580. t.type = lexer::token::e_mulass;
  2581. t.value = "*=";
  2582. t.position = t0.position;
  2583. return true;
  2584. }
  2585. // '/ =' --> '/='
  2586. else if ((t0.type == lexer::token::e_div) && (t1.type == lexer::token::e_eq))
  2587. {
  2588. t.type = lexer::token::e_divass;
  2589. t.value = "/=";
  2590. t.position = t0.position;
  2591. return true;
  2592. }
  2593. // '% =' --> '%='
  2594. else if ((t0.type == lexer::token::e_mod) && (t1.type == lexer::token::e_eq))
  2595. {
  2596. t.type = lexer::token::e_modass;
  2597. t.value = "%=";
  2598. t.position = t0.position;
  2599. return true;
  2600. }
  2601. // '> =' --> '>='
  2602. else if ((t0.type == lexer::token::e_gt) && (t1.type == lexer::token::e_eq))
  2603. {
  2604. t.type = lexer::token::e_gte;
  2605. t.value = ">=";
  2606. t.position = t0.position;
  2607. return true;
  2608. }
  2609. // '< =' --> '<='
  2610. else if ((t0.type == lexer::token::e_lt) && (t1.type == lexer::token::e_eq))
  2611. {
  2612. t.type = lexer::token::e_lte;
  2613. t.value = "<=";
  2614. t.position = t0.position;
  2615. return true;
  2616. }
  2617. // '= =' --> '=='
  2618. else if ((t0.type == lexer::token::e_eq) && (t1.type == lexer::token::e_eq))
  2619. {
  2620. t.type = lexer::token::e_eq;
  2621. t.value = "==";
  2622. t.position = t0.position;
  2623. return true;
  2624. }
  2625. // '! =' --> '!='
  2626. else if ((static_cast<char>(t0.type) == '!') && (t1.type == lexer::token::e_eq))
  2627. {
  2628. t.type = lexer::token::e_ne;
  2629. t.value = "!=";
  2630. t.position = t0.position;
  2631. return true;
  2632. }
  2633. // '< >' --> '<>'
  2634. else if ((t0.type == lexer::token::e_lt) && (t1.type == lexer::token::e_gt))
  2635. {
  2636. t.type = lexer::token::e_ne;
  2637. t.value = "<>";
  2638. t.position = t0.position;
  2639. return true;
  2640. }
  2641. // '<= >' --> '<=>'
  2642. else if ((t0.type == lexer::token::e_lte) && (t1.type == lexer::token::e_gt))
  2643. {
  2644. t.type = lexer::token::e_swap;
  2645. t.value = "<=>";
  2646. t.position = t0.position;
  2647. return true;
  2648. }
  2649. else
  2650. return false;
  2651. }
  2652. inline bool join(const lexer::token& t0, const lexer::token& t1, const lexer::token& t2, lexer::token& t)
  2653. {
  2654. // '[ * ]' --> '[*]'
  2655. if (
  2656. (t0.type == lexer::token::e_lsqrbracket) &&
  2657. (t1.type == lexer::token::e_mul ) &&
  2658. (t2.type == lexer::token::e_rsqrbracket)
  2659. )
  2660. {
  2661. t.type = lexer::token::e_symbol;
  2662. t.value = "[*]";
  2663. t.position = t0.position;
  2664. return true;
  2665. }
  2666. else
  2667. return false;
  2668. }
  2669. };
  2670. class bracket_checker : public lexer::token_scanner
  2671. {
  2672. public:
  2673. bracket_checker()
  2674. : token_scanner(1),
  2675. state_(true)
  2676. {}
  2677. bool result()
  2678. {
  2679. if (!stack_.empty())
  2680. {
  2681. lexer::token t;
  2682. t.value = stack_.top().first;
  2683. t.position = stack_.top().second;
  2684. error_token_ = t;
  2685. state_ = false;
  2686. return false;
  2687. }
  2688. else
  2689. return state_;
  2690. }
  2691. lexer::token error_token()
  2692. {
  2693. return error_token_;
  2694. }
  2695. void reset()
  2696. {
  2697. // Why? because msvc doesn't support swap properly.
  2698. stack_ = std::stack<std::pair<char,std::size_t> >();
  2699. state_ = true;
  2700. error_token_.clear();
  2701. }
  2702. bool operator()(const lexer::token& t)
  2703. {
  2704. if (
  2705. !t.value.empty() &&
  2706. (lexer::token::e_string != t.type) &&
  2707. (lexer::token::e_symbol != t.type) &&
  2708. exprtk::details::is_bracket(t.value[0])
  2709. )
  2710. {
  2711. char c = t.value[0];
  2712. if (t.type == lexer::token::e_lbracket) stack_.push(std::make_pair(')',t.position));
  2713. else if (t.type == lexer::token::e_lcrlbracket) stack_.push(std::make_pair('}',t.position));
  2714. else if (t.type == lexer::token::e_lsqrbracket) stack_.push(std::make_pair(']',t.position));
  2715. else if (exprtk::details::is_right_bracket(c))
  2716. {
  2717. if (stack_.empty())
  2718. {
  2719. state_ = false;
  2720. error_token_ = t;
  2721. return false;
  2722. }
  2723. else if (c != stack_.top().first)
  2724. {
  2725. state_ = false;
  2726. error_token_ = t;
  2727. return false;
  2728. }
  2729. else
  2730. stack_.pop();
  2731. }
  2732. }
  2733. return true;
  2734. }
  2735. private:
  2736. bool state_;
  2737. std::stack<std::pair<char,std::size_t> > stack_;
  2738. lexer::token error_token_;
  2739. };
  2740. class numeric_checker : public lexer::token_scanner
  2741. {
  2742. public:
  2743. numeric_checker()
  2744. : token_scanner (1),
  2745. current_index_(0)
  2746. {}
  2747. bool result()
  2748. {
  2749. return error_list_.empty();
  2750. }
  2751. void reset()
  2752. {
  2753. error_list_.clear();
  2754. current_index_ = 0;
  2755. }
  2756. bool operator()(const lexer::token& t)
  2757. {
  2758. if (token::e_number == t.type)
  2759. {
  2760. double v;
  2761. if (!exprtk::details::string_to_real(t.value,v))
  2762. {
  2763. error_list_.push_back(current_index_);
  2764. }
  2765. }
  2766. ++current_index_;
  2767. return true;
  2768. }
  2769. std::size_t error_count() const
  2770. {
  2771. return error_list_.size();
  2772. }
  2773. std::size_t error_index(const std::size_t& i)
  2774. {
  2775. if (i < error_list_.size())
  2776. return error_list_[i];
  2777. else
  2778. return std::numeric_limits<std::size_t>::max();
  2779. }
  2780. void clear_errors()
  2781. {
  2782. error_list_.clear();
  2783. }
  2784. private:
  2785. std::size_t current_index_;
  2786. std::vector<std::size_t> error_list_;
  2787. };
  2788. class symbol_replacer : public lexer::token_modifier
  2789. {
  2790. private:
  2791. typedef std::map<std::string,std::pair<std::string,token::token_type>,details::ilesscompare> replace_map_t;
  2792. public:
  2793. bool remove(const std::string& target_symbol)
  2794. {
  2795. replace_map_t::iterator itr = replace_map_.find(target_symbol);
  2796. if (replace_map_.end() == itr)
  2797. return false;
  2798. replace_map_.erase(itr);
  2799. return true;
  2800. }
  2801. bool add_replace(const std::string& target_symbol,
  2802. const std::string& replace_symbol,
  2803. const lexer::token::token_type token_type = lexer::token::e_symbol)
  2804. {
  2805. replace_map_t::iterator itr = replace_map_.find(target_symbol);
  2806. if (replace_map_.end() != itr)
  2807. {
  2808. return false;
  2809. }
  2810. replace_map_[target_symbol] = std::make_pair(replace_symbol,token_type);
  2811. return true;
  2812. }
  2813. void clear()
  2814. {
  2815. replace_map_.clear();
  2816. }
  2817. private:
  2818. bool modify(lexer::token& t)
  2819. {
  2820. if (lexer::token::e_symbol == t.type)
  2821. {
  2822. if (replace_map_.empty())
  2823. return false;
  2824. replace_map_t::iterator itr = replace_map_.find(t.value);
  2825. if (replace_map_.end() != itr)
  2826. {
  2827. t.value = itr->second.first;
  2828. t.type = itr->second.second;
  2829. return true;
  2830. }
  2831. }
  2832. return false;
  2833. }
  2834. replace_map_t replace_map_;
  2835. };
  2836. class sequence_validator : public lexer::token_scanner
  2837. {
  2838. private:
  2839. typedef std::pair<lexer::token::token_type,lexer::token::token_type> token_pair_t;
  2840. typedef std::set<token_pair_t> set_t;
  2841. public:
  2842. sequence_validator()
  2843. : lexer::token_scanner(2)
  2844. {
  2845. add_invalid(lexer::token::e_number ,lexer::token::e_number );
  2846. add_invalid(lexer::token::e_string ,lexer::token::e_string );
  2847. add_invalid(lexer::token::e_number ,lexer::token::e_string );
  2848. add_invalid(lexer::token::e_string ,lexer::token::e_number );
  2849. add_invalid(lexer::token::e_string ,lexer::token::e_ternary);
  2850. add_invalid_set1(lexer::token::e_assign );
  2851. add_invalid_set1(lexer::token::e_shr );
  2852. add_invalid_set1(lexer::token::e_shl );
  2853. add_invalid_set1(lexer::token::e_lte );
  2854. add_invalid_set1(lexer::token::e_ne );
  2855. add_invalid_set1(lexer::token::e_gte );
  2856. add_invalid_set1(lexer::token::e_lt );
  2857. add_invalid_set1(lexer::token::e_gt );
  2858. add_invalid_set1(lexer::token::e_eq );
  2859. add_invalid_set1(lexer::token::e_comma );
  2860. add_invalid_set1(lexer::token::e_add );
  2861. add_invalid_set1(lexer::token::e_sub );
  2862. add_invalid_set1(lexer::token::e_div );
  2863. add_invalid_set1(lexer::token::e_mul );
  2864. add_invalid_set1(lexer::token::e_mod );
  2865. add_invalid_set1(lexer::token::e_pow );
  2866. add_invalid_set1(lexer::token::e_colon );
  2867. add_invalid_set1(lexer::token::e_ternary);
  2868. }
  2869. bool result()
  2870. {
  2871. return error_list_.empty();
  2872. }
  2873. bool operator()(const lexer::token& t0, const lexer::token& t1)
  2874. {
  2875. set_t::value_type p = std::make_pair(t0.type,t1.type);
  2876. if (invalid_bracket_check(t0.type,t1.type))
  2877. {
  2878. error_list_.push_back(std::make_pair(t0,t1));
  2879. }
  2880. else if (invalid_comb_.find(p) != invalid_comb_.end())
  2881. {
  2882. error_list_.push_back(std::make_pair(t0,t1));
  2883. }
  2884. return true;
  2885. }
  2886. std::size_t error_count()
  2887. {
  2888. return error_list_.size();
  2889. }
  2890. std::pair<lexer::token,lexer::token> error(const std::size_t index)
  2891. {
  2892. if (index < error_list_.size())
  2893. {
  2894. return error_list_[index];
  2895. }
  2896. else
  2897. {
  2898. static const lexer::token error_token;
  2899. return std::make_pair(error_token,error_token);
  2900. }
  2901. }
  2902. void clear_errors()
  2903. {
  2904. error_list_.clear();
  2905. }
  2906. private:
  2907. void add_invalid(lexer::token::token_type base, lexer::token::token_type t)
  2908. {
  2909. invalid_comb_.insert(std::make_pair(base,t));
  2910. }
  2911. void add_invalid_set1(lexer::token::token_type t)
  2912. {
  2913. add_invalid(t,lexer::token::e_assign);
  2914. add_invalid(t,lexer::token::e_shr );
  2915. add_invalid(t,lexer::token::e_shl );
  2916. add_invalid(t,lexer::token::e_lte );
  2917. add_invalid(t,lexer::token::e_ne );
  2918. add_invalid(t,lexer::token::e_gte );
  2919. add_invalid(t,lexer::token::e_lt );
  2920. add_invalid(t,lexer::token::e_gt );
  2921. add_invalid(t,lexer::token::e_eq );
  2922. add_invalid(t,lexer::token::e_comma );
  2923. add_invalid(t,lexer::token::e_div );
  2924. add_invalid(t,lexer::token::e_mul );
  2925. add_invalid(t,lexer::token::e_mod );
  2926. add_invalid(t,lexer::token::e_pow );
  2927. add_invalid(t,lexer::token::e_colon );
  2928. }
  2929. bool invalid_bracket_check(lexer::token::token_type base, lexer::token::token_type t)
  2930. {
  2931. if (details::is_right_bracket(static_cast<char>(base)))
  2932. {
  2933. switch (t)
  2934. {
  2935. case lexer::token::e_assign : return (']' != base);
  2936. case lexer::token::e_string : return true;
  2937. default : return false;
  2938. }
  2939. }
  2940. else if (details::is_left_bracket(static_cast<char>(base)))
  2941. {
  2942. if (details::is_right_bracket(static_cast<char>(t)))
  2943. return false;
  2944. else if (details::is_left_bracket(static_cast<char>(t)))
  2945. return false;
  2946. else
  2947. {
  2948. switch (t)
  2949. {
  2950. case lexer::token::e_number : return false;
  2951. case lexer::token::e_symbol : return false;
  2952. case lexer::token::e_string : return false;
  2953. case lexer::token::e_add : return false;
  2954. case lexer::token::e_sub : return false;
  2955. case lexer::token::e_colon : return false;
  2956. case lexer::token::e_ternary : return false;
  2957. default : return true;
  2958. }
  2959. }
  2960. }
  2961. else if (details::is_right_bracket(static_cast<char>(t)))
  2962. {
  2963. switch (base)
  2964. {
  2965. case lexer::token::e_number : return false;
  2966. case lexer::token::e_symbol : return false;
  2967. case lexer::token::e_string : return false;
  2968. case lexer::token::e_eof : return false;
  2969. case lexer::token::e_colon : return false;
  2970. case lexer::token::e_ternary : return false;
  2971. default : return true;
  2972. }
  2973. }
  2974. else if (details::is_left_bracket(static_cast<char>(t)))
  2975. {
  2976. switch (base)
  2977. {
  2978. case lexer::token::e_rbracket : return true;
  2979. case lexer::token::e_rsqrbracket : return true;
  2980. case lexer::token::e_rcrlbracket : return true;
  2981. default : return false;
  2982. }
  2983. }
  2984. return false;
  2985. }
  2986. set_t invalid_comb_;
  2987. std::vector<std::pair<lexer::token,lexer::token> > error_list_;
  2988. };
  2989. struct helper_assembly
  2990. {
  2991. inline bool register_scanner(lexer::token_scanner* scanner)
  2992. {
  2993. if (token_scanner_list.end() != std::find(token_scanner_list.begin(),
  2994. token_scanner_list.end(),
  2995. scanner))
  2996. {
  2997. return false;
  2998. }
  2999. token_scanner_list.push_back(scanner);
  3000. return true;
  3001. }
  3002. inline bool register_modifier(lexer::token_modifier* modifier)
  3003. {
  3004. if (token_modifier_list.end() != std::find(token_modifier_list.begin(),
  3005. token_modifier_list.end(),
  3006. modifier))
  3007. {
  3008. return false;
  3009. }
  3010. token_modifier_list.push_back(modifier);
  3011. return true;
  3012. }
  3013. inline bool register_joiner(lexer::token_joiner* joiner)
  3014. {
  3015. if (token_joiner_list.end() != std::find(token_joiner_list.begin(),
  3016. token_joiner_list.end(),
  3017. joiner))
  3018. {
  3019. return false;
  3020. }
  3021. token_joiner_list.push_back(joiner);
  3022. return true;
  3023. }
  3024. inline bool register_inserter(lexer::token_inserter* inserter)
  3025. {
  3026. if (token_inserter_list.end() != std::find(token_inserter_list.begin(),
  3027. token_inserter_list.end(),
  3028. inserter))
  3029. {
  3030. return false;
  3031. }
  3032. token_inserter_list.push_back(inserter);
  3033. return true;
  3034. }
  3035. inline bool run_modifiers(lexer::generator& g)
  3036. {
  3037. error_token_modifier = reinterpret_cast<lexer::token_modifier*>(0);
  3038. bool result = true;
  3039. for (std::size_t i = 0; i < token_modifier_list.size(); ++i)
  3040. {
  3041. lexer::token_modifier& modifier = (*token_modifier_list[i]);
  3042. modifier.reset();
  3043. modifier.process(g);
  3044. if (!modifier.result())
  3045. {
  3046. error_token_modifier = token_modifier_list[i];
  3047. return false;
  3048. }
  3049. }
  3050. return result;
  3051. }
  3052. inline bool run_joiners(lexer::generator& g)
  3053. {
  3054. error_token_joiner = reinterpret_cast<lexer::token_joiner*>(0);
  3055. bool result = true;
  3056. for (std::size_t i = 0; i < token_joiner_list.size(); ++i)
  3057. {
  3058. lexer::token_joiner& joiner = (*token_joiner_list[i]);
  3059. joiner.reset();
  3060. joiner.process(g);
  3061. if (!joiner.result())
  3062. {
  3063. error_token_joiner = token_joiner_list[i];
  3064. return false;
  3065. }
  3066. }
  3067. return result;
  3068. }
  3069. inline bool run_inserters(lexer::generator& g)
  3070. {
  3071. error_token_inserter = reinterpret_cast<lexer::token_inserter*>(0);
  3072. bool result = true;
  3073. for (std::size_t i = 0; i < token_inserter_list.size(); ++i)
  3074. {
  3075. lexer::token_inserter& inserter = (*token_inserter_list[i]);
  3076. inserter.reset();
  3077. inserter.process(g);
  3078. if (!inserter.result())
  3079. {
  3080. error_token_inserter = token_inserter_list[i];
  3081. return false;
  3082. }
  3083. }
  3084. return result;
  3085. }
  3086. inline bool run_scanners(lexer::generator& g)
  3087. {
  3088. error_token_scanner = reinterpret_cast<lexer::token_scanner*>(0);
  3089. bool result = true;
  3090. for (std::size_t i = 0; i < token_scanner_list.size(); ++i)
  3091. {
  3092. lexer::token_scanner& scanner = (*token_scanner_list[i]);
  3093. scanner.reset();
  3094. scanner.process(g);
  3095. if (!scanner.result())
  3096. {
  3097. error_token_scanner = token_scanner_list[i];
  3098. return false;
  3099. }
  3100. }
  3101. return result;
  3102. }
  3103. std::vector<lexer::token_scanner*> token_scanner_list;
  3104. std::vector<lexer::token_modifier*> token_modifier_list;
  3105. std::vector<lexer::token_joiner*> token_joiner_list;
  3106. std::vector<lexer::token_inserter*> token_inserter_list;
  3107. lexer::token_scanner* error_token_scanner;
  3108. lexer::token_modifier* error_token_modifier;
  3109. lexer::token_joiner* error_token_joiner;
  3110. lexer::token_inserter* error_token_inserter;
  3111. };
  3112. }
  3113. class parser_helper
  3114. {
  3115. public:
  3116. typedef token token_t;
  3117. typedef generator generator_t;
  3118. inline bool init(const std::string& str)
  3119. {
  3120. if (!lexer_.process(str))
  3121. {
  3122. return false;
  3123. }
  3124. lexer_.begin();
  3125. next_token();
  3126. return true;
  3127. }
  3128. inline generator_t& lexer()
  3129. {
  3130. return lexer_;
  3131. }
  3132. inline const generator_t& lexer() const
  3133. {
  3134. return lexer_;
  3135. }
  3136. inline void store_token()
  3137. {
  3138. lexer_.store();
  3139. store_current_token_ = current_token_;
  3140. }
  3141. inline void restore_token()
  3142. {
  3143. lexer_.restore();
  3144. current_token_ = store_current_token_;
  3145. }
  3146. inline void next_token()
  3147. {
  3148. current_token_ = lexer_.next_token();
  3149. }
  3150. inline const token_t& current_token() const
  3151. {
  3152. return current_token_;
  3153. }
  3154. inline bool token_is(const token_t::token_type& ttype, const bool advance_token = true)
  3155. {
  3156. if (current_token().type != ttype)
  3157. {
  3158. return false;
  3159. }
  3160. if (advance_token)
  3161. {
  3162. next_token();
  3163. }
  3164. return true;
  3165. }
  3166. inline bool token_is(const token_t::token_type& ttype,
  3167. const std::string& value,
  3168. const bool advance_token = true)
  3169. {
  3170. if (
  3171. (current_token().type != ttype) ||
  3172. !exprtk::details::imatch(value,current_token().value)
  3173. )
  3174. {
  3175. return false;
  3176. }
  3177. if (advance_token)
  3178. {
  3179. next_token();
  3180. }
  3181. return true;
  3182. }
  3183. inline bool token_is_then_assign(const token_t::token_type& ttype,
  3184. std::string& token,
  3185. const bool advance_token = true)
  3186. {
  3187. if (current_token_.type != ttype)
  3188. {
  3189. return false;
  3190. }
  3191. token = current_token_.value;
  3192. if (advance_token)
  3193. {
  3194. next_token();
  3195. }
  3196. return true;
  3197. }
  3198. template <typename Allocator,
  3199. template <typename,typename> class Container>
  3200. inline bool token_is_then_assign(const token_t::token_type& ttype,
  3201. Container<std::string,Allocator>& token_list,
  3202. const bool advance_token = true)
  3203. {
  3204. if (current_token_.type != ttype)
  3205. {
  3206. return false;
  3207. }
  3208. token_list.push_back(current_token_.value);
  3209. if (advance_token)
  3210. {
  3211. next_token();
  3212. }
  3213. return true;
  3214. }
  3215. inline bool peek_token_is(const token_t::token_type& ttype)
  3216. {
  3217. return (lexer_.peek_next_token().type == ttype);
  3218. }
  3219. inline bool peek_token_is(const std::string& s)
  3220. {
  3221. return (exprtk::details::imatch(lexer_.peek_next_token().value,s));
  3222. }
  3223. private:
  3224. generator_t lexer_;
  3225. token_t current_token_;
  3226. token_t store_current_token_;
  3227. };
  3228. }
  3229. template <typename T> class results_context;
  3230. template <typename T>
  3231. struct type_store
  3232. {
  3233. enum store_type
  3234. {
  3235. e_unknown,
  3236. e_scalar,
  3237. e_vector,
  3238. e_string
  3239. };
  3240. type_store()
  3241. : size(0),
  3242. data(0),
  3243. type(e_unknown)
  3244. {}
  3245. std::size_t size;
  3246. void* data;
  3247. store_type type;
  3248. class parameter_list
  3249. {
  3250. public:
  3251. parameter_list(std::vector<type_store>& pl)
  3252. : parameter_list_(pl)
  3253. {}
  3254. inline bool empty() const
  3255. {
  3256. return parameter_list_.empty();
  3257. }
  3258. inline std::size_t size() const
  3259. {
  3260. return parameter_list_.size();
  3261. }
  3262. inline type_store& operator[](const std::size_t& index)
  3263. {
  3264. return parameter_list_[index];
  3265. }
  3266. inline const type_store& operator[](const std::size_t& index) const
  3267. {
  3268. return parameter_list_[index];
  3269. }
  3270. inline type_store& front()
  3271. {
  3272. return parameter_list_[0];
  3273. }
  3274. inline const type_store& front() const
  3275. {
  3276. return parameter_list_[0];
  3277. }
  3278. inline type_store& back()
  3279. {
  3280. return parameter_list_.back();
  3281. }
  3282. inline const type_store& back() const
  3283. {
  3284. return parameter_list_.back();
  3285. }
  3286. private:
  3287. std::vector<type_store>& parameter_list_;
  3288. friend class results_context<T>;
  3289. };
  3290. template <typename ViewType>
  3291. struct type_view
  3292. {
  3293. typedef type_store<T> type_store_t;
  3294. typedef ViewType value_t;
  3295. type_view(type_store_t& ts)
  3296. : ts_(ts),
  3297. data_(reinterpret_cast<value_t*>(ts_.data))
  3298. {}
  3299. inline std::size_t size() const
  3300. {
  3301. return ts_.size;
  3302. }
  3303. inline value_t& operator[](const std::size_t& i)
  3304. {
  3305. return data_[i];
  3306. }
  3307. inline const value_t& operator[](const std::size_t& i) const
  3308. {
  3309. return data_[i];
  3310. }
  3311. inline const value_t* begin() const { return data_; }
  3312. inline value_t* begin() { return data_; }
  3313. inline const value_t* end() const
  3314. {
  3315. return static_cast<value_t*>(data_ + ts_.size);
  3316. }
  3317. inline value_t* end()
  3318. {
  3319. return static_cast<value_t*>(data_ + ts_.size);
  3320. }
  3321. type_store_t& ts_;
  3322. value_t* data_;
  3323. };
  3324. typedef type_view<T> vector_view;
  3325. typedef type_view<char> string_view;
  3326. struct scalar_view
  3327. {
  3328. typedef type_store<T> type_store_t;
  3329. typedef T value_t;
  3330. scalar_view(type_store_t& ts)
  3331. : v_(*reinterpret_cast<value_t*>(ts.data))
  3332. {}
  3333. scalar_view(const type_store_t& ts)
  3334. : v_(*reinterpret_cast<value_t*>(const_cast<type_store_t&>(ts).data))
  3335. {}
  3336. value_t& operator()()
  3337. {
  3338. return v_;
  3339. }
  3340. const value_t& operator()() const
  3341. {
  3342. return v_;
  3343. }
  3344. T& v_;
  3345. };
  3346. };
  3347. template <typename StringView>
  3348. inline std::string to_str(const StringView& view)
  3349. {
  3350. return std::string(view.begin(),view.size());
  3351. }
  3352. namespace details
  3353. {
  3354. template <typename T> class return_node;
  3355. template <typename T> class return_envelope_node;
  3356. }
  3357. template <typename T>
  3358. class results_context
  3359. {
  3360. public:
  3361. typedef type_store<T> type_store_t;
  3362. results_context()
  3363. : results_available_(false)
  3364. {}
  3365. inline std::size_t count() const
  3366. {
  3367. if (results_available_)
  3368. return parameter_list_.size();
  3369. else
  3370. return 0;
  3371. }
  3372. inline type_store_t& operator[](const std::size_t& index)
  3373. {
  3374. return parameter_list_[index];
  3375. }
  3376. inline const type_store_t& operator[](const std::size_t& index) const
  3377. {
  3378. return parameter_list_[index];
  3379. }
  3380. private:
  3381. inline void clear()
  3382. {
  3383. results_available_ = false;
  3384. }
  3385. typedef std::vector<type_store_t> ts_list_t;
  3386. typedef typename type_store_t::parameter_list parameter_list_t;
  3387. inline void assign(const parameter_list_t& pl)
  3388. {
  3389. parameter_list_ = pl.parameter_list_;
  3390. results_available_ = true;
  3391. }
  3392. bool results_available_;
  3393. ts_list_t parameter_list_;
  3394. friend class details::return_node<T>;
  3395. friend class details::return_envelope_node<T>;
  3396. };
  3397. namespace details
  3398. {
  3399. enum operator_type
  3400. {
  3401. e_default , e_null , e_add , e_sub ,
  3402. e_mul , e_div , e_mod , e_pow ,
  3403. e_atan2 , e_min , e_max , e_avg ,
  3404. e_sum , e_prod , e_lt , e_lte ,
  3405. e_eq , e_equal , e_ne , e_nequal ,
  3406. e_gte , e_gt , e_and , e_nand ,
  3407. e_or , e_nor , e_xor , e_xnor ,
  3408. e_mand , e_mor , e_scand , e_scor ,
  3409. e_shr , e_shl , e_abs , e_acos ,
  3410. e_acosh , e_asin , e_asinh , e_atan ,
  3411. e_atanh , e_ceil , e_cos , e_cosh ,
  3412. e_exp , e_expm1 , e_floor , e_log ,
  3413. e_log10 , e_log2 , e_log1p , e_logn ,
  3414. e_neg , e_pos , e_round , e_roundn ,
  3415. e_root , e_sqrt , e_sin , e_sinc ,
  3416. e_sinh , e_sec , e_csc , e_tan ,
  3417. e_tanh , e_cot , e_clamp , e_iclamp ,
  3418. e_inrange , e_sgn , e_r2d , e_d2r ,
  3419. e_d2g , e_g2d , e_hypot , e_notl ,
  3420. e_erf , e_erfc , e_ncdf , e_frac ,
  3421. e_trunc , e_assign , e_addass , e_subass ,
  3422. e_mulass , e_divass , e_modass , e_in ,
  3423. e_like , e_ilike , e_multi , e_swap ,
  3424. // Do not add new functions/operators after this point.
  3425. e_sf00 = 1000, e_sf01 = 1001, e_sf02 = 1002, e_sf03 = 1003,
  3426. e_sf04 = 1004, e_sf05 = 1005, e_sf06 = 1006, e_sf07 = 1007,
  3427. e_sf08 = 1008, e_sf09 = 1009, e_sf10 = 1010, e_sf11 = 1011,
  3428. e_sf12 = 1012, e_sf13 = 1013, e_sf14 = 1014, e_sf15 = 1015,
  3429. e_sf16 = 1016, e_sf17 = 1017, e_sf18 = 1018, e_sf19 = 1019,
  3430. e_sf20 = 1020, e_sf21 = 1021, e_sf22 = 1022, e_sf23 = 1023,
  3431. e_sf24 = 1024, e_sf25 = 1025, e_sf26 = 1026, e_sf27 = 1027,
  3432. e_sf28 = 1028, e_sf29 = 1029, e_sf30 = 1030, e_sf31 = 1031,
  3433. e_sf32 = 1032, e_sf33 = 1033, e_sf34 = 1034, e_sf35 = 1035,
  3434. e_sf36 = 1036, e_sf37 = 1037, e_sf38 = 1038, e_sf39 = 1039,
  3435. e_sf40 = 1040, e_sf41 = 1041, e_sf42 = 1042, e_sf43 = 1043,
  3436. e_sf44 = 1044, e_sf45 = 1045, e_sf46 = 1046, e_sf47 = 1047,
  3437. e_sf48 = 1048, e_sf49 = 1049, e_sf50 = 1050, e_sf51 = 1051,
  3438. e_sf52 = 1052, e_sf53 = 1053, e_sf54 = 1054, e_sf55 = 1055,
  3439. e_sf56 = 1056, e_sf57 = 1057, e_sf58 = 1058, e_sf59 = 1059,
  3440. e_sf60 = 1060, e_sf61 = 1061, e_sf62 = 1062, e_sf63 = 1063,
  3441. e_sf64 = 1064, e_sf65 = 1065, e_sf66 = 1066, e_sf67 = 1067,
  3442. e_sf68 = 1068, e_sf69 = 1069, e_sf70 = 1070, e_sf71 = 1071,
  3443. e_sf72 = 1072, e_sf73 = 1073, e_sf74 = 1074, e_sf75 = 1075,
  3444. e_sf76 = 1076, e_sf77 = 1077, e_sf78 = 1078, e_sf79 = 1079,
  3445. e_sf80 = 1080, e_sf81 = 1081, e_sf82 = 1082, e_sf83 = 1083,
  3446. e_sf84 = 1084, e_sf85 = 1085, e_sf86 = 1086, e_sf87 = 1087,
  3447. e_sf88 = 1088, e_sf89 = 1089, e_sf90 = 1090, e_sf91 = 1091,
  3448. e_sf92 = 1092, e_sf93 = 1093, e_sf94 = 1094, e_sf95 = 1095,
  3449. e_sf96 = 1096, e_sf97 = 1097, e_sf98 = 1098, e_sf99 = 1099,
  3450. e_sffinal = 1100,
  3451. e_sf4ext00 = 2000, e_sf4ext01 = 2001, e_sf4ext02 = 2002, e_sf4ext03 = 2003,
  3452. e_sf4ext04 = 2004, e_sf4ext05 = 2005, e_sf4ext06 = 2006, e_sf4ext07 = 2007,
  3453. e_sf4ext08 = 2008, e_sf4ext09 = 2009, e_sf4ext10 = 2010, e_sf4ext11 = 2011,
  3454. e_sf4ext12 = 2012, e_sf4ext13 = 2013, e_sf4ext14 = 2014, e_sf4ext15 = 2015,
  3455. e_sf4ext16 = 2016, e_sf4ext17 = 2017, e_sf4ext18 = 2018, e_sf4ext19 = 2019,
  3456. e_sf4ext20 = 2020, e_sf4ext21 = 2021, e_sf4ext22 = 2022, e_sf4ext23 = 2023,
  3457. e_sf4ext24 = 2024, e_sf4ext25 = 2025, e_sf4ext26 = 2026, e_sf4ext27 = 2027,
  3458. e_sf4ext28 = 2028, e_sf4ext29 = 2029, e_sf4ext30 = 2030, e_sf4ext31 = 2031,
  3459. e_sf4ext32 = 2032, e_sf4ext33 = 2033, e_sf4ext34 = 2034, e_sf4ext35 = 2035,
  3460. e_sf4ext36 = 2036, e_sf4ext37 = 2037, e_sf4ext38 = 2038, e_sf4ext39 = 2039,
  3461. e_sf4ext40 = 2040, e_sf4ext41 = 2041, e_sf4ext42 = 2042, e_sf4ext43 = 2043,
  3462. e_sf4ext44 = 2044, e_sf4ext45 = 2045, e_sf4ext46 = 2046, e_sf4ext47 = 2047,
  3463. e_sf4ext48 = 2048, e_sf4ext49 = 2049, e_sf4ext50 = 2050, e_sf4ext51 = 2051,
  3464. e_sf4ext52 = 2052, e_sf4ext53 = 2053, e_sf4ext54 = 2054, e_sf4ext55 = 2055,
  3465. e_sf4ext56 = 2056, e_sf4ext57 = 2057, e_sf4ext58 = 2058, e_sf4ext59 = 2059
  3466. };
  3467. struct base_operation_t
  3468. {
  3469. base_operation_t(const operator_type t, const unsigned int& np)
  3470. : type(t),
  3471. num_params(np)
  3472. {}
  3473. operator_type type;
  3474. unsigned int num_params;
  3475. };
  3476. namespace numeric
  3477. {
  3478. namespace details
  3479. {
  3480. template <typename T>
  3481. inline T process_impl(const operator_type operation, const T arg)
  3482. {
  3483. switch (operation)
  3484. {
  3485. case e_abs : return numeric::abs (arg);
  3486. case e_acos : return numeric::acos (arg);
  3487. case e_acosh : return numeric::acosh(arg);
  3488. case e_asin : return numeric::asin (arg);
  3489. case e_asinh : return numeric::asinh(arg);
  3490. case e_atan : return numeric::atan (arg);
  3491. case e_atanh : return numeric::atanh(arg);
  3492. case e_ceil : return numeric::ceil (arg);
  3493. case e_cos : return numeric::cos (arg);
  3494. case e_cosh : return numeric::cosh (arg);
  3495. case e_exp : return numeric::exp (arg);
  3496. case e_expm1 : return numeric::expm1(arg);
  3497. case e_floor : return numeric::floor(arg);
  3498. case e_log : return numeric::log (arg);
  3499. case e_log10 : return numeric::log10(arg);
  3500. case e_log2 : return numeric::log2 (arg);
  3501. case e_log1p : return numeric::log1p(arg);
  3502. case e_neg : return numeric::neg (arg);
  3503. case e_pos : return numeric::pos (arg);
  3504. case e_round : return numeric::round(arg);
  3505. case e_sin : return numeric::sin (arg);
  3506. case e_sinc : return numeric::sinc (arg);
  3507. case e_sinh : return numeric::sinh (arg);
  3508. case e_sqrt : return numeric::sqrt (arg);
  3509. case e_tan : return numeric::tan (arg);
  3510. case e_tanh : return numeric::tanh (arg);
  3511. case e_cot : return numeric::cot (arg);
  3512. case e_sec : return numeric::sec (arg);
  3513. case e_csc : return numeric::csc (arg);
  3514. case e_r2d : return numeric::r2d (arg);
  3515. case e_d2r : return numeric::d2r (arg);
  3516. case e_d2g : return numeric::d2g (arg);
  3517. case e_g2d : return numeric::g2d (arg);
  3518. case e_notl : return numeric::notl (arg);
  3519. case e_sgn : return numeric::sgn (arg);
  3520. case e_erf : return numeric::erf (arg);
  3521. case e_erfc : return numeric::erfc (arg);
  3522. case e_ncdf : return numeric::ncdf (arg);
  3523. case e_frac : return numeric::frac (arg);
  3524. case e_trunc : return numeric::trunc(arg);
  3525. default : return std::numeric_limits<T>::quiet_NaN();
  3526. }
  3527. }
  3528. template <typename T>
  3529. inline T process_impl(const operator_type operation, const T arg0, const T arg1)
  3530. {
  3531. switch (operation)
  3532. {
  3533. case e_add : return (arg0 + arg1);
  3534. case e_sub : return (arg0 - arg1);
  3535. case e_mul : return (arg0 * arg1);
  3536. case e_div : return (arg0 / arg1);
  3537. case e_mod : return modulus<T>(arg0,arg1);
  3538. case e_pow : return pow<T>(arg0,arg1);
  3539. case e_atan2 : return atan2<T>(arg0,arg1);
  3540. case e_min : return std::min<T>(arg0,arg1);
  3541. case e_max : return std::max<T>(arg0,arg1);
  3542. case e_logn : return logn<T>(arg0,arg1);
  3543. case e_lt : return (arg0 < arg1) ? T(1) : T(0);
  3544. case e_lte : return (arg0 <= arg1) ? T(1) : T(0);
  3545. case e_eq : return std::equal_to<T>()(arg0,arg1) ? T(1) : T(0);
  3546. case e_ne : return std::not_equal_to<T>()(arg0,arg1) ? T(1) : T(0);
  3547. case e_gte : return (arg0 >= arg1) ? T(1) : T(0);
  3548. case e_gt : return (arg0 > arg1) ? T(1) : T(0);
  3549. case e_and : return and_opr<T> (arg0,arg1);
  3550. case e_nand : return nand_opr<T>(arg0,arg1);
  3551. case e_or : return or_opr<T> (arg0,arg1);
  3552. case e_nor : return nor_opr<T> (arg0,arg1);
  3553. case e_xor : return xor_opr<T> (arg0,arg1);
  3554. case e_xnor : return xnor_opr<T>(arg0,arg1);
  3555. case e_root : return root<T> (arg0,arg1);
  3556. case e_roundn : return roundn<T> (arg0,arg1);
  3557. case e_equal : return equal<T> (arg0,arg1);
  3558. case e_nequal : return nequal<T> (arg0,arg1);
  3559. case e_hypot : return hypot<T> (arg0,arg1);
  3560. case e_shr : return shr<T> (arg0,arg1);
  3561. case e_shl : return shl<T> (arg0,arg1);
  3562. default : return std::numeric_limits<T>::quiet_NaN();
  3563. }
  3564. }
  3565. template <typename T>
  3566. inline T process_impl(const operator_type operation, const T arg0, const T arg1, int_type_tag)
  3567. {
  3568. switch (operation)
  3569. {
  3570. case e_add : return (arg0 + arg1);
  3571. case e_sub : return (arg0 - arg1);
  3572. case e_mul : return (arg0 * arg1);
  3573. case e_div : return (arg0 / arg1);
  3574. case e_mod : return arg0 % arg1;
  3575. case e_pow : return pow<T>(arg0,arg1);
  3576. case e_min : return std::min<T>(arg0,arg1);
  3577. case e_max : return std::max<T>(arg0,arg1);
  3578. case e_logn : return logn<T>(arg0,arg1);
  3579. case e_lt : return (arg0 < arg1) ? T(1) : T(0);
  3580. case e_lte : return (arg0 <= arg1) ? T(1) : T(0);
  3581. case e_eq : return (arg0 == arg1) ? T(1) : T(0);
  3582. case e_ne : return (arg0 != arg1) ? T(1) : T(0);
  3583. case e_gte : return (arg0 >= arg1) ? T(1) : T(0);
  3584. case e_gt : return (arg0 > arg1) ? T(1) : T(0);
  3585. case e_and : return ((arg0 != T(0)) && (arg1 != T(0))) ? T(1) : T(0);
  3586. case e_nand : return ((arg0 != T(0)) && (arg1 != T(0))) ? T(0) : T(1);
  3587. case e_or : return ((arg0 != T(0)) || (arg1 != T(0))) ? T(1) : T(0);
  3588. case e_nor : return ((arg0 != T(0)) || (arg1 != T(0))) ? T(0) : T(1);
  3589. case e_xor : return arg0 ^ arg1;
  3590. case e_xnor : return !(arg0 ^ arg1);
  3591. case e_root : return root<T>(arg0,arg1);
  3592. case e_equal : return arg0 == arg1;
  3593. case e_nequal : return arg0 != arg1;
  3594. case e_hypot : return hypot<T>(arg0,arg1);
  3595. case e_shr : return arg0 >> arg1;
  3596. case e_shl : return arg0 << arg1;
  3597. default : return std::numeric_limits<T>::quiet_NaN();
  3598. }
  3599. }
  3600. }
  3601. template <typename T>
  3602. inline T process(const operator_type operation, const T arg)
  3603. {
  3604. return exprtk::details::numeric::details::process_impl(operation,arg);
  3605. }
  3606. template <typename T>
  3607. inline T process(const operator_type operation, const T arg0, const T arg1)
  3608. {
  3609. return exprtk::details::numeric::details::process_impl(operation,arg0,arg1);
  3610. }
  3611. }
  3612. template <typename T>
  3613. class expression_node
  3614. {
  3615. public:
  3616. enum node_type
  3617. {
  3618. e_none , e_null , e_constant , e_unary ,
  3619. e_binary , e_binary_ext , e_trinary , e_quaternary ,
  3620. e_vararg , e_conditional , e_while , e_repeat ,
  3621. e_for , e_switch , e_mswitch , e_return ,
  3622. e_retenv , e_variable , e_stringvar , e_stringconst ,
  3623. e_stringvarrng , e_cstringvarrng, e_strgenrange , e_strconcat ,
  3624. e_stringvarsize, e_strswap , e_stringsize , e_function ,
  3625. e_vafunction , e_genfunction , e_strfunction , e_strcondition ,
  3626. e_strccondition, e_add , e_sub , e_mul ,
  3627. e_div , e_mod , e_pow , e_lt ,
  3628. e_lte , e_gt , e_gte , e_eq ,
  3629. e_ne , e_and , e_nand , e_or ,
  3630. e_nor , e_xor , e_xnor , e_in ,
  3631. e_like , e_ilike , e_inranges , e_ipow ,
  3632. e_ipowinv , e_abs , e_acos , e_acosh ,
  3633. e_asin , e_asinh , e_atan , e_atanh ,
  3634. e_ceil , e_cos , e_cosh , e_exp ,
  3635. e_expm1 , e_floor , e_log , e_log10 ,
  3636. e_log2 , e_log1p , e_neg , e_pos ,
  3637. e_round , e_sin , e_sinc , e_sinh ,
  3638. e_sqrt , e_tan , e_tanh , e_cot ,
  3639. e_sec , e_csc , e_r2d , e_d2r ,
  3640. e_d2g , e_g2d , e_notl , e_sgn ,
  3641. e_erf , e_erfc , e_ncdf , e_frac ,
  3642. e_trunc , e_uvouv , e_vov , e_cov ,
  3643. e_voc , e_vob , e_bov , e_cob ,
  3644. e_boc , e_vovov , e_vovoc , e_vocov ,
  3645. e_covov , e_covoc , e_vovovov , e_vovovoc ,
  3646. e_vovocov , e_vocovov , e_covovov , e_covocov ,
  3647. e_vocovoc , e_covovoc , e_vococov , e_sf3ext ,
  3648. e_sf4ext , e_nulleq , e_strass , e_vector ,
  3649. e_vecelem , e_vecdefass , e_vecvalass , e_vecvecass ,
  3650. e_vecopvalass , e_vecopvecass , e_vecfunc , e_vecvecswap ,
  3651. e_vecvecineq , e_vecvalineq , e_valvecineq , e_vecvecarith ,
  3652. e_vecvalarith , e_valvecarith , e_vecunaryop , e_break ,
  3653. e_continue , e_swap
  3654. };
  3655. typedef T value_type;
  3656. typedef expression_node<T>* expression_ptr;
  3657. virtual ~expression_node()
  3658. {}
  3659. inline virtual T value() const
  3660. {
  3661. return std::numeric_limits<T>::quiet_NaN();
  3662. }
  3663. inline virtual expression_node<T>* branch(const std::size_t& index = 0) const
  3664. {
  3665. return reinterpret_cast<expression_ptr>(index * 0);
  3666. }
  3667. inline virtual node_type type() const
  3668. {
  3669. return e_none;
  3670. }
  3671. };
  3672. template <typename T>
  3673. inline bool is_generally_string_node(const expression_node<T>* node);
  3674. inline bool is_true(const double v)
  3675. {
  3676. return std::not_equal_to<double>()(0.0,v);
  3677. }
  3678. inline bool is_true(const long double v)
  3679. {
  3680. return std::not_equal_to<long double>()(0.0L,v);
  3681. }
  3682. inline bool is_true(const float v)
  3683. {
  3684. return std::not_equal_to<float>()(0.0f,v);
  3685. }
  3686. template <typename T>
  3687. inline bool is_true(const std::complex<T>& v)
  3688. {
  3689. return std::not_equal_to<std::complex<T> >()(std::complex<T>(0),v);
  3690. }
  3691. template <typename T>
  3692. inline bool is_true(const expression_node<T>* node)
  3693. {
  3694. return std::not_equal_to<T>()(T(0),node->value());
  3695. }
  3696. template <typename T>
  3697. inline bool is_false(const expression_node<T>* node)
  3698. {
  3699. return std::equal_to<T>()(T(0),node->value());
  3700. }
  3701. template <typename T>
  3702. inline bool is_unary_node(const expression_node<T>* node)
  3703. {
  3704. return node && (details::expression_node<T>::e_unary == node->type());
  3705. }
  3706. template <typename T>
  3707. inline bool is_neg_unary_node(const expression_node<T>* node)
  3708. {
  3709. return node && (details::expression_node<T>::e_neg == node->type());
  3710. }
  3711. template <typename T>
  3712. inline bool is_binary_node(const expression_node<T>* node)
  3713. {
  3714. return node && (details::expression_node<T>::e_binary == node->type());
  3715. }
  3716. template <typename T>
  3717. inline bool is_variable_node(const expression_node<T>* node)
  3718. {
  3719. return node && (details::expression_node<T>::e_variable == node->type());
  3720. }
  3721. template <typename T>
  3722. inline bool is_ivariable_node(const expression_node<T>* node)
  3723. {
  3724. return node &&
  3725. (
  3726. details::expression_node<T>::e_variable == node->type() ||
  3727. details::expression_node<T>::e_vecelem == node->type()
  3728. );
  3729. }
  3730. template <typename T>
  3731. inline bool is_vector_elem_node(const expression_node<T>* node)
  3732. {
  3733. return node && (details::expression_node<T>::e_vecelem == node->type());
  3734. }
  3735. template <typename T>
  3736. inline bool is_vector_node(const expression_node<T>* node)
  3737. {
  3738. return node && (details::expression_node<T>::e_vector == node->type());
  3739. }
  3740. template <typename T>
  3741. inline bool is_ivector_node(const expression_node<T>* node)
  3742. {
  3743. if (node)
  3744. {
  3745. switch (node->type())
  3746. {
  3747. case details::expression_node<T>::e_vector :
  3748. case details::expression_node<T>::e_vecvalass :
  3749. case details::expression_node<T>::e_vecvecass :
  3750. case details::expression_node<T>::e_vecopvalass :
  3751. case details::expression_node<T>::e_vecopvecass :
  3752. case details::expression_node<T>::e_vecvecswap :
  3753. case details::expression_node<T>::e_vecvecarith :
  3754. case details::expression_node<T>::e_vecvalarith :
  3755. case details::expression_node<T>::e_valvecarith :
  3756. case details::expression_node<T>::e_vecunaryop : return true;
  3757. default : return false;
  3758. }
  3759. }
  3760. else
  3761. return false;
  3762. }
  3763. template <typename T>
  3764. inline bool is_constant_node(const expression_node<T>* node)
  3765. {
  3766. return node && (details::expression_node<T>::e_constant == node->type());
  3767. }
  3768. template <typename T>
  3769. inline bool is_null_node(const expression_node<T>* node)
  3770. {
  3771. return node && (details::expression_node<T>::e_null == node->type());
  3772. }
  3773. template <typename T>
  3774. inline bool is_break_node(const expression_node<T>* node)
  3775. {
  3776. return node && (details::expression_node<T>::e_break == node->type());
  3777. }
  3778. template <typename T>
  3779. inline bool is_continue_node(const expression_node<T>* node)
  3780. {
  3781. return node && (details::expression_node<T>::e_continue == node->type());
  3782. }
  3783. template <typename T>
  3784. inline bool is_swap_node(const expression_node<T>* node)
  3785. {
  3786. return node && (details::expression_node<T>::e_swap == node->type());
  3787. }
  3788. template <typename T>
  3789. inline bool is_function(const expression_node<T>* node)
  3790. {
  3791. return node && (details::expression_node<T>::e_function == node->type());
  3792. }
  3793. template <typename T>
  3794. inline bool is_return_node(const expression_node<T>* node)
  3795. {
  3796. return node && (details::expression_node<T>::e_return == node->type());
  3797. }
  3798. template <typename T> class unary_node;
  3799. template <typename T>
  3800. inline bool is_negate_node(const expression_node<T>* node)
  3801. {
  3802. if (node && is_unary_node(node))
  3803. {
  3804. return (details::e_neg == static_cast<const unary_node<T>*>(node)->operation());
  3805. }
  3806. else
  3807. return false;
  3808. }
  3809. template <typename T>
  3810. inline bool branch_deletable(expression_node<T>* node)
  3811. {
  3812. return !is_variable_node(node) &&
  3813. !is_string_node (node) ;
  3814. }
  3815. template <std::size_t N, typename T>
  3816. inline bool all_nodes_valid(expression_node<T>* (&b)[N])
  3817. {
  3818. for (std::size_t i = 0; i < N; ++i)
  3819. {
  3820. if (0 == b[i]) return false;
  3821. }
  3822. return true;
  3823. }
  3824. template <typename T,
  3825. typename Allocator,
  3826. template <typename,typename> class Sequence>
  3827. inline bool all_nodes_valid(const Sequence<expression_node<T>*,Allocator>& b)
  3828. {
  3829. for (std::size_t i = 0; i < b.size(); ++i)
  3830. {
  3831. if (0 == b[i]) return false;
  3832. }
  3833. return true;
  3834. }
  3835. template <std::size_t N, typename T>
  3836. inline bool all_nodes_variables(expression_node<T>* (&b)[N])
  3837. {
  3838. for (std::size_t i = 0; i < N; ++i)
  3839. {
  3840. if (0 == b[i])
  3841. return false;
  3842. else if (!is_variable_node(b[i]))
  3843. return false;
  3844. }
  3845. return true;
  3846. }
  3847. template <typename T,
  3848. typename Allocator,
  3849. template <typename,typename> class Sequence>
  3850. inline bool all_nodes_variables(Sequence<expression_node<T>*,Allocator>& b)
  3851. {
  3852. for (std::size_t i = 0; i < b.size(); ++i)
  3853. {
  3854. if (0 == b[i])
  3855. return false;
  3856. else if (!is_variable_node(b[i]))
  3857. return false;
  3858. }
  3859. return true;
  3860. }
  3861. template <typename NodeAllocator, typename T, std::size_t N>
  3862. inline void free_all_nodes(NodeAllocator& node_allocator, expression_node<T>* (&b)[N])
  3863. {
  3864. for (std::size_t i = 0; i < N; ++i)
  3865. {
  3866. free_node(node_allocator,b[i]);
  3867. }
  3868. }
  3869. template <typename NodeAllocator,
  3870. typename T,
  3871. typename Allocator,
  3872. template <typename,typename> class Sequence>
  3873. inline void free_all_nodes(NodeAllocator& node_allocator, Sequence<expression_node<T>*,Allocator>& b)
  3874. {
  3875. for (std::size_t i = 0; i < b.size(); ++i)
  3876. {
  3877. free_node(node_allocator,b[i]);
  3878. }
  3879. b.clear();
  3880. }
  3881. template <typename NodeAllocator, typename T>
  3882. inline void free_node(NodeAllocator& node_allocator, expression_node<T>*& node, const bool force_delete = false)
  3883. {
  3884. if (0 != node)
  3885. {
  3886. if (
  3887. (is_variable_node(node) || is_string_node(node)) ||
  3888. force_delete
  3889. )
  3890. return;
  3891. node_allocator.free(node);
  3892. node = 0;
  3893. }
  3894. }
  3895. template <typename Type>
  3896. class vector_holder
  3897. {
  3898. private:
  3899. typedef Type value_type;
  3900. typedef value_type* value_ptr;
  3901. typedef const value_ptr const_value_ptr;
  3902. class vector_holder_base
  3903. {
  3904. public:
  3905. virtual ~vector_holder_base(){}
  3906. inline value_ptr operator[](const std::size_t& index) const
  3907. {
  3908. return value_at(index);
  3909. }
  3910. inline std::size_t size() const
  3911. {
  3912. return vector_size();
  3913. }
  3914. protected:
  3915. virtual value_ptr value_at(const std::size_t&) const = 0;
  3916. virtual std::size_t vector_size() const = 0;
  3917. };
  3918. class array_vector_impl : public vector_holder_base
  3919. {
  3920. public:
  3921. array_vector_impl(const Type* vec, const std::size_t& vec_size)
  3922. : vec_(vec),
  3923. size_(vec_size)
  3924. {}
  3925. protected:
  3926. value_ptr value_at(const std::size_t& index) const
  3927. {
  3928. if (index < size_)
  3929. return const_cast<const_value_ptr>(vec_ + index);
  3930. else
  3931. return const_value_ptr(0);
  3932. }
  3933. std::size_t vector_size() const
  3934. {
  3935. return size_;
  3936. }
  3937. private:
  3938. array_vector_impl operator=(const array_vector_impl&);
  3939. const Type* vec_;
  3940. const std::size_t size_;
  3941. };
  3942. template <typename Allocator,
  3943. template <typename,typename> class Sequence>
  3944. class sequence_vector_impl : public vector_holder_base
  3945. {
  3946. public:
  3947. typedef Sequence<Type,Allocator> sequence_t;
  3948. sequence_vector_impl(sequence_t& seq)
  3949. : sequence_(seq)
  3950. {}
  3951. protected:
  3952. value_ptr value_at(const std::size_t& index) const
  3953. {
  3954. return (index < sequence_.size()) ? (&sequence_[index]) : const_value_ptr(0);
  3955. }
  3956. std::size_t vector_size() const
  3957. {
  3958. return sequence_.size();
  3959. }
  3960. private:
  3961. sequence_vector_impl operator=(const sequence_vector_impl&);
  3962. sequence_t& sequence_;
  3963. };
  3964. public:
  3965. vector_holder(Type* vec, const std::size_t& vec_size)
  3966. : vector_holder_base_(new(buffer)array_vector_impl(vec,vec_size))
  3967. {}
  3968. template <typename Allocator>
  3969. vector_holder(std::vector<Type,Allocator>& vec)
  3970. : vector_holder_base_(new(buffer)sequence_vector_impl<Allocator,std::vector>(vec))
  3971. {}
  3972. template <typename Allocator>
  3973. vector_holder(std::deque<Type,Allocator>& deq)
  3974. : vector_holder_base_(new(buffer)sequence_vector_impl<Allocator,std::deque>(deq))
  3975. {}
  3976. inline value_ptr operator[](const std::size_t& index) const
  3977. {
  3978. return (*vector_holder_base_)[index];
  3979. }
  3980. inline std::size_t size() const
  3981. {
  3982. return vector_holder_base_->size();
  3983. }
  3984. private:
  3985. mutable vector_holder_base* vector_holder_base_;
  3986. unsigned char buffer[64];
  3987. };
  3988. template <typename T>
  3989. class null_node : public expression_node<T>
  3990. {
  3991. public:
  3992. inline T value() const
  3993. {
  3994. return std::numeric_limits<T>::quiet_NaN();
  3995. }
  3996. inline typename expression_node<T>::node_type type() const
  3997. {
  3998. return expression_node<T>::e_null;
  3999. }
  4000. };
  4001. template <typename T>
  4002. class null_eq_node : public expression_node<T>
  4003. {
  4004. public:
  4005. typedef expression_node<T>* expression_ptr;
  4006. null_eq_node(expression_ptr brnch, const bool equality = true)
  4007. : branch_(brnch),
  4008. branch_deletable_(branch_deletable(branch_)),
  4009. equality_(equality)
  4010. {}
  4011. ~null_eq_node()
  4012. {
  4013. if (branch_ && branch_deletable_)
  4014. {
  4015. delete branch_;
  4016. branch_ = 0;
  4017. }
  4018. }
  4019. inline T value() const
  4020. {
  4021. const T v = branch_->value();
  4022. const bool result = details::numeric::is_nan(v);
  4023. if (result)
  4024. return (equality_) ? T(1) : T(0);
  4025. else
  4026. return (equality_) ? T(0) : T(1);
  4027. }
  4028. inline typename expression_node<T>::node_type type() const
  4029. {
  4030. return expression_node<T>::e_nulleq;
  4031. }
  4032. inline operator_type operation() const
  4033. {
  4034. return details::e_eq;
  4035. }
  4036. inline expression_node<T>* branch(const std::size_t&) const
  4037. {
  4038. return branch_;
  4039. }
  4040. private:
  4041. expression_ptr branch_;
  4042. bool branch_deletable_;
  4043. bool equality_;
  4044. };
  4045. template <typename T>
  4046. class literal_node : public expression_node<T>
  4047. {
  4048. public:
  4049. explicit literal_node(const T& v)
  4050. : value_(v)
  4051. {}
  4052. inline T value() const
  4053. {
  4054. return value_;
  4055. }
  4056. inline typename expression_node<T>::node_type type() const
  4057. {
  4058. return expression_node<T>::e_constant;
  4059. }
  4060. inline expression_node<T>* branch(const std::size_t&) const
  4061. {
  4062. return reinterpret_cast<expression_node<T>*>(0);
  4063. }
  4064. private:
  4065. literal_node(literal_node<T>&) {}
  4066. literal_node<T>& operator=(literal_node<T>&) { return *this; }
  4067. const T value_;
  4068. };
  4069. template <typename T>
  4070. struct range_pack;
  4071. template <typename T>
  4072. struct range_data_type;
  4073. template <typename T>
  4074. class range_interface
  4075. {
  4076. public:
  4077. typedef range_pack<T> range_t;
  4078. virtual range_t& range_ref() = 0;
  4079. virtual const range_t& range_ref() const = 0;
  4080. };
  4081. template <typename T>
  4082. class string_base_node
  4083. {
  4084. public:
  4085. typedef range_data_type<T> range_data_type_t;
  4086. virtual std::string str () const = 0;
  4087. virtual const char* base() const = 0;
  4088. virtual std::size_t size() const = 0;
  4089. };
  4090. template <typename T>
  4091. class string_literal_node : public expression_node <T>,
  4092. public string_base_node<T>,
  4093. public range_interface <T>
  4094. {
  4095. public:
  4096. typedef range_pack<T> range_t;
  4097. explicit string_literal_node(const std::string& v)
  4098. : value_(v)
  4099. {
  4100. rp_.n0_c = std::make_pair<bool,std::size_t>(true,0);
  4101. rp_.n1_c = std::make_pair<bool,std::size_t>(true,v.size() - 1);
  4102. rp_.cache.first = rp_.n0_c.second;
  4103. rp_.cache.second = rp_.n1_c.second;
  4104. }
  4105. inline T value() const
  4106. {
  4107. return std::numeric_limits<T>::quiet_NaN();
  4108. }
  4109. inline typename expression_node<T>::node_type type() const
  4110. {
  4111. return expression_node<T>::e_stringconst;
  4112. }
  4113. inline expression_node<T>* branch(const std::size_t&) const
  4114. {
  4115. return reinterpret_cast<expression_node<T>*>(0);
  4116. }
  4117. std::string str() const
  4118. {
  4119. return value_;
  4120. }
  4121. const char* base() const
  4122. {
  4123. return value_.data();
  4124. }
  4125. std::size_t size() const
  4126. {
  4127. return value_.size();
  4128. }
  4129. range_t& range_ref()
  4130. {
  4131. return rp_;
  4132. }
  4133. const range_t& range_ref() const
  4134. {
  4135. return rp_;
  4136. }
  4137. private:
  4138. string_literal_node(const string_literal_node<T>&);
  4139. string_literal_node<T>& operator=(const string_literal_node<T>&);
  4140. const std::string value_;
  4141. range_t rp_;
  4142. };
  4143. template <typename T>
  4144. class unary_node : public expression_node<T>
  4145. {
  4146. public:
  4147. typedef expression_node<T>* expression_ptr;
  4148. unary_node(const operator_type& opr,
  4149. expression_ptr brnch)
  4150. : operation_(opr),
  4151. branch_(brnch),
  4152. branch_deletable_(branch_deletable(branch_))
  4153. {}
  4154. ~unary_node()
  4155. {
  4156. if (branch_ && branch_deletable_)
  4157. {
  4158. delete branch_;
  4159. branch_ = 0;
  4160. }
  4161. }
  4162. inline T value() const
  4163. {
  4164. const T arg = branch_->value();
  4165. return numeric::process<T>(operation_,arg);
  4166. }
  4167. inline typename expression_node<T>::node_type type() const
  4168. {
  4169. return expression_node<T>::e_unary;
  4170. }
  4171. inline operator_type operation() const
  4172. {
  4173. return operation_;
  4174. }
  4175. inline expression_node<T>* branch(const std::size_t&) const
  4176. {
  4177. return branch_;
  4178. }
  4179. inline void release()
  4180. {
  4181. branch_deletable_ = false;
  4182. }
  4183. protected:
  4184. operator_type operation_;
  4185. expression_ptr branch_;
  4186. bool branch_deletable_;
  4187. };
  4188. template <typename T, std::size_t D, bool B>
  4189. struct construct_branch_pair
  4190. {
  4191. template <std::size_t N>
  4192. static inline void process(std::pair<expression_node<T>*,bool> (&)[N], expression_node<T>*)
  4193. {}
  4194. };
  4195. template <typename T, std::size_t D>
  4196. struct construct_branch_pair<T,D,true>
  4197. {
  4198. template <std::size_t N>
  4199. static inline void process(std::pair<expression_node<T>*,bool> (&branch)[N], expression_node<T>* b)
  4200. {
  4201. if (b)
  4202. {
  4203. branch[D] = std::make_pair(b,branch_deletable(b));
  4204. }
  4205. }
  4206. };
  4207. template <std::size_t N, typename T>
  4208. inline void init_branches(std::pair<expression_node<T>*,bool> (&branch)[N],
  4209. expression_node<T>* b0,
  4210. expression_node<T>* b1 = reinterpret_cast<expression_node<T>*>(0),
  4211. expression_node<T>* b2 = reinterpret_cast<expression_node<T>*>(0),
  4212. expression_node<T>* b3 = reinterpret_cast<expression_node<T>*>(0),
  4213. expression_node<T>* b4 = reinterpret_cast<expression_node<T>*>(0),
  4214. expression_node<T>* b5 = reinterpret_cast<expression_node<T>*>(0),
  4215. expression_node<T>* b6 = reinterpret_cast<expression_node<T>*>(0),
  4216. expression_node<T>* b7 = reinterpret_cast<expression_node<T>*>(0),
  4217. expression_node<T>* b8 = reinterpret_cast<expression_node<T>*>(0),
  4218. expression_node<T>* b9 = reinterpret_cast<expression_node<T>*>(0))
  4219. {
  4220. construct_branch_pair<T,0,(N > 0)>::process(branch,b0);
  4221. construct_branch_pair<T,1,(N > 1)>::process(branch,b1);
  4222. construct_branch_pair<T,2,(N > 2)>::process(branch,b2);
  4223. construct_branch_pair<T,3,(N > 3)>::process(branch,b3);
  4224. construct_branch_pair<T,4,(N > 4)>::process(branch,b4);
  4225. construct_branch_pair<T,5,(N > 5)>::process(branch,b5);
  4226. construct_branch_pair<T,6,(N > 6)>::process(branch,b6);
  4227. construct_branch_pair<T,7,(N > 7)>::process(branch,b7);
  4228. construct_branch_pair<T,8,(N > 8)>::process(branch,b8);
  4229. construct_branch_pair<T,9,(N > 9)>::process(branch,b9);
  4230. }
  4231. struct cleanup_branches
  4232. {
  4233. template <typename T, std::size_t N>
  4234. static inline void execute(std::pair<expression_node<T>*,bool> (&branch)[N])
  4235. {
  4236. for (std::size_t i = 0; i < N; ++i)
  4237. {
  4238. if (branch[i].first && branch[i].second)
  4239. {
  4240. delete branch[i].first;
  4241. branch[i].first = 0;
  4242. }
  4243. }
  4244. }
  4245. template <typename T,
  4246. typename Allocator,
  4247. template <typename,typename> class Sequence>
  4248. static inline void execute(Sequence<std::pair<expression_node<T>*,bool>,Allocator>& branch)
  4249. {
  4250. for (std::size_t i = 0; i < branch.size(); ++i)
  4251. {
  4252. if (branch[i].first && branch[i].second)
  4253. {
  4254. delete branch[i].first;
  4255. branch[i].first = 0;
  4256. }
  4257. }
  4258. }
  4259. };
  4260. template <typename T>
  4261. class binary_node : public expression_node<T>
  4262. {
  4263. public:
  4264. typedef expression_node<T>* expression_ptr;
  4265. typedef std::pair<expression_ptr,bool> branch_t;
  4266. binary_node(const operator_type& opr,
  4267. expression_ptr branch0,
  4268. expression_ptr branch1)
  4269. : operation_(opr)
  4270. {
  4271. init_branches<2>(branch_,branch0,branch1);
  4272. }
  4273. ~binary_node()
  4274. {
  4275. cleanup_branches::execute<T,2>(branch_);
  4276. }
  4277. inline T value() const
  4278. {
  4279. const T arg0 = branch_[0].first->value();
  4280. const T arg1 = branch_[1].first->value();
  4281. return numeric::process<T>(operation_,arg0,arg1);
  4282. }
  4283. inline typename expression_node<T>::node_type type() const
  4284. {
  4285. return expression_node<T>::e_binary;
  4286. }
  4287. inline operator_type operation()
  4288. {
  4289. return operation_;
  4290. }
  4291. inline expression_node<T>* branch(const std::size_t& index = 0) const
  4292. {
  4293. if (0 == index)
  4294. return branch_[0].first;
  4295. else if (1 == index)
  4296. return branch_[1].first;
  4297. else
  4298. return reinterpret_cast<expression_ptr>(0);
  4299. }
  4300. protected:
  4301. operator_type operation_;
  4302. branch_t branch_[2];
  4303. };
  4304. template <typename T, typename Operation>
  4305. class binary_ext_node : public expression_node<T>
  4306. {
  4307. public:
  4308. typedef expression_node<T>* expression_ptr;
  4309. typedef std::pair<expression_ptr,bool> branch_t;
  4310. binary_ext_node(expression_ptr branch0, expression_ptr branch1)
  4311. {
  4312. init_branches<2>(branch_,branch0,branch1);
  4313. }
  4314. ~binary_ext_node()
  4315. {
  4316. cleanup_branches::execute<T,2>(branch_);
  4317. }
  4318. inline T value() const
  4319. {
  4320. const T arg0 = branch_[0].first->value();
  4321. const T arg1 = branch_[1].first->value();
  4322. return Operation::process(arg0,arg1);
  4323. }
  4324. inline typename expression_node<T>::node_type type() const
  4325. {
  4326. return expression_node<T>::e_binary_ext;
  4327. }
  4328. inline operator_type operation()
  4329. {
  4330. return Operation::operation();
  4331. }
  4332. inline expression_node<T>* branch(const std::size_t& index = 0) const
  4333. {
  4334. if (0 == index)
  4335. return branch_[0].first;
  4336. else if (1 == index)
  4337. return branch_[1].first;
  4338. else
  4339. return reinterpret_cast<expression_ptr>(0);
  4340. }
  4341. protected:
  4342. branch_t branch_[2];
  4343. };
  4344. template <typename T>
  4345. class trinary_node : public expression_node<T>
  4346. {
  4347. public:
  4348. typedef expression_node<T>* expression_ptr;
  4349. typedef std::pair<expression_ptr,bool> branch_t;
  4350. trinary_node(const operator_type& opr,
  4351. expression_ptr branch0,
  4352. expression_ptr branch1,
  4353. expression_ptr branch2)
  4354. : operation_(opr)
  4355. {
  4356. init_branches<3>(branch_,branch0,branch1,branch2);
  4357. }
  4358. ~trinary_node()
  4359. {
  4360. cleanup_branches::execute<T,3>(branch_);
  4361. }
  4362. inline T value() const
  4363. {
  4364. const T arg0 = branch_[0].first->value();
  4365. const T arg1 = branch_[1].first->value();
  4366. const T arg2 = branch_[2].first->value();
  4367. switch (operation_)
  4368. {
  4369. case e_inrange : return (arg1 < arg0) ? T(0) : ((arg1 > arg2) ? T(0) : T(1));
  4370. case e_clamp : return (arg1 < arg0) ? arg0 : (arg1 > arg2 ? arg2 : arg1);
  4371. case e_iclamp : if ((arg1 <= arg0) || (arg1 >= arg2))
  4372. return arg1;
  4373. else
  4374. return ((T(2) * arg1 <= (arg2 + arg0)) ? arg0 : arg2);
  4375. default : return std::numeric_limits<T>::quiet_NaN();
  4376. }
  4377. }
  4378. inline typename expression_node<T>::node_type type() const
  4379. {
  4380. return expression_node<T>::e_trinary;
  4381. }
  4382. protected:
  4383. operator_type operation_;
  4384. branch_t branch_[3];
  4385. };
  4386. template <typename T>
  4387. class quaternary_node : public expression_node<T>
  4388. {
  4389. public:
  4390. typedef expression_node<T>* expression_ptr;
  4391. typedef std::pair<expression_ptr,bool> branch_t;
  4392. quaternary_node(const operator_type& opr,
  4393. expression_ptr branch0,
  4394. expression_ptr branch1,
  4395. expression_ptr branch2,
  4396. expression_ptr branch3)
  4397. : operation_(opr)
  4398. {
  4399. init_branches<4>(branch_,branch0,branch1,branch2,branch3);
  4400. }
  4401. ~quaternary_node()
  4402. {
  4403. cleanup_branches::execute<T,4>(branch_);
  4404. }
  4405. inline T value() const
  4406. {
  4407. return std::numeric_limits<T>::quiet_NaN();
  4408. }
  4409. inline typename expression_node<T>::node_type type() const
  4410. {
  4411. return expression_node<T>::e_quaternary;
  4412. }
  4413. protected:
  4414. operator_type operation_;
  4415. branch_t branch_[4];
  4416. };
  4417. template <typename T>
  4418. class conditional_node : public expression_node<T>
  4419. {
  4420. public:
  4421. typedef expression_node<T>* expression_ptr;
  4422. conditional_node(expression_ptr test,
  4423. expression_ptr consequent,
  4424. expression_ptr alternative)
  4425. : test_(test),
  4426. consequent_(consequent),
  4427. alternative_(alternative),
  4428. test_deletable_(branch_deletable(test_)),
  4429. consequent_deletable_(branch_deletable(consequent_)),
  4430. alternative_deletable_(branch_deletable(alternative_))
  4431. {}
  4432. ~conditional_node()
  4433. {
  4434. if (test_ && test_deletable_ ) delete test_;
  4435. if (consequent_ && consequent_deletable_ ) delete consequent_;
  4436. if (alternative_ && alternative_deletable_) delete alternative_;
  4437. }
  4438. inline T value() const
  4439. {
  4440. if (is_true(test_))
  4441. return consequent_->value();
  4442. else
  4443. return alternative_->value();
  4444. }
  4445. inline typename expression_node<T>::node_type type() const
  4446. {
  4447. return expression_node<T>::e_conditional;
  4448. }
  4449. private:
  4450. expression_ptr test_;
  4451. expression_ptr consequent_;
  4452. expression_ptr alternative_;
  4453. bool test_deletable_;
  4454. bool consequent_deletable_;
  4455. bool alternative_deletable_;
  4456. };
  4457. template <typename T>
  4458. class cons_conditional_node : public expression_node<T>
  4459. {
  4460. public:
  4461. // Consequent only conditional statement node
  4462. typedef expression_node<T>* expression_ptr;
  4463. cons_conditional_node(expression_ptr test,
  4464. expression_ptr consequent)
  4465. : test_(test),
  4466. consequent_(consequent),
  4467. test_deletable_(branch_deletable(test_)),
  4468. consequent_deletable_(branch_deletable(consequent_))
  4469. {}
  4470. ~cons_conditional_node()
  4471. {
  4472. if (test_ && test_deletable_ ) delete test_;
  4473. if (consequent_ && consequent_deletable_) delete consequent_;
  4474. }
  4475. inline T value() const
  4476. {
  4477. if (is_true(test_))
  4478. return consequent_->value();
  4479. else
  4480. return std::numeric_limits<T>::quiet_NaN();
  4481. }
  4482. inline typename expression_node<T>::node_type type() const
  4483. {
  4484. return expression_node<T>::e_conditional;
  4485. }
  4486. private:
  4487. expression_ptr test_;
  4488. expression_ptr consequent_;
  4489. bool test_deletable_;
  4490. bool consequent_deletable_;
  4491. };
  4492. #ifndef exprtk_disable_break_continue
  4493. template <typename T>
  4494. class break_exception
  4495. {
  4496. public:
  4497. break_exception(const T& v)
  4498. : value(v)
  4499. {}
  4500. T value;
  4501. };
  4502. class continue_exception
  4503. {};
  4504. template <typename T>
  4505. class break_node : public expression_node<T>
  4506. {
  4507. public:
  4508. typedef expression_node<T>* expression_ptr;
  4509. break_node(expression_ptr ret = expression_ptr(0))
  4510. : return_(ret),
  4511. return_deletable_(branch_deletable(return_))
  4512. {}
  4513. ~break_node()
  4514. {
  4515. if (return_deletable_)
  4516. {
  4517. delete return_;
  4518. }
  4519. }
  4520. inline T value() const
  4521. {
  4522. throw break_exception<T>(return_ ? return_->value() : std::numeric_limits<T>::quiet_NaN());
  4523. #ifndef _MSC_VER
  4524. return std::numeric_limits<T>::quiet_NaN();
  4525. #endif
  4526. }
  4527. inline typename expression_node<T>::node_type type() const
  4528. {
  4529. return expression_node<T>::e_break;
  4530. }
  4531. private:
  4532. expression_ptr return_;
  4533. bool return_deletable_;
  4534. };
  4535. template <typename T>
  4536. class continue_node : public expression_node<T>
  4537. {
  4538. public:
  4539. inline T value() const
  4540. {
  4541. throw continue_exception();
  4542. #ifndef _MSC_VER
  4543. return std::numeric_limits<T>::quiet_NaN();
  4544. #endif
  4545. }
  4546. inline typename expression_node<T>::node_type type() const
  4547. {
  4548. return expression_node<T>::e_break;
  4549. }
  4550. };
  4551. #endif
  4552. template <typename T>
  4553. class while_loop_node : public expression_node<T>
  4554. {
  4555. public:
  4556. typedef expression_node<T>* expression_ptr;
  4557. while_loop_node(expression_ptr condition, expression_ptr loop_body)
  4558. : condition_(condition),
  4559. loop_body_(loop_body),
  4560. condition_deletable_(branch_deletable(condition_)),
  4561. loop_body_deletable_(branch_deletable(loop_body_))
  4562. {}
  4563. ~while_loop_node()
  4564. {
  4565. if (condition_ && condition_deletable_)
  4566. {
  4567. delete condition_;
  4568. }
  4569. if (loop_body_ && loop_body_deletable_)
  4570. {
  4571. delete loop_body_;
  4572. }
  4573. }
  4574. inline T value() const
  4575. {
  4576. T result = T(0);
  4577. while (is_true(condition_))
  4578. {
  4579. result = loop_body_->value();
  4580. }
  4581. return result;
  4582. }
  4583. inline typename expression_node<T>::node_type type() const
  4584. {
  4585. return expression_node<T>::e_while;
  4586. }
  4587. private:
  4588. expression_ptr condition_;
  4589. expression_ptr loop_body_;
  4590. bool condition_deletable_;
  4591. bool loop_body_deletable_;
  4592. };
  4593. template <typename T>
  4594. class repeat_until_loop_node : public expression_node<T>
  4595. {
  4596. public:
  4597. typedef expression_node<T>* expression_ptr;
  4598. repeat_until_loop_node(expression_ptr condition, expression_ptr loop_body)
  4599. : condition_(condition),
  4600. loop_body_(loop_body),
  4601. condition_deletable_(branch_deletable(condition_)),
  4602. loop_body_deletable_(branch_deletable(loop_body_))
  4603. {}
  4604. ~repeat_until_loop_node()
  4605. {
  4606. if (condition_ && condition_deletable_)
  4607. {
  4608. delete condition_;
  4609. }
  4610. if (loop_body_ && loop_body_deletable_)
  4611. {
  4612. delete loop_body_;
  4613. }
  4614. }
  4615. inline T value() const
  4616. {
  4617. T result = T(0);
  4618. do
  4619. {
  4620. result = loop_body_->value();
  4621. }
  4622. while (is_false(condition_));
  4623. return result;
  4624. }
  4625. inline typename expression_node<T>::node_type type() const
  4626. {
  4627. return expression_node<T>::e_repeat;
  4628. }
  4629. private:
  4630. expression_ptr condition_;
  4631. expression_ptr loop_body_;
  4632. bool condition_deletable_;
  4633. bool loop_body_deletable_;
  4634. };
  4635. template <typename T>
  4636. class for_loop_node : public expression_node<T>
  4637. {
  4638. public:
  4639. typedef expression_node<T>* expression_ptr;
  4640. for_loop_node(expression_ptr initialiser,
  4641. expression_ptr condition,
  4642. expression_ptr incrementor,
  4643. expression_ptr loop_body)
  4644. : initialiser_(initialiser),
  4645. condition_ (condition),
  4646. incrementor_(incrementor),
  4647. loop_body_ (loop_body),
  4648. initialiser_deletable_(branch_deletable(initialiser_)),
  4649. condition_deletable_ (branch_deletable(condition_ )),
  4650. incrementor_deletable_(branch_deletable(incrementor_)),
  4651. loop_body_deletable_ (branch_deletable(loop_body_ ))
  4652. {}
  4653. ~for_loop_node()
  4654. {
  4655. if (initialiser_ && initialiser_deletable_)
  4656. {
  4657. delete initialiser_;
  4658. }
  4659. if (condition_ && condition_deletable_)
  4660. {
  4661. delete condition_;
  4662. }
  4663. if (incrementor_ && incrementor_deletable_)
  4664. {
  4665. delete incrementor_;
  4666. }
  4667. if (loop_body_ && loop_body_deletable_)
  4668. {
  4669. delete loop_body_;
  4670. }
  4671. }
  4672. inline T value() const
  4673. {
  4674. T result = T(0);
  4675. if (initialiser_)
  4676. initialiser_->value();
  4677. if (incrementor_)
  4678. {
  4679. while (is_true(condition_))
  4680. {
  4681. result = loop_body_->value();
  4682. incrementor_->value();
  4683. }
  4684. }
  4685. else
  4686. {
  4687. while (is_true(condition_))
  4688. {
  4689. result = loop_body_->value();
  4690. }
  4691. }
  4692. return result;
  4693. }
  4694. inline typename expression_node<T>::node_type type() const
  4695. {
  4696. return expression_node<T>::e_for;
  4697. }
  4698. private:
  4699. expression_ptr initialiser_;
  4700. expression_ptr condition_ ;
  4701. expression_ptr incrementor_;
  4702. expression_ptr loop_body_ ;
  4703. bool initialiser_deletable_;
  4704. bool condition_deletable_ ;
  4705. bool incrementor_deletable_;
  4706. bool loop_body_deletable_ ;
  4707. };
  4708. #ifndef exprtk_disable_break_continue
  4709. template <typename T>
  4710. class while_loop_bc_node : public expression_node<T>
  4711. {
  4712. public:
  4713. typedef expression_node<T>* expression_ptr;
  4714. while_loop_bc_node(expression_ptr condition, expression_ptr loop_body)
  4715. : condition_(condition),
  4716. loop_body_(loop_body),
  4717. condition_deletable_(branch_deletable(condition_)),
  4718. loop_body_deletable_(branch_deletable(loop_body_))
  4719. {}
  4720. ~while_loop_bc_node()
  4721. {
  4722. if (condition_ && condition_deletable_)
  4723. {
  4724. delete condition_;
  4725. }
  4726. if (loop_body_ && loop_body_deletable_)
  4727. {
  4728. delete loop_body_;
  4729. }
  4730. }
  4731. inline T value() const
  4732. {
  4733. T result = T(0);
  4734. while (is_true(condition_))
  4735. {
  4736. try
  4737. {
  4738. result = loop_body_->value();
  4739. }
  4740. catch(const break_exception<T>& e)
  4741. {
  4742. return e.value;
  4743. }
  4744. catch(const continue_exception&)
  4745. {}
  4746. }
  4747. return result;
  4748. }
  4749. inline typename expression_node<T>::node_type type() const
  4750. {
  4751. return expression_node<T>::e_while;
  4752. }
  4753. private:
  4754. expression_ptr condition_;
  4755. expression_ptr loop_body_;
  4756. bool condition_deletable_;
  4757. bool loop_body_deletable_;
  4758. };
  4759. template <typename T>
  4760. class repeat_until_loop_bc_node : public expression_node<T>
  4761. {
  4762. public:
  4763. typedef expression_node<T>* expression_ptr;
  4764. repeat_until_loop_bc_node(expression_ptr condition, expression_ptr loop_body)
  4765. : condition_(condition),
  4766. loop_body_(loop_body),
  4767. condition_deletable_(branch_deletable(condition_)),
  4768. loop_body_deletable_(branch_deletable(loop_body_))
  4769. {}
  4770. ~repeat_until_loop_bc_node()
  4771. {
  4772. if (condition_ && condition_deletable_)
  4773. {
  4774. delete condition_;
  4775. }
  4776. if (loop_body_ && loop_body_deletable_)
  4777. {
  4778. delete loop_body_;
  4779. }
  4780. }
  4781. inline T value() const
  4782. {
  4783. T result = T(0);
  4784. do
  4785. {
  4786. try
  4787. {
  4788. result = loop_body_->value();
  4789. }
  4790. catch(const break_exception<T>& e)
  4791. {
  4792. return e.value;
  4793. }
  4794. catch(const continue_exception&)
  4795. {}
  4796. }
  4797. while (is_false(condition_));
  4798. return result;
  4799. }
  4800. inline typename expression_node<T>::node_type type() const
  4801. {
  4802. return expression_node<T>::e_repeat;
  4803. }
  4804. private:
  4805. expression_ptr condition_;
  4806. expression_ptr loop_body_;
  4807. bool condition_deletable_;
  4808. bool loop_body_deletable_;
  4809. };
  4810. template <typename T>
  4811. class for_loop_bc_node : public expression_node<T>
  4812. {
  4813. public:
  4814. typedef expression_node<T>* expression_ptr;
  4815. for_loop_bc_node(expression_ptr initialiser,
  4816. expression_ptr condition,
  4817. expression_ptr incrementor,
  4818. expression_ptr loop_body)
  4819. : initialiser_(initialiser),
  4820. condition_ (condition ),
  4821. incrementor_(incrementor),
  4822. loop_body_ (loop_body ),
  4823. initialiser_deletable_(branch_deletable(initialiser_)),
  4824. condition_deletable_ (branch_deletable(condition_ )),
  4825. incrementor_deletable_(branch_deletable(incrementor_)),
  4826. loop_body_deletable_ (branch_deletable(loop_body_ ))
  4827. {}
  4828. ~for_loop_bc_node()
  4829. {
  4830. if (initialiser_ && initialiser_deletable_)
  4831. {
  4832. delete initialiser_;
  4833. }
  4834. if (condition_ && condition_deletable_)
  4835. {
  4836. delete condition_;
  4837. }
  4838. if (incrementor_ && incrementor_deletable_)
  4839. {
  4840. delete incrementor_;
  4841. }
  4842. if (loop_body_ && loop_body_deletable_)
  4843. {
  4844. delete loop_body_;
  4845. }
  4846. }
  4847. inline T value() const
  4848. {
  4849. T result = T(0);
  4850. if (initialiser_)
  4851. initialiser_->value();
  4852. if (incrementor_)
  4853. {
  4854. while (is_true(condition_))
  4855. {
  4856. try
  4857. {
  4858. result = loop_body_->value();
  4859. }
  4860. catch(const break_exception<T>& e)
  4861. {
  4862. return e.value;
  4863. }
  4864. catch(const continue_exception&)
  4865. {}
  4866. incrementor_->value();
  4867. }
  4868. }
  4869. else
  4870. {
  4871. while (is_true(condition_))
  4872. {
  4873. try
  4874. {
  4875. result = loop_body_->value();
  4876. }
  4877. catch(const break_exception<T>& e)
  4878. {
  4879. return e.value;
  4880. }
  4881. catch(const continue_exception&)
  4882. {}
  4883. }
  4884. }
  4885. return result;
  4886. }
  4887. inline typename expression_node<T>::node_type type() const
  4888. {
  4889. return expression_node<T>::e_for;
  4890. }
  4891. private:
  4892. expression_ptr initialiser_;
  4893. expression_ptr condition_ ;
  4894. expression_ptr incrementor_;
  4895. expression_ptr loop_body_ ;
  4896. bool initialiser_deletable_;
  4897. bool condition_deletable_ ;
  4898. bool incrementor_deletable_;
  4899. bool loop_body_deletable_ ;
  4900. };
  4901. #endif
  4902. template <typename T>
  4903. class switch_node : public expression_node<T>
  4904. {
  4905. public:
  4906. typedef expression_node<T>* expression_ptr;
  4907. template <typename Allocator,
  4908. template <typename,typename> class Sequence>
  4909. switch_node(const Sequence<expression_ptr,Allocator>& arg_list)
  4910. {
  4911. if (1 != (arg_list.size() & 1))
  4912. return;
  4913. arg_list_.resize(arg_list.size());
  4914. delete_branch_.resize(arg_list.size());
  4915. for (std::size_t i = 0; i < arg_list.size(); ++i)
  4916. {
  4917. if (arg_list[i])
  4918. {
  4919. arg_list_[i] = arg_list[i];
  4920. delete_branch_[i] = static_cast<unsigned char>(branch_deletable(arg_list_[i]) ? 1 : 0);
  4921. }
  4922. else
  4923. {
  4924. arg_list_.clear();
  4925. delete_branch_.clear();
  4926. return;
  4927. }
  4928. }
  4929. }
  4930. ~switch_node()
  4931. {
  4932. for (std::size_t i = 0; i < arg_list_.size(); ++i)
  4933. {
  4934. if (arg_list_[i] && delete_branch_[i])
  4935. {
  4936. delete arg_list_[i];
  4937. arg_list_[i] = 0;
  4938. }
  4939. }
  4940. }
  4941. inline T value() const
  4942. {
  4943. if (!arg_list_.empty())
  4944. {
  4945. const std::size_t upper_bound = (arg_list_.size() - 1);
  4946. for (std::size_t i = 0; i < upper_bound; i += 2)
  4947. {
  4948. expression_ptr condition = arg_list_[i ];
  4949. expression_ptr consequent = arg_list_[i + 1];
  4950. if (is_true(condition))
  4951. {
  4952. return consequent->value();
  4953. }
  4954. }
  4955. return arg_list_[upper_bound]->value();
  4956. }
  4957. else
  4958. return std::numeric_limits<T>::quiet_NaN();
  4959. }
  4960. inline typename expression_node<T>::node_type type() const
  4961. {
  4962. return expression_node<T>::e_switch;
  4963. }
  4964. protected:
  4965. std::vector<expression_ptr> arg_list_;
  4966. std::vector<unsigned char> delete_branch_;
  4967. };
  4968. template <typename T, typename Switch_N>
  4969. class switch_n_node : public switch_node<T>
  4970. {
  4971. public:
  4972. typedef expression_node<T>* expression_ptr;
  4973. template <typename Allocator,
  4974. template <typename,typename> class Sequence>
  4975. switch_n_node(const Sequence<expression_ptr,Allocator>& arg_list)
  4976. : switch_node<T>(arg_list)
  4977. {}
  4978. inline T value() const
  4979. {
  4980. return Switch_N::process(switch_node<T>::arg_list_);
  4981. }
  4982. };
  4983. template <typename T>
  4984. class multi_switch_node : public expression_node<T>
  4985. {
  4986. public:
  4987. typedef expression_node<T>* expression_ptr;
  4988. template <typename Allocator,
  4989. template <typename,typename> class Sequence>
  4990. multi_switch_node(const Sequence<expression_ptr,Allocator>& arg_list)
  4991. {
  4992. if (0 != (arg_list.size() & 1))
  4993. return;
  4994. arg_list_.resize(arg_list.size());
  4995. delete_branch_.resize(arg_list.size());
  4996. for (std::size_t i = 0; i < arg_list.size(); ++i)
  4997. {
  4998. if (arg_list[i])
  4999. {
  5000. arg_list_[i] = arg_list[i];
  5001. delete_branch_[i] = static_cast<unsigned char>(branch_deletable(arg_list_[i]) ? 1 : 0);
  5002. }
  5003. else
  5004. {
  5005. arg_list_.clear();
  5006. delete_branch_.clear();
  5007. return;
  5008. }
  5009. }
  5010. }
  5011. ~multi_switch_node()
  5012. {
  5013. for (std::size_t i = 0; i < arg_list_.size(); ++i)
  5014. {
  5015. if (arg_list_[i] && delete_branch_[i])
  5016. {
  5017. delete arg_list_[i];
  5018. arg_list_[i] = 0;
  5019. }
  5020. }
  5021. }
  5022. inline T value() const
  5023. {
  5024. T result = T(0);
  5025. if (arg_list_.empty())
  5026. {
  5027. return std::numeric_limits<T>::quiet_NaN();
  5028. }
  5029. const std::size_t upper_bound = (arg_list_.size() - 1);
  5030. for (std::size_t i = 0; i < upper_bound; i += 2)
  5031. {
  5032. expression_ptr condition = arg_list_[i ];
  5033. expression_ptr consequent = arg_list_[i + 1];
  5034. if (is_true(condition))
  5035. {
  5036. result = consequent->value();
  5037. }
  5038. }
  5039. return result;
  5040. }
  5041. inline typename expression_node<T>::node_type type() const
  5042. {
  5043. return expression_node<T>::e_mswitch;
  5044. }
  5045. private:
  5046. std::vector<expression_ptr> arg_list_;
  5047. std::vector<unsigned char> delete_branch_;
  5048. };
  5049. template <typename T>
  5050. class ivariable
  5051. {
  5052. public:
  5053. virtual T& ref() = 0;
  5054. virtual const T& ref() const = 0;
  5055. };
  5056. template <typename T>
  5057. class variable_node : public expression_node<T>,
  5058. public ivariable <T>
  5059. {
  5060. public:
  5061. static T null_value;
  5062. explicit variable_node()
  5063. : value_(&null_value),
  5064. delete_value_(false)
  5065. {}
  5066. variable_node(T& v)
  5067. : value_(&v),
  5068. delete_value_(false)
  5069. {}
  5070. ~variable_node()
  5071. {
  5072. if (delete_value_)
  5073. {
  5074. delete value_;
  5075. }
  5076. }
  5077. inline bool operator <(const variable_node<T>& v) const
  5078. {
  5079. return this < (&v);
  5080. }
  5081. inline T value() const
  5082. {
  5083. return (*value_);
  5084. }
  5085. inline T& ref()
  5086. {
  5087. return (*value_);
  5088. }
  5089. inline const T& ref() const
  5090. {
  5091. return (*value_);
  5092. }
  5093. inline typename expression_node<T>::node_type type() const
  5094. {
  5095. return expression_node<T>::e_variable;
  5096. }
  5097. inline bool& delete_value()
  5098. {
  5099. return delete_value_;
  5100. }
  5101. private:
  5102. T* value_;
  5103. bool delete_value_;
  5104. };
  5105. template <typename T>
  5106. T variable_node<T>::null_value = T(std::numeric_limits<T>::quiet_NaN());
  5107. template <typename T>
  5108. struct range_pack
  5109. {
  5110. typedef expression_node<T>* expression_node_ptr;
  5111. typedef std::pair<std::size_t,std::size_t> cached_range_t;
  5112. range_pack()
  5113. : n0_e (std::make_pair(false,expression_node_ptr(0))),
  5114. n1_e (std::make_pair(false,expression_node_ptr(0))),
  5115. n0_c (std::make_pair(false,0)),
  5116. n1_c (std::make_pair(false,0)),
  5117. cache(std::make_pair(0,0))
  5118. {}
  5119. void clear()
  5120. {
  5121. n0_e = std::make_pair(false,expression_node_ptr(0));
  5122. n1_e = std::make_pair(false,expression_node_ptr(0));
  5123. n0_c = std::make_pair(false,0);
  5124. n1_c = std::make_pair(false,0);
  5125. cache = std::make_pair(0,0);
  5126. }
  5127. void free()
  5128. {
  5129. if (n0_e.first && n0_e.second)
  5130. {
  5131. n0_e.first = false;
  5132. if (
  5133. !is_variable_node(n0_e.second) &&
  5134. !is_string_node (n0_e.second)
  5135. )
  5136. {
  5137. delete n0_e.second;
  5138. n0_e.second = expression_node_ptr(0);
  5139. }
  5140. }
  5141. if (n1_e.first && n1_e.second)
  5142. {
  5143. n1_e.first = false;
  5144. if (
  5145. !is_variable_node(n1_e.second) &&
  5146. !is_string_node (n1_e.second)
  5147. )
  5148. {
  5149. delete n1_e.second;
  5150. n1_e.second = expression_node_ptr(0);
  5151. }
  5152. }
  5153. }
  5154. bool const_range()
  5155. {
  5156. return ( n0_c.first && n1_c.first) &&
  5157. (!n0_e.first && !n1_e.first);
  5158. }
  5159. bool var_range()
  5160. {
  5161. return ( n0_e.first && n1_e.first) &&
  5162. (!n0_c.first && !n1_c.first);
  5163. }
  5164. bool operator()(std::size_t& r0, std::size_t& r1, const std::size_t& size = std::numeric_limits<std::size_t>::max()) const
  5165. {
  5166. if (n0_c.first)
  5167. r0 = n0_c.second;
  5168. else if (n0_e.first)
  5169. {
  5170. T r0_value = n0_e.second->value();
  5171. if (r0_value < 0)
  5172. return false;
  5173. else
  5174. r0 = static_cast<std::size_t>(details::numeric::to_int64(r0_value));
  5175. }
  5176. else
  5177. return false;
  5178. if (n1_c.first)
  5179. r1 = n1_c.second;
  5180. else if (n1_e.first)
  5181. {
  5182. T r1_value = n1_e.second->value();
  5183. if (r1_value < 0)
  5184. return false;
  5185. else
  5186. r1 = static_cast<std::size_t>(details::numeric::to_int64(r1_value));
  5187. }
  5188. else
  5189. return false;
  5190. if (
  5191. (std::numeric_limits<std::size_t>::max() != size) &&
  5192. (std::numeric_limits<std::size_t>::max() == r1 )
  5193. )
  5194. {
  5195. r1 = size - 1;
  5196. }
  5197. cache.first = r0;
  5198. cache.second = r1;
  5199. return (r0 <= r1);
  5200. }
  5201. inline std::size_t const_size() const
  5202. {
  5203. return (n1_c.second - n0_c.second + 1);
  5204. }
  5205. inline std::size_t cache_size() const
  5206. {
  5207. return (cache.second - cache.first + 1);
  5208. }
  5209. std::pair<bool,expression_node_ptr> n0_e;
  5210. std::pair<bool,expression_node_ptr> n1_e;
  5211. std::pair<bool,std::size_t > n0_c;
  5212. std::pair<bool,std::size_t > n1_c;
  5213. mutable cached_range_t cache;
  5214. };
  5215. template <typename T>
  5216. class string_base_node;
  5217. template <typename T>
  5218. struct range_data_type
  5219. {
  5220. typedef range_pack<T> range_t;
  5221. typedef string_base_node<T>* strbase_ptr_t;
  5222. range_data_type()
  5223. : range(0),
  5224. data (0),
  5225. size (0),
  5226. type_size(0),
  5227. str_node (0)
  5228. {}
  5229. range_t* range;
  5230. void* data;
  5231. std::size_t size;
  5232. std::size_t type_size;
  5233. strbase_ptr_t str_node;
  5234. };
  5235. template <typename T> class vector_node;
  5236. template <typename T>
  5237. class vector_interface
  5238. {
  5239. public:
  5240. typedef vector_node<T>* vector_node_ptr;
  5241. virtual ~vector_interface()
  5242. {}
  5243. virtual vector_node_ptr vec() const = 0;
  5244. virtual vector_node_ptr vec() = 0;
  5245. virtual std::size_t size() const = 0;
  5246. };
  5247. template <typename T>
  5248. class vector_node : public expression_node <T>,
  5249. public vector_interface<T>
  5250. {
  5251. public:
  5252. typedef expression_node<T>* expression_ptr;
  5253. typedef vector_holder<T> vector_holder_t;
  5254. typedef vector_node<T>* vector_node_ptr;
  5255. vector_node(vector_holder_t* vh)
  5256. : vector_holder_(vh)
  5257. {}
  5258. inline T value() const
  5259. {
  5260. return *(ref()[0]);
  5261. }
  5262. inline const vector_holder_t& ref() const
  5263. {
  5264. return (*vector_holder_);
  5265. }
  5266. inline vector_holder_t& ref()
  5267. {
  5268. return (*vector_holder_);
  5269. }
  5270. vector_node_ptr vec() const
  5271. {
  5272. return const_cast<vector_node_ptr>(this);
  5273. }
  5274. vector_node_ptr vec()
  5275. {
  5276. return this;
  5277. }
  5278. inline typename expression_node<T>::node_type type() const
  5279. {
  5280. return expression_node<T>::e_vector;
  5281. }
  5282. std::size_t size() const
  5283. {
  5284. return ref().size();
  5285. }
  5286. private:
  5287. vector_holder_t* vector_holder_;
  5288. };
  5289. template <typename T>
  5290. class vector_elem_node : public expression_node<T>,
  5291. public ivariable <T>
  5292. {
  5293. public:
  5294. typedef expression_node<T>* expression_ptr;
  5295. vector_elem_node(expression_ptr index, T* vector_base)
  5296. : index_(index),
  5297. vector_base_(vector_base),
  5298. index_deletable_(branch_deletable(index_))
  5299. {}
  5300. ~vector_elem_node()
  5301. {
  5302. if (index_ && index_deletable_)
  5303. {
  5304. delete index_;
  5305. }
  5306. }
  5307. inline T value() const
  5308. {
  5309. return *(vector_base_ + static_cast<std::size_t>(details::numeric::to_int64(index_->value())));
  5310. }
  5311. inline T& ref()
  5312. {
  5313. return *(vector_base_ + static_cast<std::size_t>(details::numeric::to_int64(index_->value())));
  5314. }
  5315. inline const T& ref() const
  5316. {
  5317. return *(vector_base_ + static_cast<std::size_t>(details::numeric::to_int64(index_->value())));
  5318. }
  5319. inline typename expression_node<T>::node_type type() const
  5320. {
  5321. return expression_node<T>::e_vecelem;
  5322. }
  5323. private:
  5324. expression_ptr index_;
  5325. T* vector_base_;
  5326. bool index_deletable_;
  5327. };
  5328. template <typename T>
  5329. class vector_assignment_node : public expression_node<T>
  5330. {
  5331. public:
  5332. typedef expression_node<T>* expression_ptr;
  5333. vector_assignment_node(T* vector_base,
  5334. const std::size_t& size,
  5335. const std::vector<expression_ptr>& initialiser_list,
  5336. const bool single_value_initialse)
  5337. : vector_base_(vector_base),
  5338. initialiser_list_(initialiser_list),
  5339. size_(size),
  5340. single_value_initialse_(single_value_initialse)
  5341. {}
  5342. ~vector_assignment_node()
  5343. {
  5344. for (std::size_t i = 0; i < initialiser_list_.size(); ++i)
  5345. {
  5346. if (branch_deletable(initialiser_list_[i]))
  5347. {
  5348. delete initialiser_list_[i];
  5349. }
  5350. }
  5351. }
  5352. inline T value() const
  5353. {
  5354. if (single_value_initialse_)
  5355. {
  5356. for (std::size_t i = 0; i < size_; ++i)
  5357. {
  5358. *(vector_base_ + i) = initialiser_list_[0]->value();
  5359. }
  5360. }
  5361. else
  5362. {
  5363. std::size_t il_size = initialiser_list_.size();
  5364. for (std::size_t i = 0; i < il_size; ++i)
  5365. {
  5366. *(vector_base_ + i) = initialiser_list_[i]->value();
  5367. }
  5368. if (il_size < size_)
  5369. {
  5370. for (std::size_t i = il_size; i < size_; ++i)
  5371. {
  5372. *(vector_base_ + i) = T(0);
  5373. }
  5374. }
  5375. }
  5376. return *(vector_base_);
  5377. }
  5378. inline typename expression_node<T>::node_type type() const
  5379. {
  5380. return expression_node<T>::e_vecdefass;
  5381. }
  5382. private:
  5383. vector_assignment_node<T>& operator=(const vector_assignment_node<T>&);
  5384. mutable T* vector_base_;
  5385. std::vector<expression_ptr> initialiser_list_;
  5386. const std::size_t size_;
  5387. const bool single_value_initialse_;
  5388. };
  5389. template <typename T>
  5390. class swap_node : public expression_node<T>
  5391. {
  5392. public:
  5393. typedef expression_node<T>* expression_ptr;
  5394. typedef variable_node<T>* variable_node_ptr;
  5395. swap_node(variable_node_ptr var0, variable_node_ptr var1)
  5396. : var0_(var0),
  5397. var1_(var1)
  5398. {}
  5399. inline T value() const
  5400. {
  5401. std::swap(var0_->ref(),var1_->ref());
  5402. return var1_->ref();
  5403. }
  5404. inline typename expression_node<T>::node_type type() const
  5405. {
  5406. return expression_node<T>::e_swap;
  5407. }
  5408. private:
  5409. variable_node_ptr var0_;
  5410. variable_node_ptr var1_;
  5411. };
  5412. template <typename T>
  5413. class swap_generic_node : public binary_node<T>
  5414. {
  5415. public:
  5416. typedef expression_node<T>* expression_ptr;
  5417. typedef ivariable<T>* ivariable_ptr;
  5418. swap_generic_node(expression_ptr var0, expression_ptr var1)
  5419. : binary_node<T>(details::e_swap,var0,var1),
  5420. var0_(dynamic_cast<ivariable_ptr>(var0)),
  5421. var1_(dynamic_cast<ivariable_ptr>(var1))
  5422. {}
  5423. inline T value() const
  5424. {
  5425. std::swap(var0_->ref(),var1_->ref());
  5426. return var1_->ref();
  5427. }
  5428. inline typename expression_node<T>::node_type type() const
  5429. {
  5430. return expression_node<T>::e_swap;
  5431. }
  5432. private:
  5433. ivariable_ptr var0_;
  5434. ivariable_ptr var1_;
  5435. };
  5436. template <typename T>
  5437. class swap_vecvec_node : public binary_node <T>,
  5438. public vector_interface<T>
  5439. {
  5440. public:
  5441. typedef expression_node<T>* expression_ptr;
  5442. typedef vector_node<T>* vector_node_ptr;
  5443. swap_vecvec_node(expression_ptr branch0,
  5444. expression_ptr branch1)
  5445. : binary_node<T>(details::e_swap,branch0,branch1),
  5446. vec0_node_ptr_(0),
  5447. vec1_node_ptr_(0),
  5448. vec_size_ (0),
  5449. initialised_(false)
  5450. {
  5451. if (is_ivector_node(binary_node<T>::branch_[0].first))
  5452. {
  5453. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  5454. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[0].first)))
  5455. {
  5456. vec0_node_ptr_ = vi->vec();
  5457. }
  5458. }
  5459. if (is_ivector_node(binary_node<T>::branch_[1].first))
  5460. {
  5461. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  5462. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[1].first)))
  5463. {
  5464. vec1_node_ptr_ = vi->vec();
  5465. }
  5466. }
  5467. if (vec0_node_ptr_ && vec1_node_ptr_)
  5468. {
  5469. vec_size_ = std::min(vec0_node_ptr_->ref().size(),
  5470. vec1_node_ptr_->ref().size());
  5471. initialised_ = true;
  5472. }
  5473. }
  5474. inline T value() const
  5475. {
  5476. if (initialised_)
  5477. {
  5478. binary_node<T>::branch_[0].first->value();
  5479. binary_node<T>::branch_[1].first->value();
  5480. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  5481. vector_holder<T>& vec1 = vec1_node_ptr_->ref();
  5482. for (std::size_t i = 0; i < vec_size_; ++i)
  5483. {
  5484. std::swap((*vec0[i]),(*vec1[i]));
  5485. }
  5486. return vec1_node_ptr_->value();
  5487. }
  5488. else
  5489. return std::numeric_limits<T>::quiet_NaN();
  5490. }
  5491. vector_node_ptr vec() const
  5492. {
  5493. return vec0_node_ptr_;
  5494. }
  5495. vector_node_ptr vec()
  5496. {
  5497. return vec0_node_ptr_;
  5498. }
  5499. inline typename expression_node<T>::node_type type() const
  5500. {
  5501. return expression_node<T>::e_vecvecswap;
  5502. }
  5503. std::size_t size() const
  5504. {
  5505. return vec_size_;
  5506. }
  5507. private:
  5508. vector_node<T>* vec0_node_ptr_;
  5509. vector_node<T>* vec1_node_ptr_;
  5510. std::size_t vec_size_;
  5511. bool initialised_;
  5512. };
  5513. #ifndef exprtk_disable_string_capabilities
  5514. template <typename T>
  5515. class stringvar_node : public expression_node <T>,
  5516. public string_base_node<T>,
  5517. public range_interface <T>
  5518. {
  5519. public:
  5520. typedef range_pack<T> range_t;
  5521. static std::string null_value;
  5522. explicit stringvar_node()
  5523. : value_(&null_value)
  5524. {}
  5525. explicit stringvar_node(std::string& v)
  5526. : value_(&v)
  5527. {
  5528. rp_.n0_c = std::make_pair<bool,std::size_t>(true,0);
  5529. rp_.n1_c = std::make_pair<bool,std::size_t>(true,v.size() - 1);
  5530. rp_.cache.first = rp_.n0_c.second;
  5531. rp_.cache.second = rp_.n1_c.second;
  5532. }
  5533. inline bool operator <(const stringvar_node<T>& v) const
  5534. {
  5535. return this < (&v);
  5536. }
  5537. inline T value() const
  5538. {
  5539. rp_.n1_c.second = (*value_).size() - 1;
  5540. rp_.cache.second = rp_.n1_c.second;
  5541. return std::numeric_limits<T>::quiet_NaN();
  5542. }
  5543. std::string str() const
  5544. {
  5545. return ref();
  5546. }
  5547. const char* base() const
  5548. {
  5549. return (*value_).data();
  5550. }
  5551. std::size_t size() const
  5552. {
  5553. return ref().size();
  5554. }
  5555. std::string& ref()
  5556. {
  5557. return (*value_);
  5558. }
  5559. const std::string& ref() const
  5560. {
  5561. return (*value_);
  5562. }
  5563. range_t& range_ref()
  5564. {
  5565. return rp_;
  5566. }
  5567. const range_t& range_ref() const
  5568. {
  5569. return rp_;
  5570. }
  5571. inline typename expression_node<T>::node_type type() const
  5572. {
  5573. return expression_node<T>::e_stringvar;
  5574. }
  5575. private:
  5576. std::string* value_;
  5577. mutable range_t rp_;
  5578. };
  5579. template <typename T>
  5580. std::string stringvar_node<T>::null_value = std::string("");
  5581. template <typename T>
  5582. class string_range_node : public expression_node <T>,
  5583. public string_base_node<T>,
  5584. public range_interface <T>
  5585. {
  5586. public:
  5587. typedef range_pack<T> range_t;
  5588. static std::string null_value;
  5589. explicit string_range_node(std::string& v, range_t rp)
  5590. : value_(&v),
  5591. rp_(rp)
  5592. {}
  5593. ~string_range_node()
  5594. {
  5595. rp_.free();
  5596. }
  5597. inline bool operator <(const string_range_node<T>& v) const
  5598. {
  5599. return this < (&v);
  5600. }
  5601. inline T value() const
  5602. {
  5603. return std::numeric_limits<T>::quiet_NaN();
  5604. }
  5605. inline std::string str() const
  5606. {
  5607. return (*value_);
  5608. }
  5609. const char* base() const
  5610. {
  5611. return (*value_).data();
  5612. }
  5613. std::size_t size() const
  5614. {
  5615. return ref().size();
  5616. }
  5617. inline range_t range() const
  5618. {
  5619. return rp_;
  5620. }
  5621. inline virtual std::string& ref()
  5622. {
  5623. return (*value_);
  5624. }
  5625. inline virtual const std::string& ref() const
  5626. {
  5627. return (*value_);
  5628. }
  5629. inline range_t& range_ref()
  5630. {
  5631. return rp_;
  5632. }
  5633. inline const range_t& range_ref() const
  5634. {
  5635. return rp_;
  5636. }
  5637. inline typename expression_node<T>::node_type type() const
  5638. {
  5639. return expression_node<T>::e_stringvarrng;
  5640. }
  5641. private:
  5642. std::string* value_;
  5643. range_t rp_;
  5644. };
  5645. template <typename T>
  5646. std::string string_range_node<T>::null_value = std::string("");
  5647. template <typename T>
  5648. class const_string_range_node : public expression_node <T>,
  5649. public string_base_node<T>,
  5650. public range_interface <T>
  5651. {
  5652. public:
  5653. typedef range_pack<T> range_t;
  5654. explicit const_string_range_node(const std::string& v, range_t rp)
  5655. : value_(v),
  5656. rp_(rp)
  5657. {}
  5658. ~const_string_range_node()
  5659. {
  5660. rp_.free();
  5661. }
  5662. inline T value() const
  5663. {
  5664. return std::numeric_limits<T>::quiet_NaN();
  5665. }
  5666. std::string str() const
  5667. {
  5668. return value_;
  5669. }
  5670. const char* base() const
  5671. {
  5672. return value_.data();
  5673. }
  5674. std::size_t size() const
  5675. {
  5676. return value_.size();
  5677. }
  5678. range_t range() const
  5679. {
  5680. return rp_;
  5681. }
  5682. range_t& range_ref()
  5683. {
  5684. return rp_;
  5685. }
  5686. const range_t& range_ref() const
  5687. {
  5688. return rp_;
  5689. }
  5690. inline typename expression_node<T>::node_type type() const
  5691. {
  5692. return expression_node<T>::e_cstringvarrng;
  5693. }
  5694. private:
  5695. const_string_range_node<T>& operator=(const const_string_range_node<T>&);
  5696. const std::string value_;
  5697. range_t rp_;
  5698. };
  5699. template <typename T>
  5700. class generic_string_range_node : public expression_node <T>,
  5701. public string_base_node<T>,
  5702. public range_interface <T>
  5703. {
  5704. public:
  5705. typedef expression_node <T>* expression_ptr;
  5706. typedef stringvar_node <T>* strvar_node_ptr;
  5707. typedef string_base_node<T>* str_base_ptr;
  5708. typedef range_pack <T> range_t;
  5709. typedef range_t* range_ptr;
  5710. typedef range_interface<T> irange_t;
  5711. typedef irange_t* irange_ptr;
  5712. generic_string_range_node(expression_ptr str_branch, range_t brange)
  5713. : initialised_(false),
  5714. branch_(str_branch),
  5715. branch_deletable_(branch_deletable(branch_)),
  5716. str_base_ptr_ (0),
  5717. str_range_ptr_(0),
  5718. base_range_(brange)
  5719. {
  5720. range_.n0_c = std::make_pair<bool,std::size_t>(true,0);
  5721. range_.n1_c = std::make_pair<bool,std::size_t>(true,0);
  5722. range_.cache.first = range_.n0_c.second;
  5723. range_.cache.second = range_.n1_c.second;
  5724. if (is_generally_string_node(branch_))
  5725. {
  5726. str_base_ptr_ = dynamic_cast<str_base_ptr>(branch_);
  5727. if (0 == str_base_ptr_)
  5728. return;
  5729. str_range_ptr_ = dynamic_cast<irange_ptr>(branch_);
  5730. if (0 == str_range_ptr_)
  5731. return;
  5732. }
  5733. initialised_ = (str_base_ptr_ && str_range_ptr_);
  5734. }
  5735. ~generic_string_range_node()
  5736. {
  5737. base_range_.free();
  5738. if (branch_ && branch_deletable_)
  5739. {
  5740. delete branch_;
  5741. branch_ = 0;
  5742. }
  5743. }
  5744. inline T value() const
  5745. {
  5746. if (initialised_)
  5747. {
  5748. branch_->value();
  5749. std::size_t str_r0 = 0;
  5750. std::size_t str_r1 = 0;
  5751. std::size_t r0 = 0;
  5752. std::size_t r1 = 0;
  5753. range_t& range = str_range_ptr_->range_ref();
  5754. const std::size_t base_str_size = str_base_ptr_->size();
  5755. if (
  5756. range (str_r0,str_r1,base_str_size) &&
  5757. base_range_( r0, r1,base_str_size)
  5758. )
  5759. {
  5760. const std::size_t size = (r1 - r0) + 1;
  5761. range_.n1_c.second = size - 1;
  5762. range_.cache.second = range_.n1_c.second;
  5763. value_.assign(str_base_ptr_->base() + str_r0 + r0, size);
  5764. }
  5765. }
  5766. return std::numeric_limits<T>::quiet_NaN();
  5767. }
  5768. std::string str() const
  5769. {
  5770. return value_;
  5771. }
  5772. const char* base() const
  5773. {
  5774. return value_.data();
  5775. }
  5776. std::size_t size() const
  5777. {
  5778. return value_.size();
  5779. }
  5780. range_t& range_ref()
  5781. {
  5782. return range_;
  5783. }
  5784. const range_t& range_ref() const
  5785. {
  5786. return range_;
  5787. }
  5788. inline typename expression_node<T>::node_type type() const
  5789. {
  5790. return expression_node<T>::e_strgenrange;
  5791. }
  5792. private:
  5793. bool initialised_;
  5794. expression_ptr branch_;
  5795. bool branch_deletable_;
  5796. str_base_ptr str_base_ptr_;
  5797. irange_ptr str_range_ptr_;
  5798. mutable range_t base_range_;
  5799. mutable range_t range_;
  5800. mutable std::string value_;
  5801. };
  5802. template <typename T>
  5803. class string_concat_node : public binary_node <T>,
  5804. public string_base_node<T>,
  5805. public range_interface <T>
  5806. {
  5807. public:
  5808. typedef expression_node <T>* expression_ptr;
  5809. typedef string_base_node<T>* str_base_ptr;
  5810. typedef range_pack <T> range_t;
  5811. typedef range_t* range_ptr;
  5812. typedef range_interface<T> irange_t;
  5813. typedef irange_t* irange_ptr;
  5814. string_concat_node(const operator_type& opr,
  5815. expression_ptr branch0,
  5816. expression_ptr branch1)
  5817. : binary_node<T>(opr,branch0,branch1),
  5818. initialised_(false),
  5819. str0_base_ptr_ (0),
  5820. str1_base_ptr_ (0),
  5821. str0_range_ptr_(0),
  5822. str1_range_ptr_(0)
  5823. {
  5824. range_.n0_c = std::make_pair<bool,std::size_t>(true,0);
  5825. range_.n1_c = std::make_pair<bool,std::size_t>(true,0);
  5826. range_.cache.first = range_.n0_c.second;
  5827. range_.cache.second = range_.n1_c.second;
  5828. if (is_generally_string_node(binary_node<T>::branch_[0].first))
  5829. {
  5830. str0_base_ptr_ = dynamic_cast<str_base_ptr>(binary_node<T>::branch_[0].first);
  5831. if (0 == str0_base_ptr_)
  5832. return;
  5833. str0_range_ptr_ = dynamic_cast<irange_ptr>(binary_node<T>::branch_[0].first);
  5834. if (0 == str0_range_ptr_)
  5835. return;
  5836. }
  5837. if (is_generally_string_node(binary_node<T>::branch_[1].first))
  5838. {
  5839. str1_base_ptr_ = dynamic_cast<str_base_ptr>(binary_node<T>::branch_[1].first);
  5840. if (0 == str1_base_ptr_)
  5841. return;
  5842. str1_range_ptr_ = dynamic_cast<irange_ptr>(binary_node<T>::branch_[1].first);
  5843. if (0 == str1_range_ptr_)
  5844. return;
  5845. }
  5846. initialised_ = str0_base_ptr_ &&
  5847. str1_base_ptr_ &&
  5848. str0_range_ptr_ &&
  5849. str1_range_ptr_ ;
  5850. }
  5851. inline T value() const
  5852. {
  5853. if (initialised_)
  5854. {
  5855. binary_node<T>::branch_[0].first->value();
  5856. binary_node<T>::branch_[1].first->value();
  5857. std::size_t str0_r0 = 0;
  5858. std::size_t str0_r1 = 0;
  5859. std::size_t str1_r0 = 0;
  5860. std::size_t str1_r1 = 0;
  5861. range_t& range0 = str0_range_ptr_->range_ref();
  5862. range_t& range1 = str1_range_ptr_->range_ref();
  5863. if (
  5864. range0(str0_r0,str0_r1,str0_base_ptr_->size()) &&
  5865. range1(str1_r0,str1_r1,str1_base_ptr_->size())
  5866. )
  5867. {
  5868. const std::size_t size0 = (str0_r1 - str0_r0) + 1;
  5869. const std::size_t size1 = (str1_r1 - str1_r0) + 1;
  5870. value_.assign(str0_base_ptr_->base() + str0_r0, size0);
  5871. value_.append(str1_base_ptr_->base() + str1_r0, size1);
  5872. range_.n1_c.second = value_.size() - 1;
  5873. range_.cache.second = range_.n1_c.second;
  5874. }
  5875. }
  5876. return std::numeric_limits<T>::quiet_NaN();
  5877. }
  5878. std::string str() const
  5879. {
  5880. return value_;
  5881. }
  5882. const char* base() const
  5883. {
  5884. return value_.data();
  5885. }
  5886. std::size_t size() const
  5887. {
  5888. return value_.size();
  5889. }
  5890. range_t& range_ref()
  5891. {
  5892. return range_;
  5893. }
  5894. const range_t& range_ref() const
  5895. {
  5896. return range_;
  5897. }
  5898. inline typename expression_node<T>::node_type type() const
  5899. {
  5900. return expression_node<T>::e_strconcat;
  5901. }
  5902. private:
  5903. bool initialised_;
  5904. str_base_ptr str0_base_ptr_;
  5905. str_base_ptr str1_base_ptr_;
  5906. irange_ptr str0_range_ptr_;
  5907. irange_ptr str1_range_ptr_;
  5908. mutable range_t range_;
  5909. mutable std::string value_;
  5910. };
  5911. template <typename T>
  5912. class swap_string_node : public binary_node <T>,
  5913. public string_base_node<T>,
  5914. public range_interface <T>
  5915. {
  5916. public:
  5917. typedef expression_node <T>* expression_ptr;
  5918. typedef stringvar_node <T>* strvar_node_ptr;
  5919. typedef string_base_node<T>* str_base_ptr;
  5920. typedef range_pack <T> range_t;
  5921. typedef range_t* range_ptr;
  5922. typedef range_interface<T> irange_t;
  5923. typedef irange_t* irange_ptr;
  5924. swap_string_node(expression_ptr branch0, expression_ptr branch1)
  5925. : binary_node<T>(details::e_swap,branch0,branch1),
  5926. initialised_(false),
  5927. str0_node_ptr_(0),
  5928. str1_node_ptr_(0)
  5929. {
  5930. if (is_string_node(binary_node<T>::branch_[0].first))
  5931. {
  5932. str0_node_ptr_ = static_cast<strvar_node_ptr>(binary_node<T>::branch_[0].first);
  5933. }
  5934. if (is_string_node(binary_node<T>::branch_[1].first))
  5935. {
  5936. str1_node_ptr_ = static_cast<strvar_node_ptr>(binary_node<T>::branch_[1].first);
  5937. }
  5938. initialised_ = (str0_node_ptr_ && str1_node_ptr_);
  5939. }
  5940. inline T value() const
  5941. {
  5942. if (initialised_)
  5943. {
  5944. binary_node<T>::branch_[0].first->value();
  5945. binary_node<T>::branch_[1].first->value();
  5946. std::swap(str0_node_ptr_->ref(),str1_node_ptr_->ref());
  5947. }
  5948. return std::numeric_limits<T>::quiet_NaN();
  5949. }
  5950. std::string str() const
  5951. {
  5952. return str0_node_ptr_->str();
  5953. }
  5954. const char* base() const
  5955. {
  5956. return str0_node_ptr_->base();
  5957. }
  5958. std::size_t size() const
  5959. {
  5960. return str0_node_ptr_->size();
  5961. }
  5962. range_t& range_ref()
  5963. {
  5964. return str0_node_ptr_->range_ref();
  5965. }
  5966. const range_t& range_ref() const
  5967. {
  5968. return str0_node_ptr_->range_ref();
  5969. }
  5970. inline typename expression_node<T>::node_type type() const
  5971. {
  5972. return expression_node<T>::e_strswap;
  5973. }
  5974. private:
  5975. bool initialised_;
  5976. strvar_node_ptr str0_node_ptr_;
  5977. strvar_node_ptr str1_node_ptr_;
  5978. };
  5979. template <typename T>
  5980. class stringvar_size_node : public expression_node<T>
  5981. {
  5982. public:
  5983. static std::string null_value;
  5984. explicit stringvar_size_node()
  5985. : value_(&null_value)
  5986. {}
  5987. explicit stringvar_size_node(std::string& v)
  5988. : value_(&v)
  5989. {}
  5990. inline T value() const
  5991. {
  5992. return T((*value_).size());
  5993. }
  5994. inline typename expression_node<T>::node_type type() const
  5995. {
  5996. return expression_node<T>::e_stringvarsize;
  5997. }
  5998. private:
  5999. std::string* value_;
  6000. };
  6001. template <typename T>
  6002. std::string stringvar_size_node<T>::null_value = std::string("");
  6003. template <typename T>
  6004. class string_size_node : public expression_node<T>
  6005. {
  6006. public:
  6007. typedef expression_node <T>* expression_ptr;
  6008. typedef string_base_node<T>* str_base_ptr;
  6009. string_size_node(expression_ptr brnch)
  6010. : branch_(brnch),
  6011. branch_deletable_(branch_deletable(branch_)),
  6012. str_base_ptr_(0)
  6013. {
  6014. if (is_generally_string_node(branch_))
  6015. {
  6016. str_base_ptr_ = dynamic_cast<str_base_ptr>(branch_);
  6017. if (0 == str_base_ptr_)
  6018. return;
  6019. }
  6020. }
  6021. ~string_size_node()
  6022. {
  6023. if (branch_ && branch_deletable_)
  6024. {
  6025. delete branch_;
  6026. branch_ = 0;
  6027. }
  6028. }
  6029. inline T value() const
  6030. {
  6031. T result = std::numeric_limits<T>::quiet_NaN();
  6032. if (str_base_ptr_)
  6033. {
  6034. branch_->value();
  6035. result = T(str_base_ptr_->size());
  6036. }
  6037. return result;
  6038. }
  6039. inline typename expression_node<T>::node_type type() const
  6040. {
  6041. return expression_node<T>::e_stringsize;
  6042. }
  6043. private:
  6044. expression_ptr branch_;
  6045. bool branch_deletable_;
  6046. str_base_ptr str_base_ptr_;
  6047. };
  6048. struct asn_assignment
  6049. {
  6050. static inline void execute(std::string& s, const char* data, const std::size_t size)
  6051. { s.assign(data,size); }
  6052. };
  6053. struct asn_addassignment
  6054. {
  6055. static inline void execute(std::string& s, const char* data, const std::size_t size)
  6056. { s.append(data,size); }
  6057. };
  6058. template <typename T, typename AssignmentProcess = asn_assignment>
  6059. class assignment_string_node : public binary_node <T>,
  6060. public string_base_node<T>,
  6061. public range_interface <T>
  6062. {
  6063. public:
  6064. typedef expression_node <T>* expression_ptr;
  6065. typedef stringvar_node <T>* strvar_node_ptr;
  6066. typedef string_base_node<T>* str_base_ptr;
  6067. typedef range_pack <T> range_t;
  6068. typedef range_t* range_ptr;
  6069. typedef range_interface<T> irange_t;
  6070. typedef irange_t* irange_ptr;
  6071. assignment_string_node(const operator_type& opr,
  6072. expression_ptr branch0,
  6073. expression_ptr branch1)
  6074. : binary_node<T>(opr,branch0,branch1),
  6075. initialised_(false),
  6076. str0_base_ptr_ (0),
  6077. str1_base_ptr_ (0),
  6078. str0_node_ptr_ (0),
  6079. str1_range_ptr_(0)
  6080. {
  6081. if (is_string_node(binary_node<T>::branch_[0].first))
  6082. {
  6083. str0_node_ptr_ = static_cast<strvar_node_ptr>(binary_node<T>::branch_[0].first);
  6084. str0_base_ptr_ = dynamic_cast<str_base_ptr>(binary_node<T>::branch_[0].first);
  6085. }
  6086. if (is_generally_string_node(binary_node<T>::branch_[1].first))
  6087. {
  6088. str1_base_ptr_ = dynamic_cast<str_base_ptr>(binary_node<T>::branch_[1].first);
  6089. if (0 == str1_base_ptr_)
  6090. return;
  6091. irange_ptr range_ptr = dynamic_cast<irange_ptr>(binary_node<T>::branch_[1].first);
  6092. if (0 == range_ptr)
  6093. return;
  6094. str1_range_ptr_ = &(range_ptr->range_ref());
  6095. }
  6096. initialised_ = str0_base_ptr_ &&
  6097. str1_base_ptr_ &&
  6098. str0_node_ptr_ &&
  6099. str1_range_ptr_ ;
  6100. }
  6101. inline T value() const
  6102. {
  6103. if (initialised_)
  6104. {
  6105. binary_node<T>::branch_[1].first->value();
  6106. std::size_t r0 = 0;
  6107. std::size_t r1 = 0;
  6108. range_t& range = (*str1_range_ptr_);
  6109. if (range(r0,r1,str1_base_ptr_->size()))
  6110. {
  6111. AssignmentProcess::execute(str0_node_ptr_->ref(),
  6112. str1_base_ptr_->base() + r0,
  6113. (r1 - r0) + 1);
  6114. binary_node<T>::branch_[0].first->value();
  6115. }
  6116. }
  6117. return std::numeric_limits<T>::quiet_NaN();
  6118. }
  6119. std::string str() const
  6120. {
  6121. return str0_node_ptr_->str();
  6122. }
  6123. const char* base() const
  6124. {
  6125. return str0_node_ptr_->base();
  6126. }
  6127. std::size_t size() const
  6128. {
  6129. return str0_node_ptr_->size();
  6130. }
  6131. range_t& range_ref()
  6132. {
  6133. return str0_node_ptr_->range_ref();
  6134. }
  6135. const range_t& range_ref() const
  6136. {
  6137. return str0_node_ptr_->range_ref();
  6138. }
  6139. inline typename expression_node<T>::node_type type() const
  6140. {
  6141. return expression_node<T>::e_strass;
  6142. }
  6143. private:
  6144. bool initialised_;
  6145. str_base_ptr str0_base_ptr_;
  6146. str_base_ptr str1_base_ptr_;
  6147. strvar_node_ptr str0_node_ptr_;
  6148. range_ptr str1_range_ptr_;
  6149. };
  6150. template <typename T, typename AssignmentProcess = asn_assignment>
  6151. class assignment_string_range_node : public binary_node <T>,
  6152. public string_base_node<T>,
  6153. public range_interface <T>
  6154. {
  6155. public:
  6156. typedef expression_node <T>* expression_ptr;
  6157. typedef stringvar_node <T>* strvar_node_ptr;
  6158. typedef string_base_node<T>* str_base_ptr;
  6159. typedef range_pack <T> range_t;
  6160. typedef range_t* range_ptr;
  6161. typedef range_interface<T> irange_t;
  6162. typedef irange_t* irange_ptr;
  6163. assignment_string_range_node(const operator_type& opr,
  6164. expression_ptr branch0,
  6165. expression_ptr branch1)
  6166. : binary_node<T>(opr,branch0,branch1),
  6167. initialised_(false),
  6168. str0_base_ptr_ (0),
  6169. str1_base_ptr_ (0),
  6170. str0_node_ptr_ (0),
  6171. str0_range_ptr_(0),
  6172. str1_range_ptr_(0)
  6173. {
  6174. if (is_string_range_node(binary_node<T>::branch_[0].first))
  6175. {
  6176. str0_node_ptr_ = static_cast<strvar_node_ptr>(binary_node<T>::branch_[0].first);
  6177. str0_base_ptr_ = dynamic_cast<str_base_ptr>(binary_node<T>::branch_[0].first);
  6178. irange_ptr range_ptr = dynamic_cast<irange_ptr>(binary_node<T>::branch_[0].first);
  6179. if (0 == range_ptr)
  6180. return;
  6181. str0_range_ptr_ = &(range_ptr->range_ref());
  6182. }
  6183. if (is_generally_string_node(binary_node<T>::branch_[1].first))
  6184. {
  6185. str1_base_ptr_ = dynamic_cast<str_base_ptr>(binary_node<T>::branch_[1].first);
  6186. if (0 == str1_base_ptr_)
  6187. return;
  6188. irange_ptr range_ptr = dynamic_cast<irange_ptr>(binary_node<T>::branch_[1].first);
  6189. if (0 == range_ptr)
  6190. return;
  6191. str1_range_ptr_ = &(range_ptr->range_ref());
  6192. }
  6193. initialised_ = str0_base_ptr_ &&
  6194. str1_base_ptr_ &&
  6195. str0_node_ptr_ &&
  6196. str0_range_ptr_ &&
  6197. str1_range_ptr_ ;
  6198. }
  6199. inline T value() const
  6200. {
  6201. if (initialised_)
  6202. {
  6203. binary_node<T>::branch_[0].first->value();
  6204. binary_node<T>::branch_[1].first->value();
  6205. std::size_t s0_r0 = 0;
  6206. std::size_t s0_r1 = 0;
  6207. std::size_t s1_r0 = 0;
  6208. std::size_t s1_r1 = 0;
  6209. range_t& range0 = (*str0_range_ptr_);
  6210. range_t& range1 = (*str1_range_ptr_);
  6211. if (
  6212. range0(s0_r0,s0_r1,str0_base_ptr_->size()) &&
  6213. range1(s1_r0,s1_r1,str1_base_ptr_->size())
  6214. )
  6215. {
  6216. std::size_t size = std::min((s0_r1 - s0_r0),(s1_r1 - s1_r0)) + 1;
  6217. std::copy(str1_base_ptr_->base() + s1_r0,
  6218. str1_base_ptr_->base() + s1_r0 + size,
  6219. const_cast<char*>(base() + s0_r0));
  6220. }
  6221. }
  6222. return std::numeric_limits<T>::quiet_NaN();
  6223. }
  6224. std::string str() const
  6225. {
  6226. return str0_node_ptr_->str();
  6227. }
  6228. const char* base() const
  6229. {
  6230. return str0_node_ptr_->base();
  6231. }
  6232. std::size_t size() const
  6233. {
  6234. return str0_node_ptr_->size();
  6235. }
  6236. range_t& range_ref()
  6237. {
  6238. return str0_node_ptr_->range_ref();
  6239. }
  6240. const range_t& range_ref() const
  6241. {
  6242. return str0_node_ptr_->range_ref();
  6243. }
  6244. inline typename expression_node<T>::node_type type() const
  6245. {
  6246. return expression_node<T>::e_strass;
  6247. }
  6248. private:
  6249. bool initialised_;
  6250. str_base_ptr str0_base_ptr_;
  6251. str_base_ptr str1_base_ptr_;
  6252. strvar_node_ptr str0_node_ptr_;
  6253. range_ptr str0_range_ptr_;
  6254. range_ptr str1_range_ptr_;
  6255. };
  6256. template <typename T>
  6257. class conditional_string_node : public trinary_node <T>,
  6258. public string_base_node<T>,
  6259. public range_interface <T>
  6260. {
  6261. public:
  6262. typedef expression_node <T>* expression_ptr;
  6263. typedef string_base_node<T>* str_base_ptr;
  6264. typedef range_pack <T> range_t;
  6265. typedef range_t* range_ptr;
  6266. typedef range_interface<T> irange_t;
  6267. typedef irange_t* irange_ptr;
  6268. conditional_string_node(expression_ptr test,
  6269. expression_ptr consequent,
  6270. expression_ptr alternative)
  6271. : trinary_node<T>(details::e_default,consequent,alternative,test),
  6272. initialised_(false),
  6273. str0_base_ptr_ (0),
  6274. str1_base_ptr_ (0),
  6275. str0_range_ptr_(0),
  6276. str1_range_ptr_(0),
  6277. test_ (test),
  6278. consequent_ (consequent),
  6279. alternative_(alternative)
  6280. {
  6281. range_.n0_c = std::make_pair<bool,std::size_t>(true,0);
  6282. range_.n1_c = std::make_pair<bool,std::size_t>(true,0);
  6283. range_.cache.first = range_.n0_c.second;
  6284. range_.cache.second = range_.n1_c.second;
  6285. if (is_generally_string_node(trinary_node<T>::branch_[0].first))
  6286. {
  6287. str0_base_ptr_ = dynamic_cast<str_base_ptr>(trinary_node<T>::branch_[0].first);
  6288. if (0 == str0_base_ptr_)
  6289. return;
  6290. str0_range_ptr_ = dynamic_cast<irange_ptr>(trinary_node<T>::branch_[0].first);
  6291. if (0 == str0_range_ptr_)
  6292. return;
  6293. }
  6294. if (is_generally_string_node(trinary_node<T>::branch_[1].first))
  6295. {
  6296. str1_base_ptr_ = dynamic_cast<str_base_ptr>(trinary_node<T>::branch_[1].first);
  6297. if (0 == str1_base_ptr_)
  6298. return;
  6299. str1_range_ptr_ = dynamic_cast<irange_ptr>(trinary_node<T>::branch_[1].first);
  6300. if (0 == str1_range_ptr_)
  6301. return;
  6302. }
  6303. initialised_ = str0_base_ptr_ &&
  6304. str1_base_ptr_ &&
  6305. str0_range_ptr_ &&
  6306. str1_range_ptr_ ;
  6307. }
  6308. inline T value() const
  6309. {
  6310. if (initialised_)
  6311. {
  6312. std::size_t r0 = 0;
  6313. std::size_t r1 = 0;
  6314. if (is_true(test_))
  6315. {
  6316. consequent_->value();
  6317. range_t& range = str0_range_ptr_->range_ref();
  6318. if (range(r0,r1,str0_base_ptr_->size()))
  6319. {
  6320. const std::size_t size = (r1 - r0) + 1;
  6321. value_.assign(str0_base_ptr_->base() + r0, size);
  6322. range_.n1_c.second = value_.size() - 1;
  6323. range_.cache.second = range_.n1_c.second;
  6324. return T(1);
  6325. }
  6326. }
  6327. else
  6328. {
  6329. alternative_->value();
  6330. range_t& range = str1_range_ptr_->range_ref();
  6331. if (range(r0,r1,str1_base_ptr_->size()))
  6332. {
  6333. const std::size_t size = (r1 - r0) + 1;
  6334. value_.assign(str1_base_ptr_->base() + r0, size);
  6335. range_.n1_c.second = value_.size() - 1;
  6336. range_.cache.second = range_.n1_c.second;
  6337. return T(0);
  6338. }
  6339. }
  6340. }
  6341. return std::numeric_limits<T>::quiet_NaN();
  6342. }
  6343. std::string str() const
  6344. {
  6345. return value_;
  6346. }
  6347. const char* base() const
  6348. {
  6349. return value_.data();
  6350. }
  6351. std::size_t size() const
  6352. {
  6353. return value_.size();
  6354. }
  6355. range_t& range_ref()
  6356. {
  6357. return range_;
  6358. }
  6359. const range_t& range_ref() const
  6360. {
  6361. return range_;
  6362. }
  6363. inline typename expression_node<T>::node_type type() const
  6364. {
  6365. return expression_node<T>::e_strcondition;
  6366. }
  6367. private:
  6368. bool initialised_;
  6369. str_base_ptr str0_base_ptr_;
  6370. str_base_ptr str1_base_ptr_;
  6371. irange_ptr str0_range_ptr_;
  6372. irange_ptr str1_range_ptr_;
  6373. mutable range_t range_;
  6374. mutable std::string value_;
  6375. expression_ptr test_;
  6376. expression_ptr consequent_;
  6377. expression_ptr alternative_;
  6378. };
  6379. template <typename T>
  6380. class cons_conditional_str_node : public binary_node <T>,
  6381. public string_base_node<T>,
  6382. public range_interface <T>
  6383. {
  6384. public:
  6385. typedef expression_node <T>* expression_ptr;
  6386. typedef string_base_node<T>* str_base_ptr;
  6387. typedef range_pack <T> range_t;
  6388. typedef range_t* range_ptr;
  6389. typedef range_interface<T> irange_t;
  6390. typedef irange_t* irange_ptr;
  6391. cons_conditional_str_node(expression_ptr test,
  6392. expression_ptr consequent)
  6393. : binary_node<T>(details::e_default,consequent,test),
  6394. initialised_(false),
  6395. str0_base_ptr_ (0),
  6396. str0_range_ptr_(0),
  6397. test_ (test),
  6398. consequent_(consequent)
  6399. {
  6400. range_.n0_c = std::make_pair<bool,std::size_t>(true,0);
  6401. range_.n1_c = std::make_pair<bool,std::size_t>(true,0);
  6402. range_.cache.first = range_.n0_c.second;
  6403. range_.cache.second = range_.n1_c.second;
  6404. if (is_generally_string_node(binary_node<T>::branch_[0].first))
  6405. {
  6406. str0_base_ptr_ = dynamic_cast<str_base_ptr>(binary_node<T>::branch_[0].first);
  6407. if (0 == str0_base_ptr_)
  6408. return;
  6409. str0_range_ptr_ = dynamic_cast<irange_ptr>(binary_node<T>::branch_[0].first);
  6410. if (0 == str0_range_ptr_)
  6411. return;
  6412. }
  6413. initialised_ = str0_base_ptr_ && str0_range_ptr_ ;
  6414. }
  6415. inline T value() const
  6416. {
  6417. if (initialised_)
  6418. {
  6419. std::size_t r0 = 0;
  6420. std::size_t r1 = 0;
  6421. if (is_true(test_))
  6422. {
  6423. consequent_->value();
  6424. range_t& range = str0_range_ptr_->range_ref();
  6425. if (range(r0,r1,str0_base_ptr_->size()))
  6426. {
  6427. const std::size_t size = (r1 - r0) + 1;
  6428. value_.assign(str0_base_ptr_->base() + r0, size);
  6429. range_.n1_c.second = value_.size() - 1;
  6430. range_.cache.second = range_.n1_c.second;
  6431. return T(1);
  6432. }
  6433. }
  6434. }
  6435. return std::numeric_limits<T>::quiet_NaN();
  6436. }
  6437. std::string str() const
  6438. {
  6439. return value_;
  6440. }
  6441. const char* base() const
  6442. {
  6443. return value_.data();
  6444. }
  6445. std::size_t size() const
  6446. {
  6447. return value_.size();
  6448. }
  6449. range_t& range_ref()
  6450. {
  6451. return range_;
  6452. }
  6453. const range_t& range_ref() const
  6454. {
  6455. return range_;
  6456. }
  6457. inline typename expression_node<T>::node_type type() const
  6458. {
  6459. return expression_node<T>::e_strccondition;
  6460. }
  6461. private:
  6462. bool initialised_;
  6463. str_base_ptr str0_base_ptr_;
  6464. irange_ptr str0_range_ptr_;
  6465. mutable range_t range_;
  6466. mutable std::string value_;
  6467. expression_ptr test_;
  6468. expression_ptr consequent_;
  6469. };
  6470. #endif
  6471. template <typename T, std::size_t N>
  6472. inline T axn(T a, T x)
  6473. {
  6474. // a*x^n
  6475. return a * exprtk::details::numeric::fast_exp<T,N>::result(x);
  6476. }
  6477. template <typename T, std::size_t N>
  6478. inline T axnb(T a, T x, T b)
  6479. {
  6480. // a*x^n+b
  6481. return a * exprtk::details::numeric::fast_exp<T,N>::result(x) + b;
  6482. }
  6483. template <typename T>
  6484. struct sf_base
  6485. {
  6486. typedef typename details::functor_t<T>::Type Type;
  6487. typedef typename details::functor_t<T> functor_t;
  6488. typedef typename functor_t::qfunc_t quaternary_functor_t;
  6489. typedef typename functor_t::tfunc_t trinary_functor_t;
  6490. typedef typename functor_t::bfunc_t binary_functor_t;
  6491. typedef typename functor_t::ufunc_t unary_functor_t;
  6492. };
  6493. #define define_sfop3(NN,OP0,OP1) \
  6494. template <typename T> \
  6495. struct sf##NN##_op : public sf_base<T> \
  6496. { \
  6497. typedef typename sf_base<T>::Type Type; \
  6498. static inline T process(Type x, Type y, Type z) \
  6499. { \
  6500. return (OP0); \
  6501. } \
  6502. static inline std::string id() \
  6503. { \
  6504. return OP1; \
  6505. } \
  6506. }; \
  6507. define_sfop3(00,(x + y) / z ,"(t+t)/t")
  6508. define_sfop3(01,(x + y) * z ,"(t+t)*t")
  6509. define_sfop3(02,(x + y) - z ,"(t+t)-t")
  6510. define_sfop3(03,(x + y) + z ,"(t+t)+t")
  6511. define_sfop3(04,(x - y) + z ,"(t-t)+t")
  6512. define_sfop3(05,(x - y) / z ,"(t-t)/t")
  6513. define_sfop3(06,(x - y) * z ,"(t-t)*t")
  6514. define_sfop3(07,(x * y) + z ,"(t*t)+t")
  6515. define_sfop3(08,(x * y) - z ,"(t*t)-t")
  6516. define_sfop3(09,(x * y) / z ,"(t*t)/t")
  6517. define_sfop3(10,(x * y) * z ,"(t*t)*t")
  6518. define_sfop3(11,(x / y) + z ,"(t/t)+t")
  6519. define_sfop3(12,(x / y) - z ,"(t/t)-t")
  6520. define_sfop3(13,(x / y) / z ,"(t/t)/t")
  6521. define_sfop3(14,(x / y) * z ,"(t/t)*t")
  6522. define_sfop3(15,x / (y + z) ,"t/(t+t)")
  6523. define_sfop3(16,x / (y - z) ,"t/(t-t)")
  6524. define_sfop3(17,x / (y * z) ,"t/(t*t)")
  6525. define_sfop3(18,x / (y / z) ,"t/(t/t)")
  6526. define_sfop3(19,x * (y + z) ,"t*(t+t)")
  6527. define_sfop3(20,x * (y - z) ,"t*(t-t)")
  6528. define_sfop3(21,x * (y * z) ,"t*(t*t)")
  6529. define_sfop3(22,x * (y / z) ,"t*(t/t)")
  6530. define_sfop3(23,x - (y + z) ,"t-(t+t)")
  6531. define_sfop3(24,x - (y - z) ,"t-(t-t)")
  6532. define_sfop3(25,x - (y / z) ,"t-(t/t)")
  6533. define_sfop3(26,x - (y * z) ,"t-(t*t)")
  6534. define_sfop3(27,x + (y * z) ,"t+(t*t)")
  6535. define_sfop3(28,x + (y / z) ,"t+(t/t)")
  6536. define_sfop3(29,x + (y + z) ,"t+(t+t)")
  6537. define_sfop3(30,x + (y - z) ,"t+(t-t)")
  6538. define_sfop3(31,(axnb<T,2>(x,y,z))," ")
  6539. define_sfop3(32,(axnb<T,3>(x,y,z))," ")
  6540. define_sfop3(33,(axnb<T,4>(x,y,z))," ")
  6541. define_sfop3(34,(axnb<T,5>(x,y,z))," ")
  6542. define_sfop3(35,(axnb<T,6>(x,y,z))," ")
  6543. define_sfop3(36,(axnb<T,7>(x,y,z))," ")
  6544. define_sfop3(37,(axnb<T,8>(x,y,z))," ")
  6545. define_sfop3(38,(axnb<T,9>(x,y,z))," ")
  6546. define_sfop3(39,x * numeric::log(y) + z,"")
  6547. define_sfop3(40,x * numeric::log(y) - z,"")
  6548. define_sfop3(41,x * numeric::log10(y) + z,"")
  6549. define_sfop3(42,x * numeric::log10(y) - z,"")
  6550. define_sfop3(43,x * numeric::sin(y) + z ,"")
  6551. define_sfop3(44,x * numeric::sin(y) - z ,"")
  6552. define_sfop3(45,x * numeric::cos(y) + z ,"")
  6553. define_sfop3(46,x * numeric::cos(y) - z ,"")
  6554. define_sfop3(47,details::is_true(x) ? y : z,"")
  6555. #define define_sfop4(NN,OP0,OP1) \
  6556. template <typename T> \
  6557. struct sf##NN##_op : public sf_base<T> \
  6558. { \
  6559. typedef typename sf_base<T>::Type Type; \
  6560. static inline T process(Type x, Type y, Type z, Type w) \
  6561. { \
  6562. return (OP0); \
  6563. } \
  6564. static inline std::string id() { return OP1; } \
  6565. }; \
  6566. define_sfop4(48,(x + ((y + z) / w)),"t+((t+t)/t)")
  6567. define_sfop4(49,(x + ((y + z) * w)),"t+((t+t)*t)")
  6568. define_sfop4(50,(x + ((y - z) / w)),"t+((t-t)/t)")
  6569. define_sfop4(51,(x + ((y - z) * w)),"t+((t-t)*t)")
  6570. define_sfop4(52,(x + ((y * z) / w)),"t+((t*t)/t)")
  6571. define_sfop4(53,(x + ((y * z) * w)),"t+((t*t)*t)")
  6572. define_sfop4(54,(x + ((y / z) + w)),"t+((t/t)+t)")
  6573. define_sfop4(55,(x + ((y / z) / w)),"t+((t/t)/t)")
  6574. define_sfop4(56,(x + ((y / z) * w)),"t+((t/t)*t)")
  6575. define_sfop4(57,(x - ((y + z) / w)),"t-((t+t)/t)")
  6576. define_sfop4(58,(x - ((y + z) * w)),"t-((t+t)*t)")
  6577. define_sfop4(59,(x - ((y - z) / w)),"t-((t-t)/t)")
  6578. define_sfop4(60,(x - ((y - z) * w)),"t-((t-t)*t)")
  6579. define_sfop4(61,(x - ((y * z) / w)),"t-((t*t)/t)")
  6580. define_sfop4(62,(x - ((y * z) * w)),"t-((t*t)*t)")
  6581. define_sfop4(63,(x - ((y / z) / w)),"t-((t/t)/t)")
  6582. define_sfop4(64,(x - ((y / z) * w)),"t-((t/t)*t)")
  6583. define_sfop4(65,(((x + y) * z) - w),"((t+t)*t)-t")
  6584. define_sfop4(66,(((x - y) * z) - w),"((t-t)*t)-t")
  6585. define_sfop4(67,(((x * y) * z) - w),"((t*t)*t)-t")
  6586. define_sfop4(68,(((x / y) * z) - w),"((t/t)*t)-t")
  6587. define_sfop4(69,(((x + y) / z) - w),"((t+t)/t)-t")
  6588. define_sfop4(70,(((x - y) / z) - w),"((t-t)/t)-t")
  6589. define_sfop4(71,(((x * y) / z) - w),"((t*t)/t)-t")
  6590. define_sfop4(72,(((x / y) / z) - w),"((t/t)/t)-t")
  6591. define_sfop4(73,((x * y) + (z * w)),"(t*t)+(t*t)")
  6592. define_sfop4(74,((x * y) - (z * w)),"(t*t)-(t*t)")
  6593. define_sfop4(75,((x * y) + (z / w)),"(t*t)+(t/t)")
  6594. define_sfop4(76,((x * y) - (z / w)),"(t*t)-(t/t)")
  6595. define_sfop4(77,((x / y) + (z / w)),"(t/t)+(t/t)")
  6596. define_sfop4(78,((x / y) - (z / w)),"(t/t)-(t/t)")
  6597. define_sfop4(79,((x / y) - (z * w)),"(t/t)-(t*t)")
  6598. define_sfop4(80,(x / (y + (z * w))),"t/(t+(t*t))")
  6599. define_sfop4(81,(x / (y - (z * w))),"t/(t-(t*t))")
  6600. define_sfop4(82,(x * (y + (z * w))),"t*(t+(t*t))")
  6601. define_sfop4(83,(x * (y - (z * w))),"t*(t-(t*t))")
  6602. define_sfop4(84,(axn<T,2>(x,y) + axn<T,2>(z,w)),"")
  6603. define_sfop4(85,(axn<T,3>(x,y) + axn<T,3>(z,w)),"")
  6604. define_sfop4(86,(axn<T,4>(x,y) + axn<T,4>(z,w)),"")
  6605. define_sfop4(87,(axn<T,5>(x,y) + axn<T,5>(z,w)),"")
  6606. define_sfop4(88,(axn<T,6>(x,y) + axn<T,6>(z,w)),"")
  6607. define_sfop4(89,(axn<T,7>(x,y) + axn<T,7>(z,w)),"")
  6608. define_sfop4(90,(axn<T,8>(x,y) + axn<T,8>(z,w)),"")
  6609. define_sfop4(91,(axn<T,9>(x,y) + axn<T,9>(z,w)),"")
  6610. define_sfop4(92,((details::is_true(x) && details::is_true(y)) ? z : w),"")
  6611. define_sfop4(93,((details::is_true(x) || details::is_true(y)) ? z : w),"")
  6612. define_sfop4(94,((x < y) ? z : w),"")
  6613. define_sfop4(95,((x <= y) ? z : w),"")
  6614. define_sfop4(96,((x > y) ? z : w),"")
  6615. define_sfop4(97,((x >= y) ? z : w),"")
  6616. define_sfop4(98,(details::is_true(numeric::equal(x,y)) ? z : w),"")
  6617. define_sfop4(99,(x * numeric::sin(y) + z * numeric::cos(w)),"")
  6618. define_sfop4(ext00,((x + y) - (z * w)),"(t+t)-(t*t)")
  6619. define_sfop4(ext01,((x + y) - (z / w)),"(t+t)-(t/t)")
  6620. define_sfop4(ext02,((x + y) + (z * w)),"(t+t)+(t*t)")
  6621. define_sfop4(ext03,((x + y) + (z / w)),"(t+t)+(t/t)")
  6622. define_sfop4(ext04,((x - y) + (z * w)),"(t-t)+(t*t)")
  6623. define_sfop4(ext05,((x - y) + (z / w)),"(t-t)+(t/t)")
  6624. define_sfop4(ext06,((x - y) - (z * w)),"(t-t)-(t*t)")
  6625. define_sfop4(ext07,((x - y) - (z / w)),"(t-t)-(t/t)")
  6626. define_sfop4(ext08,((x + y) - (z - w)),"(t+t)-(t-t)")
  6627. define_sfop4(ext09,((x + y) + (z - w)),"(t+t)+(t-t)")
  6628. define_sfop4(ext10,((x + y) * (z - w)),"(t+t)*(t-t)")
  6629. define_sfop4(ext11,((x + y) / (z - w)),"(t+t)/(t-t)")
  6630. define_sfop4(ext12,((x - y) - (z + w)),"(t-t)-(t+t)")
  6631. define_sfop4(ext13,((x - y) + (z + w)),"(t-t)+(t+t)")
  6632. define_sfop4(ext14,((x - y) * (z + w)),"(t-t)*(t+t)")
  6633. define_sfop4(ext15,((x - y) / (z + w)),"(t-t)/(t+t)")
  6634. define_sfop4(ext16,((x * y) - (z + w)),"(t*t)-(t+t)")
  6635. define_sfop4(ext17,((x / y) - (z + w)),"(t/t)-(t+t)")
  6636. define_sfop4(ext18,((x * y) + (z + w)),"(t*t)+(t+t)")
  6637. define_sfop4(ext19,((x / y) + (z + w)),"(t/t)+(t+t)")
  6638. define_sfop4(ext20,((x * y) + (z - w)),"(t*t)+(t-t)")
  6639. define_sfop4(ext21,((x / y) + (z - w)),"(t/t)+(t-t)")
  6640. define_sfop4(ext22,((x * y) - (z - w)),"(t*t)-(t-t)")
  6641. define_sfop4(ext23,((x / y) - (z - w)),"(t/t)-(t-t)")
  6642. define_sfop4(ext24,((x + y) * (z * w)),"(t+t)*(t*t)")
  6643. define_sfop4(ext25,((x + y) * (z / w)),"(t+t)*(t/t)")
  6644. define_sfop4(ext26,((x + y) / (z * w)),"(t+t)/(t*t)")
  6645. define_sfop4(ext27,((x + y) / (z / w)),"(t+t)/(t/t)")
  6646. define_sfop4(ext28,((x - y) / (z * w)),"(t-t)/(t*t)")
  6647. define_sfop4(ext29,((x - y) / (z / w)),"(t-t)/(t/t)")
  6648. define_sfop4(ext30,((x - y) * (z * w)),"(t-t)*(t*t)")
  6649. define_sfop4(ext31,((x - y) * (z / w)),"(t-t)*(t/t)")
  6650. define_sfop4(ext32,((x * y) * (z + w)),"(t*t)*(t+t)")
  6651. define_sfop4(ext33,((x / y) * (z + w)),"(t/t)*(t+t)")
  6652. define_sfop4(ext34,((x * y) / (z + w)),"(t*t)/(t+t)")
  6653. define_sfop4(ext35,((x / y) / (z + w)),"(t/t)/(t+t)")
  6654. define_sfop4(ext36,((x * y) / (z - w)),"(t*t)/(t-t)")
  6655. define_sfop4(ext37,((x / y) / (z - w)),"(t/t)/(t-t)")
  6656. define_sfop4(ext38,((x * y) * (z - w)),"(t*t)*(t-t)")
  6657. define_sfop4(ext39,((x * y) / (z * w)),"(t*t)/(t*t)")
  6658. define_sfop4(ext40,((x / y) * (z / w)),"(t/t)*(t/t)")
  6659. define_sfop4(ext41,((x / y) * (z - w)),"(t/t)*(t-t)")
  6660. define_sfop4(ext42,((x * y) * (z * w)),"(t*t)*(t*t)")
  6661. define_sfop4(ext43,(x + (y * (z / w))),"t+(t*(t/t))")
  6662. define_sfop4(ext44,(x - (y * (z / w))),"t-(t*(t/t))")
  6663. define_sfop4(ext45,(x + (y / (z * w))),"t+(t/(t*t))")
  6664. define_sfop4(ext46,(x - (y / (z * w))),"t-(t/(t*t))")
  6665. define_sfop4(ext47,(((x - y) - z) * w),"((t-t)-t)*t")
  6666. define_sfop4(ext48,(((x - y) - z) / w),"((t-t)-t)/t")
  6667. define_sfop4(ext49,(((x - y) + z) * w),"((t-t)+t)*t")
  6668. define_sfop4(ext50,(((x - y) + z) / w),"((t-t)+t)/t")
  6669. define_sfop4(ext51,((x + (y - z)) * w),"(t+(t-t))*t")
  6670. define_sfop4(ext52,((x + (y - z)) / w),"(t+(t-t))/t")
  6671. define_sfop4(ext53,((x + y) / (z + w)),"(t+t)/(t+t)")
  6672. define_sfop4(ext54,((x - y) / (z - w)),"(t-t)/(t-t)")
  6673. define_sfop4(ext55,((x + y) * (z + w)),"(t+t)*(t+t)")
  6674. define_sfop4(ext56,((x - y) * (z - w)),"(t-t)*(t-t)")
  6675. define_sfop4(ext57,((x - y) + (z - w)),"(t-t)+(t-t)")
  6676. define_sfop4(ext58,((x - y) - (z - w)),"(t-t)-(t-t)")
  6677. define_sfop4(ext59,((x / y) + (z * w)),"(t/t)+(t*t)")
  6678. #undef define_sfop3
  6679. #undef define_sfop4
  6680. template <typename T, typename SpecialFunction>
  6681. class sf3_node : public trinary_node<T>
  6682. {
  6683. public:
  6684. typedef expression_node<T>* expression_ptr;
  6685. sf3_node(const operator_type& opr,
  6686. expression_ptr branch0,
  6687. expression_ptr branch1,
  6688. expression_ptr branch2)
  6689. : trinary_node<T>(opr,branch0,branch1,branch2)
  6690. {}
  6691. inline T value() const
  6692. {
  6693. const T x = trinary_node<T>::branch_[0].first->value();
  6694. const T y = trinary_node<T>::branch_[1].first->value();
  6695. const T z = trinary_node<T>::branch_[2].first->value();
  6696. return SpecialFunction::process(x,y,z);
  6697. }
  6698. };
  6699. template <typename T, typename SpecialFunction>
  6700. class sf4_node : public quaternary_node<T>
  6701. {
  6702. public:
  6703. typedef expression_node<T>* expression_ptr;
  6704. sf4_node(const operator_type& opr,
  6705. expression_ptr branch0,
  6706. expression_ptr branch1,
  6707. expression_ptr branch2,
  6708. expression_ptr branch3)
  6709. : quaternary_node<T>(opr,branch0,branch1,branch2,branch3)
  6710. {}
  6711. inline T value() const
  6712. {
  6713. const T x = quaternary_node<T>::branch_[0].first->value();
  6714. const T y = quaternary_node<T>::branch_[1].first->value();
  6715. const T z = quaternary_node<T>::branch_[2].first->value();
  6716. const T w = quaternary_node<T>::branch_[3].first->value();
  6717. return SpecialFunction::process(x,y,z,w);
  6718. }
  6719. };
  6720. template <typename T, typename SpecialFunction>
  6721. class sf3_var_node : public expression_node<T>
  6722. {
  6723. public:
  6724. typedef expression_node<T>* expression_ptr;
  6725. sf3_var_node(const T& v0, const T& v1, const T& v2)
  6726. : v0_(v0),
  6727. v1_(v1),
  6728. v2_(v2)
  6729. {}
  6730. inline T value() const
  6731. {
  6732. return SpecialFunction::process(v0_,v1_,v2_);
  6733. }
  6734. inline typename expression_node<T>::node_type type() const
  6735. {
  6736. return expression_node<T>::e_trinary;
  6737. }
  6738. private:
  6739. sf3_var_node(sf3_var_node<T,SpecialFunction>&);
  6740. sf3_var_node<T,SpecialFunction>& operator=(sf3_var_node<T,SpecialFunction>&);
  6741. const T& v0_;
  6742. const T& v1_;
  6743. const T& v2_;
  6744. };
  6745. template <typename T, typename SpecialFunction>
  6746. class sf4_var_node : public expression_node<T>
  6747. {
  6748. public:
  6749. typedef expression_node<T>* expression_ptr;
  6750. sf4_var_node(const T& v0, const T& v1, const T& v2, const T& v3)
  6751. : v0_(v0),
  6752. v1_(v1),
  6753. v2_(v2),
  6754. v3_(v3)
  6755. {}
  6756. inline T value() const
  6757. {
  6758. return SpecialFunction::process(v0_,v1_,v2_,v3_);
  6759. }
  6760. inline typename expression_node<T>::node_type type() const
  6761. {
  6762. return expression_node<T>::e_trinary;
  6763. }
  6764. private:
  6765. sf4_var_node(sf4_var_node<T,SpecialFunction>&);
  6766. sf4_var_node<T,SpecialFunction>& operator=(sf4_var_node<T,SpecialFunction>&);
  6767. const T& v0_;
  6768. const T& v1_;
  6769. const T& v2_;
  6770. const T& v3_;
  6771. };
  6772. template <typename T, typename VarArgFunction>
  6773. class vararg_node : public expression_node<T>
  6774. {
  6775. public:
  6776. typedef expression_node<T>* expression_ptr;
  6777. template <typename Allocator,
  6778. template <typename,typename> class Sequence>
  6779. vararg_node(const Sequence<expression_ptr,Allocator>& arg_list)
  6780. {
  6781. arg_list_.resize(arg_list.size());
  6782. delete_branch_.resize(arg_list.size());
  6783. for (std::size_t i = 0; i < arg_list.size(); ++i)
  6784. {
  6785. if (arg_list[i])
  6786. {
  6787. arg_list_[i] = arg_list[i];
  6788. delete_branch_[i] = static_cast<unsigned char>(branch_deletable(arg_list_[i]) ? 1 : 0);
  6789. }
  6790. else
  6791. {
  6792. arg_list_.clear();
  6793. delete_branch_.clear();
  6794. return;
  6795. }
  6796. }
  6797. }
  6798. ~vararg_node()
  6799. {
  6800. for (std::size_t i = 0; i < arg_list_.size(); ++i)
  6801. {
  6802. if (arg_list_[i] && delete_branch_[i])
  6803. {
  6804. delete arg_list_[i];
  6805. arg_list_[i] = 0;
  6806. }
  6807. }
  6808. }
  6809. inline T value() const
  6810. {
  6811. if (!arg_list_.empty())
  6812. return VarArgFunction::process(arg_list_);
  6813. else
  6814. return std::numeric_limits<T>::quiet_NaN();
  6815. }
  6816. inline typename expression_node<T>::node_type type() const
  6817. {
  6818. return expression_node<T>::e_vararg;
  6819. }
  6820. private:
  6821. std::vector<expression_ptr> arg_list_;
  6822. std::vector<unsigned char> delete_branch_;
  6823. };
  6824. template <typename T, typename VarArgFunction>
  6825. class vararg_varnode : public expression_node<T>
  6826. {
  6827. public:
  6828. typedef expression_node<T>* expression_ptr;
  6829. template <typename Allocator,
  6830. template <typename,typename> class Sequence>
  6831. vararg_varnode(const Sequence<expression_ptr,Allocator>& arg_list)
  6832. {
  6833. arg_list_.resize(arg_list.size());
  6834. for (std::size_t i = 0; i < arg_list.size(); ++i)
  6835. {
  6836. if (arg_list[i] && is_variable_node(arg_list[i]))
  6837. {
  6838. variable_node<T>* var_node_ptr = static_cast<variable_node<T>*>(arg_list[i]);
  6839. arg_list_[i] = (&var_node_ptr->ref());
  6840. }
  6841. else
  6842. {
  6843. arg_list_.clear();
  6844. return;
  6845. }
  6846. }
  6847. }
  6848. inline T value() const
  6849. {
  6850. if (!arg_list_.empty())
  6851. return VarArgFunction::process(arg_list_);
  6852. else
  6853. return std::numeric_limits<T>::quiet_NaN();
  6854. }
  6855. inline typename expression_node<T>::node_type type() const
  6856. {
  6857. return expression_node<T>::e_vararg;
  6858. }
  6859. private:
  6860. std::vector<const T*> arg_list_;
  6861. };
  6862. template <typename T, typename VecFunction>
  6863. class vectorize_node : public expression_node<T>
  6864. {
  6865. public:
  6866. typedef expression_node<T>* expression_ptr;
  6867. vectorize_node(const expression_ptr v)
  6868. : ivec_ptr_(0),
  6869. v_(v),
  6870. v_deletable_(branch_deletable(v_))
  6871. {
  6872. if (is_ivector_node(v))
  6873. {
  6874. ivec_ptr_ = dynamic_cast<vector_interface<T>*>(v);
  6875. }
  6876. else
  6877. ivec_ptr_ = 0;
  6878. }
  6879. ~vectorize_node()
  6880. {
  6881. if (v_ && v_deletable_)
  6882. {
  6883. delete v_;
  6884. }
  6885. }
  6886. inline T value() const
  6887. {
  6888. if (ivec_ptr_)
  6889. {
  6890. v_->value();
  6891. return VecFunction::process(ivec_ptr_);
  6892. }
  6893. else
  6894. return std::numeric_limits<T>::quiet_NaN();
  6895. }
  6896. inline typename expression_node<T>::node_type type() const
  6897. {
  6898. return expression_node<T>::e_vecfunc;
  6899. }
  6900. private:
  6901. vector_interface<T>* ivec_ptr_;
  6902. expression_ptr v_;
  6903. bool v_deletable_;
  6904. };
  6905. template <typename T>
  6906. class assignment_node : public binary_node<T>
  6907. {
  6908. public:
  6909. typedef expression_node<T>* expression_ptr;
  6910. assignment_node(const operator_type& opr,
  6911. expression_ptr branch0,
  6912. expression_ptr branch1)
  6913. : binary_node<T>(opr,branch0,branch1),
  6914. var_node_ptr_(0)
  6915. {
  6916. if (is_variable_node(binary_node<T>::branch_[0].first))
  6917. {
  6918. var_node_ptr_ = static_cast<variable_node<T>*>(binary_node<T>::branch_[0].first);
  6919. }
  6920. }
  6921. inline T value() const
  6922. {
  6923. if (var_node_ptr_)
  6924. {
  6925. T& result = var_node_ptr_->ref();
  6926. result = binary_node<T>::branch_[1].first->value();
  6927. return result;
  6928. }
  6929. else
  6930. return std::numeric_limits<T>::quiet_NaN();
  6931. }
  6932. private:
  6933. variable_node<T>* var_node_ptr_;
  6934. };
  6935. template <typename T>
  6936. class assignment_vec_elem_node : public binary_node<T>
  6937. {
  6938. public:
  6939. typedef expression_node<T>* expression_ptr;
  6940. assignment_vec_elem_node(const operator_type& opr,
  6941. expression_ptr branch0,
  6942. expression_ptr branch1)
  6943. : binary_node<T>(opr,branch0,branch1),
  6944. vec_node_ptr_(0)
  6945. {
  6946. if (is_vector_elem_node(binary_node<T>::branch_[0].first))
  6947. {
  6948. vec_node_ptr_ = static_cast<vector_elem_node<T>*>(binary_node<T>::branch_[0].first);
  6949. }
  6950. }
  6951. inline T value() const
  6952. {
  6953. if (vec_node_ptr_)
  6954. {
  6955. T& result = vec_node_ptr_->ref();
  6956. result = binary_node<T>::branch_[1].first->value();
  6957. return result;
  6958. }
  6959. else
  6960. return std::numeric_limits<T>::quiet_NaN();
  6961. }
  6962. private:
  6963. vector_elem_node<T>* vec_node_ptr_;
  6964. };
  6965. template <typename T>
  6966. class assignment_vec_node : public binary_node <T>,
  6967. public vector_interface<T>
  6968. {
  6969. public:
  6970. typedef expression_node<T>* expression_ptr;
  6971. typedef vector_node<T>* vector_node_ptr;
  6972. assignment_vec_node(const operator_type& opr,
  6973. expression_ptr branch0,
  6974. expression_ptr branch1)
  6975. : binary_node<T>(opr,branch0,branch1),
  6976. vec_node_ptr_(0),
  6977. vec_size_ (0)
  6978. {
  6979. if (is_vector_node(binary_node<T>::branch_[0].first))
  6980. {
  6981. vec_node_ptr_ = static_cast<vector_node<T>*>(binary_node<T>::branch_[0].first);
  6982. vec_size_ = vec_node_ptr_->ref().size();
  6983. }
  6984. }
  6985. inline T value() const
  6986. {
  6987. if (vec_node_ptr_)
  6988. {
  6989. vector_holder<T>& vec_hldr = vec_node_ptr_->ref();
  6990. const T v = binary_node<T>::branch_[1].first->value();
  6991. for (std::size_t i = 0; i < vec_size_; ++i)
  6992. {
  6993. (*vec_hldr[i]) = v;
  6994. }
  6995. return vec_node_ptr_->value();
  6996. }
  6997. else
  6998. return std::numeric_limits<T>::quiet_NaN();
  6999. }
  7000. vector_node_ptr vec() const
  7001. {
  7002. return vec_node_ptr_;
  7003. }
  7004. vector_node_ptr vec()
  7005. {
  7006. return vec_node_ptr_;
  7007. }
  7008. inline typename expression_node<T>::node_type type() const
  7009. {
  7010. return expression_node<T>::e_vecvalass;
  7011. }
  7012. std::size_t size() const
  7013. {
  7014. return vec_size_;
  7015. }
  7016. private:
  7017. vector_node<T>* vec_node_ptr_;
  7018. std::size_t vec_size_;
  7019. };
  7020. template <typename T>
  7021. class assignment_vecvec_node : public binary_node <T>,
  7022. public vector_interface<T>
  7023. {
  7024. public:
  7025. typedef expression_node<T>* expression_ptr;
  7026. typedef vector_node<T>* vector_node_ptr;
  7027. assignment_vecvec_node(const operator_type& opr,
  7028. expression_ptr branch0,
  7029. expression_ptr branch1)
  7030. : binary_node<T>(opr,branch0,branch1),
  7031. vec0_node_ptr_(0),
  7032. vec1_node_ptr_(0),
  7033. vec_size_ (0)
  7034. {
  7035. if (is_vector_node(binary_node<T>::branch_[0].first))
  7036. {
  7037. vec0_node_ptr_ = static_cast<vector_node<T>*>(binary_node<T>::branch_[0].first);
  7038. }
  7039. if (is_vector_node(binary_node<T>::branch_[1].first))
  7040. {
  7041. vec1_node_ptr_ = static_cast<vector_node<T>*>(binary_node<T>::branch_[1].first);
  7042. }
  7043. else if (is_ivector_node(binary_node<T>::branch_[1].first))
  7044. {
  7045. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7046. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[1].first)))
  7047. {
  7048. vec1_node_ptr_ = vi->vec();
  7049. }
  7050. }
  7051. if (vec0_node_ptr_ && vec1_node_ptr_)
  7052. {
  7053. vec_size_ = std::min(vec0_node_ptr_->ref().size(),
  7054. vec1_node_ptr_->ref().size());
  7055. }
  7056. }
  7057. inline T value() const
  7058. {
  7059. binary_node<T>::branch_[1].first->value();
  7060. if (vec0_node_ptr_ && vec1_node_ptr_)
  7061. {
  7062. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  7063. vector_holder<T>& vec1 = vec1_node_ptr_->ref();
  7064. for (std::size_t i = 0; i < vec_size_; ++i)
  7065. {
  7066. (*vec0[i]) = (*vec1[i]);
  7067. }
  7068. return vec0_node_ptr_->value();
  7069. }
  7070. else
  7071. return std::numeric_limits<T>::quiet_NaN();
  7072. }
  7073. vector_node_ptr vec() const
  7074. {
  7075. return vec0_node_ptr_;
  7076. }
  7077. vector_node_ptr vec()
  7078. {
  7079. return vec0_node_ptr_;
  7080. }
  7081. inline typename expression_node<T>::node_type type() const
  7082. {
  7083. return expression_node<T>::e_vecvecass;
  7084. }
  7085. std::size_t size() const
  7086. {
  7087. return vec_size_;
  7088. }
  7089. private:
  7090. vector_node<T>* vec0_node_ptr_;
  7091. vector_node<T>* vec1_node_ptr_;
  7092. std::size_t vec_size_;
  7093. };
  7094. template <typename T, typename Operation>
  7095. class assignment_op_node : public binary_node<T>
  7096. {
  7097. public:
  7098. typedef expression_node<T>* expression_ptr;
  7099. assignment_op_node(const operator_type& opr,
  7100. expression_ptr branch0,
  7101. expression_ptr branch1)
  7102. : binary_node<T>(opr,branch0,branch1),
  7103. var_node_ptr_(0)
  7104. {
  7105. if (is_variable_node(binary_node<T>::branch_[0].first))
  7106. {
  7107. var_node_ptr_ = static_cast<variable_node<T>*>(binary_node<T>::branch_[0].first);
  7108. }
  7109. }
  7110. inline T value() const
  7111. {
  7112. if (var_node_ptr_)
  7113. {
  7114. T& v = var_node_ptr_->ref();
  7115. v = Operation::process(v,binary_node<T>::branch_[1].first->value());
  7116. return v;
  7117. }
  7118. else
  7119. return std::numeric_limits<T>::quiet_NaN();
  7120. }
  7121. private:
  7122. variable_node<T>* var_node_ptr_;
  7123. };
  7124. template <typename T, typename Operation>
  7125. class assignment_vec_elem_op_node : public binary_node<T>
  7126. {
  7127. public:
  7128. typedef expression_node<T>* expression_ptr;
  7129. assignment_vec_elem_op_node(const operator_type& opr,
  7130. expression_ptr branch0,
  7131. expression_ptr branch1)
  7132. : binary_node<T>(opr,branch0,branch1),
  7133. vec_node_ptr_(0)
  7134. {
  7135. if (is_vector_elem_node(binary_node<T>::branch_[0].first))
  7136. {
  7137. vec_node_ptr_ = static_cast<vector_elem_node<T>*>(binary_node<T>::branch_[0].first);
  7138. }
  7139. }
  7140. inline T value() const
  7141. {
  7142. if (vec_node_ptr_)
  7143. {
  7144. T& v = vec_node_ptr_->ref();
  7145. v = Operation::process(v,binary_node<T>::branch_[1].first->value());
  7146. return v;
  7147. }
  7148. else
  7149. return std::numeric_limits<T>::quiet_NaN();
  7150. }
  7151. private:
  7152. vector_elem_node<T>* vec_node_ptr_;
  7153. };
  7154. template <typename T, typename Operation>
  7155. class assignment_vec_op_node : public binary_node <T>,
  7156. public vector_interface<T>
  7157. {
  7158. public:
  7159. typedef expression_node<T>* expression_ptr;
  7160. typedef vector_node<T>* vector_node_ptr;
  7161. assignment_vec_op_node(const operator_type& opr,
  7162. expression_ptr branch0,
  7163. expression_ptr branch1)
  7164. : binary_node<T>(opr,branch0,branch1),
  7165. vec_node_ptr_(0),
  7166. vec_size_ (0)
  7167. {
  7168. if (is_vector_node(binary_node<T>::branch_[0].first))
  7169. {
  7170. vec_node_ptr_ = static_cast<vector_node<T>*>(binary_node<T>::branch_[0].first);
  7171. vec_size_ = vec_node_ptr_->ref().size();
  7172. }
  7173. }
  7174. inline T value() const
  7175. {
  7176. if (vec_node_ptr_)
  7177. {
  7178. vector_holder<T>& vec_hldr = vec_node_ptr_->ref();
  7179. const T v = binary_node<T>::branch_[1].first->value();
  7180. for (std::size_t i = 0; i < vec_size_; ++i)
  7181. {
  7182. T& vec_i = *vec_hldr[i];
  7183. vec_i = Operation::process(vec_i,v);
  7184. }
  7185. return vec_node_ptr_->value();
  7186. }
  7187. else
  7188. return std::numeric_limits<T>::quiet_NaN();
  7189. }
  7190. vector_node_ptr vec() const
  7191. {
  7192. return vec_node_ptr_;
  7193. }
  7194. vector_node_ptr vec()
  7195. {
  7196. return vec_node_ptr_;
  7197. }
  7198. inline typename expression_node<T>::node_type type() const
  7199. {
  7200. return expression_node<T>::e_vecopvalass;
  7201. }
  7202. std::size_t size() const
  7203. {
  7204. return vec_size_;
  7205. }
  7206. private:
  7207. vector_node<T>* vec_node_ptr_;
  7208. std::size_t vec_size_;
  7209. };
  7210. template <typename T, typename Operation>
  7211. class assignment_vecvec_op_node : public binary_node <T>,
  7212. public vector_interface<T>
  7213. {
  7214. public:
  7215. typedef expression_node<T>* expression_ptr;
  7216. typedef vector_node<T>* vector_node_ptr;
  7217. assignment_vecvec_op_node(const operator_type& opr,
  7218. expression_ptr branch0,
  7219. expression_ptr branch1)
  7220. : binary_node<T>(opr,branch0,branch1),
  7221. vec0_node_ptr_(0),
  7222. vec1_node_ptr_(0),
  7223. vec_size_ (0)
  7224. {
  7225. if (is_vector_node(binary_node<T>::branch_[0].first))
  7226. {
  7227. vec0_node_ptr_ = static_cast<vector_node<T>*>(binary_node<T>::branch_[0].first);
  7228. }
  7229. if (is_vector_node(binary_node<T>::branch_[1].first))
  7230. {
  7231. vec1_node_ptr_ = static_cast<vector_node<T>*>(binary_node<T>::branch_[1].first);
  7232. }
  7233. else if (is_ivector_node(binary_node<T>::branch_[1].first))
  7234. {
  7235. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7236. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[1].first)))
  7237. {
  7238. vec1_node_ptr_ = vi->vec();
  7239. }
  7240. }
  7241. if (vec0_node_ptr_ && vec1_node_ptr_)
  7242. {
  7243. vec_size_ = std::min(vec0_node_ptr_->ref().size(),
  7244. vec1_node_ptr_->ref().size());
  7245. }
  7246. }
  7247. inline T value() const
  7248. {
  7249. if (vec0_node_ptr_ && vec1_node_ptr_)
  7250. {
  7251. binary_node<T>::branch_[0].first->value();
  7252. binary_node<T>::branch_[1].first->value();
  7253. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  7254. vector_holder<T>& vec1 = vec1_node_ptr_->ref();
  7255. for (std::size_t i = 0; i < vec_size_; ++i)
  7256. {
  7257. T& vec0_i = *vec0[i];
  7258. T& vec1_i = *vec1[i];
  7259. vec0_i = Operation::process(vec0_i,vec1_i);
  7260. }
  7261. return vec0_node_ptr_->value();
  7262. }
  7263. else
  7264. return std::numeric_limits<T>::quiet_NaN();
  7265. }
  7266. vector_node_ptr vec() const
  7267. {
  7268. return vec0_node_ptr_;
  7269. }
  7270. vector_node_ptr vec()
  7271. {
  7272. return vec0_node_ptr_;
  7273. }
  7274. inline typename expression_node<T>::node_type type() const
  7275. {
  7276. return expression_node<T>::e_vecopvecass;
  7277. }
  7278. std::size_t size() const
  7279. {
  7280. return vec_size_;
  7281. }
  7282. private:
  7283. vector_node<T>* vec0_node_ptr_;
  7284. vector_node<T>* vec1_node_ptr_;
  7285. std::size_t vec_size_;
  7286. };
  7287. template <typename T, typename Operation>
  7288. class eqineq_vecvec_node : public binary_node <T>,
  7289. public vector_interface<T>
  7290. {
  7291. public:
  7292. typedef expression_node<T>* expression_ptr;
  7293. typedef vector_node<T>* vector_node_ptr;
  7294. eqineq_vecvec_node(const operator_type& opr,
  7295. expression_ptr branch0,
  7296. expression_ptr branch1)
  7297. : binary_node<T>(opr,branch0,branch1),
  7298. vec0_node_ptr_(0),
  7299. vec1_node_ptr_(0),
  7300. vec_size_ (0)
  7301. {
  7302. if (is_vector_node(binary_node<T>::branch_[0].first))
  7303. {
  7304. vec0_node_ptr_ = static_cast<vector_node<T>*>(binary_node<T>::branch_[0].first);
  7305. }
  7306. else if (is_ivector_node(binary_node<T>::branch_[0].first))
  7307. {
  7308. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7309. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[0].first)))
  7310. {
  7311. vec0_node_ptr_ = vi->vec();
  7312. }
  7313. }
  7314. if (is_vector_node(binary_node<T>::branch_[1].first))
  7315. {
  7316. vec1_node_ptr_ = static_cast<vector_node<T>*>(binary_node<T>::branch_[1].first);
  7317. }
  7318. else if (is_ivector_node(binary_node<T>::branch_[1].first))
  7319. {
  7320. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7321. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[1].first)))
  7322. {
  7323. vec1_node_ptr_ = vi->vec();
  7324. }
  7325. }
  7326. if (vec0_node_ptr_ && vec1_node_ptr_)
  7327. {
  7328. vec_size_ = std::min(vec0_node_ptr_->ref().size(),
  7329. vec1_node_ptr_->ref().size());
  7330. }
  7331. }
  7332. inline T value() const
  7333. {
  7334. if (vec0_node_ptr_ && vec1_node_ptr_)
  7335. {
  7336. binary_node<T>::branch_[0].first->value();
  7337. binary_node<T>::branch_[1].first->value();
  7338. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  7339. vector_holder<T>& vec1 = vec1_node_ptr_->ref();
  7340. for (std::size_t i = 0; i < vec_size_; ++i)
  7341. {
  7342. if (std::equal_to<T>()(T(0),Operation::process(*vec0[i],*vec1[i])))
  7343. {
  7344. return T(0);
  7345. }
  7346. }
  7347. return T(1);
  7348. }
  7349. else
  7350. return std::numeric_limits<T>::quiet_NaN();
  7351. }
  7352. vector_node_ptr vec() const
  7353. {
  7354. return vec0_node_ptr_;
  7355. }
  7356. vector_node_ptr vec()
  7357. {
  7358. return vec0_node_ptr_;
  7359. }
  7360. inline typename expression_node<T>::node_type type() const
  7361. {
  7362. return expression_node<T>::e_vecvecineq;
  7363. }
  7364. std::size_t size() const
  7365. {
  7366. return vec_size_;
  7367. }
  7368. private:
  7369. vector_node<T>* vec0_node_ptr_;
  7370. vector_node<T>* vec1_node_ptr_;
  7371. std::size_t vec_size_;
  7372. };
  7373. template <typename T, typename Operation>
  7374. class eqineq_vecval_node : public binary_node <T>,
  7375. public vector_interface<T>
  7376. {
  7377. public:
  7378. typedef expression_node<T>* expression_ptr;
  7379. typedef vector_node<T>* vector_node_ptr;
  7380. eqineq_vecval_node(const operator_type& opr,
  7381. expression_ptr branch0,
  7382. expression_ptr branch1)
  7383. : binary_node<T>(opr,branch0,branch1),
  7384. vec_node_ptr_(0),
  7385. vec_size_ (0)
  7386. {
  7387. if (is_vector_node(binary_node<T>::branch_[0].first))
  7388. {
  7389. vec_node_ptr_ = static_cast<vector_node_ptr>(binary_node<T>::branch_[0].first);
  7390. }
  7391. else if (is_ivector_node(binary_node<T>::branch_[0].first))
  7392. {
  7393. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7394. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[0].first)))
  7395. {
  7396. vec_node_ptr_ = vi->vec();
  7397. }
  7398. }
  7399. if (vec_node_ptr_)
  7400. {
  7401. vec_size_ = vec_node_ptr_->ref().size();
  7402. }
  7403. }
  7404. inline T value() const
  7405. {
  7406. if (vec_node_ptr_)
  7407. {
  7408. binary_node<T>::branch_[0].first->value();
  7409. T v = binary_node<T>::branch_[1].first->value();
  7410. vector_holder<T>& vec_hldr = vec_node_ptr_->ref();
  7411. for (std::size_t i = 0; i < vec_size_; ++i)
  7412. {
  7413. if (std::equal_to<T>()(T(0),Operation::process(*vec_hldr[i],v)))
  7414. {
  7415. return T(0);
  7416. }
  7417. }
  7418. return T(1);
  7419. }
  7420. else
  7421. return std::numeric_limits<T>::quiet_NaN();
  7422. }
  7423. vector_node_ptr vec() const
  7424. {
  7425. return vec_node_ptr_;
  7426. }
  7427. vector_node_ptr vec()
  7428. {
  7429. return vec_node_ptr_;
  7430. }
  7431. inline typename expression_node<T>::node_type type() const
  7432. {
  7433. return expression_node<T>::e_vecvalineq;
  7434. }
  7435. std::size_t size() const
  7436. {
  7437. return vec_size_;
  7438. }
  7439. private:
  7440. vector_node<T>* vec_node_ptr_;
  7441. std::size_t vec_size_;
  7442. };
  7443. template <typename T, typename Operation>
  7444. class eqineq_valvec_node : public binary_node <T>,
  7445. public vector_interface<T>
  7446. {
  7447. public:
  7448. typedef expression_node<T>* expression_ptr;
  7449. typedef vector_node<T>* vector_node_ptr;
  7450. eqineq_valvec_node(const operator_type& opr,
  7451. expression_ptr branch0,
  7452. expression_ptr branch1)
  7453. : binary_node<T>(opr,branch0,branch1),
  7454. vec_node_ptr_(0),
  7455. vec_size_ (0)
  7456. {
  7457. if (is_vector_node(binary_node<T>::branch_[1].first))
  7458. {
  7459. vec_node_ptr_ = static_cast<vector_node_ptr>(binary_node<T>::branch_[1].first);
  7460. }
  7461. else if (is_ivector_node(binary_node<T>::branch_[1].first))
  7462. {
  7463. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7464. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[1].first)))
  7465. {
  7466. vec_node_ptr_ = vi->vec();
  7467. }
  7468. }
  7469. if (vec_node_ptr_)
  7470. {
  7471. vec_size_ = vec_node_ptr_->ref().size();
  7472. }
  7473. }
  7474. inline T value() const
  7475. {
  7476. if (vec_node_ptr_)
  7477. {
  7478. T v = binary_node<T>::branch_[0].first->value();
  7479. binary_node<T>::branch_[1].first->value();
  7480. vector_holder<T>& vec_hldr = vec_node_ptr_->ref();
  7481. for (std::size_t i = 0; i < vec_size_; ++i)
  7482. {
  7483. if (std::equal_to<T>()(T(0),Operation::process(v,*vec_hldr[i])))
  7484. {
  7485. return T(0);
  7486. }
  7487. }
  7488. return T(1);
  7489. }
  7490. else
  7491. return std::numeric_limits<T>::quiet_NaN();
  7492. }
  7493. vector_node_ptr vec() const
  7494. {
  7495. return vec_node_ptr_;
  7496. }
  7497. vector_node_ptr vec()
  7498. {
  7499. return vec_node_ptr_;
  7500. }
  7501. inline typename expression_node<T>::node_type type() const
  7502. {
  7503. return expression_node<T>::e_valvecineq;
  7504. }
  7505. std::size_t size() const
  7506. {
  7507. return vec_size_;
  7508. }
  7509. private:
  7510. vector_node<T>* vec_node_ptr_;
  7511. std::size_t vec_size_;
  7512. };
  7513. template <typename T, typename Operation>
  7514. class vecarith_vecvec_node : public binary_node <T>,
  7515. public vector_interface<T>
  7516. {
  7517. public:
  7518. typedef expression_node<T>* expression_ptr;
  7519. typedef vector_node<T>* vector_node_ptr;
  7520. typedef vector_holder<T>* vector_holder_ptr;
  7521. vecarith_vecvec_node(const operator_type& opr,
  7522. expression_ptr branch0,
  7523. expression_ptr branch1)
  7524. : binary_node<T>(opr,branch0,branch1),
  7525. vec0_node_ptr_(0),
  7526. vec1_node_ptr_(0),
  7527. vec_size_ (0),
  7528. data_ (0),
  7529. temp_ (0),
  7530. temp_vec_node_(0)
  7531. {
  7532. if (is_vector_node(binary_node<T>::branch_[0].first))
  7533. {
  7534. vec0_node_ptr_ = static_cast<vector_node_ptr>(binary_node<T>::branch_[0].first);
  7535. }
  7536. else if (is_ivector_node(binary_node<T>::branch_[0].first))
  7537. {
  7538. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7539. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[0].first)))
  7540. {
  7541. vec0_node_ptr_ = vi->vec();
  7542. }
  7543. }
  7544. if (is_vector_node(binary_node<T>::branch_[1].first))
  7545. {
  7546. vec1_node_ptr_ = static_cast<vector_node_ptr>(binary_node<T>::branch_[1].first);
  7547. }
  7548. else if (is_ivector_node(binary_node<T>::branch_[1].first))
  7549. {
  7550. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7551. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[1].first)))
  7552. {
  7553. vec1_node_ptr_ = vi->vec();
  7554. }
  7555. }
  7556. if (vec0_node_ptr_ && vec1_node_ptr_)
  7557. {
  7558. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  7559. vector_holder<T>& vec1 = vec1_node_ptr_->ref();
  7560. vec_size_ = std::min(vec0.size(),vec1.size());
  7561. data_ = new T[vec_size_];
  7562. temp_ = new vector_holder<T>(data_,vec_size_);
  7563. temp_vec_node_ = new vector_node<T> (temp_);
  7564. }
  7565. }
  7566. ~vecarith_vecvec_node()
  7567. {
  7568. delete[] data_;
  7569. delete temp_;
  7570. delete temp_vec_node_;
  7571. }
  7572. inline T value() const
  7573. {
  7574. if (vec0_node_ptr_ && vec1_node_ptr_)
  7575. {
  7576. binary_node<T>::branch_[0].first->value();
  7577. binary_node<T>::branch_[1].first->value();
  7578. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  7579. vector_holder<T>& vec1 = vec1_node_ptr_->ref();
  7580. vector_holder<T>& vec2 = *temp_;
  7581. for (std::size_t i = 0; i < vec_size_; ++i)
  7582. {
  7583. T& vec0_i = *vec0[i];
  7584. T& vec1_i = *vec1[i];
  7585. T& vec2_i = *vec2[i];
  7586. vec2_i = Operation::process(vec0_i,vec1_i);
  7587. }
  7588. return *vec2[0];
  7589. }
  7590. else
  7591. return std::numeric_limits<T>::quiet_NaN();
  7592. }
  7593. vector_node_ptr vec() const
  7594. {
  7595. return temp_vec_node_;
  7596. }
  7597. vector_node_ptr vec()
  7598. {
  7599. return temp_vec_node_;
  7600. }
  7601. inline typename expression_node<T>::node_type type() const
  7602. {
  7603. return expression_node<T>::e_vecvecarith;
  7604. }
  7605. std::size_t size() const
  7606. {
  7607. return vec_size_;
  7608. }
  7609. private:
  7610. vector_node_ptr vec0_node_ptr_;
  7611. vector_node_ptr vec1_node_ptr_;
  7612. std::size_t vec_size_;
  7613. T* data_;
  7614. vector_holder_ptr temp_;
  7615. vector_node_ptr temp_vec_node_;
  7616. };
  7617. template <typename T, typename Operation>
  7618. class vecarith_vecval_node : public binary_node <T>,
  7619. public vector_interface<T>
  7620. {
  7621. public:
  7622. typedef expression_node<T>* expression_ptr;
  7623. typedef vector_node<T>* vector_node_ptr;
  7624. typedef vector_holder<T>* vector_holder_ptr;
  7625. vecarith_vecval_node(const operator_type& opr,
  7626. expression_ptr branch0,
  7627. expression_ptr branch1)
  7628. : binary_node<T>(opr,branch0,branch1),
  7629. vec0_node_ptr_(0),
  7630. vec_size_ (0),
  7631. data_ (0),
  7632. temp_ (0),
  7633. temp_vec_node_(0)
  7634. {
  7635. if (is_vector_node(binary_node<T>::branch_[0].first))
  7636. {
  7637. vec0_node_ptr_ = static_cast<vector_node_ptr>(binary_node<T>::branch_[0].first);
  7638. }
  7639. else if (is_ivector_node(binary_node<T>::branch_[0].first))
  7640. {
  7641. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7642. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[0].first)))
  7643. {
  7644. vec0_node_ptr_ = vi->vec();
  7645. }
  7646. }
  7647. if (vec0_node_ptr_)
  7648. {
  7649. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  7650. vec_size_ = vec0.size();
  7651. data_ = new T[vec_size_];
  7652. temp_ = new vector_holder<T>(data_,vec_size_);
  7653. temp_vec_node_ = new vector_node<T> (temp_);
  7654. }
  7655. }
  7656. ~vecarith_vecval_node()
  7657. {
  7658. delete[] data_;
  7659. delete temp_;
  7660. delete temp_vec_node_;
  7661. }
  7662. inline T value() const
  7663. {
  7664. if (vec0_node_ptr_)
  7665. {
  7666. binary_node<T>::branch_[0].first->value();
  7667. const T v = binary_node<T>::branch_[1].first->value();
  7668. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  7669. vector_holder<T>& vec1 = *temp_;
  7670. for (std::size_t i = 0; i < vec_size_; ++i)
  7671. {
  7672. T& vec0_i = *vec0[i];
  7673. T& vec1_i = *vec1[i];
  7674. vec1_i = Operation::process(vec0_i,v);
  7675. }
  7676. return *vec1[0];
  7677. }
  7678. else
  7679. return std::numeric_limits<T>::quiet_NaN();
  7680. }
  7681. vector_node_ptr vec() const
  7682. {
  7683. return temp_vec_node_;
  7684. }
  7685. vector_node_ptr vec()
  7686. {
  7687. return temp_vec_node_;
  7688. }
  7689. inline typename expression_node<T>::node_type type() const
  7690. {
  7691. return expression_node<T>::e_vecvalarith;
  7692. }
  7693. std::size_t size() const
  7694. {
  7695. return vec_size_;
  7696. }
  7697. private:
  7698. vector_node_ptr vec0_node_ptr_;
  7699. std::size_t vec_size_;
  7700. T* data_;
  7701. vector_holder_ptr temp_;
  7702. vector_node_ptr temp_vec_node_;
  7703. };
  7704. template <typename T, typename Operation>
  7705. class vecarith_valvec_node : public binary_node <T>,
  7706. public vector_interface<T>
  7707. {
  7708. public:
  7709. typedef expression_node<T>* expression_ptr;
  7710. typedef vector_node<T>* vector_node_ptr;
  7711. typedef vector_holder<T>* vector_holder_ptr;
  7712. vecarith_valvec_node(const operator_type& opr,
  7713. expression_ptr branch0,
  7714. expression_ptr branch1)
  7715. : binary_node<T>(opr,branch0,branch1),
  7716. vec1_node_ptr_(0),
  7717. vec_size_ (0),
  7718. data_ (0),
  7719. temp_ (0),
  7720. temp_vec_node_(0)
  7721. {
  7722. if (is_vector_node(binary_node<T>::branch_[1].first))
  7723. {
  7724. vec1_node_ptr_ = static_cast<vector_node_ptr>(binary_node<T>::branch_[1].first);
  7725. }
  7726. else if (is_ivector_node(binary_node<T>::branch_[1].first))
  7727. {
  7728. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7729. if ((vi = dynamic_cast<vector_interface<T>*>(binary_node<T>::branch_[1].first)))
  7730. {
  7731. vec1_node_ptr_ = vi->vec();
  7732. }
  7733. }
  7734. if (vec1_node_ptr_)
  7735. {
  7736. vector_holder<T>& vec0 = vec1_node_ptr_->ref();
  7737. vec_size_ = vec0.size();
  7738. data_ = new T[vec_size_];
  7739. temp_ = new vector_holder<T>(data_,vec_size_);
  7740. temp_vec_node_ = new vector_node<T> (temp_);
  7741. }
  7742. }
  7743. ~vecarith_valvec_node()
  7744. {
  7745. delete[] data_;
  7746. delete temp_;
  7747. delete temp_vec_node_;
  7748. }
  7749. inline T value() const
  7750. {
  7751. if (vec1_node_ptr_)
  7752. {
  7753. const T v = binary_node<T>::branch_[0].first->value();
  7754. binary_node<T>::branch_[1].first->value();
  7755. vector_holder<T>& vec1 = vec1_node_ptr_->ref();
  7756. vector_holder<T>& vec2 = *temp_;
  7757. for (std::size_t i = 0; i < vec_size_; ++i)
  7758. {
  7759. T& vec1_i = *vec1[i];
  7760. T& vec2_i = *vec2[i];
  7761. vec2_i = Operation::process(v,vec1_i);
  7762. }
  7763. return *vec2[0];
  7764. }
  7765. else
  7766. return std::numeric_limits<T>::quiet_NaN();
  7767. }
  7768. vector_node_ptr vec() const
  7769. {
  7770. return temp_vec_node_;
  7771. }
  7772. vector_node_ptr vec()
  7773. {
  7774. return temp_vec_node_;
  7775. }
  7776. inline typename expression_node<T>::node_type type() const
  7777. {
  7778. return expression_node<T>::e_vecvalarith;
  7779. }
  7780. std::size_t size() const
  7781. {
  7782. return vec_size_;
  7783. }
  7784. private:
  7785. vector_node_ptr vec1_node_ptr_;
  7786. std::size_t vec_size_;
  7787. T* data_;
  7788. vector_holder_ptr temp_;
  7789. vector_node_ptr temp_vec_node_;
  7790. };
  7791. template <typename T, typename Operation>
  7792. class unary_vector_node : public unary_node <T>,
  7793. public vector_interface<T>
  7794. {
  7795. public:
  7796. typedef expression_node<T>* expression_ptr;
  7797. typedef vector_node<T>* vector_node_ptr;
  7798. typedef vector_holder<T>* vector_holder_ptr;
  7799. unary_vector_node(const operator_type& opr, expression_ptr branch0)
  7800. : unary_node<T>(opr,branch0),
  7801. vec0_node_ptr_(0),
  7802. vec_size_ (0),
  7803. data_ (0),
  7804. temp_ (0),
  7805. temp_vec_node_(0)
  7806. {
  7807. if (is_vector_node(unary_node<T>::branch_))
  7808. {
  7809. vec0_node_ptr_ = static_cast<vector_node_ptr>(unary_node<T>::branch_);
  7810. }
  7811. else if (is_ivector_node(unary_node<T>::branch_))
  7812. {
  7813. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  7814. if ((vi = dynamic_cast<vector_interface<T>*>(unary_node<T>::branch_)))
  7815. {
  7816. vec0_node_ptr_ = vi->vec();
  7817. }
  7818. }
  7819. if (vec0_node_ptr_)
  7820. {
  7821. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  7822. vec_size_ = vec0.size();
  7823. data_ = new T[vec_size_];
  7824. temp_ = new vector_holder<T>(data_,vec_size_);
  7825. temp_vec_node_ = new vector_node<T> (temp_);
  7826. }
  7827. }
  7828. ~unary_vector_node()
  7829. {
  7830. delete[] data_;
  7831. delete temp_;
  7832. delete temp_vec_node_;
  7833. }
  7834. inline T value() const
  7835. {
  7836. unary_node<T>::branch_->value();
  7837. if (vec0_node_ptr_)
  7838. {
  7839. vector_holder<T>& vec0 = vec0_node_ptr_->ref();
  7840. vector_holder<T>& vec1 = *temp_;
  7841. for (std::size_t i = 0; i < vec_size_; ++i)
  7842. {
  7843. T& vec0_i = *vec0[i];
  7844. T& vec1_i = *vec1[i];
  7845. vec1_i = Operation::process(vec0_i);
  7846. }
  7847. return *vec1[0];
  7848. }
  7849. else
  7850. return std::numeric_limits<T>::quiet_NaN();
  7851. }
  7852. vector_node_ptr vec() const
  7853. {
  7854. return temp_vec_node_;
  7855. }
  7856. vector_node_ptr vec()
  7857. {
  7858. return temp_vec_node_;
  7859. }
  7860. inline typename expression_node<T>::node_type type() const
  7861. {
  7862. return expression_node<T>::e_vecunaryop;
  7863. }
  7864. std::size_t size() const
  7865. {
  7866. return vec_size_;
  7867. }
  7868. private:
  7869. vector_node_ptr vec0_node_ptr_;
  7870. std::size_t vec_size_;
  7871. T* data_;
  7872. vector_holder_ptr temp_;
  7873. vector_node_ptr temp_vec_node_;
  7874. };
  7875. template <typename T>
  7876. class scand_node : public binary_node<T>
  7877. {
  7878. public:
  7879. typedef expression_node<T>* expression_ptr;
  7880. scand_node(const operator_type& opr,
  7881. expression_ptr branch0,
  7882. expression_ptr branch1)
  7883. : binary_node<T>(opr,branch0,branch1)
  7884. {}
  7885. inline T value() const
  7886. {
  7887. return (
  7888. std::not_equal_to<T>()
  7889. (T(0),binary_node<T>::branch_[0].first->value()) &&
  7890. std::not_equal_to<T>()
  7891. (T(0),binary_node<T>::branch_[1].first->value())
  7892. ) ? T(1) : T(0);
  7893. }
  7894. };
  7895. template <typename T>
  7896. class scor_node : public binary_node<T>
  7897. {
  7898. public:
  7899. typedef expression_node<T>* expression_ptr;
  7900. scor_node(const operator_type& opr,
  7901. expression_ptr branch0,
  7902. expression_ptr branch1)
  7903. : binary_node<T>(opr,branch0,branch1)
  7904. {}
  7905. inline T value() const
  7906. {
  7907. return (
  7908. std::not_equal_to<T>()
  7909. (T(0),binary_node<T>::branch_[0].first->value()) ||
  7910. std::not_equal_to<T>()
  7911. (T(0),binary_node<T>::branch_[1].first->value())
  7912. ) ? T(1) : T(0);
  7913. }
  7914. };
  7915. template <typename T, typename IFunction, std::size_t N>
  7916. class function_N_node : public expression_node<T>
  7917. {
  7918. public:
  7919. // Function of N paramters.
  7920. typedef expression_node<T>* expression_ptr;
  7921. typedef std::pair<expression_ptr,bool> branch_t;
  7922. typedef IFunction ifunction;
  7923. function_N_node(ifunction* func)
  7924. : function_((N == func->param_count) ? func : reinterpret_cast<ifunction*>(0)),
  7925. parameter_count_(func->param_count)
  7926. {}
  7927. ~function_N_node()
  7928. {
  7929. cleanup_branches::execute<T,N>(branch_);
  7930. }
  7931. template <std::size_t NumBranches>
  7932. bool init_branches(expression_ptr (&b)[NumBranches])
  7933. {
  7934. // Needed for incompetent and broken msvc compiler versions
  7935. #ifdef _MSC_VER
  7936. #pragma warning(push)
  7937. #pragma warning(disable: 4127)
  7938. #endif
  7939. if (N != NumBranches)
  7940. return false;
  7941. else
  7942. {
  7943. for (std::size_t i = 0; i < NumBranches; ++i)
  7944. {
  7945. if (b[i])
  7946. branch_[i] = std::make_pair(b[i],branch_deletable(b[i]));
  7947. else
  7948. return false;
  7949. }
  7950. return true;
  7951. }
  7952. #ifdef _MSC_VER
  7953. #pragma warning(pop)
  7954. #endif
  7955. }
  7956. inline bool operator <(const function_N_node<T,IFunction,N>& fn) const
  7957. {
  7958. return this < (&fn);
  7959. }
  7960. inline T value() const
  7961. {
  7962. // Needed for incompetent and broken msvc compiler versions
  7963. #ifdef _MSC_VER
  7964. #pragma warning(push)
  7965. #pragma warning(disable: 4127)
  7966. #endif
  7967. if ((0 == function_) || (0 == N))
  7968. return std::numeric_limits<T>::quiet_NaN();
  7969. else
  7970. {
  7971. T v[N];
  7972. evaluate_branches<T,N>::execute(v,branch_);
  7973. return invoke<T,N>::execute(*function_,v);
  7974. }
  7975. #ifdef _MSC_VER
  7976. #pragma warning(pop)
  7977. #endif
  7978. }
  7979. template <typename T_, std::size_t BranchCount>
  7980. struct evaluate_branches
  7981. {
  7982. static inline void execute(T_ (&v)[BranchCount], const branch_t (&b)[BranchCount])
  7983. {
  7984. for (std::size_t i = 0; i < BranchCount; ++i)
  7985. {
  7986. v[i] = b[i].first->value();
  7987. }
  7988. }
  7989. };
  7990. template <typename T_>
  7991. struct evaluate_branches <T_,5>
  7992. {
  7993. static inline void execute(T_ (&v)[5], const branch_t (&b)[5])
  7994. {
  7995. v[0] = b[0].first->value();
  7996. v[1] = b[1].first->value();
  7997. v[2] = b[2].first->value();
  7998. v[3] = b[3].first->value();
  7999. v[4] = b[4].first->value();
  8000. }
  8001. };
  8002. template <typename T_>
  8003. struct evaluate_branches <T_,4>
  8004. {
  8005. static inline void execute(T_ (&v)[4], const branch_t (&b)[4])
  8006. {
  8007. v[0] = b[0].first->value();
  8008. v[1] = b[1].first->value();
  8009. v[2] = b[2].first->value();
  8010. v[3] = b[3].first->value();
  8011. }
  8012. };
  8013. template <typename T_>
  8014. struct evaluate_branches <T_,3>
  8015. {
  8016. static inline void execute(T_ (&v)[3], const branch_t (&b)[3])
  8017. {
  8018. v[0] = b[0].first->value();
  8019. v[1] = b[1].first->value();
  8020. v[2] = b[2].first->value();
  8021. }
  8022. };
  8023. template <typename T_>
  8024. struct evaluate_branches <T_,2>
  8025. {
  8026. static inline void execute(T_ (&v)[2], const branch_t (&b)[2])
  8027. {
  8028. v[0] = b[0].first->value();
  8029. v[1] = b[1].first->value();
  8030. }
  8031. };
  8032. template <typename T_>
  8033. struct evaluate_branches <T_,1>
  8034. {
  8035. static inline void execute(T_ (&v)[1], const branch_t (&b)[1])
  8036. {
  8037. v[0] = b[0].first->value();
  8038. }
  8039. };
  8040. template <typename T_, std::size_t ParamCount>
  8041. struct invoke { static inline T execute(ifunction&, branch_t (&)[ParamCount]) { return std::numeric_limits<T_>::quiet_NaN(); } };
  8042. template <typename T_>
  8043. struct invoke<T_,20>
  8044. {
  8045. static inline T_ execute(ifunction& f, T_ (&v)[20])
  8046. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14],v[15],v[16],v[17],v[18],v[19]); }
  8047. };
  8048. template <typename T_>
  8049. struct invoke<T_,19>
  8050. {
  8051. static inline T_ execute(ifunction& f, T_ (&v)[19])
  8052. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14],v[15],v[16],v[17],v[18]); }
  8053. };
  8054. template <typename T_>
  8055. struct invoke<T_,18>
  8056. {
  8057. static inline T_ execute(ifunction& f, T_ (&v)[18])
  8058. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14],v[15],v[16],v[17]); }
  8059. };
  8060. template <typename T_>
  8061. struct invoke<T_,17>
  8062. {
  8063. static inline T_ execute(ifunction& f, T_ (&v)[17])
  8064. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14],v[15],v[16]); }
  8065. };
  8066. template <typename T_>
  8067. struct invoke<T_,16>
  8068. {
  8069. static inline T_ execute(ifunction& f, T_ (&v)[16])
  8070. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14],v[15]); }
  8071. };
  8072. template <typename T_>
  8073. struct invoke<T_,15>
  8074. {
  8075. static inline T_ execute(ifunction& f, T_ (&v)[15])
  8076. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14]); }
  8077. };
  8078. template <typename T_>
  8079. struct invoke<T_,14>
  8080. {
  8081. static inline T_ execute(ifunction& f, T_ (&v)[14])
  8082. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13]); }
  8083. };
  8084. template <typename T_>
  8085. struct invoke<T_,13>
  8086. {
  8087. static inline T_ execute(ifunction& f, T_ (&v)[13])
  8088. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12]); }
  8089. };
  8090. template <typename T_>
  8091. struct invoke<T_,12>
  8092. {
  8093. static inline T_ execute(ifunction& f, T_ (&v)[12])
  8094. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11]); }
  8095. };
  8096. template <typename T_>
  8097. struct invoke<T_,11>
  8098. {
  8099. static inline T_ execute(ifunction& f, T_ (&v)[11])
  8100. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10]); }
  8101. };
  8102. template <typename T_>
  8103. struct invoke<T_,10>
  8104. {
  8105. static inline T_ execute(ifunction& f, T_ (&v)[10])
  8106. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9]); }
  8107. };
  8108. template <typename T_>
  8109. struct invoke<T_,9>
  8110. {
  8111. static inline T_ execute(ifunction& f, T_ (&v)[9])
  8112. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8]); }
  8113. };
  8114. template <typename T_>
  8115. struct invoke<T_,8>
  8116. {
  8117. static inline T_ execute(ifunction& f, T_ (&v)[8])
  8118. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]); }
  8119. };
  8120. template <typename T_>
  8121. struct invoke<T_,7>
  8122. {
  8123. static inline T_ execute(ifunction& f, T_ (&v)[7])
  8124. { return f(v[0],v[1],v[2],v[3],v[4],v[5],v[6]); }
  8125. };
  8126. template <typename T_>
  8127. struct invoke<T_,6>
  8128. {
  8129. static inline T_ execute(ifunction& f, T_ (&v)[6])
  8130. { return f(v[0],v[1],v[2],v[3],v[4],v[5]); }
  8131. };
  8132. template <typename T_>
  8133. struct invoke<T_,5>
  8134. {
  8135. static inline T_ execute(ifunction& f, T_ (&v)[5])
  8136. { return f(v[0],v[1],v[2],v[3],v[4]); }
  8137. };
  8138. template <typename T_>
  8139. struct invoke<T_,4>
  8140. {
  8141. static inline T_ execute(ifunction& f, T_ (&v)[4])
  8142. { return f(v[0],v[1],v[2],v[3]); }
  8143. };
  8144. template <typename T_>
  8145. struct invoke<T_,3>
  8146. {
  8147. static inline T_ execute(ifunction& f, T_ (&v)[3])
  8148. { return f(v[0],v[1],v[2]); }
  8149. };
  8150. template <typename T_>
  8151. struct invoke<T_,2>
  8152. {
  8153. static inline T_ execute(ifunction& f, T_ (&v)[2])
  8154. { return f(v[0],v[1]); }
  8155. };
  8156. template <typename T_>
  8157. struct invoke<T_,1>
  8158. {
  8159. static inline T_ execute(ifunction& f, T_ (&v)[1])
  8160. { return f(v[0]); }
  8161. };
  8162. inline typename expression_node<T>::node_type type() const
  8163. {
  8164. return expression_node<T>::e_function;
  8165. }
  8166. private:
  8167. ifunction* function_;
  8168. std::size_t parameter_count_;
  8169. branch_t branch_[N];
  8170. };
  8171. template <typename T, typename IFunction>
  8172. class function_N_node<T,IFunction,0> : public expression_node<T>
  8173. {
  8174. public:
  8175. typedef expression_node<T>* expression_ptr;
  8176. typedef IFunction ifunction;
  8177. function_N_node(ifunction* func)
  8178. : function_((0 == func->param_count) ? func : reinterpret_cast<ifunction*>(0))
  8179. {}
  8180. inline bool operator <(const function_N_node<T,IFunction,0>& fn) const
  8181. {
  8182. return this < (&fn);
  8183. }
  8184. inline T value() const
  8185. {
  8186. if (function_)
  8187. return (*function_)();
  8188. else
  8189. return std::numeric_limits<T>::quiet_NaN();
  8190. }
  8191. inline typename expression_node<T>::node_type type() const
  8192. {
  8193. return expression_node<T>::e_function;
  8194. }
  8195. private:
  8196. ifunction* function_;
  8197. };
  8198. template <typename T, typename VarArgFunction>
  8199. class vararg_function_node : public expression_node<T>
  8200. {
  8201. public:
  8202. typedef expression_node<T>* expression_ptr;
  8203. vararg_function_node(VarArgFunction* func,
  8204. const std::vector<expression_ptr>& arg_list)
  8205. : function_(func),
  8206. arg_list_(arg_list)
  8207. {
  8208. value_list_.resize(arg_list.size(),std::numeric_limits<T>::quiet_NaN());
  8209. }
  8210. ~vararg_function_node()
  8211. {
  8212. for (std::size_t i = 0; i < arg_list_.size(); ++i)
  8213. {
  8214. if (arg_list_[i] && !details::is_variable_node(arg_list_[i]))
  8215. {
  8216. delete arg_list_[i];
  8217. arg_list_[i] = 0;
  8218. }
  8219. }
  8220. }
  8221. inline bool operator <(const vararg_function_node<T,VarArgFunction>& fn) const
  8222. {
  8223. return this < (&fn);
  8224. }
  8225. inline T value() const
  8226. {
  8227. if (function_)
  8228. {
  8229. populate_value_list();
  8230. return (*function_)(value_list_);
  8231. }
  8232. else
  8233. return std::numeric_limits<T>::quiet_NaN();
  8234. }
  8235. inline typename expression_node<T>::node_type type() const
  8236. {
  8237. return expression_node<T>::e_vafunction;
  8238. }
  8239. private:
  8240. inline void populate_value_list() const
  8241. {
  8242. for (std::size_t i = 0; i < arg_list_.size(); ++i)
  8243. {
  8244. value_list_[i] = arg_list_[i]->value();
  8245. }
  8246. }
  8247. VarArgFunction* function_;
  8248. std::vector<expression_ptr> arg_list_;
  8249. mutable std::vector<T> value_list_;
  8250. };
  8251. template <typename T, typename GenericFunction>
  8252. class generic_function_node : public expression_node<T>
  8253. {
  8254. public:
  8255. typedef type_store<T> type_store_t;
  8256. typedef expression_node<T>* expression_ptr;
  8257. typedef variable_node<T> variable_node_t;
  8258. typedef vector_elem_node<T> vector_elem_node_t;
  8259. typedef vector_node<T> vector_node_t;
  8260. typedef variable_node_t* variable_node_ptr_t;
  8261. typedef vector_elem_node_t* vector_elem_node_ptr_t;
  8262. typedef vector_node_t* vector_node_ptr_t;
  8263. typedef range_interface<T> range_interface_t;
  8264. typedef range_data_type<T> range_data_type_t;
  8265. typedef range_pack<T> range_t;
  8266. typedef std::pair<expression_ptr,bool> branch_t;
  8267. typedef std::pair<void*,std::size_t> void_t;
  8268. typedef std::vector<T> tmp_vs_t;
  8269. typedef std::vector<type_store_t> typestore_list_t;
  8270. typedef std::vector<range_data_type_t> range_list_t;
  8271. generic_function_node(const std::vector<expression_ptr>& arg_list,
  8272. GenericFunction* func = (GenericFunction*)(0))
  8273. : function_(func),
  8274. arg_list_(arg_list)
  8275. {}
  8276. ~generic_function_node()
  8277. {
  8278. cleanup_branches::execute(branch_);
  8279. }
  8280. virtual bool init_branches()
  8281. {
  8282. expr_as_vec1_store_.resize(arg_list_.size(),T(0) );
  8283. typestore_list_ .resize(arg_list_.size(),type_store_t() );
  8284. range_list_ .resize(arg_list_.size(),range_data_type_t());
  8285. branch_ .resize(arg_list_.size(),branch_t((expression_ptr)0,false));
  8286. for (std::size_t i = 0; i < arg_list_.size(); ++i)
  8287. {
  8288. type_store_t& ts = typestore_list_[i];
  8289. if (0 == arg_list_[i])
  8290. return false;
  8291. else if (is_ivector_node(arg_list_[i]))
  8292. {
  8293. vector_interface<T>* vi = reinterpret_cast<vector_interface<T>*>(0);
  8294. if (0 == (vi = dynamic_cast<vector_interface<T>*>(arg_list_[i])))
  8295. return false;
  8296. ts.size = vi->size();
  8297. ts.data = vi->vec()->ref()[0];
  8298. ts.type = type_store_t::e_vector;
  8299. }
  8300. else if (is_generally_string_node(arg_list_[i]))
  8301. {
  8302. string_base_node<T>* sbn = reinterpret_cast<string_base_node<T>*>(0);
  8303. if (0 == (sbn = dynamic_cast<string_base_node<T>*>(arg_list_[i])))
  8304. return false;
  8305. ts.size = sbn->size();
  8306. ts.data = reinterpret_cast<void*>(const_cast<char*>(sbn->base()));
  8307. ts.type = type_store_t::e_string;
  8308. range_list_[i].data = ts.data;
  8309. range_list_[i].size = ts.size;
  8310. range_list_[i].type_size = sizeof(char);
  8311. range_list_[i].str_node = sbn;
  8312. range_interface_t* ri = reinterpret_cast<range_interface_t*>(0);
  8313. if (0 == (ri = dynamic_cast<range_interface_t*>(arg_list_[i])))
  8314. return false;
  8315. range_t& rp = ri->range_ref();
  8316. if (
  8317. rp.const_range() &&
  8318. is_const_string_range_node(arg_list_[i])
  8319. )
  8320. {
  8321. ts.size = rp.const_size();
  8322. ts.data = static_cast<char*>(ts.data) + rp.n0_c.second;
  8323. range_list_[i].range = reinterpret_cast<range_t*>(0);
  8324. }
  8325. else
  8326. range_list_[i].range = &(ri->range_ref());
  8327. }
  8328. else if (is_variable_node(arg_list_[i]))
  8329. {
  8330. variable_node_ptr_t var = variable_node_ptr_t(0);
  8331. if (0 == (var = dynamic_cast<variable_node_ptr_t>(arg_list_[i])))
  8332. return false;
  8333. ts.size = 1;
  8334. ts.data = &var->ref();
  8335. ts.type = type_store_t::e_scalar;
  8336. }
  8337. else if (is_vector_elem_node(arg_list_[i]))
  8338. {
  8339. vector_elem_node_ptr_t var = vector_elem_node_ptr_t(0);
  8340. if (0 == (var = dynamic_cast<vector_elem_node_ptr_t>(arg_list_[i])))
  8341. return false;
  8342. ts.size = 1;
  8343. ts.data = reinterpret_cast<void*>(&var->ref());
  8344. ts.type = type_store_t::e_scalar;
  8345. }
  8346. else
  8347. {
  8348. ts.size = 1;
  8349. ts.data = reinterpret_cast<void*>(&expr_as_vec1_store_[i]);
  8350. ts.type = type_store_t::e_scalar;
  8351. }
  8352. branch_[i] = std::make_pair(arg_list_[i],branch_deletable(arg_list_[i]));
  8353. }
  8354. return true;
  8355. }
  8356. inline bool operator <(const generic_function_node<T,GenericFunction>& fn) const
  8357. {
  8358. return this < (&fn);
  8359. }
  8360. inline T value() const
  8361. {
  8362. if (function_)
  8363. {
  8364. if (populate_value_list())
  8365. {
  8366. typedef typename GenericFunction::parameter_list_t parameter_list_t;
  8367. return (*function_)(parameter_list_t(typestore_list_));
  8368. }
  8369. }
  8370. return std::numeric_limits<T>::quiet_NaN();
  8371. }
  8372. inline typename expression_node<T>::node_type type() const
  8373. {
  8374. return expression_node<T>::e_genfunction;
  8375. }
  8376. protected:
  8377. inline virtual bool populate_value_list() const
  8378. {
  8379. for (std::size_t i = 0; i < branch_.size(); ++i)
  8380. {
  8381. expr_as_vec1_store_[i] = branch_[i].first->value();
  8382. }
  8383. for (std::size_t i = 0; i < branch_.size(); ++i)
  8384. {
  8385. range_data_type_t& rdt = range_list_[i];
  8386. if (rdt.range)
  8387. {
  8388. range_t& rp = (*rdt.range);
  8389. std::size_t r0 = 0;
  8390. std::size_t r1 = 0;
  8391. if (rp(r0,r1,rdt.size))
  8392. {
  8393. type_store_t& ts = typestore_list_[i];
  8394. ts.size = rp.cache_size();
  8395. if (ts.type == type_store_t::e_string)
  8396. ts.data = const_cast<char*>(rdt.str_node->base()) + rp.cache.first;
  8397. else
  8398. ts.data = static_cast<char*>(rdt.data) + (rp.cache.first * rdt.type_size);
  8399. }
  8400. else
  8401. return false;
  8402. }
  8403. }
  8404. return true;
  8405. }
  8406. GenericFunction* function_;
  8407. mutable typestore_list_t typestore_list_;
  8408. private:
  8409. std::vector<expression_ptr> arg_list_;
  8410. std::vector<branch_t> branch_;
  8411. mutable tmp_vs_t expr_as_vec1_store_;
  8412. mutable range_list_t range_list_;
  8413. };
  8414. template <typename T, typename StringFunction>
  8415. class string_function_node : public generic_function_node<T,StringFunction>,
  8416. public string_base_node<T>,
  8417. public range_interface <T>
  8418. {
  8419. public:
  8420. typedef generic_function_node<T,StringFunction> gen_function_t;
  8421. typedef range_pack<T> range_t;
  8422. string_function_node(StringFunction* func,
  8423. const std::vector<typename gen_function_t::expression_ptr>& arg_list)
  8424. : gen_function_t(arg_list,func)
  8425. {
  8426. range_.n0_c = std::make_pair<bool,std::size_t>(true,0);
  8427. range_.n1_c = std::make_pair<bool,std::size_t>(true,0);
  8428. range_.cache.first = range_.n0_c.second;
  8429. range_.cache.second = range_.n1_c.second;
  8430. }
  8431. inline bool operator <(const string_function_node<T,StringFunction>& fn) const
  8432. {
  8433. return this < (&fn);
  8434. }
  8435. inline T value() const
  8436. {
  8437. T result = std::numeric_limits<T>::quiet_NaN();
  8438. if (gen_function_t::function_)
  8439. {
  8440. if (gen_function_t::populate_value_list())
  8441. {
  8442. typedef typename StringFunction::parameter_list_t parameter_list_t;
  8443. result = (*gen_function_t::function_)(ret_string_,
  8444. parameter_list_t(gen_function_t::typestore_list_));
  8445. range_.n1_c.second = ret_string_.size() - 1;
  8446. range_.cache.second = range_.n1_c.second;
  8447. return result;
  8448. }
  8449. }
  8450. return result;
  8451. }
  8452. inline typename expression_node<T>::node_type type() const
  8453. {
  8454. return expression_node<T>::e_strfunction;
  8455. }
  8456. std::string str() const
  8457. {
  8458. return ret_string_;
  8459. }
  8460. const char* base() const
  8461. {
  8462. return ret_string_.data();
  8463. }
  8464. std::size_t size() const
  8465. {
  8466. return ret_string_.size();
  8467. }
  8468. range_t& range_ref()
  8469. {
  8470. return range_;
  8471. }
  8472. const range_t& range_ref() const
  8473. {
  8474. return range_;
  8475. }
  8476. protected:
  8477. mutable range_t range_;
  8478. mutable std::string ret_string_;
  8479. };
  8480. template <typename T, typename GenericFunction>
  8481. class multimode_genfunction_node : public generic_function_node<T,GenericFunction>
  8482. {
  8483. public:
  8484. typedef generic_function_node<T,GenericFunction> gen_function_t;
  8485. typedef range_pack<T> range_t;
  8486. multimode_genfunction_node(GenericFunction* func,
  8487. const std::size_t& param_seq_index,
  8488. const std::vector<typename gen_function_t::expression_ptr>& arg_list)
  8489. : gen_function_t(arg_list,func),
  8490. param_seq_index_(param_seq_index)
  8491. {}
  8492. inline T value() const
  8493. {
  8494. T result = std::numeric_limits<T>::quiet_NaN();
  8495. if (gen_function_t::function_)
  8496. {
  8497. if (gen_function_t::populate_value_list())
  8498. {
  8499. typedef typename GenericFunction::parameter_list_t parameter_list_t;
  8500. return (*gen_function_t::function_)(param_seq_index_,
  8501. parameter_list_t(gen_function_t::typestore_list_));
  8502. }
  8503. }
  8504. return result;
  8505. }
  8506. inline typename expression_node<T>::node_type type() const
  8507. {
  8508. return expression_node<T>::e_genfunction;
  8509. }
  8510. private:
  8511. std::size_t param_seq_index_;
  8512. };
  8513. template <typename T, typename StringFunction>
  8514. class multimode_strfunction_node : public string_function_node<T,StringFunction>
  8515. {
  8516. public:
  8517. typedef string_function_node<T,StringFunction> str_function_t;
  8518. typedef range_pack<T> range_t;
  8519. multimode_strfunction_node(StringFunction* func,
  8520. const std::size_t& param_seq_index,
  8521. const std::vector<typename str_function_t::expression_ptr>& arg_list)
  8522. : str_function_t(func,arg_list),
  8523. param_seq_index_(param_seq_index)
  8524. {}
  8525. inline T value() const
  8526. {
  8527. T result = std::numeric_limits<T>::quiet_NaN();
  8528. if (str_function_t::function_)
  8529. {
  8530. if (str_function_t::populate_value_list())
  8531. {
  8532. typedef typename StringFunction::parameter_list_t parameter_list_t;
  8533. result = (*str_function_t::function_)(param_seq_index_,
  8534. str_function_t::ret_string_,
  8535. parameter_list_t(str_function_t::typestore_list_));
  8536. str_function_t::range_.n1_c.second = str_function_t::ret_string_.size() - 1;
  8537. str_function_t::range_.cache.second = str_function_t::range_.n1_c.second;
  8538. return result;
  8539. }
  8540. }
  8541. return result;
  8542. }
  8543. inline typename expression_node<T>::node_type type() const
  8544. {
  8545. return expression_node<T>::e_strfunction;
  8546. }
  8547. private:
  8548. std::size_t param_seq_index_;
  8549. };
  8550. class return_exception
  8551. {};
  8552. template <typename T>
  8553. class null_igenfunc
  8554. {
  8555. public:
  8556. typedef type_store<T> generic_type;
  8557. typedef typename generic_type::parameter_list parameter_list_t;
  8558. inline virtual T operator()(parameter_list_t)
  8559. {
  8560. return std::numeric_limits<T>::quiet_NaN();
  8561. }
  8562. };
  8563. template <typename T>
  8564. class return_node : public generic_function_node<T,null_igenfunc<T> >
  8565. {
  8566. public:
  8567. typedef null_igenfunc<T> igeneric_function_t;
  8568. typedef igeneric_function_t* igeneric_function_ptr;
  8569. typedef generic_function_node<T,igeneric_function_t> gen_function_t;
  8570. typedef results_context<T> results_context_t;
  8571. return_node(const std::vector<typename gen_function_t::expression_ptr>& arg_list,
  8572. results_context_t& rc)
  8573. : gen_function_t (arg_list),
  8574. results_context_(&rc)
  8575. {}
  8576. inline T value() const
  8577. {
  8578. if (
  8579. (0 != results_context_) &&
  8580. gen_function_t::populate_value_list()
  8581. )
  8582. {
  8583. typedef typename type_store<T>::parameter_list parameter_list_t;
  8584. results_context_->
  8585. assign(parameter_list_t(gen_function_t::typestore_list_));
  8586. throw return_exception();
  8587. }
  8588. return std::numeric_limits<T>::quiet_NaN();
  8589. }
  8590. inline typename expression_node<T>::node_type type() const
  8591. {
  8592. return expression_node<T>::e_return;
  8593. }
  8594. private:
  8595. results_context_t* results_context_;
  8596. };
  8597. template <typename T>
  8598. class return_envelope_node : public expression_node<T>
  8599. {
  8600. public:
  8601. typedef expression_node<T>* expression_ptr;
  8602. typedef results_context<T> results_context_t;
  8603. return_envelope_node(expression_ptr body, results_context_t& rc)
  8604. : results_context_(&rc),
  8605. return_invoked_ (false),
  8606. body_ (body),
  8607. body_deletable_ (branch_deletable(body_))
  8608. {}
  8609. ~return_envelope_node()
  8610. {
  8611. if (body_ && body_deletable_)
  8612. {
  8613. delete body_;
  8614. }
  8615. }
  8616. inline T value() const
  8617. {
  8618. try
  8619. {
  8620. return_invoked_ = false;
  8621. results_context_->clear();
  8622. return body_->value();
  8623. }
  8624. catch(const return_exception&)
  8625. {
  8626. return_invoked_ = true;
  8627. return std::numeric_limits<T>::quiet_NaN();
  8628. }
  8629. }
  8630. inline typename expression_node<T>::node_type type() const
  8631. {
  8632. return expression_node<T>::e_retenv;
  8633. }
  8634. inline bool* retinvk_ptr()
  8635. {
  8636. return &return_invoked_;
  8637. }
  8638. private:
  8639. results_context_t* results_context_;
  8640. mutable bool return_invoked_;
  8641. expression_ptr body_;
  8642. bool body_deletable_;
  8643. };
  8644. #define exprtk_define_unary_op(OpName) \
  8645. template <typename T> \
  8646. struct OpName##_op \
  8647. { \
  8648. typedef typename functor_t<T>::Type Type; \
  8649. \
  8650. static inline T process(Type v) \
  8651. { \
  8652. return numeric:: OpName (v); \
  8653. } \
  8654. \
  8655. static inline typename expression_node<T>::node_type type() \
  8656. { \
  8657. return expression_node<T>::e_##OpName; \
  8658. } \
  8659. \
  8660. static inline details::operator_type operation() \
  8661. { \
  8662. return details::e_##OpName; \
  8663. } \
  8664. }; \
  8665. exprtk_define_unary_op(abs )
  8666. exprtk_define_unary_op(acos )
  8667. exprtk_define_unary_op(acosh)
  8668. exprtk_define_unary_op(asin )
  8669. exprtk_define_unary_op(asinh)
  8670. exprtk_define_unary_op(atan )
  8671. exprtk_define_unary_op(atanh)
  8672. exprtk_define_unary_op(ceil )
  8673. exprtk_define_unary_op(cos )
  8674. exprtk_define_unary_op(cosh )
  8675. exprtk_define_unary_op(cot )
  8676. exprtk_define_unary_op(csc )
  8677. exprtk_define_unary_op(d2g )
  8678. exprtk_define_unary_op(d2r )
  8679. exprtk_define_unary_op(erf )
  8680. exprtk_define_unary_op(erfc )
  8681. exprtk_define_unary_op(exp )
  8682. exprtk_define_unary_op(expm1)
  8683. exprtk_define_unary_op(floor)
  8684. exprtk_define_unary_op(frac )
  8685. exprtk_define_unary_op(g2d )
  8686. exprtk_define_unary_op(log )
  8687. exprtk_define_unary_op(log10)
  8688. exprtk_define_unary_op(log2 )
  8689. exprtk_define_unary_op(log1p)
  8690. exprtk_define_unary_op(ncdf )
  8691. exprtk_define_unary_op(neg )
  8692. exprtk_define_unary_op(notl )
  8693. exprtk_define_unary_op(pos )
  8694. exprtk_define_unary_op(r2d )
  8695. exprtk_define_unary_op(round)
  8696. exprtk_define_unary_op(sec )
  8697. exprtk_define_unary_op(sgn )
  8698. exprtk_define_unary_op(sin )
  8699. exprtk_define_unary_op(sinc )
  8700. exprtk_define_unary_op(sinh )
  8701. exprtk_define_unary_op(sqrt )
  8702. exprtk_define_unary_op(tan )
  8703. exprtk_define_unary_op(tanh )
  8704. exprtk_define_unary_op(trunc)
  8705. #undef exprtk_define_unary_op
  8706. template <typename T>
  8707. struct opr_base
  8708. {
  8709. typedef typename details::functor_t<T>::Type Type;
  8710. typedef typename details::functor_t<T> functor_t;
  8711. typedef typename functor_t::qfunc_t quaternary_functor_t;
  8712. typedef typename functor_t::tfunc_t trinary_functor_t;
  8713. typedef typename functor_t::bfunc_t binary_functor_t;
  8714. typedef typename functor_t::ufunc_t unary_functor_t;
  8715. };
  8716. template <typename T>
  8717. struct add_op : public opr_base<T>
  8718. {
  8719. typedef typename opr_base<T>::Type Type;
  8720. static inline T process(Type t1, Type t2) { return t1 + t2; }
  8721. static inline T process(Type t1, Type t2, Type t3) { return t1 + t2 + t3; }
  8722. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_add; }
  8723. static inline details::operator_type operation() { return details::e_add; }
  8724. };
  8725. template <typename T>
  8726. struct mul_op : public opr_base<T>
  8727. {
  8728. typedef typename opr_base<T>::Type Type;
  8729. static inline T process(Type t1, Type t2) { return t1 * t2; }
  8730. static inline T process(Type t1, Type t2, Type t3) { return t1 * t2 * t3; }
  8731. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_mul; }
  8732. static inline details::operator_type operation() { return details::e_mul; }
  8733. };
  8734. template <typename T>
  8735. struct sub_op : public opr_base<T>
  8736. {
  8737. typedef typename opr_base<T>::Type Type;
  8738. static inline T process(Type t1, Type t2) { return t1 - t2; }
  8739. static inline T process(Type t1, Type t2, Type t3) { return t1 - t2 - t3; }
  8740. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_sub; }
  8741. static inline details::operator_type operation() { return details::e_sub; }
  8742. };
  8743. template <typename T>
  8744. struct div_op : public opr_base<T>
  8745. {
  8746. typedef typename opr_base<T>::Type Type;
  8747. static inline T process(Type t1, Type t2) { return t1 / t2; }
  8748. static inline T process(Type t1, Type t2, Type t3) { return t1 / t2 / t3; }
  8749. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_div; }
  8750. static inline details::operator_type operation() { return details::e_div; }
  8751. };
  8752. template <typename T>
  8753. struct mod_op : public opr_base<T>
  8754. {
  8755. typedef typename opr_base<T>::Type Type;
  8756. static inline T process(Type t1, Type t2) { return numeric::modulus<T>(t1,t2); }
  8757. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_mod; }
  8758. static inline details::operator_type operation() { return details::e_mod; }
  8759. };
  8760. template <typename T>
  8761. struct pow_op : public opr_base<T>
  8762. {
  8763. typedef typename opr_base<T>::Type Type;
  8764. static inline T process(Type t1, Type t2) { return numeric::pow<T>(t1,t2); }
  8765. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_pow; }
  8766. static inline details::operator_type operation() { return details::e_pow; }
  8767. };
  8768. template <typename T>
  8769. struct lt_op : public opr_base<T>
  8770. {
  8771. typedef typename opr_base<T>::Type Type;
  8772. static inline T process(Type t1, Type t2) { return ((t1 < t2) ? T(1) : T(0)); }
  8773. static inline T process(const std::string& t1, const std::string& t2) { return ((t1 < t2) ? T(1) : T(0)); }
  8774. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_lt; }
  8775. static inline details::operator_type operation() { return details::e_lt; }
  8776. };
  8777. template <typename T>
  8778. struct lte_op : public opr_base<T>
  8779. {
  8780. typedef typename opr_base<T>::Type Type;
  8781. static inline T process(Type t1, Type t2) { return ((t1 <= t2) ? T(1) : T(0)); }
  8782. static inline T process(const std::string& t1, const std::string& t2) { return ((t1 <= t2) ? T(1) : T(0)); }
  8783. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_lte; }
  8784. static inline details::operator_type operation() { return details::e_lte; }
  8785. };
  8786. template <typename T>
  8787. struct gt_op : public opr_base<T>
  8788. {
  8789. typedef typename opr_base<T>::Type Type;
  8790. static inline T process(Type t1, Type t2) { return ((t1 > t2) ? T(1) : T(0)); }
  8791. static inline T process(const std::string& t1, const std::string& t2) { return ((t1 > t2) ? T(1) : T(0)); }
  8792. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_gt; }
  8793. static inline details::operator_type operation() { return details::e_gt; }
  8794. };
  8795. template <typename T>
  8796. struct gte_op : public opr_base<T>
  8797. {
  8798. typedef typename opr_base<T>::Type Type;
  8799. static inline T process(Type t1, Type t2) { return ((t1 >= t2) ? T(1) : T(0)); }
  8800. static inline T process(const std::string& t1, const std::string& t2) { return ((t1 >= t2) ? T(1) : T(0)); }
  8801. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_gte; }
  8802. static inline details::operator_type operation() { return details::e_gte; }
  8803. };
  8804. template <typename T>
  8805. struct eq_op : public opr_base<T>
  8806. {
  8807. typedef typename opr_base<T>::Type Type;
  8808. static inline T process(Type t1, Type t2) { return (std::equal_to<T>()(t1,t2) ? T(1) : T(0)); }
  8809. static inline T process(const std::string& t1, const std::string& t2) { return ((t1 == t2) ? T(1) : T(0)); }
  8810. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_eq; }
  8811. static inline details::operator_type operation() { return details::e_eq; }
  8812. };
  8813. template <typename T>
  8814. struct ne_op : public opr_base<T>
  8815. {
  8816. typedef typename opr_base<T>::Type Type;
  8817. static inline T process(Type t1, Type t2) { return (std::not_equal_to<T>()(t1,t2) ? T(1) : T(0)); }
  8818. static inline T process(const std::string& t1, const std::string& t2) { return ((t1 != t2) ? T(1) : T(0)); }
  8819. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_ne; }
  8820. static inline details::operator_type operation() { return details::e_ne; }
  8821. };
  8822. template <typename T>
  8823. struct and_op : public opr_base<T>
  8824. {
  8825. typedef typename opr_base<T>::Type Type;
  8826. static inline T process(Type t1, Type t2) { return (details::is_true(t1) && details::is_true(t2)) ? T(1) : T(0); }
  8827. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_and; }
  8828. static inline details::operator_type operation() { return details::e_and; }
  8829. };
  8830. template <typename T>
  8831. struct nand_op : public opr_base<T>
  8832. {
  8833. typedef typename opr_base<T>::Type Type;
  8834. static inline T process(Type t1, Type t2) { return (details::is_true(t1) && details::is_true(t2)) ? T(0) : T(1); }
  8835. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_nand; }
  8836. static inline details::operator_type operation() { return details::e_nand; }
  8837. };
  8838. template <typename T>
  8839. struct or_op : public opr_base<T>
  8840. {
  8841. typedef typename opr_base<T>::Type Type;
  8842. static inline T process(Type t1, Type t2) { return (details::is_true(t1) || details::is_true(t2)) ? T(1) : T(0); }
  8843. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_or; }
  8844. static inline details::operator_type operation() { return details::e_or; }
  8845. };
  8846. template <typename T>
  8847. struct nor_op : public opr_base<T>
  8848. {
  8849. typedef typename opr_base<T>::Type Type;
  8850. static inline T process(Type t1, Type t2) { return (details::is_true(t1) || details::is_true(t2)) ? T(0) : T(1); }
  8851. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_nor; }
  8852. static inline details::operator_type operation() { return details::e_nor; }
  8853. };
  8854. template <typename T>
  8855. struct xor_op : public opr_base<T>
  8856. {
  8857. typedef typename opr_base<T>::Type Type;
  8858. static inline T process(Type t1, Type t2) { return numeric::xor_opr<T>(t1,t2); }
  8859. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_nor; }
  8860. static inline details::operator_type operation() { return details::e_xor; }
  8861. };
  8862. template <typename T>
  8863. struct xnor_op : public opr_base<T>
  8864. {
  8865. typedef typename opr_base<T>::Type Type;
  8866. static inline T process(Type t1, Type t2) { return numeric::xnor_opr<T>(t1,t2); }
  8867. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_nor; }
  8868. static inline details::operator_type operation() { return details::e_xnor; }
  8869. };
  8870. template <typename T>
  8871. struct in_op : public opr_base<T>
  8872. {
  8873. typedef typename opr_base<T>::Type Type;
  8874. static inline T process(const T&, const T&) { return std::numeric_limits<T>::quiet_NaN(); }
  8875. static inline T process(const std::string& t1, const std::string& t2) { return ((std::string::npos != t2.find(t1)) ? T(1) : T(0)); }
  8876. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_in; }
  8877. static inline details::operator_type operation() { return details::e_in; }
  8878. };
  8879. template <typename T>
  8880. struct like_op : public opr_base<T>
  8881. {
  8882. typedef typename opr_base<T>::Type Type;
  8883. static inline T process(const T&, const T&) { return std::numeric_limits<T>::quiet_NaN(); }
  8884. static inline T process(const std::string& t1, const std::string& t2) { return (details::wc_match(t2,t1) ? T(1) : T(0)); }
  8885. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_like; }
  8886. static inline details::operator_type operation() { return details::e_like; }
  8887. };
  8888. template <typename T>
  8889. struct ilike_op : public opr_base<T>
  8890. {
  8891. typedef typename opr_base<T>::Type Type;
  8892. static inline T process(const T&, const T&) { return std::numeric_limits<T>::quiet_NaN(); }
  8893. static inline T process(const std::string& t1, const std::string& t2) { return (details::wc_imatch(t2,t1) ? T(1) : T(0)); }
  8894. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_ilike; }
  8895. static inline details::operator_type operation() { return details::e_ilike; }
  8896. };
  8897. template <typename T>
  8898. struct inrange_op : public opr_base<T>
  8899. {
  8900. typedef typename opr_base<T>::Type Type;
  8901. static inline T process(const T& t0, const T& t1, const T& t2) { return ((t0 <= t1) && (t1 <= t2)) ? T(1) : T(0); }
  8902. static inline T process(const std::string& t0, const std::string& t1, const std::string& t2)
  8903. {
  8904. return ((t0 <= t1) && (t1 <= t2)) ? T(1) : T(0);
  8905. }
  8906. static inline typename expression_node<T>::node_type type() { return expression_node<T>::e_inranges; }
  8907. static inline details::operator_type operation() { return details::e_inrange; }
  8908. };
  8909. template <typename T>
  8910. inline T value(details::expression_node<T>* n)
  8911. {
  8912. return n->value();
  8913. }
  8914. template <typename T>
  8915. inline T value(T* t)
  8916. {
  8917. return (*t);
  8918. }
  8919. template <typename T>
  8920. struct vararg_add_op : public opr_base<T>
  8921. {
  8922. typedef typename opr_base<T>::Type Type;
  8923. template <typename Type,
  8924. typename Allocator,
  8925. template <typename,typename> class Sequence>
  8926. static inline T process(const Sequence<Type,Allocator>& arg_list)
  8927. {
  8928. switch (arg_list.size())
  8929. {
  8930. case 0 : return T(0);
  8931. case 1 : return process_1(arg_list);
  8932. case 2 : return process_2(arg_list);
  8933. case 3 : return process_3(arg_list);
  8934. case 4 : return process_4(arg_list);
  8935. case 5 : return process_5(arg_list);
  8936. default :
  8937. {
  8938. T result = T(0);
  8939. for (std::size_t i = 0; i < arg_list.size(); ++i)
  8940. {
  8941. result += value(arg_list[i]);
  8942. }
  8943. return result;
  8944. }
  8945. }
  8946. }
  8947. template <typename Sequence>
  8948. static inline T process_1(const Sequence& arg_list)
  8949. {
  8950. return value(arg_list[0]);
  8951. }
  8952. template <typename Sequence>
  8953. static inline T process_2(const Sequence& arg_list)
  8954. {
  8955. return value(arg_list[0]) + value(arg_list[1]);
  8956. }
  8957. template <typename Sequence>
  8958. static inline T process_3(const Sequence& arg_list)
  8959. {
  8960. return value(arg_list[0]) + value(arg_list[1]) +
  8961. value(arg_list[2]);
  8962. }
  8963. template <typename Sequence>
  8964. static inline T process_4(const Sequence& arg_list)
  8965. {
  8966. return value(arg_list[0]) + value(arg_list[1]) +
  8967. value(arg_list[2]) + value(arg_list[3]);
  8968. }
  8969. template <typename Sequence>
  8970. static inline T process_5(const Sequence& arg_list)
  8971. {
  8972. return value(arg_list[0]) + value(arg_list[1]) +
  8973. value(arg_list[2]) + value(arg_list[3]) +
  8974. value(arg_list[4]);
  8975. }
  8976. };
  8977. template <typename T>
  8978. struct vararg_mul_op : public opr_base<T>
  8979. {
  8980. typedef typename opr_base<T>::Type Type;
  8981. template <typename Type,
  8982. typename Allocator,
  8983. template <typename,typename> class Sequence>
  8984. static inline T process(const Sequence<Type,Allocator>& arg_list)
  8985. {
  8986. switch (arg_list.size())
  8987. {
  8988. case 0 : return T(0);
  8989. case 1 : return process_1(arg_list);
  8990. case 2 : return process_2(arg_list);
  8991. case 3 : return process_3(arg_list);
  8992. case 4 : return process_4(arg_list);
  8993. case 5 : return process_5(arg_list);
  8994. default :
  8995. {
  8996. T result = T(value(arg_list[0]));
  8997. for (std::size_t i = 1; i < arg_list.size(); ++i)
  8998. {
  8999. result *= value(arg_list[i]);
  9000. }
  9001. return result;
  9002. }
  9003. }
  9004. }
  9005. template <typename Sequence>
  9006. static inline T process_1(const Sequence& arg_list)
  9007. {
  9008. return value(arg_list[0]);
  9009. }
  9010. template <typename Sequence>
  9011. static inline T process_2(const Sequence& arg_list)
  9012. {
  9013. return value(arg_list[0]) * value(arg_list[1]);
  9014. }
  9015. template <typename Sequence>
  9016. static inline T process_3(const Sequence& arg_list)
  9017. {
  9018. return value(arg_list[0]) * value(arg_list[1]) *
  9019. value(arg_list[2]);
  9020. }
  9021. template <typename Sequence>
  9022. static inline T process_4(const Sequence& arg_list)
  9023. {
  9024. return value(arg_list[0]) * value(arg_list[1]) *
  9025. value(arg_list[2]) * value(arg_list[3]);
  9026. }
  9027. template <typename Sequence>
  9028. static inline T process_5(const Sequence& arg_list)
  9029. {
  9030. return value(arg_list[0]) * value(arg_list[1]) *
  9031. value(arg_list[2]) * value(arg_list[3]) *
  9032. value(arg_list[4]);
  9033. }
  9034. };
  9035. template <typename T>
  9036. struct vararg_avg_op : public opr_base<T>
  9037. {
  9038. typedef typename opr_base<T>::Type Type;
  9039. template <typename Type,
  9040. typename Allocator,
  9041. template <typename,typename> class Sequence>
  9042. static inline T process(const Sequence<Type,Allocator>& arg_list)
  9043. {
  9044. switch (arg_list.size())
  9045. {
  9046. case 0 : return T(0);
  9047. case 1 : return process_1(arg_list);
  9048. case 2 : return process_2(arg_list);
  9049. case 3 : return process_3(arg_list);
  9050. case 4 : return process_4(arg_list);
  9051. case 5 : return process_5(arg_list);
  9052. default : return vararg_add_op<T>::process(arg_list) / arg_list.size();
  9053. }
  9054. }
  9055. template <typename Sequence>
  9056. static inline T process_1(const Sequence& arg_list)
  9057. {
  9058. return value(arg_list[0]);
  9059. }
  9060. template <typename Sequence>
  9061. static inline T process_2(const Sequence& arg_list)
  9062. {
  9063. return (value(arg_list[0]) + value(arg_list[1])) / T(2);
  9064. }
  9065. template <typename Sequence>
  9066. static inline T process_3(const Sequence& arg_list)
  9067. {
  9068. return (value(arg_list[0]) + value(arg_list[1]) + value(arg_list[2])) / T(3);
  9069. }
  9070. template <typename Sequence>
  9071. static inline T process_4(const Sequence& arg_list)
  9072. {
  9073. return (value(arg_list[0]) + value(arg_list[1]) +
  9074. value(arg_list[2]) + value(arg_list[3])) / T(4);
  9075. }
  9076. template <typename Sequence>
  9077. static inline T process_5(const Sequence& arg_list)
  9078. {
  9079. return (value(arg_list[0]) + value(arg_list[1]) +
  9080. value(arg_list[2]) + value(arg_list[3]) +
  9081. value(arg_list[4])) / T(5);
  9082. }
  9083. };
  9084. template <typename T>
  9085. struct vararg_min_op : public opr_base<T>
  9086. {
  9087. typedef typename opr_base<T>::Type Type;
  9088. template <typename Type,
  9089. typename Allocator,
  9090. template <typename,typename> class Sequence>
  9091. static inline T process(const Sequence<Type,Allocator>& arg_list)
  9092. {
  9093. switch (arg_list.size())
  9094. {
  9095. case 0 : return T(0);
  9096. case 1 : return process_1(arg_list);
  9097. case 2 : return process_2(arg_list);
  9098. case 3 : return process_3(arg_list);
  9099. case 4 : return process_4(arg_list);
  9100. case 5 : return process_5(arg_list);
  9101. default :
  9102. {
  9103. T result = T(value(arg_list[0]));
  9104. for (std::size_t i = 1; i < arg_list.size(); ++i)
  9105. {
  9106. const T v = value(arg_list[i]);
  9107. if (v < result)
  9108. result = v;
  9109. }
  9110. return result;
  9111. }
  9112. }
  9113. }
  9114. template <typename Sequence>
  9115. static inline T process_1(const Sequence& arg_list)
  9116. {
  9117. return value(arg_list[0]);
  9118. }
  9119. template <typename Sequence>
  9120. static inline T process_2(const Sequence& arg_list)
  9121. {
  9122. return std::min<T>(value(arg_list[0]),value(arg_list[1]));
  9123. }
  9124. template <typename Sequence>
  9125. static inline T process_3(const Sequence& arg_list)
  9126. {
  9127. return std::min<T>(std::min<T>(value(arg_list[0]),value(arg_list[1])),value(arg_list[2]));
  9128. }
  9129. template <typename Sequence>
  9130. static inline T process_4(const Sequence& arg_list)
  9131. {
  9132. return std::min<T>(
  9133. std::min<T>(value(arg_list[0]),value(arg_list[1])),
  9134. std::min<T>(value(arg_list[2]),value(arg_list[3])));
  9135. }
  9136. template <typename Sequence>
  9137. static inline T process_5(const Sequence& arg_list)
  9138. {
  9139. return std::min<T>(
  9140. std::min<T>(std::min<T>(value(arg_list[0]),value(arg_list[1])),
  9141. std::min<T>(value(arg_list[2]),value(arg_list[3]))),
  9142. value(arg_list[4]));
  9143. }
  9144. };
  9145. template <typename T>
  9146. struct vararg_max_op : public opr_base<T>
  9147. {
  9148. typedef typename opr_base<T>::Type Type;
  9149. template <typename Type,
  9150. typename Allocator,
  9151. template <typename,typename> class Sequence>
  9152. static inline T process(const Sequence<Type,Allocator>& arg_list)
  9153. {
  9154. switch (arg_list.size())
  9155. {
  9156. case 0 : return T(0);
  9157. case 1 : return process_1(arg_list);
  9158. case 2 : return process_2(arg_list);
  9159. case 3 : return process_3(arg_list);
  9160. case 4 : return process_4(arg_list);
  9161. case 5 : return process_5(arg_list);
  9162. default :
  9163. {
  9164. T result = T(value(arg_list[0]));
  9165. for (std::size_t i = 1; i < arg_list.size(); ++i)
  9166. {
  9167. const T v = value(arg_list[i]);
  9168. if (v > result)
  9169. result = v;
  9170. }
  9171. return result;
  9172. }
  9173. }
  9174. }
  9175. template <typename Sequence>
  9176. static inline T process_1(const Sequence& arg_list)
  9177. {
  9178. return value(arg_list[0]);
  9179. }
  9180. template <typename Sequence>
  9181. static inline T process_2(const Sequence& arg_list)
  9182. {
  9183. return std::max<T>(value(arg_list[0]),value(arg_list[1]));
  9184. }
  9185. template <typename Sequence>
  9186. static inline T process_3(const Sequence& arg_list)
  9187. {
  9188. return std::max<T>(std::max<T>(value(arg_list[0]),value(arg_list[1])),value(arg_list[2]));
  9189. }
  9190. template <typename Sequence>
  9191. static inline T process_4(const Sequence& arg_list)
  9192. {
  9193. return std::max<T>(
  9194. std::max<T>(value(arg_list[0]),value(arg_list[1])),
  9195. std::max<T>(value(arg_list[2]),value(arg_list[3])));
  9196. }
  9197. template <typename Sequence>
  9198. static inline T process_5(const Sequence& arg_list)
  9199. {
  9200. return std::max<T>(
  9201. std::max<T>(std::max<T>(value(arg_list[0]),value(arg_list[1])),
  9202. std::max<T>(value(arg_list[2]),value(arg_list[3]))),
  9203. value(arg_list[4]));
  9204. }
  9205. };
  9206. template <typename T>
  9207. struct vararg_mand_op : public opr_base<T>
  9208. {
  9209. typedef typename opr_base<T>::Type Type;
  9210. template <typename Type,
  9211. typename Allocator,
  9212. template <typename,typename> class Sequence>
  9213. static inline T process(const Sequence<Type,Allocator>& arg_list)
  9214. {
  9215. switch (arg_list.size())
  9216. {
  9217. case 1 : return process_1(arg_list);
  9218. case 2 : return process_2(arg_list);
  9219. case 3 : return process_3(arg_list);
  9220. case 4 : return process_4(arg_list);
  9221. case 5 : return process_5(arg_list);
  9222. default :
  9223. {
  9224. for (std::size_t i = 0; i < arg_list.size(); ++i)
  9225. {
  9226. if (std::equal_to<T>()(T(0),value(arg_list[i])))
  9227. return T(0);
  9228. }
  9229. return T(1);
  9230. }
  9231. }
  9232. }
  9233. template <typename Sequence>
  9234. static inline T process_1(const Sequence& arg_list)
  9235. {
  9236. return std::not_equal_to<T>()
  9237. (T(0),value(arg_list[0])) ? T(1) : T(0);
  9238. }
  9239. template <typename Sequence>
  9240. static inline T process_2(const Sequence& arg_list)
  9241. {
  9242. return (
  9243. std::not_equal_to<T>()(T(0),value(arg_list[0])) &&
  9244. std::not_equal_to<T>()(T(0),value(arg_list[1]))
  9245. ) ? T(1) : T(0);
  9246. }
  9247. template <typename Sequence>
  9248. static inline T process_3(const Sequence& arg_list)
  9249. {
  9250. return (
  9251. std::not_equal_to<T>()(T(0),value(arg_list[0])) &&
  9252. std::not_equal_to<T>()(T(0),value(arg_list[1])) &&
  9253. std::not_equal_to<T>()(T(0),value(arg_list[2]))
  9254. ) ? T(1) : T(0);
  9255. }
  9256. template <typename Sequence>
  9257. static inline T process_4(const Sequence& arg_list)
  9258. {
  9259. return (
  9260. std::not_equal_to<T>()(T(0),value(arg_list[0])) &&
  9261. std::not_equal_to<T>()(T(0),value(arg_list[1])) &&
  9262. std::not_equal_to<T>()(T(0),value(arg_list[2])) &&
  9263. std::not_equal_to<T>()(T(0),value(arg_list[3]))
  9264. ) ? T(1) : T(0);
  9265. }
  9266. template <typename Sequence>
  9267. static inline T process_5(const Sequence& arg_list)
  9268. {
  9269. return (
  9270. std::not_equal_to<T>()(T(0),value(arg_list[0])) &&
  9271. std::not_equal_to<T>()(T(0),value(arg_list[1])) &&
  9272. std::not_equal_to<T>()(T(0),value(arg_list[2])) &&
  9273. std::not_equal_to<T>()(T(0),value(arg_list[3])) &&
  9274. std::not_equal_to<T>()(T(0),value(arg_list[4]))
  9275. ) ? T(1) : T(0);
  9276. }
  9277. };
  9278. template <typename T>
  9279. struct vararg_mor_op : public opr_base<T>
  9280. {
  9281. typedef typename opr_base<T>::Type Type;
  9282. template <typename Type,
  9283. typename Allocator,
  9284. template <typename,typename> class Sequence>
  9285. static inline T process(const Sequence<Type,Allocator>& arg_list)
  9286. {
  9287. switch (arg_list.size())
  9288. {
  9289. case 1 : return process_1(arg_list);
  9290. case 2 : return process_2(arg_list);
  9291. case 3 : return process_3(arg_list);
  9292. case 4 : return process_4(arg_list);
  9293. case 5 : return process_5(arg_list);
  9294. default :
  9295. {
  9296. for (std::size_t i = 0; i < arg_list.size(); ++i)
  9297. {
  9298. if (std::not_equal_to<T>()(T(0),value(arg_list[i])))
  9299. return T(1);
  9300. }
  9301. return T(0);
  9302. }
  9303. }
  9304. }
  9305. template <typename Sequence>
  9306. static inline T process_1(const Sequence& arg_list)
  9307. {
  9308. return std::not_equal_to<T>()
  9309. (T(0),value(arg_list[0])) ? T(1) : T(0);
  9310. }
  9311. template <typename Sequence>
  9312. static inline T process_2(const Sequence& arg_list)
  9313. {
  9314. return (
  9315. std::not_equal_to<T>()(T(0),value(arg_list[0])) ||
  9316. std::not_equal_to<T>()(T(0),value(arg_list[1]))
  9317. ) ? T(1) : T(0);
  9318. }
  9319. template <typename Sequence>
  9320. static inline T process_3(const Sequence& arg_list)
  9321. {
  9322. return (
  9323. std::not_equal_to<T>()(T(0),value(arg_list[0])) ||
  9324. std::not_equal_to<T>()(T(0),value(arg_list[1])) ||
  9325. std::not_equal_to<T>()(T(0),value(arg_list[2]))
  9326. ) ? T(1) : T(0);
  9327. }
  9328. template <typename Sequence>
  9329. static inline T process_4(const Sequence& arg_list)
  9330. {
  9331. return (
  9332. std::not_equal_to<T>()(T(0),value(arg_list[0])) ||
  9333. std::not_equal_to<T>()(T(0),value(arg_list[1])) ||
  9334. std::not_equal_to<T>()(T(0),value(arg_list[2])) ||
  9335. std::not_equal_to<T>()(T(0),value(arg_list[3]))
  9336. ) ? T(1) : T(0);
  9337. }
  9338. template <typename Sequence>
  9339. static inline T process_5(const Sequence& arg_list)
  9340. {
  9341. return (
  9342. std::not_equal_to<T>()(T(0),value(arg_list[0])) ||
  9343. std::not_equal_to<T>()(T(0),value(arg_list[1])) ||
  9344. std::not_equal_to<T>()(T(0),value(arg_list[2])) ||
  9345. std::not_equal_to<T>()(T(0),value(arg_list[3])) ||
  9346. std::not_equal_to<T>()(T(0),value(arg_list[4]))
  9347. ) ? T(1) : T(0);
  9348. }
  9349. };
  9350. template <typename T>
  9351. struct vararg_multi_op : public opr_base<T>
  9352. {
  9353. typedef typename opr_base<T>::Type Type;
  9354. template <typename Type,
  9355. typename Allocator,
  9356. template <typename,typename> class Sequence>
  9357. static inline T process(const Sequence<Type,Allocator>& arg_list)
  9358. {
  9359. switch (arg_list.size())
  9360. {
  9361. case 0 : return std::numeric_limits<T>::quiet_NaN();
  9362. case 1 : return process_1(arg_list);
  9363. case 2 : return process_2(arg_list);
  9364. case 3 : return process_3(arg_list);
  9365. case 4 : return process_4(arg_list);
  9366. case 5 : return process_5(arg_list);
  9367. case 6 : return process_6(arg_list);
  9368. case 7 : return process_7(arg_list);
  9369. case 8 : return process_8(arg_list);
  9370. default :
  9371. {
  9372. for (std::size_t i = 0; i < (arg_list.size() - 1); ++i)
  9373. {
  9374. value(arg_list[i]);
  9375. }
  9376. return value(arg_list.back());
  9377. }
  9378. }
  9379. }
  9380. template <typename Sequence>
  9381. static inline T process_1(const Sequence& arg_list)
  9382. {
  9383. return value(arg_list[0]);
  9384. }
  9385. template <typename Sequence>
  9386. static inline T process_2(const Sequence& arg_list)
  9387. {
  9388. value(arg_list[0]);
  9389. return value(arg_list[1]);
  9390. }
  9391. template <typename Sequence>
  9392. static inline T process_3(const Sequence& arg_list)
  9393. {
  9394. value(arg_list[0]);
  9395. value(arg_list[1]);
  9396. return value(arg_list[2]);
  9397. }
  9398. template <typename Sequence>
  9399. static inline T process_4(const Sequence& arg_list)
  9400. {
  9401. value(arg_list[0]);
  9402. value(arg_list[1]);
  9403. value(arg_list[2]);
  9404. return value(arg_list[3]);
  9405. }
  9406. template <typename Sequence>
  9407. static inline T process_5(const Sequence& arg_list)
  9408. {
  9409. value(arg_list[0]);
  9410. value(arg_list[1]);
  9411. value(arg_list[2]);
  9412. value(arg_list[3]);
  9413. return value(arg_list[4]);
  9414. }
  9415. template <typename Sequence>
  9416. static inline T process_6(const Sequence& arg_list)
  9417. {
  9418. value(arg_list[0]);
  9419. value(arg_list[1]);
  9420. value(arg_list[2]);
  9421. value(arg_list[3]);
  9422. value(arg_list[4]);
  9423. return value(arg_list[5]);
  9424. }
  9425. template <typename Sequence>
  9426. static inline T process_7(const Sequence& arg_list)
  9427. {
  9428. value(arg_list[0]);
  9429. value(arg_list[1]);
  9430. value(arg_list[2]);
  9431. value(arg_list[3]);
  9432. value(arg_list[4]);
  9433. value(arg_list[5]);
  9434. return value(arg_list[6]);
  9435. }
  9436. template <typename Sequence>
  9437. static inline T process_8(const Sequence& arg_list)
  9438. {
  9439. value(arg_list[0]);
  9440. value(arg_list[1]);
  9441. value(arg_list[2]);
  9442. value(arg_list[3]);
  9443. value(arg_list[4]);
  9444. value(arg_list[5]);
  9445. value(arg_list[6]);
  9446. return value(arg_list[7]);
  9447. }
  9448. };
  9449. template <typename T>
  9450. struct vec_add_op
  9451. {
  9452. typedef vector_interface<T>* ivector_ptr;
  9453. static inline T process(const ivector_ptr v)
  9454. {
  9455. vector_holder<T>& vec = v->vec()->ref();
  9456. T result = T(0);
  9457. for (std::size_t i = 0; i < vec.size(); ++i)
  9458. {
  9459. result += (*vec[i]);
  9460. }
  9461. return result;
  9462. }
  9463. };
  9464. template <typename T>
  9465. struct vec_mul_op
  9466. {
  9467. typedef vector_interface<T>* ivector_ptr;
  9468. static inline T process(const ivector_ptr v)
  9469. {
  9470. vector_holder<T>& vec = v->vec()->ref();
  9471. T result = (*vec[0]);
  9472. for (std::size_t i = 1; i < vec.size(); ++i)
  9473. {
  9474. result *= (*vec[i]);
  9475. }
  9476. return result;
  9477. }
  9478. };
  9479. template <typename T>
  9480. struct vec_avg_op
  9481. {
  9482. typedef vector_interface<T>* ivector_ptr;
  9483. static inline T process(const ivector_ptr v)
  9484. {
  9485. vector_holder<T>& vec = v->vec()->ref();
  9486. T result = T(0);
  9487. for (std::size_t i = 0; i < vec.size(); ++i)
  9488. {
  9489. result += (*vec[i]);
  9490. }
  9491. return result / vec.size();
  9492. }
  9493. };
  9494. template <typename T>
  9495. struct vec_min_op
  9496. {
  9497. typedef vector_interface<T>* ivector_ptr;
  9498. static inline T process(const ivector_ptr v)
  9499. {
  9500. vector_holder<T>& vec = v->vec()->ref();
  9501. T result = (*vec[0]);
  9502. for (std::size_t i = 1; i < vec.size(); ++i)
  9503. {
  9504. T v_i = (*vec[i]);
  9505. if (v_i < result)
  9506. result = v_i;
  9507. }
  9508. return result;
  9509. }
  9510. };
  9511. template <typename T>
  9512. struct vec_max_op
  9513. {
  9514. typedef vector_interface<T>* ivector_ptr;
  9515. static inline T process(const ivector_ptr v)
  9516. {
  9517. vector_holder<T>& vec = v->vec()->ref();
  9518. T result = (*vec[0]);
  9519. for (std::size_t i = 1; i < vec.size(); ++i)
  9520. {
  9521. T v_i = (*vec[i]);
  9522. if (v_i > result)
  9523. result = v_i;
  9524. }
  9525. return result;
  9526. }
  9527. };
  9528. template <typename T>
  9529. class vov_base_node : public expression_node<T>
  9530. {
  9531. public:
  9532. inline virtual operator_type operation() const
  9533. {
  9534. return details::e_default;
  9535. }
  9536. virtual const T& v0() const = 0;
  9537. virtual const T& v1() const = 0;
  9538. };
  9539. template <typename T>
  9540. class cov_base_node : public expression_node<T>
  9541. {
  9542. public:
  9543. inline virtual operator_type operation() const
  9544. {
  9545. return details::e_default;
  9546. }
  9547. virtual const T c() const = 0;
  9548. virtual const T& v() const = 0;
  9549. };
  9550. template <typename T>
  9551. class voc_base_node : public expression_node<T>
  9552. {
  9553. public:
  9554. inline virtual operator_type operation() const
  9555. {
  9556. return details::e_default;
  9557. }
  9558. virtual const T c() const = 0;
  9559. virtual const T& v() const = 0;
  9560. };
  9561. template <typename T>
  9562. class vob_base_node : public expression_node<T>
  9563. {
  9564. public:
  9565. virtual const T& v() const = 0;
  9566. };
  9567. template <typename T>
  9568. class bov_base_node : public expression_node<T>
  9569. {
  9570. public:
  9571. virtual const T& v() const = 0;
  9572. };
  9573. template <typename T>
  9574. class cob_base_node : public expression_node<T>
  9575. {
  9576. public:
  9577. inline virtual operator_type operation() const
  9578. {
  9579. return details::e_default;
  9580. }
  9581. virtual const T c() const = 0;
  9582. virtual void set_c(const T) = 0;
  9583. virtual expression_node<T>* move_branch(const std::size_t& index) = 0;
  9584. };
  9585. template <typename T>
  9586. class boc_base_node : public expression_node<T>
  9587. {
  9588. public:
  9589. inline virtual operator_type operation() const
  9590. {
  9591. return details::e_default;
  9592. }
  9593. virtual const T c() const = 0;
  9594. virtual void set_c(const T) = 0;
  9595. virtual expression_node<T>* move_branch(const std::size_t& index) = 0;
  9596. };
  9597. template <typename T>
  9598. class uv_base_node : public expression_node<T>
  9599. {
  9600. public:
  9601. inline virtual operator_type operation() const
  9602. {
  9603. return details::e_default;
  9604. }
  9605. virtual const T& v() const = 0;
  9606. };
  9607. template <typename T>
  9608. class sos_base_node : public expression_node<T>
  9609. {
  9610. public:
  9611. inline virtual operator_type operation() const
  9612. {
  9613. return details::e_default;
  9614. }
  9615. };
  9616. template <typename T>
  9617. class sosos_base_node : public expression_node<T>
  9618. {
  9619. public:
  9620. inline virtual operator_type operation() const
  9621. {
  9622. return details::e_default;
  9623. }
  9624. };
  9625. template <typename T>
  9626. class T0oT1oT2_base_node : public expression_node<T>
  9627. {
  9628. public:
  9629. virtual std::string type_id() const = 0;
  9630. };
  9631. template <typename T>
  9632. class T0oT1oT2oT3_base_node : public expression_node<T>
  9633. {
  9634. public:
  9635. virtual std::string type_id() const = 0;
  9636. };
  9637. template <typename T, typename Operation>
  9638. class unary_variable_node : public uv_base_node<T>
  9639. {
  9640. public:
  9641. typedef expression_node<T>* expression_ptr;
  9642. typedef Operation operation_t;
  9643. explicit unary_variable_node(const T& var)
  9644. : v_(var)
  9645. {}
  9646. inline T value() const
  9647. {
  9648. return Operation::process(v_);
  9649. }
  9650. inline typename expression_node<T>::node_type type() const
  9651. {
  9652. return Operation::type();
  9653. }
  9654. inline operator_type operation() const
  9655. {
  9656. return Operation::operation();
  9657. }
  9658. inline const T& v() const
  9659. {
  9660. return v_;
  9661. }
  9662. private:
  9663. unary_variable_node(unary_variable_node<T,Operation>&);
  9664. unary_variable_node<T,Operation>& operator=(unary_variable_node<T,Operation>&);
  9665. const T& v_;
  9666. };
  9667. template <typename T>
  9668. class uvouv_node : public expression_node<T>
  9669. {
  9670. public:
  9671. // UOpr1(v0) Op UOpr2(v1)
  9672. typedef expression_node<T>* expression_ptr;
  9673. typedef typename details::functor_t<T> functor_t;
  9674. typedef typename functor_t::bfunc_t bfunc_t;
  9675. typedef typename functor_t::ufunc_t ufunc_t;
  9676. explicit uvouv_node(const T& var0,const T& var1,
  9677. ufunc_t uf0, ufunc_t uf1, bfunc_t bf)
  9678. : v0_(var0),
  9679. v1_(var1),
  9680. u0_(uf0),
  9681. u1_(uf1),
  9682. f_ (bf)
  9683. {}
  9684. inline T value() const
  9685. {
  9686. return f_(u0_(v0_),u1_(v1_));
  9687. }
  9688. inline typename expression_node<T>::node_type type() const
  9689. {
  9690. return expression_node<T>::e_uvouv;
  9691. }
  9692. inline operator_type operation() const
  9693. {
  9694. return details::e_default;
  9695. }
  9696. inline const T& v0()
  9697. {
  9698. return v0_;
  9699. }
  9700. inline const T& v1()
  9701. {
  9702. return v1_;
  9703. }
  9704. inline ufunc_t u0()
  9705. {
  9706. return u0_;
  9707. }
  9708. inline ufunc_t u1()
  9709. {
  9710. return u1_;
  9711. }
  9712. inline ufunc_t f()
  9713. {
  9714. return f_;
  9715. }
  9716. private:
  9717. uvouv_node(uvouv_node<T>&);
  9718. uvouv_node<T>& operator=(uvouv_node<T>&);
  9719. const T& v0_;
  9720. const T& v1_;
  9721. const ufunc_t u0_;
  9722. const ufunc_t u1_;
  9723. const bfunc_t f_;
  9724. };
  9725. template <typename T, typename Operation>
  9726. class unary_branch_node : public expression_node<T>
  9727. {
  9728. public:
  9729. typedef expression_node<T>* expression_ptr;
  9730. typedef Operation operation_t;
  9731. explicit unary_branch_node(expression_ptr brnch)
  9732. : branch_(brnch),
  9733. branch_deletable_(branch_deletable(branch_))
  9734. {}
  9735. ~unary_branch_node()
  9736. {
  9737. if (branch_ && branch_deletable_)
  9738. {
  9739. delete branch_;
  9740. branch_ = 0;
  9741. }
  9742. }
  9743. inline T value() const
  9744. {
  9745. return Operation::process(branch_->value());
  9746. }
  9747. inline typename expression_node<T>::node_type type() const
  9748. {
  9749. return Operation::type();
  9750. }
  9751. inline operator_type operation() const
  9752. {
  9753. return Operation::operation();
  9754. }
  9755. inline expression_node<T>* branch(const std::size_t&) const
  9756. {
  9757. return branch_;
  9758. }
  9759. inline void release()
  9760. {
  9761. branch_deletable_ = false;
  9762. }
  9763. private:
  9764. unary_branch_node(unary_branch_node<T,Operation>&);
  9765. unary_branch_node<T,Operation>& operator=(unary_branch_node<T,Operation>&);
  9766. expression_ptr branch_;
  9767. bool branch_deletable_;
  9768. };
  9769. template <typename T> struct is_const { enum {result = 0}; };
  9770. template <typename T> struct is_const <const T> { enum {result = 1}; };
  9771. template <typename T> struct is_const_ref { enum {result = 0}; };
  9772. template <typename T> struct is_const_ref <const T&> { enum {result = 1}; };
  9773. template <typename T> struct is_ref { enum {result = 0}; };
  9774. template <typename T> struct is_ref<T&> { enum {result = 1}; };
  9775. template <typename T> struct is_ref<const T&> { enum {result = 0}; };
  9776. template <std::size_t State>
  9777. struct param_to_str { static std::string result() { static const std::string r("v"); return r; } };
  9778. template <>
  9779. struct param_to_str<0> { static std::string result() { static const std::string r("c"); return r; } };
  9780. #define exprtk_crtype(Type) \
  9781. param_to_str<is_const_ref< Type >::result>::result() \
  9782. template <typename T>
  9783. struct T0oT1oT2process
  9784. {
  9785. typedef typename details::functor_t<T> functor_t;
  9786. typedef typename functor_t::bfunc_t bfunc_t;
  9787. struct mode0
  9788. {
  9789. static inline T process(const T& t0, const T& t1, const T& t2, const bfunc_t bf0, const bfunc_t bf1)
  9790. {
  9791. // (T0 o0 T1) o1 T2
  9792. return bf1(bf0(t0,t1),t2);
  9793. }
  9794. template <typename T0, typename T1, typename T2>
  9795. static inline std::string id()
  9796. {
  9797. static const std::string result = "(" + exprtk_crtype(T0) + "o" +
  9798. exprtk_crtype(T1) + ")o(" +
  9799. exprtk_crtype(T2) + ")" ;
  9800. return result;
  9801. }
  9802. };
  9803. struct mode1
  9804. {
  9805. static inline T process(const T& t0, const T& t1, const T& t2, const bfunc_t bf0, const bfunc_t bf1)
  9806. {
  9807. // T0 o0 (T1 o1 T2)
  9808. return bf0(t0,bf1(t1,t2));
  9809. }
  9810. template <typename T0, typename T1, typename T2>
  9811. static inline std::string id()
  9812. {
  9813. static const std::string result = "(" + exprtk_crtype(T0) + ")o(" +
  9814. exprtk_crtype(T1) + "o" +
  9815. exprtk_crtype(T2) + ")" ;
  9816. return result;
  9817. }
  9818. };
  9819. };
  9820. template <typename T>
  9821. struct T0oT1oT20T3process
  9822. {
  9823. typedef typename details::functor_t<T> functor_t;
  9824. typedef typename functor_t::bfunc_t bfunc_t;
  9825. struct mode0
  9826. {
  9827. static inline T process(const T& t0, const T& t1,
  9828. const T& t2, const T& t3,
  9829. const bfunc_t bf0, const bfunc_t bf1, const bfunc_t bf2)
  9830. {
  9831. // (T0 o0 T1) o1 (T2 o2 T3)
  9832. return bf1(bf0(t0,t1),bf2(t2,t3));
  9833. }
  9834. template <typename T0, typename T1, typename T2, typename T3>
  9835. static inline std::string id()
  9836. {
  9837. static const std::string result = "(" + exprtk_crtype(T0) + "o" +
  9838. exprtk_crtype(T1) + ")o" +
  9839. "(" + exprtk_crtype(T2) + "o" +
  9840. exprtk_crtype(T3) + ")" ;
  9841. return result;
  9842. }
  9843. };
  9844. struct mode1
  9845. {
  9846. static inline T process(const T& t0, const T& t1,
  9847. const T& t2, const T& t3,
  9848. const bfunc_t bf0, const bfunc_t bf1, const bfunc_t bf2)
  9849. {
  9850. // (T0 o0 (T1 o1 (T2 o2 T3))
  9851. return bf0(t0,bf1(t1,bf2(t2,t3)));
  9852. }
  9853. template <typename T0, typename T1, typename T2, typename T3>
  9854. static inline std::string id()
  9855. {
  9856. static const std::string result = "(" + exprtk_crtype(T0) + ")o((" +
  9857. exprtk_crtype(T1) + ")o(" +
  9858. exprtk_crtype(T2) + "o" +
  9859. exprtk_crtype(T3) + "))" ;
  9860. return result;
  9861. }
  9862. };
  9863. struct mode2
  9864. {
  9865. static inline T process(const T& t0, const T& t1,
  9866. const T& t2, const T& t3,
  9867. const bfunc_t bf0, const bfunc_t bf1, const bfunc_t bf2)
  9868. {
  9869. // (T0 o0 ((T1 o1 T2) o2 T3)
  9870. return bf0(t0,bf2(bf1(t1,t2),t3));
  9871. }
  9872. template <typename T0, typename T1, typename T2, typename T3>
  9873. static inline std::string id()
  9874. {
  9875. static const std::string result = "(" + exprtk_crtype(T0) + ")o((" +
  9876. exprtk_crtype(T1) + "o" +
  9877. exprtk_crtype(T2) + ")o(" +
  9878. exprtk_crtype(T3) + "))" ;
  9879. return result;
  9880. }
  9881. };
  9882. struct mode3
  9883. {
  9884. static inline T process(const T& t0, const T& t1,
  9885. const T& t2, const T& t3,
  9886. const bfunc_t bf0, const bfunc_t bf1, const bfunc_t bf2)
  9887. {
  9888. // (((T0 o0 T1) o1 T2) o2 T3)
  9889. return bf2(bf1(bf0(t0,t1),t2),t3);
  9890. }
  9891. template <typename T0, typename T1, typename T2, typename T3>
  9892. static inline std::string id()
  9893. {
  9894. static const std::string result = "((" + exprtk_crtype(T0) + "o" +
  9895. exprtk_crtype(T1) + ")o(" +
  9896. exprtk_crtype(T2) + "))o(" +
  9897. exprtk_crtype(T3) + ")";
  9898. return result;
  9899. }
  9900. };
  9901. struct mode4
  9902. {
  9903. static inline T process(const T& t0, const T& t1,
  9904. const T& t2, const T& t3,
  9905. const bfunc_t bf0, const bfunc_t bf1, const bfunc_t bf2)
  9906. {
  9907. // ((T0 o0 (T1 o1 T2)) o2 T3
  9908. return bf2(bf0(t0,bf1(t1,t2)),t3);
  9909. }
  9910. template <typename T0, typename T1, typename T2, typename T3>
  9911. static inline std::string id()
  9912. {
  9913. static const std::string result = "((" + exprtk_crtype(T0) + ")o(" +
  9914. exprtk_crtype(T1) + "o" +
  9915. exprtk_crtype(T2) + "))o(" +
  9916. exprtk_crtype(T3) + ")" ;
  9917. return result;
  9918. }
  9919. };
  9920. };
  9921. #undef exprtk_crtype
  9922. template <typename T, typename T0, typename T1>
  9923. struct nodetype_T0oT1 { static const typename expression_node<T>::node_type result; };
  9924. template <typename T, typename T0, typename T1>
  9925. const typename expression_node<T>::node_type nodetype_T0oT1<T,T0,T1>::result = expression_node<T>::e_none;
  9926. #define synthesis_node_type_define(T0_,T1_,v_) \
  9927. template <typename T, typename T0, typename T1> \
  9928. struct nodetype_T0oT1<T,T0_,T1_> { static const typename expression_node<T>::node_type result; }; \
  9929. template <typename T, typename T0, typename T1> \
  9930. const typename expression_node<T>::node_type nodetype_T0oT1<T,T0_,T1_>::result = expression_node<T>:: v_; \
  9931. synthesis_node_type_define(const T0&,const T1&, e_vov)
  9932. synthesis_node_type_define(const T0&,const T1 , e_voc)
  9933. synthesis_node_type_define(const T0 ,const T1&, e_cov)
  9934. synthesis_node_type_define( T0&, T1&,e_none)
  9935. synthesis_node_type_define(const T0 ,const T1 ,e_none)
  9936. synthesis_node_type_define( T0&,const T1 ,e_none)
  9937. synthesis_node_type_define(const T0 , T1&,e_none)
  9938. synthesis_node_type_define(const T0&, T1&,e_none)
  9939. synthesis_node_type_define( T0&,const T1&,e_none)
  9940. #undef synthesis_node_type_define
  9941. template <typename T, typename T0, typename T1, typename T2>
  9942. struct nodetype_T0oT1oT2 { static const typename expression_node<T>::node_type result; };
  9943. template <typename T, typename T0, typename T1, typename T2>
  9944. const typename expression_node<T>::node_type nodetype_T0oT1oT2<T,T0,T1,T2>::result = expression_node<T>::e_none;
  9945. #define synthesis_node_type_define(T0_,T1_,T2_,v_) \
  9946. template <typename T, typename T0, typename T1, typename T2> \
  9947. struct nodetype_T0oT1oT2<T,T0_,T1_,T2_> { static const typename expression_node<T>::node_type result; }; \
  9948. template <typename T, typename T0, typename T1, typename T2> \
  9949. const typename expression_node<T>::node_type nodetype_T0oT1oT2<T,T0_,T1_,T2_>::result = expression_node<T>:: v_; \
  9950. synthesis_node_type_define(const T0&,const T1&,const T2&, e_vovov)
  9951. synthesis_node_type_define(const T0&,const T1&,const T2 , e_vovoc)
  9952. synthesis_node_type_define(const T0&,const T1 ,const T2&, e_vocov)
  9953. synthesis_node_type_define(const T0 ,const T1&,const T2&, e_covov)
  9954. synthesis_node_type_define(const T0 ,const T1&,const T2 , e_covoc)
  9955. synthesis_node_type_define(const T0 ,const T1 ,const T2 , e_none )
  9956. synthesis_node_type_define(const T0 ,const T1 ,const T2&, e_none )
  9957. synthesis_node_type_define(const T0&,const T1 ,const T2 , e_none )
  9958. synthesis_node_type_define( T0&, T1&, T2&, e_none )
  9959. #undef synthesis_node_type_define
  9960. template <typename T, typename T0, typename T1, typename T2, typename T3>
  9961. struct nodetype_T0oT1oT2oT3 { static const typename expression_node<T>::node_type result; };
  9962. template <typename T, typename T0, typename T1, typename T2, typename T3>
  9963. const typename expression_node<T>::node_type nodetype_T0oT1oT2oT3<T,T0,T1,T2,T3>::result = expression_node<T>::e_none;
  9964. #define synthesis_node_type_define(T0_,T1_,T2_,T3_,v_) \
  9965. template <typename T, typename T0, typename T1, typename T2, typename T3> \
  9966. struct nodetype_T0oT1oT2oT3<T,T0_,T1_,T2_,T3_> { static const typename expression_node<T>::node_type result; }; \
  9967. template <typename T, typename T0, typename T1, typename T2, typename T3> \
  9968. const typename expression_node<T>::node_type nodetype_T0oT1oT2oT3<T,T0_,T1_,T2_,T3_>::result = expression_node<T>:: v_; \
  9969. synthesis_node_type_define(const T0&,const T1&,const T2&, const T3&,e_vovovov)
  9970. synthesis_node_type_define(const T0&,const T1&,const T2&, const T3 ,e_vovovoc)
  9971. synthesis_node_type_define(const T0&,const T1&,const T2 , const T3&,e_vovocov)
  9972. synthesis_node_type_define(const T0&,const T1 ,const T2&, const T3&,e_vocovov)
  9973. synthesis_node_type_define(const T0 ,const T1&,const T2&, const T3&,e_covovov)
  9974. synthesis_node_type_define(const T0 ,const T1&,const T2 , const T3&,e_covocov)
  9975. synthesis_node_type_define(const T0&,const T1 ,const T2&, const T3 ,e_vocovoc)
  9976. synthesis_node_type_define(const T0 ,const T1&,const T2&, const T3 ,e_covovoc)
  9977. synthesis_node_type_define(const T0&,const T1 ,const T2 , const T3&,e_vococov)
  9978. synthesis_node_type_define(const T0 ,const T1 ,const T2 , const T3 ,e_none )
  9979. synthesis_node_type_define(const T0 ,const T1 ,const T2 , const T3&,e_none )
  9980. synthesis_node_type_define(const T0 ,const T1 ,const T2&, const T3 ,e_none )
  9981. synthesis_node_type_define(const T0 ,const T1&,const T2 , const T3 ,e_none )
  9982. synthesis_node_type_define(const T0&,const T1 ,const T2 , const T3 ,e_none )
  9983. synthesis_node_type_define(const T0 ,const T1 ,const T2&, const T3&,e_none )
  9984. synthesis_node_type_define(const T0&,const T1&,const T2 , const T3 ,e_none )
  9985. #undef synthesis_node_type_define
  9986. template <typename T, typename T0, typename T1>
  9987. class T0oT1 : public expression_node<T>
  9988. {
  9989. public:
  9990. typedef typename details::functor_t<T> functor_t;
  9991. typedef typename functor_t::bfunc_t bfunc_t;
  9992. typedef T value_type;
  9993. typedef T0oT1<T,T0,T1> node_type;
  9994. T0oT1(T0 p0, T1 p1, const bfunc_t p2)
  9995. : t0_(p0),
  9996. t1_(p1),
  9997. f_ (p2)
  9998. {}
  9999. inline typename expression_node<T>::node_type type() const
  10000. {
  10001. static const typename expression_node<T>::node_type result = nodetype_T0oT1<T,T0,T1>::result;
  10002. return result;
  10003. }
  10004. inline operator_type operation() const
  10005. {
  10006. return e_default;
  10007. }
  10008. inline T value() const
  10009. {
  10010. return f_(t0_,t1_);
  10011. }
  10012. inline T0 t0() const
  10013. {
  10014. return t0_;
  10015. }
  10016. inline T1 t1() const
  10017. {
  10018. return t1_;
  10019. }
  10020. inline bfunc_t f() const
  10021. {
  10022. return f_;
  10023. }
  10024. template <typename Allocator>
  10025. static inline expression_node<T>* allocate(Allocator& allocator,
  10026. T0 p0, T1 p1,
  10027. bfunc_t p2)
  10028. {
  10029. return allocator.template allocate_type<node_type,T0,T1,bfunc_t&>(p0,p1,p2);
  10030. }
  10031. private:
  10032. T0oT1(T0oT1<T,T0,T1>&) {}
  10033. T0oT1<T,T0,T1>& operator=(T0oT1<T,T0,T1>&) { return *this; }
  10034. T0 t0_;
  10035. T1 t1_;
  10036. const bfunc_t f_;
  10037. };
  10038. template <typename T, typename T0, typename T1, typename T2, typename ProcessMode>
  10039. class T0oT1oT2 : public T0oT1oT2_base_node<T>
  10040. {
  10041. public:
  10042. typedef typename details::functor_t<T> functor_t;
  10043. typedef typename functor_t::bfunc_t bfunc_t;
  10044. typedef T value_type;
  10045. typedef T0oT1oT2<T,T0,T1,T2,ProcessMode> node_type;
  10046. typedef ProcessMode process_mode_t;
  10047. T0oT1oT2(T0 p0, T1 p1, T2 p2, const bfunc_t p3, const bfunc_t p4)
  10048. : t0_(p0),
  10049. t1_(p1),
  10050. t2_(p2),
  10051. f0_(p3),
  10052. f1_(p4)
  10053. {}
  10054. inline typename expression_node<T>::node_type type() const
  10055. {
  10056. static const typename expression_node<T>::node_type result = nodetype_T0oT1oT2<T,T0,T1,T2>::result;
  10057. return result;
  10058. }
  10059. inline operator_type operation() const
  10060. {
  10061. return e_default;
  10062. }
  10063. inline T value() const
  10064. {
  10065. return ProcessMode::process(t0_,t1_,t2_,f0_,f1_);
  10066. }
  10067. inline T0 t0() const
  10068. {
  10069. return t0_;
  10070. }
  10071. inline T1 t1() const
  10072. {
  10073. return t1_;
  10074. }
  10075. inline T2 t2() const
  10076. {
  10077. return t2_;
  10078. }
  10079. bfunc_t f0() const
  10080. {
  10081. return f0_;
  10082. }
  10083. bfunc_t f1() const
  10084. {
  10085. return f1_;
  10086. }
  10087. std::string type_id() const
  10088. {
  10089. return id();
  10090. }
  10091. static inline std::string id()
  10092. {
  10093. return process_mode_t::template id<T0,T1,T2>();
  10094. }
  10095. template <typename Allocator>
  10096. static inline expression_node<T>* allocate(Allocator& allocator, T0 p0, T1 p1, T2 p2, bfunc_t p3, bfunc_t p4)
  10097. {
  10098. return allocator.template allocate_type<node_type,T0,T1,T2,bfunc_t,bfunc_t>(p0,p1,p2,p3,p4);
  10099. }
  10100. private:
  10101. T0oT1oT2(node_type&) {}
  10102. node_type& operator=(node_type&) { return *this; }
  10103. T0 t0_;
  10104. T1 t1_;
  10105. T2 t2_;
  10106. const bfunc_t f0_;
  10107. const bfunc_t f1_;
  10108. };
  10109. template <typename T, typename T0_, typename T1_, typename T2_, typename T3_, typename ProcessMode>
  10110. class T0oT1oT2oT3 : public T0oT1oT2oT3_base_node<T>
  10111. {
  10112. public:
  10113. typedef typename details::functor_t<T> functor_t;
  10114. typedef typename functor_t::bfunc_t bfunc_t;
  10115. typedef T value_type;
  10116. typedef T0_ T0;
  10117. typedef T1_ T1;
  10118. typedef T2_ T2;
  10119. typedef T3_ T3;
  10120. typedef T0oT1oT2oT3<T,T0,T1,T2,T3,ProcessMode> node_type;
  10121. typedef ProcessMode process_mode_t;
  10122. T0oT1oT2oT3(T0 p0, T1 p1, T2 p2, T3 p3, bfunc_t p4, bfunc_t p5, bfunc_t p6)
  10123. : t0_(p0),
  10124. t1_(p1),
  10125. t2_(p2),
  10126. t3_(p3),
  10127. f0_(p4),
  10128. f1_(p5),
  10129. f2_(p6)
  10130. {}
  10131. inline T value() const
  10132. {
  10133. return ProcessMode::process(t0_,t1_,t2_,t3_,f0_,f1_,f2_);
  10134. }
  10135. inline T0 t0() const
  10136. {
  10137. return t0_;
  10138. }
  10139. inline T1 t1() const
  10140. {
  10141. return t1_;
  10142. }
  10143. inline T2 t2() const
  10144. {
  10145. return t2_;
  10146. }
  10147. inline T3 t3() const
  10148. {
  10149. return t3_;
  10150. }
  10151. inline bfunc_t f0() const
  10152. {
  10153. return f0_;
  10154. }
  10155. inline bfunc_t f1() const
  10156. {
  10157. return f1_;
  10158. }
  10159. inline bfunc_t f2() const
  10160. {
  10161. return f2_;
  10162. }
  10163. inline std::string type_id() const
  10164. {
  10165. return id();
  10166. }
  10167. static inline std::string id()
  10168. {
  10169. return process_mode_t::template id<T0,T1,T2,T3>();
  10170. }
  10171. template <typename Allocator>
  10172. static inline expression_node<T>* allocate(Allocator& allocator,
  10173. T0 p0, T1 p1, T2 p2, T3 p3,
  10174. bfunc_t p4, bfunc_t p5, bfunc_t p6)
  10175. {
  10176. return allocator.template allocate_type<node_type,T0,T1,T2,T3,bfunc_t,bfunc_t>(p0,p1,p2,p3,p4,p5,p6);
  10177. }
  10178. private:
  10179. T0oT1oT2oT3(node_type&) {}
  10180. node_type& operator=(node_type&) { return *this; }
  10181. T0 t0_;
  10182. T1 t1_;
  10183. T2 t2_;
  10184. T3 t3_;
  10185. const bfunc_t f0_;
  10186. const bfunc_t f1_;
  10187. const bfunc_t f2_;
  10188. };
  10189. template <typename T, typename T0, typename T1, typename T2>
  10190. class T0oT1oT2_sf3 : public T0oT1oT2_base_node<T>
  10191. {
  10192. public:
  10193. typedef typename details::functor_t<T> functor_t;
  10194. typedef typename functor_t::tfunc_t tfunc_t;
  10195. typedef T value_type;
  10196. typedef T0oT1oT2_sf3<T,T0,T1,T2> node_type;
  10197. T0oT1oT2_sf3(T0 p0, T1 p1, T2 p2, const tfunc_t p3)
  10198. : t0_(p0),
  10199. t1_(p1),
  10200. t2_(p2),
  10201. f_ (p3)
  10202. {}
  10203. inline typename expression_node<T>::node_type type() const
  10204. {
  10205. static const typename expression_node<T>::node_type result = nodetype_T0oT1oT2<T,T0,T1,T2>::result;
  10206. return result;
  10207. }
  10208. inline operator_type operation() const
  10209. {
  10210. return e_default;
  10211. }
  10212. inline T value() const
  10213. {
  10214. return f_(t0_,t1_,t2_);
  10215. }
  10216. inline T0 t0() const
  10217. {
  10218. return t0_;
  10219. }
  10220. inline T1 t1() const
  10221. {
  10222. return t1_;
  10223. }
  10224. inline T2 t2() const
  10225. {
  10226. return t2_;
  10227. }
  10228. tfunc_t f() const
  10229. {
  10230. return f_;
  10231. }
  10232. std::string type_id() const
  10233. {
  10234. return id();
  10235. }
  10236. static inline std::string id()
  10237. {
  10238. return "sf3";
  10239. }
  10240. template <typename Allocator>
  10241. static inline expression_node<T>* allocate(Allocator& allocator, T0 p0, T1 p1, T2 p2, tfunc_t p3)
  10242. {
  10243. return allocator.template allocate_type<node_type,T0,T1,T2,tfunc_t>(p0,p1,p2,p3);
  10244. }
  10245. private:
  10246. T0oT1oT2_sf3(node_type&) {}
  10247. node_type& operator=(node_type&) { return *this; }
  10248. T0 t0_;
  10249. T1 t1_;
  10250. T2 t2_;
  10251. const tfunc_t f_;
  10252. };
  10253. template <typename T, typename T0, typename T1, typename T2>
  10254. class sf3ext_type_node : public T0oT1oT2_base_node<T>
  10255. {
  10256. public:
  10257. virtual T0 t0() const = 0;
  10258. virtual T1 t1() const = 0;
  10259. virtual T2 t2() const = 0;
  10260. };
  10261. template <typename T, typename T0, typename T1, typename T2, typename SF3Operation>
  10262. class T0oT1oT2_sf3ext : public sf3ext_type_node<T,T0,T1,T2>
  10263. {
  10264. public:
  10265. typedef typename details::functor_t<T> functor_t;
  10266. typedef typename functor_t::tfunc_t tfunc_t;
  10267. typedef T value_type;
  10268. typedef T0oT1oT2_sf3ext<T,T0,T1,T2,SF3Operation> node_type;
  10269. T0oT1oT2_sf3ext(T0 p0, T1 p1, T2 p2)
  10270. : t0_(p0),
  10271. t1_(p1),
  10272. t2_(p2)
  10273. {}
  10274. inline typename expression_node<T>::node_type type() const
  10275. {
  10276. static const typename expression_node<T>::node_type result = nodetype_T0oT1oT2<T,T0,T1,T2>::result;
  10277. return result;
  10278. }
  10279. inline operator_type operation() const
  10280. {
  10281. return e_default;
  10282. }
  10283. inline T value() const
  10284. {
  10285. return SF3Operation::process(t0_,t1_,t2_);
  10286. }
  10287. T0 t0() const
  10288. {
  10289. return t0_;
  10290. }
  10291. T1 t1() const
  10292. {
  10293. return t1_;
  10294. }
  10295. T2 t2() const
  10296. {
  10297. return t2_;
  10298. }
  10299. std::string type_id() const
  10300. {
  10301. return id();
  10302. }
  10303. static inline std::string id()
  10304. {
  10305. return SF3Operation::id();
  10306. }
  10307. template <typename Allocator>
  10308. static inline expression_node<T>* allocate(Allocator& allocator, T0 p0, T1 p1, T2 p2)
  10309. {
  10310. return allocator.template allocate_type<node_type,T0,T1,T2>(p0,p1,p2);
  10311. }
  10312. private:
  10313. T0oT1oT2_sf3ext(node_type&) {}
  10314. node_type& operator=(node_type&) { return *this; }
  10315. T0 t0_;
  10316. T1 t1_;
  10317. T2 t2_;
  10318. };
  10319. template <typename T>
  10320. inline bool is_sf3ext_node(const expression_node<T>* n)
  10321. {
  10322. switch (n->type())
  10323. {
  10324. case expression_node<T>::e_vovov : return true;
  10325. case expression_node<T>::e_vovoc : return true;
  10326. case expression_node<T>::e_vocov : return true;
  10327. case expression_node<T>::e_covov : return true;
  10328. case expression_node<T>::e_covoc : return true;
  10329. default : return false;
  10330. }
  10331. }
  10332. template <typename T, typename T0, typename T1, typename T2, typename T3>
  10333. class T0oT1oT2oT3_sf4 : public T0oT1oT2_base_node<T>
  10334. {
  10335. public:
  10336. typedef typename details::functor_t<T> functor_t;
  10337. typedef typename functor_t::qfunc_t qfunc_t;
  10338. typedef T value_type;
  10339. typedef T0oT1oT2oT3_sf4<T,T0,T1,T2,T3> node_type;
  10340. T0oT1oT2oT3_sf4(T0 p0, T1 p1, T2 p2, T3 p3, const qfunc_t p4)
  10341. : t0_(p0),
  10342. t1_(p1),
  10343. t2_(p2),
  10344. t3_(p3),
  10345. f_ (p4)
  10346. {}
  10347. inline typename expression_node<T>::node_type type() const
  10348. {
  10349. static const typename expression_node<T>::node_type result = nodetype_T0oT1oT2oT3<T,T0,T1,T2,T3>::result;
  10350. return result;
  10351. }
  10352. inline operator_type operation() const
  10353. {
  10354. return e_default;
  10355. }
  10356. inline T value() const
  10357. {
  10358. return f_(t0_,t1_,t2_,t3_);
  10359. }
  10360. inline T0 t0() const
  10361. {
  10362. return t0_;
  10363. }
  10364. inline T1 t1() const
  10365. {
  10366. return t1_;
  10367. }
  10368. inline T2 t2() const
  10369. {
  10370. return t2_;
  10371. }
  10372. inline T3 t3() const
  10373. {
  10374. return t3_;
  10375. }
  10376. qfunc_t f() const
  10377. {
  10378. return f_;
  10379. }
  10380. std::string type_id() const
  10381. {
  10382. return id();
  10383. }
  10384. static inline std::string id()
  10385. {
  10386. return "sf4";
  10387. }
  10388. template <typename Allocator>
  10389. static inline expression_node<T>* allocate(Allocator& allocator, T0 p0, T1 p1, T2 p2, T3 p3, qfunc_t p4)
  10390. {
  10391. return allocator.template allocate_type<node_type,T0,T1,T2,T3,qfunc_t>(p0,p1,p2,p3,p4);
  10392. }
  10393. private:
  10394. T0oT1oT2oT3_sf4(node_type&) {}
  10395. node_type& operator=(node_type&) { return *this; }
  10396. T0 t0_;
  10397. T1 t1_;
  10398. T2 t2_;
  10399. T3 t3_;
  10400. const qfunc_t f_;
  10401. };
  10402. template <typename T, typename T0, typename T1, typename T2, typename T3, typename SF4Operation>
  10403. class T0oT1oT2oT3_sf4ext : public T0oT1oT2oT3_base_node<T>
  10404. {
  10405. public:
  10406. typedef typename details::functor_t<T> functor_t;
  10407. typedef typename functor_t::tfunc_t tfunc_t;
  10408. typedef T value_type;
  10409. typedef T0oT1oT2oT3_sf4ext<T,T0,T1,T2,T3,SF4Operation> node_type;
  10410. T0oT1oT2oT3_sf4ext(T0 p0, T1 p1, T2 p2, T3 p3)
  10411. : t0_(p0),
  10412. t1_(p1),
  10413. t2_(p2),
  10414. t3_(p3)
  10415. {}
  10416. inline typename expression_node<T>::node_type type() const
  10417. {
  10418. static const typename expression_node<T>::node_type result = nodetype_T0oT1oT2oT3<T,T0,T1,T2,T3>::result;
  10419. return result;
  10420. }
  10421. inline operator_type operation() const
  10422. {
  10423. return e_default;
  10424. }
  10425. inline T value() const
  10426. {
  10427. return SF4Operation::process(t0_,t1_,t2_,t3_);
  10428. }
  10429. inline T0 t0() const
  10430. {
  10431. return t0_;
  10432. }
  10433. inline T1 t1() const
  10434. {
  10435. return t1_;
  10436. }
  10437. inline T2 t2() const
  10438. {
  10439. return t2_;
  10440. }
  10441. inline T3 t3() const
  10442. {
  10443. return t2_;
  10444. }
  10445. std::string type_id() const
  10446. {
  10447. return id();
  10448. }
  10449. static inline std::string id()
  10450. {
  10451. return SF4Operation::id();
  10452. }
  10453. template <typename Allocator>
  10454. static inline expression_node<T>* allocate(Allocator& allocator, T0 p0, T1 p1, T2 p2, T3 p3)
  10455. {
  10456. return allocator.template allocate_type<node_type,T0,T1,T2,T3>(p0,p1,p2,p3);
  10457. }
  10458. private:
  10459. T0oT1oT2oT3_sf4ext(node_type&) {}
  10460. node_type& operator=(node_type&) { return *this; }
  10461. T0 t0_;
  10462. T1 t1_;
  10463. T2 t2_;
  10464. T3 t3_;
  10465. };
  10466. template <typename T>
  10467. inline bool is_sf4ext_node(const expression_node<T>* n)
  10468. {
  10469. switch (n->type())
  10470. {
  10471. case expression_node<T>::e_vovovov : return true;
  10472. case expression_node<T>::e_vovovoc : return true;
  10473. case expression_node<T>::e_vovocov : return true;
  10474. case expression_node<T>::e_vocovov : return true;
  10475. case expression_node<T>::e_covovov : return true;
  10476. case expression_node<T>::e_covocov : return true;
  10477. case expression_node<T>::e_vocovoc : return true;
  10478. case expression_node<T>::e_covovoc : return true;
  10479. case expression_node<T>::e_vococov : return true;
  10480. default : return false;
  10481. }
  10482. }
  10483. template <typename T, typename T0, typename T1>
  10484. struct T0oT1_define
  10485. {
  10486. typedef details::T0oT1<T,T0,T1> type0;
  10487. };
  10488. template <typename T, typename T0, typename T1, typename T2>
  10489. struct T0oT1oT2_define
  10490. {
  10491. typedef details::T0oT1oT2<T,T0,T1,T2,typename T0oT1oT2process<T>::mode0> type0;
  10492. typedef details::T0oT1oT2<T,T0,T1,T2,typename T0oT1oT2process<T>::mode1> type1;
  10493. typedef details::T0oT1oT2_sf3<T,T0,T1,T2> sf3_type;
  10494. typedef details::sf3ext_type_node<T,T0,T1,T2> sf3_type_node;
  10495. };
  10496. template <typename T, typename T0, typename T1, typename T2, typename T3>
  10497. struct T0oT1oT2oT3_define
  10498. {
  10499. typedef details::T0oT1oT2oT3<T,T0,T1,T2,T3,typename T0oT1oT20T3process<T>::mode0> type0;
  10500. typedef details::T0oT1oT2oT3<T,T0,T1,T2,T3,typename T0oT1oT20T3process<T>::mode1> type1;
  10501. typedef details::T0oT1oT2oT3<T,T0,T1,T2,T3,typename T0oT1oT20T3process<T>::mode2> type2;
  10502. typedef details::T0oT1oT2oT3<T,T0,T1,T2,T3,typename T0oT1oT20T3process<T>::mode3> type3;
  10503. typedef details::T0oT1oT2oT3<T,T0,T1,T2,T3,typename T0oT1oT20T3process<T>::mode4> type4;
  10504. typedef details::T0oT1oT2oT3_sf4<T,T0,T1,T2,T3> sf4_type;
  10505. };
  10506. template <typename T, typename Operation>
  10507. class vov_node : public vov_base_node<T>
  10508. {
  10509. public:
  10510. typedef expression_node<T>* expression_ptr;
  10511. typedef Operation operation_t;
  10512. // variable op variable node
  10513. explicit vov_node(const T& var0, const T& var1)
  10514. : v0_(var0),
  10515. v1_(var1)
  10516. {}
  10517. inline T value() const
  10518. {
  10519. return Operation::process(v0_,v1_);
  10520. }
  10521. inline typename expression_node<T>::node_type type() const
  10522. {
  10523. return Operation::type();
  10524. }
  10525. inline operator_type operation() const
  10526. {
  10527. return Operation::operation();
  10528. }
  10529. inline const T& v0() const
  10530. {
  10531. return v0_;
  10532. }
  10533. inline const T& v1() const
  10534. {
  10535. return v1_;
  10536. }
  10537. protected:
  10538. const T& v0_;
  10539. const T& v1_;
  10540. private:
  10541. vov_node(vov_node<T,Operation>&);
  10542. vov_node<T,Operation>& operator=(vov_node<T,Operation>&);
  10543. };
  10544. template <typename T, typename Operation>
  10545. class cov_node : public cov_base_node<T>
  10546. {
  10547. public:
  10548. typedef expression_node<T>* expression_ptr;
  10549. typedef Operation operation_t;
  10550. // constant op variable node
  10551. explicit cov_node(const T& const_var, const T& var)
  10552. : c_(const_var),
  10553. v_(var)
  10554. {}
  10555. inline T value() const
  10556. {
  10557. return Operation::process(c_,v_);
  10558. }
  10559. inline typename expression_node<T>::node_type type() const
  10560. {
  10561. return Operation::type();
  10562. }
  10563. inline operator_type operation() const
  10564. {
  10565. return Operation::operation();
  10566. }
  10567. inline const T c() const
  10568. {
  10569. return c_;
  10570. }
  10571. inline const T& v() const
  10572. {
  10573. return v_;
  10574. }
  10575. protected:
  10576. const T c_;
  10577. const T& v_;
  10578. private:
  10579. cov_node(const cov_node<T,Operation>&);
  10580. cov_node<T,Operation>& operator=(const cov_node<T,Operation>&);
  10581. };
  10582. template <typename T, typename Operation>
  10583. class voc_node : public voc_base_node<T>
  10584. {
  10585. public:
  10586. typedef expression_node<T>* expression_ptr;
  10587. typedef Operation operation_t;
  10588. // variable op constant node
  10589. explicit voc_node(const T& var, const T& const_var)
  10590. : v_(var),
  10591. c_(const_var)
  10592. {}
  10593. inline T value() const
  10594. {
  10595. return Operation::process(v_,c_);
  10596. }
  10597. inline operator_type operation() const
  10598. {
  10599. return Operation::operation();
  10600. }
  10601. inline const T c() const
  10602. {
  10603. return c_;
  10604. }
  10605. inline const T& v() const
  10606. {
  10607. return v_;
  10608. }
  10609. protected:
  10610. const T& v_;
  10611. const T c_;
  10612. private:
  10613. voc_node(const voc_node<T,Operation>&);
  10614. voc_node<T,Operation>& operator=(const voc_node<T,Operation>&);
  10615. };
  10616. template <typename T, typename Operation>
  10617. class vob_node : public vob_base_node<T>
  10618. {
  10619. public:
  10620. typedef expression_node<T>* expression_ptr;
  10621. typedef std::pair<expression_ptr,bool> branch_t;
  10622. typedef Operation operation_t;
  10623. // variable op constant node
  10624. explicit vob_node(const T& var, const expression_ptr brnch)
  10625. : v_(var)
  10626. {
  10627. init_branches<1>(branch_,brnch);
  10628. }
  10629. ~vob_node()
  10630. {
  10631. cleanup_branches::execute<T,1>(branch_);
  10632. }
  10633. inline T value() const
  10634. {
  10635. return Operation::process(v_,branch_[0].first->value());
  10636. }
  10637. inline operator_type operation() const
  10638. {
  10639. return Operation::operation();
  10640. }
  10641. inline const T& v() const
  10642. {
  10643. return v_;
  10644. }
  10645. inline expression_node<T>* branch(const std::size_t&) const
  10646. {
  10647. return branch_[0].first;
  10648. }
  10649. private:
  10650. vob_node(const vob_node<T,Operation>&);
  10651. vob_node<T,Operation>& operator=(const vob_node<T,Operation>&);
  10652. const T& v_;
  10653. branch_t branch_[1];
  10654. };
  10655. template <typename T, typename Operation>
  10656. class bov_node : public bov_base_node<T>
  10657. {
  10658. public:
  10659. typedef expression_node<T>* expression_ptr;
  10660. typedef std::pair<expression_ptr,bool> branch_t;
  10661. typedef Operation operation_t;
  10662. // variable op constant node
  10663. explicit bov_node(const expression_ptr brnch, const T& var)
  10664. : v_(var)
  10665. {
  10666. init_branches<1>(branch_,brnch);
  10667. }
  10668. ~bov_node()
  10669. {
  10670. cleanup_branches::execute<T,1>(branch_);
  10671. }
  10672. inline T value() const
  10673. {
  10674. return Operation::process(branch_[0].first->value(),v_);
  10675. }
  10676. inline operator_type operation() const
  10677. {
  10678. return Operation::operation();
  10679. }
  10680. inline const T& v() const
  10681. {
  10682. return v_;
  10683. }
  10684. inline expression_node<T>* branch(const std::size_t&) const
  10685. {
  10686. return branch_[0].first;
  10687. }
  10688. private:
  10689. bov_node(const bov_node<T,Operation>&);
  10690. bov_node<T,Operation>& operator=(const bov_node<T,Operation>&);
  10691. const T& v_;
  10692. branch_t branch_[1];
  10693. };
  10694. template <typename T, typename Operation>
  10695. class cob_node : public cob_base_node<T>
  10696. {
  10697. public:
  10698. typedef expression_node<T>* expression_ptr;
  10699. typedef std::pair<expression_ptr,bool> branch_t;
  10700. typedef Operation operation_t;
  10701. // variable op constant node
  10702. explicit cob_node(const T const_var, const expression_ptr brnch)
  10703. : c_(const_var)
  10704. {
  10705. init_branches<1>(branch_,brnch);
  10706. }
  10707. ~cob_node()
  10708. {
  10709. cleanup_branches::execute<T,1>(branch_);
  10710. }
  10711. inline T value() const
  10712. {
  10713. return Operation::process(c_,branch_[0].first->value());
  10714. }
  10715. inline operator_type operation() const
  10716. {
  10717. return Operation::operation();
  10718. }
  10719. inline const T c() const
  10720. {
  10721. return c_;
  10722. }
  10723. inline void set_c(const T new_c)
  10724. {
  10725. (*const_cast<T*>(&c_)) = new_c;
  10726. }
  10727. inline expression_node<T>* branch(const std::size_t&) const
  10728. {
  10729. return branch_[0].first;
  10730. }
  10731. inline expression_node<T>* move_branch(const std::size_t&)
  10732. {
  10733. branch_[0].second = false;
  10734. return branch_[0].first;
  10735. }
  10736. private:
  10737. cob_node(const cob_node<T,Operation>&);
  10738. cob_node<T,Operation>& operator=(const cob_node<T,Operation>&);
  10739. const T c_;
  10740. branch_t branch_[1];
  10741. };
  10742. template <typename T, typename Operation>
  10743. class boc_node : public boc_base_node<T>
  10744. {
  10745. public:
  10746. typedef expression_node<T>* expression_ptr;
  10747. typedef std::pair<expression_ptr,bool> branch_t;
  10748. typedef Operation operation_t;
  10749. // variable op constant node
  10750. explicit boc_node(const expression_ptr brnch, const T const_var)
  10751. : c_(const_var)
  10752. {
  10753. init_branches<1>(branch_,brnch);
  10754. }
  10755. ~boc_node()
  10756. {
  10757. cleanup_branches::execute<T,1>(branch_);
  10758. }
  10759. inline T value() const
  10760. {
  10761. return Operation::process(branch_[0].first->value(),c_);
  10762. }
  10763. inline operator_type operation() const
  10764. {
  10765. return Operation::operation();
  10766. }
  10767. inline const T c() const
  10768. {
  10769. return c_;
  10770. }
  10771. inline void set_c(const T new_c)
  10772. {
  10773. (*const_cast<T*>(&c_)) = new_c;
  10774. }
  10775. inline expression_node<T>* branch(const std::size_t&) const
  10776. {
  10777. return branch_[0].first;
  10778. }
  10779. inline expression_node<T>* move_branch(const std::size_t&)
  10780. {
  10781. branch_[0].second = false;
  10782. return branch_[0].first;
  10783. }
  10784. private:
  10785. boc_node(const boc_node<T,Operation>&);
  10786. boc_node<T,Operation>& operator=(const boc_node<T,Operation>&);
  10787. const T c_;
  10788. branch_t branch_[1];
  10789. };
  10790. #ifndef exprtk_disable_string_capabilities
  10791. template <typename T, typename SType0, typename SType1, typename Operation>
  10792. class sos_node : public sos_base_node<T>
  10793. {
  10794. public:
  10795. typedef expression_node<T>* expression_ptr;
  10796. typedef Operation operation_t;
  10797. // string op string node
  10798. explicit sos_node(SType0 p0, SType1 p1)
  10799. : s0_(p0),
  10800. s1_(p1)
  10801. {}
  10802. inline T value() const
  10803. {
  10804. return Operation::process(s0_,s1_);
  10805. }
  10806. inline typename expression_node<T>::node_type type() const
  10807. {
  10808. return Operation::type();
  10809. }
  10810. inline operator_type operation() const
  10811. {
  10812. return Operation::operation();
  10813. }
  10814. inline std::string& s0()
  10815. {
  10816. return s0_;
  10817. }
  10818. inline std::string& s1()
  10819. {
  10820. return s1_;
  10821. }
  10822. protected:
  10823. SType0 s0_;
  10824. SType1 s1_;
  10825. private:
  10826. sos_node(sos_node<T,SType0,SType1,Operation>&);
  10827. sos_node<T,SType0,SType1,Operation>& operator=(sos_node<T,SType0,SType1,Operation>&);
  10828. };
  10829. template <typename T, typename SType0, typename SType1, typename RangePack, typename Operation>
  10830. class str_xrox_node : public sos_base_node<T>
  10831. {
  10832. public:
  10833. typedef expression_node<T>* expression_ptr;
  10834. typedef Operation operation_t;
  10835. // string-range op string node
  10836. explicit str_xrox_node(SType0 p0, SType1 p1, RangePack rp0)
  10837. : s0_(p0),
  10838. s1_(p1),
  10839. rp0_(rp0)
  10840. {}
  10841. ~str_xrox_node()
  10842. {
  10843. rp0_.free();
  10844. }
  10845. inline T value() const
  10846. {
  10847. std::size_t r0 = 0;
  10848. std::size_t r1 = 0;
  10849. if (rp0_(r0,r1,s0_.size()))
  10850. return Operation::process(s0_.substr(r0,(r1 - r0) + 1),s1_);
  10851. else
  10852. return T(0);
  10853. }
  10854. inline typename expression_node<T>::node_type type() const
  10855. {
  10856. return Operation::type();
  10857. }
  10858. inline operator_type operation() const
  10859. {
  10860. return Operation::operation();
  10861. }
  10862. inline std::string& s0()
  10863. {
  10864. return s0_;
  10865. }
  10866. inline std::string& s1()
  10867. {
  10868. return s1_;
  10869. }
  10870. protected:
  10871. SType0 s0_;
  10872. SType1 s1_;
  10873. RangePack rp0_;
  10874. private:
  10875. str_xrox_node(str_xrox_node<T,SType0,SType1,RangePack,Operation>&);
  10876. str_xrox_node<T,SType0,SType1,RangePack,Operation>& operator=(str_xrox_node<T,SType0,SType1,RangePack,Operation>&);
  10877. };
  10878. template <typename T, typename SType0, typename SType1, typename RangePack, typename Operation>
  10879. class str_xoxr_node : public sos_base_node<T>
  10880. {
  10881. public:
  10882. typedef expression_node<T>* expression_ptr;
  10883. typedef Operation operation_t;
  10884. // string op string range node
  10885. explicit str_xoxr_node(SType0 p0, SType1 p1, RangePack rp1)
  10886. : s0_ (p0 ),
  10887. s1_ (p1 ),
  10888. rp1_(rp1)
  10889. {}
  10890. ~str_xoxr_node()
  10891. {
  10892. rp1_.free();
  10893. }
  10894. inline T value() const
  10895. {
  10896. std::size_t r0 = 0;
  10897. std::size_t r1 = 0;
  10898. if (rp1_(r0,r1,s1_.size()))
  10899. return Operation::process(s0_,s1_.substr(r0,(r1 - r0) + 1));
  10900. else
  10901. return T(0);
  10902. }
  10903. inline typename expression_node<T>::node_type type() const
  10904. {
  10905. return Operation::type();
  10906. }
  10907. inline operator_type operation() const
  10908. {
  10909. return Operation::operation();
  10910. }
  10911. inline std::string& s0()
  10912. {
  10913. return s0_;
  10914. }
  10915. inline std::string& s1()
  10916. {
  10917. return s1_;
  10918. }
  10919. protected:
  10920. SType0 s0_;
  10921. SType1 s1_;
  10922. RangePack rp1_;
  10923. private:
  10924. str_xoxr_node(str_xoxr_node<T,SType0,SType1,RangePack,Operation>&);
  10925. str_xoxr_node<T,SType0,SType1,RangePack,Operation>& operator=(str_xoxr_node<T,SType0,SType1,RangePack,Operation>&);
  10926. };
  10927. template <typename T, typename SType0, typename SType1, typename RangePack, typename Operation>
  10928. class str_xroxr_node : public sos_base_node<T>
  10929. {
  10930. public:
  10931. typedef expression_node<T>* expression_ptr;
  10932. typedef Operation operation_t;
  10933. // string-range op string-range node
  10934. explicit str_xroxr_node(SType0 p0, SType1 p1, RangePack rp0, RangePack rp1)
  10935. : s0_ (p0 ),
  10936. s1_ (p1 ),
  10937. rp0_(rp0),
  10938. rp1_(rp1)
  10939. {}
  10940. ~str_xroxr_node()
  10941. {
  10942. rp0_.free();
  10943. rp1_.free();
  10944. }
  10945. inline T value() const
  10946. {
  10947. std::size_t r0_0 = 0;
  10948. std::size_t r0_1 = 0;
  10949. std::size_t r1_0 = 0;
  10950. std::size_t r1_1 = 0;
  10951. if (
  10952. rp0_(r0_0,r1_0,s0_.size()) &&
  10953. rp1_(r0_1,r1_1,s1_.size())
  10954. )
  10955. {
  10956. return Operation::process(
  10957. s0_.substr(r0_0,(r1_0 - r0_0) + 1),
  10958. s1_.substr(r0_1,(r1_1 - r0_1) + 1)
  10959. );
  10960. }
  10961. else
  10962. return T(0);
  10963. }
  10964. inline typename expression_node<T>::node_type type() const
  10965. {
  10966. return Operation::type();
  10967. }
  10968. inline operator_type operation() const
  10969. {
  10970. return Operation::operation();
  10971. }
  10972. inline std::string& s0()
  10973. {
  10974. return s0_;
  10975. }
  10976. inline std::string& s1()
  10977. {
  10978. return s1_;
  10979. }
  10980. protected:
  10981. SType0 s0_;
  10982. SType1 s1_;
  10983. RangePack rp0_;
  10984. RangePack rp1_;
  10985. private:
  10986. str_xroxr_node(str_xroxr_node<T,SType0,SType1,RangePack,Operation>&);
  10987. str_xroxr_node<T,SType0,SType1,RangePack,Operation>& operator=(str_xroxr_node<T,SType0,SType1,RangePack,Operation>&);
  10988. };
  10989. template <typename T, typename Operation>
  10990. class str_sogens_node : public binary_node<T>
  10991. {
  10992. public:
  10993. typedef expression_node <T>* expression_ptr;
  10994. typedef string_base_node<T>* str_base_ptr;
  10995. typedef range_pack <T> range_t;
  10996. typedef range_t* range_ptr;
  10997. typedef range_interface<T> irange_t;
  10998. typedef irange_t* irange_ptr;
  10999. str_sogens_node(const operator_type& opr,
  11000. expression_ptr branch0,
  11001. expression_ptr branch1)
  11002. : binary_node<T>(opr,branch0,branch1),
  11003. str0_base_ptr_ (0),
  11004. str1_base_ptr_ (0),
  11005. str0_range_ptr_(0),
  11006. str1_range_ptr_(0)
  11007. {
  11008. if (is_generally_string_node(binary_node<T>::branch_[0].first))
  11009. {
  11010. str0_base_ptr_ = dynamic_cast<str_base_ptr>(binary_node<T>::branch_[0].first);
  11011. if (0 == str0_base_ptr_)
  11012. return;
  11013. irange_ptr range_ptr = dynamic_cast<irange_ptr>(binary_node<T>::branch_[0].first);
  11014. if (0 == range_ptr)
  11015. return;
  11016. str0_range_ptr_ = &(range_ptr->range_ref());
  11017. }
  11018. if (is_generally_string_node(binary_node<T>::branch_[1].first))
  11019. {
  11020. str1_base_ptr_ = dynamic_cast<str_base_ptr>(binary_node<T>::branch_[1].first);
  11021. if (0 == str1_base_ptr_)
  11022. return;
  11023. irange_ptr range_ptr = dynamic_cast<irange_ptr>(binary_node<T>::branch_[1].first);
  11024. if (0 == range_ptr)
  11025. return;
  11026. str1_range_ptr_ = &(range_ptr->range_ref());
  11027. }
  11028. }
  11029. inline T value() const
  11030. {
  11031. if (
  11032. str0_base_ptr_ &&
  11033. str1_base_ptr_ &&
  11034. str0_range_ptr_ &&
  11035. str1_range_ptr_
  11036. )
  11037. {
  11038. binary_node<T>::branch_[0].first->value();
  11039. binary_node<T>::branch_[1].first->value();
  11040. std::size_t str0_r0 = 0;
  11041. std::size_t str0_r1 = 0;
  11042. std::size_t str1_r0 = 0;
  11043. std::size_t str1_r1 = 0;
  11044. range_t& range0 = (*str0_range_ptr_);
  11045. range_t& range1 = (*str1_range_ptr_);
  11046. if (
  11047. range0(str0_r0,str0_r1,str0_base_ptr_->size()) &&
  11048. range1(str1_r0,str1_r1,str1_base_ptr_->size())
  11049. )
  11050. {
  11051. return Operation::process(
  11052. str0_base_ptr_->str().substr(str0_r0,(str0_r1 - str0_r0) + 1),
  11053. str1_base_ptr_->str().substr(str1_r0,(str1_r1 - str1_r0) + 1)
  11054. );
  11055. }
  11056. }
  11057. return std::numeric_limits<T>::quiet_NaN();
  11058. }
  11059. inline typename expression_node<T>::node_type type() const
  11060. {
  11061. return Operation::type();
  11062. }
  11063. inline operator_type operation() const
  11064. {
  11065. return Operation::operation();
  11066. }
  11067. private:
  11068. str_sogens_node(str_sogens_node<T,Operation>&);
  11069. str_sogens_node<T,Operation>& operator=(str_sogens_node<T,Operation>&);
  11070. str_base_ptr str0_base_ptr_;
  11071. str_base_ptr str1_base_ptr_;
  11072. range_ptr str0_range_ptr_;
  11073. range_ptr str1_range_ptr_;
  11074. };
  11075. template <typename T, typename SType0, typename SType1, typename SType2, typename Operation>
  11076. class sosos_node : public sosos_base_node<T>
  11077. {
  11078. public:
  11079. typedef expression_node<T>* expression_ptr;
  11080. typedef Operation operation_t;
  11081. // variable op variable node
  11082. explicit sosos_node(SType0 p0, SType1 p1, SType2 p2)
  11083. : s0_(p0),
  11084. s1_(p1),
  11085. s2_(p2)
  11086. {}
  11087. inline T value() const
  11088. {
  11089. return Operation::process(s0_,s1_,s2_);
  11090. }
  11091. inline typename expression_node<T>::node_type type() const
  11092. {
  11093. return Operation::type();
  11094. }
  11095. inline operator_type operation() const
  11096. {
  11097. return Operation::operation();
  11098. }
  11099. inline std::string& s0()
  11100. {
  11101. return s0_;
  11102. }
  11103. inline std::string& s1()
  11104. {
  11105. return s1_;
  11106. }
  11107. inline std::string& s2()
  11108. {
  11109. return s2_;
  11110. }
  11111. protected:
  11112. SType0 s0_;
  11113. SType1 s1_;
  11114. SType2 s2_;
  11115. private:
  11116. sosos_node(sosos_node<T,SType0,SType1,SType2,Operation>&);
  11117. sosos_node<T,SType0,SType1,SType2,Operation>& operator=(sosos_node<T,SType0,SType1,SType2,Operation>&);
  11118. };
  11119. #endif
  11120. template <typename T, typename PowOp>
  11121. class ipow_node : public expression_node<T>
  11122. {
  11123. public:
  11124. typedef expression_node<T>* expression_ptr;
  11125. typedef PowOp operation_t;
  11126. explicit ipow_node(const T& v)
  11127. : v_(v)
  11128. {}
  11129. inline T value() const
  11130. {
  11131. return PowOp::result(v_);
  11132. }
  11133. inline typename expression_node<T>::node_type type() const
  11134. {
  11135. return expression_node<T>::e_ipow;
  11136. }
  11137. private:
  11138. ipow_node(const ipow_node<T,PowOp>&);
  11139. ipow_node<T,PowOp>& operator=(const ipow_node<T,PowOp>&);
  11140. const T& v_;
  11141. };
  11142. template <typename T, typename PowOp>
  11143. class ipowinv_node : public expression_node<T>
  11144. {
  11145. public:
  11146. typedef expression_node<T>* expression_ptr;
  11147. typedef PowOp operation_t;
  11148. explicit ipowinv_node(const T& v)
  11149. : v_(v)
  11150. {}
  11151. inline T value() const
  11152. {
  11153. return (T(1) / PowOp::result(v_));
  11154. }
  11155. inline typename expression_node<T>::node_type type() const
  11156. {
  11157. return expression_node<T>::e_ipowinv;
  11158. }
  11159. private:
  11160. ipowinv_node(const ipowinv_node<T,PowOp>&);
  11161. ipowinv_node<T,PowOp>& operator=(const ipowinv_node<T,PowOp>&);
  11162. const T& v_;
  11163. };
  11164. template <typename T>
  11165. inline bool is_vov_node(const expression_node<T>* node)
  11166. {
  11167. return (0 != dynamic_cast<const vov_base_node<T>*>(node));
  11168. }
  11169. template <typename T>
  11170. inline bool is_cov_node(const expression_node<T>* node)
  11171. {
  11172. return (0 != dynamic_cast<const cov_base_node<T>*>(node));
  11173. }
  11174. template <typename T>
  11175. inline bool is_voc_node(const expression_node<T>* node)
  11176. {
  11177. return (0 != dynamic_cast<const voc_base_node<T>*>(node));
  11178. }
  11179. template <typename T>
  11180. inline bool is_cob_node(const expression_node<T>* node)
  11181. {
  11182. return (0 != dynamic_cast<const cob_base_node<T>*>(node));
  11183. }
  11184. template <typename T>
  11185. inline bool is_boc_node(const expression_node<T>* node)
  11186. {
  11187. return (0 != dynamic_cast<const boc_base_node<T>*>(node));
  11188. }
  11189. template <typename T>
  11190. inline bool is_t0ot1ot2_node(const expression_node<T>* node)
  11191. {
  11192. return (0 != dynamic_cast<const T0oT1oT2_base_node<T>*>(node));
  11193. }
  11194. template <typename T>
  11195. inline bool is_t0ot1ot2ot3_node(const expression_node<T>* node)
  11196. {
  11197. return (0 != dynamic_cast<const T0oT1oT2oT3_base_node<T>*>(node));
  11198. }
  11199. template <typename T>
  11200. inline bool is_uv_node(const expression_node<T>* node)
  11201. {
  11202. return (0 != dynamic_cast<const uv_base_node<T>*>(node));
  11203. }
  11204. template <typename T>
  11205. inline bool is_string_node(const expression_node<T>* node)
  11206. {
  11207. return node && (expression_node<T>::e_stringvar == node->type());
  11208. }
  11209. template <typename T>
  11210. inline bool is_string_range_node(const expression_node<T>* node)
  11211. {
  11212. return node && (expression_node<T>::e_stringvarrng == node->type());
  11213. }
  11214. template <typename T>
  11215. inline bool is_const_string_node(const expression_node<T>* node)
  11216. {
  11217. return node && (expression_node<T>::e_stringconst == node->type());
  11218. }
  11219. template <typename T>
  11220. inline bool is_const_string_range_node(const expression_node<T>* node)
  11221. {
  11222. return node && (expression_node<T>::e_cstringvarrng == node->type());
  11223. }
  11224. template <typename T>
  11225. inline bool is_string_assignment_node(const expression_node<T>* node)
  11226. {
  11227. return node && (expression_node<T>::e_strass == node->type());
  11228. }
  11229. template <typename T>
  11230. inline bool is_string_concat_node(const expression_node<T>* node)
  11231. {
  11232. return node && (expression_node<T>::e_strconcat == node->type());
  11233. }
  11234. template <typename T>
  11235. inline bool is_string_function_node(const expression_node<T>* node)
  11236. {
  11237. return node && (expression_node<T>::e_strfunction == node->type());
  11238. }
  11239. template <typename T>
  11240. inline bool is_string_condition_node(const expression_node<T>* node)
  11241. {
  11242. return node && (expression_node<T>::e_strcondition == node->type());
  11243. }
  11244. template <typename T>
  11245. inline bool is_string_ccondition_node(const expression_node<T>* node)
  11246. {
  11247. return node && (expression_node<T>::e_strccondition == node->type());
  11248. }
  11249. template <typename T>
  11250. inline bool is_genricstring_range_node(const expression_node<T>* node)
  11251. {
  11252. return node && (expression_node<T>::e_strgenrange == node->type());
  11253. }
  11254. template <typename T>
  11255. inline bool is_generally_string_node(const expression_node<T>* node)
  11256. {
  11257. if (node)
  11258. {
  11259. switch (node->type())
  11260. {
  11261. case expression_node<T>::e_stringvar :
  11262. case expression_node<T>::e_stringconst :
  11263. case expression_node<T>::e_stringvarrng :
  11264. case expression_node<T>::e_cstringvarrng :
  11265. case expression_node<T>::e_strgenrange :
  11266. case expression_node<T>::e_strass :
  11267. case expression_node<T>::e_strconcat :
  11268. case expression_node<T>::e_strfunction :
  11269. case expression_node<T>::e_strcondition :
  11270. case expression_node<T>::e_strccondition : return true;
  11271. default : return false;
  11272. }
  11273. }
  11274. return false;
  11275. }
  11276. class node_allocator
  11277. {
  11278. public:
  11279. template <typename ResultNode, typename OpType, typename ExprNode>
  11280. inline expression_node<typename ResultNode::value_type>* allocate(OpType& operation, ExprNode (&branch)[1])
  11281. {
  11282. return allocate<ResultNode>(operation,branch[0]);
  11283. }
  11284. template <typename ResultNode, typename OpType, typename ExprNode>
  11285. inline expression_node<typename ResultNode::value_type>* allocate(OpType& operation, ExprNode (&branch)[2])
  11286. {
  11287. return allocate<ResultNode>(operation,branch[0],branch[1]);
  11288. }
  11289. template <typename ResultNode, typename OpType, typename ExprNode>
  11290. inline expression_node<typename ResultNode::value_type>* allocate(OpType& operation, ExprNode (&branch)[3])
  11291. {
  11292. return allocate<ResultNode>(operation,branch[0],branch[1],branch[2]);
  11293. }
  11294. template <typename ResultNode, typename OpType, typename ExprNode>
  11295. inline expression_node<typename ResultNode::value_type>* allocate(OpType& operation, ExprNode (&branch)[4])
  11296. {
  11297. return allocate<ResultNode>(operation,branch[0],branch[1],branch[2],branch[3]);
  11298. }
  11299. template <typename ResultNode, typename OpType, typename ExprNode>
  11300. inline expression_node<typename ResultNode::value_type>* allocate(OpType& operation, ExprNode (&branch)[5])
  11301. {
  11302. return allocate<ResultNode>(operation,branch[0],branch[1],branch[2],branch[3],branch[4]);
  11303. }
  11304. template <typename ResultNode, typename OpType, typename ExprNode>
  11305. inline expression_node<typename ResultNode::value_type>* allocate(OpType& operation, ExprNode (&branch)[6])
  11306. {
  11307. return allocate<ResultNode>(operation,branch[0],branch[1],branch[2],branch[3],branch[4],branch[5]);
  11308. }
  11309. template <typename node_type>
  11310. inline expression_node<typename node_type::value_type>* allocate() const
  11311. {
  11312. return new node_type();
  11313. }
  11314. template <typename node_type,
  11315. typename Type,
  11316. typename Allocator,
  11317. template <typename,typename> class Sequence>
  11318. inline expression_node<typename node_type::value_type>* allocate(const Sequence<Type,Allocator>& seq) const
  11319. {
  11320. return new node_type(seq);
  11321. }
  11322. template <typename node_type, typename T1>
  11323. inline expression_node<typename node_type::value_type>* allocate(T1& t1) const
  11324. {
  11325. return new node_type(t1);
  11326. }
  11327. template <typename node_type, typename T1>
  11328. inline expression_node<typename node_type::value_type>* allocate_c(const T1& t1) const
  11329. {
  11330. return new node_type(t1);
  11331. }
  11332. template <typename node_type,
  11333. typename T1, typename T2>
  11334. inline expression_node<typename node_type::value_type>* allocate(const T1& t1, const T2& t2) const
  11335. {
  11336. return new node_type(t1,t2);
  11337. }
  11338. template <typename node_type,
  11339. typename T1, typename T2>
  11340. inline expression_node<typename node_type::value_type>* allocate_cr(const T1& t1, T2& t2) const
  11341. {
  11342. return new node_type(t1,t2);
  11343. }
  11344. template <typename node_type,
  11345. typename T1, typename T2>
  11346. inline expression_node<typename node_type::value_type>* allocate_rc(T1& t1, const T2& t2) const
  11347. {
  11348. return new node_type(t1,t2);
  11349. }
  11350. template <typename node_type,
  11351. typename T1, typename T2>
  11352. inline expression_node<typename node_type::value_type>* allocate_rr(T1& t1, T2& t2) const
  11353. {
  11354. return new node_type(t1,t2);
  11355. }
  11356. template <typename node_type,
  11357. typename T1, typename T2>
  11358. inline expression_node<typename node_type::value_type>* allocate_tt(T1 t1, T2 t2) const
  11359. {
  11360. return new node_type(t1,t2);
  11361. }
  11362. template <typename node_type,
  11363. typename T1, typename T2, typename T3>
  11364. inline expression_node<typename node_type::value_type>* allocate_ttt(T1 t1, T2 t2, T3 t3) const
  11365. {
  11366. return new node_type(t1,t2,t3);
  11367. }
  11368. template <typename node_type,
  11369. typename T1, typename T2, typename T3, typename T4>
  11370. inline expression_node<typename node_type::value_type>* allocate_tttt(T1 t1, T2 t2, T3 t3, T4 t4) const
  11371. {
  11372. return new node_type(t1,t2,t3,t4);
  11373. }
  11374. template <typename node_type,
  11375. typename T1, typename T2, typename T3>
  11376. inline expression_node<typename node_type::value_type>* allocate_rrr(T1& t1, T2& t2, T3& t3) const
  11377. {
  11378. return new node_type(t1,t2,t3);
  11379. }
  11380. template <typename node_type,
  11381. typename T1, typename T2, typename T3, typename T4>
  11382. inline expression_node<typename node_type::value_type>* allocate_rrrr(T1& t1, T2& t2, T3& t3, T4& t4) const
  11383. {
  11384. return new node_type(t1,t2,t3,t4);
  11385. }
  11386. template <typename node_type,
  11387. typename T1, typename T2, typename T3, typename T4, typename T5>
  11388. inline expression_node<typename node_type::value_type>* allocate_rrrrr(T1& t1, T2& t2, T3& t3, T4& t4, T5& t5) const
  11389. {
  11390. return new node_type(t1,t2,t3,t4,t5);
  11391. }
  11392. template <typename node_type,
  11393. typename T1, typename T2, typename T3>
  11394. inline expression_node<typename node_type::value_type>* allocate(const T1& t1, const T2& t2,
  11395. const T3& t3) const
  11396. {
  11397. return new node_type(t1,t2,t3);
  11398. }
  11399. template <typename node_type,
  11400. typename T1, typename T2,
  11401. typename T3, typename T4>
  11402. inline expression_node<typename node_type::value_type>* allocate(const T1& t1, const T2& t2,
  11403. const T3& t3, const T4& t4) const
  11404. {
  11405. return new node_type(t1,t2,t3,t4);
  11406. }
  11407. template <typename node_type,
  11408. typename T1, typename T2,
  11409. typename T3, typename T4, typename T5>
  11410. inline expression_node<typename node_type::value_type>* allocate(const T1& t1, const T2& t2,
  11411. const T3& t3, const T4& t4,
  11412. const T5& t5) const
  11413. {
  11414. return new node_type(t1,t2,t3,t4,t5);
  11415. }
  11416. template <typename node_type,
  11417. typename T1, typename T2,
  11418. typename T3, typename T4, typename T5, typename T6>
  11419. inline expression_node<typename node_type::value_type>* allocate(const T1& t1, const T2& t2,
  11420. const T3& t3, const T4& t4,
  11421. const T5& t5, const T6& t6) const
  11422. {
  11423. return new node_type(t1,t2,t3,t4,t5,t6);
  11424. }
  11425. template <typename node_type,
  11426. typename T1, typename T2,
  11427. typename T3, typename T4,
  11428. typename T5, typename T6, typename T7>
  11429. inline expression_node<typename node_type::value_type>* allocate(const T1& t1, const T2& t2,
  11430. const T3& t3, const T4& t4,
  11431. const T5& t5, const T6& t6,
  11432. const T7& t7) const
  11433. {
  11434. return new node_type(t1,t2,t3,t4,t5,t6,t7);
  11435. }
  11436. template <typename node_type,
  11437. typename T1, typename T2,
  11438. typename T3, typename T4,
  11439. typename T5, typename T6,
  11440. typename T7, typename T8>
  11441. inline expression_node<typename node_type::value_type>* allocate(const T1& t1, const T2& t2,
  11442. const T3& t3, const T4& t4,
  11443. const T5& t5, const T6& t6,
  11444. const T7& t7, const T8& t8) const
  11445. {
  11446. return new node_type(t1,t2,t3,t4,t5,t6,t7,t8);
  11447. }
  11448. template <typename node_type,
  11449. typename T1, typename T2,
  11450. typename T3, typename T4,
  11451. typename T5, typename T6,
  11452. typename T7, typename T8, typename T9>
  11453. inline expression_node<typename node_type::value_type>* allocate(const T1& t1, const T2& t2,
  11454. const T3& t3, const T4& t4,
  11455. const T5& t5, const T6& t6,
  11456. const T7& t7, const T8& t8,
  11457. const T9& t9) const
  11458. {
  11459. return new node_type(t1,t2,t3,t4,t5,t6,t7,t8,t9);
  11460. }
  11461. template <typename node_type,
  11462. typename T1, typename T2,
  11463. typename T3, typename T4,
  11464. typename T5, typename T6,
  11465. typename T7, typename T8,
  11466. typename T9, typename T10>
  11467. inline expression_node<typename node_type::value_type>* allocate(const T1& t1, const T2& t2,
  11468. const T3& t3, const T4& t4,
  11469. const T5& t5, const T6& t6,
  11470. const T7& t7, const T8& t8,
  11471. const T9& t9, const T10& t10) const
  11472. {
  11473. return new node_type(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10);
  11474. }
  11475. template <typename node_type,
  11476. typename T1, typename T2, typename T3>
  11477. inline expression_node<typename node_type::value_type>* allocate_type(T1 t1, T2 t2, T3 t3) const
  11478. {
  11479. return new node_type(t1,t2,t3);
  11480. }
  11481. template <typename node_type,
  11482. typename T1, typename T2,
  11483. typename T3, typename T4>
  11484. inline expression_node<typename node_type::value_type>* allocate_type(T1 t1, T2 t2,
  11485. T3 t3, T4 t4) const
  11486. {
  11487. return new node_type(t1,t2,t3,t4);
  11488. }
  11489. template <typename node_type,
  11490. typename T1, typename T2,
  11491. typename T3, typename T4,
  11492. typename T5>
  11493. inline expression_node<typename node_type::value_type>* allocate_type(T1 t1, T2 t2,
  11494. T3 t3, T4 t4,
  11495. T5 t5) const
  11496. {
  11497. return new node_type(t1,t2,t3,t4,t5);
  11498. }
  11499. template <typename node_type,
  11500. typename T1, typename T2,
  11501. typename T3, typename T4,
  11502. typename T5, typename T6, typename T7>
  11503. inline expression_node<typename node_type::value_type>* allocate_type(T1 t1, T2 t2,
  11504. T3 t3, T4 t4,
  11505. T5 t5, T6 t6,
  11506. T7 t7) const
  11507. {
  11508. return new node_type(t1,t2,t3,t4,t5,t6,t7);
  11509. }
  11510. template <typename T>
  11511. void inline free(expression_node<T>*& e) const
  11512. {
  11513. delete e;
  11514. e = 0;
  11515. }
  11516. };
  11517. inline void load_operations_map(std::multimap<std::string,details::base_operation_t,details::ilesscompare>& m)
  11518. {
  11519. #define register_op(Symbol,Type,Args) \
  11520. m.insert(std::make_pair(std::string(Symbol),details::base_operation_t(Type,Args))); \
  11521. register_op( "abs",e_abs , 1)
  11522. register_op( "acos",e_acos , 1)
  11523. register_op( "acosh",e_acosh , 1)
  11524. register_op( "asin",e_asin , 1)
  11525. register_op( "asinh",e_asinh , 1)
  11526. register_op( "atan",e_atan , 1)
  11527. register_op( "atanh",e_atanh , 1)
  11528. register_op( "ceil",e_ceil , 1)
  11529. register_op( "cos",e_cos , 1)
  11530. register_op( "cosh",e_cosh , 1)
  11531. register_op( "exp",e_exp , 1)
  11532. register_op( "expm1",e_expm1 , 1)
  11533. register_op( "floor",e_floor , 1)
  11534. register_op( "log",e_log , 1)
  11535. register_op( "log10",e_log10 , 1)
  11536. register_op( "log2",e_log2 , 1)
  11537. register_op( "log1p",e_log1p , 1)
  11538. register_op( "round",e_round , 1)
  11539. register_op( "sin",e_sin , 1)
  11540. register_op( "sinc",e_sinc , 1)
  11541. register_op( "sinh",e_sinh , 1)
  11542. register_op( "sec",e_sec , 1)
  11543. register_op( "csc",e_csc , 1)
  11544. register_op( "sqrt",e_sqrt , 1)
  11545. register_op( "tan",e_tan , 1)
  11546. register_op( "tanh",e_tanh , 1)
  11547. register_op( "cot",e_cot , 1)
  11548. register_op( "rad2deg",e_r2d , 1)
  11549. register_op( "deg2rad",e_d2r , 1)
  11550. register_op( "deg2grad",e_d2g , 1)
  11551. register_op( "grad2deg",e_g2d , 1)
  11552. register_op( "sgn",e_sgn , 1)
  11553. register_op( "not",e_notl , 1)
  11554. register_op( "erf",e_erf , 1)
  11555. register_op( "erfc",e_erfc , 1)
  11556. register_op( "ncdf",e_ncdf , 1)
  11557. register_op( "frac",e_frac , 1)
  11558. register_op( "trunc",e_trunc , 1)
  11559. register_op( "atan2",e_atan2 , 2)
  11560. register_op( "mod",e_mod , 2)
  11561. register_op( "logn",e_logn , 2)
  11562. register_op( "pow",e_pow , 2)
  11563. register_op( "root",e_root , 2)
  11564. register_op( "roundn",e_roundn , 2)
  11565. register_op( "equal",e_equal , 2)
  11566. register_op("not_equal",e_nequal , 2)
  11567. register_op( "hypot",e_hypot , 2)
  11568. register_op( "shr",e_shr , 2)
  11569. register_op( "shl",e_shl , 2)
  11570. register_op( "clamp",e_clamp , 3)
  11571. register_op( "iclamp",e_iclamp , 3)
  11572. register_op( "inrange",e_inrange , 3)
  11573. #undef register_op
  11574. }
  11575. } // namespace details
  11576. class function_traits
  11577. {
  11578. public:
  11579. function_traits()
  11580. : allow_zero_parameters_(false),
  11581. has_side_effects_(true)
  11582. {}
  11583. inline bool& allow_zero_parameters()
  11584. {
  11585. return allow_zero_parameters_;
  11586. }
  11587. inline bool& has_side_effects()
  11588. {
  11589. return has_side_effects_;
  11590. }
  11591. private:
  11592. bool allow_zero_parameters_;
  11593. bool has_side_effects_;
  11594. };
  11595. template <typename FunctionType>
  11596. void enable_zero_parameters(FunctionType& func)
  11597. {
  11598. func.allow_zero_parameters() = true;
  11599. }
  11600. template <typename FunctionType>
  11601. void disable_zero_parameters(FunctionType& func)
  11602. {
  11603. func.allow_zero_parameters() = false;
  11604. }
  11605. template <typename FunctionType>
  11606. void enable_has_side_effects(FunctionType& func)
  11607. {
  11608. func.has_side_effects() = true;
  11609. }
  11610. template <typename FunctionType>
  11611. void disable_has_side_effects(FunctionType& func)
  11612. {
  11613. func.has_side_effects() = false;
  11614. }
  11615. template <typename T>
  11616. class ifunction : public function_traits
  11617. {
  11618. public:
  11619. explicit ifunction(const std::size_t& pc)
  11620. : param_count(pc)
  11621. {}
  11622. virtual ~ifunction()
  11623. {}
  11624. inline virtual T operator()()
  11625. {
  11626. return std::numeric_limits<T>::quiet_NaN();
  11627. }
  11628. inline virtual T operator()(const T&)
  11629. {
  11630. return std::numeric_limits<T>::quiet_NaN();
  11631. }
  11632. inline virtual T operator()(const T&,const T&)
  11633. {
  11634. return std::numeric_limits<T>::quiet_NaN();
  11635. }
  11636. inline virtual T operator()(const T&, const T&, const T&)
  11637. {
  11638. return std::numeric_limits<T>::quiet_NaN();
  11639. }
  11640. inline virtual T operator()(const T&, const T&, const T&, const T&)
  11641. {
  11642. return std::numeric_limits<T>::quiet_NaN();
  11643. }
  11644. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&)
  11645. {
  11646. return std::numeric_limits<T>::quiet_NaN();
  11647. }
  11648. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&)
  11649. {
  11650. return std::numeric_limits<T>::quiet_NaN();
  11651. }
  11652. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&)
  11653. {
  11654. return std::numeric_limits<T>::quiet_NaN();
  11655. }
  11656. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&)
  11657. {
  11658. return std::numeric_limits<T>::quiet_NaN();
  11659. }
  11660. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&)
  11661. {
  11662. return std::numeric_limits<T>::quiet_NaN();
  11663. }
  11664. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&)
  11665. {
  11666. return std::numeric_limits<T>::quiet_NaN();
  11667. }
  11668. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11669. const T&)
  11670. {
  11671. return std::numeric_limits<T>::quiet_NaN();
  11672. }
  11673. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11674. const T&, const T&)
  11675. {
  11676. return std::numeric_limits<T>::quiet_NaN();
  11677. }
  11678. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11679. const T&, const T&, const T&)
  11680. {
  11681. return std::numeric_limits<T>::quiet_NaN();
  11682. }
  11683. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11684. const T&, const T&, const T&, const T&)
  11685. {
  11686. return std::numeric_limits<T>::quiet_NaN();
  11687. }
  11688. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11689. const T&, const T&, const T&, const T&, const T&)
  11690. {
  11691. return std::numeric_limits<T>::quiet_NaN();
  11692. }
  11693. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11694. const T&, const T&, const T&, const T&, const T&, const T&)
  11695. {
  11696. return std::numeric_limits<T>::quiet_NaN();
  11697. }
  11698. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11699. const T&, const T&, const T&, const T&, const T&, const T&, const T&)
  11700. {
  11701. return std::numeric_limits<T>::quiet_NaN();
  11702. }
  11703. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11704. const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&)
  11705. {
  11706. return std::numeric_limits<T>::quiet_NaN();
  11707. }
  11708. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11709. const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&)
  11710. {
  11711. return std::numeric_limits<T>::quiet_NaN();
  11712. }
  11713. inline virtual T operator()(const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&,
  11714. const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&, const T&)
  11715. {
  11716. return std::numeric_limits<T>::quiet_NaN();
  11717. }
  11718. std::size_t param_count;
  11719. };
  11720. template <typename T>
  11721. class ivararg_function : public function_traits
  11722. {
  11723. public:
  11724. virtual ~ivararg_function()
  11725. {}
  11726. inline virtual T operator()(const std::vector<T>&)
  11727. {
  11728. exprtk_debug(("ivararg_function::operator() - Operator has not been overridden.\n"));
  11729. return std::numeric_limits<T>::quiet_NaN();
  11730. }
  11731. };
  11732. template <typename T>
  11733. class igeneric_function : public function_traits
  11734. {
  11735. public:
  11736. typedef T type;
  11737. typedef type_store<T> generic_type;
  11738. typedef typename generic_type::parameter_list parameter_list_t;
  11739. igeneric_function(const std::string& param_seq = "")
  11740. : parameter_sequence(param_seq)
  11741. {}
  11742. virtual ~igeneric_function()
  11743. {}
  11744. // f(i_0,i_1,....,i_N) --> Number
  11745. inline virtual T operator()(parameter_list_t)
  11746. {
  11747. exprtk_debug(("igeneric_function::operator() - Operator has not been overridden. [1]\n"));
  11748. return std::numeric_limits<T>::quiet_NaN();
  11749. }
  11750. // f(i_0,i_1,....,i_N) --> String
  11751. inline virtual T operator()(std::string&, parameter_list_t)
  11752. {
  11753. exprtk_debug(("igeneric_function::operator() - Operator has not been overridden. [2]\n"));
  11754. return std::numeric_limits<T>::quiet_NaN();
  11755. }
  11756. // f(psi,i_0,i_1,....,i_N) --> Number
  11757. inline virtual T operator()(const std::size_t&, parameter_list_t)
  11758. {
  11759. exprtk_debug(("igeneric_function::operator() - Operator has not been overridden. [3]\n"));
  11760. return std::numeric_limits<T>::quiet_NaN();
  11761. }
  11762. // f(psi,i_0,i_1,....,i_N) --> String
  11763. inline virtual T operator()(const std::size_t&, std::string&, parameter_list_t)
  11764. {
  11765. exprtk_debug(("igeneric_function::operator() - Operator has not been overridden. [4]\n"));
  11766. return std::numeric_limits<T>::quiet_NaN();
  11767. }
  11768. std::string parameter_sequence;
  11769. };
  11770. template <typename T> class parser;
  11771. template <typename T> class expression_helper;
  11772. template <typename T>
  11773. class symbol_table
  11774. {
  11775. protected:
  11776. template <typename Type, typename RawType>
  11777. struct type_store
  11778. {
  11779. typedef details::expression_node<T>* expression_ptr;
  11780. typedef typename details::variable_node<T> variable_node_t;
  11781. typedef ifunction<T> ifunction_t;
  11782. typedef ivararg_function<T> ivararg_function_t;
  11783. typedef igeneric_function<T> igeneric_function_t;
  11784. typedef details::vector_holder<T> vector_t;
  11785. #ifndef exprtk_disable_string_capabilities
  11786. typedef typename details::stringvar_node<T> stringvar_node_t;
  11787. #endif
  11788. typedef Type type_t;
  11789. typedef type_t* type_ptr;
  11790. typedef std::pair<bool,type_ptr> type_pair_t;
  11791. typedef std::map<std::string,type_pair_t,details::ilesscompare> type_map_t;
  11792. typedef typename type_map_t::iterator tm_itr_t;
  11793. typedef typename type_map_t::const_iterator tm_const_itr_t;
  11794. enum { lut_size = 256 };
  11795. type_map_t map;
  11796. std::size_t size;
  11797. type_store()
  11798. : size(0)
  11799. {}
  11800. inline bool symbol_exists(const std::string& symbol_name) const
  11801. {
  11802. if (symbol_name.empty())
  11803. return false;
  11804. else if (map.end() != map.find(symbol_name))
  11805. return true;
  11806. else
  11807. return false;
  11808. }
  11809. template <typename PtrType>
  11810. inline std::string entity_name(const PtrType& ptr) const
  11811. {
  11812. if (map.empty())
  11813. return std::string();
  11814. tm_const_itr_t itr = map.begin();
  11815. while (map.end() != itr)
  11816. {
  11817. if (itr->second.second == ptr)
  11818. {
  11819. return itr->first;
  11820. }
  11821. else
  11822. ++itr;
  11823. }
  11824. return std::string();
  11825. }
  11826. inline bool is_constant(const std::string& symbol_name) const
  11827. {
  11828. if (symbol_name.empty())
  11829. return false;
  11830. else
  11831. {
  11832. tm_const_itr_t itr = map.find(symbol_name);
  11833. if (map.end() == itr)
  11834. return false;
  11835. else
  11836. return (*itr).second.first;
  11837. }
  11838. }
  11839. template <typename Tie, typename RType>
  11840. inline bool add_impl(const std::string& symbol_name, RType t, const bool is_const)
  11841. {
  11842. if (symbol_name.size() > 1)
  11843. {
  11844. for (std::size_t i = 0; i < details::reserved_symbols_size; ++i)
  11845. {
  11846. if (details::imatch(symbol_name,details::reserved_symbols[i]))
  11847. {
  11848. return false;
  11849. }
  11850. }
  11851. }
  11852. tm_itr_t itr = map.find(symbol_name);
  11853. if (map.end() == itr)
  11854. {
  11855. map[symbol_name] = Tie::make(t,is_const);
  11856. ++size;
  11857. }
  11858. return true;
  11859. }
  11860. struct tie_array
  11861. {
  11862. static inline std::pair<bool,vector_t*> make(std::pair<T*,std::size_t> v, const bool is_const = false)
  11863. {
  11864. return std::make_pair(is_const,new vector_t(v.first,v.second));
  11865. }
  11866. };
  11867. struct tie_stdvec
  11868. {
  11869. template <typename Allocator>
  11870. static inline std::pair<bool,vector_t*> make(std::vector<T,Allocator>& v, const bool is_const = false)
  11871. {
  11872. return std::make_pair(is_const,new vector_t(v));
  11873. }
  11874. };
  11875. struct tie_stddeq
  11876. {
  11877. template <typename Allocator>
  11878. static inline std::pair<bool,vector_t*> make(std::deque<T,Allocator>& v, const bool is_const = false)
  11879. {
  11880. return std::make_pair(is_const,new vector_t(v));
  11881. }
  11882. };
  11883. template <std::size_t v_size>
  11884. inline bool add(const std::string& symbol_name, T (&v)[v_size], const bool is_const = false)
  11885. {
  11886. return add_impl<tie_array,std::pair<T*,std::size_t> >(symbol_name,std::make_pair(v,v_size),is_const);
  11887. }
  11888. inline bool add(const std::string& symbol_name, T* v, const std::size_t v_size, const bool is_const = false)
  11889. {
  11890. return add_impl<tie_array,std::pair<T*,std::size_t> >(symbol_name,std::make_pair(v,v_size),is_const);
  11891. }
  11892. template <typename Allocator>
  11893. inline bool add(const std::string& symbol_name, std::vector<T,Allocator>& v, const bool is_const = false)
  11894. {
  11895. return add_impl<tie_stdvec,std::vector<T,Allocator>&>(symbol_name,v,is_const);
  11896. }
  11897. template <typename Allocator>
  11898. inline bool add(const std::string& symbol_name, std::deque<T,Allocator>& v, const bool is_const = false)
  11899. {
  11900. return add_impl<tie_stddeq,std::deque<T,Allocator>&>(symbol_name,v,is_const);
  11901. }
  11902. inline bool add(const std::string& symbol_name, RawType& t, const bool is_const = false)
  11903. {
  11904. struct tie
  11905. {
  11906. static inline std::pair<bool,variable_node_t*> make(T& t,const bool is_const = false)
  11907. {
  11908. return std::make_pair(is_const,new variable_node_t(t));
  11909. }
  11910. #ifndef exprtk_disable_string_capabilities
  11911. static inline std::pair<bool,stringvar_node_t*> make(std::string& t,const bool is_const = false)
  11912. {
  11913. return std::make_pair(is_const,new stringvar_node_t(t));
  11914. }
  11915. #endif
  11916. static inline std::pair<bool,function_t*> make(function_t& t, const bool is_constant = false)
  11917. {
  11918. return std::make_pair(is_constant,&t);
  11919. }
  11920. static inline std::pair<bool,vararg_function_t*> make(vararg_function_t& t, const bool is_const = false)
  11921. {
  11922. return std::make_pair(is_const,&t);
  11923. }
  11924. static inline std::pair<bool,generic_function_t*> make(generic_function_t& t, const bool is_constant = false)
  11925. {
  11926. return std::make_pair(is_constant,&t);
  11927. }
  11928. };
  11929. tm_itr_t itr = map.find(symbol_name);
  11930. if (map.end() == itr)
  11931. {
  11932. map[symbol_name] = tie::make(t,is_const);
  11933. ++size;
  11934. }
  11935. return true;
  11936. }
  11937. inline type_ptr get(const std::string& symbol_name) const
  11938. {
  11939. tm_const_itr_t itr = map.find(symbol_name);
  11940. if (map.end() == itr)
  11941. return reinterpret_cast<type_ptr>(0);
  11942. else
  11943. return itr->second.second;
  11944. }
  11945. template <typename TType, typename TRawType, typename PtrType>
  11946. struct ptr_match
  11947. {
  11948. static inline bool test(const PtrType, const void*)
  11949. {
  11950. return false;
  11951. }
  11952. };
  11953. template <typename TType, typename TRawType>
  11954. struct ptr_match<TType,TRawType,variable_node_t*>
  11955. {
  11956. static inline bool test(const variable_node_t* p, const void* ptr)
  11957. {
  11958. exprtk_debug(("ptr_match::test() - %p <--> %p\n",(void*)(&(p->ref())),ptr));
  11959. return (&(p->ref()) == ptr);
  11960. }
  11961. };
  11962. inline type_ptr get_from_varptr(const void* ptr) const
  11963. {
  11964. tm_const_itr_t itr = map.begin();
  11965. while (map.end() != itr)
  11966. {
  11967. type_ptr ret_ptr = itr->second.second;
  11968. if (ptr_match<Type,RawType,type_ptr>::test(ret_ptr,ptr))
  11969. {
  11970. return ret_ptr;
  11971. }
  11972. ++itr;
  11973. }
  11974. return type_ptr(0);
  11975. }
  11976. inline bool remove(const std::string& symbol_name, const bool delete_node = true)
  11977. {
  11978. tm_itr_t itr = map.find(symbol_name);
  11979. if (map.end() != itr)
  11980. {
  11981. struct deleter
  11982. {
  11983. static inline void process(std::pair<bool,variable_node_t*>& n) { delete n.second; }
  11984. static inline void process(std::pair<bool,vector_t*>& n) { delete n.second; }
  11985. #ifndef exprtk_disable_string_capabilities
  11986. static inline void process(std::pair<bool,stringvar_node_t*>& n) { delete n.second; }
  11987. #endif
  11988. static inline void process(std::pair<bool,function_t*>&) { }
  11989. };
  11990. if (delete_node)
  11991. {
  11992. deleter::process((*itr).second);
  11993. }
  11994. map.erase(itr);
  11995. --size;
  11996. return true;
  11997. }
  11998. else
  11999. return false;
  12000. }
  12001. inline RawType& type_ref(const std::string& symbol_name)
  12002. {
  12003. struct init_type
  12004. {
  12005. static inline double set(double) { return (0.0); }
  12006. static inline double set(long double) { return (0.0); }
  12007. static inline float set(float) { return (0.0f); }
  12008. static inline std::string set(std::string) { return std::string(""); }
  12009. };
  12010. static RawType null_type = init_type::set(RawType());
  12011. tm_const_itr_t itr = map.find(symbol_name);
  12012. if (map.end() == itr)
  12013. return null_type;
  12014. else
  12015. return itr->second.second->ref();
  12016. }
  12017. inline void clear(const bool delete_node = true)
  12018. {
  12019. struct deleter
  12020. {
  12021. static inline void process(std::pair<bool,variable_node_t*>& n) { delete n.second; }
  12022. static inline void process(std::pair<bool,vector_t*>& n) { delete n.second; }
  12023. static inline void process(std::pair<bool,function_t*>&) { }
  12024. #ifndef exprtk_disable_string_capabilities
  12025. static inline void process(std::pair<bool,stringvar_node_t*>& n) { delete n.second; }
  12026. #endif
  12027. };
  12028. if (!map.empty())
  12029. {
  12030. if (delete_node)
  12031. {
  12032. tm_itr_t itr = map.begin();
  12033. tm_itr_t end = map.end();
  12034. while (end != itr)
  12035. {
  12036. deleter::process((*itr).second);
  12037. ++itr;
  12038. }
  12039. }
  12040. map.clear();
  12041. }
  12042. size = 0;
  12043. }
  12044. template <typename Allocator,
  12045. template <typename, typename> class Sequence>
  12046. inline std::size_t get_list(Sequence<std::pair<std::string,RawType>,Allocator>& list) const
  12047. {
  12048. std::size_t count = 0;
  12049. if (!map.empty())
  12050. {
  12051. tm_const_itr_t itr = map.begin();
  12052. tm_const_itr_t end = map.end();
  12053. while (end != itr)
  12054. {
  12055. list.push_back(std::make_pair((*itr).first,itr->second.second->ref()));
  12056. ++itr;
  12057. ++count;
  12058. }
  12059. }
  12060. return count;
  12061. }
  12062. template <typename Allocator,
  12063. template <typename, typename> class Sequence>
  12064. inline std::size_t get_list(Sequence<std::string,Allocator>& vlist) const
  12065. {
  12066. std::size_t count = 0;
  12067. if (!map.empty())
  12068. {
  12069. tm_const_itr_t itr = map.begin();
  12070. tm_const_itr_t end = map.end();
  12071. while (end != itr)
  12072. {
  12073. vlist.push_back((*itr).first);
  12074. ++itr;
  12075. ++count;
  12076. }
  12077. }
  12078. return count;
  12079. }
  12080. };
  12081. typedef details::expression_node<T>* expression_ptr;
  12082. typedef typename details::variable_node<T> variable_t;
  12083. typedef typename details::vector_holder<T> vector_holder_t;
  12084. typedef variable_t* variable_ptr;
  12085. #ifndef exprtk_disable_string_capabilities
  12086. typedef typename details::stringvar_node<T> stringvar_t;
  12087. typedef stringvar_t* stringvar_ptr;
  12088. #endif
  12089. typedef ifunction <T> function_t;
  12090. typedef ivararg_function <T> vararg_function_t;
  12091. typedef igeneric_function<T> generic_function_t;
  12092. typedef function_t* function_ptr;
  12093. typedef vararg_function_t* vararg_function_ptr;
  12094. typedef generic_function_t* generic_function_ptr;
  12095. static const std::size_t lut_size = 256;
  12096. // Symbol Table Holder
  12097. struct st_holder
  12098. {
  12099. struct st_data
  12100. {
  12101. type_store<typename details::variable_node<T>,T> variable_store;
  12102. #ifndef exprtk_disable_string_capabilities
  12103. type_store<typename details::stringvar_node<T>,std::string> stringvar_store;
  12104. #endif
  12105. type_store<ifunction<T>,ifunction<T> > function_store;
  12106. type_store<ivararg_function <T>,ivararg_function <T> > vararg_function_store;
  12107. type_store<igeneric_function<T>,igeneric_function<T> > generic_function_store;
  12108. type_store<igeneric_function<T>,igeneric_function<T> > string_function_store;
  12109. type_store<vector_holder_t,vector_holder_t> vector_store;
  12110. st_data()
  12111. {
  12112. for (std::size_t i = 0; i < details::reserved_words_size; ++i)
  12113. {
  12114. reserved_symbol_table_.insert(details::reserved_words[i]);
  12115. }
  12116. for (std::size_t i = 0; i < details::reserved_symbols_size; ++i)
  12117. {
  12118. reserved_symbol_table_.insert(details::reserved_symbols[i]);
  12119. }
  12120. }
  12121. inline bool is_reserved_symbol(const std::string& symbol) const
  12122. {
  12123. return (reserved_symbol_table_.end() != reserved_symbol_table_.find(symbol));
  12124. }
  12125. std::list<T> local_symbol_list_;
  12126. std::list<std::string> local_stringvar_list_;
  12127. std::set<std::string> reserved_symbol_table_;
  12128. };
  12129. st_holder()
  12130. : ref_count(1),
  12131. data_(new st_data)
  12132. {}
  12133. st_holder(st_data* data)
  12134. : ref_count(1),
  12135. data_(data)
  12136. {}
  12137. ~st_holder()
  12138. {
  12139. if (data_ && (0 == ref_count))
  12140. {
  12141. delete data_;
  12142. data_ = 0;
  12143. }
  12144. }
  12145. std::size_t ref_count;
  12146. st_data* data_;
  12147. };
  12148. public:
  12149. symbol_table()
  12150. : holder_(new st_holder)
  12151. {
  12152. clear();
  12153. }
  12154. ~symbol_table()
  12155. {
  12156. if (holder_)
  12157. {
  12158. if (0 == --holder_->ref_count)
  12159. {
  12160. clear();
  12161. delete holder_;
  12162. }
  12163. }
  12164. }
  12165. symbol_table(const symbol_table<T>& st)
  12166. {
  12167. holder_ = st.holder_;
  12168. holder_->ref_count++;
  12169. }
  12170. inline symbol_table<T>& operator=(const symbol_table<T>& st)
  12171. {
  12172. if (holder_)
  12173. {
  12174. if (0 == --holder_->ref_count)
  12175. {
  12176. delete holder_;
  12177. }
  12178. holder_ = 0;
  12179. }
  12180. holder_ = st.holder_;
  12181. holder_->ref_count++;
  12182. return *this;
  12183. }
  12184. inline bool operator==(const symbol_table<T>& st)
  12185. {
  12186. return (this == &st) || (holder_ == st.holder_);
  12187. }
  12188. inline void clear_variables(const bool delete_node = true)
  12189. {
  12190. local_data().variable_store.clear(delete_node);
  12191. }
  12192. inline void clear_functions()
  12193. {
  12194. local_data().function_store.clear();
  12195. }
  12196. inline void clear_strings()
  12197. {
  12198. #ifndef exprtk_disable_string_capabilities
  12199. local_data().stringvar_store.clear();
  12200. #endif
  12201. }
  12202. inline void clear_vectors()
  12203. {
  12204. local_data().vector_store.clear();
  12205. }
  12206. inline void clear()
  12207. {
  12208. if (!valid()) return;
  12209. clear_variables();
  12210. clear_functions();
  12211. clear_strings ();
  12212. clear_vectors ();
  12213. }
  12214. inline std::size_t variable_count() const
  12215. {
  12216. if (valid())
  12217. return local_data().variable_store.size;
  12218. else
  12219. return 0;
  12220. }
  12221. #ifndef exprtk_disable_string_capabilities
  12222. inline std::size_t stringvar_count() const
  12223. {
  12224. if (valid())
  12225. return local_data().stringvar_store.size;
  12226. else
  12227. return 0;
  12228. }
  12229. #endif
  12230. inline std::size_t function_count() const
  12231. {
  12232. if (valid())
  12233. return local_data().function_store.size;
  12234. else
  12235. return 0;
  12236. }
  12237. inline std::size_t vector_count() const
  12238. {
  12239. if (valid())
  12240. return local_data().vector_store.size;
  12241. else
  12242. return 0;
  12243. }
  12244. inline variable_ptr get_variable(const std::string& variable_name) const
  12245. {
  12246. if (!valid())
  12247. return reinterpret_cast<variable_ptr>(0);
  12248. else if (!valid_symbol(variable_name))
  12249. return reinterpret_cast<variable_ptr>(0);
  12250. else
  12251. return local_data().variable_store.get(variable_name);
  12252. }
  12253. inline variable_ptr get_variable(const T& var_ref) const
  12254. {
  12255. if (!valid())
  12256. return reinterpret_cast<variable_ptr>(0);
  12257. else
  12258. return local_data().variable_store.get_from_varptr(
  12259. reinterpret_cast<const void*>(&var_ref));
  12260. }
  12261. #ifndef exprtk_disable_string_capabilities
  12262. inline stringvar_ptr get_stringvar(const std::string& string_name) const
  12263. {
  12264. if (!valid())
  12265. return reinterpret_cast<stringvar_ptr>(0);
  12266. else if (!valid_symbol(string_name))
  12267. return reinterpret_cast<stringvar_ptr>(0);
  12268. else
  12269. return local_data().stringvar_store.get(string_name);
  12270. }
  12271. #endif
  12272. inline function_ptr get_function(const std::string& function_name) const
  12273. {
  12274. if (!valid())
  12275. return reinterpret_cast<function_ptr>(0);
  12276. else if (!valid_symbol(function_name))
  12277. return reinterpret_cast<function_ptr>(0);
  12278. else
  12279. return local_data().function_store.get(function_name);
  12280. }
  12281. inline vararg_function_ptr get_vararg_function(const std::string& vararg_function_name) const
  12282. {
  12283. if (!valid())
  12284. return reinterpret_cast<vararg_function_ptr>(0);
  12285. else if (!valid_symbol(vararg_function_name))
  12286. return reinterpret_cast<vararg_function_ptr>(0);
  12287. else
  12288. return local_data().vararg_function_store.get(vararg_function_name);
  12289. }
  12290. inline generic_function_ptr get_generic_function(const std::string& function_name) const
  12291. {
  12292. if (!valid())
  12293. return reinterpret_cast<generic_function_ptr>(0);
  12294. else if (!valid_symbol(function_name))
  12295. return reinterpret_cast<generic_function_ptr>(0);
  12296. else
  12297. return local_data().generic_function_store.get(function_name);
  12298. }
  12299. inline generic_function_ptr get_string_function(const std::string& function_name) const
  12300. {
  12301. if (!valid())
  12302. return reinterpret_cast<generic_function_ptr>(0);
  12303. else if (!valid_symbol(function_name))
  12304. return reinterpret_cast<generic_function_ptr>(0);
  12305. else
  12306. return local_data().string_function_store.get(function_name);
  12307. }
  12308. typedef vector_holder_t* vector_holder_ptr;
  12309. inline vector_holder_ptr get_vector(const std::string& vector_name) const
  12310. {
  12311. if (!valid())
  12312. return reinterpret_cast<vector_holder_ptr>(0);
  12313. else if (!valid_symbol(vector_name))
  12314. return reinterpret_cast<vector_holder_ptr>(0);
  12315. else
  12316. return local_data().vector_store.get(vector_name);
  12317. }
  12318. inline T& variable_ref(const std::string& symbol_name)
  12319. {
  12320. static T null_var = T(0);
  12321. if (!valid())
  12322. return null_var;
  12323. else if (!valid_symbol(symbol_name))
  12324. return null_var;
  12325. else
  12326. return local_data().variable_store.type_ref(symbol_name);
  12327. }
  12328. #ifndef exprtk_disable_string_capabilities
  12329. inline std::string& stringvar_ref(const std::string& symbol_name)
  12330. {
  12331. static std::string null_stringvar;
  12332. if (!valid())
  12333. return null_stringvar;
  12334. else if (!valid_symbol(symbol_name))
  12335. return null_stringvar;
  12336. else
  12337. return local_data().stringvar_store.type_ref(symbol_name);
  12338. }
  12339. #endif
  12340. inline bool is_constant_node(const std::string& symbol_name) const
  12341. {
  12342. if (!valid())
  12343. return false;
  12344. else if (!valid_symbol(symbol_name))
  12345. return false;
  12346. else
  12347. return local_data().variable_store.is_constant(symbol_name);
  12348. }
  12349. #ifndef exprtk_disable_string_capabilities
  12350. inline bool is_constant_string(const std::string& symbol_name) const
  12351. {
  12352. if (!valid())
  12353. return false;
  12354. else if (!valid_symbol(symbol_name))
  12355. return false;
  12356. else if (!local_data().stringvar_store.symbol_exists(symbol_name))
  12357. return false;
  12358. else
  12359. return local_data().stringvar_store.is_constant(symbol_name);
  12360. }
  12361. #endif
  12362. inline bool create_variable(const std::string& variable_name, const T& value = T(0))
  12363. {
  12364. if (!valid())
  12365. return false;
  12366. else if (!valid_symbol(variable_name))
  12367. return false;
  12368. else if (symbol_exists(variable_name))
  12369. return false;
  12370. local_data().local_symbol_list_.push_back(value);
  12371. T& t = local_data().local_symbol_list_.back();
  12372. return add_variable(variable_name,t);
  12373. }
  12374. #ifndef exprtk_disable_string_capabilities
  12375. inline bool create_stringvar(const std::string& stringvar_name, const std::string& value = std::string(""))
  12376. {
  12377. if (!valid())
  12378. return false;
  12379. else if (!valid_symbol(stringvar_name))
  12380. return false;
  12381. else if (symbol_exists(stringvar_name))
  12382. return false;
  12383. local_data().local_stringvar_list_.push_back(value);
  12384. std::string& s = local_data().local_stringvar_list_.back();
  12385. return add_stringvar(stringvar_name,s);
  12386. }
  12387. #endif
  12388. inline bool add_variable(const std::string& variable_name, T& t, const bool is_constant = false)
  12389. {
  12390. if (!valid())
  12391. return false;
  12392. else if (!valid_symbol(variable_name))
  12393. return false;
  12394. else if (symbol_exists(variable_name))
  12395. return false;
  12396. else
  12397. return local_data().variable_store.add(variable_name,t,is_constant);
  12398. }
  12399. inline bool add_constant(const std::string& constant_name, const T& value)
  12400. {
  12401. if (!valid())
  12402. return false;
  12403. else if (!valid_symbol(constant_name))
  12404. return false;
  12405. else if (symbol_exists(constant_name))
  12406. return false;
  12407. local_data().local_symbol_list_.push_back(value);
  12408. T& t = local_data().local_symbol_list_.back();
  12409. return add_variable(constant_name,t,true);
  12410. }
  12411. #ifndef exprtk_disable_string_capabilities
  12412. inline bool add_stringvar(const std::string& stringvar_name, std::string& s, const bool is_constant = false)
  12413. {
  12414. if (!valid())
  12415. return false;
  12416. else if (!valid_symbol(stringvar_name))
  12417. return false;
  12418. else if (symbol_exists(stringvar_name))
  12419. return false;
  12420. else
  12421. return local_data().stringvar_store.add(stringvar_name,s,is_constant);
  12422. }
  12423. #endif
  12424. inline bool add_function(const std::string& function_name, function_t& function)
  12425. {
  12426. if (!valid())
  12427. return false;
  12428. else if (!valid_symbol(function_name))
  12429. return false;
  12430. else if (symbol_exists(function_name))
  12431. return false;
  12432. else
  12433. return local_data().function_store.add(function_name,function);
  12434. }
  12435. inline bool add_function(const std::string& vararg_function_name, vararg_function_t& vararg_function)
  12436. {
  12437. if (!valid())
  12438. return false;
  12439. else if (!valid_symbol(vararg_function_name))
  12440. return false;
  12441. else if (symbol_exists(vararg_function_name))
  12442. return false;
  12443. else
  12444. return local_data().vararg_function_store.add(vararg_function_name,vararg_function);
  12445. }
  12446. enum func_type
  12447. {
  12448. e_ft_unknown = 0,
  12449. e_ft_basicfunc = 1,
  12450. e_ft_strfunc = 2
  12451. };
  12452. inline bool add_function(const std::string& function_name, generic_function_t& function, const func_type ft = e_ft_basicfunc)
  12453. {
  12454. if (!valid())
  12455. return false;
  12456. else if (!valid_symbol(function_name))
  12457. return false;
  12458. else if (symbol_exists(function_name))
  12459. return false;
  12460. else if (std::string::npos != function.parameter_sequence.find_first_not_of("STV*?|"))
  12461. return false;
  12462. else if (e_ft_basicfunc == ft)
  12463. return local_data().generic_function_store.add(function_name,function);
  12464. else if (e_ft_strfunc == ft)
  12465. return local_data().string_function_store.add(function_name, function);
  12466. else
  12467. return false;
  12468. }
  12469. inline bool add_reserved_function(const std::string& function_name, function_t& function)
  12470. {
  12471. if (!valid())
  12472. return false;
  12473. else if (!valid_symbol(function_name,false))
  12474. return false;
  12475. else if (symbol_exists(function_name,false))
  12476. return false;
  12477. else
  12478. return local_data().function_store.add(function_name,function);
  12479. }
  12480. inline bool add_reserved_function(const std::string& vararg_function_name, vararg_function_t& vararg_function)
  12481. {
  12482. if (!valid())
  12483. return false;
  12484. else if (!valid_symbol(vararg_function_name,false))
  12485. return false;
  12486. else if (symbol_exists(vararg_function_name,false))
  12487. return false;
  12488. else
  12489. return local_data().vararg_function_store.add(vararg_function_name,vararg_function);
  12490. }
  12491. inline bool add_reserved_function(const std::string& function_name, generic_function_t& function, const func_type ft = e_ft_basicfunc)
  12492. {
  12493. if (!valid())
  12494. return false;
  12495. else if (!valid_symbol(function_name,false))
  12496. return false;
  12497. else if (symbol_exists(function_name,false))
  12498. return false;
  12499. else if (std::string::npos != function.parameter_sequence.find_first_not_of("STV*?|"))
  12500. return false;
  12501. else if (e_ft_basicfunc == ft)
  12502. return local_data().generic_function_store.add(function_name,function);
  12503. else if (e_ft_strfunc == ft)
  12504. return local_data().string_function_store.add(function_name, function);
  12505. else
  12506. return false;
  12507. }
  12508. template <std::size_t N>
  12509. inline bool add_vector(const std::string& vector_name, T (&v)[N])
  12510. {
  12511. if (!valid())
  12512. return false;
  12513. else if (!valid_symbol(vector_name))
  12514. return false;
  12515. else if (symbol_exists(vector_name))
  12516. return false;
  12517. else
  12518. return local_data().vector_store.add(vector_name,v);
  12519. }
  12520. inline bool add_vector(const std::string& vector_name, T* v, const std::size_t& v_size)
  12521. {
  12522. if (!valid())
  12523. return false;
  12524. else if (!valid_symbol(vector_name))
  12525. return false;
  12526. else if (symbol_exists(vector_name))
  12527. return false;
  12528. else
  12529. return local_data().vector_store.add(vector_name,v,v_size);
  12530. }
  12531. template <typename Allocator>
  12532. inline bool add_vector(const std::string& vector_name, std::vector<T,Allocator>& v)
  12533. {
  12534. if (!valid())
  12535. return false;
  12536. else if (!valid_symbol(vector_name))
  12537. return false;
  12538. else if (symbol_exists(vector_name))
  12539. return false;
  12540. else
  12541. return local_data().vector_store.add(vector_name,v);
  12542. }
  12543. template <typename Allocator>
  12544. inline bool add_vector(const std::string& vector_name, std::deque<T,Allocator>& v)
  12545. {
  12546. if (!valid())
  12547. return false;
  12548. else if (!valid_symbol(vector_name))
  12549. return false;
  12550. else if (symbol_exists(vector_name))
  12551. return false;
  12552. else
  12553. return local_data().vector_store.add(vector_name,v);
  12554. }
  12555. inline bool remove_variable(const std::string& variable_name, const bool delete_node = true)
  12556. {
  12557. if (!valid())
  12558. return false;
  12559. else
  12560. return local_data().variable_store.remove(variable_name, delete_node);
  12561. }
  12562. #ifndef exprtk_disable_string_capabilities
  12563. inline bool remove_stringvar(const std::string& string_name)
  12564. {
  12565. if (!valid())
  12566. return false;
  12567. else
  12568. return local_data().stringvar_store.remove(string_name);
  12569. }
  12570. #endif
  12571. inline bool remove_function(const std::string& function_name)
  12572. {
  12573. if (!valid())
  12574. return false;
  12575. else
  12576. return local_data().function_store.remove(function_name);
  12577. }
  12578. inline bool remove_vararg_function(const std::string& vararg_function_name)
  12579. {
  12580. if (!valid())
  12581. return false;
  12582. else
  12583. return local_data().vararg_function_store.remove(vararg_function_name);
  12584. }
  12585. inline bool remove_vector(const std::string& vector_name)
  12586. {
  12587. if (!valid())
  12588. return false;
  12589. else
  12590. return local_data().vector_store.remove(vector_name);
  12591. }
  12592. inline bool add_constants()
  12593. {
  12594. return add_pi () &&
  12595. add_epsilon () &&
  12596. add_infinity();
  12597. }
  12598. inline bool add_pi()
  12599. {
  12600. static const T local_pi = T(details::numeric::constant::pi);
  12601. return add_constant("pi",local_pi);
  12602. }
  12603. inline bool add_epsilon()
  12604. {
  12605. static const T local_epsilon = details::numeric::details::epsilon_type<T>::value();
  12606. return add_constant("epsilon",local_epsilon);
  12607. }
  12608. inline bool add_infinity()
  12609. {
  12610. static const T local_infinity = std::numeric_limits<T>::infinity();
  12611. return add_constant("inf",local_infinity);
  12612. }
  12613. template <typename Allocator,
  12614. template <typename, typename> class Sequence>
  12615. inline std::size_t get_variable_list(Sequence<std::pair<std::string,T>,Allocator>& vlist) const
  12616. {
  12617. if (!valid())
  12618. return 0;
  12619. else
  12620. return local_data().variable_store.get_list(vlist);
  12621. }
  12622. template <typename Allocator,
  12623. template <typename, typename> class Sequence>
  12624. inline std::size_t get_variable_list(Sequence<std::string,Allocator>& vlist) const
  12625. {
  12626. if (!valid())
  12627. return 0;
  12628. else
  12629. return local_data().variable_store.get_list(vlist);
  12630. }
  12631. #ifndef exprtk_disable_string_capabilities
  12632. template <typename Allocator,
  12633. template <typename, typename> class Sequence>
  12634. inline std::size_t get_stringvar_list(Sequence<std::pair<std::string,std::string>,Allocator>& svlist) const
  12635. {
  12636. if (!valid())
  12637. return 0;
  12638. else
  12639. return local_data().stringvar_store.get_list(svlist);
  12640. }
  12641. template <typename Allocator,
  12642. template <typename, typename> class Sequence>
  12643. inline std::size_t get_stringvar_list(Sequence<std::string,Allocator>& svlist) const
  12644. {
  12645. if (!valid())
  12646. return 0;
  12647. else
  12648. return local_data().stringvar_store.get_list(svlist);
  12649. }
  12650. #endif
  12651. template <typename Allocator,
  12652. template <typename, typename> class Sequence>
  12653. inline std::size_t get_vector_list(Sequence<std::string,Allocator>& vlist) const
  12654. {
  12655. if (!valid())
  12656. return 0;
  12657. else
  12658. return local_data().vector_store.get_list(vlist);
  12659. }
  12660. inline bool symbol_exists(const std::string& symbol_name, const bool check_reserved_symb = true) const
  12661. {
  12662. /*
  12663. Will return true if symbol_name exists as either a reserved symbol,
  12664. variable, stringvar or function name in any of the type stores.
  12665. */
  12666. if (!valid())
  12667. return false;
  12668. else if (local_data().variable_store.symbol_exists(symbol_name))
  12669. return true;
  12670. #ifndef exprtk_disable_string_capabilities
  12671. else if (local_data().stringvar_store.symbol_exists(symbol_name))
  12672. return true;
  12673. #endif
  12674. else if (local_data().function_store.symbol_exists(symbol_name))
  12675. return true;
  12676. else if (check_reserved_symb && local_data().is_reserved_symbol(symbol_name))
  12677. return true;
  12678. else
  12679. return false;
  12680. }
  12681. inline bool is_variable(const std::string& variable_name) const
  12682. {
  12683. if (!valid())
  12684. return false;
  12685. else
  12686. return local_data().variable_store.symbol_exists(variable_name);
  12687. }
  12688. #ifndef exprtk_disable_string_capabilities
  12689. inline bool is_stringvar(const std::string& stringvar_name) const
  12690. {
  12691. if (!valid())
  12692. return false;
  12693. else
  12694. return local_data().stringvar_store.symbol_exists(stringvar_name);
  12695. }
  12696. inline bool is_conststr_stringvar(const std::string& symbol_name) const
  12697. {
  12698. if (!valid())
  12699. return false;
  12700. else if (!valid_symbol(symbol_name))
  12701. return false;
  12702. else if (!local_data().stringvar_store.symbol_exists(symbol_name))
  12703. return false;
  12704. return (
  12705. local_data().stringvar_store.symbol_exists(symbol_name) ||
  12706. local_data().stringvar_store.is_constant (symbol_name)
  12707. );
  12708. }
  12709. #endif
  12710. inline bool is_function(const std::string& function_name) const
  12711. {
  12712. if (!valid())
  12713. return false;
  12714. else
  12715. return local_data().function_store.symbol_exists(function_name);
  12716. }
  12717. inline bool is_vararg_function(const std::string& vararg_function_name) const
  12718. {
  12719. if (!valid())
  12720. return false;
  12721. else
  12722. return local_data().vararg_function_store.symbol_exists(vararg_function_name);
  12723. }
  12724. inline bool is_vector(const std::string& vector_name) const
  12725. {
  12726. if (!valid())
  12727. return false;
  12728. else
  12729. return local_data().vector_store.symbol_exists(vector_name);
  12730. }
  12731. inline std::string get_variable_name(const expression_ptr& ptr) const
  12732. {
  12733. return local_data().variable_store.entity_name(ptr);
  12734. }
  12735. inline std::string get_vector_name(const vector_holder_ptr& ptr) const
  12736. {
  12737. return local_data().vector_store.entity_name(ptr);
  12738. }
  12739. #ifndef exprtk_disable_string_capabilities
  12740. inline std::string get_stringvar_name(const expression_ptr& ptr) const
  12741. {
  12742. return local_data().stringvar_store.entity_name(ptr);
  12743. }
  12744. inline std::string get_conststr_stringvar_name(const expression_ptr& ptr) const
  12745. {
  12746. return local_data().stringvar_store.entity_name(ptr);
  12747. }
  12748. #endif
  12749. inline bool valid() const
  12750. {
  12751. // Symbol table sanity check.
  12752. return holder_ && holder_->data_;
  12753. }
  12754. inline void load_from(const symbol_table<T>& st)
  12755. {
  12756. {
  12757. std::vector<std::string> name_list;
  12758. st.local_data().function_store.get_list(name_list);
  12759. if (!name_list.empty())
  12760. {
  12761. for (std::size_t i = 0; i < name_list.size(); ++i)
  12762. {
  12763. exprtk::ifunction<T>& ifunc = *st.get_function(name_list[i]);
  12764. add_function(name_list[i],ifunc);
  12765. }
  12766. }
  12767. }
  12768. {
  12769. std::vector<std::string> name_list;
  12770. st.local_data().vararg_function_store.get_list(name_list);
  12771. if (!name_list.empty())
  12772. {
  12773. for (std::size_t i = 0; i < name_list.size(); ++i)
  12774. {
  12775. exprtk::ivararg_function<T>& ivafunc = *st.get_vararg_function(name_list[i]);
  12776. add_function(name_list[i],ivafunc);
  12777. }
  12778. }
  12779. }
  12780. {
  12781. std::vector<std::string> name_list;
  12782. st.local_data().generic_function_store.get_list(name_list);
  12783. if (!name_list.empty())
  12784. {
  12785. for (std::size_t i = 0; i < name_list.size(); ++i)
  12786. {
  12787. exprtk::igeneric_function<T>& ifunc = *st.get_generic_function(name_list[i]);
  12788. add_function(name_list[i],ifunc);
  12789. }
  12790. }
  12791. }
  12792. {
  12793. std::vector<std::string> name_list;
  12794. st.local_data().string_function_store.get_list(name_list);
  12795. if (!name_list.empty())
  12796. {
  12797. for (std::size_t i = 0; i < name_list.size(); ++i)
  12798. {
  12799. exprtk::igeneric_function<T>& ifunc = *st.get_string_function(name_list[i]);
  12800. add_function(name_list[i],ifunc,e_ft_strfunc);
  12801. }
  12802. }
  12803. }
  12804. }
  12805. private:
  12806. inline bool valid_symbol(const std::string& symbol, const bool check_reserved_symb = true) const
  12807. {
  12808. if (symbol.empty())
  12809. return false;
  12810. if (!details::is_letter(symbol[0]))
  12811. return false;
  12812. else if (symbol.size() > 1)
  12813. {
  12814. for (std::size_t i = 1; i < symbol.size(); ++i)
  12815. {
  12816. if (
  12817. (!details::is_letter(symbol[i])) &&
  12818. (!details:: is_digit(symbol[i])) &&
  12819. ('_' != symbol[i])
  12820. )
  12821. {
  12822. return false;
  12823. }
  12824. }
  12825. }
  12826. return (check_reserved_symb) ? (!local_data().is_reserved_symbol(symbol)) : true;
  12827. }
  12828. inline bool valid_function(const std::string& symbol) const
  12829. {
  12830. if (symbol.empty())
  12831. return false;
  12832. if (!details::is_letter(symbol[0]))
  12833. return false;
  12834. else if (symbol.size() > 1)
  12835. {
  12836. for (std::size_t i = 1; i < symbol.size(); ++i)
  12837. {
  12838. if (
  12839. (!details::is_letter(symbol[i])) &&
  12840. (!details:: is_digit(symbol[i])) &&
  12841. ('_' != symbol[i])
  12842. )
  12843. {
  12844. return false;
  12845. }
  12846. }
  12847. }
  12848. return true;
  12849. }
  12850. typedef typename st_holder::st_data local_data_t;
  12851. inline local_data_t& local_data()
  12852. {
  12853. return *(holder_->data_);
  12854. }
  12855. inline const local_data_t& local_data() const
  12856. {
  12857. return *(holder_->data_);
  12858. }
  12859. st_holder* holder_;
  12860. friend class parser<T>;
  12861. };
  12862. template <typename T>
  12863. class function_compositor;
  12864. template <typename T>
  12865. class expression
  12866. {
  12867. private:
  12868. typedef details::expression_node<T>* expression_ptr;
  12869. typedef details::vector_holder<T>* vector_holder_ptr;
  12870. typedef std::vector<symbol_table<T> > symtab_list_t;
  12871. struct expression_holder
  12872. {
  12873. enum data_type
  12874. {
  12875. e_unknown ,
  12876. e_expr ,
  12877. e_vecholder,
  12878. e_data ,
  12879. e_vecdata ,
  12880. e_string
  12881. };
  12882. struct data_pack
  12883. {
  12884. data_pack()
  12885. : pointer(0),
  12886. type(e_unknown),
  12887. size(0)
  12888. {}
  12889. data_pack(void* ptr, data_type dt, std::size_t sz = 0)
  12890. : pointer(ptr),
  12891. type(dt),
  12892. size(sz)
  12893. {}
  12894. void* pointer;
  12895. data_type type;
  12896. std::size_t size;
  12897. };
  12898. typedef std::vector<data_pack> local_data_list_t;
  12899. typedef results_context<T> results_context_t;
  12900. expression_holder()
  12901. : ref_count(0),
  12902. expr (0),
  12903. results (0),
  12904. retinv_null(false),
  12905. return_invoked(&retinv_null)
  12906. {}
  12907. expression_holder(expression_ptr e)
  12908. : ref_count(1),
  12909. expr (e),
  12910. results (0),
  12911. retinv_null(false),
  12912. return_invoked(&retinv_null)
  12913. {}
  12914. ~expression_holder()
  12915. {
  12916. if (expr && details::branch_deletable(expr))
  12917. {
  12918. delete expr;
  12919. }
  12920. if (!local_data_list.empty())
  12921. {
  12922. for (std::size_t i = 0; i < local_data_list.size(); ++i)
  12923. {
  12924. switch (local_data_list[i].type)
  12925. {
  12926. case e_expr : delete reinterpret_cast<expression_ptr>(local_data_list[i].pointer);
  12927. break;
  12928. case e_vecholder : delete reinterpret_cast<vector_holder_ptr>(local_data_list[i].pointer);
  12929. break;
  12930. case e_data : delete (T*)(local_data_list[i].pointer);
  12931. break;
  12932. case e_vecdata : delete [] (T*)(local_data_list[i].pointer);
  12933. break;
  12934. case e_string : delete (std::string*)(local_data_list[i].pointer);
  12935. break;
  12936. default : break;
  12937. }
  12938. }
  12939. }
  12940. if (results)
  12941. {
  12942. delete results;
  12943. }
  12944. }
  12945. std::size_t ref_count;
  12946. expression_ptr expr;
  12947. local_data_list_t local_data_list;
  12948. results_context_t* results;
  12949. bool retinv_null;
  12950. bool* return_invoked;
  12951. friend class function_compositor<T>;
  12952. };
  12953. public:
  12954. expression()
  12955. : expression_holder_(0)
  12956. {
  12957. set_expression(new details::null_node<T>());
  12958. }
  12959. expression(const expression<T>& e)
  12960. : expression_holder_(e.expression_holder_),
  12961. symbol_table_list_(e.symbol_table_list_)
  12962. {
  12963. expression_holder_->ref_count++;
  12964. }
  12965. inline expression<T>& operator=(const expression<T>& e)
  12966. {
  12967. if (this != &e)
  12968. {
  12969. if (expression_holder_)
  12970. {
  12971. if (0 == --expression_holder_->ref_count)
  12972. {
  12973. delete expression_holder_;
  12974. }
  12975. expression_holder_ = 0;
  12976. }
  12977. expression_holder_ = e.expression_holder_;
  12978. expression_holder_->ref_count++;
  12979. symbol_table_list_ = e.symbol_table_list_;
  12980. }
  12981. return *this;
  12982. }
  12983. inline bool operator==(const expression<T>& e)
  12984. {
  12985. return (this == &e);
  12986. }
  12987. inline bool operator!() const
  12988. {
  12989. return (
  12990. (0 == expression_holder_ ) ||
  12991. (0 == expression_holder_->expr)
  12992. );
  12993. }
  12994. inline expression<T>& release()
  12995. {
  12996. if (expression_holder_)
  12997. {
  12998. if (0 == --expression_holder_->ref_count)
  12999. {
  13000. delete expression_holder_;
  13001. }
  13002. expression_holder_ = 0;
  13003. }
  13004. return *this;
  13005. }
  13006. ~expression()
  13007. {
  13008. if (expression_holder_)
  13009. {
  13010. if (0 == --expression_holder_->ref_count)
  13011. {
  13012. delete expression_holder_;
  13013. }
  13014. }
  13015. }
  13016. inline T value() const
  13017. {
  13018. return expression_holder_->expr->value();
  13019. }
  13020. inline T operator()() const
  13021. {
  13022. return value();
  13023. }
  13024. inline operator T() const
  13025. {
  13026. return value();
  13027. }
  13028. inline operator bool() const
  13029. {
  13030. return details::is_true(value());
  13031. }
  13032. inline void register_symbol_table(symbol_table<T>& st)
  13033. {
  13034. symbol_table_list_.push_back(st);
  13035. }
  13036. inline const symbol_table<T>& get_symbol_table(const std::size_t& index = 0) const
  13037. {
  13038. return symbol_table_list_[index];
  13039. }
  13040. inline symbol_table<T>& get_symbol_table(const std::size_t& index = 0)
  13041. {
  13042. return symbol_table_list_[index];
  13043. }
  13044. typedef results_context<T> results_context_t;
  13045. inline const results_context_t& results() const
  13046. {
  13047. if (expression_holder_->results)
  13048. return (*expression_holder_->results);
  13049. else
  13050. {
  13051. static const results_context_t null_results;
  13052. return null_results;
  13053. }
  13054. }
  13055. inline bool return_invoked() const
  13056. {
  13057. return (*expression_holder_->return_invoked);
  13058. }
  13059. private:
  13060. inline symtab_list_t get_symbol_table_list() const
  13061. {
  13062. return symbol_table_list_;
  13063. }
  13064. inline void set_expression(const expression_ptr expr)
  13065. {
  13066. if (expr)
  13067. {
  13068. if (expression_holder_)
  13069. {
  13070. if (0 == --expression_holder_->ref_count)
  13071. {
  13072. delete expression_holder_;
  13073. }
  13074. }
  13075. expression_holder_ = new expression_holder(expr);
  13076. }
  13077. }
  13078. inline void register_local_var(expression_ptr expr)
  13079. {
  13080. if (expr)
  13081. {
  13082. if (expression_holder_)
  13083. {
  13084. expression_holder_->
  13085. local_data_list.push_back(
  13086. typename expression<T>::expression_holder::
  13087. data_pack(reinterpret_cast<void*>(expr),
  13088. expression_holder::e_expr));
  13089. }
  13090. }
  13091. }
  13092. inline void register_local_var(vector_holder_ptr vec_holder)
  13093. {
  13094. if (vec_holder)
  13095. {
  13096. if (expression_holder_)
  13097. {
  13098. expression_holder_->
  13099. local_data_list.push_back(
  13100. typename expression<T>::expression_holder::
  13101. data_pack(reinterpret_cast<void*>(vec_holder),
  13102. expression_holder::e_vecholder));
  13103. }
  13104. }
  13105. }
  13106. inline void register_local_data(void* data, const std::size_t& size = 0, const std::size_t data_mode = 0)
  13107. {
  13108. if (data)
  13109. {
  13110. if (expression_holder_)
  13111. {
  13112. typename expression_holder::data_type dt = expression_holder::e_data;
  13113. switch(data_mode)
  13114. {
  13115. case 0 : dt = expression_holder::e_data; break;
  13116. case 1 : dt = expression_holder::e_vecdata; break;
  13117. case 2 : dt = expression_holder::e_string; break;
  13118. }
  13119. expression_holder_->
  13120. local_data_list.push_back(
  13121. typename expression<T>::expression_holder::
  13122. data_pack(reinterpret_cast<void*>(data),dt,size));
  13123. }
  13124. }
  13125. }
  13126. inline const typename expression_holder::local_data_list_t& local_data_list()
  13127. {
  13128. if (expression_holder_)
  13129. {
  13130. return expression_holder_->local_data_list;
  13131. }
  13132. else
  13133. {
  13134. static typename expression_holder::local_data_list_t null_local_data_list;
  13135. return null_local_data_list;
  13136. }
  13137. }
  13138. inline void register_return_results(results_context_t* rc)
  13139. {
  13140. if (expression_holder_ && rc)
  13141. {
  13142. expression_holder_->results = rc;
  13143. }
  13144. }
  13145. inline void set_retinvk(bool* retinvk_ptr)
  13146. {
  13147. if (expression_holder_)
  13148. {
  13149. expression_holder_->return_invoked = retinvk_ptr;
  13150. }
  13151. }
  13152. expression_holder* expression_holder_;
  13153. symtab_list_t symbol_table_list_;
  13154. friend class parser<T>;
  13155. friend class expression_helper<T>;
  13156. friend class function_compositor<T>;
  13157. };
  13158. template <typename T>
  13159. class expression_helper
  13160. {
  13161. public:
  13162. static inline bool is_constant(const expression<T>& expr)
  13163. {
  13164. return details::is_constant_node(expr.expression_holder_->expr);
  13165. }
  13166. static inline bool is_variable(const expression<T>& expr)
  13167. {
  13168. return details::is_variable_node(expr.expression_holder_->expr);
  13169. }
  13170. static inline bool is_unary(const expression<T>& expr)
  13171. {
  13172. return details::is_unary_node(expr.expression_holder_->expr);
  13173. }
  13174. static inline bool is_binary(const expression<T>& expr)
  13175. {
  13176. return details::is_binary_node(expr.expression_holder_->expr);
  13177. }
  13178. static inline bool is_function(const expression<T>& expr)
  13179. {
  13180. return details::is_function(expr.expression_holder_->expr);
  13181. }
  13182. };
  13183. namespace parser_error
  13184. {
  13185. enum error_mode
  13186. {
  13187. e_unknown = 0,
  13188. e_syntax = 1,
  13189. e_token = 2,
  13190. e_numeric = 4,
  13191. e_symtab = 5,
  13192. e_lexer = 6,
  13193. e_helper = 7
  13194. };
  13195. struct type
  13196. {
  13197. type()
  13198. : mode(parser_error::e_unknown),
  13199. line_no (0),
  13200. column_no(0)
  13201. {}
  13202. lexer::token token;
  13203. error_mode mode;
  13204. std::string diagnostic;
  13205. std::string error_line;
  13206. std::size_t line_no;
  13207. std::size_t column_no;
  13208. };
  13209. inline type make_error(error_mode mode, const std::string& diagnostic = "")
  13210. {
  13211. type t;
  13212. t.mode = mode;
  13213. t.token.type = lexer::token::e_error;
  13214. t.diagnostic = diagnostic;
  13215. exprtk_debug(("%s\n",diagnostic .c_str()));
  13216. return t;
  13217. }
  13218. inline type make_error(error_mode mode, const lexer::token& tk, const std::string& diagnostic = "")
  13219. {
  13220. type t;
  13221. t.mode = mode;
  13222. t.token = tk;
  13223. t.diagnostic = diagnostic;
  13224. exprtk_debug(("%s\n",diagnostic .c_str()));
  13225. return t;
  13226. }
  13227. inline std::string to_str(error_mode mode)
  13228. {
  13229. switch (mode)
  13230. {
  13231. case e_unknown : return std::string("Unknown Error");
  13232. case e_syntax : return std::string("Syntax Error" );
  13233. case e_token : return std::string("Token Error" );
  13234. case e_numeric : return std::string("Numeric Error");
  13235. case e_symtab : return std::string("Symbol Error" );
  13236. case e_lexer : return std::string("Lexer Error" );
  13237. case e_helper : return std::string("Helper Error" );
  13238. default : return std::string("Unknown Error");
  13239. }
  13240. }
  13241. inline bool update_error(type& error, const std::string& expression)
  13242. {
  13243. if (
  13244. expression.empty() ||
  13245. (error.token.position > expression.size()) ||
  13246. (std::numeric_limits<std::size_t>::max() == error.token.position)
  13247. )
  13248. {
  13249. return false;
  13250. }
  13251. std::size_t error_line_start = 0;
  13252. for (std::size_t i = error.token.position; i > 0; --i)
  13253. {
  13254. const char c = expression[i];
  13255. if (('\n' == c) || ('\r' == c))
  13256. {
  13257. error_line_start = i + 1;
  13258. break;
  13259. }
  13260. }
  13261. std::size_t next_nl_position = std::min(expression.size(),
  13262. expression.find_first_of('\n',error.token.position + 1));
  13263. error.column_no = error.token.position - error_line_start;
  13264. error.error_line = expression.substr(error_line_start,
  13265. next_nl_position - error_line_start);
  13266. error.line_no = 0;
  13267. for (std::size_t i = 0; i < next_nl_position; ++i)
  13268. {
  13269. if ('\n' == expression[i])
  13270. ++error.line_no;
  13271. }
  13272. return true;
  13273. }
  13274. inline void dump_error(const type& error)
  13275. {
  13276. printf("Position: %02d Type: [%s] Msg: %s\n",
  13277. static_cast<int>(error.token.position),
  13278. exprtk::parser_error::to_str(error.mode).c_str(),
  13279. error.diagnostic.c_str());
  13280. }
  13281. }
  13282. template <typename T>
  13283. class parser : public lexer::parser_helper
  13284. {
  13285. private:
  13286. enum precedence_level
  13287. {
  13288. e_level00,
  13289. e_level01,
  13290. e_level02,
  13291. e_level03,
  13292. e_level04,
  13293. e_level05,
  13294. e_level06,
  13295. e_level07,
  13296. e_level08,
  13297. e_level09,
  13298. e_level10,
  13299. e_level11,
  13300. e_level12,
  13301. e_level13,
  13302. e_level14
  13303. };
  13304. typedef const T& cref_t;
  13305. typedef const T const_t;
  13306. typedef ifunction <T> F;
  13307. typedef ivararg_function <T> VAF;
  13308. typedef igeneric_function <T> GF;
  13309. typedef ifunction <T> ifunction_t;
  13310. typedef ivararg_function <T> ivararg_function_t;
  13311. typedef igeneric_function <T> igeneric_function_t;
  13312. typedef details::expression_node <T> expression_node_t;
  13313. typedef details::literal_node <T> literal_node_t;
  13314. typedef details::unary_node <T> unary_node_t;
  13315. typedef details::binary_node <T> binary_node_t;
  13316. typedef details::trinary_node <T> trinary_node_t;
  13317. typedef details::quaternary_node <T> quaternary_node_t;
  13318. typedef details::conditional_node<T> conditional_node_t;
  13319. typedef details::cons_conditional_node<T> cons_conditional_node_t;
  13320. typedef details::while_loop_node <T> while_loop_node_t;
  13321. typedef details::repeat_until_loop_node<T> repeat_until_loop_node_t;
  13322. typedef details::for_loop_node <T> for_loop_node_t;
  13323. #ifndef exprtk_disable_break_continue
  13324. typedef details::while_loop_bc_node <T> while_loop_bc_node_t;
  13325. typedef details::repeat_until_loop_bc_node<T> repeat_until_loop_bc_node_t;
  13326. typedef details::for_loop_bc_node<T> for_loop_bc_node_t;
  13327. #endif
  13328. typedef details::switch_node <T> switch_node_t;
  13329. typedef details::variable_node <T> variable_node_t;
  13330. typedef details::vector_elem_node<T> vector_elem_node_t;
  13331. typedef details::vector_node <T> vector_node_t;
  13332. typedef details::range_pack <T> range_t;
  13333. #ifndef exprtk_disable_string_capabilities
  13334. typedef details::stringvar_node <T> stringvar_node_t;
  13335. typedef details::string_literal_node<T> string_literal_node_t;
  13336. typedef details::string_range_node <T> string_range_node_t;
  13337. typedef details::const_string_range_node<T> const_string_range_node_t;
  13338. typedef details::generic_string_range_node<T> generic_string_range_node_t;
  13339. typedef details::string_concat_node <T> string_concat_node_t;
  13340. typedef details::assignment_string_node<T> assignment_string_node_t;
  13341. typedef details::assignment_string_range_node<T> assignment_string_range_node_t;
  13342. typedef details::conditional_string_node<T> conditional_string_node_t;
  13343. typedef details::cons_conditional_str_node<T> cons_conditional_str_node_t;
  13344. #endif
  13345. typedef details::assignment_node<T> assignment_node_t;
  13346. typedef details::assignment_vec_elem_node<T> assignment_vec_elem_node_t;
  13347. typedef details::assignment_vec_node <T> assignment_vec_node_t;
  13348. typedef details::assignment_vecvec_node <T> assignment_vecvec_node_t;
  13349. typedef details::scand_node<T> scand_node_t;
  13350. typedef details::scor_node<T> scor_node_t;
  13351. typedef lexer::token token_t;
  13352. typedef expression_node_t* expression_node_ptr;
  13353. typedef symbol_table<T> symbol_table_t;
  13354. typedef typename expression<T>::symtab_list_t symbol_table_list_t;
  13355. typedef details::vector_holder<T>* vector_holder_ptr;
  13356. typedef typename details::functor_t<T> functor_t;
  13357. typedef typename functor_t::qfunc_t quaternary_functor_t;
  13358. typedef typename functor_t::tfunc_t trinary_functor_t;
  13359. typedef typename functor_t::bfunc_t binary_functor_t;
  13360. typedef typename functor_t::ufunc_t unary_functor_t;
  13361. typedef details::operator_type operator_t;
  13362. typedef std::map<operator_t, unary_functor_t> unary_op_map_t;
  13363. typedef std::map<operator_t, binary_functor_t> binary_op_map_t;
  13364. typedef std::map<operator_t,trinary_functor_t> trinary_op_map_t;
  13365. typedef std::map<std::string,std::pair<trinary_functor_t ,operator_t> > sf3_map_t;
  13366. typedef std::map<std::string,std::pair<quaternary_functor_t,operator_t> > sf4_map_t;
  13367. typedef std::map<binary_functor_t,operator_t> inv_binary_op_map_t;
  13368. typedef std::multimap<std::string,details::base_operation_t,details::ilesscompare> base_ops_map_t;
  13369. typedef std::set<std::string,details::ilesscompare> disabled_func_set_t;
  13370. typedef details::T0oT1_define<T, cref_t, cref_t> vov_t;
  13371. typedef details::T0oT1_define<T,const_t, cref_t> cov_t;
  13372. typedef details::T0oT1_define<T, cref_t,const_t> voc_t;
  13373. typedef details::T0oT1oT2_define<T, cref_t, cref_t, cref_t> vovov_t;
  13374. typedef details::T0oT1oT2_define<T, cref_t, cref_t,const_t> vovoc_t;
  13375. typedef details::T0oT1oT2_define<T, cref_t,const_t, cref_t> vocov_t;
  13376. typedef details::T0oT1oT2_define<T,const_t, cref_t, cref_t> covov_t;
  13377. typedef details::T0oT1oT2_define<T,const_t, cref_t,const_t> covoc_t;
  13378. typedef details::T0oT1oT2_define<T,const_t,const_t, cref_t> cocov_t;
  13379. typedef details::T0oT1oT2_define<T,cref_t,const_t, const_t> vococ_t;
  13380. typedef details::T0oT1oT2oT3_define<T, cref_t, cref_t, cref_t, cref_t> vovovov_t;
  13381. typedef details::T0oT1oT2oT3_define<T, cref_t, cref_t, cref_t,const_t> vovovoc_t;
  13382. typedef details::T0oT1oT2oT3_define<T, cref_t, cref_t,const_t, cref_t> vovocov_t;
  13383. typedef details::T0oT1oT2oT3_define<T, cref_t,const_t, cref_t, cref_t> vocovov_t;
  13384. typedef details::T0oT1oT2oT3_define<T,const_t, cref_t, cref_t, cref_t> covovov_t;
  13385. typedef details::T0oT1oT2oT3_define<T,const_t, cref_t,const_t, cref_t> covocov_t;
  13386. typedef details::T0oT1oT2oT3_define<T, cref_t,const_t, cref_t,const_t> vocovoc_t;
  13387. typedef details::T0oT1oT2oT3_define<T,const_t, cref_t, cref_t,const_t> covovoc_t;
  13388. typedef details::T0oT1oT2oT3_define<T, cref_t,const_t,const_t, cref_t> vococov_t;
  13389. typedef results_context<T> results_context_t;
  13390. struct scope_element
  13391. {
  13392. enum element_type
  13393. {
  13394. e_none ,
  13395. e_variable,
  13396. e_vector ,
  13397. e_vecelem ,
  13398. e_string
  13399. };
  13400. typedef details::vector_holder<T> vector_holder_t;
  13401. typedef variable_node_t* variable_node_ptr;
  13402. typedef vector_holder_t* vector_holder_ptr;
  13403. #ifndef exprtk_disable_string_capabilities
  13404. typedef stringvar_node_t* stringvar_node_ptr;
  13405. #endif
  13406. scope_element()
  13407. : name("???"),
  13408. size (std::numeric_limits<std::size_t>::max()),
  13409. index(std::numeric_limits<std::size_t>::max()),
  13410. depth(std::numeric_limits<std::size_t>::max()),
  13411. ref_count(0),
  13412. ip_index (0),
  13413. type (e_none),
  13414. active(false),
  13415. data (0),
  13416. var_node(0),
  13417. vec_node(0)
  13418. #ifndef exprtk_disable_string_capabilities
  13419. ,str_node(0)
  13420. #endif
  13421. {}
  13422. bool operator < (const scope_element& se) const
  13423. {
  13424. if (ip_index < se.ip_index)
  13425. return true;
  13426. else if (ip_index > se.ip_index)
  13427. return false;
  13428. else if (depth < se.depth)
  13429. return true;
  13430. else if (depth > se.depth)
  13431. return false;
  13432. else if (index < se.index)
  13433. return true;
  13434. else if (index > se.index)
  13435. return false;
  13436. else
  13437. return (name < se.name);
  13438. }
  13439. void clear()
  13440. {
  13441. name = "???";
  13442. size = std::numeric_limits<std::size_t>::max();
  13443. index = std::numeric_limits<std::size_t>::max();
  13444. depth = std::numeric_limits<std::size_t>::max();
  13445. type = e_none;
  13446. active = false;
  13447. ref_count = 0;
  13448. ip_index = 0;
  13449. data = 0;
  13450. var_node = 0;
  13451. vec_node = 0;
  13452. #ifndef exprtk_disable_string_capabilities
  13453. str_node = 0;
  13454. #endif
  13455. }
  13456. std::string name;
  13457. std::size_t size;
  13458. std::size_t index;
  13459. std::size_t depth;
  13460. std::size_t ref_count;
  13461. std::size_t ip_index;
  13462. element_type type;
  13463. bool active;
  13464. void* data;
  13465. variable_node_ptr var_node;
  13466. vector_holder_ptr vec_node;
  13467. #ifndef exprtk_disable_string_capabilities
  13468. stringvar_node_ptr str_node;
  13469. #endif
  13470. };
  13471. class scope_element_manager
  13472. {
  13473. public:
  13474. typedef variable_node_t* variable_node_ptr;
  13475. typedef parser<T> parser_t;
  13476. scope_element_manager(parser<T>& p)
  13477. : parser_(p),
  13478. input_param_cnt_(0)
  13479. {}
  13480. inline std::size_t size() const
  13481. {
  13482. return element_.size();
  13483. }
  13484. inline bool empty() const
  13485. {
  13486. return element_.empty();
  13487. }
  13488. inline scope_element& get_element(const std::size_t& index)
  13489. {
  13490. if (index < element_.size())
  13491. return element_[index];
  13492. else
  13493. return null_element_;
  13494. }
  13495. inline scope_element& get_element(const std::string& var_name,
  13496. const std::size_t index = std::numeric_limits<std::size_t>::max())
  13497. {
  13498. const std::size_t current_depth = parser_.state_.scope_depth;
  13499. for (std::size_t i = 0; i < element_.size(); ++i)
  13500. {
  13501. scope_element& se = element_[i];
  13502. if (se.depth > current_depth)
  13503. continue;
  13504. else if (
  13505. (se.name == var_name) &&
  13506. (se.index == index)
  13507. )
  13508. return se;
  13509. }
  13510. return null_element_;
  13511. }
  13512. inline scope_element& get_active_element(const std::string& var_name,
  13513. const std::size_t index = std::numeric_limits<std::size_t>::max())
  13514. {
  13515. const std::size_t current_depth = parser_.state_.scope_depth;
  13516. for (std::size_t i = 0; i < element_.size(); ++i)
  13517. {
  13518. scope_element& se = element_[i];
  13519. if (se.depth > current_depth)
  13520. continue;
  13521. else if (
  13522. (se.name == var_name) &&
  13523. (se.index == index) &&
  13524. (se.active)
  13525. )
  13526. return se;
  13527. }
  13528. return null_element_;
  13529. }
  13530. inline bool add_element(const scope_element& se)
  13531. {
  13532. for (std::size_t i = 0; i < element_.size(); ++i)
  13533. {
  13534. scope_element& cse = element_[i];
  13535. if (
  13536. (cse.name == se.name ) &&
  13537. (cse.depth <= se.depth) &&
  13538. (cse.index == se.index) &&
  13539. (cse.size == se.size ) &&
  13540. (cse.type == se.type ) &&
  13541. (cse.active)
  13542. )
  13543. return false;
  13544. }
  13545. element_.push_back(se);
  13546. std::sort(element_.begin(),element_.end());
  13547. return true;
  13548. }
  13549. inline void deactivate(const std::size_t& scope_depth)
  13550. {
  13551. exprtk_debug(("deactivate() - Scope depth: %d\n",
  13552. static_cast<int>(parser_.state_.scope_depth)));
  13553. for (std::size_t i = 0; i < element_.size(); ++i)
  13554. {
  13555. scope_element& se = element_[i];
  13556. if (se.active && (se.depth >= scope_depth))
  13557. {
  13558. exprtk_debug(("deactivate() - element[%02d] '%s'\n",
  13559. static_cast<int>(i),
  13560. se.name.c_str()));
  13561. se.active = false;
  13562. }
  13563. }
  13564. }
  13565. inline void free_element(scope_element& se)
  13566. {
  13567. switch (se.type)
  13568. {
  13569. case scope_element::e_variable: if (se.data ) delete (T*) se.data;
  13570. if (se.var_node) delete se.var_node;
  13571. break;
  13572. case scope_element::e_vector : if (se.data ) delete[] (T*) se.data;
  13573. if (se.vec_node) delete se.vec_node;
  13574. break;
  13575. case scope_element::e_vecelem : if (se.var_node) delete se.var_node;
  13576. break;
  13577. case scope_element::e_string : if (se.data ) delete (std::string*) se.data;
  13578. if (se.str_node) delete se.str_node;
  13579. break;
  13580. default : return;
  13581. }
  13582. se.clear();
  13583. }
  13584. inline void cleanup()
  13585. {
  13586. for (std::size_t i = 0; i < element_.size(); ++i)
  13587. {
  13588. free_element(element_[i]);
  13589. }
  13590. element_.clear();
  13591. input_param_cnt_ = 0;
  13592. }
  13593. inline std::size_t next_ip_index()
  13594. {
  13595. return ++input_param_cnt_;
  13596. }
  13597. inline variable_node_ptr get_variable(const T& v)
  13598. {
  13599. for (std::size_t i = 0; i < element_.size(); ++i)
  13600. {
  13601. scope_element& se = element_[i];
  13602. if (se.active && se.var_node)
  13603. {
  13604. if (&se.var_node->ref() == (&v))
  13605. {
  13606. return se.var_node;
  13607. }
  13608. }
  13609. }
  13610. return variable_node_ptr(0);
  13611. }
  13612. private:
  13613. scope_element_manager& operator=(const scope_element_manager&);
  13614. parser_t& parser_;
  13615. std::vector<scope_element> element_;
  13616. scope_element null_element_;
  13617. std::size_t input_param_cnt_;
  13618. };
  13619. class scope_handler
  13620. {
  13621. public:
  13622. typedef parser<T> parser_t;
  13623. scope_handler(parser<T>& p)
  13624. : parser_(p)
  13625. {
  13626. parser_.state_.scope_depth++;
  13627. #ifdef exprtk_enable_debugging
  13628. std::string depth(2 * parser_.state_.scope_depth,'-');
  13629. exprtk_debug(("%s> Scope Depth: %02d\n",
  13630. depth.c_str(),
  13631. static_cast<int>(parser_.state_.scope_depth)));
  13632. #endif
  13633. }
  13634. ~scope_handler()
  13635. {
  13636. parser_.sem_.deactivate(parser_.state_.scope_depth);
  13637. parser_.state_.scope_depth--;
  13638. #ifdef exprtk_enable_debugging
  13639. std::string depth(2 * parser_.state_.scope_depth,'-');
  13640. exprtk_debug(("<%s Scope Depth: %02d\n",
  13641. depth.c_str(),
  13642. static_cast<int>(parser_.state_.scope_depth)));
  13643. #endif
  13644. }
  13645. private:
  13646. scope_handler& operator=(const scope_handler&);
  13647. parser_t& parser_;
  13648. };
  13649. struct symtab_store
  13650. {
  13651. symbol_table_list_t symtab_list_;
  13652. typedef typename symbol_table_t::local_data_t local_data_t;
  13653. typedef typename symbol_table_t::variable_ptr variable_ptr;
  13654. typedef typename symbol_table_t::function_ptr function_ptr;
  13655. #ifndef exprtk_disable_string_capabilities
  13656. typedef typename symbol_table_t::stringvar_ptr stringvar_ptr;
  13657. #endif
  13658. typedef typename symbol_table_t::vector_holder_ptr vector_holder_ptr;
  13659. typedef typename symbol_table_t::vararg_function_ptr vararg_function_ptr;
  13660. typedef typename symbol_table_t::generic_function_ptr generic_function_ptr;
  13661. inline bool empty() const
  13662. {
  13663. return symtab_list_.empty();
  13664. }
  13665. inline void clear()
  13666. {
  13667. symtab_list_.clear();
  13668. }
  13669. inline bool valid() const
  13670. {
  13671. if (!empty())
  13672. {
  13673. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13674. {
  13675. if (symtab_list_[i].valid())
  13676. return true;
  13677. }
  13678. }
  13679. return false;
  13680. }
  13681. inline bool valid_symbol(const std::string& symbol) const
  13682. {
  13683. if (!symtab_list_.empty())
  13684. return symtab_list_[0].valid_symbol(symbol);
  13685. else
  13686. return false;
  13687. }
  13688. inline bool valid_function_name(const std::string& symbol) const
  13689. {
  13690. if (!symtab_list_.empty())
  13691. return symtab_list_[0].valid_function(symbol);
  13692. else
  13693. return false;
  13694. }
  13695. inline variable_ptr get_variable(const std::string& variable_name) const
  13696. {
  13697. if (!valid_symbol(variable_name))
  13698. return reinterpret_cast<variable_ptr>(0);
  13699. variable_ptr result = reinterpret_cast<variable_ptr>(0);
  13700. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13701. {
  13702. if (!symtab_list_[i].valid())
  13703. continue;
  13704. else
  13705. result = local_data(i)
  13706. .variable_store.get(variable_name);
  13707. if (result) break;
  13708. }
  13709. return result;
  13710. }
  13711. inline variable_ptr get_variable(const T& var_ref) const
  13712. {
  13713. variable_ptr result = reinterpret_cast<variable_ptr>(0);
  13714. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13715. {
  13716. if (!symtab_list_[i].valid())
  13717. continue;
  13718. else
  13719. result = local_data(i).variable_store
  13720. .get_from_varptr(reinterpret_cast<const void*>(&var_ref));
  13721. if (result) break;
  13722. }
  13723. return result;
  13724. }
  13725. #ifndef exprtk_disable_string_capabilities
  13726. inline stringvar_ptr get_stringvar(const std::string& string_name) const
  13727. {
  13728. if (!valid_symbol(string_name))
  13729. return reinterpret_cast<stringvar_ptr>(0);
  13730. stringvar_ptr result = reinterpret_cast<stringvar_ptr>(0);
  13731. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13732. {
  13733. if (!symtab_list_[i].valid())
  13734. continue;
  13735. else
  13736. result = local_data(i)
  13737. .stringvar_store.get(string_name);
  13738. if (result) break;
  13739. }
  13740. return result;
  13741. }
  13742. #endif
  13743. inline function_ptr get_function(const std::string& function_name) const
  13744. {
  13745. if (!valid_function_name(function_name))
  13746. return reinterpret_cast<function_ptr>(0);
  13747. function_ptr result = reinterpret_cast<function_ptr>(0);
  13748. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13749. {
  13750. if (!symtab_list_[i].valid())
  13751. continue;
  13752. else
  13753. result = local_data(i)
  13754. .function_store.get(function_name);
  13755. if (result) break;
  13756. }
  13757. return result;
  13758. }
  13759. inline vararg_function_ptr get_vararg_function(const std::string& vararg_function_name) const
  13760. {
  13761. if (!valid_function_name(vararg_function_name))
  13762. return reinterpret_cast<vararg_function_ptr>(0);
  13763. vararg_function_ptr result = reinterpret_cast<vararg_function_ptr>(0);
  13764. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13765. {
  13766. if (!symtab_list_[i].valid())
  13767. continue;
  13768. else
  13769. result = local_data(i)
  13770. .vararg_function_store.get(vararg_function_name);
  13771. if (result) break;
  13772. }
  13773. return result;
  13774. }
  13775. inline generic_function_ptr get_generic_function(const std::string& function_name) const
  13776. {
  13777. if (!valid_function_name(function_name))
  13778. return reinterpret_cast<generic_function_ptr>(0);
  13779. generic_function_ptr result = reinterpret_cast<generic_function_ptr>(0);
  13780. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13781. {
  13782. if (!symtab_list_[i].valid())
  13783. continue;
  13784. else
  13785. result = local_data(i)
  13786. .generic_function_store.get(function_name);
  13787. if (result) break;
  13788. }
  13789. return result;
  13790. }
  13791. inline generic_function_ptr get_string_function(const std::string& function_name) const
  13792. {
  13793. if (!valid_function_name(function_name))
  13794. return reinterpret_cast<generic_function_ptr>(0);
  13795. generic_function_ptr result = reinterpret_cast<generic_function_ptr>(0);
  13796. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13797. {
  13798. if (!symtab_list_[i].valid())
  13799. continue;
  13800. else
  13801. result =
  13802. local_data(i).string_function_store.get(function_name);
  13803. if (result) break;
  13804. }
  13805. return result;
  13806. }
  13807. inline vector_holder_ptr get_vector(const std::string& vector_name) const
  13808. {
  13809. if (!valid_symbol(vector_name))
  13810. return reinterpret_cast<vector_holder_ptr>(0);
  13811. vector_holder_ptr result = reinterpret_cast<vector_holder_ptr>(0);
  13812. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13813. {
  13814. if (!symtab_list_[i].valid())
  13815. continue;
  13816. else
  13817. result =
  13818. local_data(i).vector_store.get(vector_name);
  13819. if (result) break;
  13820. }
  13821. return result;
  13822. }
  13823. inline bool is_constant_node(const std::string& symbol_name) const
  13824. {
  13825. if (!valid_symbol(symbol_name))
  13826. return false;
  13827. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13828. {
  13829. if (!symtab_list_[i].valid())
  13830. continue;
  13831. else if (local_data(i).variable_store.is_constant(symbol_name))
  13832. return true;
  13833. }
  13834. return false;
  13835. }
  13836. #ifndef exprtk_disable_string_capabilities
  13837. inline bool is_constant_string(const std::string& symbol_name) const
  13838. {
  13839. if (!valid_symbol(symbol_name))
  13840. return false;
  13841. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13842. {
  13843. if (!symtab_list_[i].valid())
  13844. continue;
  13845. else if (!local_data(i).stringvar_store.symbol_exists(symbol_name))
  13846. continue;
  13847. else if ( local_data(i).stringvar_store.is_constant(symbol_name))
  13848. return true;
  13849. }
  13850. return false;
  13851. }
  13852. #endif
  13853. inline bool symbol_exists(const std::string& symbol) const
  13854. {
  13855. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13856. {
  13857. if (!symtab_list_[i].valid())
  13858. continue;
  13859. else if (symtab_list_[i].symbol_exists(symbol))
  13860. return true;
  13861. }
  13862. return false;
  13863. }
  13864. inline bool is_variable(const std::string& variable_name) const
  13865. {
  13866. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13867. {
  13868. if (!symtab_list_[i].valid())
  13869. continue;
  13870. else if (
  13871. symtab_list_[i].local_data().variable_store
  13872. .symbol_exists(variable_name)
  13873. )
  13874. return true;
  13875. }
  13876. return false;
  13877. }
  13878. #ifndef exprtk_disable_string_capabilities
  13879. inline bool is_stringvar(const std::string& stringvar_name) const
  13880. {
  13881. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13882. {
  13883. if (!symtab_list_[i].valid())
  13884. continue;
  13885. else if (
  13886. symtab_list_[i].local_data().stringvar_store
  13887. .symbol_exists(stringvar_name)
  13888. )
  13889. return true;
  13890. }
  13891. return false;
  13892. }
  13893. inline bool is_conststr_stringvar(const std::string& symbol_name) const
  13894. {
  13895. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13896. {
  13897. if (!symtab_list_[i].valid())
  13898. continue;
  13899. else if (
  13900. symtab_list_[i].local_data().stringvar_store
  13901. .symbol_exists(symbol_name)
  13902. )
  13903. {
  13904. return (
  13905. local_data(i).stringvar_store.symbol_exists(symbol_name) ||
  13906. local_data(i).stringvar_store.is_constant (symbol_name)
  13907. );
  13908. }
  13909. }
  13910. return false;
  13911. }
  13912. #endif
  13913. inline bool is_function(const std::string& function_name) const
  13914. {
  13915. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13916. {
  13917. if (!symtab_list_[i].valid())
  13918. continue;
  13919. else if (
  13920. local_data(i).vararg_function_store
  13921. .symbol_exists(function_name)
  13922. )
  13923. return true;
  13924. }
  13925. return false;
  13926. }
  13927. inline bool is_vararg_function(const std::string& vararg_function_name) const
  13928. {
  13929. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13930. {
  13931. if (!symtab_list_[i].valid())
  13932. continue;
  13933. else if (
  13934. local_data(i).vararg_function_store
  13935. .symbol_exists(vararg_function_name)
  13936. )
  13937. return true;
  13938. }
  13939. return false;
  13940. }
  13941. inline bool is_vector(const std::string& vector_name) const
  13942. {
  13943. for (std::size_t i = 0; i < symtab_list_.size(); ++i)
  13944. {
  13945. if (!symtab_list_[i].valid())
  13946. continue;
  13947. else if (
  13948. local_data(i).vector_store
  13949. .symbol_exists(vector_name)
  13950. )
  13951. return true;
  13952. }
  13953. return false;
  13954. }
  13955. inline std::string get_variable_name(const expression_node_ptr& ptr) const
  13956. {
  13957. return local_data().variable_store.entity_name(ptr);
  13958. }
  13959. inline std::string get_vector_name(const vector_holder_ptr& ptr) const
  13960. {
  13961. return local_data().vector_store.entity_name(ptr);
  13962. }
  13963. #ifndef exprtk_disable_string_capabilities
  13964. inline std::string get_stringvar_name(const expression_node_ptr& ptr) const
  13965. {
  13966. return local_data().stringvar_store.entity_name(ptr);
  13967. }
  13968. inline std::string get_conststr_stringvar_name(const expression_node_ptr& ptr) const
  13969. {
  13970. return local_data().stringvar_store.entity_name(ptr);
  13971. }
  13972. #endif
  13973. inline local_data_t& local_data(const std::size_t& index = 0)
  13974. {
  13975. return symtab_list_[index].local_data();
  13976. }
  13977. inline const local_data_t& local_data(const std::size_t& index = 0) const
  13978. {
  13979. return symtab_list_[index].local_data();
  13980. }
  13981. inline symbol_table_t& get_symbol_table(const std::size_t& index = 0)
  13982. {
  13983. return symtab_list_[index];
  13984. }
  13985. };
  13986. struct parser_state
  13987. {
  13988. parser_state()
  13989. : parsing_return_stmt(false),
  13990. parsing_break_stmt (false),
  13991. return_stmt_present(false),
  13992. side_effect_present(false),
  13993. scope_depth(0)
  13994. {}
  13995. void reset()
  13996. {
  13997. parsing_return_stmt = false;
  13998. parsing_break_stmt = false;
  13999. return_stmt_present = false;
  14000. side_effect_present = false;
  14001. scope_depth = 0;
  14002. }
  14003. #ifndef exprtk_enable_debugging
  14004. void activate_side_effect(const std::string&)
  14005. #else
  14006. void activate_side_effect(const std::string& source)
  14007. #endif
  14008. {
  14009. if (!side_effect_present)
  14010. {
  14011. side_effect_present = true;
  14012. exprtk_debug(("activate_side_effect() - caller: %s\n",source.c_str()));
  14013. }
  14014. }
  14015. bool parsing_return_stmt;
  14016. bool parsing_break_stmt;
  14017. bool return_stmt_present;
  14018. bool side_effect_present;
  14019. std::size_t scope_depth;
  14020. };
  14021. public:
  14022. struct unknown_symbol_resolver
  14023. {
  14024. enum usr_symbol_type
  14025. {
  14026. e_usr_variable_type = 0,
  14027. e_usr_constant_type = 1
  14028. };
  14029. virtual ~unknown_symbol_resolver()
  14030. {}
  14031. virtual bool process(const std::string& /*unknown_symbol*/,
  14032. usr_symbol_type& st,
  14033. T& default_value,
  14034. std::string& error_message)
  14035. {
  14036. st = e_usr_variable_type;
  14037. default_value = T(0);
  14038. error_message = "";
  14039. return true;
  14040. }
  14041. };
  14042. enum collect_type
  14043. {
  14044. e_ct_none = 0,
  14045. e_ct_variables = 1,
  14046. e_ct_functions = 2,
  14047. e_ct_assignments = 4
  14048. };
  14049. enum symbol_type
  14050. {
  14051. e_st_unknown = 0,
  14052. e_st_variable = 1,
  14053. e_st_vector = 2,
  14054. e_st_string = 3,
  14055. e_st_function = 4,
  14056. e_st_local_variable = 5,
  14057. e_st_local_vector = 6,
  14058. e_st_local_string = 7
  14059. };
  14060. class dependent_entity_collector
  14061. {
  14062. public:
  14063. typedef std::pair<std::string,symbol_type> symbol_t;
  14064. typedef std::vector<symbol_t> symbol_list_t;
  14065. dependent_entity_collector(const std::size_t options = e_ct_none)
  14066. : options_(options),
  14067. collect_variables_ ((options_ & e_ct_variables ) == e_ct_variables ),
  14068. collect_functions_ ((options_ & e_ct_functions ) == e_ct_functions ),
  14069. collect_assignments_((options_ & e_ct_assignments) == e_ct_assignments),
  14070. return_present_ (false),
  14071. final_stmt_return_(false)
  14072. {}
  14073. template <typename Allocator,
  14074. template <typename,typename> class Sequence>
  14075. inline std::size_t symbols(Sequence<symbol_t,Allocator>& symbols_list)
  14076. {
  14077. if (!collect_variables_ && !collect_functions_)
  14078. return 0;
  14079. else if (symbol_name_list_.empty())
  14080. return 0;
  14081. for (std::size_t i = 0; i < symbol_name_list_.size(); ++i)
  14082. {
  14083. std::string& s = symbol_name_list_[i].first;
  14084. std::transform(s.begin(),s.end(),s.begin(),static_cast<int(*)(int)>(std::tolower));
  14085. }
  14086. std::sort(symbol_name_list_.begin(),symbol_name_list_.end());
  14087. std::unique_copy(symbol_name_list_.begin(),
  14088. symbol_name_list_.end(),
  14089. std::back_inserter(symbols_list));
  14090. return symbols_list.size();
  14091. }
  14092. template <typename Allocator,
  14093. template <typename,typename> class Sequence>
  14094. inline std::size_t assignment_symbols(Sequence<symbol_t,Allocator>& assignment_list)
  14095. {
  14096. if (!collect_assignments_)
  14097. return 0;
  14098. else if (assignment_name_list_.empty())
  14099. return 0;
  14100. for (std::size_t i = 0; i < assignment_name_list_.size(); ++i)
  14101. {
  14102. std::string& s = assignment_name_list_[i].first;
  14103. std::transform(s.begin(),s.end(),s.begin(),static_cast<int(*)(int)>(std::tolower));
  14104. }
  14105. std::sort(assignment_name_list_.begin(),assignment_name_list_.end());
  14106. std::unique_copy(assignment_name_list_.begin(),
  14107. assignment_name_list_.end(),
  14108. std::back_inserter(assignment_list));
  14109. return assignment_list.size();
  14110. }
  14111. void clear()
  14112. {
  14113. symbol_name_list_ .clear();
  14114. assignment_name_list_.clear();
  14115. retparam_list_ .clear();
  14116. return_present_ = false;
  14117. final_stmt_return_ = false;
  14118. }
  14119. bool& collect_variables()
  14120. {
  14121. return collect_variables_;
  14122. }
  14123. bool& collect_functions()
  14124. {
  14125. return collect_functions_;
  14126. }
  14127. bool& collect_assignments()
  14128. {
  14129. return collect_assignments_;
  14130. }
  14131. bool return_present() const
  14132. {
  14133. return return_present_;
  14134. }
  14135. bool final_stmt_return() const
  14136. {
  14137. return final_stmt_return_;
  14138. }
  14139. typedef std::vector<std::string> retparam_list_t;
  14140. retparam_list_t return_param_type_list() const
  14141. {
  14142. return retparam_list_;
  14143. }
  14144. private:
  14145. inline void add_symbol(const std::string& symbol, const symbol_type st)
  14146. {
  14147. switch (st)
  14148. {
  14149. case e_st_variable :
  14150. case e_st_vector :
  14151. case e_st_string :
  14152. case e_st_local_variable :
  14153. case e_st_local_vector :
  14154. case e_st_local_string :
  14155. case e_st_function :
  14156. if (collect_variables_ || collect_functions_)
  14157. symbol_name_list_.push_back(std::make_pair(symbol,st));
  14158. break;
  14159. default : return;
  14160. }
  14161. }
  14162. inline void add_assignment(const std::string& symbol, const symbol_type st)
  14163. {
  14164. switch (st)
  14165. {
  14166. case e_st_variable :
  14167. case e_st_vector :
  14168. case e_st_string :
  14169. if (collect_assignments_)
  14170. assignment_name_list_.push_back(std::make_pair(symbol,st));
  14171. break;
  14172. default : return;
  14173. }
  14174. }
  14175. std::size_t options_;
  14176. bool collect_variables_;
  14177. bool collect_functions_;
  14178. bool collect_assignments_;
  14179. bool return_present_;
  14180. bool final_stmt_return_;
  14181. symbol_list_t symbol_name_list_;
  14182. symbol_list_t assignment_name_list_;
  14183. retparam_list_t retparam_list_;
  14184. friend class parser<T>;
  14185. };
  14186. class settings_store
  14187. {
  14188. private:
  14189. typedef std::set<std::string,details::ilesscompare> disabled_entity_set_t;
  14190. typedef disabled_entity_set_t::iterator des_itr_t;
  14191. public:
  14192. enum settings_compilation_options
  14193. {
  14194. e_unknown = 0,
  14195. e_replacer = 1,
  14196. e_joiner = 2,
  14197. e_numeric_check = 4,
  14198. e_bracket_check = 8,
  14199. e_sequence_check = 16,
  14200. e_commutative_check = 32,
  14201. e_strength_reduction = 64,
  14202. e_disable_vardef = 128,
  14203. e_collect_vars = 256,
  14204. e_collect_funcs = 512,
  14205. e_collect_assings = 1024,
  14206. e_disable_usr_on_rsrvd = 2048,
  14207. e_disable_zero_return = 4096
  14208. };
  14209. enum settings_base_funcs
  14210. {
  14211. e_bf_unknown = 0,
  14212. e_bf_abs , e_bf_acos , e_bf_acosh , e_bf_asin ,
  14213. e_bf_asinh , e_bf_atan , e_bf_atan2 , e_bf_atanh ,
  14214. e_bf_avg , e_bf_ceil , e_bf_clamp , e_bf_cos ,
  14215. e_bf_cosh , e_bf_cot , e_bf_csc , e_bf_equal ,
  14216. e_bf_erf , e_bf_erfc , e_bf_exp , e_bf_expm1 ,
  14217. e_bf_floor , e_bf_frac , e_bf_hypot , e_bf_iclamp ,
  14218. e_bf_like , e_bf_log , e_bf_log10 , e_bf_log1p ,
  14219. e_bf_log2 , e_bf_logn , e_bf_mand , e_bf_max ,
  14220. e_bf_min , e_bf_mod , e_bf_mor , e_bf_mul ,
  14221. e_bf_ncdf , e_bf_pow , e_bf_root , e_bf_round ,
  14222. e_bf_roundn , e_bf_sec , e_bf_sgn , e_bf_sin ,
  14223. e_bf_sinc , e_bf_sinh , e_bf_sqrt , e_bf_sum ,
  14224. e_bf_swap , e_bf_tan , e_bf_tanh , e_bf_trunc ,
  14225. e_bf_not_equal , e_bf_inrange , e_bf_deg2grad , e_bf_deg2rad,
  14226. e_bf_rad2deg , e_bf_grad2deg
  14227. };
  14228. enum settings_control_structs
  14229. {
  14230. e_ctrl_unknown = 0,
  14231. e_ctrl_ifelse,
  14232. e_ctrl_switch,
  14233. e_ctrl_for_loop,
  14234. e_ctrl_while_loop,
  14235. e_ctrl_repeat_loop
  14236. };
  14237. enum settings_logic_opr
  14238. {
  14239. e_logic_unknown = 0,
  14240. e_logic_and, e_logic_nand, e_logic_nor,
  14241. e_logic_not, e_logic_or, e_logic_xnor,
  14242. e_logic_xor, e_logic_scand, e_logic_scor
  14243. };
  14244. static const std::size_t compile_all_opts = e_replacer +
  14245. e_joiner +
  14246. e_numeric_check +
  14247. e_bracket_check +
  14248. e_sequence_check +
  14249. e_commutative_check +
  14250. e_strength_reduction;
  14251. settings_store(const std::size_t compile_options = compile_all_opts)
  14252. {
  14253. load_compile_options(compile_options);
  14254. }
  14255. settings_store& enable_all_base_functions()
  14256. {
  14257. disabled_func_set_.clear();
  14258. return *this;
  14259. }
  14260. settings_store& enable_all_control_structures()
  14261. {
  14262. disabled_ctrl_set_.clear();
  14263. return *this;
  14264. }
  14265. settings_store& enable_all_logic_ops()
  14266. {
  14267. disabled_logic_set_.clear();
  14268. return *this;
  14269. }
  14270. settings_store& disable_all_base_functions()
  14271. {
  14272. std::copy(details::base_function_list,
  14273. details::base_function_list + details::base_function_list_size,
  14274. std::insert_iterator<disabled_entity_set_t>
  14275. (disabled_func_set_,disabled_func_set_.begin()));
  14276. return *this;
  14277. }
  14278. settings_store& disable_all_control_structures()
  14279. {
  14280. std::copy(details::cntrl_struct_list,
  14281. details::cntrl_struct_list + details::cntrl_struct_list_size,
  14282. std::insert_iterator<disabled_entity_set_t>
  14283. (disabled_ctrl_set_,disabled_ctrl_set_.begin()));
  14284. return *this;
  14285. }
  14286. settings_store& disable_all_logic_ops()
  14287. {
  14288. std::copy(details::logic_ops_list,
  14289. details::logic_ops_list + details::logic_ops_list_size,
  14290. std::insert_iterator<disabled_entity_set_t>
  14291. (disabled_logic_set_,disabled_logic_set_.begin()));
  14292. return *this;
  14293. }
  14294. bool replacer_enabled () const { return enable_replacer_; }
  14295. bool commutative_check_enabled () const { return enable_commutative_check_; }
  14296. bool joiner_enabled () const { return enable_joiner_; }
  14297. bool numeric_check_enabled () const { return enable_numeric_check_; }
  14298. bool bracket_check_enabled () const { return enable_bracket_check_; }
  14299. bool sequence_check_enabled () const { return enable_sequence_check_; }
  14300. bool strength_reduction_enabled () const { return enable_strength_reduction_; }
  14301. bool collect_variables_enabled () const { return enable_collect_vars_; }
  14302. bool collect_functions_enabled () const { return enable_collect_funcs_; }
  14303. bool collect_assignments_enabled() const { return enable_collect_assings_; }
  14304. bool vardef_disabled () const { return disable_vardef_; }
  14305. bool rsrvd_sym_usr_disabled () const { return disable_rsrvd_sym_usr_; }
  14306. bool zero_return_disabled () const { return disable_zero_return_; }
  14307. bool function_enabled(const std::string& function_name)
  14308. {
  14309. if (disabled_func_set_.empty())
  14310. return true;
  14311. else
  14312. return (disabled_func_set_.end() == disabled_func_set_.find(function_name));
  14313. }
  14314. bool control_struct_enabled(const std::string& control_struct)
  14315. {
  14316. if (disabled_ctrl_set_.empty())
  14317. return true;
  14318. else
  14319. return (disabled_ctrl_set_.end() == disabled_ctrl_set_.find(control_struct));
  14320. }
  14321. bool logic_enabled(const std::string& logic_operation)
  14322. {
  14323. if (disabled_logic_set_.empty())
  14324. return true;
  14325. else
  14326. return (disabled_logic_set_.end() == disabled_logic_set_.find(logic_operation));
  14327. }
  14328. bool function_disabled(const std::string& function_name)
  14329. {
  14330. if (disabled_func_set_.empty())
  14331. return false;
  14332. else
  14333. return (disabled_func_set_.end() != disabled_func_set_.find(function_name));
  14334. }
  14335. bool control_struct_disabled(const std::string& control_struct)
  14336. {
  14337. if (disabled_ctrl_set_.empty())
  14338. return false;
  14339. else
  14340. return (disabled_ctrl_set_.end() != disabled_ctrl_set_.find(control_struct));
  14341. }
  14342. bool logic_disabled(const std::string& logic_operation)
  14343. {
  14344. if (disabled_logic_set_.empty())
  14345. return false;
  14346. else
  14347. return (disabled_logic_set_.end() != disabled_logic_set_.find(logic_operation));
  14348. }
  14349. settings_store& disable_base_function(settings_base_funcs bf)
  14350. {
  14351. if (
  14352. (e_bf_unknown != bf) &&
  14353. (bf < details::base_function_list_size)
  14354. )
  14355. {
  14356. disabled_func_set_.insert(details::base_function_list[bf - 1]);
  14357. }
  14358. return *this;
  14359. }
  14360. settings_store& disable_control_structure(settings_control_structs ctrl_struct)
  14361. {
  14362. if (
  14363. (e_ctrl_unknown != ctrl_struct) &&
  14364. (ctrl_struct < details::cntrl_struct_list_size)
  14365. )
  14366. {
  14367. disabled_ctrl_set_.insert(details::cntrl_struct_list[ctrl_struct - 1]);
  14368. }
  14369. return *this;
  14370. }
  14371. settings_store& disable_logic_operation(settings_logic_opr logic)
  14372. {
  14373. if (
  14374. (e_logic_unknown != logic) &&
  14375. (logic < details::logic_ops_list_size)
  14376. )
  14377. {
  14378. disabled_logic_set_.insert(details::logic_ops_list[logic - 1]);
  14379. }
  14380. return *this;
  14381. }
  14382. settings_store& enable_base_function(settings_base_funcs bf)
  14383. {
  14384. if (
  14385. (e_bf_unknown != bf) &&
  14386. (bf < details::base_function_list_size)
  14387. )
  14388. {
  14389. des_itr_t itr = disabled_func_set_.find(details::base_function_list[bf - 1]);
  14390. if (disabled_func_set_.end() != itr)
  14391. {
  14392. disabled_func_set_.erase(itr);
  14393. }
  14394. }
  14395. return *this;
  14396. }
  14397. settings_store& enable_control_structure(settings_control_structs ctrl_struct)
  14398. {
  14399. if (
  14400. (e_ctrl_unknown != ctrl_struct) &&
  14401. (ctrl_struct < details::cntrl_struct_list_size)
  14402. )
  14403. {
  14404. des_itr_t itr = disabled_ctrl_set_.find(details::cntrl_struct_list[ctrl_struct - 1]);
  14405. if (disabled_ctrl_set_.end() != itr)
  14406. {
  14407. disabled_ctrl_set_.erase(itr);
  14408. }
  14409. }
  14410. return *this;
  14411. }
  14412. settings_store& enable_logic_operation(settings_logic_opr logic)
  14413. {
  14414. if (
  14415. (e_logic_unknown != logic) &&
  14416. (logic < details::logic_ops_list_size)
  14417. )
  14418. {
  14419. des_itr_t itr = disabled_logic_set_.find(details::logic_ops_list[logic - 1]);
  14420. if (disabled_logic_set_.end() != itr)
  14421. {
  14422. disabled_logic_set_.erase(itr);
  14423. }
  14424. }
  14425. return *this;
  14426. }
  14427. private:
  14428. void load_compile_options(const std::size_t compile_options)
  14429. {
  14430. enable_replacer_ = (compile_options & e_replacer ) == e_replacer;
  14431. enable_joiner_ = (compile_options & e_joiner ) == e_joiner;
  14432. enable_numeric_check_ = (compile_options & e_numeric_check ) == e_numeric_check;
  14433. enable_bracket_check_ = (compile_options & e_bracket_check ) == e_bracket_check;
  14434. enable_sequence_check_ = (compile_options & e_sequence_check ) == e_sequence_check;
  14435. enable_commutative_check_ = (compile_options & e_commutative_check ) == e_commutative_check;
  14436. enable_strength_reduction_ = (compile_options & e_strength_reduction ) == e_strength_reduction;
  14437. enable_collect_vars_ = (compile_options & e_collect_vars ) == e_collect_vars;
  14438. enable_collect_funcs_ = (compile_options & e_collect_funcs ) == e_collect_funcs;
  14439. enable_collect_assings_ = (compile_options & e_collect_assings ) == e_collect_assings;
  14440. disable_vardef_ = (compile_options & e_disable_vardef ) == e_disable_vardef;
  14441. disable_rsrvd_sym_usr_ = (compile_options & e_disable_usr_on_rsrvd) == e_disable_usr_on_rsrvd;
  14442. disable_zero_return_ = (compile_options & e_disable_zero_return ) == e_disable_zero_return;
  14443. }
  14444. bool enable_replacer_;
  14445. bool enable_joiner_;
  14446. bool enable_numeric_check_;
  14447. bool enable_bracket_check_;
  14448. bool enable_sequence_check_;
  14449. bool enable_commutative_check_;
  14450. bool enable_strength_reduction_;
  14451. bool enable_collect_vars_;
  14452. bool enable_collect_funcs_;
  14453. bool enable_collect_assings_;
  14454. bool disable_vardef_;
  14455. bool disable_rsrvd_sym_usr_;
  14456. bool disable_zero_return_;
  14457. disabled_entity_set_t disabled_func_set_ ;
  14458. disabled_entity_set_t disabled_ctrl_set_ ;
  14459. disabled_entity_set_t disabled_logic_set_;
  14460. friend class parser<T>;
  14461. };
  14462. typedef settings_store settings_t;
  14463. parser(const settings_t& settings = settings_t())
  14464. : settings_(settings),
  14465. resolve_unknown_symbol_(false),
  14466. results_context_(0),
  14467. unknown_symbol_resolver_(reinterpret_cast<unknown_symbol_resolver*>(0)),
  14468. #ifdef _MSC_VER
  14469. #pragma warning(push)
  14470. #pragma warning (disable:4355)
  14471. #endif
  14472. sem_(*this),
  14473. #ifdef _MSC_VER
  14474. #pragma warning(pop)
  14475. #endif
  14476. operator_joiner_2_(2),
  14477. operator_joiner_3_(3)
  14478. {
  14479. init_precompilation();
  14480. load_operations_map (base_ops_map_ );
  14481. load_unary_operations_map (unary_op_map_ );
  14482. load_binary_operations_map (binary_op_map_ );
  14483. load_inv_binary_operations_map(inv_binary_op_map_);
  14484. load_sf3_map (sf3_map_ );
  14485. load_sf4_map (sf4_map_ );
  14486. expression_generator_.init_synthesize_map();
  14487. expression_generator_.set_parser(*this);
  14488. expression_generator_.set_uom(unary_op_map_);
  14489. expression_generator_.set_bom(binary_op_map_);
  14490. expression_generator_.set_ibom(inv_binary_op_map_);
  14491. expression_generator_.set_sf3m(sf3_map_);
  14492. expression_generator_.set_sf4m(sf4_map_);
  14493. expression_generator_.set_strength_reduction_state(settings_.strength_reduction_enabled());
  14494. }
  14495. ~parser()
  14496. {}
  14497. inline void init_precompilation()
  14498. {
  14499. if (settings_.collect_variables_enabled())
  14500. dec_.collect_variables() = true;
  14501. if (settings_.collect_functions_enabled())
  14502. dec_.collect_functions() = true;
  14503. if (settings_.collect_assignments_enabled())
  14504. dec_.collect_assignments() = true;
  14505. if (settings_.replacer_enabled())
  14506. {
  14507. symbol_replacer_.clear();
  14508. symbol_replacer_.add_replace("true" ,"1",lexer::token::e_number);
  14509. symbol_replacer_.add_replace("false","0",lexer::token::e_number);
  14510. helper_assembly_.token_modifier_list.clear();
  14511. helper_assembly_.register_modifier(&symbol_replacer_);
  14512. }
  14513. if (settings_.commutative_check_enabled())
  14514. {
  14515. for (std::size_t i = 0; i < details::reserved_words_size; ++i)
  14516. {
  14517. commutative_inserter_.ignore_symbol(details::reserved_words[i]);
  14518. }
  14519. helper_assembly_.token_inserter_list.clear();
  14520. helper_assembly_.register_inserter(&commutative_inserter_);
  14521. }
  14522. if (settings_.joiner_enabled())
  14523. {
  14524. helper_assembly_.token_joiner_list.clear();
  14525. helper_assembly_.register_joiner(&operator_joiner_2_);
  14526. helper_assembly_.register_joiner(&operator_joiner_3_);
  14527. }
  14528. if (
  14529. settings_.numeric_check_enabled () ||
  14530. settings_.bracket_check_enabled () ||
  14531. settings_.sequence_check_enabled()
  14532. )
  14533. {
  14534. helper_assembly_.token_scanner_list.clear();
  14535. if (settings_.numeric_check_enabled())
  14536. {
  14537. helper_assembly_.register_scanner(&numeric_checker_);
  14538. }
  14539. if (settings_.bracket_check_enabled())
  14540. {
  14541. helper_assembly_.register_scanner(&bracket_checker_);
  14542. }
  14543. if (settings_.sequence_check_enabled())
  14544. {
  14545. helper_assembly_.register_scanner(&sequence_validator_);
  14546. }
  14547. }
  14548. }
  14549. inline bool compile(const std::string& expression_string, expression<T>& expr)
  14550. {
  14551. state_ .reset();
  14552. error_list_ .clear();
  14553. brkcnt_list_ .clear();
  14554. synthesis_error_.clear();
  14555. sem_ .cleanup();
  14556. return_cleanup();
  14557. expression_generator_.set_allocator(node_allocator_);
  14558. if (expression_string.empty())
  14559. {
  14560. set_error(
  14561. make_error(parser_error::e_syntax,
  14562. "ERR000 - Empty expression!"));
  14563. return false;
  14564. }
  14565. if (!init(expression_string))
  14566. {
  14567. process_lexer_errors();
  14568. return false;
  14569. }
  14570. if (lexer().empty())
  14571. {
  14572. set_error(
  14573. make_error(parser_error::e_syntax,
  14574. "ERR001 - Empty expression!"));
  14575. return false;
  14576. }
  14577. if (!run_assemblies())
  14578. {
  14579. return false;
  14580. }
  14581. symtab_store_.symtab_list_ = expr.get_symbol_table_list();
  14582. dec_.clear();
  14583. lexer().begin();
  14584. next_token();
  14585. expression_node_ptr e = parse_corpus();
  14586. if ((0 != e) && (token_t::e_eof == current_token().type))
  14587. {
  14588. bool* retinvk_ptr = 0;
  14589. if (state_.return_stmt_present)
  14590. {
  14591. dec_.return_present_ = true;
  14592. e = expression_generator_
  14593. .return_envelope(e,results_context_,retinvk_ptr);
  14594. }
  14595. expr.set_expression(e);
  14596. expr.set_retinvk(retinvk_ptr);
  14597. register_local_vars(expr);
  14598. register_return_results(expr);
  14599. return !(!expr);
  14600. }
  14601. else
  14602. {
  14603. if (error_list_.empty())
  14604. {
  14605. set_error(
  14606. make_error(parser_error::e_syntax,
  14607. current_token(),
  14608. "ERR002 - Invalid expression encountered"));
  14609. }
  14610. dec_.clear ();
  14611. sem_.cleanup ();
  14612. return_cleanup();
  14613. if (0 != e)
  14614. {
  14615. delete e;
  14616. }
  14617. return false;
  14618. }
  14619. }
  14620. void process_lexer_errors()
  14621. {
  14622. for (std::size_t i = 0; i < lexer().size(); ++i)
  14623. {
  14624. if (lexer()[i].is_error())
  14625. {
  14626. std::string diagnostic = "ERR003 - ";
  14627. switch (lexer()[i].type)
  14628. {
  14629. case lexer::token::e_error : diagnostic += "General token error";
  14630. break;
  14631. case lexer::token::e_err_symbol : diagnostic += "Symbol error";
  14632. break;
  14633. case lexer::token::e_err_number : diagnostic += "Invalid numeric token";
  14634. break;
  14635. case lexer::token::e_err_string : diagnostic += "Invalid string token";
  14636. break;
  14637. case lexer::token::e_err_sfunc : diagnostic += "Invalid special function token";
  14638. break;
  14639. default : diagnostic += "Unknown compiler error";
  14640. }
  14641. set_error(
  14642. make_error(parser_error::e_lexer,
  14643. lexer()[i],
  14644. diagnostic + ": " + lexer()[i].value));
  14645. }
  14646. }
  14647. }
  14648. inline bool run_assemblies()
  14649. {
  14650. if (settings_.commutative_check_enabled())
  14651. {
  14652. helper_assembly_.run_inserters(lexer());
  14653. }
  14654. if (settings_.joiner_enabled())
  14655. {
  14656. helper_assembly_.run_joiners(lexer());
  14657. }
  14658. if (settings_.replacer_enabled())
  14659. {
  14660. helper_assembly_.run_modifiers(lexer());
  14661. }
  14662. if (
  14663. settings_.numeric_check_enabled () ||
  14664. settings_.bracket_check_enabled () ||
  14665. settings_.sequence_check_enabled()
  14666. )
  14667. {
  14668. if (!helper_assembly_.run_scanners(lexer()))
  14669. {
  14670. if (helper_assembly_.error_token_scanner)
  14671. {
  14672. lexer::helper::bracket_checker* bracket_checker_ptr = 0;
  14673. lexer::helper::numeric_checker* numeric_checker_ptr = 0;
  14674. lexer::helper::sequence_validator* sequence_validator_ptr = 0;
  14675. if (0 != (bracket_checker_ptr = dynamic_cast<lexer::helper::bracket_checker*>(helper_assembly_.error_token_scanner)))
  14676. {
  14677. set_error(
  14678. make_error(parser_error::e_token,
  14679. bracket_checker_ptr->error_token(),
  14680. "ERR004 - Mismatched brackets: '" + bracket_checker_ptr->error_token().value + "'"));
  14681. }
  14682. else if (0 != (numeric_checker_ptr = dynamic_cast<lexer::helper::numeric_checker*>(helper_assembly_.error_token_scanner)))
  14683. {
  14684. for (std::size_t i = 0; i < numeric_checker_ptr->error_count(); ++i)
  14685. {
  14686. lexer::token error_token = lexer()[numeric_checker_ptr->error_index(i)];
  14687. set_error(
  14688. make_error(parser_error::e_token,
  14689. error_token,
  14690. "ERR005 - Invalid numeric token: '" + error_token.value + "'"));
  14691. }
  14692. if (numeric_checker_ptr->error_count())
  14693. {
  14694. numeric_checker_ptr->clear_errors();
  14695. }
  14696. }
  14697. else if (0 != (sequence_validator_ptr = dynamic_cast<lexer::helper::sequence_validator*>(helper_assembly_.error_token_scanner)))
  14698. {
  14699. for (std::size_t i = 0; i < sequence_validator_ptr->error_count(); ++i)
  14700. {
  14701. std::pair<lexer::token,lexer::token> error_token = sequence_validator_ptr->error(i);
  14702. set_error(
  14703. make_error(parser_error::e_token,
  14704. error_token.first,
  14705. "ERR006 - Invalid token sequence: '" +
  14706. error_token.first.value + "' and '" +
  14707. error_token.second.value + "'"));
  14708. }
  14709. if (sequence_validator_ptr->error_count())
  14710. {
  14711. sequence_validator_ptr->clear_errors();
  14712. }
  14713. }
  14714. }
  14715. return false;
  14716. }
  14717. }
  14718. return true;
  14719. }
  14720. inline settings_store& settings()
  14721. {
  14722. return settings_;
  14723. }
  14724. inline parser_error::type get_error(const std::size_t& index)
  14725. {
  14726. if (index < error_list_.size())
  14727. return error_list_[index];
  14728. else
  14729. throw std::invalid_argument("parser::get_error() - Invalid error index specificed");
  14730. }
  14731. inline std::string error() const
  14732. {
  14733. if (!error_list_.empty())
  14734. {
  14735. return error_list_[0].diagnostic;
  14736. }
  14737. else
  14738. return std::string("No Error");
  14739. }
  14740. inline std::size_t error_count() const
  14741. {
  14742. return error_list_.size();
  14743. }
  14744. inline dependent_entity_collector& dec()
  14745. {
  14746. return dec_;
  14747. }
  14748. inline bool replace_symbol(const std::string& old_symbol, const std::string& new_symbol)
  14749. {
  14750. if (!settings_.replacer_enabled())
  14751. return false;
  14752. else if (details::is_reserved_word(old_symbol))
  14753. return false;
  14754. else
  14755. return symbol_replacer_.add_replace(old_symbol,new_symbol,lexer::token::e_symbol);
  14756. }
  14757. inline bool remove_replace_symbol(const std::string& symbol)
  14758. {
  14759. if (!settings_.replacer_enabled())
  14760. return false;
  14761. else if (details::is_reserved_word(symbol))
  14762. return false;
  14763. else
  14764. return symbol_replacer_.remove(symbol);
  14765. }
  14766. inline void enable_unknown_symbol_resolver(unknown_symbol_resolver* usr = reinterpret_cast<unknown_symbol_resolver*>(0))
  14767. {
  14768. resolve_unknown_symbol_ = true;
  14769. if (usr)
  14770. unknown_symbol_resolver_ = usr;
  14771. else
  14772. unknown_symbol_resolver_ = &default_usr_;
  14773. }
  14774. inline void disable_unknown_symbol_resolver()
  14775. {
  14776. resolve_unknown_symbol_ = false;
  14777. unknown_symbol_resolver_ = &default_usr_;
  14778. }
  14779. private:
  14780. inline bool valid_base_operation(const std::string& symbol)
  14781. {
  14782. const std::size_t length = symbol.size();
  14783. if (
  14784. (length < 3) || // Shortest base op symbol length
  14785. (length > 9) // Longest base op symbol length
  14786. )
  14787. return false;
  14788. else
  14789. return settings_.function_enabled(symbol) &&
  14790. (base_ops_map_.end() != base_ops_map_.find(symbol));
  14791. }
  14792. inline bool valid_vararg_operation(const std::string& symbol)
  14793. {
  14794. static const std::string s_sum = "sum" ;
  14795. static const std::string s_mul = "mul" ;
  14796. static const std::string s_avg = "avg" ;
  14797. static const std::string s_min = "min" ;
  14798. static const std::string s_max = "max" ;
  14799. static const std::string s_mand = "mand";
  14800. static const std::string s_mor = "mor" ;
  14801. static const std::string s_multi = "~" ;
  14802. static const std::string s_mswitch = "[*]" ;
  14803. return
  14804. (
  14805. details::imatch(symbol,s_sum ) ||
  14806. details::imatch(symbol,s_mul ) ||
  14807. details::imatch(symbol,s_avg ) ||
  14808. details::imatch(symbol,s_min ) ||
  14809. details::imatch(symbol,s_max ) ||
  14810. details::imatch(symbol,s_mand ) ||
  14811. details::imatch(symbol,s_mor ) ||
  14812. details::imatch(symbol,s_multi ) ||
  14813. details::imatch(symbol,s_mswitch)
  14814. ) &&
  14815. settings_.function_enabled(symbol);
  14816. }
  14817. #ifdef exprtk_enable_debugging
  14818. inline void next_token()
  14819. {
  14820. std::string ct_str = current_token().value;
  14821. parser_helper::next_token();
  14822. std::string depth(2 * state_.scope_depth,' ');
  14823. exprtk_debug(("%s"
  14824. "prev[%s] --> curr[%s]\n",
  14825. depth.c_str(),
  14826. ct_str.c_str(),
  14827. current_token().value.c_str()));
  14828. }
  14829. #endif
  14830. inline expression_node_ptr parse_corpus()
  14831. {
  14832. std::vector<expression_node_ptr> arg_list;
  14833. std::vector<bool> side_effect_list;
  14834. expression_node_ptr result = error_node();
  14835. scoped_vec_delete<expression_node_t> sdd(*this,arg_list);
  14836. lexer::token begin_token;
  14837. lexer::token end_token;
  14838. for ( ; ; )
  14839. {
  14840. state_.side_effect_present = false;
  14841. begin_token = current_token();
  14842. expression_node_ptr arg = parse_expression();
  14843. if (0 == arg)
  14844. {
  14845. if (error_list_.empty())
  14846. {
  14847. set_error(
  14848. make_error(parser_error::e_syntax,
  14849. current_token(),
  14850. "ERR007 - Invalid expression encountered"));
  14851. }
  14852. return error_node();
  14853. }
  14854. else
  14855. {
  14856. arg_list.push_back(arg);
  14857. side_effect_list.push_back(state_.side_effect_present);
  14858. end_token = current_token();
  14859. std::string sub_expr = construct_subexpr(begin_token,end_token);
  14860. exprtk_debug(("parse_corpus(%02d) Subexpr: %s\n",
  14861. static_cast<int>(arg_list.size() - 1),
  14862. sub_expr.c_str()));
  14863. exprtk_debug(("parse_corpus(%02d) - Side effect present: %s\n",
  14864. static_cast<int>(arg_list.size() - 1),
  14865. state_.side_effect_present ? "true" : "false"));
  14866. exprtk_debug(("-------------------------------------------------\n"));
  14867. }
  14868. if (lexer().finished())
  14869. break;
  14870. else if (token_is(token_t::e_eof,false))
  14871. {
  14872. if (lexer().finished())
  14873. break;
  14874. else
  14875. next_token();
  14876. }
  14877. }
  14878. if (
  14879. !arg_list.empty() &&
  14880. is_return_node(arg_list.back())
  14881. )
  14882. {
  14883. dec_.final_stmt_return_ = true;
  14884. }
  14885. result = simplify(arg_list,side_effect_list);
  14886. sdd.delete_ptr = (0 == result);
  14887. return result;
  14888. }
  14889. std::string construct_subexpr(lexer::token& begin_token, lexer::token& end_token)
  14890. {
  14891. std::string result = lexer().substr(begin_token.position,end_token.position);
  14892. for (std::size_t i = 0; i < result.size(); ++i)
  14893. {
  14894. if (details::is_whitespace(result[i])) result[i] = ' ';
  14895. }
  14896. return result;
  14897. }
  14898. static const precedence_level default_precedence = e_level00;
  14899. struct state_t
  14900. {
  14901. inline void set(const precedence_level& l,
  14902. const precedence_level& r,
  14903. const details::operator_type& o)
  14904. {
  14905. left = l;
  14906. right = r;
  14907. operation = o;
  14908. }
  14909. inline void reset()
  14910. {
  14911. left = e_level00;
  14912. right = e_level00;
  14913. }
  14914. precedence_level left;
  14915. precedence_level right;
  14916. details::operator_type operation;
  14917. };
  14918. inline expression_node_ptr parse_expression(precedence_level precedence = e_level00)
  14919. {
  14920. expression_node_ptr expression = parse_branch(precedence);
  14921. if (0 == expression)
  14922. {
  14923. return error_node();
  14924. }
  14925. bool break_loop = false;
  14926. state_t current_state;
  14927. for ( ; ; )
  14928. {
  14929. current_state.reset();
  14930. switch (current_token().type)
  14931. {
  14932. case token_t::e_assign : current_state.set(e_level00,e_level00,details::e_assign); break;
  14933. case token_t::e_addass : current_state.set(e_level00,e_level00,details::e_addass); break;
  14934. case token_t::e_subass : current_state.set(e_level00,e_level00,details::e_subass); break;
  14935. case token_t::e_mulass : current_state.set(e_level00,e_level00,details::e_mulass); break;
  14936. case token_t::e_divass : current_state.set(e_level00,e_level00,details::e_divass); break;
  14937. case token_t::e_modass : current_state.set(e_level00,e_level00,details::e_modass); break;
  14938. case token_t::e_swap : current_state.set(e_level00,e_level00,details::e_swap ); break;
  14939. case token_t::e_lt : current_state.set(e_level05,e_level06,details:: e_lt); break;
  14940. case token_t::e_lte : current_state.set(e_level05,e_level06,details:: e_lte); break;
  14941. case token_t::e_eq : current_state.set(e_level05,e_level06,details:: e_eq); break;
  14942. case token_t::e_ne : current_state.set(e_level05,e_level06,details:: e_ne); break;
  14943. case token_t::e_gte : current_state.set(e_level05,e_level06,details:: e_gte); break;
  14944. case token_t::e_gt : current_state.set(e_level05,e_level06,details:: e_gt); break;
  14945. case token_t::e_add : current_state.set(e_level07,e_level08,details:: e_add); break;
  14946. case token_t::e_sub : current_state.set(e_level07,e_level08,details:: e_sub); break;
  14947. case token_t::e_div : current_state.set(e_level10,e_level11,details:: e_div); break;
  14948. case token_t::e_mul : current_state.set(e_level10,e_level11,details:: e_mul); break;
  14949. case token_t::e_mod : current_state.set(e_level10,e_level11,details:: e_mod); break;
  14950. case token_t::e_pow : current_state.set(e_level12,e_level12,details:: e_pow); break;
  14951. default : if (token_t::e_symbol == current_token().type)
  14952. {
  14953. static const std::string s_and = "and";
  14954. static const std::string s_nand = "nand";
  14955. static const std::string s_or = "or";
  14956. static const std::string s_nor = "nor";
  14957. static const std::string s_xor = "xor";
  14958. static const std::string s_xnor = "xnor";
  14959. static const std::string s_in = "in";
  14960. static const std::string s_like = "like";
  14961. static const std::string s_ilike = "ilike";
  14962. static const std::string s_and1 = "&";
  14963. static const std::string s_or1 = "|";
  14964. if (details::imatch(current_token().value,s_and))
  14965. {
  14966. current_state.set(e_level03,e_level04,details::e_and);
  14967. break;
  14968. }
  14969. else if (details::imatch(current_token().value,s_and1))
  14970. {
  14971. #ifndef exprtk_disable_sc_andor
  14972. current_state.set(e_level03,e_level04,details::e_scand);
  14973. #else
  14974. current_state.set(e_level03,e_level04,details::e_and);
  14975. #endif
  14976. break;
  14977. }
  14978. else if (details::imatch(current_token().value,s_nand))
  14979. {
  14980. current_state.set(e_level03,e_level04,details::e_nand);
  14981. break;
  14982. }
  14983. else if (details::imatch(current_token().value,s_or))
  14984. {
  14985. current_state.set(e_level01,e_level02,details::e_or);
  14986. break;
  14987. }
  14988. else if (details::imatch(current_token().value,s_or1))
  14989. {
  14990. #ifndef exprtk_disable_sc_andor
  14991. current_state.set(e_level01,e_level02,details::e_scor);
  14992. #else
  14993. current_state.set(e_level01,e_level02,details::e_or);
  14994. #endif
  14995. break;
  14996. }
  14997. else if (details::imatch(current_token().value,s_nor))
  14998. {
  14999. current_state.set(e_level01,e_level02,details::e_nor);
  15000. break;
  15001. }
  15002. else if (details::imatch(current_token().value,s_xor))
  15003. {
  15004. current_state.set(e_level01,e_level02,details::e_xor);
  15005. break;
  15006. }
  15007. else if (details::imatch(current_token().value,s_xnor))
  15008. {
  15009. current_state.set(e_level01,e_level02,details::e_xnor);
  15010. break;
  15011. }
  15012. else if (details::imatch(current_token().value,s_in))
  15013. {
  15014. current_state.set(e_level03,e_level04,details::e_in);
  15015. break;
  15016. }
  15017. else if (details::imatch(current_token().value,s_like))
  15018. {
  15019. current_state.set(e_level03,e_level04,details::e_like);
  15020. break;
  15021. }
  15022. else if (details::imatch(current_token().value,s_ilike))
  15023. {
  15024. current_state.set(e_level03,e_level04,details::e_ilike);
  15025. break;
  15026. }
  15027. }
  15028. break_loop = true;
  15029. }
  15030. if (break_loop)
  15031. {
  15032. parse_pending_string_rangesize(expression);
  15033. break;
  15034. }
  15035. else if (current_state.left < precedence)
  15036. break;
  15037. lexer::token prev_token = current_token();
  15038. next_token();
  15039. expression_node_ptr right_branch = error_node();
  15040. expression_node_ptr new_expression = error_node();
  15041. if ((right_branch = parse_expression(current_state.right)))
  15042. {
  15043. if (
  15044. details::is_return_node( expression) ||
  15045. details::is_return_node(right_branch)
  15046. )
  15047. {
  15048. free_node(node_allocator_, expression);
  15049. free_node(node_allocator_,right_branch);
  15050. set_error(
  15051. make_error(parser_error::e_syntax,
  15052. prev_token,
  15053. "ERR008 - Return statements cannot be part of sub-expressions"));
  15054. return error_node();
  15055. }
  15056. new_expression = expression_generator_
  15057. (
  15058. current_state.operation,
  15059. expression,
  15060. right_branch
  15061. );
  15062. }
  15063. if (0 == new_expression)
  15064. {
  15065. if (error_list_.empty())
  15066. {
  15067. set_error(
  15068. make_error(parser_error::e_syntax,
  15069. prev_token,
  15070. !synthesis_error_.empty() ?
  15071. synthesis_error_ :
  15072. "ERR009 - General parsing error at token: '" + prev_token.value + "'"));
  15073. }
  15074. free_node(node_allocator_,expression);
  15075. return error_node();
  15076. }
  15077. else
  15078. {
  15079. expression = new_expression;
  15080. if (token_is(token_t::e_ternary,false) && (precedence == e_level00))
  15081. {
  15082. expression = parse_ternary_conditional_statement(expression);
  15083. }
  15084. parse_pending_string_rangesize(expression);
  15085. }
  15086. }
  15087. return expression;
  15088. }
  15089. bool simplify_unary_negation_branch(expression_node_ptr& node)
  15090. {
  15091. {
  15092. typedef details::unary_branch_node<T,details::neg_op<T> > ubn_t;
  15093. ubn_t* n = dynamic_cast<ubn_t*>(node);
  15094. if (n)
  15095. {
  15096. expression_node_ptr un_r = n->branch(0);
  15097. n->release();
  15098. free_node(node_allocator_,node);
  15099. node = un_r;
  15100. return true;
  15101. }
  15102. }
  15103. {
  15104. typedef details::unary_variable_node<T,details::neg_op<T> > ubn_t;
  15105. ubn_t* n = dynamic_cast<ubn_t*>(node);
  15106. if (n)
  15107. {
  15108. const T& v = n->v();
  15109. expression_node_ptr return_node = error_node();
  15110. if (
  15111. (return_node = symtab_store_.get_variable(v)) ||
  15112. (return_node = sem_ .get_variable(v))
  15113. )
  15114. {
  15115. free_node(node_allocator_,node);
  15116. node = return_node;
  15117. return true;
  15118. }
  15119. else
  15120. {
  15121. set_error(
  15122. make_error(parser_error::e_syntax,
  15123. current_token(),
  15124. "ERR010 - Failed to find variable node in symbol table"));
  15125. free_node(node_allocator_,node);
  15126. return false;
  15127. }
  15128. }
  15129. }
  15130. return false;
  15131. }
  15132. static inline expression_node_ptr error_node()
  15133. {
  15134. return reinterpret_cast<expression_node_ptr>(0);
  15135. }
  15136. template <typename Type, std::size_t N>
  15137. struct scoped_delete
  15138. {
  15139. typedef Type* ptr_t;
  15140. scoped_delete(parser<T>& pr, ptr_t& p)
  15141. : delete_ptr(true),
  15142. parser_(pr),
  15143. p_(&p)
  15144. {}
  15145. scoped_delete(parser<T>& pr, ptr_t (&p)[N])
  15146. : delete_ptr(true),
  15147. parser_(pr),
  15148. p_(&p[0])
  15149. {}
  15150. ~scoped_delete()
  15151. {
  15152. if (delete_ptr)
  15153. {
  15154. for (std::size_t i = 0; i < N; ++i)
  15155. {
  15156. free_node(parser_.node_allocator_,p_[i]);
  15157. }
  15158. }
  15159. }
  15160. bool delete_ptr;
  15161. parser<T>& parser_;
  15162. ptr_t* p_;
  15163. private:
  15164. scoped_delete<Type,N>& operator=(const scoped_delete<Type,N>&);
  15165. };
  15166. template <typename Type>
  15167. struct scoped_deq_delete
  15168. {
  15169. typedef Type* ptr_t;
  15170. scoped_deq_delete(parser<T>& pr, std::deque<ptr_t>& deq)
  15171. : delete_ptr(true),
  15172. parser_(pr),
  15173. deq_(deq)
  15174. {}
  15175. ~scoped_deq_delete()
  15176. {
  15177. if (delete_ptr && !deq_.empty())
  15178. {
  15179. for (std::size_t i = 0; i < deq_.size(); ++i)
  15180. {
  15181. free_node(parser_.node_allocator_,deq_[i]);
  15182. }
  15183. deq_.clear();
  15184. }
  15185. }
  15186. bool delete_ptr;
  15187. parser<T>& parser_;
  15188. std::deque<ptr_t>& deq_;
  15189. private:
  15190. scoped_deq_delete<Type>& operator=(const scoped_deq_delete<Type>&);
  15191. };
  15192. template <typename Type>
  15193. struct scoped_vec_delete
  15194. {
  15195. typedef Type* ptr_t;
  15196. scoped_vec_delete(parser<T>& pr, std::vector<ptr_t>& vec)
  15197. : delete_ptr(true),
  15198. parser_(pr),
  15199. vec_(vec)
  15200. {}
  15201. ~scoped_vec_delete()
  15202. {
  15203. if (delete_ptr && !vec_.empty())
  15204. {
  15205. for (std::size_t i = 0; i < vec_.size(); ++i)
  15206. {
  15207. free_node(parser_.node_allocator_,vec_[i]);
  15208. }
  15209. vec_.clear();
  15210. }
  15211. }
  15212. bool delete_ptr;
  15213. parser<T>& parser_;
  15214. std::vector<ptr_t>& vec_;
  15215. private:
  15216. scoped_vec_delete<Type>& operator=(const scoped_vec_delete<Type>&);
  15217. };
  15218. struct scoped_bool_negator
  15219. {
  15220. scoped_bool_negator(bool& bb)
  15221. : b(bb)
  15222. { b = !b; }
  15223. ~scoped_bool_negator()
  15224. { b = !b; }
  15225. bool& b;
  15226. };
  15227. struct scoped_bool_or_restorer
  15228. {
  15229. scoped_bool_or_restorer(bool& bb)
  15230. : b(bb),
  15231. original_value_(bb)
  15232. {}
  15233. ~scoped_bool_or_restorer()
  15234. {
  15235. b = b || original_value_;
  15236. }
  15237. bool& b;
  15238. bool original_value_;
  15239. };
  15240. inline expression_node_ptr parse_function_invocation(ifunction<T>* function, const std::string& function_name)
  15241. {
  15242. expression_node_ptr func_node = reinterpret_cast<expression_node_ptr>(0);
  15243. switch (function->param_count)
  15244. {
  15245. case 0 : func_node = parse_function_call_0 (function,function_name); break;
  15246. case 1 : func_node = parse_function_call< 1>(function,function_name); break;
  15247. case 2 : func_node = parse_function_call< 2>(function,function_name); break;
  15248. case 3 : func_node = parse_function_call< 3>(function,function_name); break;
  15249. case 4 : func_node = parse_function_call< 4>(function,function_name); break;
  15250. case 5 : func_node = parse_function_call< 5>(function,function_name); break;
  15251. case 6 : func_node = parse_function_call< 6>(function,function_name); break;
  15252. case 7 : func_node = parse_function_call< 7>(function,function_name); break;
  15253. case 8 : func_node = parse_function_call< 8>(function,function_name); break;
  15254. case 9 : func_node = parse_function_call< 9>(function,function_name); break;
  15255. case 10 : func_node = parse_function_call<10>(function,function_name); break;
  15256. case 11 : func_node = parse_function_call<11>(function,function_name); break;
  15257. case 12 : func_node = parse_function_call<12>(function,function_name); break;
  15258. case 13 : func_node = parse_function_call<13>(function,function_name); break;
  15259. case 14 : func_node = parse_function_call<14>(function,function_name); break;
  15260. case 15 : func_node = parse_function_call<15>(function,function_name); break;
  15261. case 16 : func_node = parse_function_call<16>(function,function_name); break;
  15262. case 17 : func_node = parse_function_call<17>(function,function_name); break;
  15263. case 18 : func_node = parse_function_call<18>(function,function_name); break;
  15264. case 19 : func_node = parse_function_call<19>(function,function_name); break;
  15265. case 20 : func_node = parse_function_call<20>(function,function_name); break;
  15266. default : {
  15267. set_error(
  15268. make_error(parser_error::e_syntax,
  15269. current_token(),
  15270. "ERR011 - Invalid number of parameters for function: '" + function_name + "'"));
  15271. return error_node();
  15272. }
  15273. }
  15274. if (func_node)
  15275. return func_node;
  15276. else
  15277. {
  15278. set_error(
  15279. make_error(parser_error::e_syntax,
  15280. current_token(),
  15281. "ERR012 - Failed to generate call to function: '" + function_name + "'"));
  15282. return error_node();
  15283. }
  15284. }
  15285. template <std::size_t NumberofParameters>
  15286. inline expression_node_ptr parse_function_call(ifunction<T>* function, const std::string& function_name)
  15287. {
  15288. if (0 == NumberofParameters)
  15289. {
  15290. set_error(
  15291. make_error(parser_error::e_syntax,
  15292. current_token(),
  15293. "ERR013 - Expecting ifunction '" + function_name + "' to have non-zero parameter count"));
  15294. return error_node();
  15295. }
  15296. expression_node_ptr branch[NumberofParameters];
  15297. expression_node_ptr result = error_node();
  15298. std::fill_n(branch,NumberofParameters,reinterpret_cast<expression_node_ptr>(0));
  15299. scoped_delete<expression_node_t,NumberofParameters> sd(*this,branch);
  15300. next_token();
  15301. if (!token_is(token_t::e_lbracket))
  15302. {
  15303. set_error(
  15304. make_error(parser_error::e_syntax,
  15305. current_token(),
  15306. "ERR014 - Expecting argument list for function: '" + function_name + "'"));
  15307. return error_node();
  15308. }
  15309. for (int i = 0; i < static_cast<int>(NumberofParameters); ++i)
  15310. {
  15311. branch[i] = parse_expression();
  15312. if (0 == branch[i])
  15313. {
  15314. set_error(
  15315. make_error(parser_error::e_syntax,
  15316. current_token(),
  15317. "ERR015 - Failed to parse argument " + details::to_str(i) + " for function: '" + function_name + "'"));
  15318. return error_node();
  15319. }
  15320. else if (i < static_cast<int>(NumberofParameters - 1))
  15321. {
  15322. if (!token_is(token_t::e_comma))
  15323. {
  15324. set_error(
  15325. make_error(parser_error::e_syntax,
  15326. current_token(),
  15327. "ERR016 - Invalid number of arguments for function: '" + function_name + "'"));
  15328. return error_node();
  15329. }
  15330. }
  15331. }
  15332. if (!token_is(token_t::e_rbracket))
  15333. {
  15334. set_error(
  15335. make_error(parser_error::e_syntax,
  15336. current_token(),
  15337. "ERR017 - Invalid number of arguments for function: '" + function_name + "'"));
  15338. return error_node();
  15339. }
  15340. else
  15341. result = expression_generator_.function(function,branch);
  15342. sd.delete_ptr = false;
  15343. return result;
  15344. }
  15345. inline expression_node_ptr parse_function_call_0(ifunction<T>* function, const std::string& function_name)
  15346. {
  15347. expression_node_ptr result = expression_generator_.function(function);
  15348. state_.side_effect_present = function->has_side_effects();
  15349. next_token();
  15350. if (
  15351. token_is(token_t::e_lbracket) &&
  15352. !token_is(token_t::e_rbracket)
  15353. )
  15354. {
  15355. set_error(
  15356. make_error(parser_error::e_syntax,
  15357. current_token(),
  15358. "ERR018 - Expecting '()' to proceed call to function: '" + function_name + "'"));
  15359. free_node(node_allocator_,result);
  15360. return error_node();
  15361. }
  15362. else
  15363. return result;
  15364. }
  15365. template <std::size_t MaxNumberofParameters>
  15366. inline int parse_base_function_call(expression_node_ptr (&param_list)[MaxNumberofParameters])
  15367. {
  15368. std::fill_n(param_list,MaxNumberofParameters,reinterpret_cast<expression_node_ptr>(0));
  15369. scoped_delete<expression_node_t,MaxNumberofParameters> sd(*this,param_list);
  15370. next_token();
  15371. if (!token_is(token_t::e_lbracket))
  15372. {
  15373. set_error(
  15374. make_error(parser_error::e_syntax,
  15375. current_token(),
  15376. "ERR019 - Expected a '(' at start of function call, instead got: '" + current_token().value + "'"));
  15377. return 0;
  15378. }
  15379. int param_index = 0;
  15380. for (; param_index < static_cast<int>(MaxNumberofParameters); ++param_index)
  15381. {
  15382. param_list[param_index] = parse_expression();
  15383. if (0 == param_list[param_index])
  15384. {
  15385. return 0;
  15386. }
  15387. else if (token_is(token_t::e_rbracket))
  15388. break;
  15389. else if (token_is(token_t::e_comma))
  15390. continue;
  15391. else
  15392. {
  15393. set_error(
  15394. make_error(parser_error::e_syntax,
  15395. current_token(),
  15396. "ERR020 - Expected a ',' between function input parameters, instead got: '" + current_token().value + "'"));
  15397. return 0;
  15398. }
  15399. }
  15400. sd.delete_ptr = false;
  15401. return (param_index + 1);
  15402. }
  15403. inline expression_node_ptr parse_base_operation()
  15404. {
  15405. typedef std::pair<base_ops_map_t::iterator,base_ops_map_t::iterator> map_range_t;
  15406. const std::string operation_name = current_token().value;
  15407. map_range_t itr_range = base_ops_map_.equal_range(operation_name);
  15408. if (0 == std::distance(itr_range.first,itr_range.second))
  15409. {
  15410. set_error(
  15411. make_error(parser_error::e_syntax,
  15412. current_token(),
  15413. "ERR021 - No entry found for base operation: " + operation_name));
  15414. return error_node();
  15415. }
  15416. static const std::size_t MaxNumberofParameters = 4;
  15417. expression_node_ptr param_list[MaxNumberofParameters] = {0};
  15418. std::size_t parameter_count = parse_base_function_call(param_list);
  15419. if (0 == parameter_count)
  15420. {
  15421. return error_node();
  15422. }
  15423. else if (parameter_count <= MaxNumberofParameters)
  15424. {
  15425. for (base_ops_map_t::iterator itr = itr_range.first; itr != itr_range.second; ++itr)
  15426. {
  15427. details::base_operation_t& operation = itr->second;
  15428. if (operation.num_params == parameter_count)
  15429. {
  15430. switch (parameter_count)
  15431. {
  15432. #define base_opr_case(N) \
  15433. case N : { \
  15434. expression_node_ptr pl##N[N] = {0}; \
  15435. std::copy(param_list,param_list + N,pl##N); \
  15436. lodge_symbol(operation_name,e_st_function); \
  15437. return expression_generator_(operation.type,pl##N); \
  15438. } \
  15439. base_opr_case(1)
  15440. base_opr_case(2)
  15441. base_opr_case(3)
  15442. base_opr_case(4)
  15443. #undef base_opr_case
  15444. }
  15445. }
  15446. }
  15447. }
  15448. for (std::size_t i = 0; i < MaxNumberofParameters; ++i)
  15449. {
  15450. free_node(node_allocator_,param_list[i]);
  15451. }
  15452. set_error(
  15453. make_error(parser_error::e_syntax,
  15454. current_token(),
  15455. "ERR022 - Invalid number of parameters for call to function: '" + operation_name + "'"));
  15456. return error_node();
  15457. }
  15458. inline expression_node_ptr parse_conditional_statement_01(expression_node_ptr condition)
  15459. {
  15460. // Parse: [if][(][condition][,][consequent][,][alternative][)]
  15461. expression_node_ptr consequent = error_node();
  15462. expression_node_ptr alternative = error_node();
  15463. bool result = true;
  15464. if (!token_is(token_t::e_comma))
  15465. {
  15466. set_error(
  15467. make_error(parser_error::e_syntax,
  15468. current_token(),
  15469. "ERR023 - Expected ',' between if-statement condition and consequent"));
  15470. result = false;
  15471. }
  15472. else if (0 == (consequent = parse_expression()))
  15473. {
  15474. set_error(
  15475. make_error(parser_error::e_syntax,
  15476. current_token(),
  15477. "ERR024 - Failed to parse consequent for if-statement"));
  15478. result = false;
  15479. }
  15480. else if (!token_is(token_t::e_comma))
  15481. {
  15482. set_error(
  15483. make_error(parser_error::e_syntax,
  15484. current_token(),
  15485. "ERR025 - Expected ',' between if-statement consequent and alternative"));
  15486. result = false;
  15487. }
  15488. else if (0 == (alternative = parse_expression()))
  15489. {
  15490. set_error(
  15491. make_error(parser_error::e_syntax,
  15492. current_token(),
  15493. "ERR026 - Failed to parse alternative for if-statement"));
  15494. result = false;
  15495. }
  15496. else if (!token_is(token_t::e_rbracket))
  15497. {
  15498. set_error(
  15499. make_error(parser_error::e_syntax,
  15500. current_token(),
  15501. "ERR027 - Expected ')' at the end of if-statement"));
  15502. result = false;
  15503. }
  15504. #ifndef exprtk_disable_string_capabilities
  15505. if (result)
  15506. {
  15507. const bool consq_is_str = is_generally_string_node( consequent);
  15508. const bool alter_is_str = is_generally_string_node(alternative);
  15509. if (consq_is_str || alter_is_str)
  15510. {
  15511. if (consq_is_str && alter_is_str)
  15512. {
  15513. return expression_generator_
  15514. .conditional_string(condition,consequent,alternative);
  15515. }
  15516. set_error(
  15517. make_error(parser_error::e_syntax,
  15518. current_token(),
  15519. "ERR028 - Return types of ternary if-statement differ"));
  15520. result = false;
  15521. }
  15522. }
  15523. #endif
  15524. if (!result)
  15525. {
  15526. free_node(node_allocator_, condition);
  15527. free_node(node_allocator_, consequent);
  15528. free_node(node_allocator_,alternative);
  15529. return error_node();
  15530. }
  15531. else
  15532. return expression_generator_
  15533. .conditional(condition,consequent,alternative);
  15534. }
  15535. inline expression_node_ptr parse_conditional_statement_02(expression_node_ptr condition)
  15536. {
  15537. expression_node_ptr consequent = error_node();
  15538. expression_node_ptr alternative = error_node();
  15539. bool result = true;
  15540. if (token_is(token_t::e_lcrlbracket,false))
  15541. {
  15542. if (0 == (consequent = parse_multi_sequence("if-statement-01")))
  15543. {
  15544. set_error(
  15545. make_error(parser_error::e_syntax,
  15546. current_token(),
  15547. "ERR029 - Failed to parse body of consequent for if-statement"));
  15548. result = false;
  15549. }
  15550. }
  15551. else
  15552. {
  15553. if (
  15554. settings_.commutative_check_enabled() &&
  15555. token_is(token_t::e_mul,false)
  15556. )
  15557. {
  15558. next_token();
  15559. }
  15560. if (0 != (consequent = parse_expression()))
  15561. {
  15562. if (!token_is(token_t::e_eof))
  15563. {
  15564. set_error(
  15565. make_error(parser_error::e_syntax,
  15566. current_token(),
  15567. "ERR030 - Expected ';' at the end of the consequent for if-statement"));
  15568. result = false;
  15569. }
  15570. }
  15571. else
  15572. {
  15573. set_error(
  15574. make_error(parser_error::e_syntax,
  15575. current_token(),
  15576. "ERR031 - Failed to parse body of consequent for if-statement"));
  15577. result = false;
  15578. }
  15579. }
  15580. if (result)
  15581. {
  15582. if (details::imatch(current_token().value,"else"))
  15583. {
  15584. next_token();
  15585. if (token_is(token_t::e_lcrlbracket,false))
  15586. {
  15587. if (0 == (alternative = parse_multi_sequence("else-statement-01")))
  15588. {
  15589. set_error(
  15590. make_error(parser_error::e_syntax,
  15591. current_token(),
  15592. "ERR032 - Failed to parse body of the 'else' for if-statement"));
  15593. result = false;
  15594. }
  15595. }
  15596. else if (details::imatch(current_token().value,"if"))
  15597. {
  15598. if (0 == (alternative = parse_conditional_statement()))
  15599. {
  15600. set_error(
  15601. make_error(parser_error::e_syntax,
  15602. current_token(),
  15603. "ERR033 - Failed to parse body of if-else statement"));
  15604. result = false;
  15605. }
  15606. }
  15607. else if (0 != (alternative = parse_expression()))
  15608. {
  15609. if (!token_is(token_t::e_eof))
  15610. {
  15611. set_error(
  15612. make_error(parser_error::e_syntax,
  15613. current_token(),
  15614. "ERR034 - Expected ';' at the end of the 'else-if' for the if-statement"));
  15615. result = false;
  15616. }
  15617. }
  15618. else
  15619. {
  15620. set_error(
  15621. make_error(parser_error::e_syntax,
  15622. current_token(),
  15623. "ERR035 - Failed to parse body of the 'else' for if-statement"));
  15624. result = false;
  15625. }
  15626. }
  15627. }
  15628. #ifndef exprtk_disable_string_capabilities
  15629. if (result)
  15630. {
  15631. const bool consq_is_str = is_generally_string_node( consequent);
  15632. const bool alter_is_str = is_generally_string_node(alternative);
  15633. if (consq_is_str || alter_is_str)
  15634. {
  15635. if (consq_is_str && alter_is_str)
  15636. {
  15637. return expression_generator_
  15638. .conditional_string(condition,consequent,alternative);
  15639. }
  15640. set_error(
  15641. make_error(parser_error::e_syntax,
  15642. current_token(),
  15643. "ERR036 - Return types of ternary if-statement differ"));
  15644. result = false;
  15645. }
  15646. }
  15647. #endif
  15648. if (!result)
  15649. {
  15650. free_node(node_allocator_, condition);
  15651. free_node(node_allocator_, consequent);
  15652. free_node(node_allocator_,alternative);
  15653. return error_node();
  15654. }
  15655. else
  15656. return expression_generator_
  15657. .conditional(condition,consequent,alternative);
  15658. }
  15659. inline expression_node_ptr parse_conditional_statement()
  15660. {
  15661. expression_node_ptr condition = error_node();
  15662. next_token();
  15663. if (!token_is(token_t::e_lbracket))
  15664. {
  15665. set_error(
  15666. make_error(parser_error::e_syntax,
  15667. current_token(),
  15668. "ERR037 - Expected '(' at start of if-statement, instead got: '" + current_token().value + "'"));
  15669. return error_node();
  15670. }
  15671. else if (0 == (condition = parse_expression()))
  15672. {
  15673. set_error(
  15674. make_error(parser_error::e_syntax,
  15675. current_token(),
  15676. "ERR038 - Failed to parse condition for if-statement"));
  15677. return error_node();
  15678. }
  15679. else if (token_is(token_t::e_comma,false))
  15680. {
  15681. // if (x,y,z)
  15682. return parse_conditional_statement_01(condition);
  15683. }
  15684. else if (token_is(token_t::e_rbracket))
  15685. {
  15686. // 00. if (x) y;
  15687. // 01. if (x) y; else z;
  15688. // 02. if (x) y; else {z0; ... zn;}
  15689. // 03. if (x) y; else if (z) w;
  15690. // 04. if (x) y; else if (z) w; else u;
  15691. // 05. if (x) y; else if (z) w; else {u0; ... un;}
  15692. // 06. if (x) y; else if (z) {w0; ... wn;}
  15693. // 07. if (x) {y0; ... yn;}
  15694. // 08. if (x) {y0; ... yn;} else z;
  15695. // 09. if (x) {y0; ... yn;} else {z0; ... zn;};
  15696. // 10. if (x) {y0; ... yn;} else if (z) w;
  15697. // 11. if (x) {y0; ... yn;} else if (z) w; else u;
  15698. // 12. if (x) {y0; ... nex;} else if (z) w; else {u0 ... un;}
  15699. // 13. if (x) {y0; ... yn;} else if (z) {w0; ... wn;}
  15700. return parse_conditional_statement_02(condition);
  15701. }
  15702. set_error(
  15703. make_error(parser_error::e_syntax,
  15704. current_token(),
  15705. "ERR039 - Invalid if-statement"));
  15706. free_node(node_allocator_,condition);
  15707. return error_node();
  15708. }
  15709. inline expression_node_ptr parse_ternary_conditional_statement(expression_node_ptr condition)
  15710. {
  15711. // Parse: [condition][?][consequent][:][alternative]
  15712. expression_node_ptr consequent = error_node();
  15713. expression_node_ptr alternative = error_node();
  15714. bool result = true;
  15715. if (0 == condition)
  15716. {
  15717. set_error(
  15718. make_error(parser_error::e_syntax,
  15719. current_token(),
  15720. "ERR040 - Encountered invalid condition branch for ternary if-statement"));
  15721. return error_node();
  15722. }
  15723. else if (!token_is(token_t::e_ternary))
  15724. {
  15725. set_error(
  15726. make_error(parser_error::e_syntax,
  15727. current_token(),
  15728. "ERR041 - Expected '?' after condition of ternary if-statement"));
  15729. result = false;
  15730. }
  15731. else if (0 == (consequent = parse_expression()))
  15732. {
  15733. set_error(
  15734. make_error(parser_error::e_syntax,
  15735. current_token(),
  15736. "ERR042 - Failed to parse consequent for ternary if-statement"));
  15737. result = false;
  15738. }
  15739. else if (!token_is(token_t::e_colon))
  15740. {
  15741. set_error(
  15742. make_error(parser_error::e_syntax,
  15743. current_token(),
  15744. "ERR043 - Expected ':' between ternary if-statement consequent and alternative"));
  15745. result = false;
  15746. }
  15747. else if (0 == (alternative = parse_expression()))
  15748. {
  15749. set_error(
  15750. make_error(parser_error::e_syntax,
  15751. current_token(),
  15752. "ERR044 - Failed to parse alternative for ternary if-statement"));
  15753. result = false;
  15754. }
  15755. #ifndef exprtk_disable_string_capabilities
  15756. if (result)
  15757. {
  15758. const bool consq_is_str = is_generally_string_node( consequent);
  15759. const bool alter_is_str = is_generally_string_node(alternative);
  15760. if (consq_is_str || alter_is_str)
  15761. {
  15762. if (consq_is_str && alter_is_str)
  15763. {
  15764. return expression_generator_
  15765. .conditional_string(condition,consequent,alternative);
  15766. }
  15767. set_error(
  15768. make_error(parser_error::e_syntax,
  15769. current_token(),
  15770. "ERR045 - Return types of ternary if-statement differ"));
  15771. result = false;
  15772. }
  15773. }
  15774. #endif
  15775. if (!result)
  15776. {
  15777. free_node(node_allocator_, condition);
  15778. free_node(node_allocator_, consequent);
  15779. free_node(node_allocator_,alternative);
  15780. return error_node();
  15781. }
  15782. else
  15783. return expression_generator_
  15784. .conditional(condition,consequent,alternative);
  15785. }
  15786. inline expression_node_ptr parse_while_loop()
  15787. {
  15788. // Parse: [while][(][test expr][)][{][expression][}]
  15789. expression_node_ptr condition = error_node();
  15790. expression_node_ptr branch = error_node();
  15791. expression_node_ptr result_node = error_node();
  15792. bool result = true;
  15793. next_token();
  15794. if (!token_is(token_t::e_lbracket))
  15795. {
  15796. set_error(
  15797. make_error(parser_error::e_syntax,
  15798. current_token(),
  15799. "ERR046 - Expected '(' at start of while-loop condition statement"));
  15800. return error_node();
  15801. }
  15802. else if (0 == (condition = parse_expression()))
  15803. {
  15804. set_error(
  15805. make_error(parser_error::e_syntax,
  15806. current_token(),
  15807. "ERR047 - Failed to parse condition for while-loop"));
  15808. return error_node();
  15809. }
  15810. else if (!token_is(token_t::e_rbracket))
  15811. {
  15812. set_error(
  15813. make_error(parser_error::e_syntax,
  15814. current_token(),
  15815. "ERR048 - Expected ')' at end of while-loop condition statement"));
  15816. result = false;
  15817. }
  15818. brkcnt_list_.push_front(false);
  15819. if (result)
  15820. {
  15821. if (0 == (branch = parse_multi_sequence("while-loop")))
  15822. {
  15823. set_error(
  15824. make_error(parser_error::e_syntax,
  15825. current_token(),
  15826. "ERR049 - Failed to parse body of while-loop"));
  15827. result = false;
  15828. }
  15829. else if (0 == (result_node = expression_generator_.while_loop(condition,
  15830. branch,
  15831. brkcnt_list_.front())))
  15832. {
  15833. set_error(
  15834. make_error(parser_error::e_syntax,
  15835. current_token(),
  15836. "ERR050 - Failed to synthesize while-loop"));
  15837. result = false;
  15838. }
  15839. }
  15840. if (!result)
  15841. {
  15842. free_node(node_allocator_, branch);
  15843. free_node(node_allocator_, condition);
  15844. free_node(node_allocator_,result_node);
  15845. brkcnt_list_.pop_front();
  15846. return error_node();
  15847. }
  15848. else
  15849. return result_node;
  15850. }
  15851. inline expression_node_ptr parse_repeat_until_loop()
  15852. {
  15853. // Parse: [repeat][{][expression][}][until][(][test expr][)]
  15854. expression_node_ptr condition = error_node();
  15855. expression_node_ptr branch = error_node();
  15856. next_token();
  15857. std::vector<expression_node_ptr> arg_list;
  15858. std::vector<bool> side_effect_list;
  15859. scoped_vec_delete<expression_node_t> sdd(*this,arg_list);
  15860. brkcnt_list_.push_front(false);
  15861. if (details::imatch(current_token().value,"until"))
  15862. {
  15863. next_token();
  15864. branch = node_allocator_.allocate<details::null_node<T> >();
  15865. }
  15866. else
  15867. {
  15868. token_t::token_type seperator = token_t::e_eof;
  15869. scope_handler sh(*this);
  15870. scoped_bool_or_restorer sbr(state_.side_effect_present);
  15871. for ( ; ; )
  15872. {
  15873. state_.side_effect_present = false;
  15874. expression_node_ptr arg = parse_expression();
  15875. if (0 == arg)
  15876. return error_node();
  15877. else
  15878. {
  15879. arg_list.push_back(arg);
  15880. side_effect_list.push_back(state_.side_effect_present);
  15881. }
  15882. if (details::imatch(current_token().value,"until"))
  15883. {
  15884. next_token();
  15885. break;
  15886. }
  15887. bool is_next_until = peek_token_is(token_t::e_symbol) &&
  15888. peek_token_is("until");
  15889. if (!token_is(seperator) && is_next_until)
  15890. {
  15891. set_error(
  15892. make_error(parser_error::e_syntax,
  15893. current_token(),
  15894. "ERR051 - Expected '" + token_t::to_str(seperator) + "' in body of repeat until loop"));
  15895. return error_node();
  15896. }
  15897. if (details::imatch(current_token().value,"until"))
  15898. {
  15899. next_token();
  15900. break;
  15901. }
  15902. }
  15903. branch = simplify(arg_list,side_effect_list);
  15904. if ((sdd.delete_ptr = (0 == branch)))
  15905. {
  15906. brkcnt_list_.pop_front();
  15907. set_error(
  15908. make_error(parser_error::e_syntax,
  15909. current_token(),
  15910. "ERR052 - Failed to parse body of repeat until loop"));
  15911. return error_node();
  15912. }
  15913. }
  15914. if (!token_is(token_t::e_lbracket))
  15915. {
  15916. brkcnt_list_.pop_front();
  15917. set_error(
  15918. make_error(parser_error::e_syntax,
  15919. current_token(),
  15920. "ERR053 - Expected '(' before condition statement of repeat until loop"));
  15921. free_node(node_allocator_,branch);
  15922. return error_node();
  15923. }
  15924. else if (0 == (condition = parse_expression()))
  15925. {
  15926. brkcnt_list_.pop_front();
  15927. set_error(
  15928. make_error(parser_error::e_syntax,
  15929. current_token(),
  15930. "ERR054 - Failed to parse condition for repeat until loop"));
  15931. free_node(node_allocator_,branch);
  15932. return error_node();
  15933. }
  15934. else if (!token_is(token_t::e_rbracket))
  15935. {
  15936. set_error(
  15937. make_error(parser_error::e_syntax,
  15938. current_token(),
  15939. "ERR055 - Expected ')' after condition of repeat until loop"));
  15940. free_node(node_allocator_, branch);
  15941. free_node(node_allocator_, condition);
  15942. brkcnt_list_.pop_front();
  15943. return error_node();
  15944. }
  15945. expression_node_ptr result;
  15946. result = expression_generator_
  15947. .repeat_until_loop(condition,branch,brkcnt_list_.front());
  15948. if (0 == result)
  15949. {
  15950. set_error(
  15951. make_error(parser_error::e_syntax,
  15952. current_token(),
  15953. "ERR056 - Failed to synthesize repeat until loop"));
  15954. free_node(node_allocator_, condition);
  15955. brkcnt_list_.pop_front();
  15956. return error_node();
  15957. }
  15958. else
  15959. {
  15960. brkcnt_list_.pop_front();
  15961. return result;
  15962. }
  15963. }
  15964. inline expression_node_ptr parse_for_loop()
  15965. {
  15966. expression_node_ptr initialiser = error_node();
  15967. expression_node_ptr condition = error_node();
  15968. expression_node_ptr incrementor = error_node();
  15969. expression_node_ptr loop_body = error_node();
  15970. scope_element* se = 0;
  15971. bool result = true;
  15972. std::string loop_counter_symbol;
  15973. next_token();
  15974. scope_handler sh(*this);
  15975. if (!token_is(token_t::e_lbracket))
  15976. {
  15977. set_error(
  15978. make_error(parser_error::e_syntax,
  15979. current_token(),
  15980. "ERR057 - Expected '(' at start of for-loop"));
  15981. return error_node();
  15982. }
  15983. if (!token_is(token_t::e_eof))
  15984. {
  15985. if (
  15986. !token_is(token_t::e_symbol,false) &&
  15987. details::imatch(current_token().value,"var")
  15988. )
  15989. {
  15990. next_token();
  15991. if (!token_is(token_t::e_symbol,false))
  15992. {
  15993. set_error(
  15994. make_error(parser_error::e_syntax,
  15995. current_token(),
  15996. "ERR058 - Expected a variable at the start of initialiser section of for-loop"));
  15997. return error_node();
  15998. }
  15999. else if (!peek_token_is(token_t::e_assign))
  16000. {
  16001. set_error(
  16002. make_error(parser_error::e_syntax,
  16003. current_token(),
  16004. "ERR059 - Expected variable assignment of initialiser section of for-loop"));
  16005. return error_node();
  16006. }
  16007. loop_counter_symbol = current_token().value;
  16008. se = &sem_.get_element(loop_counter_symbol);
  16009. if ((se->name == loop_counter_symbol) && se->active)
  16010. {
  16011. set_error(
  16012. make_error(parser_error::e_syntax,
  16013. current_token(),
  16014. "ERR060 - For-loop variable '" + loop_counter_symbol+ "' is being shadowed by a previous declaration"));
  16015. return error_node();
  16016. }
  16017. else if (!symtab_store_.is_variable(loop_counter_symbol))
  16018. {
  16019. if (
  16020. !se->active &&
  16021. (se->name == loop_counter_symbol) &&
  16022. (se->type == scope_element::e_variable)
  16023. )
  16024. {
  16025. se->active = true;
  16026. se->ref_count++;
  16027. }
  16028. else
  16029. {
  16030. scope_element nse;
  16031. nse.name = loop_counter_symbol;
  16032. nse.active = true;
  16033. nse.ref_count = 1;
  16034. nse.type = scope_element::e_variable;
  16035. nse.depth = state_.scope_depth;
  16036. nse.data = new T(T(0));
  16037. nse.var_node = new variable_node_t(*(T*)(nse.data));
  16038. if (!sem_.add_element(nse))
  16039. {
  16040. set_error(
  16041. make_error(parser_error::e_syntax,
  16042. current_token(),
  16043. "ERR061 - Failed to add new local variable '" + loop_counter_symbol + "' to SEM"));
  16044. sem_.free_element(nse);
  16045. result = false;
  16046. }
  16047. else
  16048. {
  16049. exprtk_debug(("parse_for_loop() - INFO - Added new local variable: %s\n",nse.name.c_str()));
  16050. state_.activate_side_effect("parse_for_loop()");
  16051. }
  16052. }
  16053. }
  16054. }
  16055. if (0 == (initialiser = parse_expression()))
  16056. {
  16057. set_error(
  16058. make_error(parser_error::e_syntax,
  16059. current_token(),
  16060. "ERR062 - Failed to parse initialiser of for-loop"));
  16061. result = false;
  16062. }
  16063. else if (!token_is(token_t::e_eof))
  16064. {
  16065. set_error(
  16066. make_error(parser_error::e_syntax,
  16067. current_token(),
  16068. "ERR063 - Expected ';' after initialiser of for-loop"));
  16069. result = false;
  16070. }
  16071. }
  16072. if (!token_is(token_t::e_eof))
  16073. {
  16074. if (0 == (condition = parse_expression()))
  16075. {
  16076. set_error(
  16077. make_error(parser_error::e_syntax,
  16078. current_token(),
  16079. "ERR064 - Failed to parse condition of for-loop"));
  16080. result = false;
  16081. }
  16082. else if (!token_is(token_t::e_eof))
  16083. {
  16084. set_error(
  16085. make_error(parser_error::e_syntax,
  16086. current_token(),
  16087. "ERR065 - Expected ';' after condition section of for-loop"));
  16088. result = false;
  16089. }
  16090. }
  16091. if (!token_is(token_t::e_rbracket))
  16092. {
  16093. if (0 == (incrementor = parse_expression()))
  16094. {
  16095. set_error(
  16096. make_error(parser_error::e_syntax,
  16097. current_token(),
  16098. "ERR066 - Failed to parse incrementor of for-loop"));
  16099. result = false;
  16100. }
  16101. else if (!token_is(token_t::e_rbracket))
  16102. {
  16103. set_error(
  16104. make_error(parser_error::e_syntax,
  16105. current_token(),
  16106. "ERR067 - Expected ')' after incrementor section of for-loop"));
  16107. result = false;
  16108. }
  16109. }
  16110. if (result)
  16111. {
  16112. brkcnt_list_.push_front(false);
  16113. if (0 == (loop_body = parse_multi_sequence("for-loop")))
  16114. {
  16115. set_error(
  16116. make_error(parser_error::e_syntax,
  16117. current_token(),
  16118. "ERR068 - Failed to parse body of for-loop"));
  16119. result = false;
  16120. }
  16121. }
  16122. if (!result)
  16123. {
  16124. if (se)
  16125. {
  16126. se->ref_count--;
  16127. }
  16128. sem_.cleanup();
  16129. free_node(node_allocator_,initialiser);
  16130. free_node(node_allocator_, condition);
  16131. free_node(node_allocator_,incrementor);
  16132. free_node(node_allocator_, loop_body);
  16133. if (!brkcnt_list_.empty())
  16134. {
  16135. brkcnt_list_.pop_front();
  16136. }
  16137. return error_node();
  16138. }
  16139. else
  16140. {
  16141. expression_node_ptr result_node =
  16142. expression_generator_.for_loop(initialiser,
  16143. condition,
  16144. incrementor,
  16145. loop_body,
  16146. brkcnt_list_.front());
  16147. brkcnt_list_.pop_front();
  16148. return result_node;
  16149. }
  16150. }
  16151. inline expression_node_ptr parse_switch_statement()
  16152. {
  16153. std::vector<expression_node_ptr> arg_list;
  16154. expression_node_ptr result = error_node();
  16155. if (!details::imatch(current_token().value,"switch"))
  16156. {
  16157. set_error(
  16158. make_error(parser_error::e_syntax,
  16159. current_token(),
  16160. "ERR069 - Expected keyword 'switch'"));
  16161. return error_node();
  16162. }
  16163. scoped_vec_delete<expression_node_t> svd(*this,arg_list);
  16164. next_token();
  16165. if (!token_is(token_t::e_lcrlbracket))
  16166. {
  16167. set_error(
  16168. make_error(parser_error::e_syntax,
  16169. current_token(),
  16170. "ERR070 - Expected '{' for call to switch statement"));
  16171. return error_node();
  16172. }
  16173. for ( ; ; )
  16174. {
  16175. if (!details::imatch("case",current_token().value))
  16176. {
  16177. set_error(
  16178. make_error(parser_error::e_syntax,
  16179. current_token(),
  16180. "ERR071 - Expected either a 'case' or 'default' statement"));
  16181. return error_node();
  16182. }
  16183. next_token();
  16184. expression_node_ptr condition = parse_expression();
  16185. if (0 == condition)
  16186. return error_node();
  16187. else if (!token_is(token_t::e_colon))
  16188. {
  16189. set_error(
  16190. make_error(parser_error::e_syntax,
  16191. current_token(),
  16192. "ERR072 - Expected ':' for case of switch statement"));
  16193. return error_node();
  16194. }
  16195. expression_node_ptr consequent = parse_expression();
  16196. if (0 == consequent)
  16197. return error_node();
  16198. else if (!token_is(token_t::e_eof))
  16199. {
  16200. set_error(
  16201. make_error(parser_error::e_syntax,
  16202. current_token(),
  16203. "ERR073 - Expected ';' at end of case for switch statement"));
  16204. return error_node();
  16205. }
  16206. // Can we optimize away the case statement?
  16207. if (is_constant_node(condition) && is_false(condition))
  16208. {
  16209. free_node(node_allocator_, condition);
  16210. free_node(node_allocator_,consequent);
  16211. condition = 0;
  16212. consequent = 0;
  16213. }
  16214. else
  16215. {
  16216. arg_list.push_back( condition);
  16217. arg_list.push_back(consequent);
  16218. }
  16219. if (details::imatch("default",current_token().value))
  16220. {
  16221. next_token();
  16222. if (!token_is(token_t::e_colon))
  16223. {
  16224. set_error(
  16225. make_error(parser_error::e_syntax,
  16226. current_token(),
  16227. "ERR074 - Expected ':' for default of switch statement"));
  16228. return error_node();
  16229. }
  16230. expression_node_ptr default_statement = error_node();
  16231. if (token_is(token_t::e_lcrlbracket,false))
  16232. default_statement = parse_multi_sequence("switch-default");
  16233. else
  16234. default_statement = parse_expression();
  16235. if (0 == default_statement)
  16236. return error_node();
  16237. else if (!token_is(token_t::e_eof))
  16238. {
  16239. free_node(node_allocator_,default_statement);
  16240. set_error(
  16241. make_error(parser_error::e_syntax,
  16242. current_token(),
  16243. "ERR075 - Expected ';' at end of default for switch statement"));
  16244. return error_node();
  16245. }
  16246. arg_list.push_back(default_statement);
  16247. break;
  16248. }
  16249. }
  16250. if (!token_is(token_t::e_rcrlbracket))
  16251. {
  16252. set_error(
  16253. make_error(parser_error::e_syntax,
  16254. current_token(),
  16255. "ERR076 - Expected '}' at end of switch statement"));
  16256. return error_node();
  16257. }
  16258. result = expression_generator_.switch_statement(arg_list);
  16259. svd.delete_ptr = (0 == result);
  16260. return result;
  16261. }
  16262. inline expression_node_ptr parse_multi_switch_statement()
  16263. {
  16264. std::vector<expression_node_ptr> arg_list;
  16265. expression_node_ptr result = error_node();
  16266. if (!details::imatch(current_token().value,"[*]"))
  16267. {
  16268. set_error(
  16269. make_error(parser_error::e_syntax,
  16270. current_token(),
  16271. "ERR077 - Expected token '[*]'"));
  16272. return error_node();
  16273. }
  16274. scoped_vec_delete<expression_node_t> svd(*this,arg_list);
  16275. next_token();
  16276. if (!token_is(token_t::e_lcrlbracket))
  16277. {
  16278. set_error(
  16279. make_error(parser_error::e_syntax,
  16280. current_token(),
  16281. "ERR078 - Expected '{' for call to [*] statement"));
  16282. return error_node();
  16283. }
  16284. for ( ; ; )
  16285. {
  16286. if (!details::imatch("case",current_token().value))
  16287. {
  16288. set_error(
  16289. make_error(parser_error::e_syntax,
  16290. current_token(),
  16291. "ERR079 - Expected a 'case' statement for multi-switch"));
  16292. return error_node();
  16293. }
  16294. next_token();
  16295. expression_node_ptr condition = parse_expression();
  16296. if (0 == condition)
  16297. return error_node();
  16298. if (!token_is(token_t::e_colon))
  16299. {
  16300. set_error(
  16301. make_error(parser_error::e_syntax,
  16302. current_token(),
  16303. "ERR080 - Expected ':' for case of [*] statement"));
  16304. return error_node();
  16305. }
  16306. expression_node_ptr consequent = parse_expression();
  16307. if (0 == consequent)
  16308. return error_node();
  16309. if (!token_is(token_t::e_eof))
  16310. {
  16311. set_error(
  16312. make_error(parser_error::e_syntax,
  16313. current_token(),
  16314. "ERR081 - Expected ';' at end of case for [*] statement"));
  16315. return error_node();
  16316. }
  16317. // Can we optimize away the case statement?
  16318. if (is_constant_node(condition) && is_false(condition))
  16319. {
  16320. free_node(node_allocator_, condition);
  16321. free_node(node_allocator_,consequent);
  16322. condition = 0;
  16323. consequent = 0;
  16324. }
  16325. else
  16326. {
  16327. arg_list.push_back(condition);
  16328. arg_list.push_back(consequent);
  16329. }
  16330. if (token_is(token_t::e_rcrlbracket,false))
  16331. {
  16332. break;
  16333. }
  16334. }
  16335. if (!token_is(token_t::e_rcrlbracket))
  16336. {
  16337. set_error(
  16338. make_error(parser_error::e_syntax,
  16339. current_token(),
  16340. "ERR082 - Expected '}' at end of [*] statement"));
  16341. return error_node();
  16342. }
  16343. result = expression_generator_.multi_switch_statement(arg_list);
  16344. svd.delete_ptr = (0 == result);
  16345. return result;
  16346. }
  16347. inline expression_node_ptr parse_vararg_function()
  16348. {
  16349. std::vector<expression_node_ptr> arg_list;
  16350. expression_node_ptr result = error_node();
  16351. details::operator_type opt_type = details::e_default;
  16352. const std::string symbol = current_token().value;
  16353. if (details::imatch(symbol,"~"))
  16354. {
  16355. next_token();
  16356. return parse_multi_sequence();
  16357. }
  16358. else if (details::imatch(symbol,"[*]"))
  16359. {
  16360. return parse_multi_switch_statement();
  16361. }
  16362. else if (details::imatch(symbol,"avg" )) opt_type = details::e_avg;
  16363. else if (details::imatch(symbol,"mand")) opt_type = details::e_mand;
  16364. else if (details::imatch(symbol,"max" )) opt_type = details::e_max;
  16365. else if (details::imatch(symbol,"min" )) opt_type = details::e_min;
  16366. else if (details::imatch(symbol,"mor" )) opt_type = details::e_mor;
  16367. else if (details::imatch(symbol,"mul" )) opt_type = details::e_prod;
  16368. else if (details::imatch(symbol,"sum" )) opt_type = details::e_sum;
  16369. else
  16370. {
  16371. set_error(
  16372. make_error(parser_error::e_syntax,
  16373. current_token(),
  16374. "ERR083 - Unsupported vararg function: " + symbol));
  16375. return error_node();
  16376. }
  16377. scoped_vec_delete<expression_node_t> sdd(*this,arg_list);
  16378. lodge_symbol(symbol,e_st_function);
  16379. next_token();
  16380. if (!token_is(token_t::e_lbracket))
  16381. {
  16382. set_error(
  16383. make_error(parser_error::e_syntax,
  16384. current_token(),
  16385. "ERR084 - Expected '(' for call to vararg function: " + symbol));
  16386. return error_node();
  16387. }
  16388. for ( ; ; )
  16389. {
  16390. expression_node_ptr arg = parse_expression();
  16391. if (0 == arg)
  16392. return error_node();
  16393. else
  16394. arg_list.push_back(arg);
  16395. if (token_is(token_t::e_rbracket))
  16396. break;
  16397. else if (!token_is(token_t::e_comma))
  16398. {
  16399. set_error(
  16400. make_error(parser_error::e_syntax,
  16401. current_token(),
  16402. "ERR085 - Expected ',' for call to vararg function: " + symbol));
  16403. return error_node();
  16404. }
  16405. }
  16406. result = expression_generator_.vararg_function(opt_type,arg_list);
  16407. sdd.delete_ptr = (0 == result);
  16408. return result;
  16409. }
  16410. #ifndef exprtk_disable_string_capabilities
  16411. inline expression_node_ptr parse_string_range_statement(expression_node_ptr& expression)
  16412. {
  16413. if (!token_is(token_t::e_lsqrbracket))
  16414. {
  16415. set_error(
  16416. make_error(parser_error::e_syntax,
  16417. current_token(),
  16418. "ERR086 - Expected '[' as start of string range definition"));
  16419. free_node(node_allocator_,expression);
  16420. return error_node();
  16421. }
  16422. else if (token_is(token_t::e_rsqrbracket))
  16423. {
  16424. return node_allocator_.allocate<details::string_size_node<T> >(expression);
  16425. }
  16426. range_t rp;
  16427. if (!parse_range(rp,true))
  16428. {
  16429. free_node(node_allocator_,expression);
  16430. return error_node();
  16431. }
  16432. expression_node_ptr result = expression_generator_(expression,rp);
  16433. if (0 == result)
  16434. {
  16435. set_error(
  16436. make_error(parser_error::e_syntax,
  16437. current_token(),
  16438. "ERR087 - Failed to generate string range node"));
  16439. free_node(node_allocator_,expression);
  16440. }
  16441. rp.clear();
  16442. return result;
  16443. }
  16444. #else
  16445. inline expression_node_ptr parse_string_range_statement(expression_node_ptr&)
  16446. {
  16447. return error_node();
  16448. }
  16449. #endif
  16450. inline void parse_pending_string_rangesize(expression_node_ptr& expression)
  16451. {
  16452. // Allow no more than 100 range calls, eg: s[][][]...[][]
  16453. const std::size_t max_rangesize_parses = 100;
  16454. std::size_t i = 0;
  16455. while
  16456. (
  16457. (0 != expression) &&
  16458. (i++ < max_rangesize_parses) &&
  16459. error_list_.empty() &&
  16460. token_is(token_t::e_lsqrbracket,false) &&
  16461. is_generally_string_node(expression)
  16462. )
  16463. {
  16464. expression = parse_string_range_statement(expression);
  16465. }
  16466. }
  16467. template <typename Allocator1,
  16468. typename Allocator2,
  16469. template <typename,typename> class Sequence>
  16470. inline expression_node_ptr simplify(Sequence<expression_node_ptr,Allocator1>& expression_list,
  16471. Sequence<bool,Allocator2>& side_effect_list)
  16472. {
  16473. if (expression_list.empty())
  16474. return error_node();
  16475. else if (1 == expression_list.size())
  16476. return expression_list[0];
  16477. Sequence<expression_node_ptr,Allocator1> tmp_expression_list;
  16478. bool return_node_present = false;
  16479. for (std::size_t i = 0; i < (expression_list.size() - 1); ++i)
  16480. {
  16481. if (is_variable_node(expression_list[i]))
  16482. continue;
  16483. else if (
  16484. is_return_node (expression_list[i]) ||
  16485. is_break_node (expression_list[i]) ||
  16486. is_continue_node(expression_list[i])
  16487. )
  16488. {
  16489. tmp_expression_list.push_back(expression_list[i]);
  16490. // Remove all subexpressions after first short-circuit
  16491. // node has been encountered.
  16492. for (std::size_t j = i + 1; j < expression_list.size(); ++j)
  16493. {
  16494. free_node(node_allocator_,expression_list[j]);
  16495. }
  16496. return_node_present = true;
  16497. break;
  16498. }
  16499. else if (
  16500. is_constant_node(expression_list[i]) ||
  16501. is_null_node (expression_list[i]) ||
  16502. !side_effect_list[i]
  16503. )
  16504. {
  16505. free_node(node_allocator_,expression_list[i]);
  16506. continue;
  16507. }
  16508. else
  16509. tmp_expression_list.push_back(expression_list[i]);
  16510. }
  16511. if (!return_node_present)
  16512. {
  16513. tmp_expression_list.push_back(expression_list.back());
  16514. }
  16515. expression_list.swap(tmp_expression_list);
  16516. if (tmp_expression_list.size() > expression_list.size())
  16517. {
  16518. exprtk_debug(("simplify() - Reduced subexpressions from %d to %d\n",
  16519. static_cast<int>(tmp_expression_list.size()),
  16520. static_cast<int>(expression_list .size())));
  16521. }
  16522. if (
  16523. return_node_present ||
  16524. side_effect_list.back() ||
  16525. (expression_list.size() > 1)
  16526. )
  16527. state_.activate_side_effect("simplify()");
  16528. if (1 == expression_list.size())
  16529. return expression_list[0];
  16530. else
  16531. return expression_generator_.vararg_function(details::e_multi,expression_list);
  16532. }
  16533. inline expression_node_ptr parse_multi_sequence(const std::string& source = "")
  16534. {
  16535. token_t::token_type close_bracket = token_t::e_rcrlbracket;
  16536. token_t::token_type seperator = token_t::e_eof;
  16537. if (!token_is(token_t::e_lcrlbracket))
  16538. {
  16539. if (token_is(token_t::e_lbracket))
  16540. {
  16541. close_bracket = token_t::e_rbracket;
  16542. seperator = token_t::e_comma;
  16543. }
  16544. else
  16545. {
  16546. set_error(
  16547. make_error(parser_error::e_syntax,
  16548. current_token(),
  16549. "ERR088 - Expected '" + token_t::to_str(close_bracket) + "' for call to multi-sequence" +
  16550. ((!source.empty()) ? std::string(" section of " + source): "")));
  16551. return error_node();
  16552. }
  16553. }
  16554. else if (token_is(token_t::e_rcrlbracket))
  16555. {
  16556. return node_allocator_.allocate<details::null_node<T> >();
  16557. }
  16558. std::vector<expression_node_ptr> arg_list;
  16559. std::vector<bool> side_effect_list;
  16560. expression_node_ptr result = error_node();
  16561. scoped_vec_delete<expression_node_t> sdd(*this,arg_list);
  16562. scope_handler sh(*this);
  16563. scoped_bool_or_restorer sbr(state_.side_effect_present);
  16564. for ( ; ; )
  16565. {
  16566. state_.side_effect_present = false;
  16567. expression_node_ptr arg = parse_expression();
  16568. if (0 == arg)
  16569. return error_node();
  16570. else
  16571. {
  16572. arg_list.push_back(arg);
  16573. side_effect_list.push_back(state_.side_effect_present);
  16574. }
  16575. if (token_is(close_bracket))
  16576. break;
  16577. bool is_next_close = peek_token_is(close_bracket);
  16578. if (!token_is(seperator) && is_next_close)
  16579. {
  16580. set_error(
  16581. make_error(parser_error::e_syntax,
  16582. current_token(),
  16583. "ERR089 - Expected '" + details::to_str(seperator) + "' for call to multi-sequence section of " + source));
  16584. return error_node();
  16585. }
  16586. if (token_is(close_bracket))
  16587. break;
  16588. }
  16589. result = simplify(arg_list,side_effect_list);
  16590. sdd.delete_ptr = (0 == result);
  16591. return result;
  16592. }
  16593. inline bool parse_range(range_t& rp, const bool skip_lsqr = false)
  16594. {
  16595. // Examples of valid ranges:
  16596. // 1. [1:5] -> 1..5
  16597. // 2. [ :5] -> 0..5
  16598. // 3. [1: ] -> 1..end
  16599. // 4. [x:y] -> x..y where x <= y
  16600. // 5. [x+1:y/2] -> x+1..y/2 where x+1 <= y/2
  16601. // 6. [ :y] -> 0..y where 0 <= y
  16602. // 7. [x: ] -> x..end where x <= end
  16603. rp.clear();
  16604. if (!skip_lsqr && !token_is(token_t::e_lsqrbracket))
  16605. {
  16606. set_error(
  16607. make_error(parser_error::e_syntax,
  16608. current_token(),
  16609. "ERR090 - Expected '[' for start of range"));
  16610. return false;
  16611. }
  16612. if (token_is(token_t::e_colon))
  16613. {
  16614. rp.n0_c.first = true;
  16615. rp.n0_c.second = 0;
  16616. rp.cache.first = 0;
  16617. }
  16618. else
  16619. {
  16620. expression_node_ptr r0 = parse_expression();
  16621. if (0 == r0)
  16622. {
  16623. set_error(
  16624. make_error(parser_error::e_syntax,
  16625. current_token(),
  16626. "ERR091 - Failed parse begin section of range"));
  16627. return false;
  16628. }
  16629. else if (is_constant_node(r0))
  16630. {
  16631. T r0_value = r0->value();
  16632. if (r0_value >= T(0))
  16633. {
  16634. rp.n0_c.first = true;
  16635. rp.n0_c.second = static_cast<std::size_t>(details::numeric::to_int64(r0_value));
  16636. rp.cache.first = rp.n0_c.second;
  16637. }
  16638. free_node(node_allocator_,r0);
  16639. if (r0_value < T(0))
  16640. {
  16641. set_error(
  16642. make_error(parser_error::e_syntax,
  16643. current_token(),
  16644. "ERR092 - Range lower bound less than zero! Constraint: r0 >= 0"));
  16645. return false;
  16646. }
  16647. }
  16648. else
  16649. {
  16650. rp.n0_e.first = true;
  16651. rp.n0_e.second = r0;
  16652. }
  16653. if (!token_is(token_t::e_colon))
  16654. {
  16655. set_error(
  16656. make_error(parser_error::e_syntax,
  16657. current_token(),
  16658. "ERR093 - Expected ':' for break in range"));
  16659. rp.free();
  16660. return false;
  16661. }
  16662. }
  16663. if (token_is(token_t::e_rsqrbracket))
  16664. {
  16665. rp.n1_c.first = true;
  16666. rp.n1_c.second = std::numeric_limits<std::size_t>::max();
  16667. }
  16668. else
  16669. {
  16670. expression_node_ptr r1 = parse_expression();
  16671. if (0 == r1)
  16672. {
  16673. set_error(
  16674. make_error(parser_error::e_syntax,
  16675. current_token(),
  16676. "ERR094 - Failed parse end section of range"));
  16677. rp.free();
  16678. return false;
  16679. }
  16680. else if (is_constant_node(r1))
  16681. {
  16682. T r1_value = r1->value();
  16683. if (r1_value >= T(0))
  16684. {
  16685. rp.n1_c.first = true;
  16686. rp.n1_c.second = static_cast<std::size_t>(details::numeric::to_int64(r1_value));
  16687. rp.cache.second = rp.n1_c.second;
  16688. }
  16689. free_node(node_allocator_,r1);
  16690. if (r1_value < T(0))
  16691. {
  16692. set_error(
  16693. make_error(parser_error::e_syntax,
  16694. current_token(),
  16695. "ERR095 - Range upper bound less than zero! Constraint: r1 >= 0"));
  16696. return false;
  16697. }
  16698. }
  16699. else
  16700. {
  16701. rp.n1_e.first = true;
  16702. rp.n1_e.second = r1;
  16703. }
  16704. if (!token_is(token_t::e_rsqrbracket))
  16705. {
  16706. set_error(
  16707. make_error(parser_error::e_syntax,
  16708. current_token(),
  16709. "ERR096 - Expected ']' for start of range"));
  16710. rp.free();
  16711. return false;
  16712. }
  16713. }
  16714. if (rp.const_range())
  16715. {
  16716. std::size_t r0 = 0;
  16717. std::size_t r1 = 0;
  16718. bool rp_result = rp(r0,r1);
  16719. if (!rp_result || (r0 > r1))
  16720. {
  16721. set_error(
  16722. make_error(parser_error::e_syntax,
  16723. current_token(),
  16724. "ERR097 - Invalid range, Constraint: r0 <= r1"));
  16725. return false;
  16726. }
  16727. }
  16728. return true;
  16729. }
  16730. inline void lodge_symbol(const std::string& symbol,
  16731. const symbol_type st)
  16732. {
  16733. dec_.add_symbol(symbol,st);
  16734. }
  16735. inline expression_node_ptr parse_string()
  16736. {
  16737. const std::string symbol = current_token().value;
  16738. if (!symtab_store_.is_conststr_stringvar(symbol))
  16739. {
  16740. set_error(
  16741. make_error(parser_error::e_syntax,
  16742. current_token(),
  16743. "ERR098 - Unknown string symbol"));
  16744. return error_node();
  16745. }
  16746. expression_node_ptr result = symtab_store_.get_stringvar(symbol);
  16747. typedef details::stringvar_node<T>* strvar_node_t;
  16748. strvar_node_t const_str_node = static_cast<strvar_node_t>(0);
  16749. const bool is_const_string = symtab_store_.is_constant_string(symbol);
  16750. if (is_const_string)
  16751. {
  16752. const_str_node = static_cast<strvar_node_t>(result);
  16753. result = expression_generator_(const_str_node->str());
  16754. }
  16755. lodge_symbol(symbol,e_st_string);
  16756. if (peek_token_is(token_t::e_lsqrbracket))
  16757. {
  16758. next_token();
  16759. if (peek_token_is(token_t::e_rsqrbracket))
  16760. {
  16761. next_token();
  16762. next_token();
  16763. if (const_str_node)
  16764. {
  16765. free_node(node_allocator_,result);
  16766. return expression_generator_(T(const_str_node->size()));
  16767. }
  16768. else
  16769. return node_allocator_.allocate<details::stringvar_size_node<T> >
  16770. (static_cast<details::stringvar_node<T>*>(result)->ref());
  16771. }
  16772. range_t rp;
  16773. if (!parse_range(rp))
  16774. {
  16775. free_node(node_allocator_,result);
  16776. return error_node();
  16777. }
  16778. else if (const_str_node)
  16779. {
  16780. free_node(node_allocator_,result);
  16781. result = expression_generator_(const_str_node->ref(),rp);
  16782. }
  16783. else
  16784. result = expression_generator_(static_cast<details::stringvar_node<T>*>(result)->ref(),rp);
  16785. if (result)
  16786. rp.clear();
  16787. }
  16788. else
  16789. next_token();
  16790. return result;
  16791. }
  16792. inline expression_node_ptr parse_const_string()
  16793. {
  16794. const std::string const_str = current_token().value;
  16795. expression_node_ptr result = expression_generator_(const_str);
  16796. if (peek_token_is(token_t::e_lsqrbracket))
  16797. {
  16798. next_token();
  16799. if (peek_token_is(token_t::e_rsqrbracket))
  16800. {
  16801. next_token();
  16802. next_token();
  16803. free_node(node_allocator_,result);
  16804. return expression_generator_(T(const_str.size()));
  16805. }
  16806. range_t rp;
  16807. if (!parse_range(rp))
  16808. {
  16809. free_node(node_allocator_,result);
  16810. return error_node();
  16811. }
  16812. free_node(node_allocator_,result);
  16813. if (rp.n1_c.first && (rp.n1_c.second == std::numeric_limits<std::size_t>::max()))
  16814. {
  16815. rp.n1_c.second = const_str.size() - 1;
  16816. rp.cache.second = rp.n1_c.second;
  16817. }
  16818. if (
  16819. (rp.n0_c.first && (rp.n0_c.second >= const_str.size())) ||
  16820. (rp.n1_c.first && (rp.n1_c.second >= const_str.size()))
  16821. )
  16822. {
  16823. set_error(
  16824. make_error(parser_error::e_syntax,
  16825. current_token(),
  16826. "ERR099 - Overflow in range for string: '" + const_str + "'[" +
  16827. (rp.n0_c.first ? details::to_str(rp.n0_c.second) : "?") + ":" +
  16828. (rp.n1_c.first ? details::to_str(rp.n1_c.second) : "?") + "]"));
  16829. return error_node();
  16830. }
  16831. result = expression_generator_(const_str,rp);
  16832. if (result)
  16833. rp.clear();
  16834. }
  16835. else
  16836. next_token();
  16837. return result;
  16838. }
  16839. inline expression_node_ptr parse_vector()
  16840. {
  16841. const std::string symbol = current_token().value;
  16842. vector_holder_ptr vec = vector_holder_ptr(0);
  16843. const scope_element& se = sem_.get_active_element(symbol);
  16844. if (
  16845. (se.name != symbol) ||
  16846. (se.depth > state_.scope_depth) ||
  16847. (scope_element::e_vector != se.type)
  16848. )
  16849. {
  16850. if (0 == (vec = symtab_store_.get_vector(symbol)))
  16851. {
  16852. set_error(
  16853. make_error(parser_error::e_syntax,
  16854. current_token(),
  16855. "ERR100 - Symbol '" + symbol+ " not a vector"));
  16856. return error_node();
  16857. }
  16858. }
  16859. else
  16860. vec = se.vec_node;
  16861. expression_node_ptr index_expr = error_node();
  16862. next_token();
  16863. if (!token_is(token_t::e_lsqrbracket))
  16864. {
  16865. return node_allocator_.allocate<vector_node_t>(vec);
  16866. }
  16867. else if (token_is(token_t::e_rsqrbracket))
  16868. {
  16869. return expression_generator_(T(vec->size()));
  16870. }
  16871. else if (0 == (index_expr = parse_expression()))
  16872. {
  16873. set_error(
  16874. make_error(parser_error::e_syntax,
  16875. current_token(),
  16876. "ERR101 - Failed to parse index for vector: '" + symbol + "'"));
  16877. return error_node();
  16878. }
  16879. else if (!token_is(token_t::e_rsqrbracket))
  16880. {
  16881. set_error(
  16882. make_error(parser_error::e_syntax,
  16883. current_token(),
  16884. "ERR102 - Expected ']' for index of vector: '" + symbol + "'"));
  16885. free_node(node_allocator_,index_expr);
  16886. return error_node();
  16887. }
  16888. return expression_generator_.vector_element(symbol,vec,index_expr);
  16889. }
  16890. inline expression_node_ptr parse_vararg_function_call(ivararg_function<T>* vararg_function, const std::string& vararg_function_name)
  16891. {
  16892. std::vector<expression_node_ptr> arg_list;
  16893. expression_node_ptr result = error_node();
  16894. scoped_vec_delete<expression_node_t> sdd(*this,arg_list);
  16895. next_token();
  16896. if (token_is(token_t::e_lbracket))
  16897. {
  16898. if (token_is(token_t::e_rbracket))
  16899. {
  16900. if (!vararg_function->allow_zero_parameters())
  16901. {
  16902. set_error(
  16903. make_error(parser_error::e_syntax,
  16904. current_token(),
  16905. "ERR103 - Zero parameter call to vararg function: "
  16906. + vararg_function_name + " not allowed"));
  16907. return error_node();
  16908. }
  16909. }
  16910. else
  16911. {
  16912. for ( ; ; )
  16913. {
  16914. expression_node_ptr arg = parse_expression();
  16915. if (0 == arg)
  16916. return error_node();
  16917. else
  16918. arg_list.push_back(arg);
  16919. if (token_is(token_t::e_rbracket))
  16920. break;
  16921. else if (!token_is(token_t::e_comma))
  16922. {
  16923. set_error(
  16924. make_error(parser_error::e_syntax,
  16925. current_token(),
  16926. "ERR104 - Expected ',' for call to vararg function: "
  16927. + vararg_function_name));
  16928. return error_node();
  16929. }
  16930. }
  16931. }
  16932. }
  16933. else if (!vararg_function->allow_zero_parameters())
  16934. {
  16935. set_error(
  16936. make_error(parser_error::e_syntax,
  16937. current_token(),
  16938. "ERR105 - Zero parameter call to vararg function: "
  16939. + vararg_function_name + " not allowed"));
  16940. return error_node();
  16941. }
  16942. result = expression_generator_.vararg_function_call(vararg_function,arg_list);
  16943. sdd.delete_ptr = (0 == result);
  16944. return result;
  16945. }
  16946. class type_checker
  16947. {
  16948. public:
  16949. typedef parser<T> parser_t;
  16950. typedef std::vector<std::string> param_seq_list_t;
  16951. type_checker(parser_t& p,
  16952. const std::string& func_name,
  16953. const std::string& param_seq)
  16954. : invalid_state_(true),
  16955. parser_(p),
  16956. function_name_(func_name)
  16957. {
  16958. split(param_seq);
  16959. }
  16960. bool verify(const std::string& param_seq, std::size_t& pseq_index)
  16961. {
  16962. if (param_seq_list_.empty())
  16963. return true;
  16964. std::vector<std::pair<std::size_t,char> > error_list;
  16965. for (std::size_t i = 0; i < param_seq_list_.size(); ++i)
  16966. {
  16967. std::size_t diff_index = 0;
  16968. char diff_value = 0;
  16969. bool result = details::sequence_match(param_seq_list_[i],
  16970. param_seq,
  16971. diff_index,diff_value);
  16972. if (result)
  16973. {
  16974. pseq_index = i;
  16975. return true;
  16976. }
  16977. else
  16978. error_list.push_back(std::make_pair(diff_index,diff_value));
  16979. }
  16980. if (1 == error_list.size())
  16981. {
  16982. parser_.
  16983. set_error(
  16984. make_error(parser_error::e_syntax,
  16985. parser_.current_token(),
  16986. "ERR106 - Failed parameter type check for function '" + function_name_ + "', "
  16987. "Expected '" + param_seq_list_[0] + "' call set: '" + param_seq +"'"));
  16988. }
  16989. else
  16990. {
  16991. // find first with largest diff_index;
  16992. std::size_t max_diff_index = 0;
  16993. for (std::size_t i = 1; i < error_list.size(); ++i)
  16994. {
  16995. if (error_list[i].first > error_list[max_diff_index].first)
  16996. {
  16997. max_diff_index = i;
  16998. }
  16999. }
  17000. parser_.
  17001. set_error(
  17002. make_error(parser_error::e_syntax,
  17003. parser_.current_token(),
  17004. "ERR107 - Failed parameter type check for function '" + function_name_ + "', "
  17005. "Best match: '" + param_seq_list_[max_diff_index] + "' call set: '" + param_seq +"'"));
  17006. }
  17007. return false;
  17008. }
  17009. std::size_t paramseq_count() const
  17010. {
  17011. return param_seq_list_.size();
  17012. }
  17013. std::string paramseq(const std::size_t& index) const
  17014. {
  17015. return param_seq_list_[index];
  17016. }
  17017. bool invalid() const
  17018. {
  17019. return !invalid_state_;
  17020. }
  17021. private:
  17022. void split(const std::string& s)
  17023. {
  17024. if (s.empty())
  17025. return;
  17026. std::size_t start = 0;
  17027. std::size_t end = 0;
  17028. param_seq_list_t param_seq_list;
  17029. struct token_validator
  17030. {
  17031. static inline bool process(const std::string& str,
  17032. std::size_t s, std::size_t e,
  17033. param_seq_list_t& psl)
  17034. {
  17035. if (
  17036. (e - s) &&
  17037. (std::string::npos == str.find("?*")) &&
  17038. (std::string::npos == str.find("**"))
  17039. )
  17040. {
  17041. const std::string curr_str = str.substr(s,e - s);
  17042. if (std::string::npos == curr_str.find_first_not_of("STV*?|"))
  17043. {
  17044. psl.push_back(curr_str);
  17045. return true;
  17046. }
  17047. }
  17048. return false;
  17049. }
  17050. };
  17051. while (std::string::npos != (end = s.find('|',start)))
  17052. {
  17053. if (!token_validator::process(s,start,end,param_seq_list))
  17054. {
  17055. invalid_state_ = false;
  17056. const std::string err_param_seq = s.substr(start,end - start);
  17057. parser_.
  17058. set_error(
  17059. make_error(parser_error::e_syntax,
  17060. parser_.current_token(),
  17061. "ERR108 - Invalid parameter sequence of '" + err_param_seq +
  17062. "' for function: " + function_name_));
  17063. return;
  17064. }
  17065. else
  17066. start = end + 1;
  17067. }
  17068. if (start < s.size())
  17069. {
  17070. if (token_validator::process(s,start,s.size(),param_seq_list))
  17071. param_seq_list_ = param_seq_list;
  17072. else
  17073. {
  17074. const std::string err_param_seq = s.substr(start,s.size() - start);
  17075. parser_.
  17076. set_error(
  17077. make_error(parser_error::e_syntax,
  17078. parser_.current_token(),
  17079. "ERR109 - Invalid parameter sequence of '" + err_param_seq +
  17080. "' for function: " + function_name_));
  17081. return;
  17082. }
  17083. }
  17084. }
  17085. type_checker(const type_checker&);
  17086. type_checker& operator=(const type_checker&);
  17087. bool invalid_state_;
  17088. parser_t& parser_;
  17089. std::string function_name_;
  17090. param_seq_list_t param_seq_list_;
  17091. };
  17092. inline expression_node_ptr parse_generic_function_call(igeneric_function<T>* function, const std::string& function_name)
  17093. {
  17094. std::vector<expression_node_ptr> arg_list;
  17095. scoped_vec_delete<expression_node_t> sdd(*this,arg_list);
  17096. next_token();
  17097. std::string param_type_list;
  17098. type_checker tc(*this,function_name,function->parameter_sequence);
  17099. if (tc.invalid())
  17100. {
  17101. set_error(
  17102. make_error(parser_error::e_syntax,
  17103. current_token(),
  17104. "ERR110 - Type checker instantiation failure for generic function: " + function_name));
  17105. return error_node();
  17106. }
  17107. if (token_is(token_t::e_lbracket))
  17108. {
  17109. if (token_is(token_t::e_rbracket))
  17110. {
  17111. if (!function->allow_zero_parameters())
  17112. {
  17113. set_error(
  17114. make_error(parser_error::e_syntax,
  17115. current_token(),
  17116. "ERR111 - Zero parameter call to generic function: "
  17117. + function_name + " not allowed"));
  17118. return error_node();
  17119. }
  17120. }
  17121. else
  17122. {
  17123. for ( ; ; )
  17124. {
  17125. expression_node_ptr arg = parse_expression();
  17126. if (0 == arg)
  17127. return error_node();
  17128. if (is_ivector_node(arg))
  17129. param_type_list += 'V';
  17130. else if (is_generally_string_node(arg))
  17131. param_type_list += 'S';
  17132. else // Everything else is assumed to be a scalar returning expression
  17133. param_type_list += 'T';
  17134. arg_list.push_back(arg);
  17135. if (token_is(token_t::e_rbracket))
  17136. break;
  17137. else if (!token_is(token_t::e_comma))
  17138. {
  17139. set_error(
  17140. make_error(parser_error::e_syntax,
  17141. current_token(),
  17142. "ERR112 - Expected ',' for call to generic function: " + function_name));
  17143. return error_node();
  17144. }
  17145. }
  17146. }
  17147. }
  17148. else if (!function->allow_zero_parameters())
  17149. {
  17150. set_error(
  17151. make_error(parser_error::e_syntax,
  17152. current_token(),
  17153. "ERR113 - Zero parameter call to generic function: "
  17154. + function_name + " not allowed"));
  17155. return error_node();
  17156. }
  17157. std::size_t param_seq_index = 0;
  17158. if (!tc.verify(param_type_list, param_seq_index))
  17159. {
  17160. set_error(
  17161. make_error(parser_error::e_syntax,
  17162. current_token(),
  17163. "ERR114 - Expected ',' for call to generic function: " + function_name));
  17164. return error_node();
  17165. }
  17166. expression_node_ptr result = error_node();
  17167. if (tc.paramseq_count() <= 1)
  17168. result = expression_generator_
  17169. .generic_function_call(function,arg_list);
  17170. else
  17171. result = expression_generator_
  17172. .generic_function_call(function,arg_list,param_seq_index);
  17173. sdd.delete_ptr = (0 == result);
  17174. return result;
  17175. }
  17176. inline expression_node_ptr parse_string_function_call(igeneric_function<T>* function, const std::string& function_name)
  17177. {
  17178. std::vector<expression_node_ptr> arg_list;
  17179. scoped_vec_delete<expression_node_t> sdd(*this,arg_list);
  17180. next_token();
  17181. std::string param_type_list;
  17182. type_checker tc(*this,function_name,function->parameter_sequence);
  17183. if (
  17184. (!function->parameter_sequence.empty()) &&
  17185. (0 == tc.paramseq_count())
  17186. )
  17187. {
  17188. return error_node();
  17189. }
  17190. if (token_is(token_t::e_lbracket))
  17191. {
  17192. if (!token_is(token_t::e_rbracket))
  17193. {
  17194. for ( ; ; )
  17195. {
  17196. expression_node_ptr arg = parse_expression();
  17197. if (0 == arg)
  17198. return error_node();
  17199. if (is_ivector_node(arg))
  17200. param_type_list += 'V';
  17201. else if (is_generally_string_node(arg))
  17202. param_type_list += 'S';
  17203. else // Everything else is a scalar returning expression
  17204. param_type_list += 'T';
  17205. arg_list.push_back(arg);
  17206. if (token_is(token_t::e_rbracket))
  17207. break;
  17208. else if (!token_is(token_t::e_comma))
  17209. {
  17210. set_error(
  17211. make_error(parser_error::e_syntax,
  17212. current_token(),
  17213. "ERR115 - Expected ',' for call to string function: " + function_name));
  17214. return error_node();
  17215. }
  17216. }
  17217. }
  17218. }
  17219. std::size_t param_seq_index = 0;
  17220. if (!tc.verify(param_type_list, param_seq_index))
  17221. {
  17222. set_error(
  17223. make_error(parser_error::e_syntax,
  17224. current_token(),
  17225. "ERR116 - Expected ',' for call to string function: " + function_name));
  17226. return error_node();
  17227. }
  17228. expression_node_ptr result = error_node();
  17229. if (tc.paramseq_count() <= 1)
  17230. result = expression_generator_
  17231. .string_function_call(function,arg_list);
  17232. else
  17233. result = expression_generator_
  17234. .string_function_call(function,arg_list,param_seq_index);
  17235. sdd.delete_ptr = (0 == result);
  17236. return result;
  17237. }
  17238. template <typename Type, std::size_t NumberOfParameters>
  17239. struct parse_special_function_impl
  17240. {
  17241. static inline expression_node_ptr process(parser<Type>& p,const details::operator_type opt_type)
  17242. {
  17243. expression_node_ptr branch[NumberOfParameters];
  17244. expression_node_ptr result = error_node();
  17245. std::fill_n(branch,NumberOfParameters,reinterpret_cast<expression_node_ptr>(0));
  17246. scoped_delete<expression_node_t,NumberOfParameters> sd(p,branch);
  17247. p.next_token();
  17248. if (!p.token_is(token_t::e_lbracket))
  17249. {
  17250. p.set_error(
  17251. make_error(parser_error::e_syntax,
  17252. p.current_token(),
  17253. "ERR117 - Expected '(' for special function"));
  17254. return error_node();
  17255. }
  17256. for (std::size_t i = 0; i < NumberOfParameters; ++i)
  17257. {
  17258. branch[i] = p.parse_expression();
  17259. if (0 == branch[i])
  17260. {
  17261. return p.error_node();
  17262. }
  17263. else if (i < (NumberOfParameters - 1))
  17264. {
  17265. if (!p.token_is(token_t::e_comma))
  17266. {
  17267. p.set_error(
  17268. make_error(parser_error::e_syntax,
  17269. p.current_token(),
  17270. "ERR118 - Expected ',' before next parameter of special function"));
  17271. return p.error_node();
  17272. }
  17273. }
  17274. }
  17275. if (!p.token_is(token_t::e_rbracket))
  17276. return p.error_node();
  17277. else
  17278. result = p.expression_generator_.special_function(opt_type,branch);
  17279. sd.delete_ptr = (0 == result);
  17280. return result;
  17281. }
  17282. };
  17283. inline expression_node_ptr parse_special_function()
  17284. {
  17285. // Expect: $fDD(expr0,expr1,expr2) or $fDD(expr0,expr1,expr2,expr3)
  17286. if (
  17287. !details::is_digit(current_token().value[2]) ||
  17288. !details::is_digit(current_token().value[3])
  17289. )
  17290. {
  17291. set_error(
  17292. make_error(parser_error::e_token,
  17293. current_token(),
  17294. "ERR119 - Invalid special function[1]: " + current_token().value));
  17295. return error_node();
  17296. }
  17297. const unsigned int id = (current_token().value[2] - '0') * 10 + (current_token().value[3] - '0');
  17298. if (id >= details::e_sffinal)
  17299. {
  17300. set_error(
  17301. make_error(parser_error::e_token,
  17302. current_token(),
  17303. "ERR120 - Invalid special function[2]: " + current_token().value));
  17304. return error_node();
  17305. }
  17306. const std::size_t sf_3_to_4 = details::e_sf48;
  17307. const details::operator_type opt_type = details::operator_type(id + 1000);
  17308. const std::size_t NumberOfParameters = (id < (sf_3_to_4 - 1000)) ? 3 : 4;
  17309. switch (NumberOfParameters)
  17310. {
  17311. case 3 : return parse_special_function_impl<T,3>::process(*this,opt_type);
  17312. case 4 : return parse_special_function_impl<T,4>::process(*this,opt_type);
  17313. default : return error_node();
  17314. }
  17315. }
  17316. inline expression_node_ptr parse_null_statement()
  17317. {
  17318. next_token();
  17319. return node_allocator_.allocate<details::null_node<T> >();
  17320. }
  17321. #ifndef exprtk_disable_break_continue
  17322. inline expression_node_ptr parse_break_statement()
  17323. {
  17324. if (state_.parsing_break_stmt)
  17325. {
  17326. set_error(
  17327. make_error(parser_error::e_syntax,
  17328. current_token(),
  17329. "ERR121 - Break call within a break call is not allowed"));
  17330. return error_node();
  17331. }
  17332. scoped_bool_negator sbn(state_.parsing_break_stmt);
  17333. if (!brkcnt_list_.empty())
  17334. {
  17335. next_token();
  17336. brkcnt_list_.front() = true;
  17337. expression_node_ptr return_expr = error_node();
  17338. if (token_is(token_t::e_lsqrbracket))
  17339. {
  17340. if (0 == (return_expr = parse_expression()))
  17341. {
  17342. set_error(
  17343. make_error(parser_error::e_syntax,
  17344. current_token(),
  17345. "ERR122 - Failed to parse return expression for 'break' statement"));
  17346. return error_node();
  17347. }
  17348. else if (!token_is(token_t::e_rsqrbracket))
  17349. {
  17350. set_error(
  17351. make_error(parser_error::e_syntax,
  17352. current_token(),
  17353. "ERR123 - Expected ']' at the completion of break's return expression"));
  17354. free_node(node_allocator_,return_expr);
  17355. return error_node();
  17356. }
  17357. }
  17358. state_.activate_side_effect("parse_break_statement()");
  17359. return node_allocator_.allocate<details::break_node<T> >(return_expr);
  17360. }
  17361. else
  17362. {
  17363. set_error(
  17364. make_error(parser_error::e_syntax,
  17365. current_token(),
  17366. "ERR124 - Invalid use of 'break', allowed only in the scope of a loop"));
  17367. }
  17368. return error_node();
  17369. }
  17370. inline expression_node_ptr parse_continue_statement()
  17371. {
  17372. if (!brkcnt_list_.empty())
  17373. {
  17374. next_token();
  17375. brkcnt_list_.front() = true;
  17376. state_.activate_side_effect("parse_continue_statement()");
  17377. return node_allocator_.allocate<details::continue_node<T> >();
  17378. }
  17379. else
  17380. {
  17381. set_error(
  17382. make_error(parser_error::e_syntax,
  17383. current_token(),
  17384. "ERR125 - Invalid use of 'continue', allowed only in the scope of a loop"));
  17385. return error_node();
  17386. }
  17387. }
  17388. #endif
  17389. inline expression_node_ptr parse_define_vector_statement(const std::string& vec_name)
  17390. {
  17391. expression_node_ptr size_expr = error_node();
  17392. if (!token_is(token_t::e_lsqrbracket))
  17393. {
  17394. set_error(
  17395. make_error(parser_error::e_syntax,
  17396. current_token(),
  17397. "ERR126 - Expected '[' as part of vector size definition"));
  17398. return error_node();
  17399. }
  17400. else if (0 == (size_expr = parse_expression()))
  17401. {
  17402. set_error(
  17403. make_error(parser_error::e_syntax,
  17404. current_token(),
  17405. "ERR127 - Failed to determine size of vector '" + vec_name + "'"));
  17406. return error_node();
  17407. }
  17408. else if (!is_constant_node(size_expr))
  17409. {
  17410. free_node(node_allocator_,size_expr);
  17411. set_error(
  17412. make_error(parser_error::e_syntax,
  17413. current_token(),
  17414. "ERR128 - Expected a literal number as size of vector '" + vec_name + "'"));
  17415. return error_node();
  17416. }
  17417. T vector_size = size_expr->value();
  17418. free_node(node_allocator_,size_expr);
  17419. if (
  17420. (vector_size <= T(0)) ||
  17421. std::not_equal_to<T>()
  17422. (T(0),vector_size - details::numeric::trunc(vector_size))
  17423. )
  17424. {
  17425. set_error(
  17426. make_error(parser_error::e_syntax,
  17427. current_token(),
  17428. "ERR129 - Invalid vector size. Must be an integer greater than zero, size: " +
  17429. details::to_str(details::numeric::to_int32(vector_size))));
  17430. return error_node();
  17431. }
  17432. std::vector<expression_node_ptr> vec_initilizer_list;
  17433. scoped_vec_delete<expression_node_t> svd(*this,vec_initilizer_list);
  17434. bool single_value_initialiser = false;
  17435. if (!token_is(token_t::e_rsqrbracket))
  17436. {
  17437. set_error(
  17438. make_error(parser_error::e_syntax,
  17439. current_token(),
  17440. "ERR130 - Expected ']' as part of vector size definition"));
  17441. return error_node();
  17442. }
  17443. else if (!token_is(token_t::e_eof))
  17444. {
  17445. if (!token_is(token_t::e_assign))
  17446. {
  17447. set_error(
  17448. make_error(parser_error::e_syntax,
  17449. current_token(),
  17450. "ERR131 - Expected ':=' as part of vector definition"));
  17451. return error_node();
  17452. }
  17453. else if (token_is(token_t::e_lsqrbracket))
  17454. {
  17455. expression_node_ptr initialiser = parse_expression();
  17456. if (0 == initialiser)
  17457. {
  17458. set_error(
  17459. make_error(parser_error::e_syntax,
  17460. current_token(),
  17461. "ERR132 - Failed to parse single vector initialiser"));
  17462. return error_node();
  17463. }
  17464. vec_initilizer_list.push_back(initialiser);
  17465. if (!token_is(token_t::e_rsqrbracket))
  17466. {
  17467. set_error(
  17468. make_error(parser_error::e_syntax,
  17469. current_token(),
  17470. "ERR133 - Expected ']' to close single value vector initialiser"));
  17471. return error_node();
  17472. }
  17473. single_value_initialiser = true;
  17474. }
  17475. else if (!token_is(token_t::e_lcrlbracket))
  17476. {
  17477. set_error(
  17478. make_error(parser_error::e_syntax,
  17479. current_token(),
  17480. "ERR134 - Expected '{' as part of vector initialiser list"));
  17481. return error_node();
  17482. }
  17483. else if (!token_is(token_t::e_rcrlbracket))
  17484. {
  17485. for ( ; ; )
  17486. {
  17487. expression_node_ptr initialiser = parse_expression();
  17488. if (0 == initialiser)
  17489. {
  17490. set_error(
  17491. make_error(parser_error::e_syntax,
  17492. current_token(),
  17493. "ERR135 - Expected '{' as part of vector initialiser list"));
  17494. return error_node();
  17495. }
  17496. else
  17497. vec_initilizer_list.push_back(initialiser);
  17498. if (token_is(token_t::e_rcrlbracket))
  17499. break;
  17500. bool is_next_close = peek_token_is(token_t::e_rcrlbracket);
  17501. if (!token_is(token_t::e_comma) && is_next_close)
  17502. {
  17503. set_error(
  17504. make_error(parser_error::e_syntax,
  17505. current_token(),
  17506. "ERR136 - Expected ',' between vector initialisers"));
  17507. return error_node();
  17508. }
  17509. if (token_is(token_t::e_rcrlbracket))
  17510. break;
  17511. }
  17512. }
  17513. if (
  17514. !token_is(token_t::e_rbracket ,false) &&
  17515. !token_is(token_t::e_rcrlbracket,false) &&
  17516. !token_is(token_t::e_rsqrbracket,false)
  17517. )
  17518. {
  17519. if (!token_is(token_t::e_eof))
  17520. {
  17521. set_error(
  17522. make_error(parser_error::e_syntax,
  17523. current_token(),
  17524. "ERR137 - Expected ';' at end of vector definition"));
  17525. return error_node();
  17526. }
  17527. }
  17528. if (vec_initilizer_list.size() > vector_size)
  17529. {
  17530. set_error(
  17531. make_error(parser_error::e_syntax,
  17532. current_token(),
  17533. "ERR138 - Initialiser list larger than the number of elements in the vector: '" + vec_name + "'"));
  17534. return error_node();
  17535. }
  17536. }
  17537. typename symbol_table_t::vector_holder_ptr vec_holder = typename symbol_table_t::vector_holder_ptr(0);
  17538. std::size_t vec_size = static_cast<std::size_t>(details::numeric::to_int32(vector_size));
  17539. scope_element& se = sem_.get_element(vec_name);
  17540. if (se.name == vec_name)
  17541. {
  17542. if (se.active)
  17543. {
  17544. set_error(
  17545. make_error(parser_error::e_syntax,
  17546. current_token(),
  17547. "ERR139 - Illegal redefinition of local vector: '" + vec_name + "'"));
  17548. return error_node();
  17549. }
  17550. else if (
  17551. (se.size == vec_size) &&
  17552. (scope_element::e_vector == se.type)
  17553. )
  17554. {
  17555. vec_holder = se.vec_node;
  17556. se.active = true;
  17557. se.depth = state_.scope_depth;
  17558. se.ref_count++;
  17559. }
  17560. }
  17561. if (0 == vec_holder)
  17562. {
  17563. scope_element nse;
  17564. nse.name = vec_name;
  17565. nse.active = true;
  17566. nse.ref_count = 1;
  17567. nse.type = scope_element::e_vector;
  17568. nse.depth = state_.scope_depth;
  17569. nse.size = vec_size;
  17570. nse.data = new T[vec_size];
  17571. nse.vec_node = new typename scope_element::vector_holder_t((T*)(nse.data),nse.size);
  17572. if (!sem_.add_element(nse))
  17573. {
  17574. set_error(
  17575. make_error(parser_error::e_syntax,
  17576. current_token(),
  17577. "ERR140 - Failed to add new local vector '" + vec_name + "' to SEM"));
  17578. sem_.free_element(nse);
  17579. return error_node();
  17580. }
  17581. vec_holder = nse.vec_node;
  17582. exprtk_debug(("parse_define_vector_statement() - INFO - Added new local vector: %s[%d]\n",
  17583. nse.name.c_str(),
  17584. static_cast<int>(nse.size)));
  17585. }
  17586. state_.activate_side_effect("parse_define_vector_statement()");
  17587. lodge_symbol(vec_name,e_st_local_vector);
  17588. expression_node_ptr result =
  17589. node_allocator_
  17590. .allocate<details::vector_assignment_node<T> >(
  17591. (*vec_holder)[0],
  17592. vec_size,
  17593. vec_initilizer_list,
  17594. single_value_initialiser);
  17595. svd.delete_ptr = (0 == result);
  17596. return result;
  17597. }
  17598. #ifndef exprtk_disable_string_capabilities
  17599. inline expression_node_ptr parse_define_string_statement(const std::string& str_name, expression_node_ptr initialisation_expression)
  17600. {
  17601. stringvar_node_t* str_node = reinterpret_cast<stringvar_node_t*>(0);
  17602. scope_element& se = sem_.get_element(str_name);
  17603. if (se.name == str_name)
  17604. {
  17605. if (se.active)
  17606. {
  17607. set_error(
  17608. make_error(parser_error::e_syntax,
  17609. current_token(),
  17610. "ERR141 - Illegal redefinition of local variable: '" + str_name + "'"));
  17611. free_node(node_allocator_,initialisation_expression);
  17612. return error_node();
  17613. }
  17614. else if ((scope_element::e_string == se.type))
  17615. {
  17616. str_node = se.str_node;
  17617. se.active = true;
  17618. se.depth = state_.scope_depth;
  17619. se.ref_count++;
  17620. }
  17621. }
  17622. if (0 == str_node)
  17623. {
  17624. scope_element nse;
  17625. nse.name = str_name;
  17626. nse.active = true;
  17627. nse.ref_count = 1;
  17628. nse.type = scope_element::e_string;
  17629. nse.depth = state_.scope_depth;
  17630. nse.data = new std::string;
  17631. nse.str_node = new stringvar_node_t(*(std::string*)(nse.data));
  17632. if (!sem_.add_element(nse))
  17633. {
  17634. set_error(
  17635. make_error(parser_error::e_syntax,
  17636. current_token(),
  17637. "ERR142 - Failed to add new local string variable '" + str_name + "' to SEM"));
  17638. free_node(node_allocator_,initialisation_expression);
  17639. sem_.free_element(nse);
  17640. return error_node();
  17641. }
  17642. str_node = nse.str_node;
  17643. exprtk_debug(("parse_define_string_statement() - INFO - Added new local string variable: %s\n",nse.name.c_str()));
  17644. }
  17645. lodge_symbol(str_name,e_st_local_string);
  17646. state_.activate_side_effect("parse_define_string_statement()");
  17647. expression_node_ptr branch[2] = {0};
  17648. branch[0] = str_node;
  17649. branch[1] = initialisation_expression;
  17650. return expression_generator_(details::e_assign,branch);
  17651. }
  17652. #else
  17653. inline expression_node_ptr parse_define_string_statement(const std::string&, expression_node_ptr)
  17654. {
  17655. return error_node();
  17656. }
  17657. #endif
  17658. inline bool local_variable_is_shadowed(const std::string& symbol)
  17659. {
  17660. const scope_element& se = sem_.get_element(symbol);
  17661. return (se.name == symbol) && se.active;
  17662. }
  17663. inline expression_node_ptr parse_define_var_statement()
  17664. {
  17665. if (settings_.vardef_disabled())
  17666. {
  17667. set_error(
  17668. make_error(parser_error::e_syntax,
  17669. current_token(),
  17670. "ERR143 - Illegal variable definition"));
  17671. return error_node();
  17672. }
  17673. else if (!details::imatch(current_token().value,"var"))
  17674. {
  17675. return error_node();
  17676. }
  17677. else
  17678. next_token();
  17679. const std::string var_name = current_token().value;
  17680. expression_node_ptr initialisation_expression = error_node();
  17681. if (!token_is(token_t::e_symbol))
  17682. {
  17683. set_error(
  17684. make_error(parser_error::e_syntax,
  17685. current_token(),
  17686. "ERR144 - Expected a symbol for variable definition"));
  17687. return error_node();
  17688. }
  17689. else if (details::is_reserved_symbol(var_name))
  17690. {
  17691. set_error(
  17692. make_error(parser_error::e_syntax,
  17693. current_token(),
  17694. "ERR145 - Illegal redefinition of reserved keyword: '" + var_name + "'"));
  17695. return error_node();
  17696. }
  17697. else if (symtab_store_.symbol_exists(var_name))
  17698. {
  17699. set_error(
  17700. make_error(parser_error::e_syntax,
  17701. current_token(),
  17702. "ERR146 - Illegal redefinition of variable '" + var_name + "'"));
  17703. return error_node();
  17704. }
  17705. else if (local_variable_is_shadowed(var_name))
  17706. {
  17707. set_error(
  17708. make_error(parser_error::e_syntax,
  17709. current_token(),
  17710. "ERR147 - Illegal redefinition of local variable: '" + var_name + "'"));
  17711. return error_node();
  17712. }
  17713. else if (token_is(token_t::e_lsqrbracket,false))
  17714. {
  17715. return parse_define_vector_statement(var_name);
  17716. }
  17717. else if (token_is(token_t::e_lcrlbracket,false))
  17718. {
  17719. return parse_uninitialised_var_statement(var_name);
  17720. }
  17721. else if (token_is(token_t::e_assign))
  17722. {
  17723. if (0 == (initialisation_expression = parse_expression()))
  17724. {
  17725. set_error(
  17726. make_error(parser_error::e_syntax,
  17727. current_token(),
  17728. "ERR148 - Failed to parse initialisation expression"));
  17729. return error_node();
  17730. }
  17731. }
  17732. if (
  17733. !token_is(token_t::e_rbracket ,false) &&
  17734. !token_is(token_t::e_rcrlbracket,false) &&
  17735. !token_is(token_t::e_rsqrbracket,false)
  17736. )
  17737. {
  17738. if (!token_is(token_t::e_eof,false))
  17739. {
  17740. set_error(
  17741. make_error(parser_error::e_syntax,
  17742. current_token(),
  17743. "ERR149 - Expected ';' after variable definition"));
  17744. free_node(node_allocator_,initialisation_expression);
  17745. return error_node();
  17746. }
  17747. }
  17748. if (
  17749. (0 != initialisation_expression) &&
  17750. details::is_generally_string_node(initialisation_expression)
  17751. )
  17752. {
  17753. return parse_define_string_statement(var_name,initialisation_expression);
  17754. }
  17755. variable_node_t* var_node = reinterpret_cast<variable_node_t*>(0);
  17756. scope_element& se = sem_.get_element(var_name);
  17757. if (se.name == var_name)
  17758. {
  17759. if (se.active)
  17760. {
  17761. set_error(
  17762. make_error(parser_error::e_syntax,
  17763. current_token(),
  17764. "ERR150 - Illegal redefinition of local variable: '" + var_name + "'"));
  17765. free_node(node_allocator_,initialisation_expression);
  17766. return error_node();
  17767. }
  17768. else if ((scope_element::e_variable == se.type))
  17769. {
  17770. var_node = se.var_node;
  17771. se.active = true;
  17772. se.depth = state_.scope_depth;
  17773. se.ref_count++;
  17774. }
  17775. }
  17776. if (0 == var_node)
  17777. {
  17778. scope_element nse;
  17779. nse.name = var_name;
  17780. nse.active = true;
  17781. nse.ref_count = 1;
  17782. nse.type = scope_element::e_variable;
  17783. nse.depth = state_.scope_depth;
  17784. nse.data = new T(T(0));
  17785. nse.var_node = new variable_node_t(*(T*)(nse.data));
  17786. if (!sem_.add_element(nse))
  17787. {
  17788. set_error(
  17789. make_error(parser_error::e_syntax,
  17790. current_token(),
  17791. "ERR151 - Failed to add new local variable '" + var_name + "' to SEM"));
  17792. free_node(node_allocator_,initialisation_expression);
  17793. sem_.free_element(nse);
  17794. return error_node();
  17795. }
  17796. var_node = nse.var_node;
  17797. exprtk_debug(("parse_define_var_statement() - INFO - Added new local variable: %s\n",nse.name.c_str()));
  17798. }
  17799. state_.activate_side_effect("parse_define_var_statement()");
  17800. lodge_symbol(var_name,e_st_local_variable);
  17801. expression_node_ptr branch[2] = {0};
  17802. branch[0] = var_node;
  17803. branch[1] = initialisation_expression ? initialisation_expression : expression_generator_(T(0));
  17804. return expression_generator_(details::e_assign,branch);
  17805. }
  17806. inline expression_node_ptr parse_uninitialised_var_statement(const std::string& var_name)
  17807. {
  17808. if (
  17809. !token_is(token_t::e_lcrlbracket) ||
  17810. !token_is(token_t::e_rcrlbracket)
  17811. )
  17812. {
  17813. set_error(
  17814. make_error(parser_error::e_syntax,
  17815. current_token(),
  17816. "ERR152 - Expected a '{}' for uninitialised var definition"));
  17817. return error_node();
  17818. }
  17819. else if (!token_is(token_t::e_eof,false))
  17820. {
  17821. set_error(
  17822. make_error(parser_error::e_syntax,
  17823. current_token(),
  17824. "ERR153 - Expected ';' after uninitialised variable definition"));
  17825. return error_node();
  17826. }
  17827. variable_node_t* var_node = reinterpret_cast<variable_node_t*>(0);
  17828. scope_element& se = sem_.get_element(var_name);
  17829. if (se.name == var_name)
  17830. {
  17831. if (se.active)
  17832. {
  17833. set_error(
  17834. make_error(parser_error::e_syntax,
  17835. current_token(),
  17836. "ERR154 - Illegal redefinition of local variable: '" + var_name + "'"));
  17837. return error_node();
  17838. }
  17839. else if (scope_element::e_variable == se.type)
  17840. {
  17841. var_node = se.var_node;
  17842. se.active = true;
  17843. se.ref_count++;
  17844. }
  17845. }
  17846. if (0 == var_node)
  17847. {
  17848. scope_element nse;
  17849. nse.name = var_name;
  17850. nse.active = true;
  17851. nse.ref_count = 1;
  17852. nse.type = scope_element::e_variable;
  17853. nse.depth = state_.scope_depth;
  17854. nse.ip_index = sem_.next_ip_index();
  17855. nse.data = new T(T(0));
  17856. nse.var_node = new variable_node_t(*(T*)(nse.data));
  17857. if (!sem_.add_element(nse))
  17858. {
  17859. set_error(
  17860. make_error(parser_error::e_syntax,
  17861. current_token(),
  17862. "ERR155 - Failed to add new local variable '" + var_name + "' to SEM"));
  17863. sem_.free_element(nse);
  17864. return error_node();
  17865. }
  17866. exprtk_debug(("parse_uninitialised_var_statement() - INFO - Added new local variable: %s\n",
  17867. nse.name.c_str()));
  17868. }
  17869. lodge_symbol(var_name,e_st_local_variable);
  17870. state_.activate_side_effect("parse_uninitialised_var_statement()");
  17871. return expression_generator_(T(0));
  17872. }
  17873. inline expression_node_ptr parse_swap_statement()
  17874. {
  17875. if (!details::imatch(current_token().value,"swap"))
  17876. {
  17877. return error_node();
  17878. }
  17879. else
  17880. next_token();
  17881. if (!token_is(token_t::e_lbracket))
  17882. {
  17883. set_error(
  17884. make_error(parser_error::e_syntax,
  17885. current_token(),
  17886. "ERR156 - Expected '(' at start of swap statement"));
  17887. return error_node();
  17888. }
  17889. expression_node_ptr variable0 = error_node();
  17890. expression_node_ptr variable1 = error_node();
  17891. bool variable0_generated = false;
  17892. bool variable1_generated = false;
  17893. const std::string var0_name = current_token().value;
  17894. if (!token_is(token_t::e_symbol,false))
  17895. {
  17896. set_error(
  17897. make_error(parser_error::e_syntax,
  17898. current_token(),
  17899. "ERR157 - Expected a symbol for variable or vector element definition"));
  17900. return error_node();
  17901. }
  17902. else if (peek_token_is(token_t::e_lsqrbracket))
  17903. {
  17904. if (0 == (variable0 = parse_vector()))
  17905. {
  17906. set_error(
  17907. make_error(parser_error::e_syntax,
  17908. current_token(),
  17909. "ERR158 - First parameter to swap is an invalid vector element: '" + var0_name + "'"));
  17910. return error_node();
  17911. }
  17912. variable0_generated = true;
  17913. }
  17914. else
  17915. {
  17916. if (symtab_store_.is_variable(var0_name))
  17917. {
  17918. variable0 = symtab_store_.get_variable(var0_name);
  17919. }
  17920. scope_element& se = sem_.get_element(var0_name);
  17921. if (
  17922. (se.active) &&
  17923. (se.name == var0_name) &&
  17924. (scope_element::e_variable == se.type)
  17925. )
  17926. {
  17927. variable0 = se.var_node;
  17928. }
  17929. lodge_symbol(var0_name,e_st_variable);
  17930. if (0 == variable0)
  17931. {
  17932. set_error(
  17933. make_error(parser_error::e_syntax,
  17934. current_token(),
  17935. "ERR159 - First parameter to swap is an invalid variable: '" + var0_name + "'"));
  17936. return error_node();
  17937. }
  17938. else
  17939. next_token();
  17940. }
  17941. if (!token_is(token_t::e_comma))
  17942. {
  17943. set_error(
  17944. make_error(parser_error::e_syntax,
  17945. current_token(),
  17946. "ERR160 - Expected ',' between parameters to swap"));
  17947. if (variable0_generated)
  17948. {
  17949. free_node(node_allocator_,variable0);
  17950. }
  17951. return error_node();
  17952. }
  17953. const std::string var1_name = current_token().value;
  17954. if (!token_is(token_t::e_symbol,false))
  17955. {
  17956. set_error(
  17957. make_error(parser_error::e_syntax,
  17958. current_token(),
  17959. "ERR161 - Expected a symbol for variable or vector element definition"));
  17960. if (variable0_generated)
  17961. {
  17962. free_node(node_allocator_,variable0);
  17963. }
  17964. return error_node();
  17965. }
  17966. else if (peek_token_is(token_t::e_lsqrbracket))
  17967. {
  17968. if (0 == (variable1 = parse_vector()))
  17969. {
  17970. set_error(
  17971. make_error(parser_error::e_syntax,
  17972. current_token(),
  17973. "ERR162 - Second parameter to swap is an invalid vector element: '" + var1_name + "'"));
  17974. if (variable0_generated)
  17975. {
  17976. free_node(node_allocator_,variable0);
  17977. }
  17978. return error_node();
  17979. }
  17980. variable1_generated = true;
  17981. }
  17982. else
  17983. {
  17984. if (symtab_store_.is_variable(var1_name))
  17985. {
  17986. variable1 = symtab_store_.get_variable(var1_name);
  17987. }
  17988. scope_element& se = sem_.get_element(var1_name);
  17989. if (
  17990. (se.active) &&
  17991. (se.name == var1_name) &&
  17992. (scope_element::e_variable == se.type)
  17993. )
  17994. {
  17995. variable1 = se.var_node;
  17996. }
  17997. lodge_symbol(var1_name,e_st_variable);
  17998. if (0 == variable1)
  17999. {
  18000. set_error(
  18001. make_error(parser_error::e_syntax,
  18002. current_token(),
  18003. "ERR163 - Second parameter to swap is an invalid variable: '" + var1_name + "'"));
  18004. if (variable0_generated)
  18005. {
  18006. free_node(node_allocator_,variable0);
  18007. }
  18008. return error_node();
  18009. }
  18010. else
  18011. next_token();
  18012. }
  18013. if (!token_is(token_t::e_rbracket))
  18014. {
  18015. set_error(
  18016. make_error(parser_error::e_syntax,
  18017. current_token(),
  18018. "ERR164 - Expected ')' at end of swap statement"));
  18019. if (variable0_generated)
  18020. {
  18021. free_node(node_allocator_,variable0);
  18022. }
  18023. if (variable1_generated)
  18024. {
  18025. free_node(node_allocator_,variable1);
  18026. }
  18027. return error_node();
  18028. }
  18029. typedef details::variable_node<T>* variable_node_ptr;
  18030. variable_node_ptr v0 = variable_node_ptr(0);
  18031. variable_node_ptr v1 = variable_node_ptr(0);
  18032. expression_node_ptr result = error_node();
  18033. if (
  18034. (0 != (v0 = dynamic_cast<variable_node_ptr>(variable0))) &&
  18035. (0 != (v1 = dynamic_cast<variable_node_ptr>(variable1)))
  18036. )
  18037. {
  18038. result = node_allocator_.allocate<details::swap_node<T> >(v0,v1);
  18039. if (variable0_generated)
  18040. {
  18041. free_node(node_allocator_,variable0);
  18042. }
  18043. if (variable1_generated)
  18044. {
  18045. free_node(node_allocator_,variable1);
  18046. }
  18047. }
  18048. else
  18049. result = node_allocator_.allocate<details::swap_generic_node<T> >(variable0,variable1);
  18050. state_.activate_side_effect("parse_swap_statement()");
  18051. return result;
  18052. }
  18053. inline expression_node_ptr parse_return_statement()
  18054. {
  18055. if (state_.parsing_return_stmt)
  18056. {
  18057. set_error(
  18058. make_error(parser_error::e_syntax,
  18059. current_token(),
  18060. "ERR165 - Return call within a return call is not allowed"));
  18061. return error_node();
  18062. }
  18063. scoped_bool_negator sbn(state_.parsing_return_stmt);
  18064. std::vector<expression_node_ptr> arg_list;
  18065. scoped_vec_delete<expression_node_t> sdd(*this,arg_list);
  18066. if (!details::imatch(current_token().value,"return"))
  18067. {
  18068. return error_node();
  18069. }
  18070. else
  18071. next_token();
  18072. if (!token_is(token_t::e_lsqrbracket))
  18073. {
  18074. set_error(
  18075. make_error(parser_error::e_syntax,
  18076. current_token(),
  18077. "ERR166 - Expected '[' at start of return statement"));
  18078. return error_node();
  18079. }
  18080. else if (!token_is(token_t::e_rsqrbracket))
  18081. {
  18082. for ( ; ; )
  18083. {
  18084. expression_node_ptr arg = parse_expression();
  18085. if (0 == arg)
  18086. return error_node();
  18087. arg_list.push_back(arg);
  18088. if (token_is(token_t::e_rsqrbracket))
  18089. break;
  18090. else if (!token_is(token_t::e_comma))
  18091. {
  18092. set_error(
  18093. make_error(parser_error::e_syntax,
  18094. current_token(),
  18095. "ERR167 - Expected ',' between values during call to return"));
  18096. return error_node();
  18097. }
  18098. }
  18099. }
  18100. else if (settings_.zero_return_disabled())
  18101. {
  18102. set_error(
  18103. make_error(parser_error::e_syntax,
  18104. current_token(),
  18105. "ERR168 - Zero parameter return statement not allowed"));
  18106. return error_node();
  18107. }
  18108. lexer::token prev_token = current_token();
  18109. if (token_is(token_t::e_rsqrbracket))
  18110. {
  18111. if (!arg_list.empty())
  18112. {
  18113. set_error(
  18114. make_error(parser_error::e_syntax,
  18115. prev_token,
  18116. "ERR169 - Invalid ']' found during return call"));
  18117. return error_node();
  18118. }
  18119. }
  18120. std::string ret_param_type_list;
  18121. for (std::size_t i = 0; i < arg_list.size(); ++i)
  18122. {
  18123. if (0 == arg_list[i])
  18124. return error_node();
  18125. else if (is_ivector_node(arg_list[i]))
  18126. ret_param_type_list += 'V';
  18127. else if (is_generally_string_node(arg_list[i]))
  18128. ret_param_type_list += 'S';
  18129. else
  18130. ret_param_type_list += 'T';
  18131. }
  18132. dec_.retparam_list_.push_back(ret_param_type_list);
  18133. expression_node_ptr result = expression_generator_.return_call(arg_list);
  18134. sdd.delete_ptr = (0 == result);
  18135. state_.return_stmt_present = true;
  18136. state_.activate_side_effect("parse_return_statement()");
  18137. return result;
  18138. }
  18139. inline bool post_variable_process(const std::string& symbol)
  18140. {
  18141. if (
  18142. peek_token_is(token_t::e_lbracket ) ||
  18143. peek_token_is(token_t::e_lcrlbracket) ||
  18144. peek_token_is(token_t::e_lsqrbracket)
  18145. )
  18146. {
  18147. if (!settings_.commutative_check_enabled())
  18148. {
  18149. set_error(
  18150. make_error(parser_error::e_syntax,
  18151. current_token(),
  18152. "ERR170 - Invalid sequence of variable '"+ symbol + "' and bracket"));
  18153. return false;
  18154. }
  18155. lexer().insert_front(token_t::e_mul);
  18156. }
  18157. return true;
  18158. }
  18159. inline bool post_bracket_process(const typename token_t::token_type& token, expression_node_ptr& branch)
  18160. {
  18161. bool implied_mul = false;
  18162. if (is_generally_string_node(branch))
  18163. return true;
  18164. switch (token)
  18165. {
  18166. case token_t::e_lcrlbracket : implied_mul = token_is(token_t::e_lbracket ,false) ||
  18167. token_is(token_t::e_lcrlbracket,false) ||
  18168. token_is(token_t::e_lsqrbracket,false) ;
  18169. break;
  18170. case token_t::e_lbracket : implied_mul = token_is(token_t::e_lbracket ,false) ||
  18171. token_is(token_t::e_lcrlbracket,false) ||
  18172. token_is(token_t::e_lsqrbracket,false) ;
  18173. break;
  18174. case token_t::e_lsqrbracket : implied_mul = token_is(token_t::e_lbracket ,false) ||
  18175. token_is(token_t::e_lcrlbracket,false) ||
  18176. token_is(token_t::e_lsqrbracket,false) ;
  18177. break;
  18178. default : return true;
  18179. }
  18180. if (implied_mul)
  18181. {
  18182. if (!settings_.commutative_check_enabled())
  18183. {
  18184. set_error(
  18185. make_error(parser_error::e_syntax,
  18186. current_token(),
  18187. "ERR171 - Invalid sequence of brackets"));
  18188. return false;
  18189. }
  18190. else if (token_t::e_eof != current_token().type)
  18191. {
  18192. lexer().insert_front(current_token().type);
  18193. lexer().insert_front(token_t::e_mul);
  18194. next_token();
  18195. }
  18196. }
  18197. return true;
  18198. }
  18199. inline expression_node_ptr parse_symtab_symbol()
  18200. {
  18201. const std::string symbol = current_token().value;
  18202. // Are we dealing with a variable or a special constant?
  18203. expression_node_ptr variable = symtab_store_.get_variable(symbol);
  18204. if (variable)
  18205. {
  18206. if (symtab_store_.is_constant_node(symbol))
  18207. {
  18208. variable = expression_generator_(variable->value());
  18209. }
  18210. if (!post_variable_process(symbol))
  18211. return error_node();
  18212. lodge_symbol(symbol,e_st_variable);
  18213. next_token();
  18214. return variable;
  18215. }
  18216. // Are we dealing with a locally defined variable, vector or string?
  18217. if (!sem_.empty())
  18218. {
  18219. scope_element& se = sem_.get_active_element(symbol);
  18220. if (se.active && (se.name == symbol))
  18221. {
  18222. if (scope_element::e_variable == se.type)
  18223. {
  18224. se.active = true;
  18225. lodge_symbol(symbol,e_st_local_variable);
  18226. if (!post_variable_process(symbol))
  18227. return error_node();
  18228. next_token();
  18229. return se.var_node;
  18230. }
  18231. else if (scope_element::e_vector == se.type)
  18232. {
  18233. return parse_vector();
  18234. }
  18235. #ifndef exprtk_disable_string_capabilities
  18236. else if (scope_element::e_string == se.type)
  18237. {
  18238. se.active = true;
  18239. lodge_symbol(symbol,e_st_local_string);
  18240. next_token();
  18241. return se.str_node;
  18242. }
  18243. #endif
  18244. }
  18245. }
  18246. #ifndef exprtk_disable_string_capabilities
  18247. // Are we dealing with a string variable?
  18248. if (symtab_store_.is_stringvar(symbol))
  18249. {
  18250. return parse_string();
  18251. }
  18252. #endif
  18253. {
  18254. // Are we dealing with a function?
  18255. ifunction<T>* function = symtab_store_.get_function(symbol);
  18256. if (function)
  18257. {
  18258. lodge_symbol(symbol,e_st_function);
  18259. expression_node_ptr func_node =
  18260. parse_function_invocation(function,symbol);
  18261. if (func_node)
  18262. return func_node;
  18263. else
  18264. {
  18265. set_error(
  18266. make_error(parser_error::e_syntax,
  18267. current_token(),
  18268. "ERR172 - Failed to generate node for function: '" + symbol + "'"));
  18269. return error_node();
  18270. }
  18271. }
  18272. }
  18273. {
  18274. // Are we dealing with a vararg function?
  18275. ivararg_function<T>* vararg_function = symtab_store_.get_vararg_function(symbol);
  18276. if (vararg_function)
  18277. {
  18278. lodge_symbol(symbol,e_st_function);
  18279. expression_node_ptr vararg_func_node =
  18280. parse_vararg_function_call(vararg_function,symbol);
  18281. if (vararg_func_node)
  18282. return vararg_func_node;
  18283. else
  18284. {
  18285. set_error(
  18286. make_error(parser_error::e_syntax,
  18287. current_token(),
  18288. "ERR173 - Failed to generate node for vararg function: '" + symbol + "'"));
  18289. return error_node();
  18290. }
  18291. }
  18292. }
  18293. {
  18294. // Are we dealing with a vararg generic function?
  18295. igeneric_function<T>* generic_function = symtab_store_.get_generic_function(symbol);
  18296. if (generic_function)
  18297. {
  18298. lodge_symbol(symbol,e_st_function);
  18299. expression_node_ptr genericfunc_node =
  18300. parse_generic_function_call(generic_function,symbol);
  18301. if (genericfunc_node)
  18302. return genericfunc_node;
  18303. else
  18304. {
  18305. set_error(
  18306. make_error(parser_error::e_syntax,
  18307. current_token(),
  18308. "ERR174 - Failed to generate node for generic function: '" + symbol + "'"));
  18309. return error_node();
  18310. }
  18311. }
  18312. }
  18313. {
  18314. // Are we dealing with a vararg string returning function?
  18315. igeneric_function<T>* string_function = symtab_store_.get_string_function(symbol);
  18316. if (string_function)
  18317. {
  18318. lodge_symbol(symbol,e_st_function);
  18319. expression_node_ptr stringfunc_node =
  18320. parse_string_function_call(string_function,symbol);
  18321. if (stringfunc_node)
  18322. return stringfunc_node;
  18323. else
  18324. {
  18325. set_error(
  18326. make_error(parser_error::e_syntax,
  18327. current_token(),
  18328. "ERR175 - Failed to generate node for string function: '" + symbol + "'"));
  18329. return error_node();
  18330. }
  18331. }
  18332. }
  18333. // Are we dealing with a vector element?
  18334. if (symtab_store_.is_vector(symbol))
  18335. {
  18336. lodge_symbol(symbol,e_st_vector);
  18337. return parse_vector();
  18338. }
  18339. if (details::is_reserved_symbol(symbol))
  18340. {
  18341. if (settings_.function_enabled(symbol) || !details::is_base_function(symbol))
  18342. {
  18343. set_error(
  18344. make_error(parser_error::e_syntax,
  18345. current_token(),
  18346. "ERR176 - Invalid use of reserved symbol '" + symbol + "'"));
  18347. return error_node();
  18348. }
  18349. }
  18350. // Should we handle unknown symbols?
  18351. if (resolve_unknown_symbol_ && unknown_symbol_resolver_)
  18352. {
  18353. if (!(settings_.rsrvd_sym_usr_disabled() && details::is_reserved_symbol(symbol)))
  18354. {
  18355. T default_value = T(0);
  18356. std::string error_message;
  18357. typename unknown_symbol_resolver::usr_symbol_type usr_symbol_type;
  18358. if (unknown_symbol_resolver_->process(symbol,usr_symbol_type,default_value,error_message))
  18359. {
  18360. bool create_result = false;
  18361. symbol_table_t& symtab = symtab_store_.get_symbol_table();
  18362. switch (usr_symbol_type)
  18363. {
  18364. case unknown_symbol_resolver::e_usr_variable_type : create_result = symtab.create_variable(symbol,default_value);
  18365. break;
  18366. case unknown_symbol_resolver::e_usr_constant_type : create_result = symtab.add_constant(symbol,default_value);
  18367. break;
  18368. default : create_result = false;
  18369. }
  18370. if (create_result)
  18371. {
  18372. expression_node_ptr var = symtab_store_.get_variable(symbol);
  18373. if (var)
  18374. {
  18375. if (symtab_store_.is_constant_node(symbol))
  18376. {
  18377. var = expression_generator_(var->value());
  18378. }
  18379. lodge_symbol(symbol,e_st_variable);
  18380. if (!post_variable_process(symbol))
  18381. return error_node();
  18382. next_token();
  18383. return var;
  18384. }
  18385. }
  18386. set_error(
  18387. make_error(parser_error::e_symtab,
  18388. current_token(),
  18389. "ERR177 - Failed to create variable: '" + symbol + "'"));
  18390. return error_node();
  18391. }
  18392. }
  18393. }
  18394. set_error(
  18395. make_error(parser_error::e_syntax,
  18396. current_token(),
  18397. "ERR178 - Undefined symbol: '" + symbol + "'"));
  18398. return error_node();
  18399. }
  18400. inline expression_node_ptr parse_symbol()
  18401. {
  18402. static const std::string symbol_if = "if" ;
  18403. static const std::string symbol_while = "while" ;
  18404. static const std::string symbol_repeat = "repeat" ;
  18405. static const std::string symbol_for = "for" ;
  18406. static const std::string symbol_switch = "switch" ;
  18407. static const std::string symbol_null = "null" ;
  18408. static const std::string symbol_break = "break" ;
  18409. static const std::string symbol_continue = "continue";
  18410. static const std::string symbol_var = "var" ;
  18411. static const std::string symbol_swap = "swap" ;
  18412. static const std::string symbol_return = "return" ;
  18413. if (valid_vararg_operation(current_token().value))
  18414. {
  18415. return parse_vararg_function();
  18416. }
  18417. else if (valid_base_operation(current_token().value))
  18418. {
  18419. return parse_base_operation();
  18420. }
  18421. else if (
  18422. details::imatch(current_token().value,symbol_if) &&
  18423. settings_.control_struct_enabled(current_token().value)
  18424. )
  18425. {
  18426. return parse_conditional_statement();
  18427. }
  18428. else if (
  18429. details::imatch(current_token().value,symbol_while) &&
  18430. settings_.control_struct_enabled(current_token().value)
  18431. )
  18432. {
  18433. return parse_while_loop();
  18434. }
  18435. else if (
  18436. details::imatch(current_token().value,symbol_repeat) &&
  18437. settings_.control_struct_enabled(current_token().value)
  18438. )
  18439. {
  18440. return parse_repeat_until_loop();
  18441. }
  18442. else if (
  18443. details::imatch(current_token().value,symbol_for) &&
  18444. settings_.control_struct_enabled(current_token().value)
  18445. )
  18446. {
  18447. return parse_for_loop();
  18448. }
  18449. else if (
  18450. details::imatch(current_token().value,symbol_switch) &&
  18451. settings_.control_struct_enabled(current_token().value)
  18452. )
  18453. {
  18454. return parse_switch_statement();
  18455. }
  18456. else if (details::is_valid_sf_symbol(current_token().value))
  18457. {
  18458. return parse_special_function();
  18459. }
  18460. else if (details::imatch(current_token().value,symbol_null))
  18461. {
  18462. return parse_null_statement();
  18463. }
  18464. #ifndef exprtk_disable_break_continue
  18465. else if (details::imatch(current_token().value,symbol_break))
  18466. {
  18467. return parse_break_statement();
  18468. }
  18469. else if (details::imatch(current_token().value,symbol_continue))
  18470. {
  18471. return parse_continue_statement();
  18472. }
  18473. #endif
  18474. else if (details::imatch(current_token().value,symbol_var))
  18475. {
  18476. return parse_define_var_statement();
  18477. }
  18478. else if (details::imatch(current_token().value,symbol_swap))
  18479. {
  18480. return parse_swap_statement();
  18481. }
  18482. else if (details::imatch(current_token().value,symbol_return))
  18483. {
  18484. return parse_return_statement();
  18485. }
  18486. else if (symtab_store_.valid() || !sem_.empty())
  18487. {
  18488. return parse_symtab_symbol();
  18489. }
  18490. else
  18491. {
  18492. set_error(
  18493. make_error(parser_error::e_symtab,
  18494. current_token(),
  18495. "ERR179 - Variable or function detected, yet symbol-table is invalid, Symbol: " + current_token().value));
  18496. return error_node();
  18497. }
  18498. }
  18499. inline expression_node_ptr parse_branch(precedence_level precedence = e_level00)
  18500. {
  18501. expression_node_ptr branch = error_node();
  18502. if (token_t::e_number == current_token().type)
  18503. {
  18504. T numeric_value = T(0);
  18505. if (details::string_to_real(current_token().value,numeric_value))
  18506. {
  18507. expression_node_ptr literal_exp = expression_generator_(numeric_value);
  18508. next_token();
  18509. branch = literal_exp;
  18510. }
  18511. else
  18512. {
  18513. set_error(
  18514. make_error(parser_error::e_numeric,
  18515. current_token(),
  18516. "ERR180 - Failed to convert '" + current_token().value + "' to a number"));
  18517. return error_node();
  18518. }
  18519. }
  18520. else if (token_t::e_symbol == current_token().type)
  18521. {
  18522. branch = parse_symbol();
  18523. }
  18524. #ifndef exprtk_disable_string_capabilities
  18525. else if (token_t::e_string == current_token().type)
  18526. {
  18527. branch = parse_const_string();
  18528. }
  18529. #endif
  18530. else if (token_t::e_lbracket == current_token().type)
  18531. {
  18532. next_token();
  18533. if (0 == (branch = parse_expression()))
  18534. return error_node();
  18535. else if (!token_is(token_t::e_rbracket))
  18536. {
  18537. set_error(
  18538. make_error(parser_error::e_syntax,
  18539. current_token(),
  18540. "ERR181 - Expected ')' instead of: '" + current_token().value + "'"));
  18541. free_node(node_allocator_,branch);
  18542. return error_node();
  18543. }
  18544. else if (!post_bracket_process(token_t::e_lbracket,branch))
  18545. {
  18546. free_node(node_allocator_,branch);
  18547. return error_node();
  18548. }
  18549. }
  18550. else if (token_t::e_lsqrbracket == current_token().type)
  18551. {
  18552. next_token();
  18553. if (0 == (branch = parse_expression()))
  18554. return error_node();
  18555. else if (!token_is(token_t::e_rsqrbracket))
  18556. {
  18557. set_error(
  18558. make_error(parser_error::e_syntax,
  18559. current_token(),
  18560. "ERR182 - Expected ']' instead of: '" + current_token().value + "'"));
  18561. free_node(node_allocator_,branch);
  18562. return error_node();
  18563. }
  18564. else if (!post_bracket_process(token_t::e_lsqrbracket,branch))
  18565. {
  18566. free_node(node_allocator_,branch);
  18567. return error_node();
  18568. }
  18569. }
  18570. else if (token_t::e_lcrlbracket == current_token().type)
  18571. {
  18572. next_token();
  18573. if (0 == (branch = parse_expression()))
  18574. return error_node();
  18575. else if (!token_is(token_t::e_rcrlbracket))
  18576. {
  18577. set_error(
  18578. make_error(parser_error::e_syntax,
  18579. current_token(),
  18580. "ERR183 - Expected '}' instead of: '" + current_token().value + "'"));
  18581. free_node(node_allocator_,branch);
  18582. return error_node();
  18583. }
  18584. else if (!post_bracket_process(token_t::e_lcrlbracket,branch))
  18585. {
  18586. free_node(node_allocator_,branch);
  18587. return error_node();
  18588. }
  18589. }
  18590. else if (token_t::e_sub == current_token().type)
  18591. {
  18592. next_token();
  18593. branch = parse_expression(e_level11);
  18594. if (
  18595. branch &&
  18596. !(
  18597. details::is_neg_unary_node (branch) &&
  18598. simplify_unary_negation_branch(branch)
  18599. )
  18600. )
  18601. {
  18602. branch = expression_generator_(details::e_neg,branch);
  18603. }
  18604. }
  18605. else if (token_t::e_add == current_token().type)
  18606. {
  18607. next_token();
  18608. branch = parse_expression(e_level13);
  18609. }
  18610. else if (token_t::e_eof == current_token().type)
  18611. {
  18612. set_error(
  18613. make_error(parser_error::e_syntax,
  18614. current_token(),
  18615. "ERR184 - Premature end of expression[1]"));
  18616. return error_node();
  18617. }
  18618. else
  18619. {
  18620. set_error(
  18621. make_error(parser_error::e_syntax,
  18622. current_token(),
  18623. "ERR185 - Premature end of expression[2]"));
  18624. return error_node();
  18625. }
  18626. if (
  18627. branch &&
  18628. (e_level00 == precedence) &&
  18629. token_is(token_t::e_ternary,false)
  18630. )
  18631. {
  18632. branch = parse_ternary_conditional_statement(branch);
  18633. }
  18634. parse_pending_string_rangesize(branch);
  18635. return branch;
  18636. }
  18637. template <typename Type>
  18638. class expression_generator
  18639. {
  18640. public:
  18641. typedef details::expression_node<Type>* expression_node_ptr;
  18642. typedef expression_node_ptr (*synthesize_functor_t)(expression_generator<T>&, const details::operator_type& operation, expression_node_ptr (&branch)[2]);
  18643. typedef std::map<std::string,synthesize_functor_t> synthesize_map_t;
  18644. typedef typename exprtk::parser<Type> parser_t;
  18645. typedef const Type& vtype;
  18646. typedef const Type ctype;
  18647. inline void init_synthesize_map()
  18648. {
  18649. #ifndef exprtk_disable_enhanced_features
  18650. synthesize_map_["(v)o(v)"] = synthesize_vov_expression::process;
  18651. synthesize_map_["(c)o(v)"] = synthesize_cov_expression::process;
  18652. synthesize_map_["(v)o(c)"] = synthesize_voc_expression::process;
  18653. #define register_synthezier(S) \
  18654. synthesize_map_[S ::node_type::id()] = S ::process; \
  18655. register_synthezier(synthesize_vovov_expression0)
  18656. register_synthezier(synthesize_vovov_expression1)
  18657. register_synthezier(synthesize_vovoc_expression0)
  18658. register_synthezier(synthesize_vovoc_expression1)
  18659. register_synthezier(synthesize_vocov_expression0)
  18660. register_synthezier(synthesize_vocov_expression1)
  18661. register_synthezier(synthesize_covov_expression0)
  18662. register_synthezier(synthesize_covov_expression1)
  18663. register_synthezier(synthesize_covoc_expression0)
  18664. register_synthezier(synthesize_covoc_expression1)
  18665. register_synthezier(synthesize_cocov_expression1)
  18666. register_synthezier(synthesize_vococ_expression0)
  18667. register_synthezier(synthesize_vovovov_expression0)
  18668. register_synthezier(synthesize_vovovoc_expression0)
  18669. register_synthezier(synthesize_vovocov_expression0)
  18670. register_synthezier(synthesize_vocovov_expression0)
  18671. register_synthezier(synthesize_covovov_expression0)
  18672. register_synthezier(synthesize_covocov_expression0)
  18673. register_synthezier(synthesize_vocovoc_expression0)
  18674. register_synthezier(synthesize_covovoc_expression0)
  18675. register_synthezier(synthesize_vococov_expression0)
  18676. register_synthezier(synthesize_vovovov_expression1)
  18677. register_synthezier(synthesize_vovovoc_expression1)
  18678. register_synthezier(synthesize_vovocov_expression1)
  18679. register_synthezier(synthesize_vocovov_expression1)
  18680. register_synthezier(synthesize_covovov_expression1)
  18681. register_synthezier(synthesize_covocov_expression1)
  18682. register_synthezier(synthesize_vocovoc_expression1)
  18683. register_synthezier(synthesize_covovoc_expression1)
  18684. register_synthezier(synthesize_vococov_expression1)
  18685. register_synthezier(synthesize_vovovov_expression2)
  18686. register_synthezier(synthesize_vovovoc_expression2)
  18687. register_synthezier(synthesize_vovocov_expression2)
  18688. register_synthezier(synthesize_vocovov_expression2)
  18689. register_synthezier(synthesize_covovov_expression2)
  18690. register_synthezier(synthesize_covocov_expression2)
  18691. register_synthezier(synthesize_vocovoc_expression2)
  18692. register_synthezier(synthesize_covovoc_expression2)
  18693. register_synthezier(synthesize_vovovov_expression3)
  18694. register_synthezier(synthesize_vovovoc_expression3)
  18695. register_synthezier(synthesize_vovocov_expression3)
  18696. register_synthezier(synthesize_vocovov_expression3)
  18697. register_synthezier(synthesize_covovov_expression3)
  18698. register_synthezier(synthesize_covocov_expression3)
  18699. register_synthezier(synthesize_vocovoc_expression3)
  18700. register_synthezier(synthesize_covovoc_expression3)
  18701. register_synthezier(synthesize_vococov_expression3)
  18702. register_synthezier(synthesize_vovovov_expression4)
  18703. register_synthezier(synthesize_vovovoc_expression4)
  18704. register_synthezier(synthesize_vovocov_expression4)
  18705. register_synthezier(synthesize_vocovov_expression4)
  18706. register_synthezier(synthesize_covovov_expression4)
  18707. register_synthezier(synthesize_covocov_expression4)
  18708. register_synthezier(synthesize_vocovoc_expression4)
  18709. register_synthezier(synthesize_covovoc_expression4)
  18710. #endif
  18711. }
  18712. inline void set_parser(parser_t& p)
  18713. {
  18714. parser_ = &p;
  18715. }
  18716. inline void set_uom(unary_op_map_t& unary_op_map)
  18717. {
  18718. unary_op_map_ = &unary_op_map;
  18719. }
  18720. inline void set_bom(binary_op_map_t& binary_op_map)
  18721. {
  18722. binary_op_map_ = &binary_op_map;
  18723. }
  18724. inline void set_ibom(inv_binary_op_map_t& inv_binary_op_map)
  18725. {
  18726. inv_binary_op_map_ = &inv_binary_op_map;
  18727. }
  18728. inline void set_sf3m(sf3_map_t& sf3_map)
  18729. {
  18730. sf3_map_ = &sf3_map;
  18731. }
  18732. inline void set_sf4m(sf4_map_t& sf4_map)
  18733. {
  18734. sf4_map_ = &sf4_map;
  18735. }
  18736. inline void set_allocator(details::node_allocator& na)
  18737. {
  18738. node_allocator_ = &na;
  18739. }
  18740. inline void set_strength_reduction_state(const bool enabled)
  18741. {
  18742. strength_reduction_enabled_ = enabled;
  18743. }
  18744. inline bool strength_reduction_enabled() const
  18745. {
  18746. return strength_reduction_enabled_;
  18747. }
  18748. inline bool valid_operator(const details::operator_type& operation, binary_functor_t& bop)
  18749. {
  18750. typename binary_op_map_t::iterator bop_itr = binary_op_map_->find(operation);
  18751. if ((*binary_op_map_).end() == bop_itr)
  18752. return false;
  18753. bop = bop_itr->second;
  18754. return true;
  18755. }
  18756. inline bool valid_operator(const details::operator_type& operation, unary_functor_t& uop)
  18757. {
  18758. typename unary_op_map_t::iterator uop_itr = unary_op_map_->find(operation);
  18759. if ((*unary_op_map_).end() == uop_itr)
  18760. return false;
  18761. uop = uop_itr->second;
  18762. return true;
  18763. }
  18764. inline details::operator_type get_operator(const binary_functor_t& bop)
  18765. {
  18766. return (*inv_binary_op_map_).find(bop)->second;
  18767. }
  18768. inline expression_node_ptr operator()(const Type& v) const
  18769. {
  18770. return node_allocator_->allocate<literal_node_t>(v);
  18771. }
  18772. inline expression_node_ptr operator()(const std::string& s) const
  18773. {
  18774. return node_allocator_->allocate<string_literal_node_t>(s);
  18775. }
  18776. inline expression_node_ptr operator()(std::string& s, range_t& rp) const
  18777. {
  18778. return node_allocator_->allocate_rr<string_range_node_t>(s,rp);
  18779. }
  18780. inline expression_node_ptr operator()(const std::string& s, range_t& rp) const
  18781. {
  18782. return node_allocator_->allocate_tt<const_string_range_node_t>(s,rp);
  18783. }
  18784. inline expression_node_ptr operator()(expression_node_ptr branch, range_t& rp) const
  18785. {
  18786. if (is_generally_string_node(branch))
  18787. return node_allocator_->allocate_tt<generic_string_range_node_t>(branch,rp);
  18788. else
  18789. return error_node();
  18790. }
  18791. inline bool unary_optimizable(const details::operator_type& operation) const
  18792. {
  18793. return (details::e_abs == operation) || (details::e_acos == operation) ||
  18794. (details::e_acosh == operation) || (details::e_asin == operation) ||
  18795. (details::e_asinh == operation) || (details::e_atan == operation) ||
  18796. (details::e_atanh == operation) || (details::e_ceil == operation) ||
  18797. (details::e_cos == operation) || (details::e_cosh == operation) ||
  18798. (details::e_exp == operation) || (details::e_expm1 == operation) ||
  18799. (details::e_floor == operation) || (details::e_log == operation) ||
  18800. (details::e_log10 == operation) || (details::e_log2 == operation) ||
  18801. (details::e_log1p == operation) || (details::e_neg == operation) ||
  18802. (details::e_pos == operation) || (details::e_round == operation) ||
  18803. (details::e_sin == operation) || (details::e_sinc == operation) ||
  18804. (details::e_sinh == operation) || (details::e_sqrt == operation) ||
  18805. (details::e_tan == operation) || (details::e_tanh == operation) ||
  18806. (details::e_cot == operation) || (details::e_sec == operation) ||
  18807. (details::e_csc == operation) || (details::e_r2d == operation) ||
  18808. (details::e_d2r == operation) || (details::e_d2g == operation) ||
  18809. (details::e_g2d == operation) || (details::e_notl == operation) ||
  18810. (details::e_sgn == operation) || (details::e_erf == operation) ||
  18811. (details::e_erfc == operation) || (details::e_ncdf == operation) ||
  18812. (details::e_frac == operation) || (details::e_trunc == operation);
  18813. }
  18814. inline bool sf3_optimizable(const std::string& sf3id, trinary_functor_t& tfunc)
  18815. {
  18816. typename sf3_map_t::iterator itr = sf3_map_->find(sf3id);
  18817. if (sf3_map_->end() == itr)
  18818. return false;
  18819. else
  18820. tfunc = itr->second.first;
  18821. return true;
  18822. }
  18823. inline bool sf4_optimizable(const std::string& sf4id, quaternary_functor_t& qfunc)
  18824. {
  18825. typename sf4_map_t::iterator itr = sf4_map_->find(sf4id);
  18826. if (sf4_map_->end() == itr)
  18827. return false;
  18828. else
  18829. qfunc = itr->second.first;
  18830. return true;
  18831. }
  18832. inline bool sf3_optimizable(const std::string& sf3id, details::operator_type& operation)
  18833. {
  18834. typename sf3_map_t::iterator itr = sf3_map_->find(sf3id);
  18835. if (sf3_map_->end() == itr)
  18836. return false;
  18837. else
  18838. operation = itr->second.second;
  18839. return true;
  18840. }
  18841. inline bool sf4_optimizable(const std::string& sf4id, details::operator_type& operation)
  18842. {
  18843. typename sf4_map_t::iterator itr = sf4_map_->find(sf4id);
  18844. if (sf4_map_->end() == itr)
  18845. return false;
  18846. else
  18847. operation = itr->second.second;
  18848. return true;
  18849. }
  18850. inline expression_node_ptr operator()(const details::operator_type& operation, expression_node_ptr (&branch)[1])
  18851. {
  18852. if (0 == branch[0])
  18853. return error_node();
  18854. else if (details::is_null_node(branch[0]))
  18855. return branch[0];
  18856. else if (details::is_break_node(branch[0]))
  18857. return error_node();
  18858. else if (details::is_continue_node(branch[0]))
  18859. return error_node();
  18860. else if (details::is_constant_node(branch[0]))
  18861. return synthesize_expression<unary_node_t,1>(operation,branch);
  18862. else if (unary_optimizable(operation) && details::is_variable_node(branch[0]))
  18863. return synthesize_uv_expression(operation,branch);
  18864. else if (unary_optimizable(operation) && details::is_ivector_node(branch[0]))
  18865. return synthesize_uvec_expression(operation,branch);
  18866. else
  18867. return synthesize_unary_expression(operation,branch);
  18868. }
  18869. inline bool is_assignment_operation(const details::operator_type& operation) const
  18870. {
  18871. return (details::e_addass == operation) ||
  18872. (details::e_subass == operation) ||
  18873. (details::e_mulass == operation) ||
  18874. (details::e_divass == operation) ||
  18875. (details::e_modass == operation) ;
  18876. }
  18877. #ifndef exprtk_disable_string_capabilities
  18878. inline bool valid_string_operation(const details::operator_type& operation) const
  18879. {
  18880. return (details::e_add == operation) ||
  18881. (details::e_lt == operation) ||
  18882. (details::e_lte == operation) ||
  18883. (details::e_gt == operation) ||
  18884. (details::e_gte == operation) ||
  18885. (details::e_eq == operation) ||
  18886. (details::e_ne == operation) ||
  18887. (details::e_in == operation) ||
  18888. (details::e_like == operation) ||
  18889. (details::e_ilike == operation) ||
  18890. (details::e_assign == operation) ||
  18891. (details::e_addass == operation) ||
  18892. (details::e_swap == operation) ;
  18893. }
  18894. #else
  18895. inline bool valid_string_operation(const details::operator_type&) const
  18896. {
  18897. return false;
  18898. }
  18899. #endif
  18900. inline std::string to_str(const details::operator_type& operation) const
  18901. {
  18902. switch (operation)
  18903. {
  18904. case details::e_add : return "+";
  18905. case details::e_sub : return "-";
  18906. case details::e_mul : return "*";
  18907. case details::e_div : return "/";
  18908. case details::e_mod : return "%";
  18909. case details::e_pow : return "^";
  18910. case details::e_lt : return "<";
  18911. case details::e_lte : return "<=";
  18912. case details::e_gt : return ">";
  18913. case details::e_gte : return ">=";
  18914. case details::e_eq : return "==";
  18915. case details::e_ne : return "!=";
  18916. case details::e_and : return "and";
  18917. case details::e_nand : return "nand";
  18918. case details::e_or : return "or";
  18919. case details::e_nor : return "nor";
  18920. case details::e_xor : return "xor";
  18921. case details::e_xnor : return "xnor";
  18922. default : return "UNKNOWN";
  18923. }
  18924. }
  18925. inline bool operation_optimizable(const details::operator_type& operation) const
  18926. {
  18927. return (details::e_add == operation) ||
  18928. (details::e_sub == operation) ||
  18929. (details::e_mul == operation) ||
  18930. (details::e_div == operation) ||
  18931. (details::e_mod == operation) ||
  18932. (details::e_pow == operation) ||
  18933. (details::e_lt == operation) ||
  18934. (details::e_lte == operation) ||
  18935. (details::e_gt == operation) ||
  18936. (details::e_gte == operation) ||
  18937. (details::e_eq == operation) ||
  18938. (details::e_ne == operation) ||
  18939. (details::e_and == operation) ||
  18940. (details::e_nand == operation) ||
  18941. (details::e_or == operation) ||
  18942. (details::e_nor == operation) ||
  18943. (details::e_xor == operation) ||
  18944. (details::e_xnor == operation) ;
  18945. }
  18946. inline std::string branch_to_id(expression_node_ptr branch)
  18947. {
  18948. static const std::string null_str ("(null)" );
  18949. static const std::string const_str ("(c)" );
  18950. static const std::string var_str ("(v)" );
  18951. static const std::string vov_str ("(vov)" );
  18952. static const std::string cov_str ("(cov)" );
  18953. static const std::string voc_str ("(voc)" );
  18954. static const std::string str_str ("(s)" );
  18955. static const std::string strrng_str ("(rngs)" );
  18956. static const std::string cs_str ("(cs)" );
  18957. static const std::string cstrrng_str("(crngs)");
  18958. if (details::is_null_node(branch))
  18959. return null_str;
  18960. else if (details::is_constant_node(branch))
  18961. return const_str;
  18962. else if (details::is_variable_node(branch))
  18963. return var_str;
  18964. else if (details::is_vov_node(branch))
  18965. return vov_str;
  18966. else if (details::is_cov_node(branch))
  18967. return cov_str;
  18968. else if (details::is_voc_node(branch))
  18969. return voc_str;
  18970. else if (details::is_string_node(branch))
  18971. return str_str;
  18972. else if (details::is_const_string_node(branch))
  18973. return cs_str;
  18974. else if (details::is_string_range_node(branch))
  18975. return strrng_str;
  18976. else if (details::is_const_string_range_node(branch))
  18977. return cstrrng_str;
  18978. else if (details::is_t0ot1ot2_node(branch))
  18979. return "(" + dynamic_cast<details::T0oT1oT2_base_node<T>*>(branch)->type_id() + ")";
  18980. else if (details::is_t0ot1ot2ot3_node(branch))
  18981. return "(" + dynamic_cast<details::T0oT1oT2oT3_base_node<T>*>(branch)->type_id() + ")";
  18982. else
  18983. return "ERROR";
  18984. }
  18985. inline std::string branch_to_id(expression_node_ptr (&branch)[2])
  18986. {
  18987. return branch_to_id(branch[0]) + std::string("o") + branch_to_id(branch[1]);
  18988. }
  18989. inline bool cov_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  18990. {
  18991. if (!operation_optimizable(operation))
  18992. return false;
  18993. else
  18994. return (details::is_constant_node(branch[0]) && details::is_variable_node(branch[1]));
  18995. }
  18996. inline bool voc_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  18997. {
  18998. if (!operation_optimizable(operation))
  18999. return false;
  19000. else
  19001. return (details::is_variable_node(branch[0]) && details::is_constant_node(branch[1]));
  19002. }
  19003. inline bool vov_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  19004. {
  19005. if (!operation_optimizable(operation))
  19006. return false;
  19007. else
  19008. return (details::is_variable_node(branch[0]) && details::is_variable_node(branch[1]));
  19009. }
  19010. inline bool cob_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  19011. {
  19012. if (!operation_optimizable(operation))
  19013. return false;
  19014. else
  19015. return (details::is_constant_node(branch[0]) && !details::is_constant_node(branch[1]));
  19016. }
  19017. inline bool boc_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  19018. {
  19019. if (!operation_optimizable(operation))
  19020. return false;
  19021. else
  19022. return (!details::is_constant_node(branch[0]) && details::is_constant_node(branch[1]));
  19023. }
  19024. inline bool cocob_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  19025. {
  19026. if (
  19027. (details::e_add == operation) ||
  19028. (details::e_sub == operation) ||
  19029. (details::e_mul == operation) ||
  19030. (details::e_div == operation)
  19031. )
  19032. {
  19033. return (details::is_constant_node(branch[0]) && details::is_cob_node(branch[1])) ||
  19034. (details::is_constant_node(branch[1]) && details::is_cob_node(branch[0]));
  19035. }
  19036. else
  19037. return false;
  19038. }
  19039. inline bool coboc_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  19040. {
  19041. if (
  19042. (details::e_add == operation) ||
  19043. (details::e_sub == operation) ||
  19044. (details::e_mul == operation) ||
  19045. (details::e_div == operation)
  19046. )
  19047. {
  19048. return (details::is_constant_node(branch[0]) && details::is_boc_node(branch[1])) ||
  19049. (details::is_constant_node(branch[1]) && details::is_boc_node(branch[0]));
  19050. }
  19051. else
  19052. return false;
  19053. }
  19054. inline bool uvouv_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  19055. {
  19056. if (!operation_optimizable(operation))
  19057. return false;
  19058. else
  19059. return (details::is_uv_node(branch[0]) && details::is_uv_node(branch[1]));
  19060. }
  19061. inline bool vob_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  19062. {
  19063. if (!operation_optimizable(operation))
  19064. return false;
  19065. else
  19066. return (details::is_variable_node(branch[0]) && !details::is_variable_node(branch[1]));
  19067. }
  19068. inline bool bov_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  19069. {
  19070. if (!operation_optimizable(operation))
  19071. return false;
  19072. else
  19073. return (!details::is_variable_node(branch[0]) && details::is_variable_node(branch[1]));
  19074. }
  19075. inline bool binext_optimizable(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const
  19076. {
  19077. if (!operation_optimizable(operation))
  19078. return false;
  19079. else
  19080. return (!details::is_constant_node(branch[0]) || !details::is_constant_node(branch[1]));
  19081. }
  19082. inline bool is_invalid_assignment_op(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  19083. {
  19084. if (is_assignment_operation(operation))
  19085. {
  19086. const bool b1_is_genstring = details::is_generally_string_node(branch[1]);
  19087. if (details::is_string_node(branch[0]))
  19088. return !b1_is_genstring;
  19089. else
  19090. return (
  19091. !details::is_variable_node (branch[0]) &&
  19092. !details::is_vector_elem_node(branch[0]) &&
  19093. !details::is_vector_node (branch[0])
  19094. )
  19095. || b1_is_genstring;
  19096. }
  19097. else
  19098. return false;
  19099. }
  19100. inline bool is_invalid_break_continue_op(expression_node_ptr (&branch)[2])
  19101. {
  19102. return (
  19103. details::is_break_node (branch[0]) ||
  19104. details::is_break_node (branch[1]) ||
  19105. details::is_continue_node(branch[0]) ||
  19106. details::is_continue_node(branch[1])
  19107. );
  19108. }
  19109. inline bool is_invalid_string_op(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  19110. {
  19111. const bool b0_string = is_generally_string_node(branch[0]);
  19112. const bool b1_string = is_generally_string_node(branch[1]);
  19113. bool result = false;
  19114. if (b0_string ^ b1_string)
  19115. result = true;
  19116. else if (!valid_string_operation(operation) && b0_string && b1_string)
  19117. result = true;
  19118. if (result)
  19119. {
  19120. parser_->set_synthesis_error("Invalid string operation");
  19121. }
  19122. return result;
  19123. }
  19124. inline bool is_invalid_string_op(const details::operator_type& operation, expression_node_ptr (&branch)[3])
  19125. {
  19126. const bool b0_string = is_generally_string_node(branch[0]);
  19127. const bool b1_string = is_generally_string_node(branch[1]);
  19128. const bool b2_string = is_generally_string_node(branch[2]);
  19129. bool result = false;
  19130. if ((b0_string ^ b1_string) || (b1_string ^ b2_string))
  19131. result = true;
  19132. else if ((details::e_inrange != operation) && b0_string && b1_string && b2_string)
  19133. result = true;
  19134. if (result)
  19135. {
  19136. parser_->set_synthesis_error("Invalid string operation");
  19137. }
  19138. return result;
  19139. }
  19140. inline bool is_string_operation(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  19141. {
  19142. const bool b0_string = is_generally_string_node(branch[0]);
  19143. const bool b1_string = is_generally_string_node(branch[1]);
  19144. return (b0_string && b1_string && valid_string_operation(operation));
  19145. }
  19146. inline bool is_string_operation(const details::operator_type& operation, expression_node_ptr (&branch)[3])
  19147. {
  19148. const bool b0_string = is_generally_string_node(branch[0]);
  19149. const bool b1_string = is_generally_string_node(branch[1]);
  19150. const bool b2_string = is_generally_string_node(branch[2]);
  19151. return (b0_string && b1_string && b2_string && (details::e_inrange == operation));
  19152. }
  19153. #ifndef exprtk_disable_sc_andor
  19154. inline bool is_shortcircuit_expression(const details::operator_type& operation)
  19155. {
  19156. return (
  19157. (details::e_scand == operation) ||
  19158. (details::e_scor == operation)
  19159. );
  19160. }
  19161. #else
  19162. inline bool is_shortcircuit_expression(const details::operator_type&)
  19163. {
  19164. return false;
  19165. }
  19166. #endif
  19167. inline bool is_null_present(expression_node_ptr (&branch)[2])
  19168. {
  19169. return (
  19170. details::is_null_node(branch[0]) ||
  19171. details::is_null_node(branch[1])
  19172. );
  19173. }
  19174. inline bool is_vector_eqineq_operation(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  19175. {
  19176. if (!is_ivector_node(branch[0]) && !is_ivector_node(branch[1]))
  19177. return false;
  19178. else
  19179. return (
  19180. (details::e_lt == operation) ||
  19181. (details::e_lte == operation) ||
  19182. (details::e_gt == operation) ||
  19183. (details::e_gte == operation) ||
  19184. (details::e_eq == operation) ||
  19185. (details::e_ne == operation)
  19186. );
  19187. }
  19188. inline bool is_vector_arithmetic_operation(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  19189. {
  19190. if (!is_ivector_node(branch[0]) && !is_ivector_node(branch[1]))
  19191. return false;
  19192. else
  19193. return (
  19194. (details::e_add == operation) ||
  19195. (details::e_sub == operation) ||
  19196. (details::e_mul == operation) ||
  19197. (details::e_div == operation) ||
  19198. (details::e_pow == operation)
  19199. );
  19200. }
  19201. inline expression_node_ptr operator()(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  19202. {
  19203. if ((0 == branch[0]) || (0 == branch[1]))
  19204. return error_node();
  19205. else if (is_invalid_string_op(operation,branch))
  19206. return error_node();
  19207. else if (is_invalid_assignment_op(operation,branch))
  19208. return error_node();
  19209. else if (is_invalid_break_continue_op(branch))
  19210. return error_node();
  19211. else if (details::e_assign == operation)
  19212. return synthesize_assignment_expression(operation,branch);
  19213. else if (details::e_swap == operation)
  19214. return synthesize_swap_expression(branch);
  19215. else if (is_assignment_operation(operation))
  19216. return synthesize_assignment_operation_expression(operation,branch);
  19217. else if (is_vector_eqineq_operation(operation,branch))
  19218. return synthesize_veceqineq_operation_expression(operation,branch);
  19219. else if (is_vector_arithmetic_operation(operation,branch))
  19220. return synthesize_vecarithmetic_operation_expression(operation,branch);
  19221. else if (is_shortcircuit_expression(operation))
  19222. return synthesize_shortcircuit_expression(operation,branch);
  19223. else if (is_string_operation(operation,branch))
  19224. return synthesize_string_expression(operation,branch);
  19225. else if (is_null_present(branch))
  19226. return synthesize_null_expression(operation,branch);
  19227. expression_node_ptr result = error_node();
  19228. #ifndef exprtk_disable_enhanced_features
  19229. if (synthesize_expression(operation,branch,result))
  19230. return result;
  19231. else
  19232. #endif
  19233. {
  19234. /*
  19235. Possible reductions:
  19236. 1. c o cob -> cob
  19237. 2. cob o c -> cob
  19238. 3. c o boc -> boc
  19239. 4. boc o c -> boc
  19240. */
  19241. result = error_node();
  19242. if (cocob_optimizable(operation,branch))
  19243. result = synthesize_cocob_expression::process(*this,operation,branch);
  19244. else if (coboc_optimizable(operation,branch) && (0 == result))
  19245. result = synthesize_coboc_expression::process(*this,operation,branch);
  19246. if (result)
  19247. return result;
  19248. }
  19249. if (uvouv_optimizable(operation,branch))
  19250. return synthesize_uvouv_expression(operation,branch);
  19251. else if (vob_optimizable(operation,branch))
  19252. return synthesize_vob_expression::process(*this,operation,branch);
  19253. else if (bov_optimizable(operation,branch))
  19254. return synthesize_bov_expression::process(*this,operation,branch);
  19255. else if (cob_optimizable(operation,branch))
  19256. return synthesize_cob_expression::process(*this,operation,branch);
  19257. else if (boc_optimizable(operation,branch))
  19258. return synthesize_boc_expression::process(*this,operation,branch);
  19259. #ifndef exprtk_disable_enhanced_features
  19260. else if (cov_optimizable(operation,branch))
  19261. return synthesize_cov_expression::process(*this,operation,branch);
  19262. #endif
  19263. else if (binext_optimizable(operation,branch))
  19264. return synthesize_binary_ext_expression::process(*this,operation,branch);
  19265. else
  19266. return synthesize_expression<binary_node_t,2>(operation,branch);
  19267. }
  19268. inline expression_node_ptr operator()(const details::operator_type& operation, expression_node_ptr (&branch)[3])
  19269. {
  19270. if (
  19271. (0 == branch[0]) ||
  19272. (0 == branch[1]) ||
  19273. (0 == branch[2])
  19274. )
  19275. {
  19276. details::free_all_nodes(*node_allocator_,branch);
  19277. return error_node();
  19278. }
  19279. else if (is_invalid_string_op(operation,branch))
  19280. return error_node();
  19281. else if (is_string_operation(operation,branch))
  19282. return synthesize_string_expression(operation,branch);
  19283. else
  19284. return synthesize_expression<trinary_node_t,3>(operation,branch);
  19285. }
  19286. inline expression_node_ptr operator()(const details::operator_type& operation, expression_node_ptr (&branch)[4])
  19287. {
  19288. return synthesize_expression<quaternary_node_t,4>(operation,branch);
  19289. }
  19290. inline expression_node_ptr operator()(const details::operator_type& operation, expression_node_ptr b0)
  19291. {
  19292. expression_node_ptr branch[1] = { b0 };
  19293. return (*this)(operation,branch);
  19294. }
  19295. inline expression_node_ptr operator()(const details::operator_type& operation, expression_node_ptr b0, expression_node_ptr b1)
  19296. {
  19297. if ((0 == b0) || (0 == b1))
  19298. return error_node();
  19299. else
  19300. {
  19301. expression_node_ptr branch[2] = { b0, b1 };
  19302. return expression_generator<Type>::operator()(operation,branch);
  19303. }
  19304. }
  19305. inline expression_node_ptr conditional(expression_node_ptr condition,
  19306. expression_node_ptr consequent,
  19307. expression_node_ptr alternative) const
  19308. {
  19309. if ((0 == condition) || (0 == consequent))
  19310. {
  19311. free_node(*node_allocator_,condition );
  19312. free_node(*node_allocator_,consequent );
  19313. free_node(*node_allocator_,alternative);
  19314. return error_node();
  19315. }
  19316. // Can the condition be immediately evaluated? if so optimize.
  19317. else if (details::is_constant_node(condition))
  19318. {
  19319. // True branch
  19320. if (details::is_true(condition))
  19321. {
  19322. free_node(*node_allocator_,condition );
  19323. free_node(*node_allocator_,alternative);
  19324. return consequent;
  19325. }
  19326. // False branch
  19327. else
  19328. {
  19329. free_node(*node_allocator_,condition );
  19330. free_node(*node_allocator_,consequent);
  19331. if (alternative)
  19332. return alternative;
  19333. else
  19334. return node_allocator_->allocate<details::null_node<T> >();
  19335. }
  19336. }
  19337. else if ((0 != consequent) && (0 != alternative))
  19338. {
  19339. return node_allocator_->
  19340. allocate<conditional_node_t>(condition,consequent,alternative);
  19341. }
  19342. else
  19343. return node_allocator_->
  19344. allocate<cons_conditional_node_t>(condition,consequent);
  19345. }
  19346. #ifndef exprtk_disable_string_capabilities
  19347. inline expression_node_ptr conditional_string(expression_node_ptr condition,
  19348. expression_node_ptr consequent,
  19349. expression_node_ptr alternative) const
  19350. {
  19351. if ((0 == condition) || (0 == consequent))
  19352. {
  19353. free_node(*node_allocator_,condition );
  19354. free_node(*node_allocator_,consequent );
  19355. free_node(*node_allocator_,alternative);
  19356. return error_node();
  19357. }
  19358. // Can the condition be immediately evaluated? if so optimize.
  19359. else if (details::is_constant_node(condition))
  19360. {
  19361. // True branch
  19362. if (details::is_true(condition))
  19363. {
  19364. free_node(*node_allocator_,condition );
  19365. free_node(*node_allocator_,alternative);
  19366. return consequent;
  19367. }
  19368. // False branch
  19369. else
  19370. {
  19371. free_node(*node_allocator_,condition );
  19372. free_node(*node_allocator_,consequent);
  19373. if (alternative)
  19374. return alternative;
  19375. else
  19376. return node_allocator_->
  19377. allocate_c<details::string_literal_node<Type> >("");
  19378. }
  19379. }
  19380. else if ((0 != consequent) && (0 != alternative))
  19381. return node_allocator_->
  19382. allocate<conditional_string_node_t>(condition,consequent,alternative);
  19383. else
  19384. return error_node();
  19385. //return node_allocator_->
  19386. // allocate<cons_conditional_str_node_t>(condition,consequent);
  19387. }
  19388. #else
  19389. inline expression_node_ptr conditional_string(expression_node_ptr ,
  19390. expression_node_ptr ,
  19391. expression_node_ptr ) const
  19392. {
  19393. return error_node();
  19394. }
  19395. #endif
  19396. inline expression_node_ptr while_loop(expression_node_ptr& condition,
  19397. expression_node_ptr& branch,
  19398. const bool brkcont = false) const
  19399. {
  19400. if (!brkcont && details::is_constant_node(condition))
  19401. {
  19402. expression_node_ptr result = error_node();
  19403. if (details::is_true(condition))
  19404. // Infinite loops are not allowed.
  19405. result = error_node();
  19406. else
  19407. result = node_allocator_->allocate<details::null_node<Type> >();
  19408. free_node(*node_allocator_, condition);
  19409. free_node(*node_allocator_, branch );
  19410. return result;
  19411. }
  19412. else if (details::is_null_node(condition))
  19413. {
  19414. free_node(*node_allocator_,condition);
  19415. return branch;
  19416. }
  19417. else if (!brkcont)
  19418. return node_allocator_->allocate<while_loop_node_t>(condition,branch);
  19419. #ifndef exprtk_disable_break_continue
  19420. else
  19421. return node_allocator_->allocate<while_loop_bc_node_t>(condition,branch);
  19422. #else
  19423. return error_node();
  19424. #endif
  19425. }
  19426. inline expression_node_ptr repeat_until_loop(expression_node_ptr& condition,
  19427. expression_node_ptr& branch,
  19428. const bool brkcont = false) const
  19429. {
  19430. if (!brkcont && details::is_constant_node(condition))
  19431. {
  19432. if (details::is_true(condition) && details::is_constant_node(branch))
  19433. {
  19434. free_node(*node_allocator_,condition);
  19435. return branch;
  19436. }
  19437. free_node(*node_allocator_, condition);
  19438. free_node(*node_allocator_, branch );
  19439. return error_node();
  19440. }
  19441. else if (details::is_null_node(condition))
  19442. {
  19443. free_node(*node_allocator_,condition);
  19444. return branch;
  19445. }
  19446. else if (!brkcont)
  19447. return node_allocator_->allocate<repeat_until_loop_node_t>(condition,branch);
  19448. #ifndef exprtk_disable_break_continue
  19449. else
  19450. return node_allocator_->allocate<repeat_until_loop_bc_node_t>(condition,branch);
  19451. #else
  19452. return error_node();
  19453. #endif
  19454. }
  19455. inline expression_node_ptr for_loop(expression_node_ptr& initialiser,
  19456. expression_node_ptr& condition,
  19457. expression_node_ptr& incrementor,
  19458. expression_node_ptr& loop_body,
  19459. bool brkcont = false) const
  19460. {
  19461. if (!brkcont && details::is_constant_node(condition))
  19462. {
  19463. expression_node_ptr result = error_node();
  19464. if (details::is_true(condition))
  19465. // Infinite loops are not allowed.
  19466. result = error_node();
  19467. else
  19468. result = node_allocator_->allocate<details::null_node<Type> >();
  19469. free_node(*node_allocator_,initialiser);
  19470. free_node(*node_allocator_,condition );
  19471. free_node(*node_allocator_,incrementor);
  19472. free_node(*node_allocator_,loop_body );
  19473. return result;
  19474. }
  19475. else if (details::is_null_node(condition))
  19476. {
  19477. free_node(*node_allocator_,initialiser);
  19478. free_node(*node_allocator_,condition );
  19479. free_node(*node_allocator_,incrementor);
  19480. return loop_body;
  19481. }
  19482. else if (!brkcont)
  19483. return node_allocator_->allocate<for_loop_node_t>(initialiser,
  19484. condition,
  19485. incrementor,
  19486. loop_body);
  19487. #ifndef exprtk_disable_break_continue
  19488. else
  19489. return node_allocator_->allocate<for_loop_bc_node_t>(initialiser,
  19490. condition,
  19491. incrementor,
  19492. loop_body);
  19493. #else
  19494. return error_node();
  19495. #endif
  19496. }
  19497. template <typename Allocator,
  19498. template <typename,typename> class Sequence>
  19499. inline expression_node_ptr const_optimize_switch(Sequence<expression_node_ptr,Allocator>& arg_list)
  19500. {
  19501. expression_node_ptr result = error_node();
  19502. for (std::size_t i = 0; i < (arg_list.size() / 2); ++i)
  19503. {
  19504. expression_node_ptr condition = arg_list[(2 * i) ];
  19505. expression_node_ptr consequent = arg_list[(2 * i) + 1];
  19506. if ((0 == result) && details::is_true(condition))
  19507. {
  19508. result = consequent;
  19509. break;
  19510. }
  19511. }
  19512. if (0 == result)
  19513. {
  19514. result = arg_list.back();
  19515. }
  19516. for (std::size_t i = 0; i < arg_list.size(); ++i)
  19517. {
  19518. expression_node_ptr current_expr = arg_list[i];
  19519. if (current_expr && (current_expr != result))
  19520. {
  19521. free_node(*node_allocator_,current_expr);
  19522. }
  19523. }
  19524. return result;
  19525. }
  19526. template <typename Allocator,
  19527. template <typename,typename> class Sequence>
  19528. inline expression_node_ptr const_optimize_mswitch(Sequence<expression_node_ptr,Allocator>& arg_list)
  19529. {
  19530. expression_node_ptr result = error_node();
  19531. for (std::size_t i = 0; i < (arg_list.size() / 2); ++i)
  19532. {
  19533. expression_node_ptr condition = arg_list[(2 * i) ];
  19534. expression_node_ptr consequent = arg_list[(2 * i) + 1];
  19535. if (details::is_true(condition))
  19536. {
  19537. result = consequent;
  19538. }
  19539. }
  19540. if (0 == result)
  19541. {
  19542. T zero = T(0);
  19543. result = node_allocator_->allocate<literal_node_t>(zero);
  19544. }
  19545. for (std::size_t i = 0; i < arg_list.size(); ++i)
  19546. {
  19547. expression_node_ptr& current_expr = arg_list[i];
  19548. if (current_expr && (current_expr != result))
  19549. {
  19550. free_node(*node_allocator_,current_expr);
  19551. }
  19552. }
  19553. return result;
  19554. }
  19555. struct switch_nodes
  19556. {
  19557. typedef std::vector<expression_node_ptr> arg_list_t;
  19558. #define case_stmt(N) \
  19559. if (is_true(arg[(2 * N)])) return arg[(2 * N) + 1]->value();
  19560. struct switch_1
  19561. {
  19562. static inline T process(const arg_list_t& arg)
  19563. {
  19564. case_stmt(0)
  19565. return arg.back()->value();
  19566. }
  19567. };
  19568. struct switch_2
  19569. {
  19570. static inline T process(const arg_list_t& arg)
  19571. {
  19572. case_stmt(0) case_stmt(1)
  19573. return arg.back()->value();
  19574. }
  19575. };
  19576. struct switch_3
  19577. {
  19578. static inline T process(const arg_list_t& arg)
  19579. {
  19580. case_stmt(0) case_stmt(1)
  19581. case_stmt(2)
  19582. return arg.back()->value();
  19583. }
  19584. };
  19585. struct switch_4
  19586. {
  19587. static inline T process(const arg_list_t& arg)
  19588. {
  19589. case_stmt(0) case_stmt(1)
  19590. case_stmt(2) case_stmt(3)
  19591. return arg.back()->value();
  19592. }
  19593. };
  19594. struct switch_5
  19595. {
  19596. static inline T process(const arg_list_t& arg)
  19597. {
  19598. case_stmt(0) case_stmt(1)
  19599. case_stmt(2) case_stmt(3)
  19600. case_stmt(4)
  19601. return arg.back()->value();
  19602. }
  19603. };
  19604. struct switch_6
  19605. {
  19606. static inline T process(const arg_list_t& arg)
  19607. {
  19608. case_stmt(0) case_stmt(1)
  19609. case_stmt(2) case_stmt(3)
  19610. case_stmt(4) case_stmt(5)
  19611. return arg.back()->value();
  19612. }
  19613. };
  19614. struct switch_7
  19615. {
  19616. static inline T process(const arg_list_t& arg)
  19617. {
  19618. case_stmt(0) case_stmt(1)
  19619. case_stmt(2) case_stmt(3)
  19620. case_stmt(4) case_stmt(5)
  19621. case_stmt(6)
  19622. return arg.back()->value();
  19623. }
  19624. };
  19625. #undef case_stmt
  19626. };
  19627. template <typename Allocator,
  19628. template <typename,typename> class Sequence>
  19629. inline expression_node_ptr switch_statement(Sequence<expression_node_ptr,Allocator>& arg_list)
  19630. {
  19631. if (arg_list.empty())
  19632. return error_node();
  19633. else if (
  19634. !all_nodes_valid(arg_list) ||
  19635. (arg_list.size() < 3) ||
  19636. ((arg_list.size() % 2) != 1)
  19637. )
  19638. {
  19639. details::free_all_nodes(*node_allocator_,arg_list);
  19640. return error_node();
  19641. }
  19642. else if (is_constant_foldable(arg_list))
  19643. return const_optimize_switch(arg_list);
  19644. switch ((arg_list.size() - 1) / 2)
  19645. {
  19646. #define case_stmt(N) \
  19647. case N : \
  19648. return node_allocator_-> \
  19649. allocate<details::switch_n_node \
  19650. <Type,typename switch_nodes::switch_##N> >(arg_list); \
  19651. case_stmt(1)
  19652. case_stmt(2)
  19653. case_stmt(3)
  19654. case_stmt(4)
  19655. case_stmt(5)
  19656. case_stmt(6)
  19657. case_stmt(7)
  19658. #undef case_stmt
  19659. default : return node_allocator_->allocate<details::switch_node<Type> >(arg_list);
  19660. }
  19661. }
  19662. template <typename Allocator,
  19663. template <typename,typename> class Sequence>
  19664. inline expression_node_ptr multi_switch_statement(Sequence<expression_node_ptr,Allocator>& arg_list)
  19665. {
  19666. if (!all_nodes_valid(arg_list))
  19667. {
  19668. details::free_all_nodes(*node_allocator_,arg_list);
  19669. return error_node();
  19670. }
  19671. else if (is_constant_foldable(arg_list))
  19672. return const_optimize_mswitch(arg_list);
  19673. else
  19674. return node_allocator_->allocate<details::multi_switch_node<Type> >(arg_list);
  19675. }
  19676. #define unary_opr_switch_statements \
  19677. case_stmt(details:: e_abs,details:: abs_op) \
  19678. case_stmt(details:: e_acos,details:: acos_op) \
  19679. case_stmt(details::e_acosh,details::acosh_op) \
  19680. case_stmt(details:: e_asin,details:: asin_op) \
  19681. case_stmt(details::e_asinh,details::asinh_op) \
  19682. case_stmt(details:: e_atan,details:: atan_op) \
  19683. case_stmt(details::e_atanh,details::atanh_op) \
  19684. case_stmt(details:: e_ceil,details:: ceil_op) \
  19685. case_stmt(details:: e_cos,details:: cos_op) \
  19686. case_stmt(details:: e_cosh,details:: cosh_op) \
  19687. case_stmt(details:: e_exp,details:: exp_op) \
  19688. case_stmt(details::e_expm1,details::expm1_op) \
  19689. case_stmt(details::e_floor,details::floor_op) \
  19690. case_stmt(details:: e_log,details:: log_op) \
  19691. case_stmt(details::e_log10,details::log10_op) \
  19692. case_stmt(details:: e_log2,details:: log2_op) \
  19693. case_stmt(details::e_log1p,details::log1p_op) \
  19694. case_stmt(details:: e_neg,details:: neg_op) \
  19695. case_stmt(details:: e_pos,details:: pos_op) \
  19696. case_stmt(details::e_round,details::round_op) \
  19697. case_stmt(details:: e_sin,details:: sin_op) \
  19698. case_stmt(details:: e_sinc,details:: sinc_op) \
  19699. case_stmt(details:: e_sinh,details:: sinh_op) \
  19700. case_stmt(details:: e_sqrt,details:: sqrt_op) \
  19701. case_stmt(details:: e_tan,details:: tan_op) \
  19702. case_stmt(details:: e_tanh,details:: tanh_op) \
  19703. case_stmt(details:: e_cot,details:: cot_op) \
  19704. case_stmt(details:: e_sec,details:: sec_op) \
  19705. case_stmt(details:: e_csc,details:: csc_op) \
  19706. case_stmt(details:: e_r2d,details:: r2d_op) \
  19707. case_stmt(details:: e_d2r,details:: d2r_op) \
  19708. case_stmt(details:: e_d2g,details:: d2g_op) \
  19709. case_stmt(details:: e_g2d,details:: g2d_op) \
  19710. case_stmt(details:: e_notl,details:: notl_op) \
  19711. case_stmt(details:: e_sgn,details:: sgn_op) \
  19712. case_stmt(details:: e_erf,details:: erf_op) \
  19713. case_stmt(details:: e_erfc,details:: erfc_op) \
  19714. case_stmt(details:: e_ncdf,details:: ncdf_op) \
  19715. case_stmt(details:: e_frac,details:: frac_op) \
  19716. case_stmt(details::e_trunc,details::trunc_op) \
  19717. inline expression_node_ptr synthesize_uv_expression(const details::operator_type& operation,
  19718. expression_node_ptr (&branch)[1])
  19719. {
  19720. T& v = static_cast<details::variable_node<T>*>(branch[0])->ref();
  19721. switch (operation)
  19722. {
  19723. #define case_stmt(op0,op1) \
  19724. case op0 : return node_allocator_-> \
  19725. allocate<typename details::unary_variable_node<Type,op1<Type> > >(v); \
  19726. unary_opr_switch_statements
  19727. #undef case_stmt
  19728. default : return error_node();
  19729. }
  19730. }
  19731. inline expression_node_ptr synthesize_uvec_expression(const details::operator_type& operation,
  19732. expression_node_ptr (&branch)[1])
  19733. {
  19734. switch (operation)
  19735. {
  19736. #define case_stmt(op0,op1) \
  19737. case op0 : return node_allocator_-> \
  19738. allocate<typename details::unary_vector_node<Type,op1<Type> > > \
  19739. (operation,branch[0]); \
  19740. unary_opr_switch_statements
  19741. #undef case_stmt
  19742. default : return error_node();
  19743. }
  19744. }
  19745. inline expression_node_ptr synthesize_unary_expression(const details::operator_type& operation,
  19746. expression_node_ptr (&branch)[1])
  19747. {
  19748. switch (operation)
  19749. {
  19750. #define case_stmt(op0,op1) \
  19751. case op0 : return node_allocator_-> \
  19752. allocate<typename details::unary_branch_node<Type,op1<Type> > >(branch[0]); \
  19753. unary_opr_switch_statements
  19754. #undef case_stmt
  19755. default : return error_node();
  19756. }
  19757. }
  19758. inline expression_node_ptr const_optimize_sf3(const details::operator_type& operation,
  19759. expression_node_ptr (&branch)[3])
  19760. {
  19761. expression_node_ptr temp_node = error_node();
  19762. switch (operation)
  19763. {
  19764. #define case_stmt(op0,op1) \
  19765. case op0 : temp_node = node_allocator_-> \
  19766. allocate<details::sf3_node<Type,op1<Type> > > \
  19767. (operation,branch); \
  19768. break; \
  19769. case_stmt(details::e_sf00,details::sf00_op) case_stmt(details::e_sf01,details::sf01_op)
  19770. case_stmt(details::e_sf02,details::sf02_op) case_stmt(details::e_sf03,details::sf03_op)
  19771. case_stmt(details::e_sf04,details::sf04_op) case_stmt(details::e_sf05,details::sf05_op)
  19772. case_stmt(details::e_sf06,details::sf06_op) case_stmt(details::e_sf07,details::sf07_op)
  19773. case_stmt(details::e_sf08,details::sf08_op) case_stmt(details::e_sf09,details::sf09_op)
  19774. case_stmt(details::e_sf10,details::sf10_op) case_stmt(details::e_sf11,details::sf11_op)
  19775. case_stmt(details::e_sf12,details::sf12_op) case_stmt(details::e_sf13,details::sf13_op)
  19776. case_stmt(details::e_sf14,details::sf14_op) case_stmt(details::e_sf15,details::sf15_op)
  19777. case_stmt(details::e_sf16,details::sf16_op) case_stmt(details::e_sf17,details::sf17_op)
  19778. case_stmt(details::e_sf18,details::sf18_op) case_stmt(details::e_sf19,details::sf19_op)
  19779. case_stmt(details::e_sf20,details::sf20_op) case_stmt(details::e_sf21,details::sf21_op)
  19780. case_stmt(details::e_sf22,details::sf22_op) case_stmt(details::e_sf23,details::sf23_op)
  19781. case_stmt(details::e_sf24,details::sf24_op) case_stmt(details::e_sf25,details::sf25_op)
  19782. case_stmt(details::e_sf26,details::sf26_op) case_stmt(details::e_sf27,details::sf27_op)
  19783. case_stmt(details::e_sf28,details::sf28_op) case_stmt(details::e_sf29,details::sf29_op)
  19784. case_stmt(details::e_sf30,details::sf30_op) case_stmt(details::e_sf31,details::sf31_op)
  19785. case_stmt(details::e_sf32,details::sf32_op) case_stmt(details::e_sf33,details::sf33_op)
  19786. case_stmt(details::e_sf34,details::sf34_op) case_stmt(details::e_sf35,details::sf35_op)
  19787. case_stmt(details::e_sf36,details::sf36_op) case_stmt(details::e_sf37,details::sf37_op)
  19788. case_stmt(details::e_sf38,details::sf38_op) case_stmt(details::e_sf39,details::sf39_op)
  19789. case_stmt(details::e_sf40,details::sf40_op) case_stmt(details::e_sf41,details::sf41_op)
  19790. case_stmt(details::e_sf42,details::sf42_op) case_stmt(details::e_sf43,details::sf43_op)
  19791. case_stmt(details::e_sf44,details::sf44_op) case_stmt(details::e_sf45,details::sf45_op)
  19792. case_stmt(details::e_sf46,details::sf46_op) case_stmt(details::e_sf47,details::sf47_op)
  19793. #undef case_stmt
  19794. default : return error_node();
  19795. }
  19796. T v = temp_node->value();
  19797. node_allocator_->free(temp_node);
  19798. details::free_node(*node_allocator_,temp_node);
  19799. return node_allocator_->allocate<literal_node_t>(v);
  19800. }
  19801. inline expression_node_ptr varnode_optimize_sf3(const details::operator_type& operation, expression_node_ptr (&branch)[3])
  19802. {
  19803. typedef details::variable_node<Type>* variable_ptr;
  19804. const Type& v0 = static_cast<variable_ptr>(branch[0])->ref();
  19805. const Type& v1 = static_cast<variable_ptr>(branch[1])->ref();
  19806. const Type& v2 = static_cast<variable_ptr>(branch[2])->ref();
  19807. switch (operation)
  19808. {
  19809. #define case_stmt(op0,op1) \
  19810. case op0 : return node_allocator_-> \
  19811. allocate_rrr<details::sf3_var_node<Type,op1<Type> > > \
  19812. (v0,v1,v2); \
  19813. case_stmt(details::e_sf00,details::sf00_op) case_stmt(details::e_sf01,details::sf01_op)
  19814. case_stmt(details::e_sf02,details::sf02_op) case_stmt(details::e_sf03,details::sf03_op)
  19815. case_stmt(details::e_sf04,details::sf04_op) case_stmt(details::e_sf05,details::sf05_op)
  19816. case_stmt(details::e_sf06,details::sf06_op) case_stmt(details::e_sf07,details::sf07_op)
  19817. case_stmt(details::e_sf08,details::sf08_op) case_stmt(details::e_sf09,details::sf09_op)
  19818. case_stmt(details::e_sf10,details::sf10_op) case_stmt(details::e_sf11,details::sf11_op)
  19819. case_stmt(details::e_sf12,details::sf12_op) case_stmt(details::e_sf13,details::sf13_op)
  19820. case_stmt(details::e_sf14,details::sf14_op) case_stmt(details::e_sf15,details::sf15_op)
  19821. case_stmt(details::e_sf16,details::sf16_op) case_stmt(details::e_sf17,details::sf17_op)
  19822. case_stmt(details::e_sf18,details::sf18_op) case_stmt(details::e_sf19,details::sf19_op)
  19823. case_stmt(details::e_sf20,details::sf20_op) case_stmt(details::e_sf21,details::sf21_op)
  19824. case_stmt(details::e_sf22,details::sf22_op) case_stmt(details::e_sf23,details::sf23_op)
  19825. case_stmt(details::e_sf24,details::sf24_op) case_stmt(details::e_sf25,details::sf25_op)
  19826. case_stmt(details::e_sf26,details::sf26_op) case_stmt(details::e_sf27,details::sf27_op)
  19827. case_stmt(details::e_sf28,details::sf28_op) case_stmt(details::e_sf29,details::sf29_op)
  19828. case_stmt(details::e_sf30,details::sf30_op) case_stmt(details::e_sf31,details::sf31_op)
  19829. case_stmt(details::e_sf32,details::sf32_op) case_stmt(details::e_sf33,details::sf33_op)
  19830. case_stmt(details::e_sf34,details::sf34_op) case_stmt(details::e_sf35,details::sf35_op)
  19831. case_stmt(details::e_sf36,details::sf36_op) case_stmt(details::e_sf37,details::sf37_op)
  19832. case_stmt(details::e_sf38,details::sf38_op) case_stmt(details::e_sf39,details::sf39_op)
  19833. case_stmt(details::e_sf40,details::sf40_op) case_stmt(details::e_sf41,details::sf41_op)
  19834. case_stmt(details::e_sf42,details::sf42_op) case_stmt(details::e_sf43,details::sf43_op)
  19835. case_stmt(details::e_sf44,details::sf44_op) case_stmt(details::e_sf45,details::sf45_op)
  19836. case_stmt(details::e_sf46,details::sf46_op) case_stmt(details::e_sf47,details::sf47_op)
  19837. #undef case_stmt
  19838. default : return error_node();
  19839. }
  19840. }
  19841. inline expression_node_ptr special_function(const details::operator_type& operation, expression_node_ptr (&branch)[3])
  19842. {
  19843. if (!all_nodes_valid(branch))
  19844. return error_node();
  19845. else if (is_constant_foldable(branch))
  19846. return const_optimize_sf3(operation,branch);
  19847. else if (all_nodes_variables(branch))
  19848. return varnode_optimize_sf3(operation,branch);
  19849. else
  19850. {
  19851. switch (operation)
  19852. {
  19853. #define case_stmt(op0,op1) \
  19854. case op0 : return node_allocator_-> \
  19855. allocate<details::sf3_node<Type,op1<Type> > >(operation,branch); \
  19856. case_stmt(details::e_sf00,details::sf00_op) case_stmt(details::e_sf01,details::sf01_op)
  19857. case_stmt(details::e_sf02,details::sf02_op) case_stmt(details::e_sf03,details::sf03_op)
  19858. case_stmt(details::e_sf04,details::sf04_op) case_stmt(details::e_sf05,details::sf05_op)
  19859. case_stmt(details::e_sf06,details::sf06_op) case_stmt(details::e_sf07,details::sf07_op)
  19860. case_stmt(details::e_sf08,details::sf08_op) case_stmt(details::e_sf09,details::sf09_op)
  19861. case_stmt(details::e_sf10,details::sf10_op) case_stmt(details::e_sf11,details::sf11_op)
  19862. case_stmt(details::e_sf12,details::sf12_op) case_stmt(details::e_sf13,details::sf13_op)
  19863. case_stmt(details::e_sf14,details::sf14_op) case_stmt(details::e_sf15,details::sf15_op)
  19864. case_stmt(details::e_sf16,details::sf16_op) case_stmt(details::e_sf17,details::sf17_op)
  19865. case_stmt(details::e_sf18,details::sf18_op) case_stmt(details::e_sf19,details::sf19_op)
  19866. case_stmt(details::e_sf20,details::sf20_op) case_stmt(details::e_sf21,details::sf21_op)
  19867. case_stmt(details::e_sf22,details::sf22_op) case_stmt(details::e_sf23,details::sf23_op)
  19868. case_stmt(details::e_sf24,details::sf24_op) case_stmt(details::e_sf25,details::sf25_op)
  19869. case_stmt(details::e_sf26,details::sf26_op) case_stmt(details::e_sf27,details::sf27_op)
  19870. case_stmt(details::e_sf28,details::sf28_op) case_stmt(details::e_sf29,details::sf29_op)
  19871. case_stmt(details::e_sf30,details::sf30_op) case_stmt(details::e_sf31,details::sf31_op)
  19872. case_stmt(details::e_sf32,details::sf32_op) case_stmt(details::e_sf33,details::sf33_op)
  19873. case_stmt(details::e_sf34,details::sf34_op) case_stmt(details::e_sf35,details::sf35_op)
  19874. case_stmt(details::e_sf36,details::sf36_op) case_stmt(details::e_sf37,details::sf37_op)
  19875. case_stmt(details::e_sf38,details::sf38_op) case_stmt(details::e_sf39,details::sf39_op)
  19876. case_stmt(details::e_sf40,details::sf40_op) case_stmt(details::e_sf41,details::sf41_op)
  19877. case_stmt(details::e_sf42,details::sf42_op) case_stmt(details::e_sf43,details::sf43_op)
  19878. case_stmt(details::e_sf44,details::sf44_op) case_stmt(details::e_sf45,details::sf45_op)
  19879. case_stmt(details::e_sf46,details::sf46_op) case_stmt(details::e_sf47,details::sf47_op)
  19880. #undef case_stmt
  19881. default : return error_node();
  19882. }
  19883. }
  19884. }
  19885. inline expression_node_ptr const_optimize_sf4(const details::operator_type& operation, expression_node_ptr (&branch)[4])
  19886. {
  19887. expression_node_ptr temp_node = error_node();
  19888. switch (operation)
  19889. {
  19890. #define case_stmt(op0,op1) \
  19891. case op0 : temp_node = node_allocator_-> \
  19892. allocate<details::sf4_node<Type,op1<Type> > >(operation,branch); \
  19893. break; \
  19894. case_stmt(details::e_sf48,details::sf48_op) case_stmt(details::e_sf49,details::sf49_op)
  19895. case_stmt(details::e_sf50,details::sf50_op) case_stmt(details::e_sf51,details::sf51_op)
  19896. case_stmt(details::e_sf52,details::sf52_op) case_stmt(details::e_sf53,details::sf53_op)
  19897. case_stmt(details::e_sf54,details::sf54_op) case_stmt(details::e_sf55,details::sf55_op)
  19898. case_stmt(details::e_sf56,details::sf56_op) case_stmt(details::e_sf57,details::sf57_op)
  19899. case_stmt(details::e_sf58,details::sf58_op) case_stmt(details::e_sf59,details::sf59_op)
  19900. case_stmt(details::e_sf60,details::sf60_op) case_stmt(details::e_sf61,details::sf61_op)
  19901. case_stmt(details::e_sf62,details::sf62_op) case_stmt(details::e_sf63,details::sf63_op)
  19902. case_stmt(details::e_sf64,details::sf64_op) case_stmt(details::e_sf65,details::sf65_op)
  19903. case_stmt(details::e_sf66,details::sf66_op) case_stmt(details::e_sf67,details::sf67_op)
  19904. case_stmt(details::e_sf68,details::sf68_op) case_stmt(details::e_sf69,details::sf69_op)
  19905. case_stmt(details::e_sf70,details::sf70_op) case_stmt(details::e_sf71,details::sf71_op)
  19906. case_stmt(details::e_sf72,details::sf72_op) case_stmt(details::e_sf73,details::sf73_op)
  19907. case_stmt(details::e_sf74,details::sf74_op) case_stmt(details::e_sf75,details::sf75_op)
  19908. case_stmt(details::e_sf76,details::sf76_op) case_stmt(details::e_sf77,details::sf77_op)
  19909. case_stmt(details::e_sf78,details::sf78_op) case_stmt(details::e_sf79,details::sf79_op)
  19910. case_stmt(details::e_sf80,details::sf80_op) case_stmt(details::e_sf81,details::sf81_op)
  19911. case_stmt(details::e_sf82,details::sf82_op) case_stmt(details::e_sf83,details::sf83_op)
  19912. case_stmt(details::e_sf84,details::sf84_op) case_stmt(details::e_sf85,details::sf85_op)
  19913. case_stmt(details::e_sf86,details::sf86_op) case_stmt(details::e_sf87,details::sf87_op)
  19914. case_stmt(details::e_sf88,details::sf88_op) case_stmt(details::e_sf89,details::sf89_op)
  19915. case_stmt(details::e_sf90,details::sf90_op) case_stmt(details::e_sf91,details::sf91_op)
  19916. case_stmt(details::e_sf92,details::sf92_op) case_stmt(details::e_sf93,details::sf93_op)
  19917. case_stmt(details::e_sf94,details::sf94_op) case_stmt(details::e_sf95,details::sf95_op)
  19918. case_stmt(details::e_sf96,details::sf96_op) case_stmt(details::e_sf97,details::sf97_op)
  19919. case_stmt(details::e_sf98,details::sf98_op) case_stmt(details::e_sf99,details::sf99_op)
  19920. #undef case_stmt
  19921. default : return error_node();
  19922. }
  19923. T v = temp_node->value();
  19924. details::free_node(*node_allocator_,temp_node);
  19925. return node_allocator_->allocate<literal_node_t>(v);
  19926. }
  19927. inline expression_node_ptr varnode_optimize_sf4(const details::operator_type& operation, expression_node_ptr (&branch)[4])
  19928. {
  19929. typedef details::variable_node<Type>* variable_ptr;
  19930. const Type& v0 = static_cast<variable_ptr>(branch[0])->ref();
  19931. const Type& v1 = static_cast<variable_ptr>(branch[1])->ref();
  19932. const Type& v2 = static_cast<variable_ptr>(branch[2])->ref();
  19933. const Type& v3 = static_cast<variable_ptr>(branch[3])->ref();
  19934. switch (operation)
  19935. {
  19936. #define case_stmt(op0,op1) \
  19937. case op0 : return node_allocator_-> \
  19938. allocate_rrrr<details::sf4_var_node<Type,op1<Type> > >(v0,v1,v2,v3); \
  19939. case_stmt(details::e_sf48,details::sf48_op) case_stmt(details::e_sf49,details::sf49_op)
  19940. case_stmt(details::e_sf50,details::sf50_op) case_stmt(details::e_sf51,details::sf51_op)
  19941. case_stmt(details::e_sf52,details::sf52_op) case_stmt(details::e_sf53,details::sf53_op)
  19942. case_stmt(details::e_sf54,details::sf54_op) case_stmt(details::e_sf55,details::sf55_op)
  19943. case_stmt(details::e_sf56,details::sf56_op) case_stmt(details::e_sf57,details::sf57_op)
  19944. case_stmt(details::e_sf58,details::sf58_op) case_stmt(details::e_sf59,details::sf59_op)
  19945. case_stmt(details::e_sf60,details::sf60_op) case_stmt(details::e_sf61,details::sf61_op)
  19946. case_stmt(details::e_sf62,details::sf62_op) case_stmt(details::e_sf63,details::sf63_op)
  19947. case_stmt(details::e_sf64,details::sf64_op) case_stmt(details::e_sf65,details::sf65_op)
  19948. case_stmt(details::e_sf66,details::sf66_op) case_stmt(details::e_sf67,details::sf67_op)
  19949. case_stmt(details::e_sf68,details::sf68_op) case_stmt(details::e_sf69,details::sf69_op)
  19950. case_stmt(details::e_sf70,details::sf70_op) case_stmt(details::e_sf71,details::sf71_op)
  19951. case_stmt(details::e_sf72,details::sf72_op) case_stmt(details::e_sf73,details::sf73_op)
  19952. case_stmt(details::e_sf74,details::sf74_op) case_stmt(details::e_sf75,details::sf75_op)
  19953. case_stmt(details::e_sf76,details::sf76_op) case_stmt(details::e_sf77,details::sf77_op)
  19954. case_stmt(details::e_sf78,details::sf78_op) case_stmt(details::e_sf79,details::sf79_op)
  19955. case_stmt(details::e_sf80,details::sf80_op) case_stmt(details::e_sf81,details::sf81_op)
  19956. case_stmt(details::e_sf82,details::sf82_op) case_stmt(details::e_sf83,details::sf83_op)
  19957. case_stmt(details::e_sf84,details::sf84_op) case_stmt(details::e_sf85,details::sf85_op)
  19958. case_stmt(details::e_sf86,details::sf86_op) case_stmt(details::e_sf87,details::sf87_op)
  19959. case_stmt(details::e_sf88,details::sf88_op) case_stmt(details::e_sf89,details::sf89_op)
  19960. case_stmt(details::e_sf90,details::sf90_op) case_stmt(details::e_sf91,details::sf91_op)
  19961. case_stmt(details::e_sf92,details::sf92_op) case_stmt(details::e_sf93,details::sf93_op)
  19962. case_stmt(details::e_sf94,details::sf94_op) case_stmt(details::e_sf95,details::sf95_op)
  19963. case_stmt(details::e_sf96,details::sf96_op) case_stmt(details::e_sf97,details::sf97_op)
  19964. case_stmt(details::e_sf98,details::sf98_op) case_stmt(details::e_sf99,details::sf99_op)
  19965. #undef case_stmt
  19966. default : return error_node();
  19967. }
  19968. }
  19969. inline expression_node_ptr special_function(const details::operator_type& operation, expression_node_ptr (&branch)[4])
  19970. {
  19971. if (!all_nodes_valid(branch))
  19972. return error_node();
  19973. else if (is_constant_foldable(branch))
  19974. return const_optimize_sf4(operation,branch);
  19975. else if (all_nodes_variables(branch))
  19976. return varnode_optimize_sf4(operation,branch);
  19977. switch (operation)
  19978. {
  19979. #define case_stmt(op0,op1) \
  19980. case op0 : return node_allocator_-> \
  19981. allocate<details::sf4_node<Type,op1<Type> > >(operation,branch); \
  19982. case_stmt(details::e_sf48,details::sf48_op) case_stmt(details::e_sf49,details::sf49_op)
  19983. case_stmt(details::e_sf50,details::sf50_op) case_stmt(details::e_sf51,details::sf51_op)
  19984. case_stmt(details::e_sf52,details::sf52_op) case_stmt(details::e_sf53,details::sf53_op)
  19985. case_stmt(details::e_sf54,details::sf54_op) case_stmt(details::e_sf55,details::sf55_op)
  19986. case_stmt(details::e_sf56,details::sf56_op) case_stmt(details::e_sf57,details::sf57_op)
  19987. case_stmt(details::e_sf58,details::sf58_op) case_stmt(details::e_sf59,details::sf59_op)
  19988. case_stmt(details::e_sf60,details::sf60_op) case_stmt(details::e_sf61,details::sf61_op)
  19989. case_stmt(details::e_sf62,details::sf62_op) case_stmt(details::e_sf63,details::sf63_op)
  19990. case_stmt(details::e_sf64,details::sf64_op) case_stmt(details::e_sf65,details::sf65_op)
  19991. case_stmt(details::e_sf66,details::sf66_op) case_stmt(details::e_sf67,details::sf67_op)
  19992. case_stmt(details::e_sf68,details::sf68_op) case_stmt(details::e_sf69,details::sf69_op)
  19993. case_stmt(details::e_sf70,details::sf70_op) case_stmt(details::e_sf71,details::sf71_op)
  19994. case_stmt(details::e_sf72,details::sf72_op) case_stmt(details::e_sf73,details::sf73_op)
  19995. case_stmt(details::e_sf74,details::sf74_op) case_stmt(details::e_sf75,details::sf75_op)
  19996. case_stmt(details::e_sf76,details::sf76_op) case_stmt(details::e_sf77,details::sf77_op)
  19997. case_stmt(details::e_sf78,details::sf78_op) case_stmt(details::e_sf79,details::sf79_op)
  19998. case_stmt(details::e_sf80,details::sf80_op) case_stmt(details::e_sf81,details::sf81_op)
  19999. case_stmt(details::e_sf82,details::sf82_op) case_stmt(details::e_sf83,details::sf83_op)
  20000. case_stmt(details::e_sf84,details::sf84_op) case_stmt(details::e_sf85,details::sf85_op)
  20001. case_stmt(details::e_sf86,details::sf86_op) case_stmt(details::e_sf87,details::sf87_op)
  20002. case_stmt(details::e_sf88,details::sf88_op) case_stmt(details::e_sf89,details::sf89_op)
  20003. case_stmt(details::e_sf90,details::sf90_op) case_stmt(details::e_sf91,details::sf91_op)
  20004. case_stmt(details::e_sf92,details::sf92_op) case_stmt(details::e_sf93,details::sf93_op)
  20005. case_stmt(details::e_sf94,details::sf94_op) case_stmt(details::e_sf95,details::sf95_op)
  20006. case_stmt(details::e_sf96,details::sf96_op) case_stmt(details::e_sf97,details::sf97_op)
  20007. case_stmt(details::e_sf98,details::sf98_op) case_stmt(details::e_sf99,details::sf99_op)
  20008. #undef case_stmt
  20009. default : return error_node();
  20010. }
  20011. }
  20012. template <typename Allocator,
  20013. template <typename,typename> class Sequence>
  20014. inline expression_node_ptr const_optimize_varargfunc(const details::operator_type& operation, Sequence<expression_node_ptr,Allocator>& arg_list)
  20015. {
  20016. expression_node_ptr temp_node = error_node();
  20017. switch (operation)
  20018. {
  20019. #define case_stmt(op0,op1) \
  20020. case op0 : temp_node = node_allocator_-> \
  20021. allocate<details::vararg_node<Type,op1<Type> > > \
  20022. (arg_list); \
  20023. break; \
  20024. case_stmt(details::e_sum, details::vararg_add_op )
  20025. case_stmt(details::e_prod, details::vararg_mul_op )
  20026. case_stmt(details::e_avg, details::vararg_avg_op )
  20027. case_stmt(details::e_min, details::vararg_min_op )
  20028. case_stmt(details::e_max, details::vararg_max_op )
  20029. case_stmt(details::e_mand, details::vararg_mand_op )
  20030. case_stmt(details::e_mor, details::vararg_mor_op )
  20031. case_stmt(details::e_multi,details::vararg_multi_op)
  20032. #undef case_stmt
  20033. default : return error_node();
  20034. }
  20035. T v = temp_node->value();
  20036. details::free_node(*node_allocator_,temp_node);
  20037. return node_allocator_->allocate<literal_node_t>(v);
  20038. }
  20039. inline bool special_one_parameter_vararg(const details::operator_type& operation)
  20040. {
  20041. return (
  20042. (details::e_sum == operation) ||
  20043. (details::e_prod == operation) ||
  20044. (details::e_avg == operation) ||
  20045. (details::e_min == operation) ||
  20046. (details::e_max == operation)
  20047. );
  20048. }
  20049. template <typename Allocator,
  20050. template <typename,typename> class Sequence>
  20051. inline expression_node_ptr varnode_optimize_varargfunc(const details::operator_type& operation, Sequence<expression_node_ptr,Allocator>& arg_list)
  20052. {
  20053. switch (operation)
  20054. {
  20055. #define case_stmt(op0,op1) \
  20056. case op0 : return node_allocator_-> \
  20057. allocate<details::vararg_varnode<Type,op1<Type> > >(arg_list); \
  20058. case_stmt(details::e_sum, details::vararg_add_op )
  20059. case_stmt(details::e_prod, details::vararg_mul_op )
  20060. case_stmt(details::e_avg, details::vararg_avg_op )
  20061. case_stmt(details::e_min, details::vararg_min_op )
  20062. case_stmt(details::e_max, details::vararg_max_op )
  20063. case_stmt(details::e_mand, details::vararg_mand_op )
  20064. case_stmt(details::e_mor, details::vararg_mor_op )
  20065. case_stmt(details::e_multi,details::vararg_multi_op)
  20066. #undef case_stmt
  20067. default : return error_node();
  20068. }
  20069. }
  20070. template <typename Allocator,
  20071. template <typename,typename> class Sequence>
  20072. inline expression_node_ptr vectorize_func(const details::operator_type& operation, Sequence<expression_node_ptr,Allocator>& arg_list)
  20073. {
  20074. if (1 == arg_list.size())
  20075. {
  20076. switch (operation)
  20077. {
  20078. #define case_stmt(op0,op1) \
  20079. case op0 : return node_allocator_-> \
  20080. allocate<details::vectorize_node<Type,op1<Type> > >(arg_list[0]); \
  20081. case_stmt(details::e_sum, details::vec_add_op)
  20082. case_stmt(details::e_prod, details::vec_mul_op)
  20083. case_stmt(details::e_avg, details::vec_avg_op)
  20084. case_stmt(details::e_min, details::vec_min_op)
  20085. case_stmt(details::e_max, details::vec_max_op)
  20086. #undef case_stmt
  20087. default : return error_node();
  20088. }
  20089. }
  20090. else
  20091. return error_node();
  20092. }
  20093. template <typename Allocator,
  20094. template <typename,typename> class Sequence>
  20095. inline expression_node_ptr vararg_function(const details::operator_type& operation, Sequence<expression_node_ptr,Allocator>& arg_list)
  20096. {
  20097. if (!all_nodes_valid(arg_list))
  20098. {
  20099. details::free_all_nodes(*node_allocator_,arg_list);
  20100. return error_node();
  20101. }
  20102. else if (is_constant_foldable(arg_list))
  20103. return const_optimize_varargfunc(operation,arg_list);
  20104. else if ((arg_list.size() == 1) && details::is_ivector_node(arg_list[0]))
  20105. return vectorize_func(operation,arg_list);
  20106. else if ((arg_list.size() == 1) && special_one_parameter_vararg(operation))
  20107. return arg_list[0];
  20108. else if (all_nodes_variables(arg_list))
  20109. return varnode_optimize_varargfunc(operation,arg_list);
  20110. switch (operation)
  20111. {
  20112. #define case_stmt(op0,op1) \
  20113. case op0 : return node_allocator_-> \
  20114. allocate<details::vararg_node<Type,op1<Type> > >(arg_list); \
  20115. case_stmt(details::e_sum, details::vararg_add_op )
  20116. case_stmt(details::e_prod, details::vararg_mul_op )
  20117. case_stmt(details::e_avg, details::vararg_avg_op )
  20118. case_stmt(details::e_min, details::vararg_min_op )
  20119. case_stmt(details::e_max, details::vararg_max_op )
  20120. case_stmt(details::e_mand, details::vararg_mand_op )
  20121. case_stmt(details::e_mor, details::vararg_mor_op )
  20122. case_stmt(details::e_multi,details::vararg_multi_op)
  20123. #undef case_stmt
  20124. default : return error_node();
  20125. }
  20126. }
  20127. template <std::size_t N>
  20128. inline expression_node_ptr function(ifunction_t* f, expression_node_ptr (&b)[N])
  20129. {
  20130. typedef typename details::function_N_node<T,ifunction_t,N> function_N_node_t;
  20131. expression_node_ptr result = synthesize_expression<function_N_node_t,N>(f,b);
  20132. if (0 == result)
  20133. return error_node();
  20134. else
  20135. {
  20136. // Can the function call be completely optimized?
  20137. if (details::is_constant_node(result))
  20138. return result;
  20139. else if (!all_nodes_valid(b))
  20140. return error_node();
  20141. else if (N != f->param_count)
  20142. {
  20143. details::free_all_nodes(*node_allocator_,b);
  20144. return error_node();
  20145. }
  20146. function_N_node_t* func_node_ptr = static_cast<function_N_node_t*>(result);
  20147. if (func_node_ptr->init_branches(b))
  20148. return result;
  20149. else
  20150. {
  20151. details::free_all_nodes(*node_allocator_,b);
  20152. return error_node();
  20153. }
  20154. }
  20155. }
  20156. inline expression_node_ptr function(ifunction_t* f)
  20157. {
  20158. typedef typename details::function_N_node<Type,ifunction_t,0> function_N_node_t;
  20159. return node_allocator_->allocate<function_N_node_t>(f);
  20160. }
  20161. inline expression_node_ptr vararg_function_call(ivararg_function_t* vaf,
  20162. std::vector<expression_node_ptr>& arg_list)
  20163. {
  20164. if (!all_nodes_valid(arg_list))
  20165. {
  20166. details::free_all_nodes(*node_allocator_,arg_list);
  20167. return error_node();
  20168. }
  20169. typedef details::vararg_function_node<Type,ivararg_function_t> alloc_type;
  20170. expression_node_ptr result = node_allocator_->allocate<alloc_type>(vaf,arg_list);
  20171. if (
  20172. !arg_list.empty() &&
  20173. !vaf->has_side_effects() &&
  20174. is_constant_foldable(arg_list)
  20175. )
  20176. {
  20177. Type v = result->value();
  20178. details::free_node(*node_allocator_,result);
  20179. result = node_allocator_->allocate<literal_node_t>(v);
  20180. }
  20181. parser_->state_.activate_side_effect("vararg_function_call()");
  20182. return result;
  20183. }
  20184. inline expression_node_ptr generic_function_call(igeneric_function_t* gf,
  20185. std::vector<expression_node_ptr>& arg_list,
  20186. const std::size_t& param_seq_index = std::numeric_limits<std::size_t>::max())
  20187. {
  20188. if (!all_nodes_valid(arg_list))
  20189. {
  20190. details::free_all_nodes(*node_allocator_,arg_list);
  20191. return error_node();
  20192. }
  20193. typedef details::generic_function_node <Type,igeneric_function_t> alloc_type1;
  20194. typedef details::multimode_genfunction_node<Type,igeneric_function_t> alloc_type2;
  20195. const std::size_t no_psi = std::numeric_limits<std::size_t>::max();
  20196. expression_node_ptr result = error_node();
  20197. if (no_psi == param_seq_index)
  20198. result = node_allocator_->allocate<alloc_type1>(arg_list,gf);
  20199. else
  20200. result = node_allocator_->allocate<alloc_type2>(gf,param_seq_index,arg_list);
  20201. alloc_type1* genfunc_node_ptr = static_cast<alloc_type1*>(result);
  20202. if (
  20203. !arg_list.empty() &&
  20204. !gf->has_side_effects() &&
  20205. is_constant_foldable(arg_list)
  20206. )
  20207. {
  20208. genfunc_node_ptr->init_branches();
  20209. Type v = result->value();
  20210. details::free_node(*node_allocator_,result);
  20211. return node_allocator_->allocate<literal_node_t>(v);
  20212. }
  20213. else if (genfunc_node_ptr->init_branches())
  20214. {
  20215. parser_->state_.activate_side_effect("generic_function_call()");
  20216. return result;
  20217. }
  20218. else
  20219. {
  20220. details::free_node(*node_allocator_,result);
  20221. details::free_all_nodes(*node_allocator_,arg_list);
  20222. return error_node();
  20223. }
  20224. }
  20225. inline expression_node_ptr string_function_call(igeneric_function_t* gf,
  20226. std::vector<expression_node_ptr>& arg_list,
  20227. const std::size_t& param_seq_index = std::numeric_limits<std::size_t>::max())
  20228. {
  20229. if (!all_nodes_valid(arg_list))
  20230. {
  20231. details::free_all_nodes(*node_allocator_,arg_list);
  20232. return error_node();
  20233. }
  20234. typedef details::string_function_node <Type,igeneric_function_t> alloc_type1;
  20235. typedef details::multimode_strfunction_node<Type,igeneric_function_t> alloc_type2;
  20236. const std::size_t no_psi = std::numeric_limits<std::size_t>::max();
  20237. expression_node_ptr result = error_node();
  20238. if (no_psi == param_seq_index)
  20239. result = node_allocator_->allocate<alloc_type1>(gf,arg_list);
  20240. else
  20241. result = node_allocator_->allocate<alloc_type2>(gf,param_seq_index,arg_list);
  20242. alloc_type1* strfunc_node_ptr = static_cast<alloc_type1*>(result);
  20243. if (
  20244. !arg_list.empty() &&
  20245. !gf->has_side_effects() &&
  20246. is_constant_foldable(arg_list)
  20247. )
  20248. {
  20249. strfunc_node_ptr->init_branches();
  20250. Type v = result->value();
  20251. details::free_node(*node_allocator_,result);
  20252. return node_allocator_->allocate<literal_node_t>(v);
  20253. }
  20254. else if (strfunc_node_ptr->init_branches())
  20255. {
  20256. parser_->state_.activate_side_effect("string_function_call()");
  20257. return result;
  20258. }
  20259. else
  20260. {
  20261. details::free_node(*node_allocator_,result);
  20262. details::free_all_nodes(*node_allocator_,arg_list);
  20263. return error_node();
  20264. }
  20265. }
  20266. inline expression_node_ptr return_call(std::vector<expression_node_ptr>& arg_list)
  20267. {
  20268. if (!all_nodes_valid(arg_list))
  20269. {
  20270. details::free_all_nodes(*node_allocator_,arg_list);
  20271. return error_node();
  20272. }
  20273. typedef details::return_node<Type> alloc_type;
  20274. expression_node_ptr result = node_allocator_->
  20275. allocate_rr<alloc_type>(arg_list,parser_->results_ctx());
  20276. alloc_type* return_node_ptr = static_cast<alloc_type*>(result);
  20277. if (return_node_ptr->init_branches())
  20278. {
  20279. parser_->state_.activate_side_effect("return_call()");
  20280. return result;
  20281. }
  20282. else
  20283. {
  20284. details::free_node(*node_allocator_,result);
  20285. details::free_all_nodes(*node_allocator_,arg_list);
  20286. return error_node();
  20287. }
  20288. }
  20289. inline expression_node_ptr return_envelope(expression_node_ptr body,
  20290. results_context_t* rc,
  20291. bool*& return_invoked)
  20292. {
  20293. typedef details::return_envelope_node<Type> alloc_type;
  20294. expression_node_ptr result = node_allocator_->
  20295. allocate_cr<alloc_type>(body,(*rc));
  20296. return_invoked = static_cast<alloc_type*>(result)->retinvk_ptr();
  20297. return result;
  20298. }
  20299. inline expression_node_ptr vector_element(const std::string& symbol,
  20300. vector_holder_ptr vector_base,
  20301. expression_node_ptr index)
  20302. {
  20303. expression_node_ptr result = error_node();
  20304. if (details::is_constant_node(index))
  20305. {
  20306. std::size_t i = static_cast<std::size_t>(details::numeric::to_int64(index->value()));
  20307. details::free_node(*node_allocator_,index);
  20308. Type* v = (*vector_base)[i];
  20309. scope_element& se = parser_->sem_.get_element(symbol,i);
  20310. if (se.index == i)
  20311. {
  20312. result = se.var_node;
  20313. }
  20314. else
  20315. {
  20316. scope_element nse;
  20317. nse.name = symbol;
  20318. nse.active = true;
  20319. nse.ref_count = 1;
  20320. nse.type = scope_element::e_vecelem;
  20321. nse.index = i;
  20322. nse.depth = parser_->state_.scope_depth;
  20323. nse.data = 0;
  20324. nse.var_node = new variable_node_t((*v));
  20325. if (!parser_->sem_.add_element(nse))
  20326. {
  20327. parser_->set_synthesis_error("Failed to add new local vector element to SEM [1]");
  20328. parser_->sem_.free_element(nse);
  20329. result = error_node();
  20330. }
  20331. exprtk_debug(("vector_element() - INFO - Added new local vector element: %s\n",nse.name.c_str()));
  20332. parser_->state_.activate_side_effect("vector_element()");
  20333. result = nse.var_node;
  20334. }
  20335. }
  20336. else
  20337. result = node_allocator_->allocate<details::vector_elem_node<Type> >(index,(*vector_base)[0]);
  20338. return result;
  20339. }
  20340. private:
  20341. template <std::size_t N, typename NodePtr>
  20342. inline bool is_constant_foldable(NodePtr (&b)[N]) const
  20343. {
  20344. for (std::size_t i = 0; i < N; ++i)
  20345. {
  20346. if (0 == b[i])
  20347. return false;
  20348. else if (!details::is_constant_node(b[i]))
  20349. return false;
  20350. }
  20351. return true;
  20352. }
  20353. template <typename NodePtr,
  20354. typename Allocator,
  20355. template <typename,typename> class Sequence>
  20356. inline bool is_constant_foldable(const Sequence<NodePtr,Allocator>& b) const
  20357. {
  20358. for (std::size_t i = 0; i < b.size(); ++i)
  20359. {
  20360. if (0 == b[i])
  20361. return false;
  20362. else if (!details::is_constant_node(b[i]))
  20363. return false;
  20364. }
  20365. return true;
  20366. }
  20367. void lodge_assignment(symbol_type cst, expression_node_ptr node)
  20368. {
  20369. parser_->state_.activate_side_effect("lodge_assignment()");
  20370. if (!parser_->dec_.collect_assignments())
  20371. return;
  20372. std::string symbol_name;
  20373. switch (cst)
  20374. {
  20375. case e_st_variable : symbol_name = parser_->symtab_store_
  20376. .get_variable_name(node);
  20377. break;
  20378. #ifndef exprtk_disable_string_capabilities
  20379. case e_st_string : symbol_name = parser_->symtab_store_
  20380. .get_stringvar_name(node);
  20381. break;
  20382. #endif
  20383. case e_st_vector : {
  20384. typedef details::vector_holder<T> vector_holder_t;
  20385. vector_holder_t& vh = static_cast<vector_node_t*>(node)->ref();
  20386. symbol_name = parser_->symtab_store_.get_vector_name(&vh);
  20387. }
  20388. break;
  20389. default : return;
  20390. }
  20391. if (!symbol_name.empty())
  20392. {
  20393. parser_->dec_.add_assignment(symbol_name,cst);
  20394. }
  20395. }
  20396. inline expression_node_ptr synthesize_assignment_expression(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  20397. {
  20398. if (details::is_variable_node(branch[0]))
  20399. {
  20400. lodge_assignment(e_st_variable,branch[0]);
  20401. return synthesize_expression<assignment_node_t,2>(operation,branch);
  20402. }
  20403. else if (details::is_vector_elem_node(branch[0]))
  20404. return synthesize_expression<assignment_vec_elem_node_t,2>(operation,branch);
  20405. #ifndef exprtk_disable_string_capabilities
  20406. else if (details::is_string_node(branch[0]))
  20407. {
  20408. lodge_assignment(e_st_string,branch[0]);
  20409. return synthesize_expression<assignment_string_node_t,2>(operation,branch);
  20410. }
  20411. else if (details::is_string_range_node(branch[0]))
  20412. {
  20413. lodge_assignment(e_st_string,branch[0]);
  20414. return synthesize_expression<assignment_string_range_node_t,2>(operation,branch);
  20415. }
  20416. #endif
  20417. else if (details::is_vector_node(branch[0]))
  20418. {
  20419. lodge_assignment(e_st_vector,branch[0]);
  20420. if (details::is_ivector_node(branch[1]))
  20421. return synthesize_expression<assignment_vecvec_node_t,2>(operation,branch);
  20422. else
  20423. return synthesize_expression<assignment_vec_node_t,2>(operation,branch);
  20424. }
  20425. else
  20426. {
  20427. parser_->set_synthesis_error("Invalid assignment operation.[1]");
  20428. return error_node();
  20429. }
  20430. }
  20431. inline expression_node_ptr synthesize_assignment_operation_expression(const details::operator_type& operation,
  20432. expression_node_ptr (&branch)[2])
  20433. {
  20434. if (details::is_variable_node(branch[0]))
  20435. {
  20436. lodge_assignment(e_st_variable,branch[0]);
  20437. switch (operation)
  20438. {
  20439. #define case_stmt(op0,op1) \
  20440. case op0 : return node_allocator_-> \
  20441. template allocate_rrr<typename details::assignment_op_node<Type,op1<Type> > > \
  20442. (operation,branch[0],branch[1]); \
  20443. case_stmt(details::e_addass,details::add_op)
  20444. case_stmt(details::e_subass,details::sub_op)
  20445. case_stmt(details::e_mulass,details::mul_op)
  20446. case_stmt(details::e_divass,details::div_op)
  20447. case_stmt(details::e_modass,details::mod_op)
  20448. #undef case_stmt
  20449. default : return error_node();
  20450. }
  20451. }
  20452. else if (details::is_vector_elem_node(branch[0]))
  20453. {
  20454. switch (operation)
  20455. {
  20456. #define case_stmt(op0,op1) \
  20457. case op0 : return node_allocator_-> \
  20458. template allocate_rrr<typename details::assignment_vec_elem_op_node<Type,op1<Type> > > \
  20459. (operation,branch[0],branch[1]); \
  20460. case_stmt(details::e_addass,details::add_op)
  20461. case_stmt(details::e_subass,details::sub_op)
  20462. case_stmt(details::e_mulass,details::mul_op)
  20463. case_stmt(details::e_divass,details::div_op)
  20464. case_stmt(details::e_modass,details::mod_op)
  20465. #undef case_stmt
  20466. default : return error_node();
  20467. }
  20468. }
  20469. else if (details::is_vector_node(branch[0]))
  20470. {
  20471. lodge_assignment(e_st_vector,branch[0]);
  20472. if (details::is_ivector_node(branch[1]))
  20473. {
  20474. switch (operation)
  20475. {
  20476. #define case_stmt(op0,op1) \
  20477. case op0 : return node_allocator_-> \
  20478. template allocate_rrr<typename details::assignment_vecvec_op_node<Type,op1<Type> > > \
  20479. (operation,branch[0],branch[1]); \
  20480. case_stmt(details::e_addass,details::add_op)
  20481. case_stmt(details::e_subass,details::sub_op)
  20482. case_stmt(details::e_mulass,details::mul_op)
  20483. case_stmt(details::e_divass,details::div_op)
  20484. case_stmt(details::e_modass,details::mod_op)
  20485. #undef case_stmt
  20486. default : return error_node();
  20487. }
  20488. }
  20489. else
  20490. {
  20491. switch (operation)
  20492. {
  20493. #define case_stmt(op0,op1) \
  20494. case op0 : return node_allocator_-> \
  20495. template allocate_rrr<typename details::assignment_vec_op_node<Type,op1<Type> > > \
  20496. (operation,branch[0],branch[1]); \
  20497. case_stmt(details::e_addass,details::add_op)
  20498. case_stmt(details::e_subass,details::sub_op)
  20499. case_stmt(details::e_mulass,details::mul_op)
  20500. case_stmt(details::e_divass,details::div_op)
  20501. case_stmt(details::e_modass,details::mod_op)
  20502. #undef case_stmt
  20503. default : return error_node();
  20504. }
  20505. }
  20506. }
  20507. #ifndef exprtk_disable_string_capabilities
  20508. else if (
  20509. (details::e_addass == operation) &&
  20510. details::is_string_node(branch[0])
  20511. )
  20512. {
  20513. typedef details::assignment_string_node<T,details::asn_addassignment> addass_t;
  20514. lodge_assignment(e_st_string,branch[0]);
  20515. return synthesize_expression<addass_t,2>(operation,branch);
  20516. }
  20517. #endif
  20518. else
  20519. {
  20520. parser_->set_synthesis_error("Invalid assignment operation[2]");
  20521. return error_node();
  20522. }
  20523. }
  20524. inline expression_node_ptr synthesize_veceqineq_operation_expression(const details::operator_type& operation,
  20525. expression_node_ptr (&branch)[2])
  20526. {
  20527. const bool is_b0_ivec = details::is_ivector_node(branch[0]);
  20528. const bool is_b1_ivec = details::is_ivector_node(branch[1]);
  20529. if (is_b0_ivec && is_b1_ivec)
  20530. {
  20531. switch (operation)
  20532. {
  20533. #define case_stmt(op0,op1) \
  20534. case op0 : return node_allocator_-> \
  20535. template allocate_rrr<typename details::eqineq_vecvec_node<Type,op1<Type> > > \
  20536. (operation,branch[0],branch[1]); \
  20537. case_stmt(details:: e_lt,details:: lt_op)
  20538. case_stmt(details:: e_lte,details:: lte_op)
  20539. case_stmt(details:: e_gt,details:: gt_op)
  20540. case_stmt(details:: e_gte,details:: gte_op)
  20541. case_stmt(details:: e_eq,details:: eq_op)
  20542. case_stmt(details:: e_ne,details:: ne_op)
  20543. #undef case_stmt
  20544. default : return error_node();
  20545. }
  20546. }
  20547. else if (is_b0_ivec && !is_b1_ivec)
  20548. {
  20549. switch (operation)
  20550. {
  20551. #define case_stmt(op0,op1) \
  20552. case op0 : return node_allocator_-> \
  20553. template allocate_rrr<typename details::eqineq_vecval_node<Type,op1<Type> > > \
  20554. (operation,branch[0],branch[1]); \
  20555. case_stmt(details:: e_lt,details:: lt_op)
  20556. case_stmt(details:: e_lte,details:: lte_op)
  20557. case_stmt(details:: e_gt,details:: gt_op)
  20558. case_stmt(details:: e_gte,details:: gte_op)
  20559. case_stmt(details:: e_eq,details:: eq_op)
  20560. case_stmt(details:: e_ne,details:: ne_op)
  20561. #undef case_stmt
  20562. default : return error_node();
  20563. }
  20564. }
  20565. else if (!is_b0_ivec && is_b1_ivec)
  20566. {
  20567. switch (operation)
  20568. {
  20569. #define case_stmt(op0,op1) \
  20570. case op0 : return node_allocator_-> \
  20571. template allocate_rrr<typename details::eqineq_valvec_node<Type,op1<Type> > > \
  20572. (operation,branch[0],branch[1]); \
  20573. case_stmt(details:: e_lt,details:: lt_op)
  20574. case_stmt(details:: e_lte,details:: lte_op)
  20575. case_stmt(details:: e_gt,details:: gt_op)
  20576. case_stmt(details:: e_gte,details:: gte_op)
  20577. case_stmt(details:: e_eq,details:: eq_op)
  20578. case_stmt(details:: e_ne,details:: ne_op)
  20579. #undef case_stmt
  20580. default : return error_node();
  20581. }
  20582. }
  20583. else
  20584. return error_node();
  20585. }
  20586. inline expression_node_ptr synthesize_vecarithmetic_operation_expression(const details::operator_type& operation,
  20587. expression_node_ptr (&branch)[2])
  20588. {
  20589. const bool is_b0_ivec = details::is_ivector_node(branch[0]);
  20590. const bool is_b1_ivec = details::is_ivector_node(branch[1]);
  20591. #define vector_ops \
  20592. case_stmt(details::e_add,details::add_op) \
  20593. case_stmt(details::e_sub,details::sub_op) \
  20594. case_stmt(details::e_mul,details::mul_op) \
  20595. case_stmt(details::e_div,details::div_op) \
  20596. case_stmt(details::e_mod,details::mod_op) \
  20597. if (is_b0_ivec && is_b1_ivec)
  20598. {
  20599. switch (operation)
  20600. {
  20601. #define case_stmt(op0,op1) \
  20602. case op0 : return node_allocator_-> \
  20603. template allocate_rrr<typename details::vecarith_vecvec_node<Type,op1<Type> > > \
  20604. (operation,branch[0],branch[1]); \
  20605. vector_ops
  20606. case_stmt(details::e_pow,details:: pow_op)
  20607. #undef case_stmt
  20608. default : return error_node();
  20609. }
  20610. }
  20611. else if (is_b0_ivec && !is_b1_ivec)
  20612. {
  20613. switch (operation)
  20614. {
  20615. #define case_stmt(op0,op1) \
  20616. case op0 : return node_allocator_-> \
  20617. template allocate_rrr<typename details::vecarith_vecval_node<Type,op1<Type> > > \
  20618. (operation,branch[0],branch[1]); \
  20619. vector_ops
  20620. case_stmt(details::e_pow,details:: pow_op)
  20621. #undef case_stmt
  20622. default : return error_node();
  20623. }
  20624. }
  20625. else if (!is_b0_ivec && is_b1_ivec)
  20626. {
  20627. switch (operation)
  20628. {
  20629. #define case_stmt(op0,op1) \
  20630. case op0 : return node_allocator_-> \
  20631. template allocate_rrr<typename details::vecarith_valvec_node<Type,op1<Type> > > \
  20632. (operation,branch[0],branch[1]); \
  20633. vector_ops
  20634. #undef case_stmt
  20635. default : return error_node();
  20636. }
  20637. }
  20638. else
  20639. return error_node();
  20640. #undef vector_ops
  20641. }
  20642. inline expression_node_ptr synthesize_swap_expression(expression_node_ptr (&branch)[2])
  20643. {
  20644. const bool v0_is_ivar = details::is_ivariable_node(branch[0]);
  20645. const bool v1_is_ivar = details::is_ivariable_node(branch[1]);
  20646. const bool v0_is_ivec = details::is_ivector_node(branch[0]);
  20647. const bool v1_is_ivec = details::is_ivector_node(branch[1]);
  20648. #ifndef exprtk_disable_string_capabilities
  20649. const bool v0_is_str = details::is_generally_string_node(branch[0]);
  20650. const bool v1_is_str = details::is_generally_string_node(branch[1]);
  20651. #endif
  20652. expression_node_ptr result = error_node();
  20653. if (v0_is_ivar && v1_is_ivar)
  20654. {
  20655. typedef details::variable_node<T>* variable_node_ptr;
  20656. variable_node_ptr v0 = variable_node_ptr(0);
  20657. variable_node_ptr v1 = variable_node_ptr(0);
  20658. if (
  20659. (0 != (v0 = dynamic_cast<variable_node_ptr>(branch[0]))) &&
  20660. (0 != (v1 = dynamic_cast<variable_node_ptr>(branch[1])))
  20661. )
  20662. {
  20663. result = node_allocator_->allocate<details::swap_node<T> >(v0,v1);
  20664. }
  20665. else
  20666. result = node_allocator_->allocate<details::swap_generic_node<T> >(branch[0],branch[1]);
  20667. }
  20668. else if (v0_is_ivec && v1_is_ivec)
  20669. {
  20670. result = node_allocator_->allocate<details::swap_vecvec_node<T> >(branch[0],branch[1]);
  20671. }
  20672. #ifndef exprtk_disable_string_capabilities
  20673. else if (v0_is_str && v1_is_str)
  20674. {
  20675. result = node_allocator_->allocate<details::swap_string_node<T> >(branch[0],branch[1]);
  20676. }
  20677. #endif
  20678. else
  20679. {
  20680. parser_->set_synthesis_error("Only variables, strings, vectors or vector elements can be swapped");
  20681. return error_node();
  20682. }
  20683. parser_->state_.activate_side_effect("synthesize_swap_expression()");
  20684. return result;
  20685. }
  20686. #ifndef exprtk_disable_sc_andor
  20687. inline expression_node_ptr synthesize_shortcircuit_expression(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  20688. {
  20689. expression_node_ptr result = error_node();
  20690. if (details::is_constant_node(branch[0]))
  20691. {
  20692. if (
  20693. (details::e_scand == operation) &&
  20694. std::equal_to<T>()(T(0),branch[0]->value())
  20695. )
  20696. result = node_allocator_->allocate_c<literal_node_t>(T(0));
  20697. else if (
  20698. (details::e_scor == operation) &&
  20699. std::not_equal_to<T>()(T(0),branch[0]->value())
  20700. )
  20701. result = node_allocator_->allocate_c<literal_node_t>(T(1));
  20702. }
  20703. if (details::is_constant_node(branch[1]) && (0 == result))
  20704. {
  20705. if (
  20706. (details::e_scand == operation) &&
  20707. std::equal_to<T>()(T(0),branch[1]->value())
  20708. )
  20709. result = node_allocator_->allocate_c<literal_node_t>(T(0));
  20710. else if (
  20711. (details::e_scor == operation) &&
  20712. std::not_equal_to<T>()(T(0),branch[1]->value())
  20713. )
  20714. result = node_allocator_->allocate_c<literal_node_t>(T(1));
  20715. }
  20716. if (result)
  20717. {
  20718. free_node(*node_allocator_,branch[0]);
  20719. free_node(*node_allocator_,branch[1]);
  20720. return result;
  20721. }
  20722. else if (details::e_scand == operation)
  20723. return synthesize_expression<scand_node_t,2>(operation,branch);
  20724. else if (details::e_scor == operation)
  20725. return synthesize_expression<scor_node_t,2>(operation,branch);
  20726. else
  20727. return error_node();
  20728. }
  20729. #else
  20730. inline expression_node_ptr synthesize_shortcircuit_expression(const details::operator_type&, expression_node_ptr (&)[2])
  20731. {
  20732. return error_node();
  20733. }
  20734. #endif
  20735. #define basic_opr_switch_statements \
  20736. case_stmt(details::e_add,details::add_op) \
  20737. case_stmt(details::e_sub,details::sub_op) \
  20738. case_stmt(details::e_mul,details::mul_op) \
  20739. case_stmt(details::e_div,details::div_op) \
  20740. case_stmt(details::e_mod,details::mod_op) \
  20741. case_stmt(details::e_pow,details::pow_op) \
  20742. #define extended_opr_switch_statements \
  20743. case_stmt(details:: e_lt,details:: lt_op) \
  20744. case_stmt(details:: e_lte,details:: lte_op) \
  20745. case_stmt(details:: e_gt,details:: gt_op) \
  20746. case_stmt(details:: e_gte,details:: gte_op) \
  20747. case_stmt(details:: e_eq,details:: eq_op) \
  20748. case_stmt(details:: e_ne,details:: ne_op) \
  20749. case_stmt(details:: e_and,details:: and_op) \
  20750. case_stmt(details::e_nand,details::nand_op) \
  20751. case_stmt(details:: e_or,details:: or_op) \
  20752. case_stmt(details:: e_nor,details:: nor_op) \
  20753. case_stmt(details:: e_xor,details:: xor_op) \
  20754. case_stmt(details::e_xnor,details::xnor_op) \
  20755. #ifndef exprtk_disable_cardinal_pow_optimisation
  20756. template <template <typename,typename> class IPowNode>
  20757. inline expression_node_ptr cardinal_pow_optimization_impl(const T& v, const unsigned int& p)
  20758. {
  20759. switch (p)
  20760. {
  20761. #define case_stmt(cp) \
  20762. case cp : return node_allocator_-> \
  20763. allocate<IPowNode<T,details::numeric::fast_exp<T,cp> > >(v); \
  20764. case_stmt( 1) case_stmt( 2) case_stmt( 3) case_stmt( 4)
  20765. case_stmt( 5) case_stmt( 6) case_stmt( 7) case_stmt( 8)
  20766. case_stmt( 9) case_stmt(10) case_stmt(11) case_stmt(12)
  20767. case_stmt(13) case_stmt(14) case_stmt(15) case_stmt(16)
  20768. case_stmt(17) case_stmt(18) case_stmt(19) case_stmt(20)
  20769. case_stmt(21) case_stmt(22) case_stmt(23) case_stmt(24)
  20770. case_stmt(25) case_stmt(26) case_stmt(27) case_stmt(28)
  20771. case_stmt(29) case_stmt(30) case_stmt(31) case_stmt(32)
  20772. case_stmt(33) case_stmt(34) case_stmt(35) case_stmt(36)
  20773. case_stmt(37) case_stmt(38) case_stmt(39) case_stmt(40)
  20774. case_stmt(41) case_stmt(42) case_stmt(43) case_stmt(44)
  20775. case_stmt(45) case_stmt(46) case_stmt(47) case_stmt(48)
  20776. case_stmt(49) case_stmt(50) case_stmt(51) case_stmt(52)
  20777. case_stmt(53) case_stmt(54) case_stmt(55) case_stmt(56)
  20778. case_stmt(57) case_stmt(58) case_stmt(59) case_stmt(60)
  20779. #undef case_stmt
  20780. default : return error_node();
  20781. }
  20782. }
  20783. inline expression_node_ptr cardinal_pow_optimization(const T& v, const T& c)
  20784. {
  20785. const bool not_recipricol = (c >= T(0));
  20786. const int p = details::numeric::to_int32(details::numeric::abs(c));
  20787. if (0 == p)
  20788. return node_allocator_->allocate_c<literal_node_t>(T(1));
  20789. else if (std::equal_to<T>()(T(2),c))
  20790. {
  20791. return node_allocator_->
  20792. template allocate_rr<typename details::vov_node<Type,details::mul_op<Type> > >(v,v);
  20793. }
  20794. else
  20795. {
  20796. if (not_recipricol)
  20797. return cardinal_pow_optimization_impl<details::ipow_node>(v,p);
  20798. else
  20799. return cardinal_pow_optimization_impl<details::ipowinv_node>(v,p);
  20800. }
  20801. }
  20802. inline bool cardinal_pow_optimizable(const details::operator_type& operation, const T& c)
  20803. {
  20804. return (details::e_pow == operation) && (details::numeric::abs(c) <= T(60)) && details::numeric::is_integer(c);
  20805. }
  20806. #else
  20807. inline expression_node_ptr cardinal_pow_optimization(T&, const T&)
  20808. {
  20809. return error_node();
  20810. }
  20811. inline bool cardinal_pow_optimizable(const details::operator_type&, const T&)
  20812. {
  20813. return false;
  20814. }
  20815. #endif
  20816. struct synthesize_binary_ext_expression
  20817. {
  20818. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  20819. const details::operator_type& operation,
  20820. expression_node_ptr (&branch)[2])
  20821. {
  20822. const bool left_neg = is_neg_unary_node(branch[0]);
  20823. const bool right_neg = is_neg_unary_node(branch[1]);
  20824. if (left_neg && right_neg)
  20825. {
  20826. if (
  20827. (details::e_add == operation) ||
  20828. (details::e_sub == operation) ||
  20829. (details::e_mul == operation) ||
  20830. (details::e_div == operation)
  20831. )
  20832. {
  20833. if (
  20834. !expr_gen.parser_->simplify_unary_negation_branch(branch[0]) ||
  20835. !expr_gen.parser_->simplify_unary_negation_branch(branch[1])
  20836. )
  20837. {
  20838. details::free_all_nodes(*expr_gen.node_allocator_,branch);
  20839. return error_node();
  20840. }
  20841. }
  20842. switch (operation)
  20843. {
  20844. // -f(x + 1) + -g(y + 1) --> -(f(x + 1) + g(y + 1))
  20845. case details::e_add : return expr_gen(details::e_neg,
  20846. expr_gen.node_allocator_->
  20847. template allocate<typename details::binary_ext_node<Type,details::add_op<Type> > >
  20848. (branch[0],branch[1]));
  20849. // -f(x + 1) - -g(y + 1) --> g(y + 1) - f(x + 1)
  20850. case details::e_sub : return expr_gen.node_allocator_->
  20851. template allocate<typename details::binary_ext_node<Type,details::sub_op<Type> > >
  20852. (branch[1],branch[0]);
  20853. default : break;
  20854. }
  20855. }
  20856. else if (left_neg && !right_neg)
  20857. {
  20858. if (
  20859. (details::e_add == operation) ||
  20860. (details::e_sub == operation) ||
  20861. (details::e_mul == operation) ||
  20862. (details::e_div == operation)
  20863. )
  20864. {
  20865. if (!expr_gen.parser_->simplify_unary_negation_branch(branch[0]))
  20866. {
  20867. details::free_all_nodes(*expr_gen.node_allocator_,branch);
  20868. return error_node();
  20869. }
  20870. switch (operation)
  20871. {
  20872. // -f(x + 1) + g(y + 1) --> g(y + 1) - f(x + 1)
  20873. case details::e_add : return expr_gen.node_allocator_->
  20874. template allocate<typename details::binary_ext_node<Type,details::sub_op<Type> > >
  20875. (branch[1],branch[0]);
  20876. // -f(x + 1) - g(y + 1) --> -(f(x + 1) + g(y + 1))
  20877. case details::e_sub : return expr_gen(details::e_neg,
  20878. expr_gen.node_allocator_->
  20879. template allocate<typename details::binary_ext_node<Type,details::add_op<Type> > >
  20880. (branch[0],branch[1]));
  20881. // -f(x + 1) * g(y + 1) --> -(f(x + 1) * g(y + 1))
  20882. case details::e_mul : return expr_gen(details::e_neg,
  20883. expr_gen.node_allocator_->
  20884. template allocate<typename details::binary_ext_node<Type,details::mul_op<Type> > >
  20885. (branch[0],branch[1]));
  20886. // -f(x + 1) / g(y + 1) --> -(f(x + 1) / g(y + 1))
  20887. case details::e_div : return expr_gen(details::e_neg,
  20888. expr_gen.node_allocator_->
  20889. template allocate<typename details::binary_ext_node<Type,details::div_op<Type> > >
  20890. (branch[0],branch[1]));
  20891. default : return error_node();
  20892. }
  20893. }
  20894. }
  20895. else if (!left_neg && right_neg)
  20896. {
  20897. if (
  20898. (details::e_add == operation) ||
  20899. (details::e_sub == operation) ||
  20900. (details::e_mul == operation) ||
  20901. (details::e_div == operation)
  20902. )
  20903. {
  20904. if (!expr_gen.parser_->simplify_unary_negation_branch(branch[1]))
  20905. {
  20906. details::free_all_nodes(*expr_gen.node_allocator_,branch);
  20907. return error_node();
  20908. }
  20909. switch (operation)
  20910. {
  20911. // f(x + 1) + -g(y + 1) --> f(x + 1) - g(y + 1)
  20912. case details::e_add : return expr_gen.node_allocator_->
  20913. template allocate<typename details::binary_ext_node<Type,details::sub_op<Type> > >
  20914. (branch[0],branch[1]);
  20915. // f(x + 1) - - g(y + 1) --> f(x + 1) + g(y + 1)
  20916. case details::e_sub : return expr_gen.node_allocator_->
  20917. template allocate<typename details::binary_ext_node<Type,details::add_op<Type> > >
  20918. (branch[0],branch[1]);
  20919. // f(x + 1) * -g(y + 1) --> -(f(x + 1) * g(y + 1))
  20920. case details::e_mul : return expr_gen(details::e_neg,
  20921. expr_gen.node_allocator_->
  20922. template allocate<typename details::binary_ext_node<Type,details::mul_op<Type> > >
  20923. (branch[0],branch[1]));
  20924. // f(x + 1) / -g(y + 1) --> -(f(x + 1) / g(y + 1))
  20925. case details::e_div : return expr_gen(details::e_neg,
  20926. expr_gen.node_allocator_->
  20927. template allocate<typename details::binary_ext_node<Type,details::div_op<Type> > >
  20928. (branch[0],branch[1]));
  20929. default : return error_node();
  20930. }
  20931. }
  20932. }
  20933. switch (operation)
  20934. {
  20935. #define case_stmt(op0,op1) \
  20936. case op0 : return expr_gen.node_allocator_-> \
  20937. template allocate<typename details::binary_ext_node<Type,op1<Type> > > \
  20938. (branch[0],branch[1]); \
  20939. basic_opr_switch_statements
  20940. extended_opr_switch_statements
  20941. #undef case_stmt
  20942. default : return error_node();
  20943. }
  20944. }
  20945. };
  20946. struct synthesize_vob_expression
  20947. {
  20948. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  20949. const details::operator_type& operation,
  20950. expression_node_ptr (&branch)[2])
  20951. {
  20952. const Type& v = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  20953. #ifndef exprtk_disable_enhanced_features
  20954. if (details::is_sf3ext_node(branch[1]))
  20955. {
  20956. expression_node_ptr result = error_node();
  20957. if (synthesize_sf4ext_expression::template compile_right<vtype>(expr_gen,v,operation,branch[1],result))
  20958. {
  20959. free_node(*expr_gen.node_allocator_,branch[1]);
  20960. return result;
  20961. }
  20962. }
  20963. #endif
  20964. if (
  20965. (details::e_mul == operation) ||
  20966. (details::e_div == operation)
  20967. )
  20968. {
  20969. if (details::is_uv_node(branch[1]))
  20970. {
  20971. typedef details::uv_base_node<Type>* uvbn_ptr_t;
  20972. details::operator_type o = static_cast<uvbn_ptr_t>(branch[1])->operation();
  20973. if (details::e_neg == o)
  20974. {
  20975. const Type& v1 = static_cast<uvbn_ptr_t>(branch[1])->v();
  20976. free_node(*expr_gen.node_allocator_,branch[1]);
  20977. switch (operation)
  20978. {
  20979. case details::e_mul : return expr_gen(details::e_neg,
  20980. expr_gen.node_allocator_->
  20981. template allocate_rr<typename details::
  20982. vov_node<Type,details::mul_op<Type> > >(v,v1));
  20983. case details::e_div : return expr_gen(details::e_neg,
  20984. expr_gen.node_allocator_->
  20985. template allocate_rr<typename details::
  20986. vov_node<Type,details::div_op<Type> > >(v,v1));
  20987. default : break;
  20988. }
  20989. }
  20990. }
  20991. }
  20992. switch (operation)
  20993. {
  20994. #define case_stmt(op0,op1) \
  20995. case op0 : return expr_gen.node_allocator_-> \
  20996. template allocate_rc<typename details::vob_node<Type,op1<Type> > > \
  20997. (v,branch[1]); \
  20998. basic_opr_switch_statements
  20999. extended_opr_switch_statements
  21000. #undef case_stmt
  21001. default : return error_node();
  21002. }
  21003. }
  21004. };
  21005. struct synthesize_bov_expression
  21006. {
  21007. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21008. const details::operator_type& operation,
  21009. expression_node_ptr (&branch)[2])
  21010. {
  21011. const Type& v = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  21012. #ifndef exprtk_disable_enhanced_features
  21013. if (details::is_sf3ext_node(branch[0]))
  21014. {
  21015. expression_node_ptr result = error_node();
  21016. if (synthesize_sf4ext_expression::template compile_left<vtype>(expr_gen,v,operation,branch[0],result))
  21017. {
  21018. free_node(*expr_gen.node_allocator_,branch[0]);
  21019. return result;
  21020. }
  21021. }
  21022. #endif
  21023. if (
  21024. (details::e_add == operation) ||
  21025. (details::e_sub == operation) ||
  21026. (details::e_mul == operation) ||
  21027. (details::e_div == operation)
  21028. )
  21029. {
  21030. if (details::is_uv_node(branch[0]))
  21031. {
  21032. typedef details::uv_base_node<Type>* uvbn_ptr_t;
  21033. details::operator_type o = static_cast<uvbn_ptr_t>(branch[0])->operation();
  21034. if (details::e_neg == o)
  21035. {
  21036. const Type& v0 = static_cast<uvbn_ptr_t>(branch[0])->v();
  21037. free_node(*expr_gen.node_allocator_,branch[0]);
  21038. switch (operation)
  21039. {
  21040. case details::e_add : return expr_gen.node_allocator_->
  21041. template allocate_rr<typename details::
  21042. vov_node<Type,details::sub_op<Type> > >(v,v0);
  21043. case details::e_sub : return expr_gen(details::e_neg,
  21044. expr_gen.node_allocator_->
  21045. template allocate_rr<typename details::
  21046. vov_node<Type,details::add_op<Type> > >(v0,v));
  21047. case details::e_mul : return expr_gen(details::e_neg,
  21048. expr_gen.node_allocator_->
  21049. template allocate_rr<typename details::
  21050. vov_node<Type,details::mul_op<Type> > >(v0,v));
  21051. case details::e_div : return expr_gen(details::e_neg,
  21052. expr_gen.node_allocator_->
  21053. template allocate_rr<typename details::
  21054. vov_node<Type,details::div_op<Type> > >(v0,v));
  21055. default : break;
  21056. }
  21057. }
  21058. }
  21059. }
  21060. switch (operation)
  21061. {
  21062. #define case_stmt(op0,op1) \
  21063. case op0 : return expr_gen.node_allocator_-> \
  21064. template allocate_cr<typename details::bov_node<Type,op1<Type> > > \
  21065. (branch[0],v); \
  21066. basic_opr_switch_statements
  21067. extended_opr_switch_statements
  21068. #undef case_stmt
  21069. default : return error_node();
  21070. }
  21071. }
  21072. };
  21073. struct synthesize_cob_expression
  21074. {
  21075. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21076. const details::operator_type& operation,
  21077. expression_node_ptr (&branch)[2])
  21078. {
  21079. const Type c = static_cast<details::literal_node<Type>*>(branch[0])->value();
  21080. free_node(*expr_gen.node_allocator_,branch[0]);
  21081. if (details::is_cob_node(branch[1]))
  21082. {
  21083. // Simplify expressions of the form:
  21084. // 1. (1 * (2 * (3 * (4 * (5 * (6 * (7 * (8 * (9 + x))))))))) --> 40320 * (9 + x)
  21085. // 2. (1 + (2 + (3 + (4 + (5 + (6 + (7 + (8 + (9 + x))))))))) --> 45 + x
  21086. if (
  21087. (operation == details::e_mul) ||
  21088. (operation == details::e_add)
  21089. )
  21090. {
  21091. details::cob_base_node<Type>* cobnode = static_cast<details::cob_base_node<Type>*>(branch[1]);
  21092. if (operation == cobnode->operation())
  21093. {
  21094. switch (operation)
  21095. {
  21096. case details::e_add : cobnode->set_c(c + cobnode->c()); break;
  21097. case details::e_mul : cobnode->set_c(c * cobnode->c()); break;
  21098. default : return error_node();
  21099. }
  21100. return cobnode;
  21101. }
  21102. }
  21103. if (operation == details::e_mul)
  21104. {
  21105. details::cob_base_node<Type>* cobnode = static_cast<details::cob_base_node<Type>*>(branch[1]);
  21106. details::operator_type cob_opr = cobnode->operation();
  21107. if (
  21108. (details::e_div == cob_opr) ||
  21109. (details::e_mul == cob_opr)
  21110. )
  21111. {
  21112. switch (cob_opr)
  21113. {
  21114. case details::e_div : cobnode->set_c(c * cobnode->c()); break;
  21115. case details::e_mul : cobnode->set_c(cobnode->c() / c); break;
  21116. default : return error_node();
  21117. }
  21118. return cobnode;
  21119. }
  21120. }
  21121. else if (operation == details::e_div)
  21122. {
  21123. details::cob_base_node<Type>* cobnode = static_cast<details::cob_base_node<Type>*>(branch[1]);
  21124. details::operator_type cob_opr = cobnode->operation();
  21125. if (
  21126. (details::e_div == cob_opr) ||
  21127. (details::e_mul == cob_opr)
  21128. )
  21129. {
  21130. details::expression_node<Type>* new_cobnode = error_node();
  21131. switch (cob_opr)
  21132. {
  21133. case details::e_div : new_cobnode = expr_gen.node_allocator_->
  21134. template allocate_tt<typename details::cob_node<Type,details::mul_op<Type> > >
  21135. (c / cobnode->c(),cobnode->move_branch(0));
  21136. break;
  21137. case details::e_mul : new_cobnode = expr_gen.node_allocator_->
  21138. template allocate_tt<typename details::cob_node<Type,details::div_op<Type> > >
  21139. (c / cobnode->c(),cobnode->move_branch(0));
  21140. break;
  21141. default : return error_node();
  21142. }
  21143. free_node(*expr_gen.node_allocator_,branch[1]);
  21144. return new_cobnode;
  21145. }
  21146. }
  21147. }
  21148. #ifndef exprtk_disable_enhanced_features
  21149. else if (details::is_sf3ext_node(branch[1]))
  21150. {
  21151. expression_node_ptr result = error_node();
  21152. if (synthesize_sf4ext_expression::template compile_right<ctype>(expr_gen,c,operation,branch[1],result))
  21153. {
  21154. free_node(*expr_gen.node_allocator_,branch[1]);
  21155. return result;
  21156. }
  21157. }
  21158. #endif
  21159. switch (operation)
  21160. {
  21161. #define case_stmt(op0,op1) \
  21162. case op0 : return expr_gen.node_allocator_-> \
  21163. template allocate_tt<typename details::cob_node<Type,op1<Type> > > \
  21164. (c,branch[1]); \
  21165. basic_opr_switch_statements
  21166. extended_opr_switch_statements
  21167. #undef case_stmt
  21168. default : return error_node();
  21169. }
  21170. }
  21171. };
  21172. struct synthesize_boc_expression
  21173. {
  21174. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21175. const details::operator_type& operation,
  21176. expression_node_ptr (&branch)[2])
  21177. {
  21178. const Type c = static_cast<details::literal_node<Type>*>(branch[1])->value();
  21179. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  21180. if (details::is_boc_node(branch[0]))
  21181. {
  21182. // Simplify expressions of the form:
  21183. // 1. (((((((((x + 9) * 8) * 7) * 6) * 5) * 4) * 3) * 2) * 1) --> (x + 9) * 40320
  21184. // 2. (((((((((x + 9) + 8) + 7) + 6) + 5) + 4) + 3) + 2) + 1) --> x + 45
  21185. if (
  21186. (operation == details::e_mul) ||
  21187. (operation == details::e_add)
  21188. )
  21189. {
  21190. details::boc_base_node<Type>* bocnode = static_cast<details::boc_base_node<Type>*>(branch[0]);
  21191. if (operation == bocnode->operation())
  21192. {
  21193. switch (operation)
  21194. {
  21195. case details::e_add : bocnode->set_c(c + bocnode->c()); break;
  21196. case details::e_mul : bocnode->set_c(c * bocnode->c()); break;
  21197. default : return error_node();
  21198. }
  21199. return bocnode;
  21200. }
  21201. }
  21202. else if (operation == details::e_div)
  21203. {
  21204. details::boc_base_node<Type>* bocnode = static_cast<details::boc_base_node<Type>*>(branch[0]);
  21205. details::operator_type boc_opr = bocnode->operation();
  21206. if (
  21207. (details::e_div == boc_opr) ||
  21208. (details::e_mul == boc_opr)
  21209. )
  21210. {
  21211. switch (boc_opr)
  21212. {
  21213. case details::e_div : bocnode->set_c(c * bocnode->c()); break;
  21214. case details::e_mul : bocnode->set_c(bocnode->c() / c); break;
  21215. default : return error_node();
  21216. }
  21217. return bocnode;
  21218. }
  21219. }
  21220. }
  21221. #ifndef exprtk_disable_enhanced_features
  21222. if (details::is_sf3ext_node(branch[0]))
  21223. {
  21224. expression_node_ptr result = error_node();
  21225. if (synthesize_sf4ext_expression::template compile_left<ctype>(expr_gen,c,operation,branch[0],result))
  21226. {
  21227. free_node(*expr_gen.node_allocator_,branch[0]);
  21228. return result;
  21229. }
  21230. }
  21231. #endif
  21232. switch (operation)
  21233. {
  21234. #define case_stmt(op0,op1) \
  21235. case op0 : return expr_gen.node_allocator_-> \
  21236. template allocate_cr<typename details::boc_node<Type,op1<Type> > > \
  21237. (branch[0],c); \
  21238. basic_opr_switch_statements
  21239. extended_opr_switch_statements
  21240. #undef case_stmt
  21241. default : return error_node();
  21242. }
  21243. }
  21244. };
  21245. struct synthesize_cocob_expression
  21246. {
  21247. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21248. const details::operator_type& operation,
  21249. expression_node_ptr (&branch)[2])
  21250. {
  21251. expression_node_ptr result = error_node();
  21252. // (cob) o c --> cob
  21253. if (details::is_cob_node(branch[0]))
  21254. {
  21255. details::cob_base_node<Type>* cobnode = static_cast<details::cob_base_node<Type>*>(branch[0]);
  21256. const Type c = static_cast<details::literal_node<Type>*>(branch[1])->value();
  21257. bool op_addsub = (details::e_add == cobnode->operation()) ||
  21258. (details::e_sub == cobnode->operation());
  21259. if (op_addsub)
  21260. {
  21261. switch (operation)
  21262. {
  21263. case details::e_add : cobnode->set_c(cobnode->c() + c); break;
  21264. case details::e_sub : cobnode->set_c(cobnode->c() - c); break;
  21265. default : return error_node();
  21266. }
  21267. result = cobnode;
  21268. }
  21269. else if (details::e_mul == cobnode->operation())
  21270. {
  21271. switch (operation)
  21272. {
  21273. case details::e_mul : cobnode->set_c(cobnode->c() * c); break;
  21274. case details::e_div : cobnode->set_c(cobnode->c() / c); break;
  21275. default : return error_node();
  21276. }
  21277. result = cobnode;
  21278. }
  21279. else if (details::e_div == cobnode->operation())
  21280. {
  21281. if (details::e_mul == operation)
  21282. {
  21283. cobnode->set_c(cobnode->c() * c);
  21284. result = cobnode;
  21285. }
  21286. else if (details::e_div == operation)
  21287. {
  21288. result = expr_gen.node_allocator_->
  21289. template allocate_tt<typename details::cob_node<Type,details::div_op<Type> > >
  21290. (cobnode->c() / c,cobnode->move_branch(0));
  21291. free_node(*expr_gen.node_allocator_,branch[0]);
  21292. }
  21293. }
  21294. if (result)
  21295. {
  21296. free_node(*expr_gen.node_allocator_,branch[1]);
  21297. }
  21298. }
  21299. // c o (cob) --> cob
  21300. else if (details::is_cob_node(branch[1]))
  21301. {
  21302. details::cob_base_node<Type>* cobnode = static_cast<details::cob_base_node<Type>*>(branch[1]);
  21303. const Type c = static_cast<details::literal_node<Type>*>(branch[0])->value();
  21304. if (details::e_add == cobnode->operation())
  21305. {
  21306. if (details::e_add == operation)
  21307. {
  21308. cobnode->set_c(c + cobnode->c());
  21309. result = cobnode;
  21310. }
  21311. else if (details::e_sub == operation)
  21312. {
  21313. result = expr_gen.node_allocator_->
  21314. template allocate_tt<typename details::cob_node<Type,details::sub_op<Type> > >
  21315. (c - cobnode->c(),cobnode->move_branch(0));
  21316. free_node(*expr_gen.node_allocator_,branch[1]);
  21317. }
  21318. }
  21319. else if (details::e_sub == cobnode->operation())
  21320. {
  21321. if (details::e_add == operation)
  21322. {
  21323. cobnode->set_c(c + cobnode->c());
  21324. result = cobnode;
  21325. }
  21326. else if (details::e_sub == operation)
  21327. {
  21328. result = expr_gen.node_allocator_->
  21329. template allocate_tt<typename details::cob_node<Type,details::add_op<Type> > >
  21330. (c - cobnode->c(),cobnode->move_branch(0));
  21331. free_node(*expr_gen.node_allocator_,branch[1]);
  21332. }
  21333. }
  21334. else if (details::e_mul == cobnode->operation())
  21335. {
  21336. if (details::e_mul == operation)
  21337. {
  21338. cobnode->set_c(c * cobnode->c());
  21339. result = cobnode;
  21340. }
  21341. else if (details::e_div == operation)
  21342. {
  21343. result = expr_gen.node_allocator_->
  21344. template allocate_tt<typename details::cob_node<Type,details::div_op<Type> > >
  21345. (c / cobnode->c(),cobnode->move_branch(0));
  21346. free_node(*expr_gen.node_allocator_,branch[1]);
  21347. }
  21348. }
  21349. else if (details::e_div == cobnode->operation())
  21350. {
  21351. if (details::e_mul == operation)
  21352. {
  21353. cobnode->set_c(c * cobnode->c());
  21354. result = cobnode;
  21355. }
  21356. else if (details::e_div == operation)
  21357. {
  21358. result = expr_gen.node_allocator_->
  21359. template allocate_tt<typename details::cob_node<Type,details::mul_op<Type> > >
  21360. (c / cobnode->c(),cobnode->move_branch(0));
  21361. free_node(*expr_gen.node_allocator_,branch[1]);
  21362. }
  21363. }
  21364. if (result)
  21365. {
  21366. free_node(*expr_gen.node_allocator_,branch[0]);
  21367. }
  21368. }
  21369. return result;
  21370. }
  21371. };
  21372. struct synthesize_coboc_expression
  21373. {
  21374. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21375. const details::operator_type& operation,
  21376. expression_node_ptr (&branch)[2])
  21377. {
  21378. expression_node_ptr result = error_node();
  21379. // (boc) o c --> boc
  21380. if (details::is_boc_node(branch[0]))
  21381. {
  21382. details::boc_base_node<Type>* bocnode = static_cast<details::boc_base_node<Type>*>(branch[0]);
  21383. const Type c = static_cast<details::literal_node<Type>*>(branch[1])->value();
  21384. if (details::e_add == bocnode->operation())
  21385. {
  21386. switch (operation)
  21387. {
  21388. case details::e_add : bocnode->set_c(bocnode->c() + c); break;
  21389. case details::e_sub : bocnode->set_c(bocnode->c() - c); break;
  21390. default : return error_node();
  21391. }
  21392. result = bocnode;
  21393. }
  21394. else if (details::e_mul == bocnode->operation())
  21395. {
  21396. switch (operation)
  21397. {
  21398. case details::e_mul : bocnode->set_c(bocnode->c() * c); break;
  21399. case details::e_div : bocnode->set_c(bocnode->c() / c); break;
  21400. default : return error_node();
  21401. }
  21402. result = bocnode;
  21403. }
  21404. else if (details::e_sub == bocnode->operation())
  21405. {
  21406. if (details::e_add == operation)
  21407. {
  21408. result = expr_gen.node_allocator_->
  21409. template allocate_tt<typename details::boc_node<Type,details::add_op<Type> > >
  21410. (bocnode->move_branch(0),c - bocnode->c());
  21411. free_node(*expr_gen.node_allocator_,branch[0]);
  21412. }
  21413. else if (details::e_sub == operation)
  21414. {
  21415. bocnode->set_c(bocnode->c() + c);
  21416. result = bocnode;
  21417. }
  21418. }
  21419. else if (details::e_div == bocnode->operation())
  21420. {
  21421. switch (operation)
  21422. {
  21423. case details::e_div : bocnode->set_c(bocnode->c() * c); break;
  21424. case details::e_mul : bocnode->set_c(bocnode->c() / c); break;
  21425. default : return error_node();
  21426. }
  21427. result = bocnode;
  21428. }
  21429. if (result)
  21430. {
  21431. free_node(*expr_gen.node_allocator_,branch[1]);
  21432. }
  21433. }
  21434. // c o (boc) --> boc
  21435. else if (details::is_boc_node(branch[1]))
  21436. {
  21437. details::boc_base_node<Type>* bocnode = static_cast<details::boc_base_node<Type>*>(branch[1]);
  21438. const Type c = static_cast<details::literal_node<Type>*>(branch[0])->value();
  21439. if (details::e_add == bocnode->operation())
  21440. {
  21441. if (details::e_add == operation)
  21442. {
  21443. bocnode->set_c(c + bocnode->c());
  21444. result = bocnode;
  21445. }
  21446. else if (details::e_sub == operation)
  21447. {
  21448. result = expr_gen.node_allocator_->
  21449. template allocate_tt<typename details::cob_node<Type,details::sub_op<Type> > >
  21450. (c - bocnode->c(),bocnode->move_branch(0));
  21451. free_node(*expr_gen.node_allocator_,branch[1]);
  21452. }
  21453. }
  21454. else if (details::e_sub == bocnode->operation())
  21455. {
  21456. if (details::e_add == operation)
  21457. {
  21458. result = expr_gen.node_allocator_->
  21459. template allocate_tt<typename details::boc_node<Type,details::add_op<Type> > >
  21460. (bocnode->move_branch(0),c - bocnode->c());
  21461. free_node(*expr_gen.node_allocator_,branch[1]);
  21462. }
  21463. else if (details::e_sub == operation)
  21464. {
  21465. result = expr_gen.node_allocator_->
  21466. template allocate_tt<typename details::cob_node<Type,details::sub_op<Type> > >
  21467. (c + bocnode->c(),bocnode->move_branch(0));
  21468. free_node(*expr_gen.node_allocator_,branch[1]);
  21469. }
  21470. }
  21471. else if (details::e_mul == bocnode->operation())
  21472. {
  21473. if (details::e_mul == operation)
  21474. {
  21475. bocnode->set_c(c * bocnode->c());
  21476. result = bocnode;
  21477. }
  21478. else if (details::e_div == operation)
  21479. {
  21480. result = expr_gen.node_allocator_->
  21481. template allocate_tt<typename details::cob_node<Type,details::div_op<Type> > >
  21482. (c / bocnode->c(),bocnode->move_branch(0));
  21483. free_node(*expr_gen.node_allocator_,branch[1]);
  21484. }
  21485. }
  21486. else if (details::e_div == bocnode->operation())
  21487. {
  21488. if (details::e_mul == operation)
  21489. {
  21490. bocnode->set_c(bocnode->c() / c);
  21491. result = bocnode;
  21492. }
  21493. else if (details::e_div == operation)
  21494. {
  21495. result = expr_gen.node_allocator_->
  21496. template allocate_tt<typename details::cob_node<Type,details::div_op<Type> > >
  21497. (c * bocnode->c(),bocnode->move_branch(0));
  21498. free_node(*expr_gen.node_allocator_,branch[1]);
  21499. }
  21500. }
  21501. if (result)
  21502. {
  21503. free_node(*expr_gen.node_allocator_,branch[0]);
  21504. }
  21505. }
  21506. return result;
  21507. }
  21508. };
  21509. #ifndef exprtk_disable_enhanced_features
  21510. inline bool synthesize_expression(const details::operator_type& operation,
  21511. expression_node_ptr (&branch)[2],
  21512. expression_node_ptr& result)
  21513. {
  21514. result = error_node();
  21515. if (!operation_optimizable(operation))
  21516. return false;
  21517. const std::string node_id = branch_to_id(branch);
  21518. typename synthesize_map_t::iterator itr = synthesize_map_.find(node_id);
  21519. if (synthesize_map_.end() != itr)
  21520. {
  21521. result = itr->second(*this,operation,branch);
  21522. return true;
  21523. }
  21524. else
  21525. return false;
  21526. }
  21527. struct synthesize_vov_expression
  21528. {
  21529. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21530. const details::operator_type& operation,
  21531. expression_node_ptr (&branch)[2])
  21532. {
  21533. const Type& v1 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  21534. const Type& v2 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  21535. switch (operation)
  21536. {
  21537. #define case_stmt(op0,op1) \
  21538. case op0 : return expr_gen.node_allocator_-> \
  21539. template allocate_rr<typename details::vov_node<Type,op1<Type> > > \
  21540. (v1,v2); \
  21541. basic_opr_switch_statements
  21542. extended_opr_switch_statements
  21543. #undef case_stmt
  21544. default : return error_node();
  21545. }
  21546. }
  21547. };
  21548. struct synthesize_cov_expression
  21549. {
  21550. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21551. const details::operator_type& operation,
  21552. expression_node_ptr (&branch)[2])
  21553. {
  21554. const Type c = static_cast<details::literal_node<Type>*> (branch[0])->value();
  21555. const Type& v = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  21556. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  21557. switch (operation)
  21558. {
  21559. #define case_stmt(op0,op1) \
  21560. case op0 : return expr_gen.node_allocator_-> \
  21561. template allocate_cr<typename details::cov_node<Type,op1<Type> > > \
  21562. (c,v); \
  21563. basic_opr_switch_statements
  21564. extended_opr_switch_statements
  21565. #undef case_stmt
  21566. default : return error_node();
  21567. }
  21568. }
  21569. };
  21570. struct synthesize_voc_expression
  21571. {
  21572. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21573. const details::operator_type& operation,
  21574. expression_node_ptr (&branch)[2])
  21575. {
  21576. const Type& v = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  21577. const Type c = static_cast<details::literal_node<Type>*> (branch[1])->value();
  21578. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  21579. if (expr_gen.cardinal_pow_optimizable(operation,c))
  21580. {
  21581. if (std::equal_to<T>()(T(1),c))
  21582. return branch[0];
  21583. else
  21584. return expr_gen.cardinal_pow_optimization(v,c);
  21585. }
  21586. switch (operation)
  21587. {
  21588. #define case_stmt(op0,op1) \
  21589. case op0 : return expr_gen.node_allocator_-> \
  21590. template allocate_rc<typename details::voc_node<Type,op1<Type> > > \
  21591. (v,c); \
  21592. basic_opr_switch_statements
  21593. extended_opr_switch_statements
  21594. #undef case_stmt
  21595. default : return error_node();
  21596. }
  21597. }
  21598. };
  21599. struct synthesize_sf3ext_expression
  21600. {
  21601. template <typename T0, typename T1, typename T2>
  21602. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21603. const details::operator_type& sf3opr,
  21604. T0 t0, T1 t1, T2 t2)
  21605. {
  21606. switch (sf3opr)
  21607. {
  21608. #define case_stmt(op0,op1) \
  21609. case op0 : return details::T0oT1oT2_sf3ext<T,T0,T1,T2,op1<Type> >:: \
  21610. allocate(*(expr_gen.node_allocator_),t0,t1,t2); \
  21611. case_stmt(details::e_sf00,details::sf00_op) case_stmt(details::e_sf01,details::sf01_op)
  21612. case_stmt(details::e_sf02,details::sf02_op) case_stmt(details::e_sf03,details::sf03_op)
  21613. case_stmt(details::e_sf04,details::sf04_op) case_stmt(details::e_sf05,details::sf05_op)
  21614. case_stmt(details::e_sf06,details::sf06_op) case_stmt(details::e_sf07,details::sf07_op)
  21615. case_stmt(details::e_sf08,details::sf08_op) case_stmt(details::e_sf09,details::sf09_op)
  21616. case_stmt(details::e_sf10,details::sf10_op) case_stmt(details::e_sf11,details::sf11_op)
  21617. case_stmt(details::e_sf12,details::sf12_op) case_stmt(details::e_sf13,details::sf13_op)
  21618. case_stmt(details::e_sf14,details::sf14_op) case_stmt(details::e_sf15,details::sf15_op)
  21619. case_stmt(details::e_sf16,details::sf16_op) case_stmt(details::e_sf17,details::sf17_op)
  21620. case_stmt(details::e_sf18,details::sf18_op) case_stmt(details::e_sf19,details::sf19_op)
  21621. case_stmt(details::e_sf20,details::sf20_op) case_stmt(details::e_sf21,details::sf21_op)
  21622. case_stmt(details::e_sf22,details::sf22_op) case_stmt(details::e_sf23,details::sf23_op)
  21623. case_stmt(details::e_sf24,details::sf24_op) case_stmt(details::e_sf25,details::sf25_op)
  21624. case_stmt(details::e_sf26,details::sf26_op) case_stmt(details::e_sf27,details::sf27_op)
  21625. case_stmt(details::e_sf28,details::sf28_op) case_stmt(details::e_sf29,details::sf29_op)
  21626. case_stmt(details::e_sf30,details::sf30_op)
  21627. #undef case_stmt
  21628. default : return error_node();
  21629. }
  21630. }
  21631. template <typename T0, typename T1, typename T2>
  21632. static inline bool compile(expression_generator<Type>& expr_gen, const std::string& id,
  21633. T0 t0, T1 t1, T2 t2,
  21634. expression_node_ptr& result)
  21635. {
  21636. details::operator_type sf3opr;
  21637. if (!expr_gen.sf3_optimizable(id,sf3opr))
  21638. return false;
  21639. else
  21640. result = synthesize_sf3ext_expression::template process<T0,T1,T2>(expr_gen,sf3opr,t0,t1,t2);
  21641. return true;
  21642. }
  21643. };
  21644. struct synthesize_sf4ext_expression
  21645. {
  21646. template <typename T0, typename T1, typename T2, typename T3>
  21647. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21648. const details::operator_type& sf4opr,
  21649. T0 t0, T1 t1, T2 t2, T3 t3)
  21650. {
  21651. switch (sf4opr)
  21652. {
  21653. #define case_stmt(op0,op1) \
  21654. case op0 : return details::T0oT1oT2oT3_sf4ext<Type,T0,T1,T2,T3,op1<Type> >:: \
  21655. allocate(*(expr_gen.node_allocator_),t0,t1,t2,t3); \
  21656. case_stmt(details::e_sf48,details::sf48_op) case_stmt(details::e_sf49,details::sf49_op)
  21657. case_stmt(details::e_sf50,details::sf50_op) case_stmt(details::e_sf51,details::sf51_op)
  21658. case_stmt(details::e_sf52,details::sf52_op) case_stmt(details::e_sf53,details::sf53_op)
  21659. case_stmt(details::e_sf54,details::sf54_op) case_stmt(details::e_sf55,details::sf55_op)
  21660. case_stmt(details::e_sf56,details::sf56_op) case_stmt(details::e_sf57,details::sf57_op)
  21661. case_stmt(details::e_sf58,details::sf58_op) case_stmt(details::e_sf59,details::sf59_op)
  21662. case_stmt(details::e_sf60,details::sf60_op) case_stmt(details::e_sf61,details::sf61_op)
  21663. case_stmt(details::e_sf62,details::sf62_op) case_stmt(details::e_sf63,details::sf63_op)
  21664. case_stmt(details::e_sf64,details::sf64_op) case_stmt(details::e_sf65,details::sf65_op)
  21665. case_stmt(details::e_sf66,details::sf66_op) case_stmt(details::e_sf67,details::sf67_op)
  21666. case_stmt(details::e_sf68,details::sf68_op) case_stmt(details::e_sf69,details::sf69_op)
  21667. case_stmt(details::e_sf70,details::sf70_op) case_stmt(details::e_sf71,details::sf71_op)
  21668. case_stmt(details::e_sf72,details::sf72_op) case_stmt(details::e_sf73,details::sf73_op)
  21669. case_stmt(details::e_sf74,details::sf74_op) case_stmt(details::e_sf75,details::sf75_op)
  21670. case_stmt(details::e_sf76,details::sf76_op) case_stmt(details::e_sf77,details::sf77_op)
  21671. case_stmt(details::e_sf78,details::sf78_op) case_stmt(details::e_sf79,details::sf79_op)
  21672. case_stmt(details::e_sf80,details::sf80_op) case_stmt(details::e_sf81,details::sf81_op)
  21673. case_stmt(details::e_sf82,details::sf82_op) case_stmt(details::e_sf83,details::sf83_op)
  21674. case_stmt(details::e_sf4ext00,details::sfext00_op) case_stmt(details::e_sf4ext01,details::sfext01_op)
  21675. case_stmt(details::e_sf4ext02,details::sfext02_op) case_stmt(details::e_sf4ext03,details::sfext03_op)
  21676. case_stmt(details::e_sf4ext04,details::sfext04_op) case_stmt(details::e_sf4ext05,details::sfext05_op)
  21677. case_stmt(details::e_sf4ext06,details::sfext06_op) case_stmt(details::e_sf4ext07,details::sfext07_op)
  21678. case_stmt(details::e_sf4ext08,details::sfext08_op) case_stmt(details::e_sf4ext09,details::sfext09_op)
  21679. case_stmt(details::e_sf4ext10,details::sfext10_op) case_stmt(details::e_sf4ext11,details::sfext11_op)
  21680. case_stmt(details::e_sf4ext12,details::sfext12_op) case_stmt(details::e_sf4ext13,details::sfext13_op)
  21681. case_stmt(details::e_sf4ext14,details::sfext14_op) case_stmt(details::e_sf4ext15,details::sfext15_op)
  21682. case_stmt(details::e_sf4ext16,details::sfext16_op) case_stmt(details::e_sf4ext17,details::sfext17_op)
  21683. case_stmt(details::e_sf4ext18,details::sfext18_op) case_stmt(details::e_sf4ext19,details::sfext19_op)
  21684. case_stmt(details::e_sf4ext20,details::sfext20_op) case_stmt(details::e_sf4ext21,details::sfext21_op)
  21685. case_stmt(details::e_sf4ext22,details::sfext22_op) case_stmt(details::e_sf4ext23,details::sfext23_op)
  21686. case_stmt(details::e_sf4ext24,details::sfext24_op) case_stmt(details::e_sf4ext25,details::sfext25_op)
  21687. case_stmt(details::e_sf4ext26,details::sfext26_op) case_stmt(details::e_sf4ext27,details::sfext27_op)
  21688. case_stmt(details::e_sf4ext28,details::sfext28_op) case_stmt(details::e_sf4ext29,details::sfext29_op)
  21689. case_stmt(details::e_sf4ext30,details::sfext30_op) case_stmt(details::e_sf4ext31,details::sfext31_op)
  21690. case_stmt(details::e_sf4ext32,details::sfext32_op) case_stmt(details::e_sf4ext33,details::sfext33_op)
  21691. case_stmt(details::e_sf4ext34,details::sfext34_op) case_stmt(details::e_sf4ext35,details::sfext35_op)
  21692. case_stmt(details::e_sf4ext36,details::sfext36_op) case_stmt(details::e_sf4ext37,details::sfext37_op)
  21693. case_stmt(details::e_sf4ext38,details::sfext38_op) case_stmt(details::e_sf4ext39,details::sfext39_op)
  21694. case_stmt(details::e_sf4ext40,details::sfext40_op) case_stmt(details::e_sf4ext41,details::sfext41_op)
  21695. case_stmt(details::e_sf4ext42,details::sfext42_op) case_stmt(details::e_sf4ext43,details::sfext43_op)
  21696. case_stmt(details::e_sf4ext44,details::sfext44_op) case_stmt(details::e_sf4ext45,details::sfext45_op)
  21697. case_stmt(details::e_sf4ext46,details::sfext46_op) case_stmt(details::e_sf4ext47,details::sfext47_op)
  21698. case_stmt(details::e_sf4ext48,details::sfext48_op) case_stmt(details::e_sf4ext49,details::sfext49_op)
  21699. case_stmt(details::e_sf4ext50,details::sfext50_op) case_stmt(details::e_sf4ext51,details::sfext51_op)
  21700. case_stmt(details::e_sf4ext52,details::sfext52_op) case_stmt(details::e_sf4ext53,details::sfext53_op)
  21701. case_stmt(details::e_sf4ext54,details::sfext54_op) case_stmt(details::e_sf4ext55,details::sfext55_op)
  21702. case_stmt(details::e_sf4ext56,details::sfext56_op) case_stmt(details::e_sf4ext57,details::sfext57_op)
  21703. case_stmt(details::e_sf4ext58,details::sfext58_op) case_stmt(details::e_sf4ext59,details::sfext59_op)
  21704. #undef case_stmt
  21705. default : return error_node();
  21706. }
  21707. }
  21708. template <typename T0, typename T1, typename T2, typename T3>
  21709. static inline bool compile(expression_generator<Type>& expr_gen, const std::string& id,
  21710. T0 t0, T1 t1, T2 t2, T3 t3,
  21711. expression_node_ptr& result)
  21712. {
  21713. details::operator_type sf4opr;
  21714. if (!expr_gen.sf4_optimizable(id,sf4opr))
  21715. return false;
  21716. else
  21717. result = synthesize_sf4ext_expression::template process<T0,T1,T2,T3>(expr_gen,sf4opr,t0,t1,t2,t3);
  21718. return true;
  21719. }
  21720. // T o (sf3ext)
  21721. template <typename ExternalType>
  21722. static inline bool compile_right(expression_generator<Type>& expr_gen,
  21723. ExternalType t,
  21724. const details::operator_type& operation,
  21725. expression_node_ptr& sf3node,
  21726. expression_node_ptr& result)
  21727. {
  21728. if (!details::is_sf3ext_node(sf3node))
  21729. return false;
  21730. typedef details::T0oT1oT2_base_node<Type>* sf3ext_base_ptr;
  21731. sf3ext_base_ptr n = static_cast<sf3ext_base_ptr>(sf3node);
  21732. std::string id = "t" + expr_gen.to_str(operation) + "(" + n->type_id() + ")";
  21733. switch (n->type())
  21734. {
  21735. case details::expression_node<Type>::e_covoc : return compile_right_impl
  21736. <typename covoc_t::sf3_type_node,ExternalType,ctype,vtype,ctype>
  21737. (expr_gen,id,t,sf3node,result);
  21738. case details::expression_node<Type>::e_covov : return compile_right_impl
  21739. <typename covov_t::sf3_type_node,ExternalType,ctype,vtype,vtype>
  21740. (expr_gen,id,t,sf3node,result);
  21741. case details::expression_node<Type>::e_vocov : return compile_right_impl
  21742. <typename vocov_t::sf3_type_node,ExternalType,vtype,ctype,vtype>
  21743. (expr_gen,id,t,sf3node,result);
  21744. case details::expression_node<Type>::e_vovoc : return compile_right_impl
  21745. <typename vovoc_t::sf3_type_node,ExternalType,vtype,vtype,ctype>
  21746. (expr_gen,id,t,sf3node,result);
  21747. case details::expression_node<Type>::e_vovov : return compile_right_impl
  21748. <typename vovov_t::sf3_type_node,ExternalType,vtype,vtype,vtype>
  21749. (expr_gen,id,t,sf3node,result);
  21750. default : return false;
  21751. }
  21752. }
  21753. // (sf3ext) o T
  21754. template <typename ExternalType>
  21755. static inline bool compile_left(expression_generator<Type>& expr_gen,
  21756. ExternalType t,
  21757. const details::operator_type& operation,
  21758. expression_node_ptr& sf3node,
  21759. expression_node_ptr& result)
  21760. {
  21761. if (!details::is_sf3ext_node(sf3node))
  21762. return false;
  21763. typedef details::T0oT1oT2_base_node<Type>* sf3ext_base_ptr;
  21764. sf3ext_base_ptr n = static_cast<sf3ext_base_ptr>(sf3node);
  21765. std::string id = "(" + n->type_id() + ")" + expr_gen.to_str(operation) + "t";
  21766. switch (n->type())
  21767. {
  21768. case details::expression_node<Type>::e_covoc : return compile_left_impl
  21769. <typename covoc_t::sf3_type_node,ExternalType,ctype,vtype,ctype>
  21770. (expr_gen,id,t,sf3node,result);
  21771. case details::expression_node<Type>::e_covov : return compile_left_impl
  21772. <typename covov_t::sf3_type_node,ExternalType,ctype,vtype,vtype>
  21773. (expr_gen,id,t,sf3node,result);
  21774. case details::expression_node<Type>::e_vocov : return compile_left_impl
  21775. <typename vocov_t::sf3_type_node,ExternalType,vtype,ctype,vtype>
  21776. (expr_gen,id,t,sf3node,result);
  21777. case details::expression_node<Type>::e_vovoc : return compile_left_impl
  21778. <typename vovoc_t::sf3_type_node,ExternalType,vtype,vtype,ctype>
  21779. (expr_gen,id,t,sf3node,result);
  21780. case details::expression_node<Type>::e_vovov : return compile_left_impl
  21781. <typename vovov_t::sf3_type_node,ExternalType,vtype,vtype,vtype>
  21782. (expr_gen,id,t,sf3node,result);
  21783. default : return false;
  21784. }
  21785. }
  21786. template <typename SF3TypeNode, typename ExternalType, typename T0, typename T1, typename T2>
  21787. static inline bool compile_right_impl(expression_generator<Type>& expr_gen,
  21788. const std::string& id,
  21789. ExternalType t,
  21790. expression_node_ptr& node,
  21791. expression_node_ptr& result)
  21792. {
  21793. SF3TypeNode* n = dynamic_cast<SF3TypeNode*>(node);
  21794. if (n)
  21795. {
  21796. T0 t0 = n->t0();
  21797. T1 t1 = n->t1();
  21798. T2 t2 = n->t2();
  21799. return synthesize_sf4ext_expression::
  21800. template compile<ExternalType,T0,T1,T2>(expr_gen,id,t,t0,t1,t2,result);
  21801. }
  21802. else
  21803. return false;
  21804. }
  21805. template <typename SF3TypeNode, typename ExternalType, typename T0, typename T1, typename T2>
  21806. static inline bool compile_left_impl(expression_generator<Type>& expr_gen,
  21807. const std::string& id,
  21808. ExternalType t,
  21809. expression_node_ptr& node,
  21810. expression_node_ptr& result)
  21811. {
  21812. SF3TypeNode* n = dynamic_cast<SF3TypeNode*>(node);
  21813. if (n)
  21814. {
  21815. T0 t0 = n->t0();
  21816. T1 t1 = n->t1();
  21817. T2 t2 = n->t2();
  21818. return synthesize_sf4ext_expression::
  21819. template compile<T0,T1,T2,ExternalType>(expr_gen,id,t0,t1,t2,t,result);
  21820. }
  21821. else
  21822. return false;
  21823. }
  21824. };
  21825. struct synthesize_vovov_expression0
  21826. {
  21827. typedef typename vovov_t::type0 node_type;
  21828. typedef typename vovov_t::sf3_type sf3_type;
  21829. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21830. const details::operator_type& operation,
  21831. expression_node_ptr (&branch)[2])
  21832. {
  21833. // (v0 o0 v1) o1 (v2)
  21834. const details::vov_base_node<Type>* vov = static_cast<details::vov_base_node<Type>*>(branch[0]);
  21835. const Type& v0 = vov->v0();
  21836. const Type& v1 = vov->v1();
  21837. const Type& v2 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  21838. const details::operator_type o0 = vov->operation();
  21839. const details::operator_type o1 = operation;
  21840. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  21841. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  21842. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  21843. expression_node_ptr result = error_node();
  21844. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  21845. {
  21846. // (v0 / v1) / v2 --> (vovov) v0 / (v1 * v2)
  21847. if ((details::e_div == o0) && (details::e_div == o1))
  21848. {
  21849. const bool synthesis_result =
  21850. synthesize_sf3ext_expression::
  21851. template compile<vtype,vtype,vtype>(expr_gen,"t/(t*t)",v0,v1,v2,result);
  21852. exprtk_debug(("(v0 / v1) / v2 --> (vovov) v0 / (v1 * v2)\n"));
  21853. return (synthesis_result) ? result : error_node();
  21854. }
  21855. }
  21856. if (synthesize_sf3ext_expression::template compile<vtype,vtype,vtype>(expr_gen,id(expr_gen,o0,o1),v0,v1,v2,result))
  21857. return result;
  21858. else if (!expr_gen.valid_operator(o0,f0))
  21859. return error_node();
  21860. else if (!expr_gen.valid_operator(o1,f1))
  21861. return error_node();
  21862. else
  21863. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,f0,f1);
  21864. }
  21865. static inline std::string id(expression_generator<Type>& expr_gen,
  21866. const details::operator_type o0, const details::operator_type o1)
  21867. {
  21868. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t");
  21869. }
  21870. };
  21871. struct synthesize_vovov_expression1
  21872. {
  21873. typedef typename vovov_t::type1 node_type;
  21874. typedef typename vovov_t::sf3_type sf3_type;
  21875. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21876. const details::operator_type& operation,
  21877. expression_node_ptr (&branch)[2])
  21878. {
  21879. // (v0) o0 (v1 o1 v2)
  21880. const details::vov_base_node<Type>* vov = static_cast<details::vov_base_node<Type>*>(branch[1]);
  21881. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  21882. const Type& v1 = vov->v0();
  21883. const Type& v2 = vov->v1();
  21884. const details::operator_type o0 = operation;
  21885. const details::operator_type o1 = vov->operation();
  21886. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  21887. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  21888. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  21889. expression_node_ptr result = error_node();
  21890. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  21891. {
  21892. // v0 / (v1 / v2) --> (vovov) (v0 * v2) / v1
  21893. if ((details::e_div == o0) && (details::e_div == o1))
  21894. {
  21895. const bool synthesis_result =
  21896. synthesize_sf3ext_expression::
  21897. template compile<vtype,vtype,vtype>(expr_gen,"(t*t)/t",v0,v2,v1,result);
  21898. exprtk_debug(("v0 / (v1 / v2) --> (vovov) (v0 * v2) / v1\n"));
  21899. return (synthesis_result) ? result : error_node();
  21900. }
  21901. }
  21902. if (synthesize_sf3ext_expression::template compile<vtype,vtype,vtype>(expr_gen,id(expr_gen,o0,o1),v0,v1,v2,result))
  21903. return result;
  21904. else if (!expr_gen.valid_operator(o0,f0))
  21905. return error_node();
  21906. else if (!expr_gen.valid_operator(o1,f1))
  21907. return error_node();
  21908. else
  21909. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,f0,f1);
  21910. }
  21911. static inline std::string id(expression_generator<Type>& expr_gen,
  21912. const details::operator_type o0, const details::operator_type o1)
  21913. {
  21914. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)");
  21915. }
  21916. };
  21917. struct synthesize_vovoc_expression0
  21918. {
  21919. typedef typename vovoc_t::type0 node_type;
  21920. typedef typename vovoc_t::sf3_type sf3_type;
  21921. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21922. const details::operator_type& operation,
  21923. expression_node_ptr (&branch)[2])
  21924. {
  21925. // (v0 o0 v1) o1 (c)
  21926. const details::vov_base_node<Type>* vov = static_cast<details::vov_base_node<Type>*>(branch[0]);
  21927. const Type& v0 = vov->v0();
  21928. const Type& v1 = vov->v1();
  21929. const Type c = static_cast<details::literal_node<Type>*>(branch[1])->value();
  21930. const details::operator_type o0 = vov->operation();
  21931. const details::operator_type o1 = operation;
  21932. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  21933. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  21934. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  21935. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  21936. expression_node_ptr result = error_node();
  21937. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  21938. {
  21939. // (v0 / v1) / c --> (vovoc) v0 / (v1 * c)
  21940. if ((details::e_div == o0) && (details::e_div == o1))
  21941. {
  21942. const bool synthesis_result =
  21943. synthesize_sf3ext_expression::
  21944. template compile<vtype,vtype,ctype>(expr_gen,"t/(t*t)",v0,v1,c,result);
  21945. exprtk_debug(("(v0 / v1) / c --> (vovoc) v0 / (v1 * c)\n"));
  21946. return (synthesis_result) ? result : error_node();
  21947. }
  21948. }
  21949. if (synthesize_sf3ext_expression::template compile<vtype,vtype,ctype>(expr_gen,id(expr_gen,o0,o1),v0,v1,c,result))
  21950. return result;
  21951. else if (!expr_gen.valid_operator(o0,f0))
  21952. return error_node();
  21953. else if (!expr_gen.valid_operator(o1,f1))
  21954. return error_node();
  21955. else
  21956. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,f0,f1);
  21957. }
  21958. static inline std::string id(expression_generator<Type>& expr_gen,
  21959. const details::operator_type o0, const details::operator_type o1)
  21960. {
  21961. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t");
  21962. }
  21963. };
  21964. struct synthesize_vovoc_expression1
  21965. {
  21966. typedef typename vovoc_t::type1 node_type;
  21967. typedef typename vovoc_t::sf3_type sf3_type;
  21968. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  21969. const details::operator_type& operation,
  21970. expression_node_ptr (&branch)[2])
  21971. {
  21972. // (v0) o0 (v1 o1 c)
  21973. const details::voc_base_node<Type>* voc = static_cast<const details::voc_base_node<Type>*>(branch[1]);
  21974. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  21975. const Type& v1 = voc->v();
  21976. const Type c = voc->c();
  21977. const details::operator_type o0 = operation;
  21978. const details::operator_type o1 = voc->operation();
  21979. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  21980. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  21981. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  21982. expression_node_ptr result = error_node();
  21983. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  21984. {
  21985. // v0 / (v1 / c) --> (vocov) (v0 * c) / v1
  21986. if ((details::e_div == o0) && (details::e_div == o1))
  21987. {
  21988. const bool synthesis_result =
  21989. synthesize_sf3ext_expression::
  21990. template compile<vtype,ctype,vtype>(expr_gen,"(t*t)/t",v0,c,v1,result);
  21991. exprtk_debug(("v0 / (v1 / c) --> (vocov) (v0 * c) / v1\n"));
  21992. return (synthesis_result) ? result : error_node();
  21993. }
  21994. }
  21995. if (synthesize_sf3ext_expression::template compile<vtype,vtype,ctype>(expr_gen,id(expr_gen,o0,o1),v0,v1,c,result))
  21996. return result;
  21997. else if (!expr_gen.valid_operator(o0,f0))
  21998. return error_node();
  21999. else if (!expr_gen.valid_operator(o1,f1))
  22000. return error_node();
  22001. else
  22002. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,f0,f1);
  22003. }
  22004. static inline std::string id(expression_generator<Type>& expr_gen,
  22005. const details::operator_type o0, const details::operator_type o1)
  22006. {
  22007. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)");
  22008. }
  22009. };
  22010. struct synthesize_vocov_expression0
  22011. {
  22012. typedef typename vocov_t::type0 node_type;
  22013. typedef typename vocov_t::sf3_type sf3_type;
  22014. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22015. const details::operator_type& operation,
  22016. expression_node_ptr (&branch)[2])
  22017. {
  22018. // (v0 o0 c) o1 (v1)
  22019. const details::voc_base_node<Type>* voc = static_cast<details::voc_base_node<Type>*>(branch[0]);
  22020. const Type& v0 = voc->v();
  22021. const Type c = voc->c();
  22022. const Type& v1 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  22023. const details::operator_type o0 = voc->operation();
  22024. const details::operator_type o1 = operation;
  22025. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22026. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22027. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22028. expression_node_ptr result = error_node();
  22029. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22030. {
  22031. // (v0 / c) / v1 --> (vovoc) v0 / (v1 * c)
  22032. if ((details::e_div == o0) && (details::e_div == o1))
  22033. {
  22034. const bool synthesis_result =
  22035. synthesize_sf3ext_expression::
  22036. template compile<vtype,vtype,ctype>(expr_gen,"t/(t*t)",v0,v1,c,result);
  22037. exprtk_debug(("(v0 / c) / v1 --> (vovoc) v0 / (v1 * c)\n"));
  22038. return (synthesis_result) ? result : error_node();
  22039. }
  22040. }
  22041. if (synthesize_sf3ext_expression::template compile<vtype,ctype,vtype>(expr_gen,id(expr_gen,o0,o1),v0,c,v1,result))
  22042. return result;
  22043. else if (!expr_gen.valid_operator(o0,f0))
  22044. return error_node();
  22045. else if (!expr_gen.valid_operator(o1,f1))
  22046. return error_node();
  22047. else
  22048. return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,f0,f1);
  22049. }
  22050. static inline std::string id(expression_generator<Type>& expr_gen,
  22051. const details::operator_type o0, const details::operator_type o1)
  22052. {
  22053. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t");
  22054. }
  22055. };
  22056. struct synthesize_vocov_expression1
  22057. {
  22058. typedef typename vocov_t::type1 node_type;
  22059. typedef typename vocov_t::sf3_type sf3_type;
  22060. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22061. const details::operator_type& operation,
  22062. expression_node_ptr (&branch)[2])
  22063. {
  22064. // (v0) o0 (c o1 v1)
  22065. const details::cov_base_node<Type>* cov = static_cast<details::cov_base_node<Type>*>(branch[1]);
  22066. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  22067. const Type c = cov->c();
  22068. const Type& v1 = cov->v();
  22069. const details::operator_type o0 = operation;
  22070. const details::operator_type o1 = cov->operation();
  22071. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22072. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22073. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22074. expression_node_ptr result = error_node();
  22075. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22076. {
  22077. // v0 / (c / v1) --> (vovoc) (v0 * v1) / c
  22078. if ((details::e_div == o0) && (details::e_div == o1))
  22079. {
  22080. const bool synthesis_result =
  22081. synthesize_sf3ext_expression::
  22082. template compile<vtype,vtype,ctype>(expr_gen,"(t*t)/t",v0,v1,c,result);
  22083. exprtk_debug(("v0 / (c / v1) --> (vovoc) (v0 * v1) / c\n"));
  22084. return (synthesis_result) ? result : error_node();
  22085. }
  22086. }
  22087. if (synthesize_sf3ext_expression::template compile<vtype,ctype,vtype>(expr_gen,id(expr_gen,o0,o1),v0,c,v1,result))
  22088. return result;
  22089. else if (!expr_gen.valid_operator(o0,f0))
  22090. return error_node();
  22091. else if (!expr_gen.valid_operator(o1,f1))
  22092. return error_node();
  22093. else
  22094. return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,f0,f1);
  22095. }
  22096. static inline std::string id(expression_generator<Type>& expr_gen,
  22097. const details::operator_type o0, const details::operator_type o1)
  22098. {
  22099. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)");
  22100. }
  22101. };
  22102. struct synthesize_covov_expression0
  22103. {
  22104. typedef typename covov_t::type0 node_type;
  22105. typedef typename covov_t::sf3_type sf3_type;
  22106. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22107. const details::operator_type& operation,
  22108. expression_node_ptr (&branch)[2])
  22109. {
  22110. // (c o0 v0) o1 (v1)
  22111. const details::cov_base_node<Type>* cov = static_cast<details::cov_base_node<Type>*>(branch[0]);
  22112. const Type c = cov->c();
  22113. const Type& v0 = cov->v();
  22114. const Type& v1 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  22115. const details::operator_type o0 = cov->operation();
  22116. const details::operator_type o1 = operation;
  22117. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22118. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22119. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22120. expression_node_ptr result = error_node();
  22121. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22122. {
  22123. // (c / v0) / v1 --> (covov) c / (v0 * v1)
  22124. if ((details::e_div == o0) && (details::e_div == o1))
  22125. {
  22126. const bool synthesis_result =
  22127. synthesize_sf3ext_expression::
  22128. template compile<ctype,vtype,vtype>(expr_gen,"t/(t*t)",c,v0,v1,result);
  22129. exprtk_debug(("(c / v0) / v1 --> (covov) c / (v0 * v1)\n"));
  22130. return (synthesis_result) ? result : error_node();
  22131. }
  22132. }
  22133. if (synthesize_sf3ext_expression::template compile<ctype,vtype,vtype>(expr_gen,id(expr_gen,o0,o1),c,v0,v1,result))
  22134. return result;
  22135. else if (!expr_gen.valid_operator(o0,f0))
  22136. return error_node();
  22137. else if (!expr_gen.valid_operator(o1,f1))
  22138. return error_node();
  22139. else
  22140. return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,f0,f1);
  22141. }
  22142. static inline std::string id(expression_generator<Type>& expr_gen,
  22143. const details::operator_type o0, const details::operator_type o1)
  22144. {
  22145. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t");
  22146. }
  22147. };
  22148. struct synthesize_covov_expression1
  22149. {
  22150. typedef typename covov_t::type1 node_type;
  22151. typedef typename covov_t::sf3_type sf3_type;
  22152. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22153. const details::operator_type& operation,
  22154. expression_node_ptr (&branch)[2])
  22155. {
  22156. // (c) o0 (v0 o1 v1)
  22157. const details::vov_base_node<Type>* vov = static_cast<details::vov_base_node<Type>*>(branch[1]);
  22158. const Type c = static_cast<details::literal_node<Type>*>(branch[0])->value();
  22159. const Type& v0 = vov->v0();
  22160. const Type& v1 = vov->v1();
  22161. const details::operator_type o0 = operation;
  22162. const details::operator_type o1 = vov->operation();
  22163. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22164. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22165. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22166. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22167. expression_node_ptr result = error_node();
  22168. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22169. {
  22170. // c / (v0 / v1) --> (covov) (c * v1) / v0
  22171. if ((details::e_div == o0) && (details::e_div == o1))
  22172. {
  22173. const bool synthesis_result =
  22174. synthesize_sf3ext_expression::
  22175. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",c,v1,v0,result);
  22176. exprtk_debug(("c / (v0 / v1) --> (covov) (c * v1) / v0\n"));
  22177. return (synthesis_result) ? result : error_node();
  22178. }
  22179. }
  22180. if (synthesize_sf3ext_expression::template compile<ctype,vtype,vtype>(expr_gen,id(expr_gen,o0,o1),c,v0,v1,result))
  22181. return result;
  22182. else if (!expr_gen.valid_operator(o0,f0))
  22183. return error_node();
  22184. else if (!expr_gen.valid_operator(o1,f1))
  22185. return error_node();
  22186. else
  22187. return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,f0,f1);
  22188. }
  22189. static inline std::string id(expression_generator<Type>& expr_gen, const details::operator_type o0, const details::operator_type o1)
  22190. {
  22191. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)");
  22192. }
  22193. };
  22194. struct synthesize_covoc_expression0
  22195. {
  22196. typedef typename covoc_t::type0 node_type;
  22197. typedef typename covoc_t::sf3_type sf3_type;
  22198. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22199. const details::operator_type& operation,
  22200. expression_node_ptr (&branch)[2])
  22201. {
  22202. // (c0 o0 v) o1 (c1)
  22203. const details::cov_base_node<Type>* cov = static_cast<details::cov_base_node<Type>*>(branch[0]);
  22204. const Type c0 = cov->c();
  22205. const Type& v = cov->v();
  22206. const Type c1 = static_cast<details::literal_node<Type>*>(branch[1])->value();
  22207. const details::operator_type o0 = cov->operation();
  22208. const details::operator_type o1 = operation;
  22209. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22210. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22211. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22212. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22213. expression_node_ptr result = error_node();
  22214. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22215. {
  22216. // (c0 + v) + c1 --> (cov) (c0 + c1) + v
  22217. if ((details::e_add == o0) && (details::e_add == o1))
  22218. {
  22219. exprtk_debug(("(c0 + v) + c1 --> (cov) (c0 + c1) + v\n"));
  22220. return expr_gen.node_allocator_->
  22221. template allocate_cr<typename details::cov_node<Type,details::add_op<Type> > >(c0 + c1,v);
  22222. }
  22223. // (c0 + v) - c1 --> (cov) (c0 - c1) + v
  22224. else if ((details::e_add == o0) && (details::e_sub == o1))
  22225. {
  22226. exprtk_debug(("(c0 + v) - c1 --> (cov) (c0 - c1) + v\n"));
  22227. return expr_gen.node_allocator_->
  22228. template allocate_cr<typename details::cov_node<Type,details::add_op<Type> > >(c0 - c1,v);
  22229. }
  22230. // (c0 - v) + c1 --> (cov) (c0 + c1) - v
  22231. else if ((details::e_sub == o0) && (details::e_add == o1))
  22232. {
  22233. exprtk_debug(("(c0 - v) + c1 --> (cov) (c0 + c1) - v\n"));
  22234. return expr_gen.node_allocator_->
  22235. template allocate_cr<typename details::cov_node<Type,details::sub_op<Type> > >(c0 + c1,v);
  22236. }
  22237. // (c0 - v) - c1 --> (cov) (c0 - c1) - v
  22238. else if ((details::e_sub == o0) && (details::e_sub == o1))
  22239. {
  22240. exprtk_debug(("(c0 - v) - c1 --> (cov) (c0 - c1) - v\n"));
  22241. return expr_gen.node_allocator_->
  22242. template allocate_cr<typename details::cov_node<Type,details::sub_op<Type> > >(c0 - c1,v);
  22243. }
  22244. // (c0 * v) * c1 --> (cov) (c0 * c1) * v
  22245. else if ((details::e_mul == o0) && (details::e_mul == o1))
  22246. {
  22247. exprtk_debug(("(c0 * v) * c1 --> (cov) (c0 * c1) * v\n"));
  22248. return expr_gen.node_allocator_->
  22249. template allocate_cr<typename details::cov_node<Type,details::mul_op<Type> > >(c0 * c1,v);
  22250. }
  22251. // (c0 * v) / c1 --> (cov) (c0 / c1) * v
  22252. else if ((details::e_mul == o0) && (details::e_div == o1))
  22253. {
  22254. exprtk_debug(("(c0 * v) / c1 --> (cov) (c0 / c1) * v\n"));
  22255. return expr_gen.node_allocator_->
  22256. template allocate_cr<typename details::cov_node<Type,details::mul_op<Type> > >(c0 / c1,v);
  22257. }
  22258. // (c0 / v) * c1 --> (cov) (c0 * c1) / v
  22259. else if ((details::e_div == o0) && (details::e_mul == o1))
  22260. {
  22261. exprtk_debug(("(c0 / v) * c1 --> (cov) (c0 * c1) / v\n"));
  22262. return expr_gen.node_allocator_->
  22263. template allocate_cr<typename details::cov_node<Type,details::div_op<Type> > >(c0 * c1,v);
  22264. }
  22265. // (c0 / v) / c1 --> (cov) (c0 / c1) / v
  22266. else if ((details::e_div == o0) && (details::e_div == o1))
  22267. {
  22268. exprtk_debug(("(c0 / v) / c1 --> (cov) (c0 / c1) / v\n"));
  22269. return expr_gen.node_allocator_->
  22270. template allocate_cr<typename details::cov_node<Type,details::div_op<Type> > >(c0 / c1,v);
  22271. }
  22272. }
  22273. if (synthesize_sf3ext_expression::template compile<ctype,vtype,ctype>(expr_gen,id(expr_gen,o0,o1),c0,v,c1,result))
  22274. return result;
  22275. else if (!expr_gen.valid_operator(o0,f0))
  22276. return error_node();
  22277. else if (!expr_gen.valid_operator(o1,f1))
  22278. return error_node();
  22279. else
  22280. return node_type::allocate(*(expr_gen.node_allocator_),c0,v,c1,f0,f1);
  22281. }
  22282. static inline std::string id(expression_generator<Type>& expr_gen,
  22283. const details::operator_type o0, const details::operator_type o1)
  22284. {
  22285. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t");
  22286. }
  22287. };
  22288. struct synthesize_covoc_expression1
  22289. {
  22290. typedef typename covoc_t::type1 node_type;
  22291. typedef typename covoc_t::sf3_type sf3_type;
  22292. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22293. const details::operator_type& operation,
  22294. expression_node_ptr (&branch)[2])
  22295. {
  22296. // (c0) o0 (v o1 c1)
  22297. const details::voc_base_node<Type>* voc = static_cast<details::voc_base_node<Type>*>(branch[1]);
  22298. const Type c0 = static_cast<details::literal_node<Type>*>(branch[0])->value();
  22299. const Type& v = voc->v();
  22300. const Type c1 = voc->c();
  22301. const details::operator_type o0 = operation;
  22302. const details::operator_type o1 = voc->operation();
  22303. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22304. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22305. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22306. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22307. expression_node_ptr result = error_node();
  22308. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22309. {
  22310. // (c0) + (v + c1) --> (cov) (c0 + c1) + v
  22311. if ((details::e_add == o0) && (details::e_add == o1))
  22312. {
  22313. exprtk_debug(("(c0) + (v + c1) --> (cov) (c0 + c1) + v\n"));
  22314. return expr_gen.node_allocator_->
  22315. template allocate_cr<typename details::cov_node<Type,details::add_op<Type> > >(c0 + c1,v);
  22316. }
  22317. // (c0) + (v - c1) --> (cov) (c0 - c1) + v
  22318. else if ((details::e_add == o0) && (details::e_sub == o1))
  22319. {
  22320. exprtk_debug(("(c0) + (v - c1) --> (cov) (c0 - c1) + v\n"));
  22321. return expr_gen.node_allocator_->
  22322. template allocate_cr<typename details::cov_node<Type,details::add_op<Type> > >(c0 - c1,v);
  22323. }
  22324. // (c0) - (v + c1) --> (cov) (c0 - c1) - v
  22325. else if ((details::e_sub == o0) && (details::e_add == o1))
  22326. {
  22327. exprtk_debug(("(c0) - (v + c1) --> (cov) (c0 - c1) - v\n"));
  22328. return expr_gen.node_allocator_->
  22329. template allocate_cr<typename details::cov_node<Type,details::sub_op<Type> > >(c0 - c1,v);
  22330. }
  22331. // (c0) - (v - c1) --> (cov) (c0 + c1) - v
  22332. else if ((details::e_sub == o0) && (details::e_sub == o1))
  22333. {
  22334. exprtk_debug(("(c0) - (v - c1) --> (cov) (c0 + c1) - v\n"));
  22335. return expr_gen.node_allocator_->
  22336. template allocate_cr<typename details::cov_node<Type,details::sub_op<Type> > >(c0 + c1,v);
  22337. }
  22338. // (c0) * (v * c1) --> (voc) v * (c0 * c1)
  22339. else if ((details::e_mul == o0) && (details::e_mul == o1))
  22340. {
  22341. exprtk_debug(("(c0) * (v * c1) --> (voc) v * (c0 * c1)\n"));
  22342. return expr_gen.node_allocator_->
  22343. template allocate_cr<typename details::cov_node<Type,details::mul_op<Type> > >(c0 * c1,v);
  22344. }
  22345. // (c0) * (v / c1) --> (cov) (c0 / c1) * v
  22346. else if ((details::e_mul == o0) && (details::e_div == o1))
  22347. {
  22348. exprtk_debug(("(c0) * (v / c1) --> (cov) (c0 / c1) * v\n"));
  22349. return expr_gen.node_allocator_->
  22350. template allocate_cr<typename details::cov_node<Type,details::mul_op<Type> > >(c0 / c1,v);
  22351. }
  22352. // (c0) / (v * c1) --> (cov) (c0 / c1) / v
  22353. else if ((details::e_div == o0) && (details::e_mul == o1))
  22354. {
  22355. exprtk_debug(("(c0) / (v * c1) --> (cov) (c0 / c1) / v\n"));
  22356. return expr_gen.node_allocator_->
  22357. template allocate_cr<typename details::cov_node<Type,details::div_op<Type> > >(c0 / c1,v);
  22358. }
  22359. // (c0) / (v / c1) --> (cov) (c0 * c1) / v
  22360. else if ((details::e_div == o0) && (details::e_div == o1))
  22361. {
  22362. exprtk_debug(("(c0) / (v / c1) --> (cov) (c0 * c1) / v\n"));
  22363. return expr_gen.node_allocator_->
  22364. template allocate_cr<typename details::cov_node<Type,details::div_op<Type> > >(c0 * c1,v);
  22365. }
  22366. }
  22367. if (synthesize_sf3ext_expression::template compile<ctype,vtype,ctype>(expr_gen,id(expr_gen,o0,o1),c0,v,c1,result))
  22368. return result;
  22369. else if (!expr_gen.valid_operator(o0,f0))
  22370. return error_node();
  22371. else if (!expr_gen.valid_operator(o1,f1))
  22372. return error_node();
  22373. else
  22374. return node_type::allocate(*(expr_gen.node_allocator_),c0,v,c1,f0,f1);
  22375. }
  22376. static inline std::string id(expression_generator<Type>& expr_gen,
  22377. const details::operator_type o0, const details::operator_type o1)
  22378. {
  22379. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)");
  22380. }
  22381. };
  22382. struct synthesize_cocov_expression0
  22383. {
  22384. typedef typename cocov_t::type0 node_type;
  22385. static inline expression_node_ptr process(expression_generator<Type>&, const details::operator_type&, expression_node_ptr (&)[2])
  22386. {
  22387. // (c0 o0 c1) o1 (v) - Not possible.
  22388. return error_node();
  22389. }
  22390. };
  22391. struct synthesize_cocov_expression1
  22392. {
  22393. typedef typename cocov_t::type1 node_type;
  22394. typedef typename cocov_t::sf3_type sf3_type;
  22395. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22396. const details::operator_type& operation,
  22397. expression_node_ptr (&branch)[2])
  22398. {
  22399. // (c0) o0 (c1 o1 v)
  22400. const details::cov_base_node<Type>* cov = static_cast<details::cov_base_node<Type>*>(branch[1]);
  22401. const Type c0 = static_cast<details::literal_node<Type>*>(branch[0])->value();
  22402. const Type c1 = cov->c();
  22403. const Type& v = cov->v();
  22404. const details::operator_type o0 = operation;
  22405. const details::operator_type o1 = cov->operation();
  22406. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22407. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22408. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22409. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22410. expression_node_ptr result = error_node();
  22411. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22412. {
  22413. // (c0) + (c1 + v) --> (cov) (c0 + c1) + v
  22414. if ((details::e_add == o0) && (details::e_add == o1))
  22415. {
  22416. exprtk_debug(("(c0) + (c1 + v) --> (cov) (c0 + c1) + v\n"));
  22417. return expr_gen.node_allocator_->
  22418. template allocate_cr<typename details::cov_node<Type,details::add_op<Type> > >(c0 + c1,v);
  22419. }
  22420. // (c0) + (c1 - v) --> (cov) (c0 + c1) - v
  22421. else if ((details::e_add == o0) && (details::e_sub == o1))
  22422. {
  22423. exprtk_debug(("(c0) + (c1 - v) --> (cov) (c0 + c1) - v\n"));
  22424. return expr_gen.node_allocator_->
  22425. template allocate_cr<typename details::cov_node<Type,details::sub_op<Type> > >(c0 + c1,v);
  22426. }
  22427. // (c0) - (c1 + v) --> (cov) (c0 - c1) - v
  22428. else if ((details::e_sub == o0) && (details::e_add == o1))
  22429. {
  22430. exprtk_debug(("(c0) - (c1 + v) --> (cov) (c0 - c1) - v\n"));
  22431. return expr_gen.node_allocator_->
  22432. template allocate_cr<typename details::cov_node<Type,details::sub_op<Type> > >(c0 - c1,v);
  22433. }
  22434. // (c0) - (c1 - v) --> (cov) (c0 - c1) + v
  22435. else if ((details::e_sub == o0) && (details::e_sub == o1))
  22436. {
  22437. exprtk_debug(("(c0) - (c1 - v) --> (cov) (c0 - c1) + v\n"));
  22438. return expr_gen.node_allocator_->
  22439. template allocate_cr<typename details::cov_node<Type,details::add_op<Type> > >(c0 - c1,v);
  22440. }
  22441. // (c0) * (c1 * v) --> (cov) (c0 * c1) * v
  22442. else if ((details::e_mul == o0) && (details::e_mul == o1))
  22443. {
  22444. exprtk_debug(("(c0) * (c1 * v) --> (cov) (c0 * c1) * v\n"));
  22445. return expr_gen.node_allocator_->
  22446. template allocate_cr<typename details::cov_node<Type,details::mul_op<Type> > >(c0 * c1,v);
  22447. }
  22448. // (c0) * (c1 / v) --> (cov) (c0 * c1) / v
  22449. else if ((details::e_mul == o0) && (details::e_div == o1))
  22450. {
  22451. exprtk_debug(("(c0) * (c1 / v) --> (cov) (c0 * c1) / v\n"));
  22452. return expr_gen.node_allocator_->
  22453. template allocate_cr<typename details::cov_node<Type,details::div_op<Type> > >(c0 * c1,v);
  22454. }
  22455. // (c0) / (c1 * v) --> (cov) (c0 / c1) / v
  22456. else if ((details::e_div == o0) && (details::e_mul == o1))
  22457. {
  22458. exprtk_debug(("(c0) / (c1 * v) --> (cov) (c0 / c1) / v\n"));
  22459. return expr_gen.node_allocator_->
  22460. template allocate_cr<typename details::cov_node<Type,details::div_op<Type> > >(c0 / c1,v);
  22461. }
  22462. // (c0) / (c1 / v) --> (cov) (c0 / c1) * v
  22463. else if ((details::e_div == o0) && (details::e_div == o1))
  22464. {
  22465. exprtk_debug(("(c0) / (c1 / v) --> (cov) (c0 / c1) * v\n"));
  22466. return expr_gen.node_allocator_->
  22467. template allocate_cr<typename details::cov_node<Type,details::mul_op<Type> > >(c0 / c1,v);
  22468. }
  22469. }
  22470. if (synthesize_sf3ext_expression::template compile<ctype,ctype,vtype>(expr_gen,id(expr_gen,o0,o1),c0,c1,v,result))
  22471. return result;
  22472. else if (!expr_gen.valid_operator(o0,f0))
  22473. return error_node();
  22474. else if (!expr_gen.valid_operator(o1,f1))
  22475. return error_node();
  22476. else
  22477. return node_type::allocate(*(expr_gen.node_allocator_),c0,c1,v,f0,f1);
  22478. }
  22479. static inline std::string id(expression_generator<Type>& expr_gen, const details::operator_type o0, const details::operator_type o1)
  22480. {
  22481. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)");
  22482. }
  22483. };
  22484. struct synthesize_vococ_expression0
  22485. {
  22486. typedef typename vococ_t::type0 node_type;
  22487. typedef typename vococ_t::sf3_type sf3_type;
  22488. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22489. const details::operator_type& operation,
  22490. expression_node_ptr (&branch)[2])
  22491. {
  22492. // (v o0 c0) o1 (c1)
  22493. const details::voc_base_node<Type>* voc = static_cast<details::voc_base_node<Type>*>(branch[0]);
  22494. const Type& v = voc->v();
  22495. const Type& c0 = voc->c();
  22496. const Type& c1 = static_cast<details::literal_node<Type>*>(branch[1])->value();
  22497. const details::operator_type o0 = voc->operation();
  22498. const details::operator_type o1 = operation;
  22499. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22500. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22501. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22502. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22503. expression_node_ptr result = error_node();
  22504. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22505. {
  22506. // (v + c0) + c1 --> (voc) v + (c0 + c1)
  22507. if ((details::e_add == o0) && (details::e_add == o1))
  22508. {
  22509. exprtk_debug(("(v + c0) + c1 --> (voc) v + (c0 + c1)\n"));
  22510. return expr_gen.node_allocator_->
  22511. template allocate_rc<typename details::voc_node<Type,details::add_op<Type> > >(v,c0 + c1);
  22512. }
  22513. // (v + c0) - c1 --> (voc) v + (c0 - c1)
  22514. else if ((details::e_add == o0) && (details::e_sub == o1))
  22515. {
  22516. exprtk_debug(("(v + c0) - c1 --> (voc) v + (c0 - c1)\n"));
  22517. return expr_gen.node_allocator_->
  22518. template allocate_rc<typename details::voc_node<Type,details::add_op<Type> > >(v,c0 - c1);
  22519. }
  22520. // (v - c0) + c1 --> (voc) v - (c0 + c1)
  22521. else if ((details::e_sub == o0) && (details::e_add == o1))
  22522. {
  22523. exprtk_debug(("(v - c0) + c1 --> (voc) v - (c0 + c1)\n"));
  22524. return expr_gen.node_allocator_->
  22525. template allocate_rc<typename details::voc_node<Type,details::add_op<Type> > >(v,c1 - c0);
  22526. }
  22527. // (v - c0) - c1 --> (voc) v - (c0 + c1)
  22528. else if ((details::e_sub == o0) && (details::e_sub == o1))
  22529. {
  22530. exprtk_debug(("(v - c0) - c1 --> (voc) v - (c0 + c1)\n"));
  22531. return expr_gen.node_allocator_->
  22532. template allocate_rc<typename details::voc_node<Type,details::sub_op<Type> > >(v,c0 + c1);
  22533. }
  22534. // (v * c0) * c1 --> (voc) v * (c0 * c1)
  22535. else if ((details::e_mul == o0) && (details::e_mul == o1))
  22536. {
  22537. exprtk_debug(("(v * c0) * c1 --> (voc) v * (c0 * c1)\n"));
  22538. return expr_gen.node_allocator_->
  22539. template allocate_rc<typename details::voc_node<Type,details::mul_op<Type> > >(v,c0 * c1);
  22540. }
  22541. // (v * c0) / c1 --> (voc) v * (c0 / c1)
  22542. else if ((details::e_mul == o0) && (details::e_div == o1))
  22543. {
  22544. exprtk_debug(("(v * c0) / c1 --> (voc) v * (c0 / c1)\n"));
  22545. return expr_gen.node_allocator_->
  22546. template allocate_rc<typename details::voc_node<Type,details::mul_op<Type> > >(v,c0 / c1);
  22547. }
  22548. // (v / c0) * c1 --> (voc) v * (c1 / c0)
  22549. else if ((details::e_div == o0) && (details::e_mul == o1))
  22550. {
  22551. exprtk_debug(("(v / c0) * c1 --> (voc) v * (c1 / c0)\n"));
  22552. return expr_gen.node_allocator_->
  22553. template allocate_rc<typename details::voc_node<Type,details::mul_op<Type> > >(v,c1 / c0);
  22554. }
  22555. // (v / c0) / c1 --> (voc) v / (c0 * c1)
  22556. else if ((details::e_div == o0) && (details::e_div == o1))
  22557. {
  22558. exprtk_debug(("(v / c0) / c1 --> (voc) v / (c0 * c1)\n"));
  22559. return expr_gen.node_allocator_->
  22560. template allocate_rc<typename details::voc_node<Type,details::div_op<Type> > >(v,c0 * c1);
  22561. }
  22562. }
  22563. if (synthesize_sf3ext_expression::template compile<vtype,ctype,ctype>(expr_gen,id(expr_gen,o0,o1),v,c0,c1,result))
  22564. return result;
  22565. else if (!expr_gen.valid_operator(o0,f0))
  22566. return error_node();
  22567. else if (!expr_gen.valid_operator(o1,f1))
  22568. return error_node();
  22569. else
  22570. return node_type::allocate(*(expr_gen.node_allocator_),v,c0,c1,f0,f1);
  22571. }
  22572. static inline std::string id(expression_generator<Type>& expr_gen,
  22573. const details::operator_type o0, const details::operator_type o1)
  22574. {
  22575. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t");
  22576. }
  22577. };
  22578. struct synthesize_vococ_expression1
  22579. {
  22580. typedef typename vococ_t::type0 node_type;
  22581. static inline expression_node_ptr process(expression_generator<Type>&, const details::operator_type&, expression_node_ptr (&)[2])
  22582. {
  22583. // (v) o0 (c0 o1 c1) - Not possible.
  22584. exprtk_debug(("(v) o0 (c0 o1 c1) - Not possible.\n"));
  22585. return error_node();
  22586. }
  22587. };
  22588. struct synthesize_vovovov_expression0
  22589. {
  22590. typedef typename vovovov_t::type0 node_type;
  22591. typedef typename vovovov_t::sf4_type sf4_type;
  22592. typedef typename node_type::T0 T0;
  22593. typedef typename node_type::T1 T1;
  22594. typedef typename node_type::T2 T2;
  22595. typedef typename node_type::T3 T3;
  22596. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22597. const details::operator_type& operation,
  22598. expression_node_ptr (&branch)[2])
  22599. {
  22600. // (v0 o0 v1) o1 (v2 o2 v3)
  22601. const details::vov_base_node<Type>* vov0 = static_cast<details::vov_base_node<Type>*>(branch[0]);
  22602. const details::vov_base_node<Type>* vov1 = static_cast<details::vov_base_node<Type>*>(branch[1]);
  22603. const Type& v0 = vov0->v0();
  22604. const Type& v1 = vov0->v1();
  22605. const Type& v2 = vov1->v0();
  22606. const Type& v3 = vov1->v1();
  22607. const details::operator_type o0 = vov0->operation();
  22608. const details::operator_type o1 = operation;
  22609. const details::operator_type o2 = vov1->operation();
  22610. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22611. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22612. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  22613. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22614. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22615. expression_node_ptr result = error_node();
  22616. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22617. {
  22618. // (v0 / v1) * (v2 / v3) --> (vovovov) (v0 * v2) / (v1 * v3)
  22619. if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_div == o2))
  22620. {
  22621. const bool synthesis_result =
  22622. synthesize_sf4ext_expression::
  22623. template compile<vtype,vtype,vtype,vtype>(expr_gen,"(t*t)/(t*t)",v0,v2,v1,v3,result);
  22624. exprtk_debug(("(v0 / v1) * (v2 / v3) --> (vovovov) (v0 * v2) / (v1 * v3)\n"));
  22625. return (synthesis_result) ? result : error_node();
  22626. }
  22627. // (v0 / v1) / (v2 / v3) --> (vovovov) (v0 * v3) / (v1 * v2)
  22628. else if ((details::e_div == o0) && (details::e_div == o1) && (details::e_div == o2))
  22629. {
  22630. const bool synthesis_result =
  22631. synthesize_sf4ext_expression::
  22632. template compile<vtype,vtype,vtype,vtype>(expr_gen,"(t*t)/(t*t)",v0,v3,v1,v2,result);
  22633. exprtk_debug(("(v0 / v1) / (v2 / v3) --> (vovovov) (v0 * v3) / (v1 * v2)\n"));
  22634. return (synthesis_result) ? result : error_node();
  22635. }
  22636. }
  22637. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result))
  22638. return result;
  22639. else if (!expr_gen.valid_operator(o0,f0))
  22640. return error_node();
  22641. else if (!expr_gen.valid_operator(o1,f1))
  22642. return error_node();
  22643. else if (!expr_gen.valid_operator(o2,f2))
  22644. return error_node();
  22645. else
  22646. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,v3,f0,f1,f2);
  22647. }
  22648. static inline std::string id(expression_generator<Type>& expr_gen,
  22649. const details::operator_type o0,
  22650. const details::operator_type o1,
  22651. const details::operator_type o2)
  22652. {
  22653. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t)");
  22654. }
  22655. };
  22656. struct synthesize_vovovoc_expression0
  22657. {
  22658. typedef typename vovovoc_t::type0 node_type;
  22659. typedef typename vovovoc_t::sf4_type sf4_type;
  22660. typedef typename node_type::T0 T0;
  22661. typedef typename node_type::T1 T1;
  22662. typedef typename node_type::T2 T2;
  22663. typedef typename node_type::T3 T3;
  22664. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22665. const details::operator_type& operation,
  22666. expression_node_ptr (&branch)[2])
  22667. {
  22668. // (v0 o0 v1) o1 (v2 o2 c)
  22669. const details::vov_base_node<Type>* vov = static_cast<details::vov_base_node<Type>*>(branch[0]);
  22670. const details::voc_base_node<Type>* voc = static_cast<details::voc_base_node<Type>*>(branch[1]);
  22671. const Type& v0 = vov->v0();
  22672. const Type& v1 = vov->v1();
  22673. const Type& v2 = voc->v ();
  22674. const Type c = voc->c ();
  22675. const details::operator_type o0 = vov->operation();
  22676. const details::operator_type o1 = operation;
  22677. const details::operator_type o2 = voc->operation();
  22678. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22679. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22680. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  22681. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22682. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22683. expression_node_ptr result = error_node();
  22684. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22685. {
  22686. // (v0 / v1) * (v2 / c) --> (vovovoc) (v0 * v2) / (v1 * c)
  22687. if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_div == o2))
  22688. {
  22689. const bool synthesis_result =
  22690. synthesize_sf4ext_expression::
  22691. template compile<vtype,vtype,vtype,ctype>(expr_gen,"(t*t)/(t*t)",v0,v2,v1,c,result);
  22692. exprtk_debug(("(v0 / v1) * (v2 / c) --> (vovovoc) (v0 * v2) / (v1 * c)\n"));
  22693. return (synthesis_result) ? result : error_node();
  22694. }
  22695. // (v0 / v1) / (v2 / c) --> (vocovov) (v0 * c) / (v1 * v2)
  22696. if ((details::e_div == o0) && (details::e_div == o1) && (details::e_div == o2))
  22697. {
  22698. const bool synthesis_result =
  22699. synthesize_sf4ext_expression::
  22700. template compile<vtype,ctype,vtype,vtype>(expr_gen,"(t*t)/(t*t)",v0,c,v1,v2,result);
  22701. exprtk_debug(("(v0 / v1) / (v2 / c) --> (vocovov) (v0 * c) / (v1 * v2)\n"));
  22702. return (synthesis_result) ? result : error_node();
  22703. }
  22704. }
  22705. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result))
  22706. return result;
  22707. else if (!expr_gen.valid_operator(o0,f0))
  22708. return error_node();
  22709. else if (!expr_gen.valid_operator(o1,f1))
  22710. return error_node();
  22711. else if (!expr_gen.valid_operator(o2,f2))
  22712. return error_node();
  22713. else
  22714. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,c,f0,f1,f2);
  22715. }
  22716. static inline std::string id(expression_generator<Type>& expr_gen,
  22717. const details::operator_type o0,
  22718. const details::operator_type o1,
  22719. const details::operator_type o2)
  22720. {
  22721. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t)");
  22722. }
  22723. };
  22724. struct synthesize_vovocov_expression0
  22725. {
  22726. typedef typename vovocov_t::type0 node_type;
  22727. typedef typename vovocov_t::sf4_type sf4_type;
  22728. typedef typename node_type::T0 T0;
  22729. typedef typename node_type::T1 T1;
  22730. typedef typename node_type::T2 T2;
  22731. typedef typename node_type::T3 T3;
  22732. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22733. const details::operator_type& operation,
  22734. expression_node_ptr (&branch)[2])
  22735. {
  22736. // (v0 o0 v1) o1 (c o2 v2)
  22737. const details::vov_base_node<Type>* vov = static_cast<details::vov_base_node<Type>*>(branch[0]);
  22738. const details::cov_base_node<Type>* cov = static_cast<details::cov_base_node<Type>*>(branch[1]);
  22739. const Type& v0 = vov->v0();
  22740. const Type& v1 = vov->v1();
  22741. const Type& v2 = cov->v ();
  22742. const Type c = cov->c ();
  22743. const details::operator_type o0 = vov->operation();
  22744. const details::operator_type o1 = operation;
  22745. const details::operator_type o2 = cov->operation();
  22746. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22747. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22748. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  22749. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22750. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22751. expression_node_ptr result = error_node();
  22752. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22753. {
  22754. // (v0 / v1) * (c / v2) --> (vocovov) (v0 * c) / (v1 * v2)
  22755. if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_div == o2))
  22756. {
  22757. const bool synthesis_result =
  22758. synthesize_sf4ext_expression::
  22759. template compile<vtype,ctype,vtype,vtype>(expr_gen,"(t*t)/(t*t)",v0,c,v1,v2,result);
  22760. exprtk_debug(("(v0 / v1) * (c / v2) --> (vocovov) (v0 * c) / (v1 * v2)\n"));
  22761. return (synthesis_result) ? result : error_node();
  22762. }
  22763. // (v0 / v1) / (c / v2) --> (vovovoc) (v0 * v2) / (v1 * c)
  22764. if ((details::e_div == o0) && (details::e_div == o1) && (details::e_div == o2))
  22765. {
  22766. const bool synthesis_result =
  22767. synthesize_sf4ext_expression::
  22768. template compile<vtype,vtype,vtype,ctype>(expr_gen,"(t*t)/(t*t)",v0,v2,v1,c,result);
  22769. exprtk_debug(("(v0 / v1) / (c / v2) --> (vovovoc) (v0 * v2) / (v1 * c)\n"));
  22770. return (synthesis_result) ? result : error_node();
  22771. }
  22772. }
  22773. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,c,v2,result))
  22774. return result;
  22775. else if (!expr_gen.valid_operator(o0,f0))
  22776. return error_node();
  22777. else if (!expr_gen.valid_operator(o1,f1))
  22778. return error_node();
  22779. else if (!expr_gen.valid_operator(o2,f2))
  22780. return error_node();
  22781. else
  22782. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,v2,f0,f1,f2);
  22783. }
  22784. static inline std::string id(expression_generator<Type>& expr_gen,
  22785. const details::operator_type o0,
  22786. const details::operator_type o1,
  22787. const details::operator_type o2)
  22788. {
  22789. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t)");
  22790. }
  22791. };
  22792. struct synthesize_vocovov_expression0
  22793. {
  22794. typedef typename vocovov_t::type0 node_type;
  22795. typedef typename vocovov_t::sf4_type sf4_type;
  22796. typedef typename node_type::T0 T0;
  22797. typedef typename node_type::T1 T1;
  22798. typedef typename node_type::T2 T2;
  22799. typedef typename node_type::T3 T3;
  22800. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22801. const details::operator_type& operation,
  22802. expression_node_ptr (&branch)[2])
  22803. {
  22804. // (v0 o0 c) o1 (v1 o2 v2)
  22805. const details::voc_base_node<Type>* voc = static_cast<details::voc_base_node<Type>*>(branch[0]);
  22806. const details::vov_base_node<Type>* vov = static_cast<details::vov_base_node<Type>*>(branch[1]);
  22807. const Type c = voc->c ();
  22808. const Type& v0 = voc->v ();
  22809. const Type& v1 = vov->v0();
  22810. const Type& v2 = vov->v1();
  22811. const details::operator_type o0 = voc->operation();
  22812. const details::operator_type o1 = operation;
  22813. const details::operator_type o2 = vov->operation();
  22814. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22815. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22816. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  22817. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22818. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22819. expression_node_ptr result = error_node();
  22820. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22821. {
  22822. // (v0 / c) * (v1 / v2) --> (vovocov) (v0 * v1) / (c * v2)
  22823. if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_div == o2))
  22824. {
  22825. const bool synthesis_result =
  22826. synthesize_sf4ext_expression::
  22827. template compile<vtype,vtype,ctype,vtype>(expr_gen,"(t*t)/(t*t)",v0,v1,c,v2,result);
  22828. exprtk_debug(("(v0 / c) * (v1 / v2) --> (vovocov) (v0 * v1) / (c * v2)\n"));
  22829. return (synthesis_result) ? result : error_node();
  22830. }
  22831. // (v0 / c) / (v1 / v2) --> (vovocov) (v0 * v2) / (c * v1)
  22832. if ((details::e_div == o0) && (details::e_div == o1) && (details::e_div == o2))
  22833. {
  22834. const bool synthesis_result =
  22835. synthesize_sf4ext_expression::
  22836. template compile<vtype,vtype,ctype,vtype>(expr_gen,"(t*t)/(t*t)",v0,v2,c,v1,result);
  22837. exprtk_debug(("(v0 / c) / (v1 / v2) --> (vovocov) (v0 * v2) / (c * v1)\n"));
  22838. return (synthesis_result) ? result : error_node();
  22839. }
  22840. }
  22841. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result))
  22842. return result;
  22843. else if (!expr_gen.valid_operator(o0,f0))
  22844. return error_node();
  22845. else if (!expr_gen.valid_operator(o1,f1))
  22846. return error_node();
  22847. else if (!expr_gen.valid_operator(o2,f2))
  22848. return error_node();
  22849. else
  22850. return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,v2,f0,f1,f2);
  22851. }
  22852. static inline std::string id(expression_generator<Type>& expr_gen,
  22853. const details::operator_type o0,
  22854. const details::operator_type o1,
  22855. const details::operator_type o2)
  22856. {
  22857. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t)");
  22858. }
  22859. };
  22860. struct synthesize_covovov_expression0
  22861. {
  22862. typedef typename covovov_t::type0 node_type;
  22863. typedef typename covovov_t::sf4_type sf4_type;
  22864. typedef typename node_type::T0 T0;
  22865. typedef typename node_type::T1 T1;
  22866. typedef typename node_type::T2 T2;
  22867. typedef typename node_type::T3 T3;
  22868. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22869. const details::operator_type& operation,
  22870. expression_node_ptr (&branch)[2])
  22871. {
  22872. // (c o0 v0) o1 (v1 o2 v2)
  22873. const details::cov_base_node<Type>* cov = static_cast<details::cov_base_node<Type>*>(branch[0]);
  22874. const details::vov_base_node<Type>* vov = static_cast<details::vov_base_node<Type>*>(branch[1]);
  22875. const Type c = cov->c ();
  22876. const Type& v0 = cov->v ();
  22877. const Type& v1 = vov->v0();
  22878. const Type& v2 = vov->v1();
  22879. const details::operator_type o0 = cov->operation();
  22880. const details::operator_type o1 = operation;
  22881. const details::operator_type o2 = vov->operation();
  22882. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22883. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22884. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  22885. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22886. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22887. expression_node_ptr result = error_node();
  22888. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22889. {
  22890. // (c / v0) * (v1 / v2) --> (covovov) (c * v1) / (v0 * v2)
  22891. if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_div == o2))
  22892. {
  22893. const bool synthesis_result =
  22894. synthesize_sf4ext_expression::
  22895. template compile<ctype,vtype,vtype,vtype>(expr_gen,"(t*t)/(t*t)",c,v1,v0,v2,result);
  22896. exprtk_debug(("(c / v0) * (v1 / v2) --> (covovov) (c * v1) / (v0 * v2)\n"));
  22897. return (synthesis_result) ? result : error_node();
  22898. }
  22899. // (c / v0) / (v1 / v2) --> (covovov) (c * v2) / (v0 * v1)
  22900. if ((details::e_div == o0) && (details::e_div == o1) && (details::e_div == o2))
  22901. {
  22902. const bool synthesis_result =
  22903. synthesize_sf4ext_expression::
  22904. template compile<ctype,vtype,vtype,vtype>(expr_gen,"(t*t)/(t*t)",c,v2,v0,v1,result);
  22905. exprtk_debug(("(c / v0) / (v1 / v2) --> (covovov) (c * v2) / (v0 * v1)\n"));
  22906. return (synthesis_result) ? result : error_node();
  22907. }
  22908. }
  22909. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c,v0,v1,v2,result))
  22910. return result;
  22911. else if (!expr_gen.valid_operator(o0,f0))
  22912. return error_node();
  22913. else if (!expr_gen.valid_operator(o1,f1))
  22914. return error_node();
  22915. else if (!expr_gen.valid_operator(o2,f2))
  22916. return error_node();
  22917. else
  22918. return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,v2,f0,f1,f2);
  22919. }
  22920. static inline std::string id(expression_generator<Type>& expr_gen,
  22921. const details::operator_type o0,
  22922. const details::operator_type o1,
  22923. const details::operator_type o2)
  22924. {
  22925. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t)");
  22926. }
  22927. };
  22928. struct synthesize_covocov_expression0
  22929. {
  22930. typedef typename covocov_t::type0 node_type;
  22931. typedef typename covocov_t::sf4_type sf4_type;
  22932. typedef typename node_type::T0 T0;
  22933. typedef typename node_type::T1 T1;
  22934. typedef typename node_type::T2 T2;
  22935. typedef typename node_type::T3 T3;
  22936. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  22937. const details::operator_type& operation,
  22938. expression_node_ptr (&branch)[2])
  22939. {
  22940. // (c0 o0 v0) o1 (c1 o2 v1)
  22941. const details::cov_base_node<Type>* cov0 = static_cast<details::cov_base_node<Type>*>(branch[0]);
  22942. const details::cov_base_node<Type>* cov1 = static_cast<details::cov_base_node<Type>*>(branch[1]);
  22943. const Type c0 = cov0->c();
  22944. const Type& v0 = cov0->v();
  22945. const Type c1 = cov1->c();
  22946. const Type& v1 = cov1->v();
  22947. const details::operator_type o0 = cov0->operation();
  22948. const details::operator_type o1 = operation;
  22949. const details::operator_type o2 = cov1->operation();
  22950. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  22951. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  22952. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  22953. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  22954. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  22955. expression_node_ptr result = error_node();
  22956. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  22957. {
  22958. // (c0 + v0) + (c1 + v1) --> (covov) (c0 + c1) + v0 + v1
  22959. if ((details::e_add == o0) && (details::e_add == o1) && (details::e_add == o2))
  22960. {
  22961. const bool synthesis_result =
  22962. synthesize_sf3ext_expression::
  22963. template compile<ctype,vtype,vtype>(expr_gen,"(t+t)+t",(c0 + c1),v0,v1,result);
  22964. exprtk_debug(("(c0 + v0) + (c1 + v1) --> (covov) (c0 + c1) + v0 + v1\n"));
  22965. return (synthesis_result) ? result : error_node();
  22966. }
  22967. // (c0 + v0) - (c1 + v1) --> (covov) (c0 - c1) + v0 - v1
  22968. else if ((details::e_add == o0) && (details::e_sub == o1) && (details::e_add == o2))
  22969. {
  22970. const bool synthesis_result =
  22971. synthesize_sf3ext_expression::
  22972. template compile<ctype,vtype,vtype>(expr_gen,"(t+t)-t",(c0 - c1),v0,v1,result);
  22973. exprtk_debug(("(c0 + v0) - (c1 + v1) --> (covov) (c0 - c1) + v0 - v1\n"));
  22974. return (synthesis_result) ? result : error_node();
  22975. }
  22976. // (c0 - v0) - (c1 - v1) --> (covov) (c0 - c1) - v0 + v1
  22977. else if ((details::e_sub == o0) && (details::e_sub == o1) && (details::e_sub == o2))
  22978. {
  22979. const bool synthesis_result =
  22980. synthesize_sf3ext_expression::
  22981. template compile<ctype,vtype,vtype>(expr_gen,"(t-t)+t",(c0 - c1),v0,v1,result);
  22982. exprtk_debug(("(c0 - v0) - (c1 - v1) --> (covov) (c0 - c1) - v0 + v1\n"));
  22983. return (synthesis_result) ? result : error_node();
  22984. }
  22985. // (c0 * v0) * (c1 * v1) --> (covov) (c0 * c1) * v0 * v1
  22986. else if ((details::e_mul == o0) && (details::e_mul == o1) && (details::e_mul == o2))
  22987. {
  22988. const bool synthesis_result =
  22989. synthesize_sf3ext_expression::
  22990. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)*t",(c0 * c1),v0,v1,result);
  22991. exprtk_debug(("(c0 * v0) * (c1 * v1) --> (covov) (c0 * c1) * v0 * v1\n"));
  22992. return (synthesis_result) ? result : error_node();
  22993. }
  22994. // (c0 * v0) / (c1 * v1) --> (covov) (c0 / c1) * (v0 / v1)
  22995. else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_mul == o2))
  22996. {
  22997. const bool synthesis_result =
  22998. synthesize_sf3ext_expression::
  22999. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",(c0 / c1),v0,v1,result);
  23000. exprtk_debug(("(c0 * v0) / (c1 * v1) --> (covov) (c0 / c1) * (v0 / v1)\n"));
  23001. return (synthesis_result) ? result : error_node();
  23002. }
  23003. // (c0 / v0) * (c1 / v1) --> (covov) (c0 * c1) / (v0 * v1)
  23004. else if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_div == o2))
  23005. {
  23006. const bool synthesis_result =
  23007. synthesize_sf3ext_expression::
  23008. template compile<ctype,vtype,vtype>(expr_gen,"t/(t*t)",(c0 * c1),v0,v1,result);
  23009. exprtk_debug(("(c0 / v0) * (c1 / v1) --> (covov) (c0 * c1) / (v0 * v1)\n"));
  23010. return (synthesis_result) ? result : error_node();
  23011. }
  23012. // (c0 / v0) / (c1 / v1) --> (covov) ((c0 / c1) * v1) / v0
  23013. else if ((details::e_div == o0) && (details::e_div == o1) && (details::e_div == o2))
  23014. {
  23015. const bool synthesis_result =
  23016. synthesize_sf3ext_expression::
  23017. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",(c0 / c1),v1,v0,result);
  23018. exprtk_debug(("(c0 / v0) / (c1 / v1) --> (covov) ((c0 / c1) * v1) / v0\n"));
  23019. return (synthesis_result) ? result : error_node();
  23020. }
  23021. // (c0 * v0) / (c1 / v1) --> (covov) (c0 / c1) * (v0 * v1)
  23022. else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_div == o2))
  23023. {
  23024. const bool synthesis_result =
  23025. synthesize_sf3ext_expression::
  23026. template compile<ctype,vtype,vtype>(expr_gen,"t*(t*t)",(c0 / c1),v0,v1,result);
  23027. exprtk_debug(("(c0 * v0) / (c1 / v1) --> (covov) (c0 / c1) * (v0 * v1)\n"));
  23028. return (synthesis_result) ? result : error_node();
  23029. }
  23030. // (c0 / v0) / (c1 * v1) --> (covov) (c0 / c1) / (v0 * v1)
  23031. else if ((details::e_div == o0) && (details::e_div == o1) && (details::e_mul == o2))
  23032. {
  23033. const bool synthesis_result =
  23034. synthesize_sf3ext_expression::
  23035. template compile<ctype,vtype,vtype>(expr_gen,"t/(t*t)",(c0 / c1),v0,v1,result);
  23036. exprtk_debug(("(c0 / v0) / (c1 * v1) --> (covov) (c0 / c1) / (v0 * v1)\n"));
  23037. return (synthesis_result) ? result : error_node();
  23038. }
  23039. // (c * v0) +/- (c * v1) --> (covov) c * (v0 +/- v1)
  23040. else if (
  23041. (std::equal_to<T>()(c0,c1)) &&
  23042. (details::e_mul == o0) &&
  23043. (details::e_mul == o2) &&
  23044. (
  23045. (details::e_add == o1) ||
  23046. (details::e_sub == o1)
  23047. )
  23048. )
  23049. {
  23050. std::string specfunc;
  23051. switch (o1)
  23052. {
  23053. case details::e_add : specfunc = "t*(t+t)"; break;
  23054. case details::e_sub : specfunc = "t*(t-t)"; break;
  23055. default : return error_node();
  23056. }
  23057. const bool synthesis_result =
  23058. synthesize_sf3ext_expression::
  23059. template compile<ctype,vtype,vtype>(expr_gen,specfunc,c0,v0,v1,result);
  23060. exprtk_debug(("(c * v0) +/- (c * v1) --> (covov) c * (v0 +/- v1)\n"));
  23061. return (synthesis_result) ? result : error_node();
  23062. }
  23063. }
  23064. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result))
  23065. return result;
  23066. else if (!expr_gen.valid_operator(o0,f0))
  23067. return error_node();
  23068. else if (!expr_gen.valid_operator(o1,f1))
  23069. return error_node();
  23070. else if (!expr_gen.valid_operator(o2,f2))
  23071. return error_node();
  23072. else
  23073. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,c1,v1,f0,f1,f2);
  23074. }
  23075. static inline std::string id(expression_generator<Type>& expr_gen,
  23076. const details::operator_type o0,
  23077. const details::operator_type o1,
  23078. const details::operator_type o2)
  23079. {
  23080. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t)");
  23081. }
  23082. };
  23083. struct synthesize_vocovoc_expression0
  23084. {
  23085. typedef typename vocovoc_t::type0 node_type;
  23086. typedef typename vocovoc_t::sf4_type sf4_type;
  23087. typedef typename node_type::T0 T0;
  23088. typedef typename node_type::T1 T1;
  23089. typedef typename node_type::T2 T2;
  23090. typedef typename node_type::T3 T3;
  23091. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23092. const details::operator_type& operation,
  23093. expression_node_ptr (&branch)[2])
  23094. {
  23095. // (v0 o0 c0) o1 (v1 o2 c1)
  23096. const details::voc_base_node<Type>* voc0 = static_cast<details::voc_base_node<Type>*>(branch[0]);
  23097. const details::voc_base_node<Type>* voc1 = static_cast<details::voc_base_node<Type>*>(branch[1]);
  23098. const Type c0 = voc0->c();
  23099. const Type& v0 = voc0->v();
  23100. const Type c1 = voc1->c();
  23101. const Type& v1 = voc1->v();
  23102. const details::operator_type o0 = voc0->operation();
  23103. const details::operator_type o1 = operation;
  23104. const details::operator_type o2 = voc1->operation();
  23105. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23106. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  23107. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  23108. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  23109. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23110. expression_node_ptr result = error_node();
  23111. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  23112. {
  23113. // (v0 + c0) + (v1 + c1) --> (covov) (c0 + c1) + v0 + v1
  23114. if ((details::e_add == o0) && (details::e_add == o1) && (details::e_add == o2))
  23115. {
  23116. const bool synthesis_result =
  23117. synthesize_sf3ext_expression::
  23118. template compile<ctype,vtype,vtype>(expr_gen,"(t+t)+t",(c0 + c1),v0,v1,result);
  23119. exprtk_debug(("(v0 + c0) + (v1 + c1) --> (covov) (c0 + c1) + v0 + v1\n"));
  23120. return (synthesis_result) ? result : error_node();
  23121. }
  23122. // (v0 + c0) - (v1 + c1) --> (covov) (c0 - c1) + v0 - v1
  23123. else if ((details::e_add == o0) && (details::e_sub == o1) && (details::e_add == o2))
  23124. {
  23125. const bool synthesis_result =
  23126. synthesize_sf3ext_expression::
  23127. template compile<ctype,vtype,vtype>(expr_gen,"(t+t)-t",(c0 - c1),v0,v1,result);
  23128. exprtk_debug(("(v0 + c0) - (v1 + c1) --> (covov) (c0 - c1) + v0 - v1\n"));
  23129. return (synthesis_result) ? result : error_node();
  23130. }
  23131. // (v0 - c0) - (v1 - c1) --> (covov) (c1 - c0) + v0 - v1
  23132. else if ((details::e_sub == o0) && (details::e_sub == o1) && (details::e_sub == o2))
  23133. {
  23134. const bool synthesis_result =
  23135. synthesize_sf3ext_expression::
  23136. template compile<ctype,vtype,vtype>(expr_gen,"(t+t)-t",(c1 - c0),v0,v1,result);
  23137. exprtk_debug(("(v0 - c0) - (v1 - c1) --> (covov) (c1 - c0) + v0 - v1\n"));
  23138. return (synthesis_result) ? result : error_node();
  23139. }
  23140. // (v0 * c0) * (v1 * c1) --> (covov) (c0 * c1) * v0 * v1
  23141. else if ((details::e_mul == o0) && (details::e_mul == o1) && (details::e_mul == o2))
  23142. {
  23143. const bool synthesis_result =
  23144. synthesize_sf3ext_expression::
  23145. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)*t",(c0 * c1),v0,v1,result);
  23146. exprtk_debug(("(v0 * c0) * (v1 * c1) --> (covov) (c0 * c1) * v0 * v1\n"));
  23147. return (synthesis_result) ? result : error_node();
  23148. }
  23149. // (v0 * c0) / (v1 * c1) --> (covov) (c0 / c1) * (v0 / v1)
  23150. else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_mul == o2))
  23151. {
  23152. const bool synthesis_result =
  23153. synthesize_sf3ext_expression::
  23154. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",(c0 / c1),v0,v1,result);
  23155. exprtk_debug(("(v0 * c0) / (v1 * c1) --> (covov) (c0 / c1) * (v0 / v1)\n"));
  23156. return (synthesis_result) ? result : error_node();
  23157. }
  23158. // (v0 / c0) * (v1 / c1) --> (covov) (1 / (c0 * c1)) * v0 * v1
  23159. else if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_div == o2))
  23160. {
  23161. const bool synthesis_result =
  23162. synthesize_sf3ext_expression::
  23163. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)*t",Type(1) / (c0 * c1),v0,v1,result);
  23164. exprtk_debug(("(v0 / c0) * (v1 / c1) --> (covov) (1 / (c0 * c1)) * v0 * v1\n"));
  23165. return (synthesis_result) ? result : error_node();
  23166. }
  23167. // (v0 / c0) / (v1 / c1) --> (covov) ((c1 / c0) * v0) / v1
  23168. else if ((details::e_div == o0) && (details::e_div == o1) && (details::e_div == o2))
  23169. {
  23170. const bool synthesis_result =
  23171. synthesize_sf3ext_expression::
  23172. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",(c1 / c0),v0,v1,result);
  23173. exprtk_debug(("(v0 / c0) / (v1 / c1) --> (covov) ((c1 / c0) * v0) / v1\n"));
  23174. return (synthesis_result) ? result : error_node();
  23175. }
  23176. // (v0 * c0) / (v1 / c1) --> (covov) (c0 * c1) * (v0 / v1)
  23177. else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_div == o2))
  23178. {
  23179. const bool synthesis_result =
  23180. synthesize_sf3ext_expression::
  23181. template compile<ctype,vtype,vtype>(expr_gen,"t*(t/t)",(c0 * c1),v0,v1,result);
  23182. exprtk_debug(("(v0 * c0) / (v1 / c1) --> (covov) (c0 * c1) * (v0 / v1)\n"));
  23183. return (synthesis_result) ? result : error_node();
  23184. }
  23185. // (v0 / c0) / (v1 * c1) --> (covov) (1 / (c0 * c1)) * v0 / v1
  23186. else if ((details::e_div == o0) && (details::e_div == o1) && (details::e_mul == o2))
  23187. {
  23188. const bool synthesis_result =
  23189. synthesize_sf3ext_expression::
  23190. template compile<ctype,vtype,vtype>(expr_gen,"t*(t/t)",Type(1) / (c0 * c1),v0,v1,result);
  23191. exprtk_debug(("(v0 / c0) / (v1 * c1) --> (covov) (1 / (c0 * c1)) * v0 / v1\n"));
  23192. return (synthesis_result) ? result : error_node();
  23193. }
  23194. // (v0 / c0) * (v1 + c1) --> (vocovoc) (v0 * (1 / c0)) * (v1 + c1)
  23195. else if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_add == o2))
  23196. {
  23197. const bool synthesis_result =
  23198. synthesize_sf4ext_expression::
  23199. template compile<vtype,ctype,vtype,ctype>(expr_gen,"(t*t)*(t+t)",v0,T(1) / c0,v1,c1,result);
  23200. exprtk_debug(("(v0 / c0) * (v1 + c1) --> (vocovoc) (v0 * (1 / c0)) * (v1 + c1)\n"));
  23201. return (synthesis_result) ? result : error_node();
  23202. }
  23203. // (v0 / c0) * (v1 - c1) --> (vocovoc) (v0 * (1 / c0)) * (v1 - c1)
  23204. else if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_sub == o2))
  23205. {
  23206. const bool synthesis_result =
  23207. synthesize_sf4ext_expression::
  23208. template compile<vtype,ctype,vtype,ctype>(expr_gen,"(t*t)*(t-t)",v0,T(1) / c0,v1,c1,result);
  23209. exprtk_debug(("(v0 / c0) * (v1 - c1) --> (vocovoc) (v0 * (1 / c0)) * (v1 - c1)\n"));
  23210. return (synthesis_result) ? result : error_node();
  23211. }
  23212. // (v0 * c) +/- (v1 * c) --> (covov) c * (v0 +/- v1)
  23213. else if (
  23214. (std::equal_to<T>()(c0,c1)) &&
  23215. (details::e_mul == o0) &&
  23216. (details::e_mul == o2) &&
  23217. (
  23218. (details::e_add == o1) ||
  23219. (details::e_sub == o1)
  23220. )
  23221. )
  23222. {
  23223. std::string specfunc;
  23224. switch (o1)
  23225. {
  23226. case details::e_add : specfunc = "t*(t+t)"; break;
  23227. case details::e_sub : specfunc = "t*(t-t)"; break;
  23228. default : return error_node();
  23229. }
  23230. const bool synthesis_result =
  23231. synthesize_sf3ext_expression::
  23232. template compile<ctype,vtype,vtype>(expr_gen,specfunc,c0,v0,v1,result);
  23233. exprtk_debug(("(v0 * c) +/- (v1 * c) --> (covov) c * (v0 +/- v1)\n"));
  23234. return (synthesis_result) ? result : error_node();
  23235. }
  23236. // (v0 / c) +/- (v1 / c) --> (vovoc) (v0 +/- v1) / c
  23237. else if (
  23238. (std::equal_to<T>()(c0,c1)) &&
  23239. (details::e_div == o0) &&
  23240. (details::e_div == o2) &&
  23241. (
  23242. (details::e_add == o1) ||
  23243. (details::e_sub == o1)
  23244. )
  23245. )
  23246. {
  23247. std::string specfunc;
  23248. switch (o1)
  23249. {
  23250. case details::e_add : specfunc = "(t+t)/t"; break;
  23251. case details::e_sub : specfunc = "(t-t)/t"; break;
  23252. default : return error_node();
  23253. }
  23254. const bool synthesis_result =
  23255. synthesize_sf3ext_expression::
  23256. template compile<ctype,vtype,vtype>(expr_gen,specfunc,c0,v0,v1,result);
  23257. exprtk_debug(("(v0 / c) +/- (v1 / c) --> (vovoc) (v0 +/- v1) / c\n"));
  23258. return (synthesis_result) ? result : error_node();
  23259. }
  23260. }
  23261. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result))
  23262. return result;
  23263. else if (!expr_gen.valid_operator(o0,f0))
  23264. return error_node();
  23265. else if (!expr_gen.valid_operator(o1,f1))
  23266. return error_node();
  23267. else if (!expr_gen.valid_operator(o2,f2))
  23268. return error_node();
  23269. else
  23270. return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,v1,c1,f0,f1,f2);
  23271. }
  23272. static inline std::string id(expression_generator<Type>& expr_gen,
  23273. const details::operator_type o0,
  23274. const details::operator_type o1,
  23275. const details::operator_type o2)
  23276. {
  23277. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t)");
  23278. }
  23279. };
  23280. struct synthesize_covovoc_expression0
  23281. {
  23282. typedef typename covovoc_t::type0 node_type;
  23283. typedef typename covovoc_t::sf4_type sf4_type;
  23284. typedef typename node_type::T0 T0;
  23285. typedef typename node_type::T1 T1;
  23286. typedef typename node_type::T2 T2;
  23287. typedef typename node_type::T3 T3;
  23288. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23289. const details::operator_type& operation,
  23290. expression_node_ptr (&branch)[2])
  23291. {
  23292. // (c0 o0 v0) o1 (v1 o2 c1)
  23293. const details::cov_base_node<Type>* cov = static_cast<details::cov_base_node<Type>*>(branch[0]);
  23294. const details::voc_base_node<Type>* voc = static_cast<details::voc_base_node<Type>*>(branch[1]);
  23295. const Type c0 = cov->c();
  23296. const Type& v0 = cov->v();
  23297. const Type c1 = voc->c();
  23298. const Type& v1 = voc->v();
  23299. const details::operator_type o0 = cov->operation();
  23300. const details::operator_type o1 = operation;
  23301. const details::operator_type o2 = voc->operation();
  23302. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23303. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  23304. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  23305. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  23306. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23307. expression_node_ptr result = error_node();
  23308. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  23309. {
  23310. // (c0 + v0) + (v1 + c1) --> (covov) (c0 + c1) + v0 + v1
  23311. if ((details::e_add == o0) && (details::e_add == o1) && (details::e_add == o2))
  23312. {
  23313. const bool synthesis_result =
  23314. synthesize_sf3ext_expression::
  23315. template compile<ctype,vtype,vtype>(expr_gen,"(t+t)+t",(c0 + c1),v0,v1,result);
  23316. exprtk_debug(("(c0 + v0) + (v1 + c1) --> (covov) (c0 + c1) + v0 + v1\n"));
  23317. return (synthesis_result) ? result : error_node();
  23318. }
  23319. // (c0 + v0) - (v1 + c1) --> (covov) (c0 - c1) + v0 - v1
  23320. else if ((details::e_add == o0) && (details::e_sub == o1) && (details::e_add == o2))
  23321. {
  23322. const bool synthesis_result =
  23323. synthesize_sf3ext_expression::
  23324. template compile<ctype,vtype,vtype>(expr_gen,"(t+t)-t",(c0 - c1),v0,v1,result);
  23325. exprtk_debug(("(c0 + v0) - (v1 + c1) --> (covov) (c0 - c1) + v0 - v1\n"));
  23326. return (synthesis_result) ? result : error_node();
  23327. }
  23328. // (c0 - v0) - (v1 - c1) --> (covov) (c0 + c1) - v0 - v1
  23329. else if ((details::e_sub == o0) && (details::e_sub == o1) && (details::e_sub == o2))
  23330. {
  23331. const bool synthesis_result =
  23332. synthesize_sf3ext_expression::
  23333. template compile<ctype,vtype,vtype>(expr_gen,"t-(t+t)",(c0 + c1),v0,v1,result);
  23334. exprtk_debug(("(c0 - v0) - (v1 - c1) --> (covov) (c0 + c1) - v0 - v1\n"));
  23335. return (synthesis_result) ? result : error_node();
  23336. }
  23337. // (c0 * v0) * (v1 * c1) --> (covov) (c0 * c1) * v0 * v1
  23338. else if ((details::e_mul == o0) && (details::e_mul == o1) && (details::e_mul == o2))
  23339. {
  23340. const bool synthesis_result =
  23341. synthesize_sf3ext_expression::
  23342. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)*t",(c0 * c1),v0,v1,result);
  23343. exprtk_debug(("(c0 * v0) * (v1 * c1) --> (covov) (c0 * c1) * v0 * v1\n"));
  23344. return (synthesis_result) ? result : error_node();
  23345. }
  23346. // (c0 * v0) / (v1 * c1) --> (covov) (c0 / c1) * (v0 / v1)
  23347. else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_mul == o2))
  23348. {
  23349. const bool synthesis_result =
  23350. synthesize_sf3ext_expression::
  23351. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",(c0 / c1),v0,v1,result);
  23352. exprtk_debug(("(c0 * v0) / (v1 * c1) --> (covov) (c0 / c1) * (v0 / v1)\n"));
  23353. return (synthesis_result) ? result : error_node();
  23354. }
  23355. // (c0 / v0) * (v1 / c1) --> (covov) (c0 / c1) * (v1 / v0)
  23356. else if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_div == o2))
  23357. {
  23358. const bool synthesis_result =
  23359. synthesize_sf3ext_expression::
  23360. template compile<ctype,vtype,vtype>(expr_gen,"t*(t/t)",(c0 / c1),v1,v0,result);
  23361. exprtk_debug(("(c0 / v0) * (v1 / c1) --> (covov) (c0 / c1) * (v1 / v0)\n"));
  23362. return (synthesis_result) ? result : error_node();
  23363. }
  23364. // (c0 / v0) / (v1 / c1) --> (covov) (c0 * c1) / (v0 * v1)
  23365. else if ((details::e_div == o0) && (details::e_div == o1) && (details::e_div == o2))
  23366. {
  23367. const bool synthesis_result =
  23368. synthesize_sf3ext_expression::
  23369. template compile<ctype,vtype,vtype>(expr_gen,"t/(t*t)",(c0 * c1),v0,v1,result);
  23370. exprtk_debug(("(c0 / v0) / (v1 / c1) --> (covov) (c0 * c1) / (v0 * v1)\n"));
  23371. return (synthesis_result) ? result : error_node();
  23372. }
  23373. // (c0 * v0) / (v1 / c1) --> (covov) (c0 * c1) * (v0 / v1)
  23374. else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_div == o2))
  23375. {
  23376. const bool synthesis_result =
  23377. synthesize_sf3ext_expression::
  23378. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",(c0 * c1),v0,v1,result);
  23379. exprtk_debug(("(c0 * v0) / (v1 / c1) --> (covov) (c0 * c1) * (v0 / v1)\n"));
  23380. return (synthesis_result) ? result : error_node();
  23381. }
  23382. // (c0 / v0) / (v1 * c1) --> (covov) (c0 / c1) / (v0 * v1)
  23383. else if ((details::e_div == o0) && (details::e_div == o1) && (details::e_mul == o2))
  23384. {
  23385. const bool synthesis_result =
  23386. synthesize_sf3ext_expression::
  23387. template compile<ctype,vtype,vtype>(expr_gen,"t/(t*t)",(c0 / c1),v0,v1,result);
  23388. exprtk_debug(("(c0 / v0) / (v1 * c1) --> (covov) (c0 / c1) / (v0 * v1)\n"));
  23389. return (synthesis_result) ? result : error_node();
  23390. }
  23391. // (c * v0) +/- (v1 * c) --> (covov) c * (v0 +/- v1)
  23392. else if (
  23393. (std::equal_to<T>()(c0,c1)) &&
  23394. (details::e_mul == o0) &&
  23395. (details::e_mul == o2) &&
  23396. (
  23397. (details::e_add == o1) ||
  23398. (details::e_sub == o1)
  23399. )
  23400. )
  23401. {
  23402. std::string specfunc;
  23403. switch (o1)
  23404. {
  23405. case details::e_add : specfunc = "t*(t+t)"; break;
  23406. case details::e_sub : specfunc = "t*(t-t)"; break;
  23407. default : return error_node();
  23408. }
  23409. const bool synthesis_result =
  23410. synthesize_sf3ext_expression::
  23411. template compile<ctype,vtype,vtype>(expr_gen,specfunc,c0,v0,v1,result);
  23412. exprtk_debug(("(c * v0) +/- (v1 * c) --> (covov) c * (v0 +/- v1)\n"));
  23413. return (synthesis_result) ? result : error_node();
  23414. }
  23415. }
  23416. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result))
  23417. return result;
  23418. else if (!expr_gen.valid_operator(o0,f0))
  23419. return error_node();
  23420. else if (!expr_gen.valid_operator(o1,f1))
  23421. return error_node();
  23422. else if (!expr_gen.valid_operator(o2,f2))
  23423. return error_node();
  23424. else
  23425. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,v1,c1,f0,f1,f2);
  23426. }
  23427. static inline std::string id(expression_generator<Type>& expr_gen,
  23428. const details::operator_type o0,
  23429. const details::operator_type o1,
  23430. const details::operator_type o2)
  23431. {
  23432. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t)");
  23433. }
  23434. };
  23435. struct synthesize_vococov_expression0
  23436. {
  23437. typedef typename vococov_t::type0 node_type;
  23438. typedef typename vococov_t::sf4_type sf4_type;
  23439. typedef typename node_type::T0 T0;
  23440. typedef typename node_type::T1 T1;
  23441. typedef typename node_type::T2 T2;
  23442. typedef typename node_type::T3 T3;
  23443. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23444. const details::operator_type& operation,
  23445. expression_node_ptr (&branch)[2])
  23446. {
  23447. // (v0 o0 c0) o1 (c1 o2 v1)
  23448. const details::voc_base_node<Type>* voc = static_cast<details::voc_base_node<Type>*>(branch[0]);
  23449. const details::cov_base_node<Type>* cov = static_cast<details::cov_base_node<Type>*>(branch[1]);
  23450. const Type c0 = voc->c();
  23451. const Type& v0 = voc->v();
  23452. const Type c1 = cov->c();
  23453. const Type& v1 = cov->v();
  23454. const details::operator_type o0 = voc->operation();
  23455. const details::operator_type o1 = operation;
  23456. const details::operator_type o2 = cov->operation();
  23457. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23458. binary_functor_t f1 = reinterpret_cast<binary_functor_t>(0);
  23459. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  23460. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  23461. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23462. expression_node_ptr result = error_node();
  23463. if (expr_gen.parser_->settings_.strength_reduction_enabled())
  23464. {
  23465. // (v0 + c0) + (c1 + v1) --> (covov) (c0 + c1) + v0 + v1
  23466. if ((details::e_add == o0) && (details::e_add == o1) && (details::e_add == o2))
  23467. {
  23468. const bool synthesis_result =
  23469. synthesize_sf3ext_expression::
  23470. template compile<ctype,vtype,vtype>(expr_gen,"(t+t)+t",(c0 + c1),v0,v1,result);
  23471. exprtk_debug(("(v0 + c0) + (c1 + v1) --> (covov) (c0 + c1) + v0 + v1\n"));
  23472. return (synthesis_result) ? result : error_node();
  23473. }
  23474. // (v0 + c0) - (c1 + v1) --> (covov) (c0 - c1) + v0 - v1
  23475. else if ((details::e_add == o0) && (details::e_sub == o1) && (details::e_add == o2))
  23476. {
  23477. const bool synthesis_result =
  23478. synthesize_sf3ext_expression::
  23479. template compile<ctype,vtype,vtype>(expr_gen,"(t+t)-t",(c0 - c1),v0,v1,result);
  23480. exprtk_debug(("(v0 + c0) - (c1 + v1) --> (covov) (c0 - c1) + v0 - v1\n"));
  23481. return (synthesis_result) ? result : error_node();
  23482. }
  23483. // (v0 - c0) - (c1 - v1) --> (vovoc) v0 + v1 - (c1 + c0)
  23484. else if ((details::e_sub == o0) && (details::e_sub == o1) && (details::e_sub == o2))
  23485. {
  23486. const bool synthesis_result =
  23487. synthesize_sf3ext_expression::
  23488. template compile<vtype,vtype,ctype>(expr_gen,"(t+t)-t",v0,v1,(c1 + c0),result);
  23489. exprtk_debug(("(v0 - c0) - (c1 - v1) --> (vovoc) v0 + v1 - (c1 + c0)\n"));
  23490. return (synthesis_result) ? result : error_node();
  23491. }
  23492. // (v0 * c0) * (c1 * v1) --> (covov) (c0 * c1) * v0 * v1
  23493. else if ((details::e_mul == o0) && (details::e_mul == o1) && (details::e_mul == o2))
  23494. {
  23495. const bool synthesis_result =
  23496. synthesize_sf3ext_expression::
  23497. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)*t",(c0 * c1),v0,v1,result);
  23498. exprtk_debug(("(v0 * c0) * (c1 * v1) --> (covov) (c0 * c1) * v0 * v1\n"));
  23499. return (synthesis_result) ? result : error_node();
  23500. }
  23501. // (v0 * c0) / (c1 * v1) --> (covov) (c0 / c1) * (v0 * v1)
  23502. else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_mul == o2))
  23503. {
  23504. const bool synthesis_result =
  23505. synthesize_sf3ext_expression::
  23506. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",(c0 / c1),v0,v1,result);
  23507. exprtk_debug(("(v0 * c0) / (c1 * v1) --> (covov) (c0 / c1) * (v0 * v1)\n"));
  23508. return (synthesis_result) ? result : error_node();
  23509. }
  23510. // (v0 / c0) * (c1 / v1) --> (covov) (c1 / c0) * (v0 / v1)
  23511. else if ((details::e_div == o0) && (details::e_mul == o1) && (details::e_div == o2))
  23512. {
  23513. const bool synthesis_result =
  23514. synthesize_sf3ext_expression::
  23515. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",(c1 / c0),v0,v1,result);
  23516. exprtk_debug(("(v0 / c0) * (c1 / v1) --> (covov) (c1 / c0) * (v0 / v1)\n"));
  23517. return (synthesis_result) ? result : error_node();
  23518. }
  23519. // (v0 * c0) / (c1 / v1) --> (covov) (c0 / c1) * (v0 * v1)
  23520. else if ((details::e_mul == o0) && (details::e_div == o1) && (details::e_div == o2))
  23521. {
  23522. const bool synthesis_result =
  23523. synthesize_sf3ext_expression::
  23524. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)*t",(c0 / c1),v0,v1,result);
  23525. exprtk_debug(("(v0 * c0) / (c1 / v1) --> (covov) (c0 / c1) * (v0 * v1)\n"));
  23526. return (synthesis_result) ? result : error_node();
  23527. }
  23528. // (v0 / c0) / (c1 * v1) --> (covov) (1 / (c0 * c1)) * (v0 / v1)
  23529. else if ((details::e_div == o0) && (details::e_div == o1) && (details::e_mul == o2))
  23530. {
  23531. const bool synthesis_result =
  23532. synthesize_sf3ext_expression::
  23533. template compile<ctype,vtype,vtype>(expr_gen,"(t*t)/t",Type(1) / (c0 * c1),v0,v1,result);
  23534. exprtk_debug(("(v0 / c0) / (c1 * v1) --> (covov) (1 / (c0 * c1)) * (v0 / v1)\n"));
  23535. return (synthesis_result) ? result : error_node();
  23536. }
  23537. // (v0 / c0) / (c1 / v1) --> (vovoc) (v0 * v1) * (1 / (c0 * c1))
  23538. else if ((details::e_div == o0) && (details::e_div == o1) && (details::e_div == o2))
  23539. {
  23540. const bool synthesis_result =
  23541. synthesize_sf3ext_expression::
  23542. template compile<vtype,vtype,ctype>(expr_gen,"(t*t)*t",v0,v1,Type(1) / (c0 * c1),result);
  23543. exprtk_debug(("(v0 / c0) / (c1 / v1) --> (vovoc) (v0 * v1) * (1 / (c0 * c1))\n"));
  23544. return (synthesis_result) ? result : error_node();
  23545. }
  23546. // (v0 * c) +/- (c * v1) --> (covov) c * (v0 +/- v1)
  23547. else if (
  23548. (std::equal_to<T>()(c0,c1)) &&
  23549. (details::e_mul == o0) &&
  23550. (details::e_mul == o2) &&
  23551. (
  23552. (details::e_add == o1) || (details::e_sub == o1)
  23553. )
  23554. )
  23555. {
  23556. std::string specfunc;
  23557. switch (o1)
  23558. {
  23559. case details::e_add : specfunc = "t*(t+t)"; break;
  23560. case details::e_sub : specfunc = "t*(t-t)"; break;
  23561. default : return error_node();
  23562. }
  23563. const bool synthesis_result =
  23564. synthesize_sf3ext_expression::
  23565. template compile<ctype,vtype,vtype>(expr_gen,specfunc,c0,v0,v1,result);
  23566. exprtk_debug(("(v0 * c) +/- (c * v1) --> (covov) c * (v0 +/- v1)\n"));
  23567. return (synthesis_result) ? result : error_node();
  23568. }
  23569. }
  23570. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,c1,v1,result))
  23571. return result;
  23572. else if (!expr_gen.valid_operator(o0,f0))
  23573. return error_node();
  23574. else if (!expr_gen.valid_operator(o1,f1))
  23575. return error_node();
  23576. else if (!expr_gen.valid_operator(o2,f2))
  23577. return error_node();
  23578. else
  23579. return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,c1,v1,f0,f1,f2);
  23580. }
  23581. static inline std::string id(expression_generator<Type>& expr_gen,
  23582. const details::operator_type o0,
  23583. const details::operator_type o1,
  23584. const details::operator_type o2)
  23585. {
  23586. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t)");
  23587. }
  23588. };
  23589. struct synthesize_vovovov_expression1
  23590. {
  23591. typedef typename vovovov_t::type1 node_type;
  23592. typedef typename vovovov_t::sf4_type sf4_type;
  23593. typedef typename node_type::T0 T0;
  23594. typedef typename node_type::T1 T1;
  23595. typedef typename node_type::T2 T2;
  23596. typedef typename node_type::T3 T3;
  23597. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23598. const details::operator_type& operation,
  23599. expression_node_ptr (&branch)[2])
  23600. {
  23601. // v0 o0 (v1 o1 (v2 o2 v3))
  23602. typedef typename synthesize_vovov_expression1::node_type vovov_t;
  23603. const vovov_t* vovov = static_cast<const vovov_t*>(branch[1]);
  23604. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  23605. const Type& v1 = vovov->t0();
  23606. const Type& v2 = vovov->t1();
  23607. const Type& v3 = vovov->t2();
  23608. const details::operator_type o0 = operation;
  23609. const details::operator_type o1 = expr_gen.get_operator(vovov->f0());
  23610. const details::operator_type o2 = expr_gen.get_operator(vovov->f1());
  23611. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23612. binary_functor_t f1 = vovov->f0();
  23613. binary_functor_t f2 = vovov->f1();
  23614. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23615. expression_node_ptr result = error_node();
  23616. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result))
  23617. return result;
  23618. else if (!expr_gen.valid_operator(o0,f0))
  23619. return error_node();
  23620. exprtk_debug(("v0 o0 (v1 o1 (v2 o2 v3))\n"));
  23621. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,v3,f0,f1,f2);
  23622. }
  23623. static inline std::string id(expression_generator<Type>& expr_gen,
  23624. const details::operator_type o0,
  23625. const details::operator_type o1,
  23626. const details::operator_type o2)
  23627. {
  23628. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t))");
  23629. }
  23630. };
  23631. struct synthesize_vovovoc_expression1
  23632. {
  23633. typedef typename vovovoc_t::type1 node_type;
  23634. typedef typename vovovoc_t::sf4_type sf4_type;
  23635. typedef typename node_type::T0 T0;
  23636. typedef typename node_type::T1 T1;
  23637. typedef typename node_type::T2 T2;
  23638. typedef typename node_type::T3 T3;
  23639. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23640. const details::operator_type& operation,
  23641. expression_node_ptr (&branch)[2])
  23642. {
  23643. // v0 o0 (v1 o1 (v2 o2 c))
  23644. typedef typename synthesize_vovoc_expression1::node_type vovoc_t;
  23645. const vovoc_t* vovoc = static_cast<const vovoc_t*>(branch[1]);
  23646. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  23647. const Type& v1 = vovoc->t0();
  23648. const Type& v2 = vovoc->t1();
  23649. const Type c = vovoc->t2();
  23650. const details::operator_type o0 = operation;
  23651. const details::operator_type o1 = expr_gen.get_operator(vovoc->f0());
  23652. const details::operator_type o2 = expr_gen.get_operator(vovoc->f1());
  23653. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23654. binary_functor_t f1 = vovoc->f0();
  23655. binary_functor_t f2 = vovoc->f1();
  23656. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23657. expression_node_ptr result = error_node();
  23658. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result))
  23659. return result;
  23660. else if (!expr_gen.valid_operator(o0,f0))
  23661. return error_node();
  23662. exprtk_debug(("v0 o0 (v1 o1 (v2 o2 c))\n"));
  23663. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,c,f0,f1,f2);
  23664. }
  23665. static inline std::string id(expression_generator<Type>& expr_gen,
  23666. const details::operator_type o0,
  23667. const details::operator_type o1,
  23668. const details::operator_type o2)
  23669. {
  23670. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t))");
  23671. }
  23672. };
  23673. struct synthesize_vovocov_expression1
  23674. {
  23675. typedef typename vovocov_t::type1 node_type;
  23676. typedef typename vovocov_t::sf4_type sf4_type;
  23677. typedef typename node_type::T0 T0;
  23678. typedef typename node_type::T1 T1;
  23679. typedef typename node_type::T2 T2;
  23680. typedef typename node_type::T3 T3;
  23681. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23682. const details::operator_type& operation,
  23683. expression_node_ptr (&branch)[2])
  23684. {
  23685. // v0 o0 (v1 o1 (c o2 v2))
  23686. typedef typename synthesize_vocov_expression1::node_type vocov_t;
  23687. const vocov_t* vocov = static_cast<const vocov_t*>(branch[1]);
  23688. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  23689. const Type& v1 = vocov->t0();
  23690. const Type c = vocov->t1();
  23691. const Type& v2 = vocov->t2();
  23692. const details::operator_type o0 = operation;
  23693. const details::operator_type o1 = expr_gen.get_operator(vocov->f0());
  23694. const details::operator_type o2 = expr_gen.get_operator(vocov->f1());
  23695. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23696. binary_functor_t f1 = vocov->f0();
  23697. binary_functor_t f2 = vocov->f1();
  23698. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23699. expression_node_ptr result = error_node();
  23700. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,c,v2,result))
  23701. return result;
  23702. if (!expr_gen.valid_operator(o0,f0))
  23703. return error_node();
  23704. exprtk_debug(("v0 o0 (v1 o1 (c o2 v2))\n"));
  23705. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,v2,f0,f1,f2);
  23706. }
  23707. static inline std::string id(expression_generator<Type>& expr_gen,
  23708. const details::operator_type o0,
  23709. const details::operator_type o1,
  23710. const details::operator_type o2)
  23711. {
  23712. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t))");
  23713. }
  23714. };
  23715. struct synthesize_vocovov_expression1
  23716. {
  23717. typedef typename vocovov_t::type1 node_type;
  23718. typedef typename vocovov_t::sf4_type sf4_type;
  23719. typedef typename node_type::T0 T0;
  23720. typedef typename node_type::T1 T1;
  23721. typedef typename node_type::T2 T2;
  23722. typedef typename node_type::T3 T3;
  23723. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23724. const details::operator_type& operation,
  23725. expression_node_ptr (&branch)[2])
  23726. {
  23727. // v0 o0 (c o1 (v1 o2 v2))
  23728. typedef typename synthesize_covov_expression1::node_type covov_t;
  23729. const covov_t* covov = static_cast<const covov_t*>(branch[1]);
  23730. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  23731. const Type c = covov->t0();
  23732. const Type& v1 = covov->t1();
  23733. const Type& v2 = covov->t2();
  23734. const details::operator_type o0 = operation;
  23735. const details::operator_type o1 = expr_gen.get_operator(covov->f0());
  23736. const details::operator_type o2 = expr_gen.get_operator(covov->f1());
  23737. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23738. binary_functor_t f1 = covov->f0();
  23739. binary_functor_t f2 = covov->f1();
  23740. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23741. expression_node_ptr result = error_node();
  23742. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result))
  23743. return result;
  23744. else if (!expr_gen.valid_operator(o0,f0))
  23745. return error_node();
  23746. exprtk_debug(("v0 o0 (c o1 (v1 o2 v2))\n"));
  23747. return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,v2,f0,f1,f2);
  23748. }
  23749. static inline std::string id(expression_generator<Type>& expr_gen,
  23750. const details::operator_type o0,
  23751. const details::operator_type o1,
  23752. const details::operator_type o2)
  23753. {
  23754. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t))");
  23755. }
  23756. };
  23757. struct synthesize_covovov_expression1
  23758. {
  23759. typedef typename covovov_t::type1 node_type;
  23760. typedef typename covovov_t::sf4_type sf4_type;
  23761. typedef typename node_type::T0 T0;
  23762. typedef typename node_type::T1 T1;
  23763. typedef typename node_type::T2 T2;
  23764. typedef typename node_type::T3 T3;
  23765. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23766. const details::operator_type& operation,
  23767. expression_node_ptr (&branch)[2])
  23768. {
  23769. // c o0 (v0 o1 (v1 o2 v2))
  23770. typedef typename synthesize_vovov_expression1::node_type vovov_t;
  23771. const vovov_t* vovov = static_cast<const vovov_t*>(branch[1]);
  23772. const Type c = static_cast<details::literal_node<Type>*>(branch[0])->value();
  23773. const Type& v0 = vovov->t0();
  23774. const Type& v1 = vovov->t1();
  23775. const Type& v2 = vovov->t2();
  23776. const details::operator_type o0 = operation;
  23777. const details::operator_type o1 = expr_gen.get_operator(vovov->f0());
  23778. const details::operator_type o2 = expr_gen.get_operator(vovov->f1());
  23779. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23780. binary_functor_t f1 = vovov->f0();
  23781. binary_functor_t f2 = vovov->f1();
  23782. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  23783. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23784. expression_node_ptr result = error_node();
  23785. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c,v0,v1,v2,result))
  23786. return result;
  23787. if (!expr_gen.valid_operator(o0,f0))
  23788. return error_node();
  23789. exprtk_debug(("c o0 (v0 o1 (v1 o2 v2))\n"));
  23790. return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,v2,f0,f1,f2);
  23791. }
  23792. static inline std::string id(expression_generator<Type>& expr_gen,
  23793. const details::operator_type o0,
  23794. const details::operator_type o1,
  23795. const details::operator_type o2)
  23796. {
  23797. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t))");
  23798. }
  23799. };
  23800. struct synthesize_covocov_expression1
  23801. {
  23802. typedef typename covocov_t::type1 node_type;
  23803. typedef typename covocov_t::sf4_type sf4_type;
  23804. typedef typename node_type::T0 T0;
  23805. typedef typename node_type::T1 T1;
  23806. typedef typename node_type::T2 T2;
  23807. typedef typename node_type::T3 T3;
  23808. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23809. const details::operator_type& operation,
  23810. expression_node_ptr (&branch)[2])
  23811. {
  23812. // c0 o0 (v0 o1 (c1 o2 v1))
  23813. typedef typename synthesize_vocov_expression1::node_type vocov_t;
  23814. const vocov_t* vocov = static_cast<const vocov_t*>(branch[1]);
  23815. const Type c0 = static_cast<details::literal_node<Type>*>(branch[0])->value();
  23816. const Type& v0 = vocov->t0();
  23817. const Type c1 = vocov->t1();
  23818. const Type& v1 = vocov->t2();
  23819. const details::operator_type o0 = operation;
  23820. const details::operator_type o1 = expr_gen.get_operator(vocov->f0());
  23821. const details::operator_type o2 = expr_gen.get_operator(vocov->f1());
  23822. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23823. binary_functor_t f1 = vocov->f0();
  23824. binary_functor_t f2 = vocov->f1();
  23825. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  23826. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23827. expression_node_ptr result = error_node();
  23828. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result))
  23829. return result;
  23830. else if (!expr_gen.valid_operator(o0,f0))
  23831. return error_node();
  23832. exprtk_debug(("c0 o0 (v0 o1 (c1 o2 v1))\n"));
  23833. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,c1,v1,f0,f1,f2);
  23834. }
  23835. static inline std::string id(expression_generator<Type>& expr_gen,
  23836. const details::operator_type o0,
  23837. const details::operator_type o1,
  23838. const details::operator_type o2)
  23839. {
  23840. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t))");
  23841. }
  23842. };
  23843. struct synthesize_vocovoc_expression1
  23844. {
  23845. typedef typename vocovoc_t::type1 node_type;
  23846. typedef typename vocovoc_t::sf4_type sf4_type;
  23847. typedef typename node_type::T0 T0;
  23848. typedef typename node_type::T1 T1;
  23849. typedef typename node_type::T2 T2;
  23850. typedef typename node_type::T3 T3;
  23851. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23852. const details::operator_type& operation,
  23853. expression_node_ptr (&branch)[2])
  23854. {
  23855. // v0 o0 (c0 o1 (v1 o2 c2))
  23856. typedef typename synthesize_covoc_expression1::node_type covoc_t;
  23857. const covoc_t* covoc = static_cast<const covoc_t*>(branch[1]);
  23858. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  23859. const Type c0 = covoc->t0();
  23860. const Type& v1 = covoc->t1();
  23861. const Type c1 = covoc->t2();
  23862. const details::operator_type o0 = operation;
  23863. const details::operator_type o1 = expr_gen.get_operator(covoc->f0());
  23864. const details::operator_type o2 = expr_gen.get_operator(covoc->f1());
  23865. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23866. binary_functor_t f1 = covoc->f0();
  23867. binary_functor_t f2 = covoc->f1();
  23868. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23869. expression_node_ptr result = error_node();
  23870. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result))
  23871. return result;
  23872. else if (!expr_gen.valid_operator(o0,f0))
  23873. return error_node();
  23874. exprtk_debug(("v0 o0 (c0 o1 (v1 o2 c2))\n"));
  23875. return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,v1,c1,f0,f1,f2);
  23876. }
  23877. static inline std::string id(expression_generator<Type>& expr_gen,
  23878. const details::operator_type o0,
  23879. const details::operator_type o1,
  23880. const details::operator_type o2)
  23881. {
  23882. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t))");
  23883. }
  23884. };
  23885. struct synthesize_covovoc_expression1
  23886. {
  23887. typedef typename covovoc_t::type1 node_type;
  23888. typedef typename covovoc_t::sf4_type sf4_type;
  23889. typedef typename node_type::T0 T0;
  23890. typedef typename node_type::T1 T1;
  23891. typedef typename node_type::T2 T2;
  23892. typedef typename node_type::T3 T3;
  23893. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23894. const details::operator_type& operation,
  23895. expression_node_ptr (&branch)[2])
  23896. {
  23897. // c0 o0 (v0 o1 (v1 o2 c1))
  23898. typedef typename synthesize_vovoc_expression1::node_type vovoc_t;
  23899. const vovoc_t* vovoc = static_cast<const vovoc_t*>(branch[1]);
  23900. const Type c0 = static_cast<details::literal_node<Type>*>(branch[0])->value();
  23901. const Type& v0 = vovoc->t0();
  23902. const Type& v1 = vovoc->t1();
  23903. const Type c1 = vovoc->t2();
  23904. const details::operator_type o0 = operation;
  23905. const details::operator_type o1 = expr_gen.get_operator(vovoc->f0());
  23906. const details::operator_type o2 = expr_gen.get_operator(vovoc->f1());
  23907. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23908. binary_functor_t f1 = vovoc->f0();
  23909. binary_functor_t f2 = vovoc->f1();
  23910. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  23911. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23912. expression_node_ptr result = error_node();
  23913. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result))
  23914. return result;
  23915. else if (!expr_gen.valid_operator(o0,f0))
  23916. return error_node();
  23917. exprtk_debug(("c0 o0 (v0 o1 (v1 o2 c1))\n"));
  23918. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,v1,c1,f0,f1,f2);
  23919. }
  23920. static inline std::string id(expression_generator<Type>& expr_gen,
  23921. const details::operator_type o0,
  23922. const details::operator_type o1,
  23923. const details::operator_type o2)
  23924. {
  23925. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t))");
  23926. }
  23927. };
  23928. struct synthesize_vococov_expression1
  23929. {
  23930. typedef typename vococov_t::type1 node_type;
  23931. typedef typename vococov_t::sf4_type sf4_type;
  23932. typedef typename node_type::T0 T0;
  23933. typedef typename node_type::T1 T1;
  23934. typedef typename node_type::T2 T2;
  23935. typedef typename node_type::T3 T3;
  23936. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23937. const details::operator_type& operation,
  23938. expression_node_ptr (&branch)[2])
  23939. {
  23940. // v0 o0 (c0 o1 (c1 o2 v1))
  23941. typedef typename synthesize_cocov_expression1::node_type cocov_t;
  23942. const cocov_t* cocov = static_cast<const cocov_t*>(branch[1]);
  23943. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  23944. const Type c0 = cocov->t0();
  23945. const Type c1 = cocov->t1();
  23946. const Type& v1 = cocov->t2();
  23947. const details::operator_type o0 = operation;
  23948. const details::operator_type o1 = expr_gen.get_operator(cocov->f0());
  23949. const details::operator_type o2 = expr_gen.get_operator(cocov->f1());
  23950. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23951. binary_functor_t f1 = cocov->f0();
  23952. binary_functor_t f2 = cocov->f1();
  23953. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23954. expression_node_ptr result = error_node();
  23955. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,c1,v1,result))
  23956. return result;
  23957. else if (!expr_gen.valid_operator(o0,f0))
  23958. return error_node();
  23959. exprtk_debug(("v0 o0 (c0 o1 (c1 o2 v1))\n"));
  23960. return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,c1,v1,f0,f1,f2);
  23961. }
  23962. static inline std::string id(expression_generator<Type>& expr_gen,
  23963. const details::operator_type o0,
  23964. const details::operator_type o1,
  23965. const details::operator_type o2)
  23966. {
  23967. return (details::build_string() << "t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "(t" << expr_gen.to_str(o2) << "t))");
  23968. }
  23969. };
  23970. struct synthesize_vovovov_expression2
  23971. {
  23972. typedef typename vovovov_t::type2 node_type;
  23973. typedef typename vovovov_t::sf4_type sf4_type;
  23974. typedef typename node_type::T0 T0;
  23975. typedef typename node_type::T1 T1;
  23976. typedef typename node_type::T2 T2;
  23977. typedef typename node_type::T3 T3;
  23978. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  23979. const details::operator_type& operation,
  23980. expression_node_ptr (&branch)[2])
  23981. {
  23982. // v0 o0 ((v1 o1 v2) o2 v3)
  23983. typedef typename synthesize_vovov_expression0::node_type vovov_t;
  23984. const vovov_t* vovov = static_cast<const vovov_t*>(branch[1]);
  23985. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  23986. const Type& v1 = vovov->t0();
  23987. const Type& v2 = vovov->t1();
  23988. const Type& v3 = vovov->t2();
  23989. const details::operator_type o0 = operation;
  23990. const details::operator_type o1 = expr_gen.get_operator(vovov->f0());
  23991. const details::operator_type o2 = expr_gen.get_operator(vovov->f1());
  23992. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  23993. binary_functor_t f1 = vovov->f0();
  23994. binary_functor_t f2 = vovov->f1();
  23995. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  23996. expression_node_ptr result = error_node();
  23997. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result))
  23998. return result;
  23999. else if (!expr_gen.valid_operator(o0,f0))
  24000. return error_node();
  24001. exprtk_debug(("v0 o0 ((v1 o1 v2) o2 v3)\n"));
  24002. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,v3,f0,f1,f2);
  24003. }
  24004. static inline std::string id(expression_generator<Type>& expr_gen,
  24005. const details::operator_type o0,
  24006. const details::operator_type o1,
  24007. const details::operator_type o2)
  24008. {
  24009. return (details::build_string() << "t" << expr_gen.to_str(o0) << "((t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t)");
  24010. }
  24011. };
  24012. struct synthesize_vovovoc_expression2
  24013. {
  24014. typedef typename vovovoc_t::type2 node_type;
  24015. typedef typename vovovoc_t::sf4_type sf4_type;
  24016. typedef typename node_type::T0 T0;
  24017. typedef typename node_type::T1 T1;
  24018. typedef typename node_type::T2 T2;
  24019. typedef typename node_type::T3 T3;
  24020. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24021. const details::operator_type& operation,
  24022. expression_node_ptr (&branch)[2])
  24023. {
  24024. // v0 o0 ((v1 o1 v2) o2 c)
  24025. typedef typename synthesize_vovoc_expression0::node_type vovoc_t;
  24026. const vovoc_t* vovoc = static_cast<const vovoc_t*>(branch[1]);
  24027. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  24028. const Type& v1 = vovoc->t0();
  24029. const Type& v2 = vovoc->t1();
  24030. const Type c = vovoc->t2();
  24031. const details::operator_type o0 = operation;
  24032. const details::operator_type o1 = expr_gen.get_operator(vovoc->f0());
  24033. const details::operator_type o2 = expr_gen.get_operator(vovoc->f1());
  24034. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  24035. binary_functor_t f1 = vovoc->f0();
  24036. binary_functor_t f2 = vovoc->f1();
  24037. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24038. expression_node_ptr result = error_node();
  24039. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result))
  24040. return result;
  24041. else if (!expr_gen.valid_operator(o0,f0))
  24042. return error_node();
  24043. exprtk_debug(("v0 o0 ((v1 o1 v2) o2 c)\n"));
  24044. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,c,f0,f1,f2);
  24045. }
  24046. static inline std::string id(expression_generator<Type>& expr_gen,
  24047. const details::operator_type o0,
  24048. const details::operator_type o1,
  24049. const details::operator_type o2)
  24050. {
  24051. return (details::build_string() << "t" << expr_gen.to_str(o0) << "((t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t)");
  24052. }
  24053. };
  24054. struct synthesize_vovocov_expression2
  24055. {
  24056. typedef typename vovocov_t::type2 node_type;
  24057. typedef typename vovocov_t::sf4_type sf4_type;
  24058. typedef typename node_type::T0 T0;
  24059. typedef typename node_type::T1 T1;
  24060. typedef typename node_type::T2 T2;
  24061. typedef typename node_type::T3 T3;
  24062. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24063. const details::operator_type& operation,
  24064. expression_node_ptr (&branch)[2])
  24065. {
  24066. // v0 o0 ((v1 o1 c) o2 v2)
  24067. typedef typename synthesize_vocov_expression0::node_type vocov_t;
  24068. const vocov_t* vocov = static_cast<const vocov_t*>(branch[1]);
  24069. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  24070. const Type& v1 = vocov->t0();
  24071. const Type c = vocov->t1();
  24072. const Type& v2 = vocov->t2();
  24073. const details::operator_type o0 = operation;
  24074. const details::operator_type o1 = expr_gen.get_operator(vocov->f0());
  24075. const details::operator_type o2 = expr_gen.get_operator(vocov->f1());
  24076. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  24077. binary_functor_t f1 = vocov->f0();
  24078. binary_functor_t f2 = vocov->f1();
  24079. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24080. expression_node_ptr result = error_node();
  24081. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,c,v2,result))
  24082. return result;
  24083. else if (!expr_gen.valid_operator(o0,f0))
  24084. return error_node();
  24085. exprtk_debug(("v0 o0 ((v1 o1 c) o2 v2)\n"));
  24086. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,v2,f0,f1,f2);
  24087. }
  24088. static inline std::string id(expression_generator<Type>& expr_gen,
  24089. const details::operator_type o0,
  24090. const details::operator_type o1,
  24091. const details::operator_type o2)
  24092. {
  24093. return (details::build_string() << "t" << expr_gen.to_str(o0) << "((t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t)");
  24094. }
  24095. };
  24096. struct synthesize_vocovov_expression2
  24097. {
  24098. typedef typename vocovov_t::type2 node_type;
  24099. typedef typename vocovov_t::sf4_type sf4_type;
  24100. typedef typename node_type::T0 T0;
  24101. typedef typename node_type::T1 T1;
  24102. typedef typename node_type::T2 T2;
  24103. typedef typename node_type::T3 T3;
  24104. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24105. const details::operator_type& operation,
  24106. expression_node_ptr (&branch)[2])
  24107. {
  24108. // v0 o0 ((c o1 v1) o2 v2)
  24109. typedef typename synthesize_covov_expression0::node_type covov_t;
  24110. const covov_t* covov = static_cast<const covov_t*>(branch[1]);
  24111. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  24112. const Type c = covov->t0();
  24113. const Type& v1 = covov->t1();
  24114. const Type& v2 = covov->t2();
  24115. const details::operator_type o0 = operation;
  24116. const details::operator_type o1 = expr_gen.get_operator(covov->f0());
  24117. const details::operator_type o2 = expr_gen.get_operator(covov->f1());
  24118. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  24119. binary_functor_t f1 = covov->f0();
  24120. binary_functor_t f2 = covov->f1();
  24121. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24122. expression_node_ptr result = error_node();
  24123. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result))
  24124. return result;
  24125. else if (!expr_gen.valid_operator(o0,f0))
  24126. return error_node();
  24127. exprtk_debug(("v0 o0 ((c o1 v1) o2 v2)\n"));
  24128. return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,v2,f0,f1,f2);
  24129. }
  24130. static inline std::string id(expression_generator<Type>& expr_gen,
  24131. const details::operator_type o0,
  24132. const details::operator_type o1,
  24133. const details::operator_type o2)
  24134. {
  24135. return (details::build_string() << "t" << expr_gen.to_str(o0) << "((t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t)");
  24136. }
  24137. };
  24138. struct synthesize_covovov_expression2
  24139. {
  24140. typedef typename covovov_t::type2 node_type;
  24141. typedef typename covovov_t::sf4_type sf4_type;
  24142. typedef typename node_type::T0 T0;
  24143. typedef typename node_type::T1 T1;
  24144. typedef typename node_type::T2 T2;
  24145. typedef typename node_type::T3 T3;
  24146. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24147. const details::operator_type& operation,
  24148. expression_node_ptr (&branch)[2])
  24149. {
  24150. // c o0 ((v1 o1 v2) o2 v3)
  24151. typedef typename synthesize_vovov_expression0::node_type vovov_t;
  24152. const vovov_t* vovov = static_cast<const vovov_t*>(branch[1]);
  24153. const Type c = static_cast<details::literal_node<Type>*>(branch[0])->value();
  24154. const Type& v0 = vovov->t0();
  24155. const Type& v1 = vovov->t1();
  24156. const Type& v2 = vovov->t2();
  24157. const details::operator_type o0 = operation;
  24158. const details::operator_type o1 = expr_gen.get_operator(vovov->f0());
  24159. const details::operator_type o2 = expr_gen.get_operator(vovov->f1());
  24160. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  24161. binary_functor_t f1 = vovov->f0();
  24162. binary_functor_t f2 = vovov->f1();
  24163. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24164. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24165. expression_node_ptr result = error_node();
  24166. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c,v0,v1,v2,result))
  24167. return result;
  24168. else if (!expr_gen.valid_operator(o0,f0))
  24169. return error_node();
  24170. exprtk_debug(("c o0 ((v1 o1 v2) o2 v3)\n"));
  24171. return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,v2,f0,f1,f2);
  24172. }
  24173. static inline std::string id(expression_generator<Type>& expr_gen,
  24174. const details::operator_type o0,
  24175. const details::operator_type o1,
  24176. const details::operator_type o2)
  24177. {
  24178. return (details::build_string() << "t" << expr_gen.to_str(o0) << "((t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t)");
  24179. }
  24180. };
  24181. struct synthesize_covocov_expression2
  24182. {
  24183. typedef typename covocov_t::type2 node_type;
  24184. typedef typename covocov_t::sf4_type sf4_type;
  24185. typedef typename node_type::T0 T0;
  24186. typedef typename node_type::T1 T1;
  24187. typedef typename node_type::T2 T2;
  24188. typedef typename node_type::T3 T3;
  24189. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24190. const details::operator_type& operation,
  24191. expression_node_ptr (&branch)[2])
  24192. {
  24193. // c0 o0 ((v0 o1 c1) o2 v1)
  24194. typedef typename synthesize_vocov_expression0::node_type vocov_t;
  24195. const vocov_t* vocov = static_cast<const vocov_t*>(branch[1]);
  24196. const Type c0 = static_cast<details::literal_node<Type>*>(branch[0])->value();
  24197. const Type& v0 = vocov->t0();
  24198. const Type c1 = vocov->t1();
  24199. const Type& v1 = vocov->t2();
  24200. const details::operator_type o0 = operation;
  24201. const details::operator_type o1 = expr_gen.get_operator(vocov->f0());
  24202. const details::operator_type o2 = expr_gen.get_operator(vocov->f1());
  24203. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  24204. binary_functor_t f1 = vocov->f0();
  24205. binary_functor_t f2 = vocov->f1();
  24206. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24207. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24208. expression_node_ptr result = error_node();
  24209. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result))
  24210. return result;
  24211. else if (!expr_gen.valid_operator(o0,f0))
  24212. return error_node();
  24213. exprtk_debug(("c0 o0 ((v0 o1 c1) o2 v1)\n"));
  24214. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,c1,v1,f0,f1,f2);
  24215. }
  24216. static inline std::string id(expression_generator<Type>& expr_gen,
  24217. const details::operator_type o0,
  24218. const details::operator_type o1,
  24219. const details::operator_type o2)
  24220. {
  24221. return (details::build_string() << "t" << expr_gen.to_str(o0) << "((t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t)");
  24222. }
  24223. };
  24224. struct synthesize_vocovoc_expression2
  24225. {
  24226. typedef typename vocovoc_t::type2 node_type;
  24227. typedef typename vocovoc_t::sf4_type sf4_type;
  24228. typedef typename node_type::T0 T0;
  24229. typedef typename node_type::T1 T1;
  24230. typedef typename node_type::T2 T2;
  24231. typedef typename node_type::T3 T3;
  24232. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24233. const details::operator_type& operation,
  24234. expression_node_ptr (&branch)[2])
  24235. {
  24236. // v0 o0 ((c0 o1 v1) o2 c1)
  24237. typedef typename synthesize_covoc_expression0::node_type covoc_t;
  24238. const covoc_t* covoc = static_cast<const covoc_t*>(branch[1]);
  24239. const Type& v0 = static_cast<details::variable_node<Type>*>(branch[0])->ref();
  24240. const Type c0 = covoc->t0();
  24241. const Type& v1 = covoc->t1();
  24242. const Type c1 = covoc->t2();
  24243. const details::operator_type o0 = operation;
  24244. const details::operator_type o1 = expr_gen.get_operator(covoc->f0());
  24245. const details::operator_type o2 = expr_gen.get_operator(covoc->f1());
  24246. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  24247. binary_functor_t f1 = covoc->f0();
  24248. binary_functor_t f2 = covoc->f1();
  24249. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24250. expression_node_ptr result = error_node();
  24251. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result))
  24252. return result;
  24253. else if (!expr_gen.valid_operator(o0,f0))
  24254. return error_node();
  24255. exprtk_debug(("v0 o0 ((c0 o1 v1) o2 c1)\n"));
  24256. return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,v1,c1,f0,f1,f2);
  24257. }
  24258. static inline std::string id(expression_generator<Type>& expr_gen,
  24259. const details::operator_type o0,
  24260. const details::operator_type o1,
  24261. const details::operator_type o2)
  24262. {
  24263. return (details::build_string() << "t" << expr_gen.to_str(o0) << "((t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t)");
  24264. }
  24265. };
  24266. struct synthesize_covovoc_expression2
  24267. {
  24268. typedef typename covovoc_t::type2 node_type;
  24269. typedef typename covovoc_t::sf4_type sf4_type;
  24270. typedef typename node_type::T0 T0;
  24271. typedef typename node_type::T1 T1;
  24272. typedef typename node_type::T2 T2;
  24273. typedef typename node_type::T3 T3;
  24274. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24275. const details::operator_type& operation,
  24276. expression_node_ptr (&branch)[2])
  24277. {
  24278. // c0 o0 ((v0 o1 v1) o2 c1)
  24279. typedef typename synthesize_vovoc_expression0::node_type vovoc_t;
  24280. const vovoc_t* vovoc = static_cast<const vovoc_t*>(branch[1]);
  24281. const Type c0 = static_cast<details::literal_node<Type>*>(branch[0])->value();
  24282. const Type& v0 = vovoc->t0();
  24283. const Type& v1 = vovoc->t1();
  24284. const Type c1 = vovoc->t2();
  24285. const details::operator_type o0 = operation;
  24286. const details::operator_type o1 = expr_gen.get_operator(vovoc->f0());
  24287. const details::operator_type o2 = expr_gen.get_operator(vovoc->f1());
  24288. binary_functor_t f0 = reinterpret_cast<binary_functor_t>(0);
  24289. binary_functor_t f1 = vovoc->f0();
  24290. binary_functor_t f2 = vovoc->f1();
  24291. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24292. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24293. expression_node_ptr result = error_node();
  24294. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result))
  24295. return result;
  24296. else if (!expr_gen.valid_operator(o0,f0))
  24297. return error_node();
  24298. exprtk_debug(("c0 o0 ((v0 o1 v1) o2 c1)\n"));
  24299. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,v1,c1,f0,f1,f2);
  24300. }
  24301. static inline std::string id(expression_generator<Type>& expr_gen,
  24302. const details::operator_type o0,
  24303. const details::operator_type o1,
  24304. const details::operator_type o2)
  24305. {
  24306. return (details::build_string() << "t" << expr_gen.to_str(o0) << "((t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t)");
  24307. }
  24308. };
  24309. struct synthesize_vococov_expression2
  24310. {
  24311. typedef typename vococov_t::type2 node_type;
  24312. static inline expression_node_ptr process(expression_generator<Type>&, const details::operator_type&, expression_node_ptr (&)[2])
  24313. {
  24314. // v0 o0 ((c0 o1 c1) o2 v1) - Not possible
  24315. exprtk_debug(("v0 o0 ((c0 o1 c1) o2 v1) - Not possible\n"));
  24316. return error_node();
  24317. }
  24318. static inline std::string id(expression_generator<Type>&,
  24319. const details::operator_type, const details::operator_type, const details::operator_type)
  24320. {
  24321. return "INVALID";
  24322. }
  24323. };
  24324. struct synthesize_vovovov_expression3
  24325. {
  24326. typedef typename vovovov_t::type3 node_type;
  24327. typedef typename vovovov_t::sf4_type sf4_type;
  24328. typedef typename node_type::T0 T0;
  24329. typedef typename node_type::T1 T1;
  24330. typedef typename node_type::T2 T2;
  24331. typedef typename node_type::T3 T3;
  24332. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24333. const details::operator_type& operation,
  24334. expression_node_ptr (&branch)[2])
  24335. {
  24336. // ((v0 o0 v1) o1 v2) o2 v3
  24337. typedef typename synthesize_vovov_expression0::node_type vovov_t;
  24338. const vovov_t* vovov = static_cast<const vovov_t*>(branch[0]);
  24339. const Type& v0 = vovov->t0();
  24340. const Type& v1 = vovov->t1();
  24341. const Type& v2 = vovov->t2();
  24342. const Type& v3 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24343. const details::operator_type o0 = expr_gen.get_operator(vovov->f0());
  24344. const details::operator_type o1 = expr_gen.get_operator(vovov->f1());
  24345. const details::operator_type o2 = operation;
  24346. binary_functor_t f0 = vovov->f0();
  24347. binary_functor_t f1 = vovov->f1();
  24348. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24349. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24350. expression_node_ptr result = error_node();
  24351. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result))
  24352. return result;
  24353. else if (!expr_gen.valid_operator(o2,f2))
  24354. return error_node();
  24355. exprtk_debug(("((v0 o0 v1) o1 v2) o2 v3\n"));
  24356. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,v3,f0,f1,f2);
  24357. }
  24358. static inline std::string id(expression_generator<Type>& expr_gen,
  24359. const details::operator_type o0,
  24360. const details::operator_type o1,
  24361. const details::operator_type o2)
  24362. {
  24363. return (details::build_string() << "((t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24364. }
  24365. };
  24366. struct synthesize_vovovoc_expression3
  24367. {
  24368. typedef typename vovovoc_t::type3 node_type;
  24369. typedef typename vovovoc_t::sf4_type sf4_type;
  24370. typedef typename node_type::T0 T0;
  24371. typedef typename node_type::T1 T1;
  24372. typedef typename node_type::T2 T2;
  24373. typedef typename node_type::T3 T3;
  24374. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24375. const details::operator_type& operation,
  24376. expression_node_ptr (&branch)[2])
  24377. {
  24378. // ((v0 o0 v1) o1 v2) o2 c
  24379. typedef typename synthesize_vovov_expression0::node_type vovov_t;
  24380. const vovov_t* vovov = static_cast<const vovov_t*>(branch[0]);
  24381. const Type& v0 = vovov->t0();
  24382. const Type& v1 = vovov->t1();
  24383. const Type& v2 = vovov->t2();
  24384. const Type c = static_cast<details::literal_node<Type>*>(branch[1])->value();
  24385. const details::operator_type o0 = expr_gen.get_operator(vovov->f0());
  24386. const details::operator_type o1 = expr_gen.get_operator(vovov->f1());
  24387. const details::operator_type o2 = operation;
  24388. binary_functor_t f0 = vovov->f0();
  24389. binary_functor_t f1 = vovov->f1();
  24390. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24391. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24392. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24393. expression_node_ptr result = error_node();
  24394. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result))
  24395. return result;
  24396. else if (!expr_gen.valid_operator(o2,f2))
  24397. return error_node();
  24398. exprtk_debug(("((v0 o0 v1) o1 v2) o2 c\n"));
  24399. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,c,f0,f1,f2);
  24400. }
  24401. static inline std::string id(expression_generator<Type>& expr_gen,
  24402. const details::operator_type o0,
  24403. const details::operator_type o1,
  24404. const details::operator_type o2)
  24405. {
  24406. return (details::build_string() << "((t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24407. }
  24408. };
  24409. struct synthesize_vovocov_expression3
  24410. {
  24411. typedef typename vovocov_t::type3 node_type;
  24412. typedef typename vovocov_t::sf4_type sf4_type;
  24413. typedef typename node_type::T0 T0;
  24414. typedef typename node_type::T1 T1;
  24415. typedef typename node_type::T2 T2;
  24416. typedef typename node_type::T3 T3;
  24417. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24418. const details::operator_type& operation,
  24419. expression_node_ptr (&branch)[2])
  24420. {
  24421. // ((v0 o0 v1) o1 c) o2 v2
  24422. typedef typename synthesize_vovoc_expression0::node_type vovoc_t;
  24423. const vovoc_t* vovoc = static_cast<const vovoc_t*>(branch[0]);
  24424. const Type& v0 = vovoc->t0();
  24425. const Type& v1 = vovoc->t1();
  24426. const Type c = vovoc->t2();
  24427. const Type& v2 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24428. const details::operator_type o0 = expr_gen.get_operator(vovoc->f0());
  24429. const details::operator_type o1 = expr_gen.get_operator(vovoc->f1());
  24430. const details::operator_type o2 = operation;
  24431. binary_functor_t f0 = vovoc->f0();
  24432. binary_functor_t f1 = vovoc->f1();
  24433. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24434. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24435. expression_node_ptr result = error_node();
  24436. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,c,v2,result))
  24437. return result;
  24438. else if (!expr_gen.valid_operator(o2,f2))
  24439. return error_node();
  24440. exprtk_debug(("((v0 o0 v1) o1 c) o2 v2\n"));
  24441. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,v2,f0,f1,f2);
  24442. }
  24443. static inline std::string id(expression_generator<Type>& expr_gen,
  24444. const details::operator_type o0,
  24445. const details::operator_type o1,
  24446. const details::operator_type o2)
  24447. {
  24448. return (details::build_string() << "((t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24449. }
  24450. };
  24451. struct synthesize_vocovov_expression3
  24452. {
  24453. typedef typename vocovov_t::type3 node_type;
  24454. typedef typename vocovov_t::sf4_type sf4_type;
  24455. typedef typename node_type::T0 T0;
  24456. typedef typename node_type::T1 T1;
  24457. typedef typename node_type::T2 T2;
  24458. typedef typename node_type::T3 T3;
  24459. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24460. const details::operator_type& operation,
  24461. expression_node_ptr (&branch)[2])
  24462. {
  24463. // ((v0 o0 c) o1 v1) o2 v2
  24464. typedef typename synthesize_vocov_expression0::node_type vocov_t;
  24465. const vocov_t* vocov = static_cast<const vocov_t*>(branch[0]);
  24466. const Type& v0 = vocov->t0();
  24467. const Type c = vocov->t1();
  24468. const Type& v1 = vocov->t2();
  24469. const Type& v2 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24470. const details::operator_type o0 = expr_gen.get_operator(vocov->f0());
  24471. const details::operator_type o1 = expr_gen.get_operator(vocov->f1());
  24472. const details::operator_type o2 = operation;
  24473. binary_functor_t f0 = vocov->f0();
  24474. binary_functor_t f1 = vocov->f1();
  24475. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24476. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24477. expression_node_ptr result = error_node();
  24478. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result))
  24479. return result;
  24480. else if (!expr_gen.valid_operator(o2,f2))
  24481. return error_node();
  24482. exprtk_debug(("((v0 o0 c) o1 v1) o2 v2\n"));
  24483. return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,v2,f0,f1,f2);
  24484. }
  24485. static inline std::string id(expression_generator<Type>& expr_gen,
  24486. const details::operator_type o0,
  24487. const details::operator_type o1,
  24488. const details::operator_type o2)
  24489. {
  24490. return (details::build_string() << "((t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24491. }
  24492. };
  24493. struct synthesize_covovov_expression3
  24494. {
  24495. typedef typename covovov_t::type3 node_type;
  24496. typedef typename covovov_t::sf4_type sf4_type;
  24497. typedef typename node_type::T0 T0;
  24498. typedef typename node_type::T1 T1;
  24499. typedef typename node_type::T2 T2;
  24500. typedef typename node_type::T3 T3;
  24501. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24502. const details::operator_type& operation,
  24503. expression_node_ptr (&branch)[2])
  24504. {
  24505. // ((c o0 v0) o1 v1) o2 v2
  24506. typedef typename synthesize_covov_expression0::node_type covov_t;
  24507. const covov_t* covov = static_cast<const covov_t*>(branch[0]);
  24508. const Type c = covov->t0();
  24509. const Type& v0 = covov->t1();
  24510. const Type& v1 = covov->t2();
  24511. const Type& v2 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24512. const details::operator_type o0 = expr_gen.get_operator(covov->f0());
  24513. const details::operator_type o1 = expr_gen.get_operator(covov->f1());
  24514. const details::operator_type o2 = operation;
  24515. binary_functor_t f0 = covov->f0();
  24516. binary_functor_t f1 = covov->f1();
  24517. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24518. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24519. expression_node_ptr result = error_node();
  24520. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c,v0,v1,v2,result))
  24521. return result;
  24522. else if (!expr_gen.valid_operator(o2,f2))
  24523. return error_node();
  24524. exprtk_debug(("((c o0 v0) o1 v1) o2 v2\n"));
  24525. return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,v2,f0,f1,f2);
  24526. }
  24527. static inline std::string id(expression_generator<Type>& expr_gen,
  24528. const details::operator_type o0,
  24529. const details::operator_type o1,
  24530. const details::operator_type o2)
  24531. {
  24532. return (details::build_string() << "((t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24533. }
  24534. };
  24535. struct synthesize_covocov_expression3
  24536. {
  24537. typedef typename covocov_t::type3 node_type;
  24538. typedef typename covocov_t::sf4_type sf4_type;
  24539. typedef typename node_type::T0 T0;
  24540. typedef typename node_type::T1 T1;
  24541. typedef typename node_type::T2 T2;
  24542. typedef typename node_type::T3 T3;
  24543. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24544. const details::operator_type& operation,
  24545. expression_node_ptr (&branch)[2])
  24546. {
  24547. // ((c0 o0 v0) o1 c1) o2 v1
  24548. typedef typename synthesize_covoc_expression0::node_type covoc_t;
  24549. const covoc_t* covoc = static_cast<const covoc_t*>(branch[0]);
  24550. const Type c0 = covoc->t0();
  24551. const Type& v0 = covoc->t1();
  24552. const Type c1 = covoc->t2();
  24553. const Type& v1 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24554. const details::operator_type o0 = expr_gen.get_operator(covoc->f0());
  24555. const details::operator_type o1 = expr_gen.get_operator(covoc->f1());
  24556. const details::operator_type o2 = operation;
  24557. binary_functor_t f0 = covoc->f0();
  24558. binary_functor_t f1 = covoc->f1();
  24559. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24560. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24561. expression_node_ptr result = error_node();
  24562. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result))
  24563. return result;
  24564. else if (!expr_gen.valid_operator(o2,f2))
  24565. return error_node();
  24566. exprtk_debug(("((c0 o0 v0) o1 c1) o2 v1\n"));
  24567. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,c1,v1,f0,f1,f2);
  24568. }
  24569. static inline std::string id(expression_generator<Type>& expr_gen,
  24570. const details::operator_type o0,
  24571. const details::operator_type o1,
  24572. const details::operator_type o2)
  24573. {
  24574. return (details::build_string() << "((t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24575. }
  24576. };
  24577. struct synthesize_vocovoc_expression3
  24578. {
  24579. typedef typename vocovoc_t::type3 node_type;
  24580. typedef typename vocovoc_t::sf4_type sf4_type;
  24581. typedef typename node_type::T0 T0;
  24582. typedef typename node_type::T1 T1;
  24583. typedef typename node_type::T2 T2;
  24584. typedef typename node_type::T3 T3;
  24585. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24586. const details::operator_type& operation,
  24587. expression_node_ptr (&branch)[2])
  24588. {
  24589. // ((v0 o0 c0) o1 v1) o2 c1
  24590. typedef typename synthesize_vocov_expression0::node_type vocov_t;
  24591. const vocov_t* vocov = static_cast<const vocov_t*>(branch[0]);
  24592. const Type& v0 = vocov->t0();
  24593. const Type c0 = vocov->t1();
  24594. const Type& v1 = vocov->t2();
  24595. const Type c1 = static_cast<details::literal_node<Type>*>(branch[1])->value();
  24596. const details::operator_type o0 = expr_gen.get_operator(vocov->f0());
  24597. const details::operator_type o1 = expr_gen.get_operator(vocov->f1());
  24598. const details::operator_type o2 = operation;
  24599. binary_functor_t f0 = vocov->f0();
  24600. binary_functor_t f1 = vocov->f1();
  24601. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24602. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24603. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24604. expression_node_ptr result = error_node();
  24605. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result))
  24606. return result;
  24607. else if (!expr_gen.valid_operator(o2,f2))
  24608. return error_node();
  24609. exprtk_debug(("((v0 o0 c0) o1 v1) o2 c1\n"));
  24610. return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,v1,c1,f0,f1,f2);
  24611. }
  24612. static inline std::string id(expression_generator<Type>& expr_gen,
  24613. const details::operator_type o0,
  24614. const details::operator_type o1,
  24615. const details::operator_type o2)
  24616. {
  24617. return (details::build_string() << "((t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24618. }
  24619. };
  24620. struct synthesize_covovoc_expression3
  24621. {
  24622. typedef typename covovoc_t::type3 node_type;
  24623. typedef typename covovoc_t::sf4_type sf4_type;
  24624. typedef typename node_type::T0 T0;
  24625. typedef typename node_type::T1 T1;
  24626. typedef typename node_type::T2 T2;
  24627. typedef typename node_type::T3 T3;
  24628. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24629. const details::operator_type& operation,
  24630. expression_node_ptr (&branch)[2])
  24631. {
  24632. // ((c0 o0 v0) o1 v1) o2 c1
  24633. typedef typename synthesize_covov_expression0::node_type covov_t;
  24634. const covov_t* covov = static_cast<const covov_t*>(branch[0]);
  24635. const Type c0 = covov->t0();
  24636. const Type& v0 = covov->t1();
  24637. const Type& v1 = covov->t2();
  24638. const Type c1 = static_cast<details::literal_node<Type>*>(branch[1])->value();
  24639. const details::operator_type o0 = expr_gen.get_operator(covov->f0());
  24640. const details::operator_type o1 = expr_gen.get_operator(covov->f1());
  24641. const details::operator_type o2 = operation;
  24642. binary_functor_t f0 = covov->f0();
  24643. binary_functor_t f1 = covov->f1();
  24644. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24645. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24646. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24647. expression_node_ptr result = error_node();
  24648. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result))
  24649. return result;
  24650. else if (!expr_gen.valid_operator(o2,f2))
  24651. return error_node();
  24652. exprtk_debug(("((c0 o0 v0) o1 v1) o2 c1\n"));
  24653. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,v1,c1,f0,f1,f2);
  24654. }
  24655. static inline std::string id(expression_generator<Type>& expr_gen,
  24656. const details::operator_type o0,
  24657. const details::operator_type o1,
  24658. const details::operator_type o2)
  24659. {
  24660. return (details::build_string() << "((t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24661. }
  24662. };
  24663. struct synthesize_vococov_expression3
  24664. {
  24665. typedef typename vococov_t::type3 node_type;
  24666. typedef typename vococov_t::sf4_type sf4_type;
  24667. typedef typename node_type::T0 T0;
  24668. typedef typename node_type::T1 T1;
  24669. typedef typename node_type::T2 T2;
  24670. typedef typename node_type::T3 T3;
  24671. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24672. const details::operator_type& operation,
  24673. expression_node_ptr (&branch)[2])
  24674. {
  24675. // ((v0 o0 c0) o1 c1) o2 v1
  24676. typedef typename synthesize_vococ_expression0::node_type vococ_t;
  24677. const vococ_t* vococ = static_cast<const vococ_t*>(branch[0]);
  24678. const Type& v0 = vococ->t0();
  24679. const Type c0 = vococ->t1();
  24680. const Type c1 = vococ->t2();
  24681. const Type& v1 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24682. const details::operator_type o0 = expr_gen.get_operator(vococ->f0());
  24683. const details::operator_type o1 = expr_gen.get_operator(vococ->f1());
  24684. const details::operator_type o2 = operation;
  24685. binary_functor_t f0 = vococ->f0();
  24686. binary_functor_t f1 = vococ->f1();
  24687. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24688. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24689. expression_node_ptr result = error_node();
  24690. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,c1,v1,result))
  24691. return result;
  24692. else if (!expr_gen.valid_operator(o2,f2))
  24693. return error_node();
  24694. exprtk_debug(("((v0 o0 c0) o1 c1) o2 v1\n"));
  24695. return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,c1,v1,f0,f1,f2);
  24696. }
  24697. static inline std::string id(expression_generator<Type>& expr_gen,
  24698. const details::operator_type o0,
  24699. const details::operator_type o1,
  24700. const details::operator_type o2)
  24701. {
  24702. return (details::build_string() << "((t" << expr_gen.to_str(o0) << "t)" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24703. }
  24704. };
  24705. struct synthesize_vovovov_expression4
  24706. {
  24707. typedef typename vovovov_t::type4 node_type;
  24708. typedef typename vovovov_t::sf4_type sf4_type;
  24709. typedef typename node_type::T0 T0;
  24710. typedef typename node_type::T1 T1;
  24711. typedef typename node_type::T2 T2;
  24712. typedef typename node_type::T3 T3;
  24713. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24714. const details::operator_type& operation,
  24715. expression_node_ptr (&branch)[2])
  24716. {
  24717. // (v0 o0 (v1 o1 v2)) o2 v3
  24718. typedef typename synthesize_vovov_expression1::node_type vovov_t;
  24719. const vovov_t* vovov = static_cast<const vovov_t*>(branch[0]);
  24720. const Type& v0 = vovov->t0();
  24721. const Type& v1 = vovov->t1();
  24722. const Type& v2 = vovov->t2();
  24723. const Type& v3 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24724. const details::operator_type o0 = expr_gen.get_operator(vovov->f0());
  24725. const details::operator_type o1 = expr_gen.get_operator(vovov->f1());
  24726. const details::operator_type o2 = operation;
  24727. binary_functor_t f0 = vovov->f0();
  24728. binary_functor_t f1 = vovov->f1();
  24729. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24730. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24731. expression_node_ptr result = error_node();
  24732. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,v3,result))
  24733. return result;
  24734. else if (!expr_gen.valid_operator(o2,f2))
  24735. return error_node();
  24736. exprtk_debug(("(v0 o0 (v1 o1 v2)) o2 v3\n"));
  24737. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,v3,f0,f1,f2);
  24738. }
  24739. static inline std::string id(expression_generator<Type>& expr_gen,
  24740. const details::operator_type o0,
  24741. const details::operator_type o1,
  24742. const details::operator_type o2)
  24743. {
  24744. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24745. }
  24746. };
  24747. struct synthesize_vovovoc_expression4
  24748. {
  24749. typedef typename vovovoc_t::type4 node_type;
  24750. typedef typename vovovoc_t::sf4_type sf4_type;
  24751. typedef typename node_type::T0 T0;
  24752. typedef typename node_type::T1 T1;
  24753. typedef typename node_type::T2 T2;
  24754. typedef typename node_type::T3 T3;
  24755. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24756. const details::operator_type& operation,
  24757. expression_node_ptr (&branch)[2])
  24758. {
  24759. // ((v0 o0 (v1 o1 v2)) o2 c)
  24760. typedef typename synthesize_vovov_expression1::node_type vovov_t;
  24761. const vovov_t* vovov = static_cast<const vovov_t*>(branch[0]);
  24762. const Type& v0 = vovov->t0();
  24763. const Type& v1 = vovov->t1();
  24764. const Type& v2 = vovov->t2();
  24765. const Type c = static_cast<details::literal_node<Type>*>(branch[1])->value();
  24766. const details::operator_type o0 = expr_gen.get_operator(vovov->f0());
  24767. const details::operator_type o1 = expr_gen.get_operator(vovov->f1());
  24768. const details::operator_type o2 = operation;
  24769. binary_functor_t f0 = vovov->f0();
  24770. binary_functor_t f1 = vovov->f1();
  24771. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24772. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24773. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24774. expression_node_ptr result = error_node();
  24775. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,v2,c,result))
  24776. return result;
  24777. else if (!expr_gen.valid_operator(o2,f2))
  24778. return error_node();
  24779. exprtk_debug(("((v0 o0 (v1 o1 v2)) o2 c)\n"));
  24780. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,v2,c,f0,f1,f2);
  24781. }
  24782. static inline std::string id(expression_generator<Type>& expr_gen,
  24783. const details::operator_type o0,
  24784. const details::operator_type o1,
  24785. const details::operator_type o2)
  24786. {
  24787. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24788. }
  24789. };
  24790. struct synthesize_vovocov_expression4
  24791. {
  24792. typedef typename vovocov_t::type4 node_type;
  24793. typedef typename vovocov_t::sf4_type sf4_type;
  24794. typedef typename node_type::T0 T0;
  24795. typedef typename node_type::T1 T1;
  24796. typedef typename node_type::T2 T2;
  24797. typedef typename node_type::T3 T3;
  24798. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24799. const details::operator_type& operation,
  24800. expression_node_ptr (&branch)[2])
  24801. {
  24802. // ((v0 o0 (v1 o1 c)) o2 v1)
  24803. typedef typename synthesize_vovoc_expression1::node_type vovoc_t;
  24804. const vovoc_t* vovoc = static_cast<const vovoc_t*>(branch[0]);
  24805. const Type& v0 = vovoc->t0();
  24806. const Type& v1 = vovoc->t1();
  24807. const Type c = vovoc->t2();
  24808. const Type& v2 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24809. const details::operator_type o0 = expr_gen.get_operator(vovoc->f0());
  24810. const details::operator_type o1 = expr_gen.get_operator(vovoc->f1());
  24811. const details::operator_type o2 = operation;
  24812. binary_functor_t f0 = vovoc->f0();
  24813. binary_functor_t f1 = vovoc->f1();
  24814. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24815. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24816. expression_node_ptr result = error_node();
  24817. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,v1,c,v2,result))
  24818. return result;
  24819. else if (!expr_gen.valid_operator(o2,f2))
  24820. return error_node();
  24821. exprtk_debug(("((v0 o0 (v1 o1 c)) o2 v1)\n"));
  24822. return node_type::allocate(*(expr_gen.node_allocator_),v0,v1,c,v2,f0,f1,f2);
  24823. }
  24824. static inline std::string id(expression_generator<Type>& expr_gen,
  24825. const details::operator_type o0,
  24826. const details::operator_type o1,
  24827. const details::operator_type o2)
  24828. {
  24829. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24830. }
  24831. };
  24832. struct synthesize_vocovov_expression4
  24833. {
  24834. typedef typename vocovov_t::type4 node_type;
  24835. typedef typename vocovov_t::sf4_type sf4_type;
  24836. typedef typename node_type::T0 T0;
  24837. typedef typename node_type::T1 T1;
  24838. typedef typename node_type::T2 T2;
  24839. typedef typename node_type::T3 T3;
  24840. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24841. const details::operator_type& operation,
  24842. expression_node_ptr (&branch)[2])
  24843. {
  24844. // ((v0 o0 (c o1 v1)) o2 v2)
  24845. typedef typename synthesize_vocov_expression1::node_type vocov_t;
  24846. const vocov_t* vocov = static_cast<const vocov_t*>(branch[0]);
  24847. const Type& v0 = vocov->t0();
  24848. const Type c = vocov->t1();
  24849. const Type& v1 = vocov->t2();
  24850. const Type& v2 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24851. const details::operator_type o0 = expr_gen.get_operator(vocov->f0());
  24852. const details::operator_type o1 = expr_gen.get_operator(vocov->f1());
  24853. const details::operator_type o2 = operation;
  24854. binary_functor_t f0 = vocov->f0();
  24855. binary_functor_t f1 = vocov->f1();
  24856. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24857. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24858. expression_node_ptr result = error_node();
  24859. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c,v1,v2,result))
  24860. return result;
  24861. else if (!expr_gen.valid_operator(o2,f2))
  24862. return error_node();
  24863. exprtk_debug(("((v0 o0 (c o1 v1)) o2 v2)\n"));
  24864. return node_type::allocate(*(expr_gen.node_allocator_),v0,c,v1,v2,f0,f1,f2);
  24865. }
  24866. static inline std::string id(expression_generator<Type>& expr_gen,
  24867. const details::operator_type o0,
  24868. const details::operator_type o1,
  24869. const details::operator_type o2)
  24870. {
  24871. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24872. }
  24873. };
  24874. struct synthesize_covovov_expression4
  24875. {
  24876. typedef typename covovov_t::type4 node_type;
  24877. typedef typename covovov_t::sf4_type sf4_type;
  24878. typedef typename node_type::T0 T0;
  24879. typedef typename node_type::T1 T1;
  24880. typedef typename node_type::T2 T2;
  24881. typedef typename node_type::T3 T3;
  24882. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24883. const details::operator_type& operation,
  24884. expression_node_ptr (&branch)[2])
  24885. {
  24886. // ((c o0 (v0 o1 v1)) o2 v2)
  24887. typedef typename synthesize_covov_expression1::node_type covov_t;
  24888. const covov_t* covov = static_cast<const covov_t*>(branch[0]);
  24889. const Type c = covov->t0();
  24890. const Type& v0 = covov->t1();
  24891. const Type& v1 = covov->t2();
  24892. const Type& v2 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24893. const details::operator_type o0 = expr_gen.get_operator(covov->f0());
  24894. const details::operator_type o1 = expr_gen.get_operator(covov->f1());
  24895. const details::operator_type o2 = operation;
  24896. binary_functor_t f0 = covov->f0();
  24897. binary_functor_t f1 = covov->f1();
  24898. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24899. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24900. expression_node_ptr result = error_node();
  24901. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c,v0,v1,v2,result))
  24902. return result;
  24903. else if (!expr_gen.valid_operator(o2,f2))
  24904. return error_node();
  24905. exprtk_debug(("((c o0 (v0 o1 v1)) o2 v2)\n"));
  24906. return node_type::allocate(*(expr_gen.node_allocator_),c,v0,v1,v2,f0,f1,f2);
  24907. }
  24908. static inline std::string id(expression_generator<Type>& expr_gen,
  24909. const details::operator_type o0,
  24910. const details::operator_type o1,
  24911. const details::operator_type o2)
  24912. {
  24913. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24914. }
  24915. };
  24916. struct synthesize_covocov_expression4
  24917. {
  24918. typedef typename covocov_t::type4 node_type;
  24919. typedef typename covocov_t::sf4_type sf4_type;
  24920. typedef typename node_type::T0 T0;
  24921. typedef typename node_type::T1 T1;
  24922. typedef typename node_type::T2 T2;
  24923. typedef typename node_type::T3 T3;
  24924. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24925. const details::operator_type& operation,
  24926. expression_node_ptr (&branch)[2])
  24927. {
  24928. // ((c0 o0 (v0 o1 c1)) o2 v1)
  24929. typedef typename synthesize_covoc_expression1::node_type covoc_t;
  24930. const covoc_t* covoc = static_cast<const covoc_t*>(branch[0]);
  24931. const Type c0 = covoc->t0();
  24932. const Type& v0 = covoc->t1();
  24933. const Type c1 = covoc->t2();
  24934. const Type& v1 = static_cast<details::variable_node<Type>*>(branch[1])->ref();
  24935. const details::operator_type o0 = expr_gen.get_operator(covoc->f0());
  24936. const details::operator_type o1 = expr_gen.get_operator(covoc->f1());
  24937. const details::operator_type o2 = operation;
  24938. binary_functor_t f0 = covoc->f0();
  24939. binary_functor_t f1 = covoc->f1();
  24940. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24941. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24942. expression_node_ptr result = error_node();
  24943. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,c1,v1,result))
  24944. return result;
  24945. else if (!expr_gen.valid_operator(o2,f2))
  24946. return error_node();
  24947. exprtk_debug(("((c0 o0 (v0 o1 c1)) o2 v1)\n"));
  24948. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,c1,v1,f0,f1,f2);
  24949. }
  24950. static inline std::string id(expression_generator<Type>& expr_gen,
  24951. const details::operator_type o0,
  24952. const details::operator_type o1,
  24953. const details::operator_type o2)
  24954. {
  24955. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24956. }
  24957. };
  24958. struct synthesize_vocovoc_expression4
  24959. {
  24960. typedef typename vocovoc_t::type4 node_type;
  24961. typedef typename vocovoc_t::sf4_type sf4_type;
  24962. typedef typename node_type::T0 T0;
  24963. typedef typename node_type::T1 T1;
  24964. typedef typename node_type::T2 T2;
  24965. typedef typename node_type::T3 T3;
  24966. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  24967. const details::operator_type& operation,
  24968. expression_node_ptr (&branch)[2])
  24969. {
  24970. // ((v0 o0 (c0 o1 v1)) o2 c1)
  24971. typedef typename synthesize_vocov_expression1::node_type vocov_t;
  24972. const vocov_t* vocov = static_cast<const vocov_t*>(branch[0]);
  24973. const Type& v0 = vocov->t0();
  24974. const Type c0 = vocov->t1();
  24975. const Type& v1 = vocov->t2();
  24976. const Type c1 = static_cast<details::literal_node<Type>*>(branch[1])->value();
  24977. const details::operator_type o0 = expr_gen.get_operator(vocov->f0());
  24978. const details::operator_type o1 = expr_gen.get_operator(vocov->f1());
  24979. const details::operator_type o2 = operation;
  24980. binary_functor_t f0 = vocov->f0();
  24981. binary_functor_t f1 = vocov->f1();
  24982. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  24983. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  24984. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  24985. expression_node_ptr result = error_node();
  24986. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),v0,c0,v1,c1,result))
  24987. return result;
  24988. else if (!expr_gen.valid_operator(o2,f2))
  24989. return error_node();
  24990. exprtk_debug(("((v0 o0 (c0 o1 v1)) o2 c1)\n"));
  24991. return node_type::allocate(*(expr_gen.node_allocator_),v0,c0,v1,c1,f0,f1,f2);
  24992. }
  24993. static inline std::string id(expression_generator<Type>& expr_gen,
  24994. const details::operator_type o0,
  24995. const details::operator_type o1,
  24996. const details::operator_type o2)
  24997. {
  24998. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  24999. }
  25000. };
  25001. struct synthesize_covovoc_expression4
  25002. {
  25003. typedef typename covovoc_t::type4 node_type;
  25004. typedef typename covovoc_t::sf4_type sf4_type;
  25005. typedef typename node_type::T0 T0;
  25006. typedef typename node_type::T1 T1;
  25007. typedef typename node_type::T2 T2;
  25008. typedef typename node_type::T3 T3;
  25009. static inline expression_node_ptr process(expression_generator<Type>& expr_gen,
  25010. const details::operator_type& operation,
  25011. expression_node_ptr (&branch)[2])
  25012. {
  25013. // ((c0 o0 (v0 o1 v1)) o2 c1)
  25014. typedef typename synthesize_covov_expression1::node_type covov_t;
  25015. const covov_t* covov = static_cast<const covov_t*>(branch[0]);
  25016. const Type c0 = covov->t0();
  25017. const Type& v0 = covov->t1();
  25018. const Type& v1 = covov->t2();
  25019. const Type c1 = static_cast<details::literal_node<Type>*>(branch[1])->value();
  25020. const details::operator_type o0 = expr_gen.get_operator(covov->f0());
  25021. const details::operator_type o1 = expr_gen.get_operator(covov->f1());
  25022. const details::operator_type o2 = operation;
  25023. binary_functor_t f0 = covov->f0();
  25024. binary_functor_t f1 = covov->f1();
  25025. binary_functor_t f2 = reinterpret_cast<binary_functor_t>(0);
  25026. details::free_node(*(expr_gen.node_allocator_),branch[0]);
  25027. details::free_node(*(expr_gen.node_allocator_),branch[1]);
  25028. expression_node_ptr result = error_node();
  25029. if (synthesize_sf4ext_expression::template compile<T0,T1,T2,T3>(expr_gen,id(expr_gen,o0,o1,o2),c0,v0,v1,c1,result))
  25030. return result;
  25031. else if (!expr_gen.valid_operator(o2,f2))
  25032. return error_node();
  25033. exprtk_debug(("((c0 o0 (v0 o1 v1)) o2 c1)\n"));
  25034. return node_type::allocate(*(expr_gen.node_allocator_),c0,v0,v1,c1,f0,f1,f2);
  25035. }
  25036. static inline std::string id(expression_generator<Type>& expr_gen,
  25037. const details::operator_type o0,
  25038. const details::operator_type o1,
  25039. const details::operator_type o2)
  25040. {
  25041. return (details::build_string() << "(t" << expr_gen.to_str(o0) << "(t" << expr_gen.to_str(o1) << "t)" << expr_gen.to_str(o2) << "t");
  25042. }
  25043. };
  25044. struct synthesize_vococov_expression4
  25045. {
  25046. typedef typename vococov_t::type4 node_type;
  25047. static inline expression_node_ptr process(expression_generator<Type>&, const details::operator_type&, expression_node_ptr (&)[2])
  25048. {
  25049. // ((v0 o0 (c0 o1 c1)) o2 v1) - Not possible
  25050. exprtk_debug(("((v0 o0 (c0 o1 c1)) o2 v1) - Not possible\n"));
  25051. return error_node();
  25052. }
  25053. static inline std::string id(expression_generator<Type>&,
  25054. const details::operator_type, const details::operator_type, const details::operator_type)
  25055. {
  25056. return "INVALID";
  25057. }
  25058. };
  25059. #endif
  25060. inline expression_node_ptr synthesize_uvouv_expression(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  25061. {
  25062. // Definition: uv o uv
  25063. details::operator_type o0 = static_cast<details::uv_base_node<Type>*>(branch[0])->operation();
  25064. details::operator_type o1 = static_cast<details::uv_base_node<Type>*>(branch[1])->operation();
  25065. const Type& v0 = static_cast<details::uv_base_node<Type>*>(branch[0])->v();
  25066. const Type& v1 = static_cast<details::uv_base_node<Type>*>(branch[1])->v();
  25067. unary_functor_t u0 = reinterpret_cast<unary_functor_t> (0);
  25068. unary_functor_t u1 = reinterpret_cast<unary_functor_t> (0);
  25069. binary_functor_t f = reinterpret_cast<binary_functor_t>(0);
  25070. if (!valid_operator(o0,u0))
  25071. return error_node();
  25072. else if (!valid_operator(o1,u1))
  25073. return error_node();
  25074. else if (!valid_operator(operation,f))
  25075. return error_node();
  25076. expression_node_ptr result = error_node();
  25077. if (
  25078. (details::e_neg == o0) &&
  25079. (details::e_neg == o1)
  25080. )
  25081. {
  25082. switch (operation)
  25083. {
  25084. // (-v0 + -v1) --> -(v0 + v1)
  25085. case details::e_add : result = (*this)(details::e_neg,
  25086. node_allocator_->
  25087. allocate_rr<typename details::
  25088. vov_node<Type,details::add_op<Type> > >(v0,v1));
  25089. exprtk_debug(("(-v0 + -v1) --> -(v0 + v1)\n"));
  25090. break;
  25091. // (-v0 - -v1) --> (v1 - v0)
  25092. case details::e_sub : result = node_allocator_->
  25093. allocate_rr<typename details::
  25094. vov_node<Type,details::sub_op<Type> > >(v1,v0);
  25095. exprtk_debug(("(-v0 - -v1) --> (v1 - v0)\n"));
  25096. break;
  25097. // (-v0 * -v1) --> (v0 * v1)
  25098. case details::e_mul : result = node_allocator_->
  25099. allocate_rr<typename details::
  25100. vov_node<Type,details::mul_op<Type> > >(v0,v1);
  25101. exprtk_debug(("(-v0 * -v1) --> (v0 * v1)\n"));
  25102. break;
  25103. // (-v0 / -v1) --> (v0 / v1)
  25104. case details::e_div : result = node_allocator_->
  25105. allocate_rr<typename details::
  25106. vov_node<Type,details::div_op<Type> > >(v0,v1);
  25107. exprtk_debug(("(-v0 / -v1) --> (v0 / v1)\n"));
  25108. break;
  25109. default : break;
  25110. }
  25111. }
  25112. if (0 == result)
  25113. {
  25114. result = node_allocator_->
  25115. allocate_rrrrr<typename details::uvouv_node<Type> >(v0,v1,u0,u1,f);
  25116. }
  25117. details::free_all_nodes(*node_allocator_,branch);
  25118. return result;
  25119. }
  25120. #undef basic_opr_switch_statements
  25121. #undef extended_opr_switch_statements
  25122. #undef unary_opr_switch_statements
  25123. #ifndef exprtk_disable_string_capabilities
  25124. #define string_opr_switch_statements \
  25125. case_stmt(details:: e_lt ,details:: lt_op) \
  25126. case_stmt(details:: e_lte ,details:: lte_op) \
  25127. case_stmt(details:: e_gt ,details:: gt_op) \
  25128. case_stmt(details:: e_gte ,details:: gte_op) \
  25129. case_stmt(details:: e_eq ,details:: eq_op) \
  25130. case_stmt(details:: e_ne ,details:: ne_op) \
  25131. case_stmt(details::e_in ,details:: in_op) \
  25132. case_stmt(details::e_like ,details:: like_op) \
  25133. case_stmt(details::e_ilike,details::ilike_op) \
  25134. template <typename T0, typename T1>
  25135. inline expression_node_ptr synthesize_str_xrox_expression_impl(const details::operator_type& opr,
  25136. T0 s0, T1 s1,
  25137. range_t rp0)
  25138. {
  25139. switch (opr)
  25140. {
  25141. #define case_stmt(op0,op1) \
  25142. case op0 : return node_allocator_-> \
  25143. allocate_ttt<typename details::str_xrox_node<Type,T0,T1,range_t,op1<Type> >,T0,T1> \
  25144. (s0,s1,rp0); \
  25145. string_opr_switch_statements
  25146. #undef case_stmt
  25147. default : return error_node();
  25148. }
  25149. }
  25150. template <typename T0, typename T1>
  25151. inline expression_node_ptr synthesize_str_xoxr_expression_impl(const details::operator_type& opr,
  25152. T0 s0, T1 s1,
  25153. range_t rp1)
  25154. {
  25155. switch (opr)
  25156. {
  25157. #define case_stmt(op0,op1) \
  25158. case op0 : return node_allocator_-> \
  25159. allocate_ttt<typename details::str_xoxr_node<Type,T0,T1,range_t,op1<Type> >,T0,T1> \
  25160. (s0,s1,rp1); \
  25161. string_opr_switch_statements
  25162. #undef case_stmt
  25163. default : return error_node();
  25164. }
  25165. }
  25166. template <typename T0, typename T1>
  25167. inline expression_node_ptr synthesize_str_xroxr_expression_impl(const details::operator_type& opr,
  25168. T0 s0, T1 s1,
  25169. range_t rp0, range_t rp1)
  25170. {
  25171. switch (opr)
  25172. {
  25173. #define case_stmt(op0,op1) \
  25174. case op0 : return node_allocator_-> \
  25175. allocate_tttt<typename details::str_xroxr_node<Type,T0,T1,range_t,op1<Type> >,T0,T1> \
  25176. (s0,s1,rp0,rp1); \
  25177. string_opr_switch_statements
  25178. #undef case_stmt
  25179. default : return error_node();
  25180. }
  25181. }
  25182. template <typename T0, typename T1>
  25183. inline expression_node_ptr synthesize_sos_expression_impl(const details::operator_type& opr, T0 s0, T1 s1)
  25184. {
  25185. switch (opr)
  25186. {
  25187. #define case_stmt(op0,op1) \
  25188. case op0 : return node_allocator_-> \
  25189. allocate_tt<typename details::sos_node<Type,T0,T1,op1<Type> >,T0,T1>(s0,s1); \
  25190. string_opr_switch_statements
  25191. #undef case_stmt
  25192. default : return error_node();
  25193. }
  25194. }
  25195. inline expression_node_ptr synthesize_sos_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25196. {
  25197. std::string& s0 = static_cast<details::stringvar_node<Type>*>(branch[0])->ref();
  25198. std::string& s1 = static_cast<details::stringvar_node<Type>*>(branch[1])->ref();
  25199. return synthesize_sos_expression_impl<std::string&,std::string&>(opr,s0,s1);
  25200. }
  25201. inline expression_node_ptr synthesize_sros_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25202. {
  25203. std::string& s0 = static_cast<details::string_range_node<Type>*>(branch[0])->ref ();
  25204. std::string& s1 = static_cast<details::stringvar_node<Type>*> (branch[1])->ref ();
  25205. range_t rp0 = static_cast<details::string_range_node<Type>*>(branch[0])->range();
  25206. static_cast<details::string_range_node<Type>*>(branch[0])->range_ref().clear();
  25207. free_node(*node_allocator_,branch[0]);
  25208. return synthesize_str_xrox_expression_impl<std::string&,std::string&>(opr,s0,s1,rp0);
  25209. }
  25210. inline expression_node_ptr synthesize_sosr_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25211. {
  25212. std::string& s0 = static_cast<details::stringvar_node<Type>*> (branch[0])->ref ();
  25213. std::string& s1 = static_cast<details::string_range_node<Type>*>(branch[1])->ref ();
  25214. range_t rp1 = static_cast<details::string_range_node<Type>*>(branch[1])->range();
  25215. static_cast<details::string_range_node<Type>*>(branch[1])->range_ref().clear();
  25216. free_node(*node_allocator_,branch[1]);
  25217. return synthesize_str_xoxr_expression_impl<std::string&,std::string&>(opr,s0,s1,rp1);
  25218. }
  25219. inline expression_node_ptr synthesize_socsr_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25220. {
  25221. std::string& s0 = static_cast<details::stringvar_node<Type>*> (branch[0])->ref ();
  25222. std::string s1 = static_cast<details::const_string_range_node<Type>*>(branch[1])->str ();
  25223. range_t rp1 = static_cast<details::const_string_range_node<Type>*>(branch[1])->range();
  25224. static_cast<details::const_string_range_node<Type>*>(branch[1])->range_ref().clear();
  25225. free_node(*node_allocator_,branch[1]);
  25226. return synthesize_str_xoxr_expression_impl<std::string&,const std::string>(opr,s0,s1,rp1);
  25227. }
  25228. inline expression_node_ptr synthesize_srosr_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25229. {
  25230. std::string& s0 = static_cast<details::string_range_node<Type>*>(branch[0])->ref ();
  25231. std::string& s1 = static_cast<details::string_range_node<Type>*>(branch[1])->ref ();
  25232. range_t rp0 = static_cast<details::string_range_node<Type>*>(branch[0])->range();
  25233. range_t rp1 = static_cast<details::string_range_node<Type>*>(branch[1])->range();
  25234. static_cast<details::string_range_node<Type>*>(branch[0])->range_ref().clear();
  25235. static_cast<details::string_range_node<Type>*>(branch[1])->range_ref().clear();
  25236. details::free_node(*node_allocator_,branch[0]);
  25237. details::free_node(*node_allocator_,branch[1]);
  25238. return synthesize_str_xroxr_expression_impl<std::string&,std::string&>(opr,s0,s1,rp0,rp1);
  25239. }
  25240. inline expression_node_ptr synthesize_socs_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25241. {
  25242. std::string& s0 = static_cast< details::stringvar_node<Type>*>(branch[0])->ref();
  25243. std::string s1 = static_cast<details::string_literal_node<Type>*>(branch[1])->str();
  25244. details::free_node(*node_allocator_,branch[1]);
  25245. return synthesize_sos_expression_impl<std::string&,const std::string>(opr,s0,s1);
  25246. }
  25247. inline expression_node_ptr synthesize_csos_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25248. {
  25249. std::string s0 = static_cast<details::string_literal_node<Type>*>(branch[0])->str();
  25250. std::string& s1 = static_cast< details::stringvar_node<Type>*>(branch[1])->ref();
  25251. details::free_node(*node_allocator_,branch[0]);
  25252. return synthesize_sos_expression_impl<const std::string,std::string&>(opr,s0,s1);
  25253. }
  25254. inline expression_node_ptr synthesize_csosr_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25255. {
  25256. std::string s0 = static_cast<details::string_literal_node<Type>*>(branch[0])->str ();
  25257. std::string& s1 = static_cast<details::string_range_node<Type>*> (branch[1])->ref ();
  25258. range_t rp1 = static_cast<details::string_range_node<Type>*> (branch[1])->range();
  25259. static_cast<details::string_range_node<Type>*>(branch[1])->range_ref().clear();
  25260. details::free_node(*node_allocator_,branch[0]);
  25261. details::free_node(*node_allocator_,branch[1]);
  25262. return synthesize_str_xoxr_expression_impl<const std::string,std::string&>(opr,s0,s1,rp1);
  25263. }
  25264. inline expression_node_ptr synthesize_srocs_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25265. {
  25266. std::string& s0 = static_cast<details::string_range_node<Type>*> (branch[0])->ref ();
  25267. std::string s1 = static_cast<details::string_literal_node<Type>*>(branch[1])->str ();
  25268. range_t rp0 = static_cast<details::string_range_node<Type>*> (branch[0])->range();
  25269. static_cast<details::string_range_node<Type>*>(branch[0])->range_ref().clear();
  25270. details::free_node(*node_allocator_,branch[0]);
  25271. details::free_node(*node_allocator_,branch[1]);
  25272. return synthesize_str_xrox_expression_impl<std::string&,const std::string>(opr,s0,s1,rp0);
  25273. }
  25274. inline expression_node_ptr synthesize_srocsr_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25275. {
  25276. std::string& s0 = static_cast<details::string_range_node<Type>*> (branch[0])->ref ();
  25277. std::string s1 = static_cast<details::const_string_range_node<Type>*>(branch[1])->str ();
  25278. range_t rp0 = static_cast<details::string_range_node<Type>*> (branch[0])->range();
  25279. range_t rp1 = static_cast<details::const_string_range_node<Type>*>(branch[1])->range();
  25280. static_cast<details::string_range_node<Type>*> (branch[0])->range_ref().clear();
  25281. static_cast<details::const_string_range_node<Type>*>(branch[1])->range_ref().clear();
  25282. details::free_node(*node_allocator_,branch[0]);
  25283. details::free_node(*node_allocator_,branch[1]);
  25284. return synthesize_str_xroxr_expression_impl<std::string&,const std::string>(opr,s0,s1,rp0,rp1);
  25285. }
  25286. inline expression_node_ptr synthesize_csocs_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25287. {
  25288. const std::string s0 = static_cast<details::string_literal_node<Type>*>(branch[0])->str();
  25289. const std::string s1 = static_cast<details::string_literal_node<Type>*>(branch[1])->str();
  25290. expression_node_ptr result = error_node();
  25291. if (details::e_add == opr)
  25292. result = node_allocator_->allocate_c<details::string_literal_node<Type> >(s0 + s1);
  25293. else if (details::e_in == opr)
  25294. result = node_allocator_->allocate_c<details::literal_node<Type> >(details::in_op<Type>::process(s0,s1));
  25295. else if (details::e_like == opr)
  25296. result = node_allocator_->allocate_c<details::literal_node<Type> >(details::like_op<Type>::process(s0,s1));
  25297. else if (details::e_ilike == opr)
  25298. result = node_allocator_->allocate_c<details::literal_node<Type> >(details::ilike_op<Type>::process(s0,s1));
  25299. else
  25300. {
  25301. expression_node_ptr temp = synthesize_sos_expression_impl<const std::string,const std::string>(opr,s0,s1);
  25302. Type v = temp->value();
  25303. details::free_node(*node_allocator_,temp);
  25304. result = node_allocator_->allocate<literal_node_t>(v);
  25305. }
  25306. details::free_all_nodes(*node_allocator_,branch);
  25307. return result;
  25308. }
  25309. inline expression_node_ptr synthesize_csocsr_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25310. {
  25311. const std::string s0 = static_cast<details::string_literal_node<Type>*> (branch[0])->str ();
  25312. std::string s1 = static_cast<details::const_string_range_node<Type>*>(branch[1])->str ();
  25313. range_t rp1 = static_cast<details::const_string_range_node<Type>*>(branch[1])->range();
  25314. static_cast<details::const_string_range_node<Type>*>(branch[1])->range_ref().clear();
  25315. free_node(*node_allocator_,branch[0]);
  25316. free_node(*node_allocator_,branch[1]);
  25317. return synthesize_str_xoxr_expression_impl<const std::string,const std::string>(opr,s0,s1,rp1);
  25318. }
  25319. inline expression_node_ptr synthesize_csros_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25320. {
  25321. std::string s0 = static_cast<details::const_string_range_node<Type>*>(branch[0])->str ();
  25322. std::string& s1 = static_cast<details::stringvar_node<Type>*> (branch[1])->ref ();
  25323. range_t rp0 = static_cast<details::const_string_range_node<Type>*>(branch[0])->range();
  25324. static_cast<details::const_string_range_node<Type>*>(branch[0])->range_ref().clear();
  25325. free_node(*node_allocator_,branch[0]);
  25326. return synthesize_str_xrox_expression_impl<const std::string,std::string&>(opr,s0,s1,rp0);
  25327. }
  25328. inline expression_node_ptr synthesize_csrosr_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25329. {
  25330. const std::string s0 = static_cast<details::const_string_range_node<Type>*>(branch[0])->str ();
  25331. std::string& s1 = static_cast<details::string_range_node<Type>*> (branch[1])->ref ();
  25332. range_t rp0 = static_cast<details::const_string_range_node<Type>*>(branch[0])->range();
  25333. range_t rp1 = static_cast<details::string_range_node<Type>*> (branch[1])->range();
  25334. static_cast<details::const_string_range_node<Type>*>(branch[0])->range_ref().clear();
  25335. static_cast<details::string_range_node<Type>*> (branch[1])->range_ref().clear();
  25336. free_node(*node_allocator_,branch[0]);
  25337. free_node(*node_allocator_,branch[1]);
  25338. return synthesize_str_xroxr_expression_impl<const std::string,std::string&>(opr,s0,s1,rp0,rp1);
  25339. }
  25340. inline expression_node_ptr synthesize_csrocs_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25341. {
  25342. std::string s0 = static_cast<details::const_string_range_node<Type>*>(branch[0])->str ();
  25343. const std::string s1 = static_cast<details::string_literal_node<Type>*> (branch[1])->str ();
  25344. range_t rp0 = static_cast<details::const_string_range_node<Type>*>(branch[0])->range();
  25345. static_cast<details::const_string_range_node<Type>*>(branch[0])->range_ref().clear();
  25346. details::free_all_nodes(*node_allocator_,branch);
  25347. return synthesize_str_xrox_expression_impl<const std::string,std::string>(opr,s0,s1,rp0);
  25348. }
  25349. inline expression_node_ptr synthesize_csrocsr_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25350. {
  25351. std::string s0 = static_cast<details::const_string_range_node<Type>*>(branch[0])->str ();
  25352. std::string s1 = static_cast<details::const_string_range_node<Type>*>(branch[1])->str ();
  25353. range_t rp0 = static_cast<details::const_string_range_node<Type>*>(branch[0])->range();
  25354. range_t rp1 = static_cast<details::const_string_range_node<Type>*>(branch[1])->range();
  25355. static_cast<details::const_string_range_node<Type>*>(branch[0])->range_ref().clear();
  25356. static_cast<details::const_string_range_node<Type>*>(branch[1])->range_ref().clear();
  25357. details::free_all_nodes(*node_allocator_,branch);
  25358. return synthesize_str_xroxr_expression_impl<const std::string,const std::string>(opr,s0,s1,rp0,rp1);
  25359. }
  25360. inline expression_node_ptr synthesize_strogen_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25361. {
  25362. switch (opr)
  25363. {
  25364. #define case_stmt(op0,op1) \
  25365. case op0 : return node_allocator_-> \
  25366. allocate_ttt<typename details::str_sogens_node<Type,op1<Type> > > \
  25367. (opr,branch[0],branch[1]); \
  25368. string_opr_switch_statements
  25369. #undef case_stmt
  25370. default : return error_node();
  25371. }
  25372. }
  25373. #endif
  25374. #ifndef exprtk_disable_string_capabilities
  25375. inline expression_node_ptr synthesize_string_expression(const details::operator_type& opr, expression_node_ptr (&branch)[2])
  25376. {
  25377. if ((0 == branch[0]) || (0 == branch[1]))
  25378. {
  25379. details::free_all_nodes(*node_allocator_,branch);
  25380. return error_node();
  25381. }
  25382. const bool b0_is_s = details::is_string_node (branch[0]);
  25383. const bool b0_is_cs = details::is_const_string_node (branch[0]);
  25384. const bool b0_is_sr = details::is_string_range_node (branch[0]);
  25385. const bool b0_is_csr = details::is_const_string_range_node(branch[0]);
  25386. const bool b1_is_s = details::is_string_node (branch[1]);
  25387. const bool b1_is_cs = details::is_const_string_node (branch[1]);
  25388. const bool b1_is_sr = details::is_string_range_node (branch[1]);
  25389. const bool b1_is_csr = details::is_const_string_range_node(branch[1]);
  25390. const bool b0_is_gen = details::is_string_assignment_node (branch[0]) ||
  25391. details::is_genricstring_range_node(branch[0]) ||
  25392. details::is_string_concat_node (branch[0]) ||
  25393. details::is_string_function_node (branch[0]) ||
  25394. details::is_string_condition_node (branch[0]) ||
  25395. details::is_string_ccondition_node (branch[0]) ;
  25396. const bool b1_is_gen = details::is_string_assignment_node (branch[1]) ||
  25397. details::is_genricstring_range_node(branch[1]) ||
  25398. details::is_string_concat_node (branch[1]) ||
  25399. details::is_string_function_node (branch[1]) ||
  25400. details::is_string_condition_node (branch[1]) ||
  25401. details::is_string_ccondition_node (branch[1]) ;
  25402. if (details::e_add == opr)
  25403. {
  25404. if (!b0_is_cs || !b1_is_cs)
  25405. {
  25406. return synthesize_expression<string_concat_node_t,2>(opr,branch);
  25407. }
  25408. }
  25409. if (b0_is_gen || b1_is_gen)
  25410. {
  25411. return synthesize_strogen_expression(opr,branch);
  25412. }
  25413. else if (b0_is_s)
  25414. {
  25415. if (b1_is_s ) return synthesize_sos_expression (opr,branch);
  25416. else if (b1_is_cs ) return synthesize_socs_expression (opr,branch);
  25417. else if (b1_is_sr ) return synthesize_sosr_expression (opr,branch);
  25418. else if (b1_is_csr) return synthesize_socsr_expression (opr,branch);
  25419. }
  25420. else if (b0_is_cs)
  25421. {
  25422. if (b1_is_s ) return synthesize_csos_expression (opr,branch);
  25423. else if (b1_is_cs ) return synthesize_csocs_expression (opr,branch);
  25424. else if (b1_is_sr ) return synthesize_csosr_expression (opr,branch);
  25425. else if (b1_is_csr) return synthesize_csocsr_expression(opr,branch);
  25426. }
  25427. else if (b0_is_sr)
  25428. {
  25429. if (b1_is_s ) return synthesize_sros_expression (opr,branch);
  25430. else if (b1_is_sr ) return synthesize_srosr_expression (opr,branch);
  25431. else if (b1_is_cs ) return synthesize_srocs_expression (opr,branch);
  25432. else if (b1_is_csr) return synthesize_srocsr_expression(opr,branch);
  25433. }
  25434. else if (b0_is_csr)
  25435. {
  25436. if (b1_is_s ) return synthesize_csros_expression (opr,branch);
  25437. else if (b1_is_sr ) return synthesize_csrosr_expression (opr,branch);
  25438. else if (b1_is_cs ) return synthesize_csrocs_expression (opr,branch);
  25439. else if (b1_is_csr) return synthesize_csrocsr_expression(opr,branch);
  25440. }
  25441. return error_node();
  25442. }
  25443. #else
  25444. inline expression_node_ptr synthesize_string_expression(const details::operator_type&, expression_node_ptr (&branch)[2])
  25445. {
  25446. details::free_all_nodes(*node_allocator_,branch);
  25447. return error_node();
  25448. }
  25449. #endif
  25450. #ifndef exprtk_disable_string_capabilities
  25451. inline expression_node_ptr synthesize_string_expression(const details::operator_type& opr, expression_node_ptr (&branch)[3])
  25452. {
  25453. if (details::e_inrange != opr)
  25454. return error_node();
  25455. else if ((0 == branch[0]) || (0 == branch[1]) || (0 == branch[2]))
  25456. {
  25457. details::free_all_nodes(*node_allocator_,branch);
  25458. return error_node();
  25459. }
  25460. else if (
  25461. details::is_const_string_node(branch[0]) &&
  25462. details::is_const_string_node(branch[1]) &&
  25463. details::is_const_string_node(branch[2])
  25464. )
  25465. {
  25466. const std::string s0 = static_cast<details::string_literal_node<Type>*>(branch[0])->str();
  25467. const std::string s1 = static_cast<details::string_literal_node<Type>*>(branch[1])->str();
  25468. const std::string s2 = static_cast<details::string_literal_node<Type>*>(branch[2])->str();
  25469. Type v = (((s0 <= s1) && (s1 <= s2)) ? Type(1) : Type(0));
  25470. details::free_all_nodes(*node_allocator_,branch);
  25471. return node_allocator_->allocate_c<details::literal_node<Type> >(v);
  25472. }
  25473. else if (
  25474. details::is_string_node(branch[0]) &&
  25475. details::is_string_node(branch[1]) &&
  25476. details::is_string_node(branch[2])
  25477. )
  25478. {
  25479. std::string& s0 = static_cast<details::stringvar_node<Type>*>(branch[0])->ref();
  25480. std::string& s1 = static_cast<details::stringvar_node<Type>*>(branch[1])->ref();
  25481. std::string& s2 = static_cast<details::stringvar_node<Type>*>(branch[2])->ref();
  25482. typedef typename details::sosos_node<Type,std::string&,std::string&,std::string&,details::inrange_op<Type> > inrange_t;
  25483. return node_allocator_->allocate_type<inrange_t,std::string&,std::string&,std::string&>(s0,s1,s2);
  25484. }
  25485. else if (
  25486. details::is_const_string_node(branch[0]) &&
  25487. details::is_string_node(branch[1]) &&
  25488. details::is_const_string_node(branch[2])
  25489. )
  25490. {
  25491. std::string s0 = static_cast<details::string_literal_node<Type>*>(branch[0])->str();
  25492. std::string& s1 = static_cast< details::stringvar_node<Type>*>(branch[1])->ref();
  25493. std::string s2 = static_cast<details::string_literal_node<Type>*>(branch[2])->str();
  25494. typedef typename details::sosos_node<Type,std::string,std::string&,std::string,details::inrange_op<Type> > inrange_t;
  25495. details::free_node(*node_allocator_,branch[0]);
  25496. details::free_node(*node_allocator_,branch[2]);
  25497. return node_allocator_->allocate_type<inrange_t,std::string,std::string&,std::string>(s0,s1,s2);
  25498. }
  25499. else if (
  25500. details::is_string_node(branch[0]) &&
  25501. details::is_const_string_node(branch[1]) &&
  25502. details::is_string_node(branch[2])
  25503. )
  25504. {
  25505. std::string& s0 = static_cast< details::stringvar_node<Type>*>(branch[0])->ref();
  25506. std::string s1 = static_cast<details::string_literal_node<Type>*>(branch[1])->str();
  25507. std::string& s2 = static_cast< details::stringvar_node<Type>*>(branch[2])->ref();
  25508. typedef typename details::sosos_node<Type,std::string&,std::string,std::string&,details::inrange_op<Type> > inrange_t;
  25509. details::free_node(*node_allocator_,branch[1]);
  25510. return node_allocator_->allocate_type<inrange_t,std::string&,std::string,std::string&>(s0,s1,s2);
  25511. }
  25512. else if (
  25513. details::is_string_node(branch[0]) &&
  25514. details::is_string_node(branch[1]) &&
  25515. details::is_const_string_node(branch[2])
  25516. )
  25517. {
  25518. std::string& s0 = static_cast< details::stringvar_node<Type>*>(branch[0])->ref();
  25519. std::string& s1 = static_cast< details::stringvar_node<Type>*>(branch[1])->ref();
  25520. std::string s2 = static_cast<details::string_literal_node<Type>*>(branch[2])->str();
  25521. typedef typename details::sosos_node<Type,std::string&,std::string&,std::string,details::inrange_op<Type> > inrange_t;
  25522. details::free_node(*node_allocator_,branch[2]);
  25523. return node_allocator_->allocate_type<inrange_t,std::string&,std::string&,std::string>(s0,s1,s2);
  25524. }
  25525. else if (
  25526. details::is_const_string_node(branch[0]) &&
  25527. details:: is_string_node(branch[1]) &&
  25528. details:: is_string_node(branch[2])
  25529. )
  25530. {
  25531. std::string s0 = static_cast<details::string_literal_node<Type>*>(branch[0])->str();
  25532. std::string& s1 = static_cast< details::stringvar_node<Type>*>(branch[1])->ref();
  25533. std::string& s2 = static_cast< details::stringvar_node<Type>*>(branch[2])->ref();
  25534. typedef typename details::sosos_node<Type,std::string,std::string&,std::string&,details::inrange_op<Type> > inrange_t;
  25535. details::free_node(*node_allocator_,branch[0]);
  25536. return node_allocator_->allocate_type<inrange_t,std::string,std::string&,std::string&>(s0,s1,s2);
  25537. }
  25538. else
  25539. return error_node();
  25540. }
  25541. #else
  25542. inline expression_node_ptr synthesize_string_expression(const details::operator_type&, expression_node_ptr (&branch)[3])
  25543. {
  25544. details::free_all_nodes(*node_allocator_,branch);
  25545. return error_node();
  25546. }
  25547. #endif
  25548. inline expression_node_ptr synthesize_null_expression(const details::operator_type& operation, expression_node_ptr (&branch)[2])
  25549. {
  25550. /*
  25551. Note: The following are the type promotion rules
  25552. that relate to operations that include 'null':
  25553. 0. null ==/!= null --> true false
  25554. 1. null operation null --> null
  25555. 2. x ==/!= null --> true/false
  25556. 3. null ==/!= x --> true/false
  25557. 4. x operation null --> x
  25558. 5. null operation x --> x
  25559. */
  25560. typedef typename details::null_eq_node<T> nulleq_node_t;
  25561. bool b0_null = details::is_null_node(branch[0]);
  25562. bool b1_null = details::is_null_node(branch[1]);
  25563. if (b0_null && b1_null)
  25564. {
  25565. expression_node_ptr result = error_node();
  25566. if (details::e_eq == operation)
  25567. result = node_allocator_->allocate_c<literal_node_t>(T(1));
  25568. else if (details::e_ne == operation)
  25569. result = node_allocator_->allocate_c<literal_node_t>(T(0));
  25570. if (result)
  25571. {
  25572. details::free_node(*node_allocator_,branch[0]);
  25573. details::free_node(*node_allocator_,branch[1]);
  25574. return result;
  25575. }
  25576. details::free_node(*node_allocator_,branch[1]);
  25577. return branch[0];
  25578. }
  25579. else if (details::e_eq == operation)
  25580. {
  25581. expression_node_ptr result = node_allocator_->
  25582. allocate_rc<nulleq_node_t>(branch[b0_null ? 0 : 1],true);
  25583. details::free_node(*node_allocator_,branch[b0_null ? 1 : 0]);
  25584. return result;
  25585. }
  25586. else if (details::e_ne == operation)
  25587. {
  25588. expression_node_ptr result = node_allocator_->
  25589. allocate_rc<nulleq_node_t>(branch[b0_null ? 0 : 1],false);
  25590. details::free_node(*node_allocator_,branch[b0_null ? 1 : 0]);
  25591. return result;
  25592. }
  25593. else if (b0_null)
  25594. {
  25595. details::free_node(*node_allocator_,branch[0]);
  25596. branch[0] = branch[1];
  25597. branch[1] = error_node();
  25598. }
  25599. else if (b1_null)
  25600. {
  25601. details::free_node(*node_allocator_,branch[1]);
  25602. branch[1] = error_node();
  25603. }
  25604. if (
  25605. (details::e_add == operation) || (details::e_sub == operation) ||
  25606. (details::e_mul == operation) || (details::e_div == operation) ||
  25607. (details::e_mod == operation) || (details::e_pow == operation)
  25608. )
  25609. {
  25610. return branch[0];
  25611. }
  25612. else if (
  25613. (details::e_lt == operation) || (details::e_lte == operation) ||
  25614. (details::e_gt == operation) || (details::e_gte == operation) ||
  25615. (details::e_and == operation) || (details::e_nand == operation) ||
  25616. (details::e_or == operation) || (details::e_nor == operation) ||
  25617. (details::e_xor == operation) || (details::e_xnor == operation) ||
  25618. (details::e_in == operation) || (details::e_like == operation) ||
  25619. (details::e_ilike == operation)
  25620. )
  25621. {
  25622. return node_allocator_->allocate_c<literal_node_t>(T(0));
  25623. }
  25624. details::free_node(*node_allocator_,branch[0]);
  25625. return node_allocator_->allocate<details::null_node<Type> >();
  25626. }
  25627. template <typename NodeType, std::size_t N>
  25628. inline expression_node_ptr synthesize_expression(const details::operator_type& operation, expression_node_ptr (&branch)[N])
  25629. {
  25630. if (
  25631. (details::e_in == operation) ||
  25632. (details::e_like == operation) ||
  25633. (details::e_ilike == operation)
  25634. )
  25635. return error_node();
  25636. else if (!details::all_nodes_valid<N>(branch))
  25637. {
  25638. free_all_nodes(*node_allocator_,branch);
  25639. return error_node();
  25640. }
  25641. else if ((details::e_default != operation))
  25642. {
  25643. // Attempt simple constant folding optimization.
  25644. expression_node_ptr expression_point = node_allocator_->allocate<NodeType>(operation,branch);
  25645. if (is_constant_foldable<N>(branch))
  25646. {
  25647. Type v = expression_point->value();
  25648. details::free_node(*node_allocator_,expression_point);
  25649. return node_allocator_->allocate<literal_node_t>(v);
  25650. }
  25651. else
  25652. return expression_point;
  25653. }
  25654. else
  25655. return error_node();
  25656. }
  25657. template <typename NodeType, std::size_t N>
  25658. inline expression_node_ptr synthesize_expression(F* f, expression_node_ptr (&branch)[N])
  25659. {
  25660. if (!details::all_nodes_valid<N>(branch))
  25661. {
  25662. free_all_nodes(*node_allocator_,branch);
  25663. return error_node();
  25664. }
  25665. typedef typename details::function_N_node<T,ifunction_t,N> function_N_node_t;
  25666. // Attempt simple constant folding optimization.
  25667. expression_node_ptr expression_point = node_allocator_->allocate<NodeType>(f);
  25668. function_N_node_t* func_node_ptr = dynamic_cast<function_N_node_t*>(expression_point);
  25669. if (0 == func_node_ptr)
  25670. {
  25671. free_all_nodes(*node_allocator_,branch);
  25672. return error_node();
  25673. }
  25674. else
  25675. func_node_ptr->init_branches(branch);
  25676. if (is_constant_foldable<N>(branch) && !f->has_side_effects())
  25677. {
  25678. Type v = expression_point->value();
  25679. details::free_node(*node_allocator_,expression_point);
  25680. return node_allocator_->allocate<literal_node_t>(v);
  25681. }
  25682. parser_->state_.activate_side_effect("synthesize_expression(function<NT,N>)");
  25683. return expression_point;
  25684. }
  25685. bool strength_reduction_enabled_;
  25686. details::node_allocator* node_allocator_;
  25687. synthesize_map_t synthesize_map_;
  25688. unary_op_map_t* unary_op_map_;
  25689. binary_op_map_t* binary_op_map_;
  25690. inv_binary_op_map_t* inv_binary_op_map_;
  25691. sf3_map_t* sf3_map_;
  25692. sf4_map_t* sf4_map_;
  25693. parser_t* parser_;
  25694. };
  25695. inline void set_error(const parser_error::type& error_type)
  25696. {
  25697. error_list_.push_back(error_type);
  25698. }
  25699. inline void remove_last_error()
  25700. {
  25701. if (!error_list_.empty())
  25702. {
  25703. error_list_.pop_back();
  25704. }
  25705. }
  25706. inline void set_synthesis_error(const std::string& synthesis_error_message)
  25707. {
  25708. if (synthesis_error_.empty())
  25709. {
  25710. synthesis_error_ = synthesis_error_message;
  25711. }
  25712. }
  25713. inline void register_local_vars(expression<T>& e)
  25714. {
  25715. for (std::size_t i = 0; i < sem_.size(); ++i)
  25716. {
  25717. scope_element& se = sem_.get_element(i);
  25718. if (
  25719. (scope_element::e_variable == se.type) ||
  25720. (scope_element::e_vecelem == se.type)
  25721. )
  25722. {
  25723. if (se.var_node)
  25724. {
  25725. e.register_local_var(se.var_node);
  25726. }
  25727. if (se.data)
  25728. {
  25729. e.register_local_data(se.data,1,0);
  25730. }
  25731. }
  25732. else if (scope_element::e_vector == se.type)
  25733. {
  25734. if (se.vec_node)
  25735. {
  25736. e.register_local_var(se.vec_node);
  25737. }
  25738. if (se.data)
  25739. {
  25740. e.register_local_data(se.data,se.size,1);
  25741. }
  25742. }
  25743. #ifndef exprtk_disable_string_capabilities
  25744. else if (scope_element::e_string == se.type)
  25745. {
  25746. if (se.str_node)
  25747. {
  25748. e.register_local_var(se.str_node);
  25749. }
  25750. if (se.data)
  25751. {
  25752. e.register_local_data(se.data,se.size,2);
  25753. }
  25754. }
  25755. #endif
  25756. se.var_node = 0;
  25757. se.vec_node = 0;
  25758. #ifndef exprtk_disable_string_capabilities
  25759. se.str_node = 0;
  25760. #endif
  25761. se.data = 0;
  25762. se.ref_count = 0;
  25763. se.active = false;
  25764. }
  25765. }
  25766. inline void register_return_results(expression<T>& e)
  25767. {
  25768. e.register_return_results(results_context_);
  25769. results_context_ = 0;
  25770. }
  25771. inline void load_unary_operations_map(unary_op_map_t& m)
  25772. {
  25773. #define register_unary_op(Op,UnaryFunctor) \
  25774. m.insert(std::make_pair(Op,UnaryFunctor<T>::process)); \
  25775. register_unary_op(details:: e_abs,details:: abs_op)
  25776. register_unary_op(details:: e_acos,details:: acos_op)
  25777. register_unary_op(details::e_acosh,details::acosh_op)
  25778. register_unary_op(details:: e_asin,details:: asin_op)
  25779. register_unary_op(details::e_asinh,details::asinh_op)
  25780. register_unary_op(details::e_atanh,details::atanh_op)
  25781. register_unary_op(details:: e_ceil,details:: ceil_op)
  25782. register_unary_op(details:: e_cos,details:: cos_op)
  25783. register_unary_op(details:: e_cosh,details:: cosh_op)
  25784. register_unary_op(details:: e_exp,details:: exp_op)
  25785. register_unary_op(details::e_expm1,details::expm1_op)
  25786. register_unary_op(details::e_floor,details::floor_op)
  25787. register_unary_op(details:: e_log,details:: log_op)
  25788. register_unary_op(details::e_log10,details::log10_op)
  25789. register_unary_op(details:: e_log2,details:: log2_op)
  25790. register_unary_op(details::e_log1p,details::log1p_op)
  25791. register_unary_op(details:: e_neg,details:: neg_op)
  25792. register_unary_op(details:: e_pos,details:: pos_op)
  25793. register_unary_op(details::e_round,details::round_op)
  25794. register_unary_op(details:: e_sin,details:: sin_op)
  25795. register_unary_op(details:: e_sinc,details:: sinc_op)
  25796. register_unary_op(details:: e_sinh,details:: sinh_op)
  25797. register_unary_op(details:: e_sqrt,details:: sqrt_op)
  25798. register_unary_op(details:: e_tan,details:: tan_op)
  25799. register_unary_op(details:: e_tanh,details:: tanh_op)
  25800. register_unary_op(details:: e_cot,details:: cot_op)
  25801. register_unary_op(details:: e_sec,details:: sec_op)
  25802. register_unary_op(details:: e_csc,details:: csc_op)
  25803. register_unary_op(details:: e_r2d,details:: r2d_op)
  25804. register_unary_op(details:: e_d2r,details:: d2r_op)
  25805. register_unary_op(details:: e_d2g,details:: d2g_op)
  25806. register_unary_op(details:: e_g2d,details:: g2d_op)
  25807. register_unary_op(details:: e_notl,details:: notl_op)
  25808. register_unary_op(details:: e_sgn,details:: sgn_op)
  25809. register_unary_op(details:: e_erf,details:: erf_op)
  25810. register_unary_op(details:: e_erfc,details:: erfc_op)
  25811. register_unary_op(details:: e_ncdf,details:: ncdf_op)
  25812. register_unary_op(details:: e_frac,details:: frac_op)
  25813. register_unary_op(details::e_trunc,details::trunc_op)
  25814. #undef register_unary_op
  25815. }
  25816. inline void load_binary_operations_map(binary_op_map_t& m)
  25817. {
  25818. typedef typename binary_op_map_t::value_type value_type;
  25819. #define register_binary_op(Op,BinaryFunctor) \
  25820. m.insert(value_type(Op,BinaryFunctor<T>::process)); \
  25821. register_binary_op(details:: e_add,details:: add_op)
  25822. register_binary_op(details:: e_sub,details:: sub_op)
  25823. register_binary_op(details:: e_mul,details:: mul_op)
  25824. register_binary_op(details:: e_div,details:: div_op)
  25825. register_binary_op(details:: e_mod,details:: mod_op)
  25826. register_binary_op(details:: e_pow,details:: pow_op)
  25827. register_binary_op(details:: e_lt,details:: lt_op)
  25828. register_binary_op(details:: e_lte,details:: lte_op)
  25829. register_binary_op(details:: e_gt,details:: gt_op)
  25830. register_binary_op(details:: e_gte,details:: gte_op)
  25831. register_binary_op(details:: e_eq,details:: eq_op)
  25832. register_binary_op(details:: e_ne,details:: ne_op)
  25833. register_binary_op(details:: e_and,details:: and_op)
  25834. register_binary_op(details::e_nand,details::nand_op)
  25835. register_binary_op(details:: e_or,details:: or_op)
  25836. register_binary_op(details:: e_nor,details:: nor_op)
  25837. register_binary_op(details:: e_xor,details:: xor_op)
  25838. register_binary_op(details::e_xnor,details::xnor_op)
  25839. #undef register_binary_op
  25840. }
  25841. inline void load_inv_binary_operations_map(inv_binary_op_map_t& m)
  25842. {
  25843. typedef typename inv_binary_op_map_t::value_type value_type;
  25844. #define register_binary_op(Op,BinaryFunctor) \
  25845. m.insert(value_type(BinaryFunctor<T>::process,Op)); \
  25846. register_binary_op(details:: e_add,details:: add_op)
  25847. register_binary_op(details:: e_sub,details:: sub_op)
  25848. register_binary_op(details:: e_mul,details:: mul_op)
  25849. register_binary_op(details:: e_div,details:: div_op)
  25850. register_binary_op(details:: e_mod,details:: mod_op)
  25851. register_binary_op(details:: e_pow,details:: pow_op)
  25852. register_binary_op(details:: e_lt,details:: lt_op)
  25853. register_binary_op(details:: e_lte,details:: lte_op)
  25854. register_binary_op(details:: e_gt,details:: gt_op)
  25855. register_binary_op(details:: e_gte,details:: gte_op)
  25856. register_binary_op(details:: e_eq,details:: eq_op)
  25857. register_binary_op(details:: e_ne,details:: ne_op)
  25858. register_binary_op(details:: e_and,details:: and_op)
  25859. register_binary_op(details::e_nand,details::nand_op)
  25860. register_binary_op(details:: e_or,details:: or_op)
  25861. register_binary_op(details:: e_nor,details:: nor_op)
  25862. register_binary_op(details:: e_xor,details:: xor_op)
  25863. register_binary_op(details::e_xnor,details::xnor_op)
  25864. #undef register_binary_op
  25865. }
  25866. inline void load_sf3_map(sf3_map_t& sf3_map)
  25867. {
  25868. typedef std::pair<trinary_functor_t,details::operator_type> pair_t;
  25869. #define register_sf3(Op) \
  25870. sf3_map[details::sf##Op##_op<T>::id()] = pair_t(details::sf##Op##_op<T>::process,details::e_sf##Op); \
  25871. register_sf3(00) register_sf3(01) register_sf3(02) register_sf3(03)
  25872. register_sf3(04) register_sf3(05) register_sf3(06) register_sf3(07)
  25873. register_sf3(08) register_sf3(09) register_sf3(10) register_sf3(11)
  25874. register_sf3(12) register_sf3(13) register_sf3(14) register_sf3(15)
  25875. register_sf3(16) register_sf3(17) register_sf3(18) register_sf3(19)
  25876. register_sf3(20) register_sf3(21) register_sf3(22) register_sf3(23)
  25877. register_sf3(24) register_sf3(25) register_sf3(26) register_sf3(27)
  25878. register_sf3(28) register_sf3(29) register_sf3(30)
  25879. #undef register_sf3
  25880. }
  25881. inline void load_sf4_map(sf4_map_t& sf4_map)
  25882. {
  25883. typedef std::pair<quaternary_functor_t,details::operator_type> pair_t;
  25884. #define register_sf4(Op) \
  25885. sf4_map[details::sf##Op##_op<T>::id()] = pair_t(details::sf##Op##_op<T>::process,details::e_sf##Op); \
  25886. register_sf4(48) register_sf4(49) register_sf4(50) register_sf4(51)
  25887. register_sf4(52) register_sf4(53) register_sf4(54) register_sf4(55)
  25888. register_sf4(56) register_sf4(57) register_sf4(58) register_sf4(59)
  25889. register_sf4(60) register_sf4(61) register_sf4(62) register_sf4(63)
  25890. register_sf4(64) register_sf4(65) register_sf4(66) register_sf4(67)
  25891. register_sf4(68) register_sf4(69) register_sf4(70) register_sf4(71)
  25892. register_sf4(72) register_sf4(73) register_sf4(74) register_sf4(75)
  25893. register_sf4(76) register_sf4(77) register_sf4(78) register_sf4(79)
  25894. register_sf4(80) register_sf4(81) register_sf4(82) register_sf4(83)
  25895. #undef register_sf4
  25896. #define register_sf4ext(Op) \
  25897. sf4_map[details::sfext##Op##_op<T>::id()] = pair_t(details::sfext##Op##_op<T>::process,details::e_sf4ext##Op); \
  25898. register_sf4ext(00) register_sf4ext(01) register_sf4ext(02) register_sf4ext(03)
  25899. register_sf4ext(04) register_sf4ext(05) register_sf4ext(06) register_sf4ext(07)
  25900. register_sf4ext(08) register_sf4ext(09) register_sf4ext(10) register_sf4ext(11)
  25901. register_sf4ext(12) register_sf4ext(13) register_sf4ext(14) register_sf4ext(15)
  25902. register_sf4ext(16) register_sf4ext(17) register_sf4ext(18) register_sf4ext(19)
  25903. register_sf4ext(20) register_sf4ext(21) register_sf4ext(22) register_sf4ext(23)
  25904. register_sf4ext(24) register_sf4ext(25) register_sf4ext(26) register_sf4ext(27)
  25905. register_sf4ext(28) register_sf4ext(29) register_sf4ext(30) register_sf4ext(31)
  25906. register_sf4ext(32) register_sf4ext(33) register_sf4ext(34) register_sf4ext(35)
  25907. register_sf4ext(36) register_sf4ext(36) register_sf4ext(38) register_sf4ext(39)
  25908. register_sf4ext(40) register_sf4ext(41) register_sf4ext(42) register_sf4ext(43)
  25909. register_sf4ext(44) register_sf4ext(45) register_sf4ext(46) register_sf4ext(47)
  25910. register_sf4ext(48) register_sf4ext(49) register_sf4ext(50) register_sf4ext(51)
  25911. register_sf4ext(52) register_sf4ext(53) register_sf4ext(54) register_sf4ext(55)
  25912. register_sf4ext(56) register_sf4ext(57) register_sf4ext(58) register_sf4ext(59)
  25913. #undef register_sf4ext
  25914. }
  25915. inline results_context_t& results_ctx()
  25916. {
  25917. if (0 == results_context_)
  25918. {
  25919. results_context_ = new results_context_t();
  25920. }
  25921. return (*results_context_);
  25922. }
  25923. inline void return_cleanup()
  25924. {
  25925. if (results_context_)
  25926. {
  25927. delete results_context_;
  25928. results_context_ = 0;
  25929. }
  25930. state_.return_stmt_present = false;
  25931. }
  25932. private:
  25933. parser(const parser<T>&);
  25934. parser<T>& operator=(const parser<T>&);
  25935. settings_store settings_;
  25936. expression_generator<T> expression_generator_;
  25937. details::node_allocator node_allocator_;
  25938. symtab_store symtab_store_;
  25939. dependent_entity_collector dec_;
  25940. std::deque<parser_error::type> error_list_;
  25941. std::deque<bool> brkcnt_list_;
  25942. parser_state state_;
  25943. bool resolve_unknown_symbol_;
  25944. results_context_t* results_context_;
  25945. unknown_symbol_resolver* unknown_symbol_resolver_;
  25946. unknown_symbol_resolver default_usr_;
  25947. base_ops_map_t base_ops_map_;
  25948. unary_op_map_t unary_op_map_;
  25949. binary_op_map_t binary_op_map_;
  25950. inv_binary_op_map_t inv_binary_op_map_;
  25951. sf3_map_t sf3_map_;
  25952. sf4_map_t sf4_map_;
  25953. std::string synthesis_error_;
  25954. scope_element_manager sem_;
  25955. lexer::helper::helper_assembly helper_assembly_;
  25956. lexer::helper::commutative_inserter commutative_inserter_;
  25957. lexer::helper::operator_joiner operator_joiner_2_;
  25958. lexer::helper::operator_joiner operator_joiner_3_;
  25959. lexer::helper::symbol_replacer symbol_replacer_;
  25960. lexer::helper::bracket_checker bracket_checker_;
  25961. lexer::helper::numeric_checker numeric_checker_;
  25962. lexer::helper::sequence_validator sequence_validator_;
  25963. };
  25964. template <typename T>
  25965. inline T integrate(expression<T>& e,
  25966. T& x,
  25967. const T& r0, const T& r1,
  25968. const std::size_t number_of_intervals = 1000000)
  25969. {
  25970. if (r0 > r1)
  25971. return T(0);
  25972. T h = (r1 - r0) / (T(2) * number_of_intervals);
  25973. T total_area = T(0);
  25974. for (std::size_t i = 0; i < number_of_intervals; ++i)
  25975. {
  25976. x = r0 + T(2) * i * h;
  25977. T y0 = e.value(); x += h;
  25978. T y1 = e.value(); x += h;
  25979. T y2 = e.value(); x += h;
  25980. total_area += h * (y0 + T(4) * y1 + y2) / T(3);
  25981. }
  25982. return total_area;
  25983. }
  25984. template <typename T>
  25985. inline T integrate(expression<T>& e,
  25986. const std::string& variable_name,
  25987. const T& r0, const T& r1,
  25988. const std::size_t number_of_intervals = 1000000)
  25989. {
  25990. symbol_table<T>& sym_table = e.get_symbol_table();
  25991. if (!sym_table.valid())
  25992. return std::numeric_limits<T>::quiet_NaN();
  25993. details::variable_node<T>* var = sym_table.get_variable(variable_name);
  25994. if (var)
  25995. {
  25996. T& x = var->ref();
  25997. T x_original = x;
  25998. T result = integrate(e,x,r0,r1,number_of_intervals);
  25999. x = x_original;
  26000. return result;
  26001. }
  26002. else
  26003. return std::numeric_limits<T>::quiet_NaN();
  26004. }
  26005. template <typename T>
  26006. inline T derivative(expression<T>& e,
  26007. T& x,
  26008. const T& h = T(0.00000001))
  26009. {
  26010. T x_init = x;
  26011. x = x_init + T(2) * h;
  26012. T y0 = e.value();
  26013. x = x_init + h;
  26014. T y1 = e.value();
  26015. x = x_init - h;
  26016. T y2 = e.value();
  26017. x = x_init - T(2) * h;
  26018. T y3 = e.value();
  26019. x = x_init;
  26020. return (-y0 + T(8) * (y1 - y2) + y3) / (T(12) * h);
  26021. }
  26022. template <typename T>
  26023. inline T second_derivative(expression<T>& e,
  26024. T& x,
  26025. const T& h = T(0.00001))
  26026. {
  26027. T y = e.value();
  26028. T x_init = x;
  26029. x = x_init + T(2) * h;
  26030. T y0 = e.value();
  26031. x = x_init + h;
  26032. T y1 = e.value();
  26033. x = x_init - h;
  26034. T y2 = e.value();
  26035. x = x_init - T(2) * h;
  26036. T y3 = e.value();
  26037. x = x_init;
  26038. return (-y0 + T(16) * (y1 + y2) - T(30) * y - y3) / (T(12) * h * h);
  26039. }
  26040. template <typename T>
  26041. inline T third_derivative(expression<T>& e,
  26042. T& x,
  26043. const T& h = T(0.0001))
  26044. {
  26045. T x_init = x;
  26046. x = x_init + T(2) * h;
  26047. T y0 = e.value();
  26048. x = x_init + h;
  26049. T y1 = e.value();
  26050. x = x_init - h;
  26051. T y2 = e.value();
  26052. x = x_init - T(2) * h;
  26053. T y3 = e.value();
  26054. x = x_init;
  26055. return (y0 + T(2) * (y2 - y1) - y3) / (T(2) * h * h * h);
  26056. }
  26057. template <typename T>
  26058. inline T derivative(expression<T>& e,
  26059. const std::string& variable_name,
  26060. const T& h = T(0.00000001))
  26061. {
  26062. symbol_table<T>& sym_table = e.get_symbol_table();
  26063. if (!sym_table.valid())
  26064. {
  26065. return std::numeric_limits<T>::quiet_NaN();
  26066. }
  26067. details::variable_node<T>* var = sym_table.get_variable(variable_name);
  26068. if (var)
  26069. {
  26070. T& x = var->ref();
  26071. T x_original = x;
  26072. T result = derivative(e,x,h);
  26073. x = x_original;
  26074. return result;
  26075. }
  26076. else
  26077. return std::numeric_limits<T>::quiet_NaN();
  26078. }
  26079. template <typename T>
  26080. inline T second_derivative(expression<T>& e,
  26081. const std::string& variable_name,
  26082. const T& h = T(0.00001))
  26083. {
  26084. symbol_table<T>& sym_table = e.get_symbol_table();
  26085. if (!sym_table.valid())
  26086. {
  26087. return std::numeric_limits<T>::quiet_NaN();
  26088. }
  26089. details::variable_node<T>* var = sym_table.get_variable(variable_name);
  26090. if (var)
  26091. {
  26092. T& x = var->ref();
  26093. T x_original = x;
  26094. T result = second_derivative(e,x,h);
  26095. x = x_original;
  26096. return result;
  26097. }
  26098. else
  26099. return std::numeric_limits<T>::quiet_NaN();
  26100. }
  26101. template <typename T>
  26102. inline T third_derivative(expression<T>& e,
  26103. const std::string& variable_name,
  26104. const T& h = T(0.0001))
  26105. {
  26106. symbol_table<T>& sym_table = e.get_symbol_table();
  26107. if (!sym_table.valid())
  26108. {
  26109. return std::numeric_limits<T>::quiet_NaN();
  26110. }
  26111. details::variable_node<T>* var = sym_table.get_variable(variable_name);
  26112. if (var)
  26113. {
  26114. T& x = var->ref();
  26115. T x_original = x;
  26116. T result = third_derivative(e,x,h);
  26117. x = x_original;
  26118. return result;
  26119. }
  26120. else
  26121. return std::numeric_limits<T>::quiet_NaN();
  26122. }
  26123. /*
  26124. Note: The following 'compute' routines are simple helpers,
  26125. for quickly setting up the required pieces of code in order
  26126. to evaluate an expression. By virtue of how they operate
  26127. there will be an overhead with regards to their setup and
  26128. teardown and hence should not be used in time critical
  26129. sections of code.
  26130. Furthermore they only assume a small sub set of variables - no
  26131. string variables or user defined functions.
  26132. */
  26133. template <typename T>
  26134. inline bool compute(const std::string& expression_string, T& result)
  26135. {
  26136. // No variables
  26137. symbol_table<T> symbol_table;
  26138. symbol_table.add_constants();
  26139. expression<T> expression;
  26140. parser<T> parser;
  26141. if (parser.compile(expression_string,expression))
  26142. {
  26143. result = expression.value();
  26144. return true;
  26145. }
  26146. else
  26147. return false;
  26148. }
  26149. template <typename T>
  26150. inline bool compute(const std::string& expression_string,
  26151. const T& x,
  26152. T& result)
  26153. {
  26154. // Only 'x'
  26155. static const std::string x_var("x");
  26156. symbol_table<T> symbol_table;
  26157. symbol_table.add_constants();
  26158. symbol_table.add_variable(x_var,x);
  26159. expression<T> expression;
  26160. parser<T> parser;
  26161. if (parser.compile(expression_string,expression))
  26162. {
  26163. result = expression.value();
  26164. return true;
  26165. }
  26166. else
  26167. return false;
  26168. }
  26169. template <typename T>
  26170. inline bool compute(const std::string& expression_string,
  26171. const T&x, const T& y,
  26172. T& result)
  26173. {
  26174. // Only 'x' and 'y'
  26175. static const std::string x_var("x");
  26176. static const std::string y_var("y");
  26177. symbol_table<T> symbol_table;
  26178. symbol_table.add_constants();
  26179. symbol_table.add_variable(x_var,x);
  26180. symbol_table.add_variable(y_var,y);
  26181. expression<T> expression;
  26182. parser<T> parser;
  26183. if (parser.compile(expression_string,expression))
  26184. {
  26185. result = expression.value();
  26186. return true;
  26187. }
  26188. else
  26189. return false;
  26190. }
  26191. template <typename T>
  26192. inline bool compute(const std::string& expression_string,
  26193. const T& x, const T& y, const T& z,
  26194. T& result)
  26195. {
  26196. // Only 'x', 'y' or 'z'
  26197. static const std::string x_var("x");
  26198. static const std::string y_var("y");
  26199. static const std::string z_var("z");
  26200. symbol_table<T> symbol_table;
  26201. symbol_table.add_constants();
  26202. symbol_table.add_variable(x_var,x);
  26203. symbol_table.add_variable(y_var,y);
  26204. symbol_table.add_variable(z_var,z);
  26205. expression<T> expression;
  26206. parser<T> parser;
  26207. if (parser.compile(expression_string,expression))
  26208. {
  26209. result = expression.value();
  26210. return true;
  26211. }
  26212. else
  26213. return false;
  26214. }
  26215. template <typename T, std::size_t N>
  26216. class polynomial : public ifunction<T>
  26217. {
  26218. private:
  26219. template <typename Type, std::size_t NumberOfCoefficients>
  26220. struct poly_impl { };
  26221. template <typename Type>
  26222. struct poly_impl <Type,12>
  26223. {
  26224. static inline T evaluate(const Type x,
  26225. const Type c12, const Type c11, const Type c10, const Type c9, const Type c8,
  26226. const Type c7, const Type c6, const Type c5, const Type c4, const Type c3,
  26227. const Type c2, const Type c1, const Type c0)
  26228. {
  26229. // p(x) = c_12x^12 + c_11x^11 + c_10x^10 + c_9x^9 + c_8x^8 + c_7x^7 + c_6x^6 + c_5x^5 + c_4x^4 + c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26230. return ((((((((((((c12 * x + c11) * x + c10) * x + c9) * x + c8) * x + c7) * x + c6) * x + c5) * x + c4) * x + c3) * x + c2) * x + c1) * x + c0);
  26231. }
  26232. };
  26233. template <typename Type>
  26234. struct poly_impl <Type,11>
  26235. {
  26236. static inline T evaluate(const Type x,
  26237. const Type c11, const Type c10, const Type c9, const Type c8, const Type c7,
  26238. const Type c6, const Type c5, const Type c4, const Type c3, const Type c2,
  26239. const Type c1, const Type c0)
  26240. {
  26241. // p(x) = c_11x^11 + c_10x^10 + c_9x^9 + c_8x^8 + c_7x^7 + c_6x^6 + c_5x^5 + c_4x^4 + c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26242. return (((((((((((c11 * x + c10) * x + c9) * x + c8) * x + c7) * x + c6) * x + c5) * x + c4) * x + c3) * x + c2) * x + c1) * x + c0);
  26243. }
  26244. };
  26245. template <typename Type>
  26246. struct poly_impl <Type,10>
  26247. {
  26248. static inline T evaluate(const Type x,
  26249. const Type c10, const Type c9, const Type c8, const Type c7, const Type c6,
  26250. const Type c5, const Type c4, const Type c3, const Type c2, const Type c1,
  26251. const Type c0)
  26252. {
  26253. // p(x) = c_10x^10 + c_9x^9 + c_8x^8 + c_7x^7 + c_6x^6 + c_5x^5 + c_4x^4 + c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26254. return ((((((((((c10 * x + c9) * x + c8) * x + c7) * x + c6) * x + c5) * x + c4) * x + c3) * x + c2) * x + c1) * x + c0);
  26255. }
  26256. };
  26257. template <typename Type>
  26258. struct poly_impl <Type,9>
  26259. {
  26260. static inline T evaluate(const Type x,
  26261. const Type c9, const Type c8, const Type c7, const Type c6, const Type c5,
  26262. const Type c4, const Type c3, const Type c2, const Type c1, const Type c0)
  26263. {
  26264. // p(x) = c_9x^9 + c_8x^8 + c_7x^7 + c_6x^6 + c_5x^5 + c_4x^4 + c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26265. return (((((((((c9 * x + c8) * x + c7) * x + c6) * x + c5) * x + c4) * x + c3) * x + c2) * x + c1) * x + c0);
  26266. }
  26267. };
  26268. template <typename Type>
  26269. struct poly_impl <Type,8>
  26270. {
  26271. static inline T evaluate(const Type x,
  26272. const Type c8, const Type c7, const Type c6, const Type c5, const Type c4,
  26273. const Type c3, const Type c2, const Type c1, const Type c0)
  26274. {
  26275. // p(x) = c_8x^8 + c_7x^7 + c_6x^6 + c_5x^5 + c_4x^4 + c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26276. return ((((((((c8 * x + c7) * x + c6) * x + c5) * x + c4) * x + c3) * x + c2) * x + c1) * x + c0);
  26277. }
  26278. };
  26279. template <typename Type>
  26280. struct poly_impl <Type,7>
  26281. {
  26282. static inline T evaluate(const Type x,
  26283. const Type c7, const Type c6, const Type c5, const Type c4, const Type c3,
  26284. const Type c2, const Type c1, const Type c0)
  26285. {
  26286. // p(x) = c_7x^7 + c_6x^6 + c_5x^5 + c_4x^4 + c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26287. return (((((((c7 * x + c6) * x + c5) * x + c4) * x + c3) * x + c2) * x + c1) * x + c0);
  26288. }
  26289. };
  26290. template <typename Type>
  26291. struct poly_impl <Type,6>
  26292. {
  26293. static inline T evaluate(const Type x,
  26294. const Type c6, const Type c5, const Type c4, const Type c3, const Type c2,
  26295. const Type c1, const Type c0)
  26296. {
  26297. // p(x) = c_6x^6 + c_5x^5 + c_4x^4 + c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26298. return ((((((c6 * x + c5) * x + c4) * x + c3) * x + c2) * x + c1) * x + c0);
  26299. }
  26300. };
  26301. template <typename Type>
  26302. struct poly_impl <Type,5>
  26303. {
  26304. static inline T evaluate(const Type x,
  26305. const Type c5, const Type c4, const Type c3, const Type c2,
  26306. const Type c1, const Type c0)
  26307. {
  26308. // p(x) = c_5x^5 + c_4x^4 + c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26309. return (((((c5 * x + c4) * x + c3) * x + c2) * x + c1) * x + c0);
  26310. }
  26311. };
  26312. template <typename Type>
  26313. struct poly_impl <Type,4>
  26314. {
  26315. static inline T evaluate(const Type x, const Type c4, const Type c3, const Type c2, const Type c1, const Type c0)
  26316. {
  26317. // p(x) = c_4x^4 + c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26318. return ((((c4 * x + c3) * x + c2) * x + c1) * x + c0);
  26319. }
  26320. };
  26321. template <typename Type>
  26322. struct poly_impl <Type,3>
  26323. {
  26324. static inline T evaluate(const Type x, const Type c3, const Type c2, const Type c1, const Type c0)
  26325. {
  26326. // p(x) = c_3x^3 + c_2x^2 + c_1x^1 + c_0x^0
  26327. return (((c3 * x + c2) * x + c1) * x + c0);
  26328. }
  26329. };
  26330. template <typename Type>
  26331. struct poly_impl <Type,2>
  26332. {
  26333. static inline T evaluate(const Type x, const Type c2, const Type c1, const Type c0)
  26334. {
  26335. // p(x) = c_2x^2 + c_1x^1 + c_0x^0
  26336. return ((c2 * x + c1) * x + c0);
  26337. }
  26338. };
  26339. template <typename Type>
  26340. struct poly_impl <Type,1>
  26341. {
  26342. static inline T evaluate(const Type x, const Type c1, const Type c0)
  26343. {
  26344. // p(x) = c_1x^1 + c_0x^0
  26345. return (c1 * x + c0);
  26346. }
  26347. };
  26348. public:
  26349. polynomial()
  26350. : ifunction<T>((N+2 <= 20) ? (N + 2) : std::numeric_limits<std::size_t>::max())
  26351. {
  26352. disable_has_side_effects(*this);
  26353. }
  26354. inline virtual T operator()(const T& x, const T& c1, const T& c0)
  26355. {
  26356. return ((1 == N) ? poly_impl<T,1>::evaluate(x,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26357. }
  26358. inline virtual T operator()(const T& x, const T& c2, const T& c1, const T& c0)
  26359. {
  26360. return ((2 == N) ? poly_impl<T,2>::evaluate(x,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26361. }
  26362. inline virtual T operator()(const T& x, const T& c3, const T& c2, const T& c1, const T& c0)
  26363. {
  26364. return ((3 == N) ? poly_impl<T,3>::evaluate(x,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26365. }
  26366. inline virtual T operator()(const T& x, const T& c4, const T& c3, const T& c2, const T& c1, const T& c0)
  26367. {
  26368. return ((4 == N) ? poly_impl<T,4>::evaluate(x,c4,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26369. }
  26370. inline virtual T operator()(const T& x, const T& c5, const T& c4, const T& c3, const T& c2, const T& c1, const T& c0)
  26371. {
  26372. return ((5 == N) ? poly_impl<T,5>::evaluate(x,c5,c4,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26373. }
  26374. inline virtual T operator()(const T& x, const T& c6, const T& c5, const T& c4, const T& c3, const T& c2, const T& c1, const T& c0)
  26375. {
  26376. return ((6 == N) ? poly_impl<T,6>::evaluate(x,c6,c5,c4,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26377. }
  26378. inline virtual T operator()(const T& x, const T& c7, const T& c6, const T& c5, const T& c4, const T& c3, const T& c2, const T& c1, const T& c0)
  26379. {
  26380. return ((7 == N) ? poly_impl<T,7>::evaluate(x,c7,c6,c5,c4,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26381. }
  26382. inline virtual T operator()(const T& x, const T& c8, const T& c7, const T& c6, const T& c5, const T& c4, const T& c3, const T& c2, const T& c1, const T& c0)
  26383. {
  26384. return ((8 == N) ? poly_impl<T,8>::evaluate(x,c8,c7,c6,c5,c4,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26385. }
  26386. inline virtual T operator()(const T& x, const T& c9, const T& c8, const T& c7, const T& c6, const T& c5, const T& c4, const T& c3, const T& c2, const T& c1, const T& c0)
  26387. {
  26388. return ((9 == N) ? poly_impl<T,9>::evaluate(x,c9,c8,c7,c6,c5,c4,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26389. }
  26390. inline virtual T operator()(const T& x, const T& c10, const T& c9, const T& c8, const T& c7, const T& c6, const T& c5, const T& c4, const T& c3, const T& c2, const T& c1, const T& c0)
  26391. {
  26392. return ((10 == N) ? poly_impl<T,10>::evaluate(x,c10,c9,c8,c7,c6,c5,c4,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26393. }
  26394. inline virtual T operator()(const T& x, const T& c11, const T& c10, const T& c9, const T& c8, const T& c7, const T& c6, const T& c5, const T& c4, const T& c3, const T& c2, const T& c1, const T& c0)
  26395. {
  26396. return ((11 == N) ? poly_impl<T,11>::evaluate(x,c11,c10,c9,c8,c7,c6,c5,c4,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26397. }
  26398. inline virtual T operator()(const T& x, const T& c12, const T& c11, const T& c10, const T& c9, const T& c8, const T& c7, const T& c6, const T& c5, const T& c4, const T& c3, const T& c2, const T& c1, const T& c0)
  26399. {
  26400. return ((12 == N) ? poly_impl<T,12>::evaluate(x,c12,c11,c10,c9,c8,c7,c6,c5,c4,c3,c2,c1,c0) : std::numeric_limits<T>::quiet_NaN());
  26401. }
  26402. inline virtual T operator()()
  26403. {
  26404. return std::numeric_limits<T>::quiet_NaN();
  26405. }
  26406. inline virtual T operator()(const T&)
  26407. {
  26408. return std::numeric_limits<T>::quiet_NaN();
  26409. }
  26410. inline virtual T operator()(const T&, const T&)
  26411. {
  26412. return std::numeric_limits<T>::quiet_NaN();
  26413. }
  26414. };
  26415. template <typename T>
  26416. class function_compositor
  26417. {
  26418. public:
  26419. typedef exprtk::expression<T> expression_t;
  26420. typedef exprtk::symbol_table<T> symbol_table_t;
  26421. typedef exprtk::parser<T> parser_t;
  26422. typedef typename parser_t::settings_store settings_t;
  26423. struct function
  26424. {
  26425. function()
  26426. {}
  26427. function(const std::string& n)
  26428. : name_(n)
  26429. {}
  26430. function(const std::string& name,
  26431. const std::string& expression)
  26432. : name_(name),
  26433. expression_(expression)
  26434. {}
  26435. function(const std::string& name,
  26436. const std::string& expression,
  26437. const std::string& v0)
  26438. : name_(name),
  26439. expression_(expression)
  26440. {
  26441. v_.push_back(v0);
  26442. }
  26443. function(const std::string& name,
  26444. const std::string& expression,
  26445. const std::string& v0, const std::string& v1)
  26446. : name_(name),
  26447. expression_(expression)
  26448. {
  26449. v_.push_back(v0); v_.push_back(v1);
  26450. }
  26451. function(const std::string& name,
  26452. const std::string& expression,
  26453. const std::string& v0, const std::string& v1,
  26454. const std::string& v2)
  26455. : name_(name),
  26456. expression_(expression)
  26457. {
  26458. v_.push_back(v0); v_.push_back(v1);
  26459. v_.push_back(v2);
  26460. }
  26461. function(const std::string& name,
  26462. const std::string& expression,
  26463. const std::string& v0, const std::string& v1,
  26464. const std::string& v2, const std::string& v3)
  26465. : name_(name),
  26466. expression_(expression)
  26467. {
  26468. v_.push_back(v0); v_.push_back(v1);
  26469. v_.push_back(v2); v_.push_back(v3);
  26470. }
  26471. inline function& name(const std::string& n)
  26472. {
  26473. name_ = n;
  26474. return (*this);
  26475. }
  26476. inline function& expression(const std::string& e)
  26477. {
  26478. expression_ = e;
  26479. return (*this);
  26480. }
  26481. inline function& var(const std::string& v)
  26482. {
  26483. v_.push_back(v);
  26484. return (*this);
  26485. }
  26486. std::string name_;
  26487. std::string expression_;
  26488. std::deque<std::string> v_;
  26489. };
  26490. private:
  26491. struct base_func : public exprtk::ifunction<T>
  26492. {
  26493. typedef const T& type;
  26494. typedef exprtk::ifunction<T> function_t;
  26495. typedef std::vector<T*> varref_t;
  26496. typedef std::vector<T> var_t;
  26497. typedef std::pair<T*,std::size_t> lvarref_t;
  26498. typedef std::vector<lvarref_t> lvr_vec_t;
  26499. base_func(const std::size_t& pc = 0)
  26500. : exprtk::ifunction<T>(pc),
  26501. local_var_stack_size(0),
  26502. stack_depth(0)
  26503. {
  26504. v.resize(pc);
  26505. }
  26506. virtual ~base_func()
  26507. {}
  26508. inline void update(const T& v0)
  26509. {
  26510. (*v[0]) = v0;
  26511. }
  26512. inline void update(const T& v0, const T& v1)
  26513. {
  26514. (*v[0]) = v0; (*v[1]) = v1;
  26515. }
  26516. inline void update(const T& v0, const T& v1, const T& v2)
  26517. {
  26518. (*v[0]) = v0; (*v[1]) = v1;
  26519. (*v[2]) = v2;
  26520. }
  26521. inline void update(const T& v0, const T& v1, const T& v2, const T& v3)
  26522. {
  26523. (*v[0]) = v0; (*v[1]) = v1;
  26524. (*v[2]) = v2; (*v[3]) = v3;
  26525. }
  26526. inline void update(const T& v0, const T& v1, const T& v2, const T& v3, const T& v4)
  26527. {
  26528. (*v[0]) = v0; (*v[1]) = v1;
  26529. (*v[2]) = v2; (*v[3]) = v3;
  26530. (*v[4]) = v4;
  26531. }
  26532. inline void update(const T& v0, const T& v1, const T& v2, const T& v3, const T& v4, const T& v5)
  26533. {
  26534. (*v[0]) = v0; (*v[1]) = v1;
  26535. (*v[2]) = v2; (*v[3]) = v3;
  26536. (*v[4]) = v4; (*v[5]) = v5;
  26537. }
  26538. inline function_t& setup(expression_t& expr)
  26539. {
  26540. expression = expr;
  26541. typedef typename expression_t::expression_holder::local_data_list_t ldl_t;
  26542. ldl_t ldl = expr.local_data_list();
  26543. std::vector<std::size_t> index_list;
  26544. for (std::size_t i = 0; i < ldl.size(); ++i)
  26545. {
  26546. if (ldl[i].size)
  26547. {
  26548. index_list.push_back(i);
  26549. }
  26550. }
  26551. std::size_t input_param_count = 0;
  26552. for (std::size_t i = 0; i < index_list.size(); ++i)
  26553. {
  26554. const std::size_t index = index_list[i];
  26555. if (i < (index_list.size() - v.size()))
  26556. {
  26557. lv.push_back(
  26558. std::make_pair(
  26559. reinterpret_cast<T*>(ldl[index].pointer),
  26560. ldl[index].size));
  26561. local_var_stack_size += ldl[index].size;
  26562. }
  26563. else
  26564. v[input_param_count++] = reinterpret_cast<T*>(ldl[index].pointer);
  26565. }
  26566. clear_stack();
  26567. return (*this);
  26568. }
  26569. inline void pre()
  26570. {
  26571. if (stack_depth++)
  26572. {
  26573. if (!v.empty())
  26574. {
  26575. var_t var_stack(v.size(),T(0));
  26576. copy(v,var_stack);
  26577. param_stack.push_back(var_stack);
  26578. }
  26579. if (!lv.empty())
  26580. {
  26581. var_t local_var_stack(local_var_stack_size,T(0));
  26582. copy(lv,local_var_stack);
  26583. local_stack.push_back(local_var_stack);
  26584. }
  26585. }
  26586. }
  26587. inline void post()
  26588. {
  26589. if (--stack_depth)
  26590. {
  26591. if (!v.empty())
  26592. {
  26593. copy(param_stack.back(),v);
  26594. param_stack.pop_back();
  26595. }
  26596. if (!lv.empty())
  26597. {
  26598. copy(local_stack.back(),lv);
  26599. local_stack.pop_back();
  26600. }
  26601. }
  26602. }
  26603. void copy(const varref_t& src_v, var_t& dest_v)
  26604. {
  26605. for (std::size_t i = 0; i < src_v.size(); ++i)
  26606. {
  26607. dest_v[i] = (*src_v[i]);
  26608. }
  26609. }
  26610. void copy(const var_t& src_v, varref_t& dest_v)
  26611. {
  26612. for (std::size_t i = 0; i < src_v.size(); ++i)
  26613. {
  26614. (*dest_v[i]) = src_v[i];
  26615. }
  26616. }
  26617. void copy(const lvr_vec_t& src_v, var_t& dest_v)
  26618. {
  26619. typename var_t::iterator itr = dest_v.begin();
  26620. for (std::size_t i = 0; i < src_v.size(); ++i)
  26621. {
  26622. lvarref_t vr = src_v[i];
  26623. if (1 == vr.second)
  26624. *itr++ = (*vr.first);
  26625. else
  26626. {
  26627. std::copy(vr.first,vr.first + vr.second,itr);
  26628. itr += vr.second;
  26629. }
  26630. }
  26631. }
  26632. void copy(const var_t& src_v, lvr_vec_t& dest_v)
  26633. {
  26634. typename var_t::const_iterator itr = src_v.begin();
  26635. for (std::size_t i = 0; i < src_v.size(); ++i)
  26636. {
  26637. lvarref_t vr = dest_v[i];
  26638. if (1 == vr.second)
  26639. (*vr.first) = *itr++;
  26640. else
  26641. {
  26642. std::copy(itr,itr + vr.second,vr.first);
  26643. itr += vr.second;
  26644. }
  26645. }
  26646. }
  26647. inline void clear_stack()
  26648. {
  26649. for (std::size_t i = 0; i < v.size(); ++i)
  26650. {
  26651. (*v[i]) = 0;
  26652. }
  26653. }
  26654. inline virtual T value(expression_t& e)
  26655. {
  26656. return e.value();
  26657. }
  26658. expression_t expression;
  26659. varref_t v;
  26660. lvr_vec_t lv;
  26661. std::size_t local_var_stack_size;
  26662. std::size_t stack_depth;
  26663. std::deque<var_t> param_stack;
  26664. std::deque<var_t> local_stack;
  26665. };
  26666. typedef std::map<std::string,base_func*> funcparam_t;
  26667. struct func_0param : public base_func
  26668. {
  26669. func_0param() : base_func(0) {}
  26670. inline T operator()()
  26671. {
  26672. return this->value(base_func::expression);
  26673. }
  26674. };
  26675. typedef const T& type;
  26676. struct func_1param : public base_func
  26677. {
  26678. func_1param() : base_func(1) {}
  26679. inline T operator()(type v0)
  26680. {
  26681. base_func::pre();
  26682. base_func::update(v0);
  26683. T result = this->value(base_func::expression);
  26684. base_func::post();
  26685. return result;
  26686. }
  26687. };
  26688. struct func_2param : public base_func
  26689. {
  26690. func_2param() : base_func(2) {}
  26691. inline T operator()(type v0, type v1)
  26692. {
  26693. base_func::pre();
  26694. base_func::update(v0,v1);
  26695. T result = this->value(base_func::expression);
  26696. base_func::post();
  26697. return result;
  26698. }
  26699. };
  26700. struct func_3param : public base_func
  26701. {
  26702. func_3param() : base_func(3) {}
  26703. inline T operator()(type v0, type v1, type v2)
  26704. {
  26705. base_func::pre();
  26706. base_func::update(v0,v1,v2);
  26707. T result = this->value(base_func::expression);
  26708. base_func::post();
  26709. return result;
  26710. }
  26711. };
  26712. struct func_4param : public base_func
  26713. {
  26714. func_4param() : base_func(4) {}
  26715. inline T operator()(type v0, type v1, type v2, type v3)
  26716. {
  26717. base_func::pre();
  26718. base_func::update(v0,v1,v2,v3);
  26719. T result = this->value(base_func::expression);
  26720. base_func::post();
  26721. return result;
  26722. }
  26723. };
  26724. struct func_5param : public base_func
  26725. {
  26726. func_5param() : base_func(5) {}
  26727. inline T operator()(type v0, type v1, type v2, type v3, type v4)
  26728. {
  26729. base_func::pre();
  26730. base_func::update(v0,v1,v2,v3,v4);
  26731. T result = this->value(base_func::expression);
  26732. base_func::post();
  26733. return result;
  26734. }
  26735. };
  26736. struct func_6param : public base_func
  26737. {
  26738. func_6param() : base_func(6) {}
  26739. inline T operator()(type v0, type v1, type v2, type v3, type v4, type v5)
  26740. {
  26741. base_func::pre();
  26742. base_func::update(v0,v1,v2,v3,v4,v5);
  26743. T result = this->value(base_func::expression);
  26744. base_func::post();
  26745. return result;
  26746. }
  26747. };
  26748. static T return_value(expression_t& e)
  26749. {
  26750. typedef exprtk::results_context<T> results_context_t;
  26751. typedef typename results_context_t::type_store_t type_t;
  26752. typedef typename type_t::scalar_view scalar_t;
  26753. T result = e.value();
  26754. if (e.return_invoked())
  26755. {
  26756. // Due to the post compilation checks, it can be safely
  26757. // assumed that there will be at least one parameter
  26758. // and that the first parameter will always be scalar.
  26759. return scalar_t(e.results()[0])();
  26760. }
  26761. return result;
  26762. }
  26763. #define def_fp_retval(N) \
  26764. struct func_##N##param_retval : public func_##N##param \
  26765. { \
  26766. inline T value(expression_t& e) \
  26767. { \
  26768. return return_value(e); \
  26769. } \
  26770. }; \
  26771. def_fp_retval(0)
  26772. def_fp_retval(1)
  26773. def_fp_retval(2)
  26774. def_fp_retval(3)
  26775. def_fp_retval(4)
  26776. def_fp_retval(5)
  26777. def_fp_retval(6)
  26778. template <typename Allocator,
  26779. template <typename,typename> class Sequence>
  26780. inline bool add(const std::string& name,
  26781. const std::string& expression,
  26782. const Sequence<std::string,Allocator>& var_list,
  26783. const bool override = false)
  26784. {
  26785. const std::size_t n = var_list.size();
  26786. typename std::map<std::string,expression_t>::iterator itr = expr_map_.find(name);
  26787. if (expr_map_.end() != itr)
  26788. {
  26789. if (!override)
  26790. {
  26791. exprtk_debug(("Compositor error(add): function '%s' already defined\n",
  26792. name.c_str()));
  26793. return false;
  26794. }
  26795. remove(name, var_list.size());
  26796. }
  26797. if (compile_expression(name,expression,var_list))
  26798. {
  26799. fp_map_[n][name]->setup(expr_map_[name]);
  26800. return true;
  26801. }
  26802. else
  26803. {
  26804. exprtk_debug(("Compositor error(add): Failed to compile function '%s'\n",
  26805. name.c_str()));
  26806. return false;
  26807. }
  26808. }
  26809. public:
  26810. function_compositor()
  26811. : parser_(settings_t::compile_all_opts +
  26812. settings_t::e_disable_zero_return),
  26813. fp_map_(7)
  26814. {}
  26815. function_compositor(const symbol_table_t& st)
  26816. : symbol_table_(st),
  26817. parser_(settings_t::compile_all_opts +
  26818. settings_t::e_disable_zero_return),
  26819. fp_map_(7)
  26820. {}
  26821. ~function_compositor()
  26822. {
  26823. clear();
  26824. }
  26825. inline symbol_table_t& symbol_table()
  26826. {
  26827. return symbol_table_;
  26828. }
  26829. inline void add_auxiliary_symtab(symbol_table_t& symtab)
  26830. {
  26831. auxiliary_symtab_list_.push_back(&symtab);
  26832. }
  26833. void clear()
  26834. {
  26835. symbol_table_.clear();
  26836. expr_map_ .clear();
  26837. for (std::size_t i = 0; i < fp_map_.size(); ++i)
  26838. {
  26839. typename funcparam_t::iterator itr = fp_map_[i].begin();
  26840. typename funcparam_t::iterator end = fp_map_[i].end ();
  26841. while (itr != end)
  26842. {
  26843. delete itr->second;
  26844. ++itr;
  26845. }
  26846. fp_map_[i].clear();
  26847. }
  26848. }
  26849. inline bool add(const function& f, const bool override = false)
  26850. {
  26851. return add(f.name_,f.expression_,f.v_,override);
  26852. }
  26853. private:
  26854. template <typename Allocator,
  26855. template <typename,typename> class Sequence>
  26856. bool compile_expression(const std::string& name,
  26857. const std::string& expression,
  26858. const Sequence<std::string,Allocator>& input_var_list,
  26859. bool return_present = false)
  26860. {
  26861. expression_t compiled_expression;
  26862. symbol_table_t local_symbol_table;
  26863. local_symbol_table.load_from(symbol_table_);
  26864. local_symbol_table.add_constants();
  26865. if (!valid(name,input_var_list.size()))
  26866. return false;
  26867. if (!forward(name,
  26868. input_var_list.size(),
  26869. local_symbol_table,
  26870. return_present))
  26871. return false;
  26872. compiled_expression.register_symbol_table(local_symbol_table);
  26873. for (std::size_t i = 0; i < auxiliary_symtab_list_.size(); ++i)
  26874. {
  26875. compiled_expression.register_symbol_table((*auxiliary_symtab_list_[i]));
  26876. }
  26877. std::string mod_expression;
  26878. for (std::size_t i = 0; i < input_var_list.size(); ++i)
  26879. {
  26880. mod_expression += " var " + input_var_list[i] + "{};\n";
  26881. }
  26882. if (
  26883. ('{' == details::front(expression)) &&
  26884. ('}' == details::back (expression))
  26885. )
  26886. mod_expression += "~" + expression + ";";
  26887. else
  26888. mod_expression += "~{" + expression + "};";
  26889. if (!parser_.compile(mod_expression,compiled_expression))
  26890. {
  26891. exprtk_debug(("Compositor Error: %s\n",parser_.error().c_str()));
  26892. exprtk_debug(("Compositor modified expression: \n%s\n",mod_expression.c_str()));
  26893. remove(name,input_var_list.size());
  26894. return false;
  26895. }
  26896. if (!return_present && parser_.dec().return_present())
  26897. {
  26898. remove(name,input_var_list.size());
  26899. return compile_expression(name,expression,input_var_list,true);
  26900. }
  26901. // Make sure every return point has a scalar as its first parameter
  26902. if (parser_.dec().return_present())
  26903. {
  26904. typedef std::vector<std::string> str_list_t;
  26905. str_list_t ret_param_list = parser_.dec().return_param_type_list();
  26906. for (std::size_t i = 0; i < ret_param_list.size(); ++i)
  26907. {
  26908. const std::string& params = ret_param_list[i];
  26909. if (params.empty() || ('T' != params[0]))
  26910. {
  26911. exprtk_debug(("Compositor Error: Return statement in function '%s' is invalid\n",
  26912. name.c_str()));
  26913. remove(name,input_var_list.size());
  26914. return false;
  26915. }
  26916. }
  26917. }
  26918. expr_map_[name] = compiled_expression;
  26919. exprtk::ifunction<T>& ifunc = (*(fp_map_[input_var_list.size()])[name]);
  26920. if (symbol_table_.add_function(name,ifunc))
  26921. return true;
  26922. else
  26923. {
  26924. exprtk_debug(("Compositor Error: Failed to add function '%s' to symbol table\n",
  26925. name.c_str()));
  26926. return false;
  26927. }
  26928. }
  26929. inline bool symbol_used(const std::string& symbol) const
  26930. {
  26931. return (
  26932. symbol_table_.is_variable (symbol) ||
  26933. symbol_table_.is_stringvar (symbol) ||
  26934. symbol_table_.is_function (symbol) ||
  26935. symbol_table_.is_vector (symbol) ||
  26936. symbol_table_.is_vararg_function(symbol)
  26937. );
  26938. }
  26939. inline bool valid(const std::string& name,
  26940. const std::size_t& arg_count) const
  26941. {
  26942. if (arg_count > 6)
  26943. return false;
  26944. else if (symbol_used(name))
  26945. return false;
  26946. else if (fp_map_[arg_count].end() != fp_map_[arg_count].find(name))
  26947. return false;
  26948. else
  26949. return true;
  26950. }
  26951. inline bool forward(const std::string& name,
  26952. const std::size_t& arg_count,
  26953. symbol_table_t& sym_table,
  26954. const bool ret_present = false)
  26955. {
  26956. switch (arg_count)
  26957. {
  26958. #define case_stmt(N) \
  26959. case N : (fp_map_[arg_count])[name] = \
  26960. (!ret_present) ? static_cast<base_func*> \
  26961. (new func_##N##param) : \
  26962. static_cast<base_func*> \
  26963. (new func_##N##param_retval) ; \
  26964. break; \
  26965. case_stmt(0) case_stmt(1) case_stmt(2)
  26966. case_stmt(3) case_stmt(4) case_stmt(5)
  26967. case_stmt(6)
  26968. #undef case_stmt
  26969. }
  26970. exprtk::ifunction<T>& ifunc = (*(fp_map_[arg_count])[name]);
  26971. return sym_table.add_function(name,ifunc);
  26972. }
  26973. inline void remove(const std::string& name, const std::size_t& arg_count)
  26974. {
  26975. if (arg_count > 6)
  26976. return;
  26977. typename std::map<std::string,expression_t>::iterator em_itr = expr_map_.find(name);
  26978. if (expr_map_.end() != em_itr)
  26979. {
  26980. expr_map_.erase(em_itr);
  26981. }
  26982. typename funcparam_t::iterator fp_itr = fp_map_[arg_count].find(name);
  26983. if (fp_map_[arg_count].end() != fp_itr)
  26984. {
  26985. delete fp_itr->second;
  26986. fp_map_[arg_count].erase(fp_itr);
  26987. }
  26988. symbol_table_.remove_function(name);
  26989. }
  26990. private:
  26991. symbol_table_t symbol_table_;
  26992. parser_t parser_;
  26993. std::map<std::string,expression_t> expr_map_;
  26994. std::vector<funcparam_t> fp_map_;
  26995. std::vector<symbol_table_t*> auxiliary_symtab_list_;
  26996. };
  26997. template <typename T>
  26998. inline bool pgo_primer()
  26999. {
  27000. static const std::string expression_list[]
  27001. = {
  27002. "(y + x)",
  27003. "2 * (y + x)",
  27004. "(2 * y + 2 * x)",
  27005. "(y + x / y) * (x - y / x)",
  27006. "x / ((x + y) * (x - y)) / y",
  27007. "1 - ((x * y) + (y / x)) - 3",
  27008. "sin(2 * x) + cos(pi / y)",
  27009. "1 - sin(2 * x) + cos(pi / y)",
  27010. "sqrt(1 - sin(2 * x) + cos(pi / y) / 3)",
  27011. "(x^2 / sin(2 * pi / y)) -x / 2",
  27012. "x + (cos(y - sin(2 / x * pi)) - sin(x - cos(2 * y / pi))) - y",
  27013. "clamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)",
  27014. "iclamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)",
  27015. "max(3.33, min(sqrt(1 - sin(2 * x) + cos(pi / y) / 3), 1.11))",
  27016. "if(avg(x,y) <= x + y, x - y, x * y) + 2 * pi / x",
  27017. "1.1x^1 + 2.2y^2 - 3.3x^3 + 4.4y^4 - 5.5x^5 + 6.6y^6 - 7.7x^27 + 8.8y^55",
  27018. "(yy + xx)",
  27019. "2 * (yy + xx)",
  27020. "(2 * yy + 2 * xx)",
  27021. "(yy + xx / yy) * (xx - yy / xx)",
  27022. "xx / ((xx + yy) * (xx - yy)) / yy",
  27023. "1 - ((xx * yy) + (yy / xx)) - 3",
  27024. "sin(2 * xx) + cos(pi / yy)",
  27025. "1 - sin(2 * xx) + cos(pi / yy)",
  27026. "sqrt(1 - sin(2 * xx) + cos(pi / yy) / 3)",
  27027. "(xx^2 / sin(2 * pi / yy)) -xx / 2",
  27028. "xx + (cos(yy - sin(2 / xx * pi)) - sin(xx - cos(2 * yy / pi))) - yy",
  27029. "clamp(-1.0, sin(2 * pi * xx) + cos(yy / 2 * pi), +1.0)",
  27030. "max(3.33, min(sqrt(1 - sin(2 * xx) + cos(pi / yy) / 3), 1.11))",
  27031. "if(avg(xx,yy) <= xx + yy, xx - yy, xx * yy) + 2 * pi / xx",
  27032. "1.1xx^1 + 2.2yy^2 - 3.3xx^3 + 4.4yy^4 - 5.5xx^5 + 6.6yy^6 - 7.7xx^27 + 8.8yy^55",
  27033. "(1.1*(2.2*(3.3*(4.4*(5.5*(6.6*(7.7*(8.8*(9.9+x)))))))))",
  27034. "(((((((((x+9.9)*8.8)*7.7)*6.6)*5.5)*4.4)*3.3)*2.2)*1.1)",
  27035. "(x + y) * z", "x + (y * z)", "(x + y) * 7", "x + (y * 7)",
  27036. "(x + 7) * y", "x + (7 * y)", "(7 + x) * y", "7 + (x * y)",
  27037. "(2 + x) * 3", "2 + (x * 3)", "(2 + 3) * x", "2 + (3 * x)",
  27038. "(x + 2) * 3", "x + (2 * 3)",
  27039. "(x + y) * (z / w)", "(x + y) * (z / 7)", "(x + y) * (7 / z)", "(x + 7) * (y / z)",
  27040. "(7 + x) * (y / z)", "(2 + x) * (y / z)", "(x + 2) * (y / 3)", "(2 + x) * (y / 3)",
  27041. "(x + 2) * (3 / y)", "x + (y * (z / w))", "x + (y * (z / 7))", "x + (y * (7 / z))",
  27042. "x + (7 * (y / z))", "7 + (x * (y / z))", "2 + (x * (3 / y))", "x + (2 * (y / 4))",
  27043. "2 + (x * (y / 3))", "x + (2 * (3 / y))",
  27044. "x + ((y * z) / w)", "x + ((y * z) / 7)", "x + ((y * 7) / z)", "x + ((7 * y) / z)",
  27045. "7 + ((y * z) / w)", "2 + ((x * 3) / y)", "x + ((2 * y) / 3)", "2 + ((x * y) / 3)",
  27046. "x + ((2 * 3) / y)", "(((x + y) * z) / w)",
  27047. "(((x + y) * z) / 7)", "(((x + y) * 7) / z)", "(((x + 7) * y) / z)", "(((7 + x) * y) / z)",
  27048. "(((2 + x) * 3) / y)", "(((x + 2) * y) / 3)", "(((2 + x) * y) / 3)", "(((x + 2) * 3) / y)",
  27049. "((x + (y * z)) / w)", "((x + (y * z)) / 7)", "((x + (y * 7)) / y)", "((x + (7 * y)) / z)",
  27050. "((7 + (x * y)) / z)", "((2 + (x * 3)) / y)", "((x + (2 * y)) / 3)", "((2 + (x * y)) / 3)",
  27051. "((x + (2 * 3)) / y)",
  27052. "(xx + yy) * zz", "xx + (yy * zz)",
  27053. "(xx + yy) * 7", "xx + (yy * 7)",
  27054. "(xx + 7) * yy", "xx + (7 * yy)",
  27055. "(7 + xx) * yy", "7 + (xx * yy)",
  27056. "(2 + x) * 3", "2 + (x * 3)",
  27057. "(2 + 3) * x", "2 + (3 * x)",
  27058. "(x + 2) * 3", "x + (2 * 3)",
  27059. "(xx + yy) * (zz / ww)", "(xx + yy) * (zz / 7)",
  27060. "(xx + yy) * (7 / zz)", "(xx + 7) * (yy / zz)",
  27061. "(7 + xx) * (yy / zz)", "(2 + xx) * (yy / zz)",
  27062. "(xx + 2) * (yy / 3)", "(2 + xx) * (yy / 3)",
  27063. "(xx + 2) * (3 / yy)", "xx + (yy * (zz / ww))",
  27064. "xx + (yy * (zz / 7))", "xx + (yy * (7 / zz))",
  27065. "xx + (7 * (yy / zz))", "7 + (xx * (yy / zz))",
  27066. "2 + (xx * (3 / yy))", "xx + (2 * (yy / 4))",
  27067. "2 + (xx * (yy / 3))", "xx + (2 * (3 / yy))",
  27068. "xx + ((yy * zz) / ww)", "xx + ((yy * zz) / 7)",
  27069. "xx + ((yy * 7) / zz)", "xx + ((7 * yy) / zz)",
  27070. "7 + ((yy * zz) / ww)", "2 + ((xx * 3) / yy)",
  27071. "xx + ((2 * yy) / 3)", "2 + ((xx * yy) / 3)",
  27072. "xx + ((2 * 3) / yy)", "(((xx + yy) * zz) / ww)",
  27073. "(((xx + yy) * zz) / 7)", "(((xx + yy) * 7) / zz)",
  27074. "(((xx + 7) * yy) / zz)", "(((7 + xx) * yy) / zz)",
  27075. "(((2 + xx) * 3) / yy)", "(((xx + 2) * yy) / 3)",
  27076. "(((2 + xx) * yy) / 3)", "(((xx + 2) * 3) / yy)",
  27077. "((xx + (yy * zz)) / ww)", "((xx + (yy * zz)) / 7)",
  27078. "((xx + (yy * 7)) / yy)", "((xx + (7 * yy)) / zz)",
  27079. "((7 + (xx * yy)) / zz)", "((2 + (xx * 3)) / yy)",
  27080. "((xx + (2 * yy)) / 3)", "((2 + (xx * yy)) / 3)",
  27081. "((xx + (2 * 3)) / yy)"
  27082. };
  27083. static const std::size_t expression_list_size = sizeof(expression_list) / sizeof(std::string);
  27084. T x = T(0);
  27085. T y = T(0);
  27086. T z = T(0);
  27087. T w = T(0);
  27088. T xx = T(0);
  27089. T yy = T(0);
  27090. T zz = T(0);
  27091. T ww = T(0);
  27092. exprtk::symbol_table<T> symbol_table;
  27093. symbol_table.add_constants();
  27094. symbol_table.add_variable( "x", x);
  27095. symbol_table.add_variable( "y", y);
  27096. symbol_table.add_variable( "z", z);
  27097. symbol_table.add_variable( "w", w);
  27098. symbol_table.add_variable("xx",xx);
  27099. symbol_table.add_variable("yy",yy);
  27100. symbol_table.add_variable("zz",zz);
  27101. symbol_table.add_variable("ww",ww);
  27102. typedef typename std::deque<exprtk::expression<T> > expr_list_t;
  27103. expr_list_t expr_list;
  27104. const std::size_t rounds = 50;
  27105. {
  27106. for (std::size_t r = 0; r < rounds; ++r)
  27107. {
  27108. expr_list.clear();
  27109. exprtk::parser<T> parser;
  27110. for (std::size_t i = 0; i < expression_list_size; ++i)
  27111. {
  27112. exprtk::expression<T> expression;
  27113. expression.register_symbol_table(symbol_table);
  27114. if (!parser.compile(expression_list[i],expression))
  27115. {
  27116. return false;
  27117. }
  27118. expr_list.push_back(expression);
  27119. }
  27120. }
  27121. }
  27122. struct execute
  27123. {
  27124. static inline T process(T& x, T& y, expression<T>& expression)
  27125. {
  27126. static const T lower_bound = T(-20);
  27127. static const T upper_bound = T(+20);
  27128. T delta = T(0.1);
  27129. T total = T(0);
  27130. for (x = lower_bound; x <= upper_bound; x += delta)
  27131. {
  27132. for (y = lower_bound; y <= upper_bound; y += delta)
  27133. {
  27134. total += expression.value();
  27135. }
  27136. }
  27137. return total;
  27138. }
  27139. };
  27140. for (std::size_t i = 0; i < expr_list.size(); ++i)
  27141. {
  27142. execute::process( x, y,expr_list[i]);
  27143. execute::process(xx,yy,expr_list[i]);
  27144. }
  27145. {
  27146. for (std::size_t i = 0; i < 10000; ++i)
  27147. {
  27148. T v = T(123.456 + i);
  27149. if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T, 1>::result(v),details::numeric::pow(v,T( 1))))) return false;
  27150. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T, 2>::result(v),details::numeric::pow(v,T( 2))))) return false;
  27151. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T, 3>::result(v),details::numeric::pow(v,T( 3))))) return false;
  27152. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T, 4>::result(v),details::numeric::pow(v,T( 4))))) return false;
  27153. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T, 5>::result(v),details::numeric::pow(v,T( 5))))) return false;
  27154. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T, 6>::result(v),details::numeric::pow(v,T( 6))))) return false;
  27155. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T, 7>::result(v),details::numeric::pow(v,T( 7))))) return false;
  27156. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T, 8>::result(v),details::numeric::pow(v,T( 8))))) return false;
  27157. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T, 9>::result(v),details::numeric::pow(v,T( 9))))) return false;
  27158. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,10>::result(v),details::numeric::pow(v,T(10))))) return false;
  27159. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,11>::result(v),details::numeric::pow(v,T(11))))) return false;
  27160. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,12>::result(v),details::numeric::pow(v,T(12))))) return false;
  27161. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,13>::result(v),details::numeric::pow(v,T(13))))) return false;
  27162. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,14>::result(v),details::numeric::pow(v,T(14))))) return false;
  27163. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,15>::result(v),details::numeric::pow(v,T(15))))) return false;
  27164. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,16>::result(v),details::numeric::pow(v,T(16))))) return false;
  27165. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,17>::result(v),details::numeric::pow(v,T(17))))) return false;
  27166. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,18>::result(v),details::numeric::pow(v,T(18))))) return false;
  27167. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,19>::result(v),details::numeric::pow(v,T(19))))) return false;
  27168. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,20>::result(v),details::numeric::pow(v,T(20))))) return false;
  27169. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,21>::result(v),details::numeric::pow(v,T(21))))) return false;
  27170. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,22>::result(v),details::numeric::pow(v,T(22))))) return false;
  27171. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,23>::result(v),details::numeric::pow(v,T(23))))) return false;
  27172. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,24>::result(v),details::numeric::pow(v,T(24))))) return false;
  27173. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,25>::result(v),details::numeric::pow(v,T(25))))) return false;
  27174. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,26>::result(v),details::numeric::pow(v,T(26))))) return false;
  27175. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,27>::result(v),details::numeric::pow(v,T(27))))) return false;
  27176. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,28>::result(v),details::numeric::pow(v,T(28))))) return false;
  27177. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,29>::result(v),details::numeric::pow(v,T(29))))) return false;
  27178. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,30>::result(v),details::numeric::pow(v,T(30))))) return false;
  27179. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,31>::result(v),details::numeric::pow(v,T(31))))) return false;
  27180. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,32>::result(v),details::numeric::pow(v,T(32))))) return false;
  27181. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,33>::result(v),details::numeric::pow(v,T(33))))) return false;
  27182. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,34>::result(v),details::numeric::pow(v,T(34))))) return false;
  27183. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,35>::result(v),details::numeric::pow(v,T(35))))) return false;
  27184. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,36>::result(v),details::numeric::pow(v,T(36))))) return false;
  27185. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,37>::result(v),details::numeric::pow(v,T(37))))) return false;
  27186. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,38>::result(v),details::numeric::pow(v,T(38))))) return false;
  27187. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,39>::result(v),details::numeric::pow(v,T(39))))) return false;
  27188. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,40>::result(v),details::numeric::pow(v,T(40))))) return false;
  27189. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,41>::result(v),details::numeric::pow(v,T(41))))) return false;
  27190. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,42>::result(v),details::numeric::pow(v,T(42))))) return false;
  27191. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,43>::result(v),details::numeric::pow(v,T(43))))) return false;
  27192. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,44>::result(v),details::numeric::pow(v,T(44))))) return false;
  27193. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,45>::result(v),details::numeric::pow(v,T(45))))) return false;
  27194. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,46>::result(v),details::numeric::pow(v,T(46))))) return false;
  27195. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,47>::result(v),details::numeric::pow(v,T(47))))) return false;
  27196. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,48>::result(v),details::numeric::pow(v,T(48))))) return false;
  27197. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,49>::result(v),details::numeric::pow(v,T(49))))) return false;
  27198. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,50>::result(v),details::numeric::pow(v,T(50))))) return false;
  27199. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,51>::result(v),details::numeric::pow(v,T(51))))) return false;
  27200. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,52>::result(v),details::numeric::pow(v,T(52))))) return false;
  27201. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,53>::result(v),details::numeric::pow(v,T(53))))) return false;
  27202. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,54>::result(v),details::numeric::pow(v,T(54))))) return false;
  27203. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,55>::result(v),details::numeric::pow(v,T(55))))) return false;
  27204. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,56>::result(v),details::numeric::pow(v,T(56))))) return false;
  27205. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,57>::result(v),details::numeric::pow(v,T(57))))) return false;
  27206. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,58>::result(v),details::numeric::pow(v,T(58))))) return false;
  27207. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,59>::result(v),details::numeric::pow(v,T(59))))) return false;
  27208. else if (details::is_true(details::numeric::nequal(details::numeric::fast_exp<T,60>::result(v),details::numeric::pow(v,T(60))))) return false;
  27209. }
  27210. }
  27211. return true;
  27212. }
  27213. namespace helper
  27214. {
  27215. namespace details
  27216. {
  27217. template <typename T>
  27218. inline void print_type(const std::string& fmt,
  27219. const T v,
  27220. exprtk::details::numeric::details::real_type_tag)
  27221. {
  27222. printf(fmt.c_str(),v);
  27223. }
  27224. template <typename T>
  27225. struct print_impl
  27226. {
  27227. typedef typename igeneric_function<T>::generic_type generic_type;
  27228. typedef typename igeneric_function<T>::parameter_list_t parameter_list_t;
  27229. typedef typename generic_type::scalar_view scalar_t;
  27230. typedef typename generic_type::vector_view vector_t;
  27231. typedef typename generic_type::string_view string_t;
  27232. static void process(const std::string& scalar_format, parameter_list_t parameters)
  27233. {
  27234. for (std::size_t i = 0; i < parameters.size(); ++i)
  27235. {
  27236. generic_type& gt = parameters[i];
  27237. typename exprtk::details::numeric::details::number_type<T>::type num_type;
  27238. switch (gt.type)
  27239. {
  27240. case generic_type::e_scalar : print_type(scalar_format,scalar_t(gt)(),num_type);
  27241. break;
  27242. case generic_type::e_vector : {
  27243. vector_t vector(gt);
  27244. for (std::size_t x = 0; x < vector.size(); ++x)
  27245. {
  27246. print_type(scalar_format,vector[x],num_type);
  27247. if ((x + 1) < vector.size())
  27248. printf(" ");
  27249. }
  27250. }
  27251. break;
  27252. case generic_type::e_string : printf("%s",to_str(string_t(gt)).c_str());
  27253. break;
  27254. default : continue;
  27255. }
  27256. }
  27257. }
  27258. };
  27259. }
  27260. template <typename T>
  27261. struct print : public exprtk::igeneric_function<T>
  27262. {
  27263. typedef typename igeneric_function<T>::parameter_list_t parameter_list_t;
  27264. print(const std::string& scalar_format = "%10.5f")
  27265. : scalar_format_(scalar_format)
  27266. {
  27267. exprtk::enable_zero_parameters(*this);
  27268. }
  27269. inline T operator()(parameter_list_t parameters)
  27270. {
  27271. details::print_impl<T>::process(scalar_format_,parameters);
  27272. return T(0);
  27273. }
  27274. std::string scalar_format_;
  27275. };
  27276. template <typename T>
  27277. struct println : public exprtk::igeneric_function<T>
  27278. {
  27279. typedef typename igeneric_function<T>::parameter_list_t parameter_list_t;
  27280. println(const std::string& scalar_format = "%10.5f")
  27281. : scalar_format_(scalar_format)
  27282. {
  27283. exprtk::enable_zero_parameters(*this);
  27284. }
  27285. inline T operator()(parameter_list_t parameters)
  27286. {
  27287. details::print_impl<T>::process(scalar_format_,parameters);
  27288. printf("\n");
  27289. return T(0);
  27290. }
  27291. std::string scalar_format_;
  27292. };
  27293. }
  27294. }
  27295. #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  27296. # ifndef NOMINMAX
  27297. # define NOMINMAX
  27298. # endif
  27299. # ifndef WIN32_LEAN_AND_MEAN
  27300. # define WIN32_LEAN_AND_MEAN
  27301. # endif
  27302. # include <windows.h>
  27303. # include <ctime>
  27304. #else
  27305. # include <ctime>
  27306. # include <sys/time.h>
  27307. # include <sys/types.h>
  27308. #endif
  27309. namespace exprtk
  27310. {
  27311. class timer
  27312. {
  27313. public:
  27314. #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  27315. timer()
  27316. : in_use_(false)
  27317. {
  27318. QueryPerformanceFrequency(&clock_frequency_);
  27319. }
  27320. inline void start()
  27321. {
  27322. in_use_ = true;
  27323. QueryPerformanceCounter(&start_time_);
  27324. }
  27325. inline void stop()
  27326. {
  27327. QueryPerformanceCounter(&stop_time_);
  27328. in_use_ = false;
  27329. }
  27330. inline double time() const
  27331. {
  27332. return (1.0 * (stop_time_.QuadPart - start_time_.QuadPart)) / (1.0 * clock_frequency_.QuadPart);
  27333. }
  27334. #else
  27335. timer()
  27336. : in_use_(false)
  27337. {
  27338. start_time_.tv_sec = 0;
  27339. start_time_.tv_usec = 0;
  27340. stop_time_.tv_sec = 0;
  27341. stop_time_.tv_usec = 0;
  27342. }
  27343. inline void start()
  27344. {
  27345. in_use_ = true;
  27346. gettimeofday(&start_time_,0);
  27347. }
  27348. inline void stop()
  27349. {
  27350. gettimeofday(&stop_time_, 0);
  27351. in_use_ = false;
  27352. }
  27353. inline unsigned long long int usec_time() const
  27354. {
  27355. if (!in_use_)
  27356. {
  27357. if (stop_time_.tv_sec >= start_time_.tv_sec)
  27358. {
  27359. return 1000000 * (stop_time_.tv_sec - start_time_.tv_sec ) +
  27360. (stop_time_.tv_usec - start_time_.tv_usec);
  27361. }
  27362. else
  27363. return std::numeric_limits<unsigned long long int>::max();
  27364. }
  27365. else
  27366. return std::numeric_limits<unsigned long long int>::max();
  27367. }
  27368. inline double time() const
  27369. {
  27370. return usec_time() * 0.000001;
  27371. }
  27372. #endif
  27373. inline bool in_use() const
  27374. {
  27375. return in_use_;
  27376. }
  27377. private:
  27378. bool in_use_;
  27379. #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  27380. LARGE_INTEGER start_time_;
  27381. LARGE_INTEGER stop_time_;
  27382. LARGE_INTEGER clock_frequency_;
  27383. #else
  27384. struct timeval start_time_;
  27385. struct timeval stop_time_;
  27386. #endif
  27387. };
  27388. namespace information
  27389. {
  27390. static const char* library = "Mathematical Expression Toolkit";
  27391. static const char* version = "2.71828182845904523536028747135"
  27392. "2662497757247093699959574966967";
  27393. static const char* date = "20150505";
  27394. static inline std::string data()
  27395. {
  27396. static const std::string info_str = std::string(library) +
  27397. std::string(" v") + std::string(version) +
  27398. std::string(" (") + date + std::string(")");
  27399. return info_str;
  27400. }
  27401. } // namespace information
  27402. } // namespace exprtk
  27403. #endif