visual_script_expression.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /*************************************************************************/
  2. /* visual_script_expression.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 "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': res = 8; break;
  315. case 't': res = 9; break;
  316. case 'n': res = 10; break;
  317. case 'f': res = 12; break;
  318. case 'r': res = 13; break;
  319. case 'u': {
  320. //hexnumbarh - oct is deprecated
  321. for (int j = 0; j < 4; j++) {
  322. CharType c = GET_CHAR();
  323. if (c == 0) {
  324. _set_error("Unterminated String");
  325. r_token.type = TK_ERROR;
  326. return ERR_PARSE_ERROR;
  327. }
  328. if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
  329. _set_error("Malformed hex constant in string");
  330. r_token.type = TK_ERROR;
  331. return ERR_PARSE_ERROR;
  332. }
  333. CharType v;
  334. if (c >= '0' && c <= '9') {
  335. v = c - '0';
  336. } else if (c >= 'a' && c <= 'f') {
  337. v = c - 'a';
  338. v += 10;
  339. } else if (c >= 'A' && c <= 'F') {
  340. v = c - 'A';
  341. v += 10;
  342. } else {
  343. ERR_PRINT("BUG");
  344. v = 0;
  345. }
  346. res <<= 4;
  347. res |= v;
  348. }
  349. } break;
  350. //case '\"': res='\"'; break;
  351. //case '\\': res='\\'; break;
  352. //case '/': res='/'; break;
  353. default: {
  354. res = next;
  355. //r_err_str="Invalid escape sequence";
  356. //return ERR_PARSE_ERROR;
  357. } break;
  358. }
  359. str += res;
  360. } else {
  361. str += ch;
  362. }
  363. }
  364. r_token.type = TK_CONSTANT;
  365. r_token.value = str;
  366. return OK;
  367. } break;
  368. default: {
  369. if (cchar <= 32) {
  370. break;
  371. }
  372. if (cchar >= '0' && cchar <= '9') {
  373. //a number
  374. String num;
  375. #define READING_SIGN 0
  376. #define READING_INT 1
  377. #define READING_DEC 2
  378. #define READING_EXP 3
  379. #define READING_DONE 4
  380. int reading = READING_INT;
  381. CharType c = cchar;
  382. bool exp_sign = false;
  383. bool exp_beg = false;
  384. bool is_float = false;
  385. while (true) {
  386. switch (reading) {
  387. case READING_INT: {
  388. if (c >= '0' && c <= '9') {
  389. //pass
  390. } else if (c == '.') {
  391. reading = READING_DEC;
  392. is_float = true;
  393. } else if (c == 'e') {
  394. reading = READING_EXP;
  395. } else {
  396. reading = READING_DONE;
  397. }
  398. } break;
  399. case READING_DEC: {
  400. if (c >= '0' && c <= '9') {
  401. } else if (c == 'e') {
  402. reading = READING_EXP;
  403. } else {
  404. reading = READING_DONE;
  405. }
  406. } break;
  407. case READING_EXP: {
  408. if (c >= '0' && c <= '9') {
  409. exp_beg = true;
  410. } else if ((c == '-' || c == '+') && !exp_sign && !exp_beg) {
  411. if (c == '-')
  412. is_float = true;
  413. exp_sign = true;
  414. } else {
  415. reading = READING_DONE;
  416. }
  417. } break;
  418. }
  419. if (reading == READING_DONE)
  420. break;
  421. num += String::chr(c);
  422. c = GET_CHAR();
  423. }
  424. str_ofs--;
  425. r_token.type = TK_CONSTANT;
  426. if (is_float)
  427. r_token.value = num.to_double();
  428. else
  429. r_token.value = num.to_int();
  430. return OK;
  431. } else if ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_') {
  432. String id;
  433. bool first = true;
  434. while ((cchar >= 'A' && cchar <= 'Z') || (cchar >= 'a' && cchar <= 'z') || cchar == '_' || (!first && cchar >= '0' && cchar <= '9')) {
  435. id += String::chr(cchar);
  436. cchar = GET_CHAR();
  437. first = false;
  438. }
  439. str_ofs--; //go back one
  440. if (id == "in") {
  441. r_token.type = TK_OP_IN;
  442. } else if (id == "null") {
  443. r_token.type = TK_CONSTANT;
  444. r_token.value = Variant();
  445. } else if (id == "true") {
  446. r_token.type = TK_CONSTANT;
  447. r_token.value = true;
  448. } else if (id == "false") {
  449. r_token.type = TK_CONSTANT;
  450. r_token.value = false;
  451. } else if (id == "PI") {
  452. r_token.type = TK_CONSTANT;
  453. r_token.value = Math_PI;
  454. } else if (id == "TAU") {
  455. r_token.type = TK_CONSTANT;
  456. r_token.value = Math_TAU;
  457. } else if (id == "INF") {
  458. r_token.type = TK_CONSTANT;
  459. r_token.value = Math_INF;
  460. } else if (id == "NAN") {
  461. r_token.type = TK_CONSTANT;
  462. r_token.value = Math_NAN;
  463. } else if (id == "not") {
  464. r_token.type = TK_OP_NOT;
  465. } else if (id == "or") {
  466. r_token.type = TK_OP_OR;
  467. } else if (id == "and") {
  468. r_token.type = TK_OP_AND;
  469. } else if (id == "self") {
  470. r_token.type = TK_SELF;
  471. } else {
  472. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  473. if (id == Variant::get_type_name(Variant::Type(i))) {
  474. r_token.type = TK_BASIC_TYPE;
  475. r_token.value = i;
  476. return OK;
  477. }
  478. }
  479. VisualScriptBuiltinFunc::BuiltinFunc bifunc = VisualScriptBuiltinFunc::find_function(id);
  480. if (bifunc != VisualScriptBuiltinFunc::FUNC_MAX) {
  481. r_token.type = TK_BUILTIN_FUNC;
  482. r_token.value = bifunc;
  483. return OK;
  484. }
  485. r_token.type = TK_IDENTIFIER;
  486. r_token.value = id;
  487. }
  488. return OK;
  489. } else {
  490. _set_error("Unexpected character.");
  491. r_token.type = TK_ERROR;
  492. return ERR_PARSE_ERROR;
  493. }
  494. }
  495. }
  496. }
  497. r_token.type = TK_ERROR;
  498. return ERR_PARSE_ERROR;
  499. }
  500. const char *VisualScriptExpression::token_name[TK_MAX] = {
  501. "CURLY BRACKET OPEN",
  502. "CURLY BRACKET CLOSE",
  503. "BRACKET OPEN",
  504. "BRACKET CLOSE",
  505. "PARENTHESIS OPEN",
  506. "PARENTHESIS CLOSE",
  507. "IDENTIFIER",
  508. "BUILTIN FUNC",
  509. "SELF",
  510. "CONSTANT",
  511. "BASIC TYPE",
  512. "COLON",
  513. "COMMA",
  514. "PERIOD",
  515. "OP IN",
  516. "OP EQUAL",
  517. "OP NOT EQUAL",
  518. "OP LESS",
  519. "OP LESS EQUAL",
  520. "OP GREATER",
  521. "OP GREATER EQUAL",
  522. "OP AND",
  523. "OP OR",
  524. "OP NOT",
  525. "OP ADD",
  526. "OP SUB",
  527. "OP MUL",
  528. "OP DIV",
  529. "OP MOD",
  530. "OP SHIFT LEFT",
  531. "OP SHIFT RIGHT",
  532. "OP BIT AND",
  533. "OP BIT OR",
  534. "OP BIT XOR",
  535. "OP BIT INVERT",
  536. "EOF",
  537. "ERROR"
  538. };
  539. VisualScriptExpression::ENode *VisualScriptExpression::_parse_expression() {
  540. Vector<Expression> expression;
  541. while (true) {
  542. //keep appending stuff to expression
  543. ENode *expr = NULL;
  544. Token tk;
  545. _get_token(tk);
  546. if (error_set)
  547. return NULL;
  548. switch (tk.type) {
  549. case TK_CURLY_BRACKET_OPEN: {
  550. //a dictionary
  551. DictionaryNode *dn = alloc_node<DictionaryNode>();
  552. while (true) {
  553. int cofs = str_ofs;
  554. _get_token(tk);
  555. if (tk.type == TK_CURLY_BRACKET_CLOSE) {
  556. break;
  557. }
  558. str_ofs = cofs; //revert
  559. //parse an expression
  560. ENode *expr = _parse_expression();
  561. if (!expr)
  562. return NULL;
  563. dn->dict.push_back(expr);
  564. _get_token(tk);
  565. if (tk.type != TK_COLON) {
  566. _set_error("Expected ':'");
  567. return NULL;
  568. }
  569. expr = _parse_expression();
  570. if (!expr)
  571. return NULL;
  572. dn->dict.push_back(expr);
  573. cofs = str_ofs;
  574. _get_token(tk);
  575. if (tk.type == TK_COMMA) {
  576. //all good
  577. } else if (tk.type == TK_CURLY_BRACKET_CLOSE) {
  578. str_ofs = cofs;
  579. } else {
  580. _set_error("Expected ',' or '}'");
  581. }
  582. }
  583. expr = dn;
  584. } break;
  585. case TK_BRACKET_OPEN: {
  586. //an array
  587. ArrayNode *an = alloc_node<ArrayNode>();
  588. while (true) {
  589. int cofs = str_ofs;
  590. _get_token(tk);
  591. if (tk.type == TK_BRACKET_CLOSE) {
  592. break;
  593. }
  594. str_ofs = cofs; //revert
  595. //parse an expression
  596. ENode *expr = _parse_expression();
  597. if (!expr)
  598. return NULL;
  599. an->array.push_back(expr);
  600. cofs = str_ofs;
  601. _get_token(tk);
  602. if (tk.type == TK_COMMA) {
  603. //all good
  604. } else if (tk.type == TK_BRACKET_CLOSE) {
  605. str_ofs = cofs;
  606. } else {
  607. _set_error("Expected ',' or ']'");
  608. }
  609. }
  610. expr = an;
  611. } break;
  612. case TK_PARENTHESIS_OPEN: {
  613. //a suexpression
  614. ENode *e = _parse_expression();
  615. if (error_set)
  616. return NULL;
  617. _get_token(tk);
  618. if (tk.type != TK_PARENTHESIS_CLOSE) {
  619. _set_error("Expected ')'");
  620. return NULL;
  621. }
  622. expr = e;
  623. } break;
  624. case TK_IDENTIFIER: {
  625. String what = tk.value;
  626. int index = -1;
  627. for (int i = 0; i < inputs.size(); i++) {
  628. if (what == inputs[i].name) {
  629. index = i;
  630. break;
  631. }
  632. }
  633. if (index != -1) {
  634. InputNode *input = alloc_node<InputNode>();
  635. input->index = index;
  636. expr = input;
  637. } else {
  638. _set_error("Invalid input identifier '" + what + "'. For script variables, use self (locals are for inputs)." + what);
  639. return NULL;
  640. }
  641. } break;
  642. case TK_SELF: {
  643. SelfNode *self = alloc_node<SelfNode>();
  644. expr = self;
  645. } break;
  646. case TK_CONSTANT: {
  647. ConstantNode *constant = alloc_node<ConstantNode>();
  648. constant->value = tk.value;
  649. expr = constant;
  650. } break;
  651. case TK_BASIC_TYPE: {
  652. //constructor..
  653. Variant::Type bt = Variant::Type(int(tk.value));
  654. _get_token(tk);
  655. if (tk.type != TK_PARENTHESIS_OPEN) {
  656. _set_error("Expected '('");
  657. return NULL;
  658. }
  659. ConstructorNode *constructor = alloc_node<ConstructorNode>();
  660. constructor->data_type = bt;
  661. while (true) {
  662. int cofs = str_ofs;
  663. _get_token(tk);
  664. if (tk.type == TK_PARENTHESIS_CLOSE) {
  665. break;
  666. }
  667. str_ofs = cofs; //revert
  668. //parse an expression
  669. ENode *expr = _parse_expression();
  670. if (!expr)
  671. return NULL;
  672. constructor->arguments.push_back(expr);
  673. cofs = str_ofs;
  674. _get_token(tk);
  675. if (tk.type == TK_COMMA) {
  676. //all good
  677. } else if (tk.type == TK_PARENTHESIS_CLOSE) {
  678. str_ofs = cofs;
  679. } else {
  680. _set_error("Expected ',' or ')'");
  681. }
  682. }
  683. expr = constructor;
  684. } break;
  685. case TK_BUILTIN_FUNC: {
  686. //builtin function
  687. _get_token(tk);
  688. if (tk.type != TK_PARENTHESIS_OPEN) {
  689. _set_error("Expected '('");
  690. return NULL;
  691. }
  692. BuiltinFuncNode *bifunc = alloc_node<BuiltinFuncNode>();
  693. bifunc->func = VisualScriptBuiltinFunc::BuiltinFunc(int(tk.value));
  694. while (true) {
  695. int cofs = str_ofs;
  696. _get_token(tk);
  697. if (tk.type == TK_PARENTHESIS_CLOSE) {
  698. break;
  699. }
  700. str_ofs = cofs; //revert
  701. //parse an expression
  702. ENode *expr = _parse_expression();
  703. if (!expr)
  704. return NULL;
  705. bifunc->arguments.push_back(expr);
  706. cofs = str_ofs;
  707. _get_token(tk);
  708. if (tk.type == TK_COMMA) {
  709. //all good
  710. } else if (tk.type == TK_PARENTHESIS_CLOSE) {
  711. str_ofs = cofs;
  712. } else {
  713. _set_error("Expected ',' or ')'");
  714. }
  715. }
  716. int expected_args = VisualScriptBuiltinFunc::get_func_argument_count(bifunc->func);
  717. if (bifunc->arguments.size() != expected_args) {
  718. _set_error("Builtin func '" + VisualScriptBuiltinFunc::get_func_name(bifunc->func) + "' expects " + itos(expected_args) + " arguments.");
  719. }
  720. expr = bifunc;
  721. } break;
  722. case TK_OP_SUB: {
  723. Expression e;
  724. e.is_op = true;
  725. e.op = Variant::OP_NEGATE;
  726. expression.push_back(e);
  727. continue;
  728. } break;
  729. case TK_OP_NOT: {
  730. Expression e;
  731. e.is_op = true;
  732. e.op = Variant::OP_NOT;
  733. expression.push_back(e);
  734. continue;
  735. } break;
  736. default: {
  737. _set_error("Expected expression.");
  738. return NULL;
  739. } break;
  740. }
  741. //before going to operators, must check indexing!
  742. while (true) {
  743. int cofs2 = str_ofs;
  744. _get_token(tk);
  745. if (error_set)
  746. return NULL;
  747. bool done = false;
  748. switch (tk.type) {
  749. case TK_BRACKET_OPEN: {
  750. //value indexing
  751. IndexNode *index = alloc_node<IndexNode>();
  752. index->base = expr;
  753. ENode *what = _parse_expression();
  754. if (!what)
  755. return NULL;
  756. index->index = what;
  757. _get_token(tk);
  758. if (tk.type != TK_BRACKET_CLOSE) {
  759. _set_error("Expected ']' at end of index.");
  760. return NULL;
  761. }
  762. expr = index;
  763. } break;
  764. case TK_PERIOD: {
  765. //named indexing or function call
  766. _get_token(tk);
  767. if (tk.type != TK_IDENTIFIER) {
  768. _set_error("Expected identifier after '.'");
  769. return NULL;
  770. }
  771. StringName identifier = tk.value;
  772. int cofs = str_ofs;
  773. _get_token(tk);
  774. if (tk.type == TK_PARENTHESIS_OPEN) {
  775. //function call
  776. CallNode *func_call = alloc_node<CallNode>();
  777. func_call->method = identifier;
  778. func_call->base = expr;
  779. while (true) {
  780. int cofs = str_ofs;
  781. _get_token(tk);
  782. if (tk.type == TK_PARENTHESIS_CLOSE) {
  783. break;
  784. }
  785. str_ofs = cofs; //revert
  786. //parse an expression
  787. ENode *expr = _parse_expression();
  788. if (!expr)
  789. return NULL;
  790. func_call->arguments.push_back(expr);
  791. cofs = str_ofs;
  792. _get_token(tk);
  793. if (tk.type == TK_COMMA) {
  794. //all good
  795. } else if (tk.type == TK_PARENTHESIS_CLOSE) {
  796. str_ofs = cofs;
  797. } else {
  798. _set_error("Expected ',' or ')'");
  799. }
  800. }
  801. expr = func_call;
  802. } else {
  803. //named indexing
  804. str_ofs = cofs;
  805. NamedIndexNode *index = alloc_node<NamedIndexNode>();
  806. index->base = expr;
  807. index->name = identifier;
  808. expr = index;
  809. }
  810. } break;
  811. default: {
  812. str_ofs = cofs2;
  813. done = true;
  814. } break;
  815. }
  816. if (done)
  817. break;
  818. }
  819. //push expression
  820. {
  821. Expression e;
  822. e.is_op = false;
  823. e.node = expr;
  824. expression.push_back(e);
  825. }
  826. //ok finally look for an operator
  827. int cofs = str_ofs;
  828. _get_token(tk);
  829. if (error_set)
  830. return NULL;
  831. Variant::Operator op = Variant::OP_MAX;
  832. switch (tk.type) {
  833. case TK_OP_IN: op = Variant::OP_IN; break;
  834. case TK_OP_EQUAL: op = Variant::OP_EQUAL; break;
  835. case TK_OP_NOT_EQUAL: op = Variant::OP_NOT_EQUAL; break;
  836. case TK_OP_LESS: op = Variant::OP_LESS; break;
  837. case TK_OP_LESS_EQUAL: op = Variant::OP_LESS_EQUAL; break;
  838. case TK_OP_GREATER: op = Variant::OP_GREATER; break;
  839. case TK_OP_GREATER_EQUAL: op = Variant::OP_GREATER_EQUAL; break;
  840. case TK_OP_AND: op = Variant::OP_AND; break;
  841. case TK_OP_OR: op = Variant::OP_OR; break;
  842. case TK_OP_NOT: op = Variant::OP_NOT; break;
  843. case TK_OP_ADD: op = Variant::OP_ADD; break;
  844. case TK_OP_SUB: op = Variant::OP_SUBTRACT; break;
  845. case TK_OP_MUL: op = Variant::OP_MULTIPLY; break;
  846. case TK_OP_DIV: op = Variant::OP_DIVIDE; break;
  847. case TK_OP_MOD: op = Variant::OP_MODULE; break;
  848. case TK_OP_SHIFT_LEFT: op = Variant::OP_SHIFT_LEFT; break;
  849. case TK_OP_SHIFT_RIGHT: op = Variant::OP_SHIFT_RIGHT; break;
  850. case TK_OP_BIT_AND: op = Variant::OP_BIT_AND; break;
  851. case TK_OP_BIT_OR: op = Variant::OP_BIT_OR; break;
  852. case TK_OP_BIT_XOR: op = Variant::OP_BIT_XOR; break;
  853. case TK_OP_BIT_INVERT: op = Variant::OP_BIT_NEGATE; break;
  854. default: {};
  855. }
  856. if (op == Variant::OP_MAX) { //stop appending stuff
  857. str_ofs = cofs;
  858. break;
  859. }
  860. //push operator and go on
  861. {
  862. Expression e;
  863. e.is_op = true;
  864. e.op = op;
  865. expression.push_back(e);
  866. }
  867. }
  868. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  869. while (expression.size() > 1) {
  870. int next_op = -1;
  871. int min_priority = 0xFFFFF;
  872. bool is_unary = false;
  873. for (int i = 0; i < expression.size(); i++) {
  874. if (!expression[i].is_op) {
  875. continue;
  876. }
  877. int priority;
  878. bool unary = false;
  879. switch (expression[i].op) {
  880. case Variant::OP_BIT_NEGATE:
  881. priority = 0;
  882. unary = true;
  883. break;
  884. case Variant::OP_NEGATE:
  885. priority = 1;
  886. unary = true;
  887. break;
  888. case Variant::OP_MULTIPLY: priority = 2; break;
  889. case Variant::OP_DIVIDE: priority = 2; break;
  890. case Variant::OP_MODULE: priority = 2; break;
  891. case Variant::OP_ADD: priority = 3; break;
  892. case Variant::OP_SUBTRACT: priority = 3; break;
  893. case Variant::OP_SHIFT_LEFT: priority = 4; break;
  894. case Variant::OP_SHIFT_RIGHT: priority = 4; break;
  895. case Variant::OP_BIT_AND: priority = 5; break;
  896. case Variant::OP_BIT_XOR: priority = 6; break;
  897. case Variant::OP_BIT_OR: priority = 7; break;
  898. case Variant::OP_LESS: priority = 8; break;
  899. case Variant::OP_LESS_EQUAL: priority = 8; break;
  900. case Variant::OP_GREATER: priority = 8; break;
  901. case Variant::OP_GREATER_EQUAL: priority = 8; break;
  902. case Variant::OP_EQUAL: priority = 8; break;
  903. case Variant::OP_NOT_EQUAL: priority = 8; break;
  904. case Variant::OP_IN: priority = 10; break;
  905. case Variant::OP_NOT:
  906. priority = 11;
  907. unary = true;
  908. break;
  909. case Variant::OP_AND: priority = 12; break;
  910. case Variant::OP_OR: priority = 13; break;
  911. default: {
  912. _set_error("Parser bug, invalid operator in expression: " + itos(expression[i].op));
  913. return NULL;
  914. }
  915. }
  916. if (priority < min_priority) {
  917. // < is used for left to right (default)
  918. // <= is used for right to left
  919. next_op = i;
  920. min_priority = priority;
  921. is_unary = unary;
  922. }
  923. }
  924. if (next_op == -1) {
  925. _set_error("Yet another parser bug....");
  926. ERR_FAIL_COND_V(next_op == -1, NULL);
  927. }
  928. // OK! create operator..
  929. if (is_unary) {
  930. int expr_pos = next_op;
  931. while (expression[expr_pos].is_op) {
  932. expr_pos++;
  933. if (expr_pos == expression.size()) {
  934. //can happen..
  935. _set_error("Unexpected end of expression...");
  936. return NULL;
  937. }
  938. }
  939. //consecutively do unary opeators
  940. for (int i = expr_pos - 1; i >= next_op; i--) {
  941. OperatorNode *op = alloc_node<OperatorNode>();
  942. op->op = expression[i].op;
  943. op->nodes[0] = expression[i + 1].node;
  944. op->nodes[1] = NULL;
  945. expression.write[i].is_op = false;
  946. expression.write[i].node = op;
  947. expression.remove(i + 1);
  948. }
  949. } else {
  950. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  951. _set_error("Parser bug...");
  952. ERR_FAIL_V(NULL);
  953. }
  954. OperatorNode *op = alloc_node<OperatorNode>();
  955. op->op = expression[next_op].op;
  956. if (expression[next_op - 1].is_op) {
  957. _set_error("Parser bug...");
  958. ERR_FAIL_V(NULL);
  959. }
  960. if (expression[next_op + 1].is_op) {
  961. // this is not invalid and can really appear
  962. // but it becomes invalid anyway because no binary op
  963. // can be followed by a unary op in a valid combination,
  964. // due to how precedence works, unaries will always disappear first
  965. _set_error("Unexpected two consecutive operators.");
  966. return NULL;
  967. }
  968. op->nodes[0] = expression[next_op - 1].node; //expression goes as left
  969. op->nodes[1] = expression[next_op + 1].node; //next expression goes as right
  970. //replace all 3 nodes by this operator and make it an expression
  971. expression.write[next_op - 1].node = op;
  972. expression.remove(next_op);
  973. expression.remove(next_op);
  974. }
  975. }
  976. return expression[0].node;
  977. }
  978. bool VisualScriptExpression::_compile_expression() {
  979. if (!expression_dirty)
  980. return error_set;
  981. if (nodes) {
  982. memdelete(nodes);
  983. nodes = NULL;
  984. root = NULL;
  985. }
  986. error_str = String();
  987. error_set = false;
  988. str_ofs = 0;
  989. root = _parse_expression();
  990. if (error_set) {
  991. root = NULL;
  992. if (nodes) {
  993. memdelete(nodes);
  994. }
  995. nodes = NULL;
  996. return true;
  997. }
  998. expression_dirty = false;
  999. return false;
  1000. }
  1001. class VisualScriptNodeInstanceExpression : public VisualScriptNodeInstance {
  1002. public:
  1003. VisualScriptInstance *instance;
  1004. VisualScriptExpression *expression;
  1005. //virtual int get_working_memory_size() const { return 0; }
  1006. //execute by parsing the tree directly
  1007. virtual bool _execute(const Variant **p_inputs, VisualScriptExpression::ENode *p_node, Variant &r_ret, String &r_error_str, Variant::CallError &ce) {
  1008. switch (p_node->type) {
  1009. case VisualScriptExpression::ENode::TYPE_INPUT: {
  1010. const VisualScriptExpression::InputNode *in = static_cast<const VisualScriptExpression::InputNode *>(p_node);
  1011. r_ret = *p_inputs[in->index];
  1012. } break;
  1013. case VisualScriptExpression::ENode::TYPE_CONSTANT: {
  1014. const VisualScriptExpression::ConstantNode *c = static_cast<const VisualScriptExpression::ConstantNode *>(p_node);
  1015. r_ret = c->value;
  1016. } break;
  1017. case VisualScriptExpression::ENode::TYPE_SELF: {
  1018. r_ret = instance->get_owner_ptr();
  1019. } break;
  1020. case VisualScriptExpression::ENode::TYPE_OPERATOR: {
  1021. const VisualScriptExpression::OperatorNode *op = static_cast<const VisualScriptExpression::OperatorNode *>(p_node);
  1022. Variant a;
  1023. bool ret = _execute(p_inputs, op->nodes[0], a, r_error_str, ce);
  1024. if (ret)
  1025. return true;
  1026. Variant b;
  1027. if (op->nodes[1]) {
  1028. ret = _execute(p_inputs, op->nodes[1], b, r_error_str, ce);
  1029. if (ret)
  1030. return true;
  1031. }
  1032. bool valid = true;
  1033. Variant::evaluate(op->op, a, b, r_ret, valid);
  1034. if (!valid) {
  1035. 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()) + ".";
  1036. return true;
  1037. }
  1038. } break;
  1039. case VisualScriptExpression::ENode::TYPE_INDEX: {
  1040. const VisualScriptExpression::IndexNode *index = static_cast<const VisualScriptExpression::IndexNode *>(p_node);
  1041. Variant base;
  1042. bool ret = _execute(p_inputs, index->base, base, r_error_str, ce);
  1043. if (ret)
  1044. return true;
  1045. Variant idx;
  1046. ret = _execute(p_inputs, index->index, idx, r_error_str, ce);
  1047. if (ret)
  1048. return true;
  1049. bool valid;
  1050. r_ret = base.get(idx, &valid);
  1051. if (!valid) {
  1052. 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()) + ".";
  1053. return true;
  1054. }
  1055. } break;
  1056. case VisualScriptExpression::ENode::TYPE_NAMED_INDEX: {
  1057. const VisualScriptExpression::NamedIndexNode *index = static_cast<const VisualScriptExpression::NamedIndexNode *>(p_node);
  1058. Variant base;
  1059. bool ret = _execute(p_inputs, index->base, base, r_error_str, ce);
  1060. if (ret)
  1061. return true;
  1062. bool valid;
  1063. r_ret = base.get_named(index->name, &valid);
  1064. if (!valid) {
  1065. r_error_str = "Invalid index '" + String(index->name) + "' for base of type " + Variant::get_type_name(base.get_type()) + ".";
  1066. return true;
  1067. }
  1068. } break;
  1069. case VisualScriptExpression::ENode::TYPE_ARRAY: {
  1070. const VisualScriptExpression::ArrayNode *array = static_cast<const VisualScriptExpression::ArrayNode *>(p_node);
  1071. Array arr;
  1072. arr.resize(array->array.size());
  1073. for (int i = 0; i < array->array.size(); i++) {
  1074. Variant value;
  1075. bool ret = _execute(p_inputs, array->array[i], value, r_error_str, ce);
  1076. if (ret)
  1077. return true;
  1078. arr[i] = value;
  1079. }
  1080. r_ret = arr;
  1081. } break;
  1082. case VisualScriptExpression::ENode::TYPE_DICTIONARY: {
  1083. const VisualScriptExpression::DictionaryNode *dictionary = static_cast<const VisualScriptExpression::DictionaryNode *>(p_node);
  1084. Dictionary d;
  1085. for (int i = 0; i < dictionary->dict.size(); i += 2) {
  1086. Variant key;
  1087. bool ret = _execute(p_inputs, dictionary->dict[i + 0], key, r_error_str, ce);
  1088. if (ret)
  1089. return true;
  1090. Variant value;
  1091. ret = _execute(p_inputs, dictionary->dict[i + 1], value, r_error_str, ce);
  1092. if (ret)
  1093. return true;
  1094. d[key] = value;
  1095. }
  1096. r_ret = d;
  1097. } break;
  1098. case VisualScriptExpression::ENode::TYPE_CONSTRUCTOR: {
  1099. const VisualScriptExpression::ConstructorNode *constructor = static_cast<const VisualScriptExpression::ConstructorNode *>(p_node);
  1100. Vector<Variant> arr;
  1101. Vector<const Variant *> argp;
  1102. arr.resize(constructor->arguments.size());
  1103. argp.resize(constructor->arguments.size());
  1104. for (int i = 0; i < constructor->arguments.size(); i++) {
  1105. Variant value;
  1106. bool ret = _execute(p_inputs, constructor->arguments[i], value, r_error_str, ce);
  1107. if (ret)
  1108. return true;
  1109. arr.write[i] = value;
  1110. argp.write[i] = &arr[i];
  1111. }
  1112. r_ret = Variant::construct(constructor->data_type, (const Variant **)argp.ptr(), argp.size(), ce);
  1113. if (ce.error != Variant::CallError::CALL_OK) {
  1114. r_error_str = "Invalid arguments to construct '" + Variant::get_type_name(constructor->data_type) + "'.";
  1115. return true;
  1116. }
  1117. } break;
  1118. case VisualScriptExpression::ENode::TYPE_BUILTIN_FUNC: {
  1119. const VisualScriptExpression::BuiltinFuncNode *bifunc = static_cast<const VisualScriptExpression::BuiltinFuncNode *>(p_node);
  1120. Vector<Variant> arr;
  1121. Vector<const Variant *> argp;
  1122. arr.resize(bifunc->arguments.size());
  1123. argp.resize(bifunc->arguments.size());
  1124. for (int i = 0; i < bifunc->arguments.size(); i++) {
  1125. Variant value;
  1126. bool ret = _execute(p_inputs, bifunc->arguments[i], value, r_error_str, ce);
  1127. if (ret)
  1128. return true;
  1129. arr.write[i] = value;
  1130. argp.write[i] = &arr[i];
  1131. }
  1132. VisualScriptBuiltinFunc::exec_func(bifunc->func, (const Variant **)argp.ptr(), &r_ret, ce, r_error_str);
  1133. if (ce.error != Variant::CallError::CALL_OK) {
  1134. r_error_str = "Builtin Call Failed. " + r_error_str;
  1135. return true;
  1136. }
  1137. } break;
  1138. case VisualScriptExpression::ENode::TYPE_CALL: {
  1139. const VisualScriptExpression::CallNode *call = static_cast<const VisualScriptExpression::CallNode *>(p_node);
  1140. Variant base;
  1141. bool ret = _execute(p_inputs, call->base, base, r_error_str, ce);
  1142. if (ret)
  1143. return true;
  1144. Vector<Variant> arr;
  1145. Vector<const Variant *> argp;
  1146. arr.resize(call->arguments.size());
  1147. argp.resize(call->arguments.size());
  1148. for (int i = 0; i < call->arguments.size(); i++) {
  1149. Variant value;
  1150. bool ret = _execute(p_inputs, call->arguments[i], value, r_error_str, ce);
  1151. if (ret)
  1152. return true;
  1153. arr.write[i] = value;
  1154. argp.write[i] = &arr[i];
  1155. }
  1156. r_ret = base.call(call->method, (const Variant **)argp.ptr(), argp.size(), ce);
  1157. if (ce.error != Variant::CallError::CALL_OK) {
  1158. r_error_str = "On call to '" + String(call->method) + "':";
  1159. return true;
  1160. }
  1161. } break;
  1162. }
  1163. return false;
  1164. }
  1165. 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) {
  1166. if (!expression->root || expression->error_set) {
  1167. r_error_str = expression->error_str;
  1168. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1169. return 0;
  1170. }
  1171. bool error = _execute(p_inputs, expression->root, *p_outputs[0], r_error_str, r_error);
  1172. if (error && r_error.error == Variant::CallError::CALL_OK) {
  1173. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1174. }
  1175. #ifdef DEBUG_ENABLED
  1176. if (!error && expression->output_type != Variant::NIL && !Variant::can_convert_strict(p_outputs[0]->get_type(), expression->output_type)) {
  1177. 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) + ".";
  1178. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1179. }
  1180. #endif
  1181. return 0;
  1182. }
  1183. };
  1184. VisualScriptNodeInstance *VisualScriptExpression::instance(VisualScriptInstance *p_instance) {
  1185. _compile_expression();
  1186. VisualScriptNodeInstanceExpression *instance = memnew(VisualScriptNodeInstanceExpression);
  1187. instance->instance = p_instance;
  1188. instance->expression = this;
  1189. return instance;
  1190. }
  1191. VisualScriptExpression::VisualScriptExpression() {
  1192. output_type = Variant::NIL;
  1193. expression_dirty = true;
  1194. error_set = true;
  1195. root = NULL;
  1196. nodes = NULL;
  1197. sequenced = false;
  1198. }
  1199. VisualScriptExpression::~VisualScriptExpression() {
  1200. if (nodes) {
  1201. memdelete(nodes);
  1202. }
  1203. }
  1204. void register_visual_script_expression_node() {
  1205. VisualScriptLanguage::singleton->add_register_func("operators/expression", create_node_generic<VisualScriptExpression>);
  1206. }