visual_script_expression.cpp 32 KB

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