visual_script_expression.cpp 37 KB

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