| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393 |
- /******************************************************************************
- * This file is part of TinTin++ *
- * *
- * Copyright 2004-2020 Igor van den Hoven *
- * *
- * TinTin++ is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 3 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with TinTin++. If not, see https://www.gnu.org/licenses. *
- ******************************************************************************/
- /******************************************************************************
- * T I N T I N + + *
- * *
- * coded by Igor van den Hoven 2004 *
- ******************************************************************************/
- #include "tintin.h"
- #define EXP_VARIABLE 0
- #define EXP_STRING 1
- #define EXP_BRACE 2
- #define EXP_OPERATOR 3
- #define EXP_PR_DICE 0
- #define EXP_PR_INTMUL 1
- #define EXP_PR_INTADD 2
- #define EXP_PR_BITSHIFT 3
- #define EXP_PR_LOGLTGT 4
- #define EXP_PR_LOGCOMP 5
- #define EXP_PR_BITAND 6
- #define EXP_PR_BITXOR 7
- #define EXP_PR_BITOR 8
- #define EXP_PR_LOGAND 9
- #define EXP_PR_LOGXOR 10
- #define EXP_PR_LOGOR 11
- #define EXP_PR_VAR 12
- #define EXP_PR_LVL 13
- struct link_data *math_head;
- struct link_data *math_tail;
- struct link_data *mathnode_s;
- struct link_data *mathnode_e;
- int precision;
- DO_COMMAND(do_math)
- {
- char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
- struct listnode *node;
- long double result;
- arg = sub_arg_in_braces(ses, arg, arg1, GET_NST, SUB_VAR|SUB_FUN);
- arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
- if (*arg1 == 0 || *arg2 == 0)
- {
- show_error(ses, LIST_VARIABLE, "#SYNTAX: #MATH {variable} {expression}.");
- }
- else
- {
- result = get_number(ses, arg2);
- node = set_nest_node_ses(ses, arg1, "%.*Lf", precision, result);
- show_message(ses, LIST_VARIABLE, "#MATH: VARIABLE {%s} HAS BEEN SET TO {%s}.", arg1, node->arg2);
- }
- return ses;
- }
- int is_math(struct session *ses, char *str)
- {
- return mathexp_tokenize(ses, str, 0, 0);
- }
- long double get_number(struct session *ses, char *str)
- {
- long double val;
- char result[BUFFER_SIZE];
- mathexp(ses, str, result, 0);
- val = tintoi(result);
- return val;
- }
- unsigned long long get_integer(struct session *ses, char *str)
- {
- unsigned long long val;
- char result[BUFFER_SIZE];
- mathexp(ses, str, result, 0);
- val = tintou(result);
- return val;
- }
- int get_ellipsis(struct listroot *root, char *name, int *min, int *max)
- {
- unsigned long long range = get_integer(root->ses, name);
- push_call("get_ellipsis(%p,%p,%p,%p)",root,name,min,max);
- *min = (int) (HAS_BIT(range, 0x00000000FFFFFFFFLL));
- *max = (int) (HAS_BIT(range, 0xFFFFFFFF00000000ULL) >> 32ULL);
- *min = *min > 0 ? *min - 1 : root->used + *min;
- *max = *max > 0 ? *max - 1 : root->used + *max;
- *min = URANGE(0, *min, root->used - 1);
- *max = URANGE(0, *max, root->used - 1);
- pop_call();
- return 1 + (*min < *max ? *max - *min : *min - *max);
- }
- long double get_double(struct session *ses, char *str)
- {
- long double val;
- char result[BUFFER_SIZE];
- mathexp(ses, str, result, 1);
- val = tintoi(result);
- return val;
- }
-
- void get_number_string(struct session *ses, char *str, char *result)
- {
- long double val = get_number(ses, str);
- sprintf(result, "%.*Lf", precision, val);
- }
- long double mathswitch(struct session *ses, char *left, char *right)
- {
- char shift[BUFFER_SIZE];
- sprintf(shift, "%s == %s", left, right);
- return get_number(ses, shift);
- }
- /*
- Flexible tokenized mathematical expression interpreter
-
- If seed is set it forces floating point math
- */
- void mathexp(struct session *ses, char *str, char *result, int seed)
- {
- struct link_data *node;
- substitute(ses, str, result, SUB_VAR|SUB_FUN);
- if (mathexp_tokenize(ses, result, seed, TRUE) == FALSE)
- {
- return;
- }
- node = math_head;
- while (node->prev || node->next)
- {
- if (node->next == NULL || atoi(node->next->str1) < atoi(node->str1))
- {
- mathexp_level(ses, node);
- node = math_head;
- }
- else
- {
- node = node->next;
- }
- }
- strcpy(result, node->str3);
- }
- #define MATH_NODE(buffercheck, priority, newstatus) \
- { \
- if (buffercheck && pta == buf3) \
- { \
- return FALSE; \
- } \
- if (badnumber && debug) \
- { \
- badnumber = 0; \
- precision = 0; \
- show_debug(ses, LIST_VARIABLE, "#MATH EXP: INVALID NUMBER %s.", buf3); \
- } \
- *pta = 0; \
- sprintf(buf1, "%02d", level); \
- sprintf(buf2, "%02d", priority); \
- add_math_node(buf1, buf2, buf3); \
- status = newstatus; \
- pta = buf3; \
- point = -1; \
- metric = 0; \
- }
- void add_math_node(char *arg1, char *arg2, char *arg3)
- {
- struct link_data *link;
- link = (struct link_data *) calloc(1, sizeof(struct link_data));
- link->str1 = strdup(arg1);
- link->str2 = strdup(arg2);
- link->str3 = strdup(arg3);
- LINK(link, math_head, math_tail);
- }
- void del_math_node(struct link_data *node)
- {
- UNLINK(node, math_head, math_tail);
- free(node->str1);
- free(node->str2);
- free(node->str3);
- free(node);
- }
- int mathexp_tokenize(struct session *ses, char *str, int seed, int debug)
- {
- char buf1[BUFFER_SIZE], buf2[BUFFER_SIZE], buf3[STRING_SIZE], *pti, *pta;
- int level, status, point, badnumber, metric, nest;
- nest = 0;
- level = 0;
- metric = 0;
- point = -1;
- status = EXP_VARIABLE;
- precision = seed;
- badnumber = 0;
- pta = buf3;
- pti = str;
- while (math_head)
- {
- del_math_node(math_head);
- }
- while (*pti)
- {
- switch (status)
- {
- case EXP_VARIABLE:
- switch (*pti)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- *pta++ = *pti++;
- if (metric)
- {
- badnumber = 1;
- if (debug == 0)
- {
- return FALSE;
- }
- }
- if (point >= 0)
- {
- point++;
- if (precision < point)
- {
- precision = point;
- }
- }
- break;
- case '!':
- case '~':
- case '+':
- case '-':
- if (pta != buf3)
- {
- MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
- }
- else
- {
- *pta++ = *pti++;
- }
- break;
- case '{':
- if (pta != buf3)
- {
- if (debug)
- {
- show_debug(ses, LIST_VARIABLE, "MATH EXP: { FOUND INSIDE A NUMBER");
- }
- return FALSE;
- }
- *pta++ = *pti++;
- status = EXP_BRACE;
- nest++;
- break;
- case '"':
- if (pta != buf3)
- {
- if (debug)
- {
- show_debug(ses, LIST_VARIABLE, "MATH EXP: \" FOUND INSIDE A NUMBER");
- }
- return FALSE;
- }
- *pta++ = *pti++;
- nest++;
- status = EXP_STRING;
- break;
- case '(':
- if (pta != buf3)
- {
- if (debug)
- {
- show_debug(ses, LIST_VARIABLE, "#MATH EXP: PARANTESES FOUND INSIDE A NUMBER");
- }
- return FALSE;
- }
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_LVL, EXP_VARIABLE);
- level++;
- break;
- case ',':
- pti++;
- break;
- case ':':
- *pta++ = *pti++;
- break;
-
- case '.':
- if (pta != buf3 && pti[1] == '.')
- {
- MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
- }
- else
- {
- *pta++ = *pti++;
- if (point >= 0)
- {
- if (debug)
- {
- show_debug(ses, LIST_VARIABLE, "#MATH EXP: MORE THAN ONE POINT FOUND INSIDE A NUMBER");
- }
- precision = 0;
- return FALSE;
- }
- point++;
- }
- break;
- case ' ':
- case '\t':
- pti++;
- break;
- case ')':
- case 'd':
- case '*':
- case '/':
- case '%':
- case '<':
- case '>':
- case '&':
- case '^':
- case '|':
- case '=':
- if (pta != buf3)
- {
- MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
- }
- else
- {
- *pta++ = *pti++;
- }
- break;
- case 'K':
- case 'M':
- // case 'G':
- // case 'T':
- // case 'P':
- // case 'E':
- // case 'Z':
- // case 'Y':
- if (pta == buf3 || metric == 1)
- {
- badnumber = 1;
- if (debug == 0)
- {
- return FALSE;
- }
- *pta++ = *pti++;
- *pta = 0;
- }
- else
- {
- MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
- *pta++ = '*';
- MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
- switch (*pti++)
- {
- case 'K': pta += sprintf(pta, "1000"); break;
- case 'M': pta += sprintf(pta, "1000000"); break;
- case 'G': pta += sprintf(pta, "1000000000"); break;
- case 'T': pta += sprintf(pta, "1000000000000"); break;
- case 'P': pta += sprintf(pta, "1000000000000000"); break;
- case 'E': pta += sprintf(pta, "1000000000000000000"); break;
- case 'Z': pta += sprintf(pta, "1000000000000000000000"); break;
- case 'Y': pta += sprintf(pta, "1000000000000000000000000"); break;
- }
- metric = 1;
- }
- break;
- case 'm':
- case 'u':
- // case 'n':
- // case 'p':
- // case 'f':
- // case 'a':
- // case 'z':
- // case 'y':
- if (pta == buf3 || metric == 1)
- {
- badnumber = 1;
- if (debug == 0)
- {
- return FALSE;
- }
- *pta++ = *pti++;
- *pta = 0;
- }
- else
- {
- MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
- *pta++ = '/';
- MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
- switch (*pti++)
- {
- case 'm': pta += sprintf(pta, "1000"); break;
- case 'u': pta += sprintf(pta, "1000000"); break;
- case 'n': pta += sprintf(pta, "1000000000"); break;
- case 'p': pta += sprintf(pta, "1000000000000"); break;
- case 'f': pta += sprintf(pta, "1000000000000000"); break;
- case 'a': pta += sprintf(pta, "1000000000000000000"); break;
- case 'z': pta += sprintf(pta, "1000000000000000000000"); break;
- case 'y': pta += sprintf(pta, "1000000000000000000000000"); break;
- }
- metric = 1;
- precision = UMAX(precision, strlen(buf3) -1);
- }
- break;
- default:
- *pta++ = *pti++;
- *pta = 0;
- badnumber = 1;
- if (debug == 0)
- {
- return FALSE;
- }
- break;
- }
- break;
- case EXP_STRING:
- switch (*pti)
- {
- case '"':
- *pta++ = *pti++;
- nest--;
- if (nest == 0)
- {
- MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
- }
- break;
- default:
- *pta++ = *pti++;
- break;
- }
- break;
- case EXP_BRACE:
- switch (*pti)
- {
- case '{':
- *pta++ = *pti++;
- nest++;
- break;
- case '}':
- *pta++ = *pti++;
- nest--;
- if (nest == 0)
- {
- MATH_NODE(FALSE, EXP_PR_VAR, EXP_OPERATOR);
- }
- break;
- default:
- *pta++ = *pti++;
- break;
- }
- break;
- case EXP_OPERATOR:
- switch (*pti)
- {
- case ' ':
- pti++;
- break;
- case '.':
- if (pti[1] == '.')
- {
- *pta++ = *pti++;
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_LOGCOMP, EXP_VARIABLE);
- break;
- }
- else
- {
- if (debug)
- {
- show_debug(ses, LIST_VARIABLE, "#MATH EXP: UNKNOWN OPERATOR: %c%c", pti[0], pti[1]);
- }
- return FALSE;
- }
- break;
- case ')':
- *pta++ = *pti++;
- level--;
- MATH_NODE(FALSE, EXP_PR_LVL, EXP_OPERATOR);
- break;
- case 'd':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_DICE, EXP_VARIABLE);
- break;
- case '*':
- *pta++ = *pti++;
- switch (*pti)
- {
- case '*':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
- break;
-
- default:
- MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
- break;
- }
- break;
-
- case '/':
- *pta++ = *pti++;
- switch (*pti)
- {
- case '/':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
- break;
- default:
- MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
- break;
- }
- break;
- case '%':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_INTMUL, EXP_VARIABLE);
- break;
- case '+':
- case '-':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_INTADD, EXP_VARIABLE);
- break;
- case '<':
- *pta++ = *pti++;
- switch (*pti)
- {
- case '<':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_BITSHIFT, EXP_VARIABLE);
- break;
- case '=':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_LOGLTGT, EXP_VARIABLE);
- break;
- default:
- MATH_NODE(FALSE, EXP_PR_LOGLTGT, EXP_VARIABLE);
- break;
- }
- break;
- case '>':
- *pta++ = *pti++;
- switch (*pti)
- {
- case '>':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_BITSHIFT, EXP_VARIABLE);
- break;
- case '=':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_LOGLTGT, EXP_VARIABLE);
- break;
- default:
- MATH_NODE(FALSE, EXP_PR_LOGLTGT, EXP_VARIABLE);
- break;
- }
- break;
- case '&':
- *pta++ = *pti++;
- switch (*pti)
- {
- case '&':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_LOGAND, EXP_VARIABLE);
- break;
- default:
- MATH_NODE(FALSE, EXP_PR_BITAND, EXP_VARIABLE);
- break;
- }
- break;
- case '^':
- *pta++ = *pti++;
- switch (*pti)
- {
- case '^':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_LOGXOR, EXP_VARIABLE);
- break;
- default:
- MATH_NODE(FALSE, EXP_PR_BITXOR, EXP_VARIABLE);
- break;
- }
- break;
- case '|':
- *pta++ = *pti++;
- switch (*pti)
- {
- case '|':
- *pta++ = *pti++;
- MATH_NODE(FALSE, EXP_PR_LOGOR, EXP_VARIABLE);
- break;
- default:
- MATH_NODE(FALSE, EXP_PR_BITOR, EXP_VARIABLE);
- break;
- }
- break;
- case '=':
- case '!':
- *pta++ = *pti++;
- switch (*pti)
- {
- case '=':
- *pta++ = *pti++;
- if (*pti == '=')
- {
- *pta++ = *pti++;
- }
- MATH_NODE(FALSE, EXP_PR_LOGCOMP, EXP_VARIABLE);
- break;
- default:
- if (debug)
- {
- show_debug(ses, LIST_VARIABLE, "#MATH EXP: UNKNOWN OPERATOR: %c%c", pti[-1], pti[0]);
- }
- return FALSE;
- }
- break;
- default:
- if (debug)
- {
- show_debug(ses, LIST_VARIABLE, "#MATH EXP: UNKNOWN OPERATOR: %c", *pti);
- }
- return FALSE;
- }
- break;
- }
- }
- if (level != 0)
- {
- if (debug)
- {
- show_debug(ses, LIST_VARIABLE, "#MATH EXP: UNMATCHED PARENTHESES, LEVEL: %d", level);
- }
- return FALSE;
- }
- if (status != EXP_OPERATOR)
- {
- MATH_NODE(TRUE, EXP_PR_VAR, EXP_OPERATOR);
- }
- return TRUE;
- }
- void mathexp_level(struct session *ses, struct link_data *node)
- {
- int priority;
- mathnode_e = node;
- while (node->prev)
- {
- if (atoi(node->prev->str1) < atoi(node->str1))
- {
- break;
- }
- node = node->prev;
- }
- mathnode_s = node;
- for (priority = 0 ; priority < EXP_PR_VAR ; priority++)
- {
- for (node = mathnode_s ; node ; node = node->next)
- {
- if (atoi(node->str2) == priority)
- {
- mathexp_compute(ses, node);
- }
- if (node == mathnode_e)
- {
- break;
- }
- }
- }
- node = mathnode_s;
- while (node->prev && node->next)
- {
- if (atoi(node->prev->str2) == EXP_PR_LVL && atoi(node->next->str2) == EXP_PR_LVL)
- {
- free(node->str1);
- node->str1 = strdup(node->next->str1);
- del_math_node(node->next);
- del_math_node(node->prev);
- }
- else
- {
- break;
- }
- }
- return;
- }
- void mathexp_compute(struct session *ses, struct link_data *node)
- {
- char temp[BUFFER_SIZE];
- int integer64 = 0;
- long double value = 0;
- unsigned long long min = 0, max = 0;
- unsigned long long value64 = 0;
- switch (node->str3[0])
- {
- case '.':
- integer64 = 1;
- SET_BIT(max, (unsigned int) tintoi(node->next->str3));
- max = max << 32ULL;
- SET_BIT(min, (unsigned int) tintoi(node->prev->str3));
- value64 = max | min;
- case 'd':
- if (tintoi(node->next->str3) <= 0)
- {
- show_debug(ses, LIST_VARIABLE, "#MATHEXP: INVALID DICE: %s", node->next->str3);
- value = 0;
- }
- else
- {
- value = tindice(ses, node->prev->str3, node->next->str3);
- }
- break;
- case '*':
- switch (node->str3[1])
- {
- case '*':
- value = pow(tintoi(node->prev->str3), tintoi(node->next->str3));
- break;
- default:
- value = tintoi(node->prev->str3) * tintoi(node->next->str3);
- break;
- }
- break;
- case '/':
- switch (node->str3[1])
- {
- case '/':
- if (tintoi(node->next->str3) == 3)
- {
- value = cbrt(tintoi(node->prev->str3));
- }
- else
- {
- value = sqrt(tintoi(node->prev->str3));
- }
- break;
- default:
- if (tintoi(node->next->str3) == 0)
- {
- show_debug(ses, LIST_VARIABLE, "#MATH ERROR: DIVISION ZERO.");
- value = 0;
- precision = 0;
- // value = tintoi(node->prev->str3);
- }
- else
- {
- if (precision)
- {
- value = tintoi(node->prev->str3) / tintoi(node->next->str3);
- }
- else
- {
- value = (long long) tintoi(node->prev->str3) / (long long) tintoi(node->next->str3);
- }
- }
- break;
- }
- break;
- case '%':
- if (tintoi(node->next->str3) == 0)
- {
- show_debug(ses, LIST_VARIABLE, "#MATH ERROR: MODULO ZERO.");
- value = tintoi(node->prev->str3);
- }
- else
- {
- integer64 = 1;
- value64 = tintou(node->prev->str3) % tintou(node->next->str3);
- }
- break;
- case '+':
- value = tintoi(node->prev->str3) + tintoi(node->next->str3);
- break;
- case '-':
- value = tintoi(node->prev->str3) - tintoi(node->next->str3);
- break;
- case '<':
- switch (node->str3[1])
- {
- case '=':
- value = tincmp(node->prev->str3, node->next->str3) <= 0;
- break;
- case '<':
- value = atoll(node->prev->str3) << atoll(node->next->str3);
- break;
- default:
- value = tincmp(node->prev->str3, node->next->str3) < 0;
- break;
- }
- break;
- case '>':
- switch (node->str3[1])
- {
- case '=':
- value = tincmp(node->prev->str3, node->next->str3) >= 0;
- break;
- case '>':
- value = atoll(node->prev->str3) >> atoll(node->next->str3);
- break;
- default:
- value = tincmp(node->prev->str3, node->next->str3) > 0;
- break;
- }
- break;
- case '&':
- switch (node->str3[1])
- {
- case '&':
- value = tintoi(node->prev->str3) && tintoi(node->next->str3);
- break;
- default:
- value = atoll(node->prev->str3) & atoll(node->next->str3);
- break;
- }
- break;
- case '^':
- switch (node->str3[1])
- {
- case '^':
- value = ((tintoi(node->prev->str3) || tintoi(node->next->str3)) && (!tintoi(node->prev->str3) != !tintoi(node->next->str3)));
- break;
- default:
- value = atoll(node->prev->str3) ^ atoll(node->next->str3);
- break;
- }
- break;
- case '|':
- switch (node->str3[1])
- {
- case '|':
- value = tintoi(node->prev->str3) || tintoi(node->next->str3);
- break;
- default:
- value = atoll(node->prev->str3) | atoll(node->next->str3);
- break;
- }
- break;
- case '=':
- if (node->str3[1] == '=' && node->str3[2] == '=')
- {
-
- value = tincmp(node->prev->str3, node->next->str3) == 0;
- }
- else
- {
- value = tineval(ses, node->prev->str3, node->next->str3) != 0;
- }
- break;
- case '!':
- if (node->str3[1] == '=' && node->str3[2] == '=')
- {
- value = tincmp(node->prev->str3, node->next->str3) != 0;
- }
- else
- {
- value = tineval(ses, node->prev->str3, node->next->str3) == 0;
- }
- break;
- default:
- show_debug(ses, LIST_VARIABLE, "#COMPUTE EXP: UNKNOWN OPERATOR: %c%c", node->str3[0], node->str3[1]);
- value = 0;
- break;
- }
- if (node->prev == mathnode_s)
- {
- mathnode_s = node;
- }
- if (node->next == mathnode_e)
- {
- mathnode_e = node;
- }
- del_math_node(node->next);
- del_math_node(node->prev);
- sprintf(temp, "%d", EXP_PR_VAR);
- free(node->str2);
- node->str2 = strdup(temp);
- if (integer64)
- {
- sprintf(temp, "%llu", value64);
- }
- else
- {
- sprintf(temp, "%.*Lf", precision, value);
- }
- free(node->str3);
- node->str3 = strdup(temp);
- }
- /*
- Keep synced with is_number()
- */
- long double tintoi(char *str)
- {
- char *ptr = str;
- long double values[5] = {0, 0, 0, 0, 0}, m = 1;
- int i = 1, d = 0;
- if (*ptr == 0)
- {
- return 0;
- }
- switch (*ptr)
- {
- case '!':
- case '~':
- case '+':
- case '-':
- ptr++;
- break;
- }
- ptr = str + strlen(str);
- while (TRUE)
- {
- ptr--;
- switch (*ptr)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- values[i] += (*ptr - '0') * m;
- m *= 10;
- break;
- case '.':
- if (d)
- {
- return 0;
- }
- d = 1;
- values[0] = values[1] / m;
- values[1] = 0;
- m = 1;
- break;
- case ':':
- switch (i)
- {
- case 2:
- values[i] *= 60;
- break;
- case 3:
- values[i] *= 60 * 60;
- break;
- case 4:
- return 0;
- }
- i++;
- m = 1;
- break;
- case '!':
- case '~':
- case '+':
- case '-':
- if (ptr == str)
- {
- break;
- }
- return 0;
- default:
- return 0;
- }
- if (ptr == str)
- {
- switch (i)
- {
- case 2:
- values[i] *= 60;
- break;
- case 3:
- values[i] *= 60 * 60;
- break;
- case 4:
- values[i] *= 60 * 60 * 24;
- break;
- }
- break;
- }
- }
- switch (*str)
- {
- case '!':
- return !(values[0] + values[1] + values[2] + values[3] + values[4]);
- case '~':
- return ~ (long long) (values[0] + values[1] + values[2] + values[3] + values[4]);
- case '+':
- return +(values[0] + values[1] + values[2] + values[3] + values[4]);
- case '-':
- return -(values[0] + values[1] + values[2] + values[3] + values[4]);
- default:
- return (values[0] + values[1] + values[2] + values[3] + values[4]);
- }
- }
- unsigned long long tintou(char *str)
- {
- char *ptr = str;
- unsigned long long value = 0, m = 1;
- if (*ptr == 0)
- {
- return 0;
- }
- switch (*ptr)
- {
- case '!':
- case '~':
- case '+':
- case '-':
- ptr++;
- break;
- }
- ptr = str + strlen(str);
- while (ptr != str)
- {
- ptr--;
- switch (*ptr)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- value += (*ptr - '0') * m;
- m *= 10;
- break;
- case '.':
- *ptr = 0;
- break;
- case ':':
- return 0;
- break;
- case '!':
- case '~':
- case '+':
- case '-':
- if (ptr == str)
- {
- break;
- }
- return 0;
- default:
- return 0;
- }
- }
- switch (str[0])
- {
- case '!':
- return !value;
- case '~':
- return ~value;
- case '+':
- return +value;
- case '-':
- return -value;
- default:
- return value;
- }
- }
- /*
- Keep synched with tintoi() and is_math()
- */
- int is_number(char *str)
- {
- char *ptr = str;
- int i = 1, d = 0, valid = 0;
- if (*ptr == 0)
- {
- return FALSE;
- }
- ptr = str + strlen(str);
- while (TRUE)
- {
- ptr--;
- switch (*ptr)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- valid = TRUE;
- break;
- case '.':
- if (d)
- {
- return FALSE;
- }
- d = 1;
- valid = FALSE;
- break;
- case ':':
- if (i == 4)
- {
- return FALSE;
- }
- i++;
- valid = FALSE;
- break;
- case '!':
- case '~':
- case '+':
- case '-':
- if (ptr != str)
- {
- return FALSE;
- }
- break;
- default:
- return FALSE;
- }
- if (ptr == str)
- {
- break;
- }
- }
- return valid;
- }
- long double tincmp(char *left, char *right)
- {
- long double left_val, right_val;
- switch (left[0])
- {
- case '{':
- case '"':
- return strcmp(left, right);
- default:
- left_val = tintoi(left);
- right_val = tintoi(right);
- return left_val - right_val;
- }
- }
- long double tineval(struct session *ses, char *left, char *right)
- {
- long double left_val, right_val;
- switch (left[0])
- {
- case '{':
- get_arg_in_braces(ses, left, left, GET_ALL);
- get_arg_in_braces(ses, right, right, GET_ALL);
- return match(ses, left, right, SUB_NONE);
- case '"':
- return match(ses, left, right, SUB_NONE);
- default:
- left_val = tintoi(left);
- right_val = tintoi(right);
- return left_val == right_val;
- }
- }
- long double tindice(struct session *ses, char *left, char *right)
- {
- unsigned long long cnt, numdice, sizedice, sum;
- long double estimate;
- numdice = (unsigned long long) tintoi(left);
- sizedice = (unsigned long long) tintoi(right);
- if (sizedice == 0)
- {
- return 0;
- }
- if (numdice > 100)
- {
- estimate = numdice / 100.0;
- numdice = 100;
- }
- else
- {
- estimate = 1;
- }
- for (cnt = sum = 0 ; cnt < numdice ; cnt++)
- {
- sum += generate_rand(ses) % sizedice + 1;
- }
- sum *= estimate;
- return (long double) sum;
- }
|