gd_compiler.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. /*************************************************************************/
  2. /* gd_compiler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "gd_compiler.h"
  30. #include "gd_script.h"
  31. /* TODO:
  32. *AND and OR need early abort
  33. -Inheritance properly process (done?)
  34. *create built in initializer and constructor
  35. *assign operators
  36. *build arrays and dictionaries
  37. *call parent constructor
  38. */
  39. void GDCompiler::_set_error(const String& p_error,const GDParser::Node *p_node) {
  40. if (error!="")
  41. return;
  42. error=p_error;
  43. err_line=p_node->line;
  44. err_column=p_node->column;
  45. }
  46. bool GDCompiler::_create_unary_operator(CodeGen& codegen,const GDParser::OperatorNode *on,Variant::Operator op, int p_stack_level) {
  47. ERR_FAIL_COND_V(on->arguments.size()!=1,false);
  48. int src_address_a = _parse_expression(codegen,on->arguments[0],p_stack_level);
  49. if (src_address_a<0)
  50. return false;
  51. codegen.opcodes.push_back(GDFunction::OPCODE_OPERATOR); // perform operator
  52. codegen.opcodes.push_back(op); //which operator
  53. codegen.opcodes.push_back(src_address_a); // argument 1
  54. codegen.opcodes.push_back(GDFunction::ADDR_TYPE_NIL); // argument 2 (unary only takes one parameter)
  55. return true;
  56. }
  57. bool GDCompiler::_create_binary_operator(CodeGen& codegen,const GDParser::OperatorNode *on,Variant::Operator op, int p_stack_level) {
  58. ERR_FAIL_COND_V(on->arguments.size()!=2,false);
  59. int src_address_a = _parse_expression(codegen,on->arguments[0],p_stack_level);
  60. if (src_address_a<0)
  61. return false;
  62. if (src_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)
  63. p_stack_level++; //uses stack for return, increase stack
  64. int src_address_b = _parse_expression(codegen,on->arguments[1],p_stack_level);
  65. if (src_address_b<0)
  66. return false;
  67. codegen.opcodes.push_back(GDFunction::OPCODE_OPERATOR); // perform operator
  68. codegen.opcodes.push_back(op); //which operator
  69. codegen.opcodes.push_back(src_address_a); // argument 1
  70. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  71. return true;
  72. }
  73. /*
  74. int GDCompiler::_parse_subexpression(CodeGen& codegen,const GDParser::Node *p_expression) {
  75. int ret = _parse_expression(codegen,p_expression);
  76. if (ret<0)
  77. return ret;
  78. if (ret&(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)) {
  79. codegen.stack_level++;
  80. codegen.check_max_stack_level();
  81. //stack was used, keep value
  82. }
  83. return ret;
  84. }
  85. */
  86. int GDCompiler::_parse_assign_right_expression(CodeGen& codegen,const GDParser::OperatorNode *p_expression, int p_stack_level) {
  87. Variant::Operator var_op=Variant::OP_MAX;
  88. switch(p_expression->op) {
  89. case GDParser::OperatorNode::OP_ASSIGN_ADD: var_op=Variant::OP_ADD; break;
  90. case GDParser::OperatorNode::OP_ASSIGN_SUB: var_op=Variant::OP_SUBSTRACT; break;
  91. case GDParser::OperatorNode::OP_ASSIGN_MUL: var_op=Variant::OP_MULTIPLY; break;
  92. case GDParser::OperatorNode::OP_ASSIGN_DIV: var_op=Variant::OP_DIVIDE; break;
  93. case GDParser::OperatorNode::OP_ASSIGN_MOD: var_op=Variant::OP_MODULE; break;
  94. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: var_op=Variant::OP_SHIFT_LEFT; break;
  95. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: var_op=Variant::OP_SHIFT_RIGHT; break;
  96. case GDParser::OperatorNode::OP_ASSIGN_BIT_AND: var_op=Variant::OP_BIT_AND; break;
  97. case GDParser::OperatorNode::OP_ASSIGN_BIT_OR: var_op=Variant::OP_BIT_OR; break;
  98. case GDParser::OperatorNode::OP_ASSIGN_BIT_XOR: var_op=Variant::OP_BIT_XOR; break;
  99. case GDParser::OperatorNode::OP_ASSIGN: {
  100. //none
  101. } break;
  102. default: {
  103. ERR_FAIL_V(-1);
  104. }
  105. }
  106. if (var_op==Variant::OP_MAX) {
  107. return _parse_expression(codegen,p_expression->arguments[1],p_stack_level);
  108. }
  109. if (!_create_binary_operator(codegen,p_expression,var_op,p_stack_level))
  110. return -1;
  111. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  112. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  113. codegen.alloc_stack(p_stack_level);
  114. return dst_addr;
  115. }
  116. int GDCompiler::_parse_expression(CodeGen& codegen,const GDParser::Node *p_expression, int p_stack_level,bool p_root) {
  117. switch(p_expression->type) {
  118. //should parse variable declaration and adjust stack accordingly...
  119. case GDParser::Node::TYPE_IDENTIFIER: {
  120. //return identifier
  121. //wait, identifier could be a local variable or something else... careful here, must reference properly
  122. //as stack may be more interesting to work with
  123. //This could be made much simpler by just indexing "self", but done this way (with custom self-addressing modes) increases peformance a lot.
  124. const GDParser::IdentifierNode *in = static_cast<const GDParser::IdentifierNode*>(p_expression);
  125. StringName identifier = in->name;
  126. // TRY STACK!
  127. if (codegen.stack_identifiers.has(identifier)) {
  128. int pos = codegen.stack_identifiers[identifier];
  129. return pos|(GDFunction::ADDR_TYPE_STACK_VARIABLE<<GDFunction::ADDR_BITS);
  130. }
  131. //TRY ARGUMENTS!
  132. if (!codegen.function_node || !codegen.function_node->_static) {
  133. // TRY MEMBER VARIABLES!
  134. //static function
  135. if (codegen.script->member_indices.has(identifier)) {
  136. int idx = codegen.script->member_indices[identifier];
  137. return idx|(GDFunction::ADDR_TYPE_MEMBER<<GDFunction::ADDR_BITS); //argument (stack root)
  138. }
  139. }
  140. //TRY CLASS CONSTANTS
  141. GDScript *scr = codegen.script;
  142. GDNativeClass *nc=NULL;
  143. while(scr) {
  144. if (scr->constants.has(identifier)) {
  145. //int idx=scr->constants[identifier];
  146. int idx = codegen.get_name_map_pos(identifier);
  147. return idx|(GDFunction::ADDR_TYPE_CLASS_CONSTANT<<GDFunction::ADDR_BITS); //argument (stack root)
  148. }
  149. if (scr->native.is_valid())
  150. nc=scr->native.ptr();
  151. scr=scr->_base;
  152. }
  153. // CLASS C++ Integer Constant
  154. if (nc) {
  155. bool success=false;
  156. int constant = ObjectTypeDB::get_integer_constant(nc->get_name(),identifier,&success);
  157. if (success) {
  158. Variant key=constant;
  159. int idx;
  160. if (!codegen.constant_map.has(key)) {
  161. idx=codegen.constant_map.size();
  162. codegen.constant_map[key]=idx;
  163. } else {
  164. idx=codegen.constant_map[key];
  165. }
  166. return idx|(GDFunction::ADDR_TYPE_LOCAL_CONSTANT<<GDFunction::ADDR_BITS); //make it a local constant (faster access)
  167. }
  168. }
  169. if (codegen.script->subclasses.has(identifier)) {
  170. //same with a subclass, make it a local constant.
  171. int idx = codegen.get_constant_pos(codegen.script->subclasses[identifier]);
  172. return idx|(GDFunction::ADDR_TYPE_LOCAL_CONSTANT<<GDFunction::ADDR_BITS); //make it a local constant (faster access)
  173. }
  174. if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  175. int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier];
  176. return idx|(GDFunction::ADDR_TYPE_GLOBAL<<GDFunction::ADDR_BITS); //argument (stack root)
  177. }
  178. //not found, error
  179. _set_error("Identifier not found: "+String(identifier),p_expression);
  180. return -1;
  181. } break;
  182. case GDParser::Node::TYPE_CONSTANT: {
  183. //return constant
  184. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode*>(p_expression);
  185. int idx;
  186. if (!codegen.constant_map.has(cn->value)) {
  187. idx=codegen.constant_map.size();
  188. codegen.constant_map[cn->value]=idx;
  189. } else {
  190. idx=codegen.constant_map[cn->value];
  191. }
  192. return idx|(GDFunction::ADDR_TYPE_LOCAL_CONSTANT<<GDFunction::ADDR_BITS); //argument (stack root)
  193. } break;
  194. case GDParser::Node::TYPE_SELF: {
  195. //return constant
  196. if (codegen.function_node && codegen.function_node->_static) {
  197. _set_error("'self' not present in static function!",p_expression);
  198. return -1;
  199. }
  200. return (GDFunction::ADDR_TYPE_SELF<<GDFunction::ADDR_BITS);
  201. } break;
  202. case GDParser::Node::TYPE_ARRAY: {
  203. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode*>(p_expression);
  204. Vector<int> values;
  205. int slevel=p_stack_level;
  206. for(int i=0;i<an->elements.size();i++) {
  207. int ret = _parse_expression(codegen,an->elements[i],slevel);
  208. if (ret<0)
  209. return ret;
  210. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  211. slevel++;
  212. codegen.alloc_stack(slevel);
  213. }
  214. values.push_back(ret);
  215. }
  216. codegen.opcodes.push_back(GDFunction::OPCODE_CONSTRUCT_ARRAY);
  217. codegen.opcodes.push_back(values.size());
  218. for(int i=0;i<values.size();i++)
  219. codegen.opcodes.push_back(values[i]);
  220. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  221. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  222. codegen.alloc_stack(p_stack_level);
  223. return dst_addr;
  224. } break;
  225. case GDParser::Node::TYPE_DICTIONARY: {
  226. const GDParser::DictionaryNode *dn = static_cast<const GDParser::DictionaryNode*>(p_expression);
  227. Vector<int> values;
  228. int slevel=p_stack_level;
  229. for(int i=0;i<dn->elements.size();i++) {
  230. int ret = _parse_expression(codegen,dn->elements[i].key,slevel);
  231. if (ret<0)
  232. return ret;
  233. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  234. slevel++;
  235. codegen.alloc_stack(slevel);
  236. }
  237. values.push_back(ret);
  238. ret = _parse_expression(codegen,dn->elements[i].value,slevel);
  239. if (ret<0)
  240. return ret;
  241. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  242. slevel++;
  243. codegen.alloc_stack(slevel);
  244. }
  245. values.push_back(ret);
  246. }
  247. codegen.opcodes.push_back(GDFunction::OPCODE_CONSTRUCT_DICTIONARY);
  248. codegen.opcodes.push_back(dn->elements.size());
  249. for(int i=0;i<values.size();i++)
  250. codegen.opcodes.push_back(values[i]);
  251. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  252. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  253. codegen.alloc_stack(p_stack_level);
  254. return dst_addr;
  255. } break;
  256. case GDParser::Node::TYPE_OPERATOR: {
  257. //hell breaks loose
  258. const GDParser::OperatorNode *on = static_cast<const GDParser::OperatorNode*>(p_expression);
  259. switch(on->op) {
  260. //call/constructor operator
  261. case GDParser::OperatorNode::OP_PARENT_CALL: {
  262. ERR_FAIL_COND_V(on->arguments.size()<1,-1);
  263. const GDParser::IdentifierNode *in = (const GDParser::IdentifierNode *)on->arguments[0];
  264. Vector<int> arguments;
  265. int slevel = p_stack_level;
  266. for(int i=1;i<on->arguments.size();i++) {
  267. int ret = _parse_expression(codegen,on->arguments[i],slevel);
  268. if (ret<0)
  269. return ret;
  270. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  271. slevel++;
  272. codegen.alloc_stack(slevel);
  273. }
  274. arguments.push_back(ret);
  275. }
  276. //push call bytecode
  277. codegen.opcodes.push_back(GDFunction::OPCODE_CALL_SELF_BASE); // basic type constructor
  278. codegen.opcodes.push_back(codegen.get_name_map_pos(in->name)); //instance
  279. codegen.opcodes.push_back(arguments.size()); //argument count
  280. codegen.alloc_call(arguments.size());
  281. for(int i=0;i<arguments.size();i++)
  282. codegen.opcodes.push_back(arguments[i]); //arguments
  283. } break;
  284. case GDParser::OperatorNode::OP_CALL: {
  285. if (on->arguments[0]->type==GDParser::Node::TYPE_TYPE) {
  286. //construct a basic type
  287. ERR_FAIL_COND_V(on->arguments.size()<1,-1);
  288. const GDParser::TypeNode *tn = (const GDParser::TypeNode *)on->arguments[0];
  289. int vtype = tn->vtype;
  290. Vector<int> arguments;
  291. int slevel = p_stack_level;
  292. for(int i=1;i<on->arguments.size();i++) {
  293. int ret = _parse_expression(codegen,on->arguments[i],slevel);
  294. if (ret<0)
  295. return ret;
  296. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  297. slevel++;
  298. codegen.alloc_stack(slevel);
  299. }
  300. arguments.push_back(ret);
  301. }
  302. //push call bytecode
  303. codegen.opcodes.push_back(GDFunction::OPCODE_CONSTRUCT); // basic type constructor
  304. codegen.opcodes.push_back(vtype); //instance
  305. codegen.opcodes.push_back(arguments.size()); //argument count
  306. codegen.alloc_call(arguments.size());
  307. for(int i=0;i<arguments.size();i++)
  308. codegen.opcodes.push_back(arguments[i]); //arguments
  309. } else if (on->arguments[0]->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  310. //built in function
  311. ERR_FAIL_COND_V(on->arguments.size()<1,-1);
  312. Vector<int> arguments;
  313. int slevel = p_stack_level;
  314. for(int i=1;i<on->arguments.size();i++) {
  315. int ret = _parse_expression(codegen,on->arguments[i],slevel);
  316. if (ret<0)
  317. return ret;
  318. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  319. slevel++;
  320. codegen.alloc_stack(slevel);
  321. }
  322. arguments.push_back(ret);
  323. }
  324. codegen.opcodes.push_back(GDFunction::OPCODE_CALL_BUILT_IN);
  325. codegen.opcodes.push_back(static_cast<const GDParser::BuiltInFunctionNode*>(on->arguments[0])->function);
  326. codegen.opcodes.push_back(on->arguments.size()-1);
  327. codegen.alloc_call(on->arguments.size()-1);
  328. for(int i=0;i<arguments.size();i++)
  329. codegen.opcodes.push_back(arguments[i]);
  330. } else {
  331. //regular function
  332. ERR_FAIL_COND_V(on->arguments.size()<2,-1);
  333. const GDParser::Node *instance = on->arguments[0];
  334. if (instance->type==GDParser::Node::TYPE_SELF) {
  335. //room for optimization
  336. }
  337. Vector<int> arguments;
  338. int slevel = p_stack_level;
  339. for(int i=0;i<on->arguments.size();i++) {
  340. int ret;
  341. if (i==1) {
  342. if (on->arguments[i]->type!=GDParser::Node::TYPE_IDENTIFIER) {
  343. _set_error("Attempt to call a non-identifier.",on);
  344. return -1;
  345. }
  346. GDParser::IdentifierNode *id = static_cast<GDParser::IdentifierNode*>(on->arguments[i]);
  347. ret=codegen.get_name_map_pos(id->name);
  348. } else {
  349. ret = _parse_expression(codegen,on->arguments[i],slevel);
  350. if (ret<0)
  351. return ret;
  352. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  353. slevel++;
  354. codegen.alloc_stack(slevel);
  355. }
  356. }
  357. arguments.push_back(ret);
  358. }
  359. codegen.opcodes.push_back(p_root?GDFunction::OPCODE_CALL:GDFunction::OPCODE_CALL_RETURN); // perform operator
  360. codegen.opcodes.push_back(on->arguments.size()-2);
  361. codegen.alloc_call(on->arguments.size()-2);
  362. for(int i=0;i<arguments.size();i++)
  363. codegen.opcodes.push_back(arguments[i]);
  364. }
  365. } break;
  366. //indexing operator
  367. case GDParser::OperatorNode::OP_INDEX:
  368. case GDParser::OperatorNode::OP_INDEX_NAMED: {
  369. ERR_FAIL_COND_V(on->arguments.size()!=2,-1);
  370. int slevel = p_stack_level;
  371. bool named=(on->op==GDParser::OperatorNode::OP_INDEX_NAMED);
  372. int from = _parse_expression(codegen,on->arguments[0],slevel);
  373. if (from<0)
  374. return from;
  375. int index;
  376. if (named) {
  377. index=codegen.get_name_map_pos(static_cast<GDParser::IdentifierNode*>(on->arguments[1])->name);
  378. } else {
  379. if (on->arguments[1]->type==GDParser::Node::TYPE_CONSTANT && static_cast<const GDParser::ConstantNode*>(on->arguments[1])->value.get_type()==Variant::STRING) {
  380. //also, somehow, named (speed up anyway)
  381. StringName name = static_cast<const GDParser::ConstantNode*>(on->arguments[1])->value;
  382. index=codegen.get_name_map_pos(name);
  383. named=true;
  384. } else {
  385. //regular indexing
  386. if (from&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  387. slevel++;
  388. codegen.alloc_stack(slevel);
  389. }
  390. index = _parse_expression(codegen,on->arguments[1],slevel);
  391. if (index<0)
  392. return index;
  393. }
  394. }
  395. codegen.opcodes.push_back(named?GDFunction::OPCODE_GET_NAMED:GDFunction::OPCODE_GET); // perform operator
  396. codegen.opcodes.push_back(from); // argument 1
  397. codegen.opcodes.push_back(index); // argument 2 (unary only takes one parameter)
  398. } break;
  399. case GDParser::OperatorNode::OP_AND: {
  400. // AND operator with early out on failure
  401. int res = _parse_expression(codegen,on->arguments[0],p_stack_level);
  402. if (res<0)
  403. return res;
  404. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  405. codegen.opcodes.push_back(res);
  406. int jump_fail_pos=codegen.opcodes.size();
  407. codegen.opcodes.push_back(0);
  408. res = _parse_expression(codegen,on->arguments[1],p_stack_level);
  409. if (res<0)
  410. return res;
  411. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  412. codegen.opcodes.push_back(res);
  413. int jump_fail_pos2=codegen.opcodes.size();
  414. codegen.opcodes.push_back(0);
  415. codegen.alloc_stack(p_stack_level); //it will be used..
  416. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_TRUE);
  417. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  418. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  419. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  420. codegen.opcodes[jump_fail_pos]=codegen.opcodes.size();
  421. codegen.opcodes[jump_fail_pos2]=codegen.opcodes.size();
  422. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_FALSE);
  423. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  424. return p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS;
  425. } break;
  426. case GDParser::OperatorNode::OP_OR: {
  427. // OR operator with early out on success
  428. int res = _parse_expression(codegen,on->arguments[0],p_stack_level);
  429. if (res<0)
  430. return res;
  431. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF);
  432. codegen.opcodes.push_back(res);
  433. int jump_success_pos=codegen.opcodes.size();
  434. codegen.opcodes.push_back(0);
  435. res = _parse_expression(codegen,on->arguments[1],p_stack_level);
  436. if (res<0)
  437. return res;
  438. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF);
  439. codegen.opcodes.push_back(res);
  440. int jump_success_pos2=codegen.opcodes.size();
  441. codegen.opcodes.push_back(0);
  442. codegen.alloc_stack(p_stack_level); //it will be used..
  443. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_FALSE);
  444. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  445. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  446. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  447. codegen.opcodes[jump_success_pos]=codegen.opcodes.size();
  448. codegen.opcodes[jump_success_pos2]=codegen.opcodes.size();
  449. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_TRUE);
  450. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  451. return p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS;
  452. } break;
  453. //unary operators
  454. case GDParser::OperatorNode::OP_NEG: { if (!_create_unary_operator(codegen,on,Variant::OP_NEGATE,p_stack_level)) return -1;} break;
  455. case GDParser::OperatorNode::OP_NOT: { if (!_create_unary_operator(codegen,on,Variant::OP_NOT,p_stack_level)) return -1;} break;
  456. case GDParser::OperatorNode::OP_BIT_INVERT: { if (!_create_unary_operator(codegen,on,Variant::OP_BIT_NEGATE,p_stack_level)) return -1;} break;
  457. case GDParser::OperatorNode::OP_PREINC: { } break; //?
  458. case GDParser::OperatorNode::OP_PREDEC: { } break;
  459. case GDParser::OperatorNode::OP_INC: { } break;
  460. case GDParser::OperatorNode::OP_DEC: { } break;
  461. //binary operators (in precedence order)
  462. case GDParser::OperatorNode::OP_IN: { if (!_create_binary_operator(codegen,on,Variant::OP_IN,p_stack_level)) return -1;} break;
  463. case GDParser::OperatorNode::OP_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_EQUAL,p_stack_level)) return -1;} break;
  464. case GDParser::OperatorNode::OP_NOT_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_NOT_EQUAL,p_stack_level)) return -1;} break;
  465. case GDParser::OperatorNode::OP_LESS: { if (!_create_binary_operator(codegen,on,Variant::OP_LESS,p_stack_level)) return -1;} break;
  466. case GDParser::OperatorNode::OP_LESS_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_LESS_EQUAL,p_stack_level)) return -1;} break;
  467. case GDParser::OperatorNode::OP_GREATER: { if (!_create_binary_operator(codegen,on,Variant::OP_GREATER,p_stack_level)) return -1;} break;
  468. case GDParser::OperatorNode::OP_GREATER_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_GREATER_EQUAL,p_stack_level)) return -1;} break;
  469. case GDParser::OperatorNode::OP_ADD: { if (!_create_binary_operator(codegen,on,Variant::OP_ADD,p_stack_level)) return -1;} break;
  470. case GDParser::OperatorNode::OP_SUB: { if (!_create_binary_operator(codegen,on,Variant::OP_SUBSTRACT,p_stack_level)) return -1;} break;
  471. case GDParser::OperatorNode::OP_MUL: { if (!_create_binary_operator(codegen,on,Variant::OP_MULTIPLY,p_stack_level)) return -1;} break;
  472. case GDParser::OperatorNode::OP_DIV: { if (!_create_binary_operator(codegen,on,Variant::OP_DIVIDE,p_stack_level)) return -1;} break;
  473. case GDParser::OperatorNode::OP_MOD: { if (!_create_binary_operator(codegen,on,Variant::OP_MODULE,p_stack_level)) return -1;} break;
  474. //case GDParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  475. //case GDParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  476. case GDParser::OperatorNode::OP_BIT_AND: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_AND,p_stack_level)) return -1;} break;
  477. case GDParser::OperatorNode::OP_BIT_OR: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_OR,p_stack_level)) return -1;} break;
  478. case GDParser::OperatorNode::OP_BIT_XOR: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_XOR,p_stack_level)) return -1;} break;
  479. //shift
  480. case GDParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  481. case GDParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  482. //assignment operators
  483. case GDParser::OperatorNode::OP_ASSIGN_ADD:
  484. case GDParser::OperatorNode::OP_ASSIGN_SUB:
  485. case GDParser::OperatorNode::OP_ASSIGN_MUL:
  486. case GDParser::OperatorNode::OP_ASSIGN_DIV:
  487. case GDParser::OperatorNode::OP_ASSIGN_MOD:
  488. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  489. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  490. case GDParser::OperatorNode::OP_ASSIGN_BIT_AND:
  491. case GDParser::OperatorNode::OP_ASSIGN_BIT_OR:
  492. case GDParser::OperatorNode::OP_ASSIGN_BIT_XOR:
  493. case GDParser::OperatorNode::OP_ASSIGN: {
  494. ERR_FAIL_COND_V(on->arguments.size()!=2,-1);
  495. if (on->arguments[0]->type==GDParser::Node::TYPE_OPERATOR && (static_cast<GDParser::OperatorNode*>(on->arguments[0])->op==GDParser::OperatorNode::OP_INDEX || static_cast<GDParser::OperatorNode*>(on->arguments[0])->op==GDParser::OperatorNode::OP_INDEX_NAMED)) {
  496. //SET (chained) MODE!!
  497. int slevel=p_stack_level;
  498. GDParser::OperatorNode* op = static_cast<GDParser::OperatorNode*>(on->arguments[0]);
  499. /* Find chain of sets */
  500. List<GDParser::OperatorNode*> chain;
  501. {
  502. //create get/set chain
  503. GDParser::OperatorNode* n=op;
  504. while(true) {
  505. chain.push_back(n);
  506. if (n->arguments[0]->type!=GDParser::Node::TYPE_OPERATOR)
  507. break;
  508. n = static_cast<GDParser::OperatorNode*>(n->arguments[0]);
  509. if (n->op!=GDParser::OperatorNode::OP_INDEX && n->op!=GDParser::OperatorNode::OP_INDEX_NAMED)
  510. break;
  511. }
  512. }
  513. /* Chain of gets */
  514. //get at (potential) root stack pos, so it can be returned
  515. int prev_pos = _parse_expression(codegen,chain.back()->get()->arguments[0],slevel);
  516. if (prev_pos<0)
  517. return prev_pos;
  518. int retval=prev_pos;
  519. if (retval&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  520. slevel++;
  521. codegen.alloc_stack(slevel);
  522. }
  523. Vector<int> setchain;
  524. for(List<GDParser::OperatorNode*>::Element *E=chain.back();E;E=E->prev()) {
  525. if (E==chain.front()) //ignore first
  526. break;
  527. bool named = E->get()->op==GDParser::OperatorNode::OP_INDEX_NAMED;
  528. int key_idx;
  529. if (named) {
  530. key_idx = codegen.get_name_map_pos(static_cast<const GDParser::IdentifierNode*>(E->get()->arguments[1])->name);
  531. } else {
  532. GDParser::Node *key = E->get()->arguments[1];
  533. key_idx = _parse_expression(codegen,key,slevel);
  534. if (retval&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  535. slevel++;
  536. codegen.alloc_stack(slevel);
  537. }
  538. }
  539. if (key_idx<0)
  540. return key_idx;
  541. codegen.opcodes.push_back(named ? GDFunction::OPCODE_GET_NAMED : GDFunction::OPCODE_GET);
  542. codegen.opcodes.push_back(prev_pos);
  543. codegen.opcodes.push_back(key_idx);
  544. slevel++;
  545. codegen.alloc_stack(slevel);
  546. int dst_pos = (GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|slevel;
  547. codegen.opcodes.push_back(dst_pos);
  548. //add in reverse order, since it will be reverted
  549. setchain.push_back(dst_pos);
  550. setchain.push_back(key_idx);
  551. setchain.push_back(prev_pos);
  552. setchain.push_back(named ? GDFunction::OPCODE_SET_NAMED : GDFunction::OPCODE_SET);
  553. prev_pos=dst_pos;
  554. }
  555. setchain.invert();
  556. int set_index;
  557. bool named=false;
  558. if (static_cast<const GDParser::OperatorNode*>(op)->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  559. set_index=codegen.get_name_map_pos(static_cast<const GDParser::IdentifierNode*>(op->arguments[1])->name);
  560. named=true;
  561. } else {
  562. set_index = _parse_expression(codegen,op->arguments[1],slevel+1);
  563. named=false;
  564. }
  565. if (set_index<0)
  566. return set_index;
  567. if (set_index&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  568. slevel++;
  569. codegen.alloc_stack(slevel);
  570. }
  571. int set_value = _parse_assign_right_expression(codegen,on,slevel+1);
  572. if (set_value<0)
  573. return set_value;
  574. codegen.opcodes.push_back(named?GDFunction::OPCODE_SET_NAMED:GDFunction::OPCODE_SET);
  575. codegen.opcodes.push_back(prev_pos);
  576. codegen.opcodes.push_back(set_index);
  577. codegen.opcodes.push_back(set_value);
  578. for(int i=0;i<setchain.size();i+=4) {
  579. codegen.opcodes.push_back(setchain[i+0]);
  580. codegen.opcodes.push_back(setchain[i+1]);
  581. codegen.opcodes.push_back(setchain[i+2]);
  582. codegen.opcodes.push_back(setchain[i+3]);
  583. }
  584. return retval;
  585. } else {
  586. //ASSIGNMENT MODE!!
  587. int slevel = p_stack_level;
  588. int dst_address_a = _parse_expression(codegen,on->arguments[0],slevel);
  589. if (dst_address_a<0)
  590. return -1;
  591. if (dst_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  592. slevel++;
  593. codegen.alloc_stack(slevel);
  594. }
  595. int src_address_b = _parse_assign_right_expression(codegen,on,slevel);
  596. if (src_address_b<0)
  597. return -1;
  598. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN); // perform operator
  599. codegen.opcodes.push_back(dst_address_a); // argument 1
  600. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  601. return dst_address_a; //if anything, returns wathever was assigned or correct stack position
  602. }
  603. } break;
  604. case GDParser::OperatorNode::OP_EXTENDS: {
  605. ERR_FAIL_COND_V(on->arguments.size()!=2,false);
  606. int slevel = p_stack_level;
  607. int src_address_a = _parse_expression(codegen,on->arguments[0],slevel);
  608. if (src_address_a<0)
  609. return -1;
  610. if (src_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)
  611. slevel++; //uses stack for return, increase stack
  612. int src_address_b = _parse_expression(codegen,on->arguments[1],slevel);
  613. if (src_address_b<0)
  614. return -1;
  615. codegen.opcodes.push_back(GDFunction::OPCODE_EXTENDS_TEST); // perform operator
  616. codegen.opcodes.push_back(src_address_a); // argument 1
  617. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  618. } break;
  619. default: {
  620. ERR_EXPLAIN("Bug in bytecode compiler, unexpected operator #"+itos(on->op)+" in parse tree while parsing expression.");
  621. ERR_FAIL_V(0); //unreachable code
  622. } break;
  623. }
  624. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  625. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  626. codegen.alloc_stack(p_stack_level);
  627. return dst_addr;
  628. } break;
  629. //TYPE_TYPE,
  630. default: {
  631. ERR_EXPLAIN("Bug in bytecode compiler, unexpected node in parse tree while parsing expression.");
  632. ERR_FAIL_V(-1); //unreachable code
  633. } break;
  634. }
  635. ERR_FAIL_V(-1); //unreachable code
  636. }
  637. Error GDCompiler::_parse_block(CodeGen& codegen,const GDParser::BlockNode *p_block,int p_stack_level,int p_break_addr,int p_continue_addr) {
  638. codegen.push_stack_identifiers();
  639. int new_identifiers=0;
  640. codegen.current_line=p_block->line;
  641. for(int i=0;i<p_block->statements.size();i++) {
  642. const GDParser::Node *s = p_block->statements[i];
  643. switch(s->type) {
  644. case GDParser::Node::TYPE_NEWLINE: {
  645. const GDParser::NewLineNode *nl = static_cast<const GDParser::NewLineNode*>(s);
  646. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  647. codegen.opcodes.push_back(nl->line);
  648. codegen.current_line=nl->line;
  649. } break;
  650. case GDParser::Node::TYPE_CONTROL_FLOW: {
  651. // try subblocks
  652. const GDParser::ControlFlowNode *cf = static_cast<const GDParser::ControlFlowNode*>(s);
  653. switch(cf->cf_type) {
  654. case GDParser::ControlFlowNode::CF_IF: {
  655. #ifdef DEBUG_ENABLED
  656. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  657. codegen.opcodes.push_back(cf->line);
  658. codegen.current_line=cf->line;
  659. #endif
  660. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  661. if (ret<0)
  662. return ERR_PARSE_ERROR;
  663. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  664. codegen.opcodes.push_back(ret);
  665. int else_addr=codegen.opcodes.size();
  666. codegen.opcodes.push_back(0); //temporary
  667. Error err = _parse_block(codegen,cf->body,p_stack_level,p_break_addr,p_continue_addr);
  668. if (err)
  669. return err;
  670. if (cf->body_else) {
  671. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  672. int end_addr=codegen.opcodes.size();
  673. codegen.opcodes.push_back(0);
  674. codegen.opcodes[else_addr]=codegen.opcodes.size();
  675. Error err = _parse_block(codegen,cf->body_else,p_stack_level,p_break_addr,p_continue_addr);
  676. if (err)
  677. return err;
  678. codegen.opcodes[end_addr]=codegen.opcodes.size();
  679. } else {
  680. //end without else
  681. codegen.opcodes[else_addr]=codegen.opcodes.size();
  682. }
  683. } break;
  684. case GDParser::ControlFlowNode::CF_FOR: {
  685. int slevel=p_stack_level;
  686. int iter_stack_pos=slevel;
  687. int iterator_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  688. int counter_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  689. int container_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  690. codegen.alloc_stack(slevel);
  691. codegen.push_stack_identifiers();
  692. codegen.add_stack_identifier(static_cast<const GDParser::IdentifierNode*>(cf->arguments[0])->name,iter_stack_pos);
  693. int ret = _parse_expression(codegen,cf->arguments[1],slevel,false);
  694. if (ret<0)
  695. return ERR_COMPILATION_FAILED;
  696. //assign container
  697. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN);
  698. codegen.opcodes.push_back(container_pos);
  699. codegen.opcodes.push_back(ret);
  700. //begin loop
  701. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE_BEGIN);
  702. codegen.opcodes.push_back(counter_pos);
  703. codegen.opcodes.push_back(container_pos);
  704. codegen.opcodes.push_back(codegen.opcodes.size()+4);
  705. codegen.opcodes.push_back(iterator_pos);
  706. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  707. codegen.opcodes.push_back(codegen.opcodes.size()+8);
  708. //break loop
  709. int break_pos=codegen.opcodes.size();
  710. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  711. codegen.opcodes.push_back(0); //skip code for next
  712. //next loop
  713. int continue_pos=codegen.opcodes.size();
  714. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE);
  715. codegen.opcodes.push_back(counter_pos);
  716. codegen.opcodes.push_back(container_pos);
  717. codegen.opcodes.push_back(break_pos);
  718. codegen.opcodes.push_back(iterator_pos);
  719. Error err = _parse_block(codegen,cf->body,slevel,break_pos,continue_pos);
  720. if (err)
  721. return err;
  722. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  723. codegen.opcodes.push_back(continue_pos);
  724. codegen.opcodes[break_pos+1]=codegen.opcodes.size();
  725. codegen.pop_stack_identifiers();
  726. } break;
  727. case GDParser::ControlFlowNode::CF_WHILE: {
  728. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  729. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  730. int break_addr=codegen.opcodes.size();
  731. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  732. codegen.opcodes.push_back(0);
  733. int continue_addr=codegen.opcodes.size();
  734. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  735. if (ret<0)
  736. return ERR_PARSE_ERROR;
  737. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  738. codegen.opcodes.push_back(ret);
  739. codegen.opcodes.push_back(break_addr);
  740. Error err = _parse_block(codegen,cf->body,p_stack_level,break_addr,continue_addr);
  741. if (err)
  742. return err;
  743. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  744. codegen.opcodes.push_back(continue_addr);
  745. codegen.opcodes[break_addr+1]=codegen.opcodes.size();
  746. } break;
  747. case GDParser::ControlFlowNode::CF_SWITCH: {
  748. } break;
  749. case GDParser::ControlFlowNode::CF_BREAK: {
  750. if (p_break_addr<0) {
  751. _set_error("'break'' not within loop",cf);
  752. return ERR_COMPILATION_FAILED;
  753. }
  754. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  755. codegen.opcodes.push_back(p_break_addr);
  756. } break;
  757. case GDParser::ControlFlowNode::CF_CONTINUE: {
  758. if (p_continue_addr<0) {
  759. _set_error("'continue' not within loop",cf);
  760. return ERR_COMPILATION_FAILED;
  761. }
  762. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  763. codegen.opcodes.push_back(p_continue_addr);
  764. } break;
  765. case GDParser::ControlFlowNode::CF_RETURN: {
  766. int ret;
  767. if (cf->arguments.size()) {
  768. ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  769. if (ret<0)
  770. return ERR_PARSE_ERROR;
  771. } else {
  772. ret=GDFunction::ADDR_TYPE_NIL << GDFunction::ADDR_BITS;
  773. }
  774. codegen.opcodes.push_back(GDFunction::OPCODE_RETURN);
  775. codegen.opcodes.push_back(ret);
  776. } break;
  777. }
  778. } break;
  779. case GDParser::Node::TYPE_ASSERT: {
  780. // try subblocks
  781. const GDParser::AssertNode *as = static_cast<const GDParser::AssertNode*>(s);
  782. int ret = _parse_expression(codegen,as->condition,p_stack_level,false);
  783. if (ret<0)
  784. return ERR_PARSE_ERROR;
  785. codegen.opcodes.push_back(GDFunction::OPCODE_ASSERT);
  786. codegen.opcodes.push_back(ret);
  787. } break;
  788. case GDParser::Node::TYPE_LOCAL_VAR: {
  789. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode*>(s);
  790. codegen.add_stack_identifier(lv->name,p_stack_level++);
  791. codegen.alloc_stack(p_stack_level);
  792. new_identifiers++;
  793. } break;
  794. default: {
  795. //expression
  796. int ret = _parse_expression(codegen,s,p_stack_level,true);
  797. if (ret<0)
  798. return ERR_PARSE_ERROR;
  799. } break;
  800. }
  801. }
  802. codegen.pop_stack_identifiers();
  803. return OK;
  804. }
  805. Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func) {
  806. Vector<int> bytecode;
  807. CodeGen codegen;
  808. codegen.class_node=p_class;
  809. codegen.script=p_script;
  810. codegen.function_node=p_func;
  811. codegen.stack_max=0;
  812. codegen.current_line=0;
  813. codegen.call_max=0;
  814. codegen.debug_stack=ScriptDebugger::get_singleton()!=NULL;
  815. int stack_level=0;
  816. if (p_func) {
  817. for(int i=0;i<p_func->arguments.size();i++) {
  818. int idx = i;
  819. codegen.add_stack_identifier(p_func->arguments[i],i);
  820. }
  821. stack_level=p_func->arguments.size();
  822. }
  823. codegen.alloc_stack(stack_level);
  824. /* Parse initializer -if applies- */
  825. bool is_initializer=false || !p_func;
  826. if (!p_func || String(p_func->name)=="_init") {
  827. //parse initializer for class members
  828. if (!p_func && p_class->extends_used && p_script->native.is_null()){
  829. //call implicit parent constructor
  830. codegen.opcodes.push_back(GDFunction::OPCODE_CALL_SELF_BASE);
  831. codegen.opcodes.push_back(codegen.get_name_map_pos("_init"));
  832. codegen.opcodes.push_back(0);
  833. codegen.opcodes.push_back((GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|0);
  834. }
  835. Error err = _parse_block(codegen,p_class->initializer,stack_level);
  836. if (err)
  837. return err;
  838. is_initializer=true;
  839. }
  840. /* Parse default argument code -if applies- */
  841. Vector<int> defarg_addr;
  842. StringName func_name;
  843. if (p_func) {
  844. if (p_func->default_values.size()) {
  845. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  846. defarg_addr.push_back(codegen.opcodes.size());
  847. for(int i=0;i<p_func->default_values.size();i++) {
  848. _parse_expression(codegen,p_func->default_values[i],stack_level,true);
  849. defarg_addr.push_back(codegen.opcodes.size());
  850. }
  851. defarg_addr.invert();
  852. }
  853. Error err = _parse_block(codegen,p_func->body,stack_level);
  854. if (err)
  855. return err;
  856. func_name=p_func->name;
  857. } else {
  858. func_name="_init";
  859. }
  860. codegen.opcodes.push_back(GDFunction::OPCODE_END);
  861. GDFunction *gdfunc=NULL;
  862. //if (String(p_func->name)=="") { //initializer func
  863. // gdfunc = &p_script->initializer;
  864. //} else { //regular func
  865. p_script->member_functions[func_name]=GDFunction();
  866. gdfunc = &p_script->member_functions[func_name];
  867. //}
  868. if (p_func)
  869. gdfunc->_static=p_func->_static;
  870. //constants
  871. if (codegen.constant_map.size()) {
  872. gdfunc->_constant_count=codegen.constant_map.size();
  873. gdfunc->constants.resize(codegen.constant_map.size());
  874. gdfunc->_constants_ptr=&gdfunc->constants[0];
  875. const Variant *K=NULL;
  876. while((K=codegen.constant_map.next(K))) {
  877. int idx = codegen.constant_map[*K];
  878. gdfunc->constants[idx]=*K;
  879. }
  880. } else {
  881. gdfunc->_constants_ptr=NULL;
  882. gdfunc->_constant_count=0;
  883. }
  884. //global names
  885. if (codegen.name_map.size()) {
  886. gdfunc->global_names.resize(codegen.name_map.size());
  887. gdfunc->_global_names_ptr = &gdfunc->global_names[0];
  888. for(Map<StringName,int>::Element *E=codegen.name_map.front();E;E=E->next()) {
  889. gdfunc->global_names[E->get()]=E->key();
  890. }
  891. gdfunc->_global_names_count=gdfunc->global_names.size();
  892. } else {
  893. gdfunc->_global_names_ptr = NULL;
  894. gdfunc->_global_names_count =0;
  895. }
  896. if (codegen.opcodes.size()) {
  897. gdfunc->code=codegen.opcodes;
  898. gdfunc->_code_ptr=&gdfunc->code[0];
  899. gdfunc->_code_size=codegen.opcodes.size();
  900. } else {
  901. gdfunc->_code_ptr=NULL;
  902. gdfunc->_code_size=0;
  903. }
  904. if (defarg_addr.size()) {
  905. gdfunc->default_arguments=defarg_addr;
  906. gdfunc->_default_arg_count=defarg_addr.size();
  907. gdfunc->_default_arg_ptr=&gdfunc->default_arguments[0];
  908. } else {
  909. gdfunc->_default_arg_count=0;
  910. gdfunc->_default_arg_ptr=NULL;
  911. }
  912. gdfunc->_argument_count=p_func ? p_func->arguments.size() : 0;
  913. gdfunc->_stack_size=codegen.stack_max;
  914. gdfunc->_call_size=codegen.call_max;
  915. gdfunc->name=func_name;
  916. gdfunc->_script=p_script;
  917. gdfunc->source=source;
  918. if (p_func) {
  919. gdfunc->_initial_line=p_func->line;
  920. } else {
  921. gdfunc->_initial_line=0;
  922. }
  923. if (codegen.debug_stack)
  924. gdfunc->stack_debug=codegen.stack_debug;
  925. if (is_initializer)
  926. p_script->initializer=gdfunc;
  927. return OK;
  928. }
  929. Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDParser::ClassNode *p_class) {
  930. p_script->native=Ref<GDNativeClass>();
  931. p_script->base=Ref<GDScript>();
  932. p_script->_base=NULL;
  933. p_script->members.clear();
  934. p_script->constants.clear();
  935. p_script->member_functions.clear();
  936. p_script->member_indices.clear();
  937. p_script->member_info.clear();
  938. p_script->initializer=NULL;
  939. p_script->subclasses.clear();
  940. p_script->_owner=p_owner;
  941. p_script->tool=p_class->tool;
  942. p_script->name=p_class->name;
  943. int index_from=0;
  944. if (p_class->extends_used) {
  945. //do inheritance
  946. String path = p_class->extends_file;
  947. Ref<GDScript> script;
  948. Ref<GDNativeClass> native;
  949. if (path!="") {
  950. //path (and optionally subclasses)
  951. script = ResourceLoader::load(path);
  952. if (script.is_null()) {
  953. _set_error("Could not load base class: "+path,p_class);
  954. return ERR_FILE_NOT_FOUND;
  955. }
  956. if (p_class->extends_class.size()) {
  957. for(int i=0;i<p_class->extends_class.size();i++) {
  958. String sub = p_class->extends_class[i];
  959. if (script->subclasses.has(sub)) {
  960. script=script->subclasses[sub];
  961. } else {
  962. _set_error("Could not find subclass: "+sub,p_class);
  963. return ERR_FILE_NOT_FOUND;
  964. }
  965. }
  966. }
  967. } else {
  968. ERR_FAIL_COND_V(p_class->extends_class.size()==0,ERR_BUG);
  969. //look around for the subclasses
  970. String base=p_class->extends_class[0];
  971. GDScript *p = p_owner;
  972. Ref<GDScript> base_class;
  973. while(p) {
  974. if (p->subclasses.has(base)) {
  975. base_class=p->subclasses[base];
  976. break;
  977. }
  978. p=p->_owner;
  979. }
  980. if (base_class.is_valid()) {
  981. for(int i=1;i<p_class->extends_class.size();i++) {
  982. String subclass=p_class->extends_class[i];
  983. if (base_class->subclasses.has(subclass)) {
  984. base_class=base_class->subclasses[subclass];
  985. } else {
  986. _set_error("Could not find subclass: "+subclass,p_class);
  987. return ERR_FILE_NOT_FOUND;
  988. }
  989. }
  990. script=base_class;
  991. } else {
  992. if (p_class->extends_class.size()>1) {
  993. _set_error("Invalid inheritance (unknown class+subclasses)",p_class);
  994. return ERR_FILE_NOT_FOUND;
  995. }
  996. //if not found, try engine classes
  997. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  998. _set_error("Unknown class: '"+base+"'",p_class);
  999. return ERR_FILE_NOT_FOUND;
  1000. }
  1001. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  1002. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  1003. if (!native.is_valid()) {
  1004. _set_error("Global not a class: '"+base+"'",p_class);
  1005. return ERR_FILE_NOT_FOUND;
  1006. }
  1007. }
  1008. }
  1009. if (script.is_valid()) {
  1010. p_script->base=script;
  1011. p_script->_base=p_script->base.ptr();
  1012. p_script->member_indices=script->member_indices;
  1013. } else if (native.is_valid()) {
  1014. p_script->native=native;
  1015. } else {
  1016. _set_error("Could not determine inheritance",p_class);
  1017. return ERR_FILE_NOT_FOUND;
  1018. }
  1019. }
  1020. for(int i=0;i<p_class->variables.size();i++) {
  1021. StringName name = p_class->variables[i].identifier;
  1022. if (p_script->member_indices.has(name)) {
  1023. _set_error("Member '"+name+"' already exists (in current or parent class)",p_class);
  1024. return ERR_ALREADY_EXISTS;
  1025. }
  1026. if (p_class->variables[i]._export.type!=Variant::NIL) {
  1027. p_script->member_info[name]=p_class->variables[i]._export;
  1028. #ifdef TOOLS_ENABLED
  1029. if (p_class->variables[i].default_value.get_type()!=Variant::NIL) {
  1030. p_script->member_default_values[name]=p_class->variables[i].default_value;
  1031. }
  1032. #endif
  1033. }
  1034. int new_idx = p_script->member_indices.size();
  1035. p_script->member_indices[name]=new_idx;
  1036. p_script->members.insert(name);
  1037. }
  1038. for(int i=0;i<p_class->constant_expressions.size();i++) {
  1039. StringName name = p_class->constant_expressions[i].identifier;
  1040. ERR_CONTINUE( p_class->constant_expressions[i].expression->type!=GDParser::Node::TYPE_CONSTANT );
  1041. GDParser::ConstantNode *constant = static_cast<GDParser::ConstantNode*>(p_class->constant_expressions[i].expression);
  1042. p_script->constants.insert(name,constant->value);
  1043. //p_script->constants[constant->value].make_const();
  1044. }
  1045. //parse sub-classes
  1046. for(int i=0;i<p_class->subclasses.size();i++) {
  1047. StringName name = p_class->subclasses[i]->name;
  1048. Ref<GDScript> subclass = memnew( GDScript );
  1049. Error err = _parse_class(subclass.ptr(),p_script,p_class->subclasses[i]);
  1050. if (err)
  1051. return err;
  1052. p_script->subclasses.insert(name,subclass);
  1053. }
  1054. //parse methods
  1055. bool has_initializer=false;
  1056. for(int i=0;i<p_class->functions.size();i++) {
  1057. if (!has_initializer && p_class->functions[i]->name=="_init")
  1058. has_initializer=true;
  1059. Error err = _parse_function(p_script,p_class,p_class->functions[i]);
  1060. if (err)
  1061. return err;
  1062. }
  1063. //parse static methods
  1064. for(int i=0;i<p_class->static_functions.size();i++) {
  1065. Error err = _parse_function(p_script,p_class,p_class->static_functions[i]);
  1066. if (err)
  1067. return err;
  1068. }
  1069. if (!has_initializer) {
  1070. //create a constructor
  1071. Error err = _parse_function(p_script,p_class,NULL);
  1072. if (err)
  1073. return err;
  1074. }
  1075. return OK;
  1076. }
  1077. Error GDCompiler::compile(const GDParser *p_parser,GDScript *p_script) {
  1078. err_line=-1;
  1079. err_column=-1;
  1080. error="";
  1081. parser=p_parser;
  1082. const GDParser::Node* root = parser->get_parse_tree();
  1083. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_INVALID_DATA);
  1084. source=p_script->get_path();
  1085. Error err = _parse_class(p_script,NULL,static_cast<const GDParser::ClassNode*>(root));
  1086. if (err)
  1087. return err;
  1088. return OK;
  1089. }
  1090. String GDCompiler::get_error() const {
  1091. return error;
  1092. }
  1093. int GDCompiler::get_error_line() const{
  1094. return err_line;
  1095. }
  1096. int GDCompiler::get_error_column() const{
  1097. return err_column;
  1098. }
  1099. GDCompiler::GDCompiler()
  1100. {
  1101. }