math.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2004 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. #define EXP_VARIABLE 0
  26. #define EXP_STRING 1
  27. #define EXP_BRACE 2
  28. #define EXP_OPERATOR 3
  29. #define EXP_PR_DICE 0
  30. #define EXP_PR_INTMUL 1
  31. #define EXP_PR_INTADD 2
  32. #define EXP_PR_BITSHIFT 3
  33. #define EXP_PR_LOGLTGT 4
  34. #define EXP_PR_LOGCOMP 5
  35. #define EXP_PR_BITAND 6
  36. #define EXP_PR_BITXOR 7
  37. #define EXP_PR_BITOR 8
  38. #define EXP_PR_LOGAND 9
  39. #define EXP_PR_LOGXOR 10
  40. #define EXP_PR_LOGOR 11
  41. #define EXP_PR_VAR 12
  42. #define EXP_PR_LVL 13
  43. struct link_data *math_head;
  44. struct link_data *math_tail;
  45. struct link_data *mathnode_s;
  46. struct link_data *mathnode_e;
  47. int precision;
  48. DO_COMMAND(do_math)
  49. {
  50. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  51. struct listnode *node;
  52. long double result;
  53. arg = sub_arg_in_braces(ses, arg, arg1, GET_NST, SUB_VAR|SUB_FUN);
  54. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  55. if (*arg1 == 0 || *arg2 == 0)
  56. {
  57. show_error(ses, LIST_VARIABLE, "#SYNTAX: #MATH {variable} {expression}.");
  58. }
  59. else
  60. {
  61. result = get_number(ses, arg2);
  62. node = set_nest_node_ses(ses, arg1, "%.*Lf", precision, result);
  63. show_message(ses, LIST_VARIABLE, "#MATH: VARIABLE {%s} HAS BEEN SET TO {%s}.", arg1, node->arg2);
  64. }
  65. return ses;
  66. }
  67. int is_math(struct session *ses, char *str)
  68. {
  69. return mathexp_tokenize(ses, str, 0, 0);
  70. }
  71. long double get_number(struct session *ses, char *str)
  72. {
  73. long double val;
  74. char result[BUFFER_SIZE];
  75. mathexp(ses, str, result, 0);
  76. val = tintoi(result);
  77. return val;
  78. }
  79. unsigned long long get_integer(struct session *ses, char *str)
  80. {
  81. unsigned long long val;
  82. char result[BUFFER_SIZE];
  83. mathexp(ses, str, result, 0);
  84. val = tintou(result);
  85. return val;
  86. }
  87. int get_ellipsis(struct listroot *root, char *name, int *min, int *max)
  88. {
  89. unsigned long long range = get_integer(root->ses, name);
  90. push_call("get_ellipsis(%p,%p,%p,%p)",root,name,min,max);
  91. *min = (int) (HAS_BIT(range, 0x00000000FFFFFFFFLL));
  92. *max = (int) (HAS_BIT(range, 0xFFFFFFFF00000000ULL) >> 32ULL);
  93. *min = *min > 0 ? *min - 1 : root->used + *min;
  94. *max = *max > 0 ? *max - 1 : root->used + *max;
  95. *min = URANGE(0, *min, root->used - 1);
  96. *max = URANGE(0, *max, root->used - 1);
  97. pop_call();
  98. return 1 + (*min < *max ? *max - *min : *min - *max);
  99. }
  100. long double get_double(struct session *ses, char *str)
  101. {
  102. long double val;
  103. char result[BUFFER_SIZE];
  104. mathexp(ses, str, result, 1);
  105. val = tintoi(result);
  106. return val;
  107. }
  108. void get_number_string(struct session *ses, char *str, char *result)
  109. {
  110. long double val = get_number(ses, str);
  111. sprintf(result, "%.*Lf", precision, val);
  112. }
  113. long double mathswitch(struct session *ses, char *left, char *right)
  114. {
  115. char shift[BUFFER_SIZE];
  116. sprintf(shift, "%s == %s", left, right);
  117. return get_number(ses, shift);
  118. }
  119. /*
  120. Flexible tokenized mathematical expression interpreter
  121. If seed is set it forces floating point math
  122. */
  123. void mathexp(struct session *ses, char *str, char *result, int seed)
  124. {
  125. struct link_data *node;
  126. substitute(ses, str, result, SUB_VAR|SUB_FUN);
  127. if (mathexp_tokenize(ses, result, seed, TRUE) == FALSE)
  128. {
  129. return;
  130. }
  131. node = math_head;
  132. while (node->prev || node->next)
  133. {
  134. if (node->next == NULL || atoi(node->next->str1) < atoi(node->str1))
  135. {
  136. mathexp_level(ses, node);
  137. node = math_head;
  138. }
  139. else
  140. {
  141. node = node->next;
  142. }
  143. }
  144. strcpy(result, node->str3);
  145. }
  146. #define MATH_NODE(buffercheck, priority, newstatus) \
  147. { \
  148. if (buffercheck && pta == buf3) \
  149. { \
  150. return FALSE; \
  151. } \
  152. if (badnumber && debug) \
  153. { \
  154. badnumber = 0; \
  155. precision = 0; \
  156. show_debug(ses, LIST_VARIABLE, "#MATH EXP: INVALID NUMBER %s.", buf3); \
  157. } \
  158. *pta = 0; \
  159. sprintf(buf1, "%02d", level); \
  160. sprintf(buf2, "%02d", priority); \
  161. add_math_node(buf1, buf2, buf3); \
  162. status = newstatus; \
  163. pta = buf3; \
  164. point = -1; \
  165. metric = 0; \
  166. }
  167. void add_math_node(char *arg1, char *arg2, char *arg3)
  168. {
  169. struct link_data *link;
  170. link = (struct link_data *) calloc(1, sizeof(struct link_data));
  171. link->str1 = strdup(arg1);
  172. link->str2 = strdup(arg2);
  173. link->str3 = strdup(arg3);
  174. LINK(link, math_head, math_tail);
  175. }
  176. void del_math_node(struct link_data *node)
  177. {
  178. UNLINK(node, math_head, math_tail);
  179. free(node->str1);
  180. free(node->str2);
  181. free(node->str3);
  182. free(node);
  183. }
  184. int mathexp_tokenize(struct session *ses, char *str, int seed, int debug)
  185. {
  186. char buf1[BUFFER_SIZE], buf2[BUFFER_SIZE], buf3[STRING_SIZE], *pti, *pta;
  187. int level, status, point, badnumber, metric, nest;
  188. nest = 0;
  189. level = 0;
  190. metric = 0;
  191. point = -1;
  192. status = EXP_VARIABLE;
  193. precision = seed;
  194. badnumber = 0;
  195. pta = buf3;
  196. pti = str;
  197. while (math_head)
  198. {
  199. del_math_node(math_head);
  200. }
  201. while (*pti)
  202. {
  203. switch (status)
  204. {
  205. case EXP_VARIABLE:
  206. switch (*pti)
  207. {
  208. case '0':
  209. case '1':
  210. case '2':
  211. case '3':
  212. case '4':
  213. case '5':
  214. case '6':
  215. case '7':
  216. case '8':
  217. case '9':
  218. *pta++ = *pti++;
  219. if (metric)
  220. {
  221. badnumber = 1;
  222. if (debug == 0)
  223. {
  224. return FALSE;
  225. }
  226. }
  227. if (point >= 0)
  228. {
  229. point++;
  230. if (precision < point)
  231. {
  232. precision = point;
  233. }
  234. }
  235. break;
  236. case '!':
  237. case '~':
  238. case '+':
  239. case '-':
  240. if (pta != buf3)
  241. {
  242. MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
  243. }
  244. else
  245. {
  246. *pta++ = *pti++;
  247. }
  248. break;
  249. case '{':
  250. if (pta != buf3)
  251. {
  252. if (debug)
  253. {
  254. show_debug(ses, LIST_VARIABLE, "MATH EXP: { FOUND INSIDE A NUMBER");
  255. }
  256. return FALSE;
  257. }
  258. *pta++ = *pti++;
  259. status = EXP_BRACE;
  260. nest++;
  261. break;
  262. case '"':
  263. if (pta != buf3)
  264. {
  265. if (debug)
  266. {
  267. show_debug(ses, LIST_VARIABLE, "MATH EXP: \" FOUND INSIDE A NUMBER");
  268. }
  269. return FALSE;
  270. }
  271. *pta++ = *pti++;
  272. nest++;
  273. status = EXP_STRING;
  274. break;
  275. case '(':
  276. if (pta != buf3)
  277. {
  278. if (debug)
  279. {
  280. show_debug(ses, LIST_VARIABLE, "#MATH EXP: PARANTESES FOUND INSIDE A NUMBER");
  281. }
  282. return FALSE;
  283. }
  284. *pta++ = *pti++;
  285. MATH_NODE(FALSE, EXP_PR_LVL, EXP_VARIABLE);
  286. level++;
  287. break;
  288. case ',':
  289. pti++;
  290. break;
  291. case ':':
  292. *pta++ = *pti++;
  293. break;
  294. case '.':
  295. if (pta != buf3 && pti[1] == '.')
  296. {
  297. MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
  298. }
  299. else
  300. {
  301. *pta++ = *pti++;
  302. if (point >= 0)
  303. {
  304. if (debug)
  305. {
  306. show_debug(ses, LIST_VARIABLE, "#MATH EXP: MORE THAN ONE POINT FOUND INSIDE A NUMBER");
  307. }
  308. precision = 0;
  309. return FALSE;
  310. }
  311. point++;
  312. }
  313. break;
  314. case ' ':
  315. case '\t':
  316. pti++;
  317. break;
  318. case ')':
  319. case 'd':
  320. case '*':
  321. case '/':
  322. case '%':
  323. case '<':
  324. case '>':
  325. case '&':
  326. case '^':
  327. case '|':
  328. case '=':
  329. if (pta != buf3)
  330. {
  331. MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
  332. }
  333. else
  334. {
  335. *pta++ = *pti++;
  336. }
  337. break;
  338. case 'K':
  339. case 'M':
  340. // case 'G':
  341. // case 'T':
  342. // case 'P':
  343. // case 'E':
  344. // case 'Z':
  345. // case 'Y':
  346. if (pta == buf3 || metric == 1)
  347. {
  348. badnumber = 1;
  349. if (debug == 0)
  350. {
  351. return FALSE;
  352. }
  353. *pta++ = *pti++;
  354. *pta = 0;
  355. }
  356. else
  357. {
  358. MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
  359. *pta++ = '*';
  360. MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
  361. switch (*pti++)
  362. {
  363. case 'K': pta += sprintf(pta, "1000"); break;
  364. case 'M': pta += sprintf(pta, "1000000"); break;
  365. case 'G': pta += sprintf(pta, "1000000000"); break;
  366. case 'T': pta += sprintf(pta, "1000000000000"); break;
  367. case 'P': pta += sprintf(pta, "1000000000000000"); break;
  368. case 'E': pta += sprintf(pta, "1000000000000000000"); break;
  369. case 'Z': pta += sprintf(pta, "1000000000000000000000"); break;
  370. case 'Y': pta += sprintf(pta, "1000000000000000000000000"); break;
  371. }
  372. metric = 1;
  373. }
  374. break;
  375. case 'm':
  376. case 'u':
  377. // case 'n':
  378. // case 'p':
  379. // case 'f':
  380. // case 'a':
  381. // case 'z':
  382. // case 'y':
  383. if (pta == buf3 || metric == 1)
  384. {
  385. badnumber = 1;
  386. if (debug == 0)
  387. {
  388. return FALSE;
  389. }
  390. *pta++ = *pti++;
  391. *pta = 0;
  392. }
  393. else
  394. {
  395. MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
  396. *pta++ = '/';
  397. MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
  398. switch (*pti++)
  399. {
  400. case 'm': pta += sprintf(pta, "1000"); break;
  401. case 'u': pta += sprintf(pta, "1000000"); break;
  402. case 'n': pta += sprintf(pta, "1000000000"); break;
  403. case 'p': pta += sprintf(pta, "1000000000000"); break;
  404. case 'f': pta += sprintf(pta, "1000000000000000"); break;
  405. case 'a': pta += sprintf(pta, "1000000000000000000"); break;
  406. case 'z': pta += sprintf(pta, "1000000000000000000000"); break;
  407. case 'y': pta += sprintf(pta, "1000000000000000000000000"); break;
  408. }
  409. metric = 1;
  410. precision = UMAX(precision, strlen(buf3) -1);
  411. }
  412. break;
  413. default:
  414. *pta++ = *pti++;
  415. *pta = 0;
  416. badnumber = 1;
  417. if (debug == 0)
  418. {
  419. return FALSE;
  420. }
  421. break;
  422. }
  423. break;
  424. case EXP_STRING:
  425. switch (*pti)
  426. {
  427. case '"':
  428. *pta++ = *pti++;
  429. nest--;
  430. if (nest == 0)
  431. {
  432. MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
  433. }
  434. break;
  435. default:
  436. *pta++ = *pti++;
  437. break;
  438. }
  439. break;
  440. case EXP_BRACE:
  441. switch (*pti)
  442. {
  443. case '{':
  444. *pta++ = *pti++;
  445. nest++;
  446. break;
  447. case '}':
  448. *pta++ = *pti++;
  449. nest--;
  450. if (nest == 0)
  451. {
  452. MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
  453. }
  454. break;
  455. default:
  456. *pta++ = *pti++;
  457. break;
  458. }
  459. break;
  460. case EXP_OPERATOR:
  461. switch (*pti)
  462. {
  463. case ' ':
  464. pti++;
  465. break;
  466. case '.':
  467. if (pti[1] == '.')
  468. {
  469. *pta++ = *pti++;
  470. *pta++ = *pti++;
  471. MATH_NODE(FALSE, EXP_PR_LOGCOMP, EXP_VARIABLE);
  472. break;
  473. }
  474. else
  475. {
  476. if (debug)
  477. {
  478. show_debug(ses, LIST_VARIABLE, "#MATH EXP: UNKNOWN OPERATOR: %c%c", pti[0], pti[1]);
  479. }
  480. return FALSE;
  481. }
  482. break;
  483. case ')':
  484. *pta++ = *pti++;
  485. level--;
  486. MATH_NODE(FALSE, EXP_PR_LVL, EXP_OPERATOR);
  487. break;
  488. case 'd':
  489. *pta++ = *pti++;
  490. MATH_NODE(FALSE, EXP_PR_DICE, EXP_VARIABLE);
  491. break;
  492. case '*':
  493. *pta++ = *pti++;
  494. switch (*pti)
  495. {
  496. case '*':
  497. *pta++ = *pti++;
  498. MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
  499. break;
  500. default:
  501. MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
  502. break;
  503. }
  504. break;
  505. case '/':
  506. *pta++ = *pti++;
  507. switch (*pti)
  508. {
  509. case '/':
  510. *pta++ = *pti++;
  511. MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
  512. break;
  513. default:
  514. MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
  515. break;
  516. }
  517. break;
  518. case '%':
  519. *pta++ = *pti++;
  520. MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
  521. break;
  522. case '+':
  523. case '-':
  524. *pta++ = *pti++;
  525. MATH_NODE(FALSE, EXP_PR_INTADD, EXP_VARIABLE);
  526. break;
  527. case '<':
  528. *pta++ = *pti++;
  529. switch (*pti)
  530. {
  531. case '<':
  532. *pta++ = *pti++;
  533. MATH_NODE(FALSE, EXP_PR_BITSHIFT, EXP_VARIABLE);
  534. break;
  535. case '=':
  536. *pta++ = *pti++;
  537. MATH_NODE(FALSE, EXP_PR_LOGLTGT, EXP_VARIABLE);
  538. break;
  539. default:
  540. MATH_NODE(FALSE, EXP_PR_LOGLTGT, EXP_VARIABLE);
  541. break;
  542. }
  543. break;
  544. case '>':
  545. *pta++ = *pti++;
  546. switch (*pti)
  547. {
  548. case '>':
  549. *pta++ = *pti++;
  550. MATH_NODE(FALSE, EXP_PR_BITSHIFT, EXP_VARIABLE);
  551. break;
  552. case '=':
  553. *pta++ = *pti++;
  554. MATH_NODE(FALSE, EXP_PR_LOGLTGT, EXP_VARIABLE);
  555. break;
  556. default:
  557. MATH_NODE(FALSE, EXP_PR_LOGLTGT, EXP_VARIABLE);
  558. break;
  559. }
  560. break;
  561. case '&':
  562. *pta++ = *pti++;
  563. switch (*pti)
  564. {
  565. case '&':
  566. *pta++ = *pti++;
  567. MATH_NODE(FALSE, EXP_PR_LOGAND, EXP_VARIABLE);
  568. break;
  569. default:
  570. MATH_NODE(FALSE, EXP_PR_BITAND, EXP_VARIABLE);
  571. break;
  572. }
  573. break;
  574. case '^':
  575. *pta++ = *pti++;
  576. switch (*pti)
  577. {
  578. case '^':
  579. *pta++ = *pti++;
  580. MATH_NODE(FALSE, EXP_PR_LOGXOR, EXP_VARIABLE);
  581. break;
  582. default:
  583. MATH_NODE(FALSE, EXP_PR_BITXOR, EXP_VARIABLE);
  584. break;
  585. }
  586. break;
  587. case '|':
  588. *pta++ = *pti++;
  589. switch (*pti)
  590. {
  591. case '|':
  592. *pta++ = *pti++;
  593. MATH_NODE(FALSE, EXP_PR_LOGOR, EXP_VARIABLE);
  594. break;
  595. default:
  596. MATH_NODE(FALSE, EXP_PR_BITOR, EXP_VARIABLE);
  597. break;
  598. }
  599. break;
  600. case '=':
  601. case '!':
  602. *pta++ = *pti++;
  603. switch (*pti)
  604. {
  605. case '=':
  606. *pta++ = *pti++;
  607. if (*pti == '=')
  608. {
  609. *pta++ = *pti++;
  610. }
  611. MATH_NODE(FALSE, EXP_PR_LOGCOMP, EXP_VARIABLE);
  612. break;
  613. default:
  614. if (debug)
  615. {
  616. show_debug(ses, LIST_VARIABLE, "#MATH EXP: UNKNOWN OPERATOR: %c%c", pti[-1], pti[0]);
  617. }
  618. return FALSE;
  619. }
  620. break;
  621. default:
  622. if (debug)
  623. {
  624. show_debug(ses, LIST_VARIABLE, "#MATH EXP: UNKNOWN OPERATOR: %c", *pti);
  625. }
  626. return FALSE;
  627. }
  628. break;
  629. }
  630. }
  631. if (level != 0)
  632. {
  633. if (debug)
  634. {
  635. show_debug(ses, LIST_VARIABLE, "#MATH EXP: UNMATCHED PARENTHESES, LEVEL: %d", level);
  636. }
  637. return FALSE;
  638. }
  639. if (status != EXP_OPERATOR)
  640. {
  641. MATH_NODE(TRUE, EXP_PR_VAR, EXP_OPERATOR);
  642. }
  643. return TRUE;
  644. }
  645. void mathexp_level(struct session *ses, struct link_data *node)
  646. {
  647. int priority;
  648. mathnode_e = node;
  649. while (node->prev)
  650. {
  651. if (atoi(node->prev->str1) < atoi(node->str1))
  652. {
  653. break;
  654. }
  655. node = node->prev;
  656. }
  657. mathnode_s = node;
  658. for (priority = 0 ; priority < EXP_PR_VAR ; priority++)
  659. {
  660. for (node = mathnode_s ; node ; node = node->next)
  661. {
  662. if (atoi(node->str2) == priority)
  663. {
  664. mathexp_compute(ses, node);
  665. }
  666. if (node == mathnode_e)
  667. {
  668. break;
  669. }
  670. }
  671. }
  672. node = mathnode_s;
  673. while (node->prev && node->next)
  674. {
  675. if (atoi(node->prev->str2) == EXP_PR_LVL && atoi(node->next->str2) == EXP_PR_LVL)
  676. {
  677. free(node->str1);
  678. node->str1 = strdup(node->next->str1);
  679. del_math_node(node->next);
  680. del_math_node(node->prev);
  681. }
  682. else
  683. {
  684. break;
  685. }
  686. }
  687. return;
  688. }
  689. void mathexp_compute(struct session *ses, struct link_data *node)
  690. {
  691. char temp[BUFFER_SIZE];
  692. int integer64 = 0;
  693. long double value = 0;
  694. unsigned long long min = 0, max = 0;
  695. unsigned long long value64 = 0;
  696. switch (node->str3[0])
  697. {
  698. case '.':
  699. integer64 = 1;
  700. SET_BIT(max, (unsigned int) tintoi(node->next->str3));
  701. max = max << 32ULL;
  702. SET_BIT(min, (unsigned int) tintoi(node->prev->str3));
  703. value64 = max | min;
  704. case 'd':
  705. if (tintoi(node->next->str3) <= 0)
  706. {
  707. show_debug(ses, LIST_VARIABLE, "#MATHEXP: INVALID DICE: %s", node->next->str3);
  708. value = 0;
  709. }
  710. else
  711. {
  712. value = tindice(ses, node->prev->str3, node->next->str3);
  713. }
  714. break;
  715. case '*':
  716. switch (node->str3[1])
  717. {
  718. case '*':
  719. value = pow(tintoi(node->prev->str3), tintoi(node->next->str3));
  720. break;
  721. default:
  722. value = tintoi(node->prev->str3) * tintoi(node->next->str3);
  723. break;
  724. }
  725. break;
  726. case '/':
  727. switch (node->str3[1])
  728. {
  729. case '/':
  730. if (tintoi(node->next->str3) == 3)
  731. {
  732. value = cbrt(tintoi(node->prev->str3));
  733. }
  734. else
  735. {
  736. value = sqrt(tintoi(node->prev->str3));
  737. }
  738. break;
  739. default:
  740. if (tintoi(node->next->str3) == 0)
  741. {
  742. show_debug(ses, LIST_VARIABLE, "#MATH ERROR: DIVISION ZERO.");
  743. value = 0;
  744. precision = 0;
  745. // value = tintoi(node->prev->str3);
  746. }
  747. else
  748. {
  749. if (precision)
  750. {
  751. value = tintoi(node->prev->str3) / tintoi(node->next->str3);
  752. }
  753. else
  754. {
  755. value = (long long) tintoi(node->prev->str3) / (long long) tintoi(node->next->str3);
  756. }
  757. }
  758. break;
  759. }
  760. break;
  761. case '%':
  762. if (tintoi(node->next->str3) == 0)
  763. {
  764. show_debug(ses, LIST_VARIABLE, "#MATH ERROR: MODULO ZERO.");
  765. value = tintoi(node->prev->str3);
  766. }
  767. else
  768. {
  769. integer64 = 1;
  770. value64 = tintou(node->prev->str3) % tintou(node->next->str3);
  771. }
  772. break;
  773. case '+':
  774. value = tintoi(node->prev->str3) + tintoi(node->next->str3);
  775. break;
  776. case '-':
  777. value = tintoi(node->prev->str3) - tintoi(node->next->str3);
  778. break;
  779. case '<':
  780. switch (node->str3[1])
  781. {
  782. case '=':
  783. value = tincmp(node->prev->str3, node->next->str3) <= 0;
  784. break;
  785. case '<':
  786. value = atoll(node->prev->str3) << atoll(node->next->str3);
  787. break;
  788. default:
  789. value = tincmp(node->prev->str3, node->next->str3) < 0;
  790. break;
  791. }
  792. break;
  793. case '>':
  794. switch (node->str3[1])
  795. {
  796. case '=':
  797. value = tincmp(node->prev->str3, node->next->str3) >= 0;
  798. break;
  799. case '>':
  800. value = atoll(node->prev->str3) >> atoll(node->next->str3);
  801. break;
  802. default:
  803. value = tincmp(node->prev->str3, node->next->str3) > 0;
  804. break;
  805. }
  806. break;
  807. case '&':
  808. switch (node->str3[1])
  809. {
  810. case '&':
  811. value = tintoi(node->prev->str3) && tintoi(node->next->str3);
  812. break;
  813. default:
  814. value = atoll(node->prev->str3) & atoll(node->next->str3);
  815. break;
  816. }
  817. break;
  818. case '^':
  819. switch (node->str3[1])
  820. {
  821. case '^':
  822. value = ((tintoi(node->prev->str3) || tintoi(node->next->str3)) && (!tintoi(node->prev->str3) != !tintoi(node->next->str3)));
  823. break;
  824. default:
  825. value = atoll(node->prev->str3) ^ atoll(node->next->str3);
  826. break;
  827. }
  828. break;
  829. case '|':
  830. switch (node->str3[1])
  831. {
  832. case '|':
  833. value = tintoi(node->prev->str3) || tintoi(node->next->str3);
  834. break;
  835. default:
  836. value = atoll(node->prev->str3) | atoll(node->next->str3);
  837. break;
  838. }
  839. break;
  840. case '=':
  841. if (node->str3[1] == '=' && node->str3[2] == '=')
  842. {
  843. value = tincmp(node->prev->str3, node->next->str3) == 0;
  844. }
  845. else
  846. {
  847. value = tineval(ses, node->prev->str3, node->next->str3) != 0;
  848. }
  849. break;
  850. case '!':
  851. if (node->str3[1] == '=' && node->str3[2] == '=')
  852. {
  853. value = tincmp(node->prev->str3, node->next->str3) != 0;
  854. }
  855. else
  856. {
  857. value = tineval(ses, node->prev->str3, node->next->str3) == 0;
  858. }
  859. break;
  860. default:
  861. show_debug(ses, LIST_VARIABLE, "#COMPUTE EXP: UNKNOWN OPERATOR: %c%c", node->str3[0], node->str3[1]);
  862. value = 0;
  863. break;
  864. }
  865. if (node->prev == mathnode_s)
  866. {
  867. mathnode_s = node;
  868. }
  869. if (node->next == mathnode_e)
  870. {
  871. mathnode_e = node;
  872. }
  873. del_math_node(node->next);
  874. del_math_node(node->prev);
  875. sprintf(temp, "%d", EXP_PR_VAR);
  876. free(node->str2);
  877. node->str2 = strdup(temp);
  878. if (integer64)
  879. {
  880. sprintf(temp, "%llu", value64);
  881. }
  882. else
  883. {
  884. sprintf(temp, "%.*Lf", precision, value);
  885. }
  886. free(node->str3);
  887. node->str3 = strdup(temp);
  888. }
  889. /*
  890. Keep synced with is_number()
  891. */
  892. long double tintoi(char *str)
  893. {
  894. char *ptr = str;
  895. long double values[5] = {0, 0, 0, 0, 0}, m = 1;
  896. int i = 1, d = 0;
  897. if (*ptr == 0)
  898. {
  899. return 0;
  900. }
  901. switch (*ptr)
  902. {
  903. case '!':
  904. case '~':
  905. case '+':
  906. case '-':
  907. ptr++;
  908. break;
  909. }
  910. ptr = str + strlen(str);
  911. while (TRUE)
  912. {
  913. ptr--;
  914. switch (*ptr)
  915. {
  916. case '0':
  917. case '1':
  918. case '2':
  919. case '3':
  920. case '4':
  921. case '5':
  922. case '6':
  923. case '7':
  924. case '8':
  925. case '9':
  926. values[i] += (*ptr - '0') * m;
  927. m *= 10;
  928. break;
  929. case '.':
  930. if (d)
  931. {
  932. return 0;
  933. }
  934. d = 1;
  935. values[0] = values[1] / m;
  936. values[1] = 0;
  937. m = 1;
  938. break;
  939. case ':':
  940. switch (i)
  941. {
  942. case 2:
  943. values[i] *= 60;
  944. break;
  945. case 3:
  946. values[i] *= 60 * 60;
  947. break;
  948. case 4:
  949. return 0;
  950. }
  951. i++;
  952. m = 1;
  953. break;
  954. case '!':
  955. case '~':
  956. case '+':
  957. case '-':
  958. if (ptr == str)
  959. {
  960. break;
  961. }
  962. return 0;
  963. default:
  964. return 0;
  965. }
  966. if (ptr == str)
  967. {
  968. switch (i)
  969. {
  970. case 2:
  971. values[i] *= 60;
  972. break;
  973. case 3:
  974. values[i] *= 60 * 60;
  975. break;
  976. case 4:
  977. values[i] *= 60 * 60 * 24;
  978. break;
  979. }
  980. break;
  981. }
  982. }
  983. switch (*str)
  984. {
  985. case '!':
  986. return !(values[0] + values[1] + values[2] + values[3] + values[4]);
  987. case '~':
  988. return ~ (long long) (values[0] + values[1] + values[2] + values[3] + values[4]);
  989. case '+':
  990. return +(values[0] + values[1] + values[2] + values[3] + values[4]);
  991. case '-':
  992. return -(values[0] + values[1] + values[2] + values[3] + values[4]);
  993. default:
  994. return (values[0] + values[1] + values[2] + values[3] + values[4]);
  995. }
  996. }
  997. unsigned long long tintou(char *str)
  998. {
  999. char *ptr = str;
  1000. unsigned long long value = 0, m = 1;
  1001. if (*ptr == 0)
  1002. {
  1003. return 0;
  1004. }
  1005. switch (*ptr)
  1006. {
  1007. case '!':
  1008. case '~':
  1009. case '+':
  1010. case '-':
  1011. ptr++;
  1012. break;
  1013. }
  1014. ptr = str + strlen(str);
  1015. while (ptr != str)
  1016. {
  1017. ptr--;
  1018. switch (*ptr)
  1019. {
  1020. case '0':
  1021. case '1':
  1022. case '2':
  1023. case '3':
  1024. case '4':
  1025. case '5':
  1026. case '6':
  1027. case '7':
  1028. case '8':
  1029. case '9':
  1030. value += (*ptr - '0') * m;
  1031. m *= 10;
  1032. break;
  1033. case '.':
  1034. *ptr = 0;
  1035. break;
  1036. case ':':
  1037. return 0;
  1038. break;
  1039. case '!':
  1040. case '~':
  1041. case '+':
  1042. case '-':
  1043. if (ptr == str)
  1044. {
  1045. break;
  1046. }
  1047. return 0;
  1048. default:
  1049. return 0;
  1050. }
  1051. }
  1052. switch (str[0])
  1053. {
  1054. case '!':
  1055. return !value;
  1056. case '~':
  1057. return ~value;
  1058. case '+':
  1059. return +value;
  1060. case '-':
  1061. return -value;
  1062. default:
  1063. return value;
  1064. }
  1065. }
  1066. /*
  1067. Keep synched with tintoi() and is_math()
  1068. */
  1069. int is_number(char *str)
  1070. {
  1071. char *ptr = str;
  1072. int i = 1, d = 0, valid = 0;
  1073. if (*ptr == 0)
  1074. {
  1075. return FALSE;
  1076. }
  1077. ptr = str + strlen(str);
  1078. while (TRUE)
  1079. {
  1080. ptr--;
  1081. switch (*ptr)
  1082. {
  1083. case '0':
  1084. case '1':
  1085. case '2':
  1086. case '3':
  1087. case '4':
  1088. case '5':
  1089. case '6':
  1090. case '7':
  1091. case '8':
  1092. case '9':
  1093. valid = TRUE;
  1094. break;
  1095. case '.':
  1096. if (d)
  1097. {
  1098. return FALSE;
  1099. }
  1100. d = 1;
  1101. valid = FALSE;
  1102. break;
  1103. case ':':
  1104. if (i == 4)
  1105. {
  1106. return FALSE;
  1107. }
  1108. i++;
  1109. valid = FALSE;
  1110. break;
  1111. case '!':
  1112. case '~':
  1113. case '+':
  1114. case '-':
  1115. if (ptr != str)
  1116. {
  1117. return FALSE;
  1118. }
  1119. break;
  1120. default:
  1121. return FALSE;
  1122. }
  1123. if (ptr == str)
  1124. {
  1125. break;
  1126. }
  1127. }
  1128. return valid;
  1129. }
  1130. long double tincmp(char *left, char *right)
  1131. {
  1132. long double left_val, right_val;
  1133. switch (left[0])
  1134. {
  1135. case '{':
  1136. case '"':
  1137. return strcmp(left, right);
  1138. default:
  1139. left_val = tintoi(left);
  1140. right_val = tintoi(right);
  1141. return left_val - right_val;
  1142. }
  1143. }
  1144. long double tineval(struct session *ses, char *left, char *right)
  1145. {
  1146. long double left_val, right_val;
  1147. switch (left[0])
  1148. {
  1149. case '{':
  1150. get_arg_in_braces(ses, left, left, GET_ALL);
  1151. get_arg_in_braces(ses, right, right, GET_ALL);
  1152. return match(ses, left, right, SUB_NONE);
  1153. case '"':
  1154. return match(ses, left, right, SUB_NONE);
  1155. default:
  1156. left_val = tintoi(left);
  1157. right_val = tintoi(right);
  1158. return left_val == right_val;
  1159. }
  1160. }
  1161. long double tindice(struct session *ses, char *left, char *right)
  1162. {
  1163. unsigned long long cnt, numdice, sizedice, sum;
  1164. long double estimate;
  1165. numdice = (unsigned long long) tintoi(left);
  1166. sizedice = (unsigned long long) tintoi(right);
  1167. if (sizedice == 0)
  1168. {
  1169. return 0;
  1170. }
  1171. if (numdice > 100)
  1172. {
  1173. estimate = numdice / 100.0;
  1174. numdice = 100;
  1175. }
  1176. else
  1177. {
  1178. estimate = 1;
  1179. }
  1180. for (cnt = sum = 0 ; cnt < numdice ; cnt++)
  1181. {
  1182. sum += generate_rand(ses) % sizedice + 1;
  1183. }
  1184. sum *= estimate;
  1185. return (long double) sum;
  1186. }