expression.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /**************************************************************************/
  2. /* expression.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "expression.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/ref_counted.h"
  35. #include "core/os/os.h"
  36. #include "core/variant/variant_parser.h"
  37. Error Expression::_get_token(Token &r_token) {
  38. while (true) {
  39. #define GET_CHAR() (str_ofs >= expression.length() ? 0 : expression[str_ofs++])
  40. char32_t cchar = GET_CHAR();
  41. switch (cchar) {
  42. case 0: {
  43. r_token.type = TK_EOF;
  44. return OK;
  45. }
  46. case '{': {
  47. r_token.type = TK_CURLY_BRACKET_OPEN;
  48. return OK;
  49. }
  50. case '}': {
  51. r_token.type = TK_CURLY_BRACKET_CLOSE;
  52. return OK;
  53. }
  54. case '[': {
  55. r_token.type = TK_BRACKET_OPEN;
  56. return OK;
  57. }
  58. case ']': {
  59. r_token.type = TK_BRACKET_CLOSE;
  60. return OK;
  61. }
  62. case '(': {
  63. r_token.type = TK_PARENTHESIS_OPEN;
  64. return OK;
  65. }
  66. case ')': {
  67. r_token.type = TK_PARENTHESIS_CLOSE;
  68. return OK;
  69. }
  70. case ',': {
  71. r_token.type = TK_COMMA;
  72. return OK;
  73. }
  74. case ':': {
  75. r_token.type = TK_COLON;
  76. return OK;
  77. }
  78. case '$': {
  79. r_token.type = TK_INPUT;
  80. int index = 0;
  81. do {
  82. if (!is_digit(expression[str_ofs])) {
  83. _set_error("Expected number after '$'");
  84. r_token.type = TK_ERROR;
  85. return ERR_PARSE_ERROR;
  86. }
  87. index *= 10;
  88. index += expression[str_ofs] - '0';
  89. str_ofs++;
  90. } while (is_digit(expression[str_ofs]));
  91. r_token.value = index;
  92. return OK;
  93. }
  94. case '=': {
  95. cchar = GET_CHAR();
  96. if (cchar == '=') {
  97. r_token.type = TK_OP_EQUAL;
  98. } else {
  99. _set_error("Expected '='");
  100. r_token.type = TK_ERROR;
  101. return ERR_PARSE_ERROR;
  102. }
  103. return OK;
  104. }
  105. case '!': {
  106. if (expression[str_ofs] == '=') {
  107. r_token.type = TK_OP_NOT_EQUAL;
  108. str_ofs++;
  109. } else {
  110. r_token.type = TK_OP_NOT;
  111. }
  112. return OK;
  113. }
  114. case '>': {
  115. if (expression[str_ofs] == '=') {
  116. r_token.type = TK_OP_GREATER_EQUAL;
  117. str_ofs++;
  118. } else if (expression[str_ofs] == '>') {
  119. r_token.type = TK_OP_SHIFT_RIGHT;
  120. str_ofs++;
  121. } else {
  122. r_token.type = TK_OP_GREATER;
  123. }
  124. return OK;
  125. }
  126. case '<': {
  127. if (expression[str_ofs] == '=') {
  128. r_token.type = TK_OP_LESS_EQUAL;
  129. str_ofs++;
  130. } else if (expression[str_ofs] == '<') {
  131. r_token.type = TK_OP_SHIFT_LEFT;
  132. str_ofs++;
  133. } else {
  134. r_token.type = TK_OP_LESS;
  135. }
  136. return OK;
  137. }
  138. case '+': {
  139. r_token.type = TK_OP_ADD;
  140. return OK;
  141. }
  142. case '-': {
  143. r_token.type = TK_OP_SUB;
  144. return OK;
  145. }
  146. case '/': {
  147. r_token.type = TK_OP_DIV;
  148. return OK;
  149. }
  150. case '*': {
  151. if (expression[str_ofs] == '*') {
  152. r_token.type = TK_OP_POW;
  153. str_ofs++;
  154. } else {
  155. r_token.type = TK_OP_MUL;
  156. }
  157. return OK;
  158. }
  159. case '%': {
  160. r_token.type = TK_OP_MOD;
  161. return OK;
  162. }
  163. case '&': {
  164. if (expression[str_ofs] == '&') {
  165. r_token.type = TK_OP_AND;
  166. str_ofs++;
  167. } else {
  168. r_token.type = TK_OP_BIT_AND;
  169. }
  170. return OK;
  171. }
  172. case '|': {
  173. if (expression[str_ofs] == '|') {
  174. r_token.type = TK_OP_OR;
  175. str_ofs++;
  176. } else {
  177. r_token.type = TK_OP_BIT_OR;
  178. }
  179. return OK;
  180. }
  181. case '^': {
  182. r_token.type = TK_OP_BIT_XOR;
  183. return OK;
  184. }
  185. case '~': {
  186. r_token.type = TK_OP_BIT_INVERT;
  187. return OK;
  188. }
  189. case '\'':
  190. case '"': {
  191. String str;
  192. char32_t prev = 0;
  193. while (true) {
  194. char32_t ch = GET_CHAR();
  195. if (ch == 0) {
  196. _set_error("Unterminated String");
  197. r_token.type = TK_ERROR;
  198. return ERR_PARSE_ERROR;
  199. } else if (ch == cchar) {
  200. // cchar contain a corresponding quote symbol
  201. break;
  202. } else if (ch == '\\') {
  203. //escaped characters...
  204. char32_t next = GET_CHAR();
  205. if (next == 0) {
  206. _set_error("Unterminated String");
  207. r_token.type = TK_ERROR;
  208. return ERR_PARSE_ERROR;
  209. }
  210. char32_t res = 0;
  211. switch (next) {
  212. case 'b':
  213. res = 8;
  214. break;
  215. case 't':
  216. res = 9;
  217. break;
  218. case 'n':
  219. res = 10;
  220. break;
  221. case 'f':
  222. res = 12;
  223. break;
  224. case 'r':
  225. res = 13;
  226. break;
  227. case 'U':
  228. case 'u': {
  229. // Hexadecimal sequence.
  230. int hex_len = (next == 'U') ? 6 : 4;
  231. for (int j = 0; j < hex_len; j++) {
  232. char32_t c = GET_CHAR();
  233. if (c == 0) {
  234. _set_error("Unterminated String");
  235. r_token.type = TK_ERROR;
  236. return ERR_PARSE_ERROR;
  237. }
  238. if (!is_hex_digit(c)) {
  239. _set_error("Malformed hex constant in string");
  240. r_token.type = TK_ERROR;
  241. return ERR_PARSE_ERROR;
  242. }
  243. char32_t v;
  244. if (is_digit(c)) {
  245. v = c - '0';
  246. } else if (c >= 'a' && c <= 'f') {
  247. v = c - 'a';
  248. v += 10;
  249. } else if (c >= 'A' && c <= 'F') {
  250. v = c - 'A';
  251. v += 10;
  252. } else {
  253. ERR_PRINT("Bug parsing hex constant.");
  254. v = 0;
  255. }
  256. res <<= 4;
  257. res |= v;
  258. }
  259. } break;
  260. default: {
  261. res = next;
  262. } break;
  263. }
  264. // Parse UTF-16 pair.
  265. if ((res & 0xfffffc00) == 0xd800) {
  266. if (prev == 0) {
  267. prev = res;
  268. continue;
  269. } else {
  270. _set_error("Invalid UTF-16 sequence in string, unpaired lead surrogate");
  271. r_token.type = TK_ERROR;
  272. return ERR_PARSE_ERROR;
  273. }
  274. } else if ((res & 0xfffffc00) == 0xdc00) {
  275. if (prev == 0) {
  276. _set_error("Invalid UTF-16 sequence in string, unpaired trail surrogate");
  277. r_token.type = TK_ERROR;
  278. return ERR_PARSE_ERROR;
  279. } else {
  280. res = (prev << 10UL) + res - ((0xd800 << 10UL) + 0xdc00 - 0x10000);
  281. prev = 0;
  282. }
  283. }
  284. if (prev != 0) {
  285. _set_error("Invalid UTF-16 sequence in string, unpaired lead surrogate");
  286. r_token.type = TK_ERROR;
  287. return ERR_PARSE_ERROR;
  288. }
  289. str += res;
  290. } else {
  291. if (prev != 0) {
  292. _set_error("Invalid UTF-16 sequence in string, unpaired lead surrogate");
  293. r_token.type = TK_ERROR;
  294. return ERR_PARSE_ERROR;
  295. }
  296. str += ch;
  297. }
  298. }
  299. if (prev != 0) {
  300. _set_error("Invalid UTF-16 sequence in string, unpaired lead surrogate");
  301. r_token.type = TK_ERROR;
  302. return ERR_PARSE_ERROR;
  303. }
  304. r_token.type = TK_CONSTANT;
  305. r_token.value = str;
  306. return OK;
  307. } break;
  308. default: {
  309. if (cchar <= 32) {
  310. break;
  311. }
  312. char32_t next_char = (str_ofs >= expression.length()) ? 0 : expression[str_ofs];
  313. if (is_digit(cchar) || (cchar == '.' && is_digit(next_char))) {
  314. //a number
  315. String num;
  316. #define READING_SIGN 0
  317. #define READING_INT 1
  318. #define READING_HEX 2
  319. #define READING_BIN 3
  320. #define READING_DEC 4
  321. #define READING_EXP 5
  322. #define READING_DONE 6
  323. int reading = READING_INT;
  324. char32_t c = cchar;
  325. bool exp_sign = false;
  326. bool exp_beg = false;
  327. bool bin_beg = false;
  328. bool hex_beg = false;
  329. bool is_float = false;
  330. bool is_first_char = true;
  331. while (true) {
  332. switch (reading) {
  333. case READING_INT: {
  334. if (is_digit(c)) {
  335. if (is_first_char && c == '0') {
  336. if (next_char == 'b') {
  337. reading = READING_BIN;
  338. } else if (next_char == 'x') {
  339. reading = READING_HEX;
  340. }
  341. }
  342. } else if (c == '.') {
  343. reading = READING_DEC;
  344. is_float = true;
  345. } else if (c == 'e') {
  346. reading = READING_EXP;
  347. is_float = true;
  348. } else {
  349. reading = READING_DONE;
  350. }
  351. } break;
  352. case READING_BIN: {
  353. if (bin_beg && !is_binary_digit(c)) {
  354. reading = READING_DONE;
  355. } else if (c == 'b') {
  356. bin_beg = true;
  357. }
  358. } break;
  359. case READING_HEX: {
  360. if (hex_beg && !is_hex_digit(c)) {
  361. reading = READING_DONE;
  362. } else if (c == 'x') {
  363. hex_beg = true;
  364. }
  365. } break;
  366. case READING_DEC: {
  367. if (is_digit(c)) {
  368. } else if (c == 'e') {
  369. reading = READING_EXP;
  370. } else {
  371. reading = READING_DONE;
  372. }
  373. } break;
  374. case READING_EXP: {
  375. if (is_digit(c)) {
  376. exp_beg = true;
  377. } else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) {
  378. exp_sign = true;
  379. } else {
  380. reading = READING_DONE;
  381. }
  382. } break;
  383. }
  384. if (reading == READING_DONE) {
  385. break;
  386. }
  387. num += String::chr(c);
  388. c = GET_CHAR();
  389. is_first_char = false;
  390. }
  391. str_ofs--;
  392. r_token.type = TK_CONSTANT;
  393. if (is_float) {
  394. r_token.value = num.to_float();
  395. } else if (bin_beg) {
  396. r_token.value = num.bin_to_int();
  397. } else if (hex_beg) {
  398. r_token.value = num.hex_to_int();
  399. } else {
  400. r_token.value = num.to_int();
  401. }
  402. return OK;
  403. } else if (is_unicode_identifier_start(cchar)) {
  404. String id = String::chr(cchar);
  405. cchar = GET_CHAR();
  406. while (is_unicode_identifier_continue(cchar)) {
  407. id += String::chr(cchar);
  408. cchar = GET_CHAR();
  409. }
  410. str_ofs--; //go back one
  411. if (id == "in") {
  412. r_token.type = TK_OP_IN;
  413. } else if (id == "null") {
  414. r_token.type = TK_CONSTANT;
  415. r_token.value = Variant();
  416. } else if (id == "true") {
  417. r_token.type = TK_CONSTANT;
  418. r_token.value = true;
  419. } else if (id == "false") {
  420. r_token.type = TK_CONSTANT;
  421. r_token.value = false;
  422. } else if (id == "PI") {
  423. r_token.type = TK_CONSTANT;
  424. r_token.value = Math_PI;
  425. } else if (id == "TAU") {
  426. r_token.type = TK_CONSTANT;
  427. r_token.value = Math_TAU;
  428. } else if (id == "INF") {
  429. r_token.type = TK_CONSTANT;
  430. r_token.value = INFINITY;
  431. } else if (id == "NAN") {
  432. r_token.type = TK_CONSTANT;
  433. r_token.value = NAN;
  434. } else if (id == "not") {
  435. r_token.type = TK_OP_NOT;
  436. } else if (id == "or") {
  437. r_token.type = TK_OP_OR;
  438. } else if (id == "and") {
  439. r_token.type = TK_OP_AND;
  440. } else if (id == "self") {
  441. r_token.type = TK_SELF;
  442. } else {
  443. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  444. if (id == Variant::get_type_name(Variant::Type(i))) {
  445. r_token.type = TK_BASIC_TYPE;
  446. r_token.value = i;
  447. return OK;
  448. }
  449. }
  450. if (Variant::has_utility_function(id)) {
  451. r_token.type = TK_BUILTIN_FUNC;
  452. r_token.value = id;
  453. return OK;
  454. }
  455. r_token.type = TK_IDENTIFIER;
  456. r_token.value = id;
  457. }
  458. return OK;
  459. } else if (cchar == '.') {
  460. // Handled down there as we support '.[0-9]' as numbers above
  461. r_token.type = TK_PERIOD;
  462. return OK;
  463. } else {
  464. _set_error("Unexpected character.");
  465. r_token.type = TK_ERROR;
  466. return ERR_PARSE_ERROR;
  467. }
  468. }
  469. }
  470. #undef GET_CHAR
  471. }
  472. r_token.type = TK_ERROR;
  473. return ERR_PARSE_ERROR;
  474. }
  475. const char *Expression::token_name[TK_MAX] = {
  476. "CURLY BRACKET OPEN",
  477. "CURLY BRACKET CLOSE",
  478. "BRACKET OPEN",
  479. "BRACKET CLOSE",
  480. "PARENTHESIS OPEN",
  481. "PARENTHESIS CLOSE",
  482. "IDENTIFIER",
  483. "BUILTIN FUNC",
  484. "SELF",
  485. "CONSTANT",
  486. "BASIC TYPE",
  487. "COLON",
  488. "COMMA",
  489. "PERIOD",
  490. "OP IN",
  491. "OP EQUAL",
  492. "OP NOT EQUAL",
  493. "OP LESS",
  494. "OP LESS EQUAL",
  495. "OP GREATER",
  496. "OP GREATER EQUAL",
  497. "OP AND",
  498. "OP OR",
  499. "OP NOT",
  500. "OP ADD",
  501. "OP SUB",
  502. "OP MUL",
  503. "OP DIV",
  504. "OP MOD",
  505. "OP POW",
  506. "OP SHIFT LEFT",
  507. "OP SHIFT RIGHT",
  508. "OP BIT AND",
  509. "OP BIT OR",
  510. "OP BIT XOR",
  511. "OP BIT INVERT",
  512. "OP INPUT",
  513. "EOF",
  514. "ERROR"
  515. };
  516. Expression::ENode *Expression::_parse_expression() {
  517. Vector<ExpressionNode> expression_nodes;
  518. while (true) {
  519. //keep appending stuff to expression
  520. ENode *expr = nullptr;
  521. Token tk;
  522. _get_token(tk);
  523. if (error_set) {
  524. return nullptr;
  525. }
  526. switch (tk.type) {
  527. case TK_CURLY_BRACKET_OPEN: {
  528. //a dictionary
  529. DictionaryNode *dn = alloc_node<DictionaryNode>();
  530. while (true) {
  531. int cofs = str_ofs;
  532. _get_token(tk);
  533. if (tk.type == TK_CURLY_BRACKET_CLOSE) {
  534. break;
  535. }
  536. str_ofs = cofs; //revert
  537. //parse an expression
  538. ENode *subexpr = _parse_expression();
  539. if (!subexpr) {
  540. return nullptr;
  541. }
  542. dn->dict.push_back(subexpr);
  543. _get_token(tk);
  544. if (tk.type != TK_COLON) {
  545. _set_error("Expected ':'");
  546. return nullptr;
  547. }
  548. subexpr = _parse_expression();
  549. if (!subexpr) {
  550. return nullptr;
  551. }
  552. dn->dict.push_back(subexpr);
  553. cofs = str_ofs;
  554. _get_token(tk);
  555. if (tk.type == TK_COMMA) {
  556. //all good
  557. } else if (tk.type == TK_CURLY_BRACKET_CLOSE) {
  558. str_ofs = cofs;
  559. } else {
  560. _set_error("Expected ',' or '}'");
  561. }
  562. }
  563. expr = dn;
  564. } break;
  565. case TK_BRACKET_OPEN: {
  566. //an array
  567. ArrayNode *an = alloc_node<ArrayNode>();
  568. while (true) {
  569. int cofs = str_ofs;
  570. _get_token(tk);
  571. if (tk.type == TK_BRACKET_CLOSE) {
  572. break;
  573. }
  574. str_ofs = cofs; //revert
  575. //parse an expression
  576. ENode *subexpr = _parse_expression();
  577. if (!subexpr) {
  578. return nullptr;
  579. }
  580. an->array.push_back(subexpr);
  581. cofs = str_ofs;
  582. _get_token(tk);
  583. if (tk.type == TK_COMMA) {
  584. //all good
  585. } else if (tk.type == TK_BRACKET_CLOSE) {
  586. str_ofs = cofs;
  587. } else {
  588. _set_error("Expected ',' or ']'");
  589. }
  590. }
  591. expr = an;
  592. } break;
  593. case TK_PARENTHESIS_OPEN: {
  594. //a suexpression
  595. ENode *e = _parse_expression();
  596. if (error_set) {
  597. return nullptr;
  598. }
  599. _get_token(tk);
  600. if (tk.type != TK_PARENTHESIS_CLOSE) {
  601. _set_error("Expected ')'");
  602. return nullptr;
  603. }
  604. expr = e;
  605. } break;
  606. case TK_IDENTIFIER: {
  607. String identifier = tk.value;
  608. int cofs = str_ofs;
  609. _get_token(tk);
  610. if (tk.type == TK_PARENTHESIS_OPEN) {
  611. //function call
  612. CallNode *func_call = alloc_node<CallNode>();
  613. func_call->method = identifier;
  614. SelfNode *self_node = alloc_node<SelfNode>();
  615. func_call->base = self_node;
  616. while (true) {
  617. int cofs2 = str_ofs;
  618. _get_token(tk);
  619. if (tk.type == TK_PARENTHESIS_CLOSE) {
  620. break;
  621. }
  622. str_ofs = cofs2; //revert
  623. //parse an expression
  624. ENode *subexpr = _parse_expression();
  625. if (!subexpr) {
  626. return nullptr;
  627. }
  628. func_call->arguments.push_back(subexpr);
  629. cofs2 = str_ofs;
  630. _get_token(tk);
  631. if (tk.type == TK_COMMA) {
  632. //all good
  633. } else if (tk.type == TK_PARENTHESIS_CLOSE) {
  634. str_ofs = cofs2;
  635. } else {
  636. _set_error("Expected ',' or ')'");
  637. }
  638. }
  639. expr = func_call;
  640. } else {
  641. //named indexing
  642. str_ofs = cofs;
  643. int input_index = -1;
  644. for (int i = 0; i < input_names.size(); i++) {
  645. if (input_names[i] == identifier) {
  646. input_index = i;
  647. break;
  648. }
  649. }
  650. if (input_index != -1) {
  651. InputNode *input = alloc_node<InputNode>();
  652. input->index = input_index;
  653. expr = input;
  654. } else {
  655. NamedIndexNode *index = alloc_node<NamedIndexNode>();
  656. SelfNode *self_node = alloc_node<SelfNode>();
  657. index->base = self_node;
  658. index->name = identifier;
  659. expr = index;
  660. }
  661. }
  662. } break;
  663. case TK_INPUT: {
  664. InputNode *input = alloc_node<InputNode>();
  665. input->index = tk.value;
  666. expr = input;
  667. } break;
  668. case TK_SELF: {
  669. SelfNode *self = alloc_node<SelfNode>();
  670. expr = self;
  671. } break;
  672. case TK_CONSTANT: {
  673. ConstantNode *constant = alloc_node<ConstantNode>();
  674. constant->value = tk.value;
  675. expr = constant;
  676. } break;
  677. case TK_BASIC_TYPE: {
  678. //constructor..
  679. Variant::Type bt = Variant::Type(int(tk.value));
  680. _get_token(tk);
  681. if (tk.type != TK_PARENTHESIS_OPEN) {
  682. _set_error("Expected '('");
  683. return nullptr;
  684. }
  685. ConstructorNode *constructor = alloc_node<ConstructorNode>();
  686. constructor->data_type = bt;
  687. while (true) {
  688. int cofs = str_ofs;
  689. _get_token(tk);
  690. if (tk.type == TK_PARENTHESIS_CLOSE) {
  691. break;
  692. }
  693. str_ofs = cofs; //revert
  694. //parse an expression
  695. ENode *subexpr = _parse_expression();
  696. if (!subexpr) {
  697. return nullptr;
  698. }
  699. constructor->arguments.push_back(subexpr);
  700. cofs = str_ofs;
  701. _get_token(tk);
  702. if (tk.type == TK_COMMA) {
  703. //all good
  704. } else if (tk.type == TK_PARENTHESIS_CLOSE) {
  705. str_ofs = cofs;
  706. } else {
  707. _set_error("Expected ',' or ')'");
  708. }
  709. }
  710. expr = constructor;
  711. } break;
  712. case TK_BUILTIN_FUNC: {
  713. //builtin function
  714. StringName func = tk.value;
  715. _get_token(tk);
  716. if (tk.type != TK_PARENTHESIS_OPEN) {
  717. _set_error("Expected '('");
  718. return nullptr;
  719. }
  720. BuiltinFuncNode *bifunc = alloc_node<BuiltinFuncNode>();
  721. bifunc->func = func;
  722. while (true) {
  723. int cofs = str_ofs;
  724. _get_token(tk);
  725. if (tk.type == TK_PARENTHESIS_CLOSE) {
  726. break;
  727. }
  728. str_ofs = cofs; //revert
  729. //parse an expression
  730. ENode *subexpr = _parse_expression();
  731. if (!subexpr) {
  732. return nullptr;
  733. }
  734. bifunc->arguments.push_back(subexpr);
  735. cofs = str_ofs;
  736. _get_token(tk);
  737. if (tk.type == TK_COMMA) {
  738. //all good
  739. } else if (tk.type == TK_PARENTHESIS_CLOSE) {
  740. str_ofs = cofs;
  741. } else {
  742. _set_error("Expected ',' or ')'");
  743. }
  744. }
  745. if (!Variant::is_utility_function_vararg(bifunc->func)) {
  746. int expected_args = Variant::get_utility_function_argument_count(bifunc->func);
  747. if (expected_args != bifunc->arguments.size()) {
  748. _set_error("Builtin func '" + String(bifunc->func) + "' expects " + itos(expected_args) + " arguments.");
  749. }
  750. }
  751. expr = bifunc;
  752. } break;
  753. case TK_OP_SUB: {
  754. ExpressionNode e;
  755. e.is_op = true;
  756. e.op = Variant::OP_NEGATE;
  757. expression_nodes.push_back(e);
  758. continue;
  759. } break;
  760. case TK_OP_NOT: {
  761. ExpressionNode e;
  762. e.is_op = true;
  763. e.op = Variant::OP_NOT;
  764. expression_nodes.push_back(e);
  765. continue;
  766. } break;
  767. default: {
  768. _set_error("Expected expression.");
  769. return nullptr;
  770. } break;
  771. }
  772. //before going to operators, must check indexing!
  773. while (true) {
  774. int cofs2 = str_ofs;
  775. _get_token(tk);
  776. if (error_set) {
  777. return nullptr;
  778. }
  779. bool done = false;
  780. switch (tk.type) {
  781. case TK_BRACKET_OPEN: {
  782. //value indexing
  783. IndexNode *index = alloc_node<IndexNode>();
  784. index->base = expr;
  785. ENode *what = _parse_expression();
  786. if (!what) {
  787. return nullptr;
  788. }
  789. index->index = what;
  790. _get_token(tk);
  791. if (tk.type != TK_BRACKET_CLOSE) {
  792. _set_error("Expected ']' at end of index.");
  793. return nullptr;
  794. }
  795. expr = index;
  796. } break;
  797. case TK_PERIOD: {
  798. //named indexing or function call
  799. _get_token(tk);
  800. if (tk.type != TK_IDENTIFIER && tk.type != TK_BUILTIN_FUNC) {
  801. _set_error("Expected identifier after '.'");
  802. return nullptr;
  803. }
  804. StringName identifier = tk.value;
  805. int cofs = str_ofs;
  806. _get_token(tk);
  807. if (tk.type == TK_PARENTHESIS_OPEN) {
  808. //function call
  809. CallNode *func_call = alloc_node<CallNode>();
  810. func_call->method = identifier;
  811. func_call->base = expr;
  812. while (true) {
  813. int cofs3 = str_ofs;
  814. _get_token(tk);
  815. if (tk.type == TK_PARENTHESIS_CLOSE) {
  816. break;
  817. }
  818. str_ofs = cofs3; //revert
  819. //parse an expression
  820. ENode *subexpr = _parse_expression();
  821. if (!subexpr) {
  822. return nullptr;
  823. }
  824. func_call->arguments.push_back(subexpr);
  825. cofs3 = str_ofs;
  826. _get_token(tk);
  827. if (tk.type == TK_COMMA) {
  828. //all good
  829. } else if (tk.type == TK_PARENTHESIS_CLOSE) {
  830. str_ofs = cofs3;
  831. } else {
  832. _set_error("Expected ',' or ')'");
  833. }
  834. }
  835. expr = func_call;
  836. } else {
  837. //named indexing
  838. str_ofs = cofs;
  839. NamedIndexNode *index = alloc_node<NamedIndexNode>();
  840. index->base = expr;
  841. index->name = identifier;
  842. expr = index;
  843. }
  844. } break;
  845. default: {
  846. str_ofs = cofs2;
  847. done = true;
  848. } break;
  849. }
  850. if (done) {
  851. break;
  852. }
  853. }
  854. //push expression
  855. {
  856. ExpressionNode e;
  857. e.is_op = false;
  858. e.node = expr;
  859. expression_nodes.push_back(e);
  860. }
  861. //ok finally look for an operator
  862. int cofs = str_ofs;
  863. _get_token(tk);
  864. if (error_set) {
  865. return nullptr;
  866. }
  867. Variant::Operator op = Variant::OP_MAX;
  868. switch (tk.type) {
  869. case TK_OP_IN:
  870. op = Variant::OP_IN;
  871. break;
  872. case TK_OP_EQUAL:
  873. op = Variant::OP_EQUAL;
  874. break;
  875. case TK_OP_NOT_EQUAL:
  876. op = Variant::OP_NOT_EQUAL;
  877. break;
  878. case TK_OP_LESS:
  879. op = Variant::OP_LESS;
  880. break;
  881. case TK_OP_LESS_EQUAL:
  882. op = Variant::OP_LESS_EQUAL;
  883. break;
  884. case TK_OP_GREATER:
  885. op = Variant::OP_GREATER;
  886. break;
  887. case TK_OP_GREATER_EQUAL:
  888. op = Variant::OP_GREATER_EQUAL;
  889. break;
  890. case TK_OP_AND:
  891. op = Variant::OP_AND;
  892. break;
  893. case TK_OP_OR:
  894. op = Variant::OP_OR;
  895. break;
  896. case TK_OP_NOT:
  897. op = Variant::OP_NOT;
  898. break;
  899. case TK_OP_ADD:
  900. op = Variant::OP_ADD;
  901. break;
  902. case TK_OP_SUB:
  903. op = Variant::OP_SUBTRACT;
  904. break;
  905. case TK_OP_MUL:
  906. op = Variant::OP_MULTIPLY;
  907. break;
  908. case TK_OP_DIV:
  909. op = Variant::OP_DIVIDE;
  910. break;
  911. case TK_OP_MOD:
  912. op = Variant::OP_MODULE;
  913. break;
  914. case TK_OP_POW:
  915. op = Variant::OP_POWER;
  916. break;
  917. case TK_OP_SHIFT_LEFT:
  918. op = Variant::OP_SHIFT_LEFT;
  919. break;
  920. case TK_OP_SHIFT_RIGHT:
  921. op = Variant::OP_SHIFT_RIGHT;
  922. break;
  923. case TK_OP_BIT_AND:
  924. op = Variant::OP_BIT_AND;
  925. break;
  926. case TK_OP_BIT_OR:
  927. op = Variant::OP_BIT_OR;
  928. break;
  929. case TK_OP_BIT_XOR:
  930. op = Variant::OP_BIT_XOR;
  931. break;
  932. case TK_OP_BIT_INVERT:
  933. op = Variant::OP_BIT_NEGATE;
  934. break;
  935. default: {
  936. }
  937. }
  938. if (op == Variant::OP_MAX) { //stop appending stuff
  939. str_ofs = cofs;
  940. break;
  941. }
  942. //push operator and go on
  943. {
  944. ExpressionNode e;
  945. e.is_op = true;
  946. e.op = op;
  947. expression_nodes.push_back(e);
  948. }
  949. }
  950. /* Reduce the set of expressions and place them in an operator tree, respecting precedence */
  951. while (expression_nodes.size() > 1) {
  952. int next_op = -1;
  953. int min_priority = 0xFFFFF;
  954. bool is_unary = false;
  955. for (int i = 0; i < expression_nodes.size(); i++) {
  956. if (!expression_nodes[i].is_op) {
  957. continue;
  958. }
  959. int priority;
  960. bool unary = false;
  961. switch (expression_nodes[i].op) {
  962. case Variant::OP_POWER:
  963. priority = 0;
  964. break;
  965. case Variant::OP_BIT_NEGATE:
  966. priority = 1;
  967. unary = true;
  968. break;
  969. case Variant::OP_NEGATE:
  970. priority = 2;
  971. unary = true;
  972. break;
  973. case Variant::OP_MULTIPLY:
  974. case Variant::OP_DIVIDE:
  975. case Variant::OP_MODULE:
  976. priority = 3;
  977. break;
  978. case Variant::OP_ADD:
  979. case Variant::OP_SUBTRACT:
  980. priority = 4;
  981. break;
  982. case Variant::OP_SHIFT_LEFT:
  983. case Variant::OP_SHIFT_RIGHT:
  984. priority = 5;
  985. break;
  986. case Variant::OP_BIT_AND:
  987. priority = 6;
  988. break;
  989. case Variant::OP_BIT_XOR:
  990. priority = 7;
  991. break;
  992. case Variant::OP_BIT_OR:
  993. priority = 8;
  994. break;
  995. case Variant::OP_LESS:
  996. case Variant::OP_LESS_EQUAL:
  997. case Variant::OP_GREATER:
  998. case Variant::OP_GREATER_EQUAL:
  999. case Variant::OP_EQUAL:
  1000. case Variant::OP_NOT_EQUAL:
  1001. priority = 9;
  1002. break;
  1003. case Variant::OP_IN:
  1004. priority = 11;
  1005. break;
  1006. case Variant::OP_NOT:
  1007. priority = 12;
  1008. unary = true;
  1009. break;
  1010. case Variant::OP_AND:
  1011. priority = 13;
  1012. break;
  1013. case Variant::OP_OR:
  1014. priority = 14;
  1015. break;
  1016. default: {
  1017. _set_error("Parser bug, invalid operator in expression: " + itos(expression_nodes[i].op));
  1018. return nullptr;
  1019. }
  1020. }
  1021. if (priority < min_priority) {
  1022. // < is used for left to right (default)
  1023. // <= is used for right to left
  1024. next_op = i;
  1025. min_priority = priority;
  1026. is_unary = unary;
  1027. }
  1028. }
  1029. if (next_op == -1) {
  1030. _set_error("Yet another parser bug....");
  1031. ERR_FAIL_V(nullptr);
  1032. }
  1033. // OK! create operator..
  1034. if (is_unary) {
  1035. int expr_pos = next_op;
  1036. while (expression_nodes[expr_pos].is_op) {
  1037. expr_pos++;
  1038. if (expr_pos == expression_nodes.size()) {
  1039. //can happen..
  1040. _set_error("Unexpected end of expression...");
  1041. return nullptr;
  1042. }
  1043. }
  1044. //consecutively do unary operators
  1045. for (int i = expr_pos - 1; i >= next_op; i--) {
  1046. OperatorNode *op = alloc_node<OperatorNode>();
  1047. op->op = expression_nodes[i].op;
  1048. op->nodes[0] = expression_nodes[i + 1].node;
  1049. op->nodes[1] = nullptr;
  1050. expression_nodes.write[i].is_op = false;
  1051. expression_nodes.write[i].node = op;
  1052. expression_nodes.remove_at(i + 1);
  1053. }
  1054. } else {
  1055. if (next_op < 1 || next_op >= (expression_nodes.size() - 1)) {
  1056. _set_error("Parser bug...");
  1057. ERR_FAIL_V(nullptr);
  1058. }
  1059. OperatorNode *op = alloc_node<OperatorNode>();
  1060. op->op = expression_nodes[next_op].op;
  1061. if (expression_nodes[next_op - 1].is_op) {
  1062. _set_error("Parser bug...");
  1063. ERR_FAIL_V(nullptr);
  1064. }
  1065. if (expression_nodes[next_op + 1].is_op) {
  1066. // this is not invalid and can really appear
  1067. // but it becomes invalid anyway because no binary op
  1068. // can be followed by a unary op in a valid combination,
  1069. // due to how precedence works, unaries will always disappear first
  1070. _set_error("Unexpected two consecutive operators.");
  1071. return nullptr;
  1072. }
  1073. op->nodes[0] = expression_nodes[next_op - 1].node; //expression goes as left
  1074. op->nodes[1] = expression_nodes[next_op + 1].node; //next expression goes as right
  1075. //replace all 3 nodes by this operator and make it an expression
  1076. expression_nodes.write[next_op - 1].node = op;
  1077. expression_nodes.remove_at(next_op);
  1078. expression_nodes.remove_at(next_op);
  1079. }
  1080. }
  1081. return expression_nodes[0].node;
  1082. }
  1083. bool Expression::_compile_expression() {
  1084. if (!expression_dirty) {
  1085. return error_set;
  1086. }
  1087. if (nodes) {
  1088. memdelete(nodes);
  1089. nodes = nullptr;
  1090. root = nullptr;
  1091. }
  1092. error_str = String();
  1093. error_set = false;
  1094. str_ofs = 0;
  1095. root = _parse_expression();
  1096. if (error_set) {
  1097. root = nullptr;
  1098. if (nodes) {
  1099. memdelete(nodes);
  1100. }
  1101. nodes = nullptr;
  1102. return true;
  1103. }
  1104. expression_dirty = false;
  1105. return false;
  1106. }
  1107. bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression::ENode *p_node, Variant &r_ret, bool p_const_calls_only, String &r_error_str) {
  1108. switch (p_node->type) {
  1109. case Expression::ENode::TYPE_INPUT: {
  1110. const Expression::InputNode *in = static_cast<const Expression::InputNode *>(p_node);
  1111. if (in->index < 0 || in->index >= p_inputs.size()) {
  1112. r_error_str = vformat(RTR("Invalid input %d (not passed) in expression"), in->index);
  1113. return true;
  1114. }
  1115. r_ret = p_inputs[in->index];
  1116. } break;
  1117. case Expression::ENode::TYPE_CONSTANT: {
  1118. const Expression::ConstantNode *c = static_cast<const Expression::ConstantNode *>(p_node);
  1119. r_ret = c->value;
  1120. } break;
  1121. case Expression::ENode::TYPE_SELF: {
  1122. if (!p_instance) {
  1123. r_error_str = RTR("self can't be used because instance is null (not passed)");
  1124. return true;
  1125. }
  1126. r_ret = p_instance;
  1127. } break;
  1128. case Expression::ENode::TYPE_OPERATOR: {
  1129. const Expression::OperatorNode *op = static_cast<const Expression::OperatorNode *>(p_node);
  1130. Variant a;
  1131. bool ret = _execute(p_inputs, p_instance, op->nodes[0], a, p_const_calls_only, r_error_str);
  1132. if (ret) {
  1133. return true;
  1134. }
  1135. Variant b;
  1136. if (op->nodes[1]) {
  1137. ret = _execute(p_inputs, p_instance, op->nodes[1], b, p_const_calls_only, r_error_str);
  1138. if (ret) {
  1139. return true;
  1140. }
  1141. }
  1142. bool valid = true;
  1143. Variant::evaluate(op->op, a, b, r_ret, valid);
  1144. if (!valid) {
  1145. r_error_str = vformat(RTR("Invalid operands to operator %s, %s and %s."), Variant::get_operator_name(op->op), Variant::get_type_name(a.get_type()), Variant::get_type_name(b.get_type()));
  1146. return true;
  1147. }
  1148. } break;
  1149. case Expression::ENode::TYPE_INDEX: {
  1150. const Expression::IndexNode *index = static_cast<const Expression::IndexNode *>(p_node);
  1151. Variant base;
  1152. bool ret = _execute(p_inputs, p_instance, index->base, base, p_const_calls_only, r_error_str);
  1153. if (ret) {
  1154. return true;
  1155. }
  1156. Variant idx;
  1157. ret = _execute(p_inputs, p_instance, index->index, idx, p_const_calls_only, r_error_str);
  1158. if (ret) {
  1159. return true;
  1160. }
  1161. bool valid;
  1162. r_ret = base.get(idx, &valid);
  1163. if (!valid) {
  1164. r_error_str = vformat(RTR("Invalid index of type %s for base type %s"), Variant::get_type_name(idx.get_type()), Variant::get_type_name(base.get_type()));
  1165. return true;
  1166. }
  1167. } break;
  1168. case Expression::ENode::TYPE_NAMED_INDEX: {
  1169. const Expression::NamedIndexNode *index = static_cast<const Expression::NamedIndexNode *>(p_node);
  1170. Variant base;
  1171. bool ret = _execute(p_inputs, p_instance, index->base, base, p_const_calls_only, r_error_str);
  1172. if (ret) {
  1173. return true;
  1174. }
  1175. bool valid;
  1176. r_ret = base.get_named(index->name, valid);
  1177. if (!valid) {
  1178. r_error_str = vformat(RTR("Invalid named index '%s' for base type %s"), String(index->name), Variant::get_type_name(base.get_type()));
  1179. return true;
  1180. }
  1181. } break;
  1182. case Expression::ENode::TYPE_ARRAY: {
  1183. const Expression::ArrayNode *array = static_cast<const Expression::ArrayNode *>(p_node);
  1184. Array arr;
  1185. arr.resize(array->array.size());
  1186. for (int i = 0; i < array->array.size(); i++) {
  1187. Variant value;
  1188. bool ret = _execute(p_inputs, p_instance, array->array[i], value, p_const_calls_only, r_error_str);
  1189. if (ret) {
  1190. return true;
  1191. }
  1192. arr[i] = value;
  1193. }
  1194. r_ret = arr;
  1195. } break;
  1196. case Expression::ENode::TYPE_DICTIONARY: {
  1197. const Expression::DictionaryNode *dictionary = static_cast<const Expression::DictionaryNode *>(p_node);
  1198. Dictionary d;
  1199. for (int i = 0; i < dictionary->dict.size(); i += 2) {
  1200. Variant key;
  1201. bool ret = _execute(p_inputs, p_instance, dictionary->dict[i + 0], key, p_const_calls_only, r_error_str);
  1202. if (ret) {
  1203. return true;
  1204. }
  1205. Variant value;
  1206. ret = _execute(p_inputs, p_instance, dictionary->dict[i + 1], value, p_const_calls_only, r_error_str);
  1207. if (ret) {
  1208. return true;
  1209. }
  1210. d[key] = value;
  1211. }
  1212. r_ret = d;
  1213. } break;
  1214. case Expression::ENode::TYPE_CONSTRUCTOR: {
  1215. const Expression::ConstructorNode *constructor = static_cast<const Expression::ConstructorNode *>(p_node);
  1216. Vector<Variant> arr;
  1217. Vector<const Variant *> argp;
  1218. arr.resize(constructor->arguments.size());
  1219. argp.resize(constructor->arguments.size());
  1220. for (int i = 0; i < constructor->arguments.size(); i++) {
  1221. Variant value;
  1222. bool ret = _execute(p_inputs, p_instance, constructor->arguments[i], value, p_const_calls_only, r_error_str);
  1223. if (ret) {
  1224. return true;
  1225. }
  1226. arr.write[i] = value;
  1227. argp.write[i] = &arr[i];
  1228. }
  1229. Callable::CallError ce;
  1230. Variant::construct(constructor->data_type, r_ret, (const Variant **)argp.ptr(), argp.size(), ce);
  1231. if (ce.error != Callable::CallError::CALL_OK) {
  1232. r_error_str = vformat(RTR("Invalid arguments to construct '%s'"), Variant::get_type_name(constructor->data_type));
  1233. return true;
  1234. }
  1235. } break;
  1236. case Expression::ENode::TYPE_BUILTIN_FUNC: {
  1237. const Expression::BuiltinFuncNode *bifunc = static_cast<const Expression::BuiltinFuncNode *>(p_node);
  1238. Vector<Variant> arr;
  1239. Vector<const Variant *> argp;
  1240. arr.resize(bifunc->arguments.size());
  1241. argp.resize(bifunc->arguments.size());
  1242. for (int i = 0; i < bifunc->arguments.size(); i++) {
  1243. Variant value;
  1244. bool ret = _execute(p_inputs, p_instance, bifunc->arguments[i], value, p_const_calls_only, r_error_str);
  1245. if (ret) {
  1246. return true;
  1247. }
  1248. arr.write[i] = value;
  1249. argp.write[i] = &arr[i];
  1250. }
  1251. r_ret = Variant(); //may not return anything
  1252. Callable::CallError ce;
  1253. Variant::call_utility_function(bifunc->func, &r_ret, (const Variant **)argp.ptr(), argp.size(), ce);
  1254. if (ce.error != Callable::CallError::CALL_OK) {
  1255. r_error_str = "Builtin call failed: " + Variant::get_call_error_text(bifunc->func, (const Variant **)argp.ptr(), argp.size(), ce);
  1256. return true;
  1257. }
  1258. } break;
  1259. case Expression::ENode::TYPE_CALL: {
  1260. const Expression::CallNode *call = static_cast<const Expression::CallNode *>(p_node);
  1261. Variant base;
  1262. bool ret = _execute(p_inputs, p_instance, call->base, base, p_const_calls_only, r_error_str);
  1263. if (ret) {
  1264. return true;
  1265. }
  1266. Vector<Variant> arr;
  1267. Vector<const Variant *> argp;
  1268. arr.resize(call->arguments.size());
  1269. argp.resize(call->arguments.size());
  1270. for (int i = 0; i < call->arguments.size(); i++) {
  1271. Variant value;
  1272. ret = _execute(p_inputs, p_instance, call->arguments[i], value, p_const_calls_only, r_error_str);
  1273. if (ret) {
  1274. return true;
  1275. }
  1276. arr.write[i] = value;
  1277. argp.write[i] = &arr[i];
  1278. }
  1279. Callable::CallError ce;
  1280. if (p_const_calls_only) {
  1281. base.call_const(call->method, (const Variant **)argp.ptr(), argp.size(), r_ret, ce);
  1282. } else {
  1283. base.callp(call->method, (const Variant **)argp.ptr(), argp.size(), r_ret, ce);
  1284. }
  1285. if (ce.error != Callable::CallError::CALL_OK) {
  1286. r_error_str = vformat(RTR("On call to '%s':"), String(call->method));
  1287. return true;
  1288. }
  1289. } break;
  1290. }
  1291. return false;
  1292. }
  1293. Error Expression::parse(const String &p_expression, const Vector<String> &p_input_names) {
  1294. if (nodes) {
  1295. memdelete(nodes);
  1296. nodes = nullptr;
  1297. root = nullptr;
  1298. }
  1299. error_str = String();
  1300. error_set = false;
  1301. str_ofs = 0;
  1302. input_names = p_input_names;
  1303. expression = p_expression;
  1304. root = _parse_expression();
  1305. if (error_set) {
  1306. root = nullptr;
  1307. if (nodes) {
  1308. memdelete(nodes);
  1309. }
  1310. nodes = nullptr;
  1311. return ERR_INVALID_PARAMETER;
  1312. }
  1313. return OK;
  1314. }
  1315. Variant Expression::execute(Array p_inputs, Object *p_base, bool p_show_error, bool p_const_calls_only) {
  1316. ERR_FAIL_COND_V_MSG(error_set, Variant(), "There was previously a parse error: " + error_str + ".");
  1317. execution_error = false;
  1318. Variant output;
  1319. String error_txt;
  1320. bool err = _execute(p_inputs, p_base, root, output, p_const_calls_only, error_txt);
  1321. if (err) {
  1322. execution_error = true;
  1323. error_str = error_txt;
  1324. ERR_FAIL_COND_V_MSG(p_show_error, Variant(), error_str);
  1325. }
  1326. return output;
  1327. }
  1328. bool Expression::has_execute_failed() const {
  1329. return execution_error;
  1330. }
  1331. String Expression::get_error_text() const {
  1332. return error_str;
  1333. }
  1334. void Expression::_bind_methods() {
  1335. ClassDB::bind_method(D_METHOD("parse", "expression", "input_names"), &Expression::parse, DEFVAL(Vector<String>()));
  1336. ClassDB::bind_method(D_METHOD("execute", "inputs", "base_instance", "show_error", "const_calls_only"), &Expression::execute, DEFVAL(Array()), DEFVAL(Variant()), DEFVAL(true), DEFVAL(false));
  1337. ClassDB::bind_method(D_METHOD("has_execute_failed"), &Expression::has_execute_failed);
  1338. ClassDB::bind_method(D_METHOD("get_error_text"), &Expression::get_error_text);
  1339. }
  1340. Expression::~Expression() {
  1341. if (nodes) {
  1342. memdelete(nodes);
  1343. }
  1344. }