test_gdscript.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /*************************************************************************/
  2. /* test_gdscript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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 "test_gdscript.h"
  30. #include "os/main_loop.h"
  31. #include "os/os.h"
  32. #include "os/file_access.h"
  33. #ifdef GDSCRIPT_ENABLED
  34. #include "modules/gdscript/gd_tokenizer.h"
  35. #include "modules/gdscript/gd_parser.h"
  36. #include "modules/gdscript/gd_compiler.h"
  37. #include "modules/gdscript/gd_script.h"
  38. namespace TestGDScript {
  39. static void _print_indent(int p_ident,const String& p_text) {
  40. String txt;
  41. for(int i=0;i<p_ident;i++) {
  42. txt+='\t';
  43. }
  44. print_line(txt+p_text);
  45. }
  46. static String _parser_extends(const GDParser::ClassNode *p_class) {
  47. String txt="extends ";
  48. if (String(p_class->extends_file)!="") {
  49. txt+="\""+p_class->extends_file+"\"";
  50. if (p_class->extends_class.size())
  51. txt+=".";
  52. }
  53. for(int i=0;i<p_class->extends_class.size();i++) {
  54. if (i!=0)
  55. txt+=".";
  56. txt+=p_class->extends_class[i];
  57. }
  58. return txt;
  59. }
  60. static String _parser_expr(const GDParser::Node *p_expr) {
  61. String txt;
  62. switch(p_expr->type) {
  63. case GDParser::Node::TYPE_IDENTIFIER: {
  64. const GDParser::IdentifierNode *id_node = static_cast<const GDParser::IdentifierNode *>(p_expr);
  65. txt=id_node->name;
  66. } break;
  67. case GDParser::Node::TYPE_CONSTANT: {
  68. const GDParser::ConstantNode *c_node = static_cast<const GDParser::ConstantNode *>(p_expr);
  69. if (c_node->value.get_type()==Variant::STRING)
  70. txt="\""+String(c_node->value)+"\"";
  71. else
  72. txt=c_node->value;
  73. } break;
  74. case GDParser::Node::TYPE_SELF: {
  75. txt="self";
  76. } break;
  77. case GDParser::Node::TYPE_ARRAY: {
  78. const GDParser::ArrayNode *arr_node = static_cast<const GDParser::ArrayNode *>(p_expr);
  79. txt+="[";
  80. for(int i=0;i<arr_node->elements.size();i++) {
  81. if (i>0)
  82. txt+=", ";
  83. txt+=_parser_expr(arr_node->elements[i]);
  84. }
  85. txt+="]";
  86. } break;
  87. case GDParser::Node::TYPE_DICTIONARY: {
  88. const GDParser::DictionaryNode *dict_node = static_cast<const GDParser::DictionaryNode *>(p_expr);
  89. txt+="{";
  90. for(int i=0;i<dict_node->elements.size();i++) {
  91. if (i>0)
  92. txt+=", ";
  93. const GDParser::DictionaryNode::Pair &p = dict_node->elements[i];
  94. txt+=_parser_expr(p.key);
  95. txt+=":";
  96. txt+=_parser_expr(p.value);
  97. }
  98. txt+="}";
  99. } break;
  100. case GDParser::Node::TYPE_OPERATOR: {
  101. const GDParser::OperatorNode *c_node = static_cast<const GDParser::OperatorNode *>(p_expr);
  102. switch(c_node->op) {
  103. case GDParser::OperatorNode::OP_PARENT_CALL:
  104. txt+=".";
  105. case GDParser::OperatorNode::OP_CALL: {
  106. ERR_FAIL_COND_V(c_node->arguments.size()<1,"");
  107. String func_name;
  108. const GDParser::Node *nfunc = c_node->arguments[0];
  109. int arg_ofs=0;
  110. if (nfunc->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  111. const GDParser::BuiltInFunctionNode *bif_node = static_cast<const GDParser::BuiltInFunctionNode *>(nfunc);
  112. func_name=GDFunctions::get_func_name(bif_node->function);
  113. arg_ofs=1;
  114. } else if (nfunc->type==GDParser::Node::TYPE_TYPE) {
  115. const GDParser::TypeNode *t_node = static_cast<const GDParser::TypeNode *>(nfunc);
  116. func_name=Variant::get_type_name(t_node->vtype);
  117. arg_ofs=1;
  118. } else {
  119. ERR_FAIL_COND_V(c_node->arguments.size()<2,"");
  120. nfunc = c_node->arguments[1];
  121. ERR_FAIL_COND_V(nfunc->type!=GDParser::Node::TYPE_IDENTIFIER,"");
  122. if (c_node->arguments[0]->type!=GDParser::Node::TYPE_SELF)
  123. func_name=_parser_expr(c_node->arguments[0])+".";
  124. func_name+=_parser_expr(nfunc);
  125. arg_ofs=2;
  126. }
  127. txt+=func_name+"(";
  128. for(int i=arg_ofs;i<c_node->arguments.size();i++) {
  129. const GDParser::Node *arg=c_node->arguments[i];
  130. if (i>arg_ofs)
  131. txt+=", ";
  132. txt+=_parser_expr(arg);
  133. }
  134. txt+=")";
  135. } break;
  136. case GDParser::OperatorNode::OP_INDEX: {
  137. ERR_FAIL_COND_V(c_node->arguments.size()!=2,"");
  138. //index with []
  139. txt=_parser_expr(c_node->arguments[0])+"["+_parser_expr(c_node->arguments[1])+"]";
  140. } break;
  141. case GDParser::OperatorNode::OP_INDEX_NAMED: {
  142. ERR_FAIL_COND_V(c_node->arguments.size()!=2,"");
  143. txt=_parser_expr(c_node->arguments[0])+"."+_parser_expr(c_node->arguments[1]);
  144. } break;
  145. case GDParser::OperatorNode::OP_NEG: { txt="-"+_parser_expr(c_node->arguments[0]); } break;
  146. case GDParser::OperatorNode::OP_NOT: { txt="not "+_parser_expr(c_node->arguments[0]); } break;
  147. case GDParser::OperatorNode::OP_BIT_INVERT: { txt="~"+_parser_expr(c_node->arguments[0]); } break;
  148. case GDParser::OperatorNode::OP_PREINC: {} break;
  149. case GDParser::OperatorNode::OP_PREDEC: {} break;
  150. case GDParser::OperatorNode::OP_INC: {} break;
  151. case GDParser::OperatorNode::OP_DEC: {} break;
  152. case GDParser::OperatorNode::OP_IN: { txt=_parser_expr(c_node->arguments[0])+" in "+_parser_expr(c_node->arguments[1]); } break;
  153. case GDParser::OperatorNode::OP_EQUAL: { txt=_parser_expr(c_node->arguments[0])+"=="+_parser_expr(c_node->arguments[1]); } break;
  154. case GDParser::OperatorNode::OP_NOT_EQUAL: { txt=_parser_expr(c_node->arguments[0])+"!="+_parser_expr(c_node->arguments[1]); } break;
  155. case GDParser::OperatorNode::OP_LESS: { txt=_parser_expr(c_node->arguments[0])+"<"+_parser_expr(c_node->arguments[1]); } break;
  156. case GDParser::OperatorNode::OP_LESS_EQUAL: { txt=_parser_expr(c_node->arguments[0])+"<="+_parser_expr(c_node->arguments[1]); } break;
  157. case GDParser::OperatorNode::OP_GREATER: { txt=_parser_expr(c_node->arguments[0])+">"+_parser_expr(c_node->arguments[1]); } break;
  158. case GDParser::OperatorNode::OP_GREATER_EQUAL: { txt=_parser_expr(c_node->arguments[0])+">="+_parser_expr(c_node->arguments[1]); } break;
  159. case GDParser::OperatorNode::OP_AND: { txt=_parser_expr(c_node->arguments[0])+" and "+_parser_expr(c_node->arguments[1]); } break;
  160. case GDParser::OperatorNode::OP_OR: { txt=_parser_expr(c_node->arguments[0])+" or "+_parser_expr(c_node->arguments[1]); } break;
  161. case GDParser::OperatorNode::OP_ADD: { txt=_parser_expr(c_node->arguments[0])+"+"+_parser_expr(c_node->arguments[1]); } break;
  162. case GDParser::OperatorNode::OP_SUB: { txt=_parser_expr(c_node->arguments[0])+"-"+_parser_expr(c_node->arguments[1]); } break;
  163. case GDParser::OperatorNode::OP_MUL: { txt=_parser_expr(c_node->arguments[0])+"*"+_parser_expr(c_node->arguments[1]); } break;
  164. case GDParser::OperatorNode::OP_DIV: { txt=_parser_expr(c_node->arguments[0])+"/"+_parser_expr(c_node->arguments[1]); } break;
  165. case GDParser::OperatorNode::OP_MOD: { txt=_parser_expr(c_node->arguments[0])+"%"+_parser_expr(c_node->arguments[1]); } break;
  166. case GDParser::OperatorNode::OP_SHIFT_LEFT: { txt=_parser_expr(c_node->arguments[0])+"<<"+_parser_expr(c_node->arguments[1]); } break;
  167. case GDParser::OperatorNode::OP_SHIFT_RIGHT: { txt=_parser_expr(c_node->arguments[0])+">>"+_parser_expr(c_node->arguments[1]); } break;
  168. case GDParser::OperatorNode::OP_ASSIGN: { txt=_parser_expr(c_node->arguments[0])+"="+_parser_expr(c_node->arguments[1]); } break;
  169. case GDParser::OperatorNode::OP_ASSIGN_ADD: { txt=_parser_expr(c_node->arguments[0])+"+="+_parser_expr(c_node->arguments[1]); } break;
  170. case GDParser::OperatorNode::OP_ASSIGN_SUB: { txt=_parser_expr(c_node->arguments[0])+"-="+_parser_expr(c_node->arguments[1]); } break;
  171. case GDParser::OperatorNode::OP_ASSIGN_MUL: { txt=_parser_expr(c_node->arguments[0])+"*="+_parser_expr(c_node->arguments[1]); } break;
  172. case GDParser::OperatorNode::OP_ASSIGN_DIV: { txt=_parser_expr(c_node->arguments[0])+"/="+_parser_expr(c_node->arguments[1]); } break;
  173. case GDParser::OperatorNode::OP_ASSIGN_MOD: { txt=_parser_expr(c_node->arguments[0])+"%="+_parser_expr(c_node->arguments[1]); } break;
  174. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT:{ txt=_parser_expr(c_node->arguments[0])+"<<="+_parser_expr(c_node->arguments[1]); } break;
  175. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: { txt=_parser_expr(c_node->arguments[0])+">>="+_parser_expr(c_node->arguments[1]); } break;
  176. case GDParser::OperatorNode::OP_ASSIGN_BIT_AND: { txt=_parser_expr(c_node->arguments[0])+"&="+_parser_expr(c_node->arguments[1]); } break;
  177. case GDParser::OperatorNode::OP_ASSIGN_BIT_OR: { txt=_parser_expr(c_node->arguments[0])+"|="+_parser_expr(c_node->arguments[1]); } break;
  178. case GDParser::OperatorNode::OP_ASSIGN_BIT_XOR: { txt=_parser_expr(c_node->arguments[0])+"^="+_parser_expr(c_node->arguments[1]); } break;
  179. case GDParser::OperatorNode::OP_BIT_AND: { txt=_parser_expr(c_node->arguments[0])+"&"+_parser_expr(c_node->arguments[1]); } break;;
  180. case GDParser::OperatorNode::OP_BIT_OR: { txt=_parser_expr(c_node->arguments[0])+"|"+_parser_expr(c_node->arguments[1]); } break;
  181. case GDParser::OperatorNode::OP_BIT_XOR: { txt=_parser_expr(c_node->arguments[0])+"^"+_parser_expr(c_node->arguments[1]); } break;
  182. }
  183. } break;
  184. case GDParser::Node::TYPE_NEWLINE: {
  185. //skippie
  186. } break;
  187. default: {
  188. String error="Parser bug at "+itos(p_expr->line)+", invalid expression type: "+itos(p_expr->type);
  189. ERR_EXPLAIN(error);
  190. ERR_FAIL_V("");
  191. }
  192. }
  193. return txt;
  194. //return "("+txt+")";
  195. }
  196. static void _parser_show_block(const GDParser::BlockNode *p_block,int p_indent) {
  197. for(int i=0;i<p_block->statements.size();i++) {
  198. const GDParser::Node *statement=p_block->statements[i];
  199. switch(statement->type) {
  200. case GDParser::Node::TYPE_CONTROL_FLOW: {
  201. const GDParser::ControlFlowNode *cf_node = static_cast<const GDParser::ControlFlowNode *>(statement);
  202. switch(cf_node->cf_type) {
  203. case GDParser::ControlFlowNode::CF_IF: {
  204. ERR_FAIL_COND(cf_node->arguments.size()!=1);
  205. String txt;
  206. txt+="if ";
  207. txt+=_parser_expr(cf_node->arguments[0]);
  208. txt+=":";
  209. _print_indent(p_indent,txt);
  210. ERR_FAIL_COND(!cf_node->body);
  211. _parser_show_block(cf_node->body,p_indent+1);
  212. if (cf_node->body_else) {
  213. _print_indent(p_indent,"else:");
  214. _parser_show_block(cf_node->body_else,p_indent+1);
  215. }
  216. } break;
  217. case GDParser::ControlFlowNode::CF_FOR: {
  218. ERR_FAIL_COND(cf_node->arguments.size()!=2);
  219. String txt;
  220. txt+="for ";
  221. txt+=_parser_expr(cf_node->arguments[0]);
  222. txt+=" in ";
  223. txt+=_parser_expr(cf_node->arguments[1]);
  224. txt+=":";
  225. _print_indent(p_indent,txt);
  226. ERR_FAIL_COND(!cf_node->body);
  227. _parser_show_block(cf_node->body,p_indent+1);
  228. } break;
  229. case GDParser::ControlFlowNode::CF_WHILE: {
  230. ERR_FAIL_COND(cf_node->arguments.size()!=1);
  231. String txt;
  232. txt+="while ";
  233. txt+=_parser_expr(cf_node->arguments[0]);
  234. txt+=":";
  235. _print_indent(p_indent,txt);
  236. ERR_FAIL_COND(!cf_node->body);
  237. _parser_show_block(cf_node->body,p_indent+1);
  238. } break;
  239. case GDParser::ControlFlowNode::CF_SWITCH: {
  240. } break;
  241. case GDParser::ControlFlowNode::CF_CONTINUE: {
  242. _print_indent(p_indent,"continue");
  243. } break;
  244. case GDParser::ControlFlowNode::CF_BREAK: {
  245. _print_indent(p_indent,"break");
  246. } break;
  247. case GDParser::ControlFlowNode::CF_RETURN: {
  248. if (cf_node->arguments.size())
  249. _print_indent(p_indent,"return "+_parser_expr(cf_node->arguments[0]));
  250. else
  251. _print_indent(p_indent,"return ");
  252. } break;
  253. }
  254. } break;
  255. case GDParser::Node::TYPE_LOCAL_VAR: {
  256. const GDParser::LocalVarNode *lv_node = static_cast<const GDParser::LocalVarNode *>(statement);
  257. _print_indent(p_indent,"var "+String(lv_node->name));
  258. } break;
  259. default: {
  260. //expression i guess
  261. _print_indent(p_indent,_parser_expr(statement));
  262. }
  263. }
  264. }
  265. }
  266. static void _parser_show_function(const GDParser::FunctionNode *p_func,int p_indent,GDParser::BlockNode *p_initializer=NULL) {
  267. String txt;
  268. if (p_func->_static)
  269. txt="static ";
  270. txt+="func ";
  271. if (p_func->name=="") // initializer
  272. txt+="[built-in-initializer]";
  273. else
  274. txt+=String(p_func->name);
  275. txt+="(";
  276. for(int i=0;i<p_func->arguments.size();i++) {
  277. if (i!=0)
  278. txt+=", ";
  279. txt+="var "+String(p_func->arguments[i]);
  280. if (i>=(p_func->arguments.size()-p_func->default_values.size())) {
  281. int defarg = i - (p_func->arguments.size() - p_func->default_values.size());
  282. txt+="=";
  283. txt+=_parser_expr(p_func->default_values[defarg]);
  284. }
  285. }
  286. txt+=")";
  287. //todo constructor check!
  288. txt+=":";
  289. _print_indent(p_indent,txt);
  290. if (p_initializer)
  291. _parser_show_block(p_initializer,p_indent+1);
  292. _parser_show_block(p_func->body,p_indent+1);
  293. }
  294. static void _parser_show_class(const GDParser::ClassNode *p_class,int p_indent,const Vector<String>& p_code) {
  295. if (p_indent==0 && (String(p_class->extends_file)!="" || p_class->extends_class.size())) {
  296. _print_indent(p_indent,_parser_extends(p_class));
  297. print_line("\n");
  298. }
  299. for(int i=0;i<p_class->subclasses.size();i++) {
  300. const GDParser::ClassNode *subclass=p_class->subclasses[i];
  301. String line="class "+subclass->name;
  302. if (String(subclass->extends_file)!="" || subclass->extends_class.size())
  303. line+=" "+_parser_extends(subclass);
  304. line+=":";
  305. _print_indent(p_indent,line);
  306. _parser_show_class(subclass,p_indent+1,p_code);
  307. print_line("\n");
  308. }
  309. for(int i=0;i<p_class->constant_expressions.size();i++) {
  310. const GDParser::ClassNode::Constant &constant=p_class->constant_expressions[i];
  311. _print_indent(p_indent,"const "+String(constant.identifier)+"="+_parser_expr(constant.expression));
  312. }
  313. for(int i=0;i<p_class->variables.size();i++) {
  314. const GDParser::ClassNode::Member &m=p_class->variables[i];
  315. _print_indent(p_indent,"var "+String(m.identifier));
  316. }
  317. print_line("\n");
  318. for(int i=0;i<p_class->static_functions.size();i++) {
  319. _parser_show_function(p_class->static_functions[i],p_indent);
  320. print_line("\n");
  321. }
  322. for(int i=0;i<p_class->functions.size();i++) {
  323. if (String(p_class->functions[i]->name)=="_init") {
  324. _parser_show_function(p_class->functions[i],p_indent,p_class->initializer);
  325. } else
  326. _parser_show_function(p_class->functions[i],p_indent);
  327. print_line("\n");
  328. }
  329. //_parser_show_function(p_class->initializer,p_indent);
  330. print_line("\n");
  331. }
  332. static String _disassemble_addr(const Ref<GDScript>& p_script,const GDFunction& func, int p_addr) {
  333. int addr=p_addr&GDFunction::ADDR_MASK;
  334. switch(p_addr>>GDFunction::ADDR_BITS) {
  335. case GDFunction::ADDR_TYPE_SELF: {
  336. return "self";
  337. } break;
  338. case GDFunction::ADDR_TYPE_CLASS: {
  339. return "class";
  340. } break;
  341. case GDFunction::ADDR_TYPE_MEMBER: {
  342. return "member("+p_script->debug_get_member_by_index(addr)+")";
  343. } break;
  344. case GDFunction::ADDR_TYPE_CLASS_CONSTANT: {
  345. return "class_const("+func.get_global_name(addr)+")";
  346. } break;
  347. case GDFunction::ADDR_TYPE_LOCAL_CONSTANT: {
  348. Variant v=func.get_constant(addr);
  349. String txt;
  350. if (v.get_type()==Variant::STRING || v.get_type()==Variant::NODE_PATH)
  351. txt="\""+String(v)+"\"";
  352. else
  353. txt=v;
  354. return "const("+txt+")";
  355. } break;
  356. case GDFunction::ADDR_TYPE_STACK: {
  357. return "stack("+itos(addr)+")";
  358. } break;
  359. case GDFunction::ADDR_TYPE_STACK_VARIABLE: {
  360. return "var_stack("+itos(addr)+")";
  361. } break;
  362. case GDFunction::ADDR_TYPE_GLOBAL: {
  363. return "global("+func.get_global_name(addr)+")";
  364. } break;
  365. case GDFunction::ADDR_TYPE_NIL: {
  366. return "nil";
  367. } break;
  368. }
  369. return "<err>";
  370. }
  371. static void _disassemble_class(const Ref<GDScript>& p_class,const Vector<String>& p_code) {
  372. const Map<StringName,GDFunction>& mf = p_class->debug_get_member_functions();
  373. for(const Map<StringName,GDFunction>::Element *E=mf.front();E;E=E->next()) {
  374. const GDFunction &func=E->get();
  375. const int *code = func.get_code();
  376. int codelen=func.get_code_size();
  377. String defargs;
  378. if (func.get_default_argument_count()) {
  379. defargs="defarg at: ";
  380. for(int i=0;i<func.get_default_argument_count();i++) {
  381. if (i>0)
  382. defargs+=",";
  383. defargs+=itos(func.get_default_argument_addr(i));
  384. }
  385. defargs+=" ";
  386. }
  387. print_line("== function "+String(func.get_name())+"() :: stack size: "+itos(func.get_max_stack_size())+" "+defargs+"==");
  388. #define DADDR(m_ip) (_disassemble_addr(p_class,func,code[ip+m_ip]))
  389. for(int ip=0;ip<codelen;) {
  390. int incr=0;
  391. String txt=itos(ip)+" ";
  392. switch(code[ip]) {
  393. case GDFunction::OPCODE_OPERATOR: {
  394. int op = code[ip+1];
  395. txt+="op ";
  396. String opname = Variant::get_operator_name(Variant::Operator(op));
  397. txt+=DADDR(4);
  398. txt+=" = ";
  399. txt+=DADDR(2);
  400. txt+=" "+opname+" ";
  401. txt+=DADDR(3);
  402. incr+=5;
  403. } break;
  404. case GDFunction::OPCODE_SET: {
  405. txt+="set ";
  406. txt+=DADDR(1);
  407. txt+="[";
  408. txt+=DADDR(2);
  409. txt+="]=";
  410. txt+=DADDR(3);
  411. incr+=4;
  412. } break;
  413. case GDFunction::OPCODE_GET: {
  414. txt+=" get ";
  415. txt+=DADDR(3);
  416. txt+="=";
  417. txt+=DADDR(1);
  418. txt+="[";
  419. txt+=DADDR(2);
  420. txt+="]";
  421. incr+=4;
  422. } break;
  423. case GDFunction::OPCODE_SET_NAMED: {
  424. txt+=" set_named ";
  425. txt+=DADDR(1);
  426. txt+="[\"";
  427. txt+=func.get_global_name(code[ip+2]);
  428. txt+="\"]=";
  429. txt+=DADDR(3);
  430. incr+=4;
  431. } break;
  432. case GDFunction::OPCODE_GET_NAMED: {
  433. txt+=" get_named ";
  434. txt+=DADDR(3);
  435. txt+="=";
  436. txt+=DADDR(1);
  437. txt+="[\"";
  438. txt+=func.get_global_name(code[ip+2]);
  439. txt+="\"]";
  440. incr+=4;
  441. } break;
  442. case GDFunction::OPCODE_ASSIGN: {
  443. txt+=" assign ";
  444. txt+=DADDR(1);
  445. txt+="=";
  446. txt+=DADDR(2);
  447. incr+=3;
  448. } break;
  449. case GDFunction::OPCODE_ASSIGN_TRUE: {
  450. txt+=" assign ";
  451. txt+=DADDR(1);
  452. txt+="= true";
  453. incr+=2;
  454. } break;
  455. case GDFunction::OPCODE_ASSIGN_FALSE: {
  456. txt+=" assign ";
  457. txt+=DADDR(1);
  458. txt+="= false";
  459. incr+=2;
  460. } break;
  461. case GDFunction::OPCODE_CONSTRUCT: {
  462. Variant::Type t=Variant::Type(code[ip+1]);
  463. int argc=code[ip+2];
  464. txt+=" construct ";
  465. txt+=DADDR(3+argc);
  466. txt+=" = ";
  467. txt+=Variant::get_type_name(t)+"(";
  468. for(int i=0;i<argc;i++) {
  469. if (i>0)
  470. txt+=", ";
  471. txt+=DADDR(i+3);
  472. }
  473. txt+=")";
  474. incr=4+argc;
  475. } break;
  476. case GDFunction::OPCODE_CONSTRUCT_ARRAY: {
  477. int argc=code[ip+1];
  478. txt+=" make_array ";
  479. txt+=DADDR(2+argc);
  480. txt+=" = [ ";
  481. for(int i=0;i<argc;i++) {
  482. if (i>0)
  483. txt+=", ";
  484. txt+=DADDR(2+i);
  485. }
  486. txt+="]";
  487. incr+=3+argc;
  488. } break;
  489. case GDFunction::OPCODE_CONSTRUCT_DICTIONARY: {
  490. int argc=code[ip+1];
  491. txt+=" make_dict ";
  492. txt+=DADDR(2+argc*2);
  493. txt+=" = { ";
  494. for(int i=0;i<argc;i++) {
  495. if (i>0)
  496. txt+=", ";
  497. txt+=DADDR(2+i*2+0);
  498. txt+=":";
  499. txt+=DADDR(2+i*2+1);
  500. }
  501. txt+="}";
  502. incr+=3+argc*2;
  503. } break;
  504. case GDFunction::OPCODE_CALL:
  505. case GDFunction::OPCODE_CALL_RETURN: {
  506. bool ret=code[ip]==GDFunction::OPCODE_CALL_RETURN;
  507. if (ret)
  508. txt+=" call-ret ";
  509. else
  510. txt+=" call ";
  511. int argc=code[ip+1];
  512. if (ret) {
  513. txt+=DADDR(4+argc)+"=";
  514. }
  515. txt+=DADDR(2)+".";
  516. txt+=String(func.get_global_name(code[ip+3]));
  517. txt+="(";
  518. for(int i=0;i<argc;i++) {
  519. if (i>0)
  520. txt+=", ";
  521. txt+=DADDR(4+i);
  522. }
  523. txt+=")";
  524. incr=5+argc;
  525. } break;
  526. case GDFunction::OPCODE_CALL_BUILT_IN: {
  527. txt+=" call-built-in ";
  528. int argc=code[ip+2];
  529. txt+=DADDR(3+argc)+"=";
  530. txt+=GDFunctions::get_func_name(GDFunctions::Function(code[ip+1]));
  531. txt+="(";
  532. for(int i=0;i<argc;i++) {
  533. if (i>0)
  534. txt+=", ";
  535. txt+=DADDR(3+i);
  536. }
  537. txt+=")";
  538. incr=4+argc;
  539. } break;
  540. case GDFunction::OPCODE_CALL_SELF_BASE: {
  541. txt+=" call-self-base ";
  542. int argc=code[ip+2];
  543. txt+=DADDR(3+argc)+"=";
  544. txt+=func.get_global_name(code[ip+1]);
  545. txt+="(";
  546. for(int i=0;i<argc;i++) {
  547. if (i>0)
  548. txt+=", ";
  549. txt+=DADDR(3+i);
  550. }
  551. txt+=")";
  552. incr=4+argc;
  553. } break;
  554. case GDFunction::OPCODE_YIELD: {
  555. txt+=" yield ";
  556. incr=1;
  557. } break;
  558. case GDFunction::OPCODE_YIELD_SIGNAL: {
  559. txt+=" yield_signal ";
  560. txt+=DADDR(1);
  561. txt+=",";
  562. txt+=DADDR(2);
  563. incr=3;
  564. } break;
  565. case GDFunction::OPCODE_YIELD_RESUME: {
  566. txt+=" yield resume: ";
  567. txt+=DADDR(1);
  568. incr=2;
  569. } break;
  570. case GDFunction::OPCODE_JUMP: {
  571. txt+=" jump ";
  572. txt+=itos(code[ip+1]);
  573. incr=2;
  574. } break;
  575. case GDFunction::OPCODE_JUMP_IF: {
  576. txt+=" jump-if ";
  577. txt+=DADDR(1);
  578. txt+=" to ";
  579. txt+=itos(code[ip+2]);
  580. incr=3;
  581. } break;
  582. case GDFunction::OPCODE_JUMP_IF_NOT: {
  583. txt+=" jump-if-not ";
  584. txt+=DADDR(1);
  585. txt+=" to ";
  586. txt+=itos(code[ip+2]);
  587. incr=3;
  588. } break;
  589. case GDFunction::OPCODE_JUMP_TO_DEF_ARGUMENT: {
  590. txt+=" jump-to-default-argument ";
  591. incr=1;
  592. } break;
  593. case GDFunction::OPCODE_RETURN: {
  594. txt+=" return ";
  595. txt+=DADDR(1);
  596. incr=2;
  597. } break;
  598. case GDFunction::OPCODE_ITERATE_BEGIN: {
  599. txt+=" for-init "+DADDR(4)+" in "+DADDR(2)+" counter "+DADDR(1)+" end "+itos(code[ip+3]);
  600. incr+=5;
  601. } break;
  602. case GDFunction::OPCODE_ITERATE: {
  603. txt+=" for-loop "+DADDR(4)+" in "+DADDR(2)+" counter "+DADDR(1)+" end "+itos(code[ip+3]);
  604. incr+=5;
  605. } break;
  606. case GDFunction::OPCODE_LINE: {
  607. int line = code[ip+1]-1;
  608. if (line>=0 && line <p_code.size())
  609. txt="\n"+itos(line+1)+": "+p_code[line]+"\n";
  610. else
  611. txt="";
  612. incr+=2;
  613. } break;
  614. case GDFunction::OPCODE_END: {
  615. txt+=" end";
  616. incr+=1;
  617. } break;
  618. case GDFunction::OPCODE_ASSERT: {
  619. txt+=" assert ";
  620. txt+=DADDR(1);
  621. incr+=2;
  622. } break;
  623. }
  624. if (incr==0) {
  625. ERR_EXPLAIN("unhandled opcode: "+itos(code[ip]));
  626. ERR_BREAK(incr==0);
  627. }
  628. ip+=incr;
  629. if (txt!="")
  630. print_line(txt);
  631. }
  632. }
  633. }
  634. MainLoop* test(TestType p_test) {
  635. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  636. if (cmdlargs.empty()) {
  637. //try editor!
  638. return NULL;
  639. }
  640. String test = cmdlargs.back()->get();
  641. FileAccess *fa = FileAccess::open(test,FileAccess::READ);
  642. if (!fa) {
  643. ERR_EXPLAIN("Could not open file: "+test);
  644. ERR_FAIL_V(NULL);
  645. }
  646. Vector<uint8_t> buf;
  647. int flen = fa->get_len();
  648. buf.resize(fa->get_len()+1);
  649. fa->get_buffer(&buf[0],flen);
  650. buf[flen]=0;
  651. String code;
  652. code.parse_utf8((const char*)&buf[0]);
  653. Vector<String> lines;
  654. int last=0;
  655. for(int i=0;i<=code.length();i++) {
  656. if (code[i]=='\n' || code[i]==0) {
  657. lines.push_back(code.substr(last,i-last));
  658. last=i+1;
  659. }
  660. }
  661. if (p_test==TEST_TOKENIZER) {
  662. GDTokenizerText tk;
  663. tk.set_code(code);
  664. int line=-1;
  665. while(tk.get_token()!=GDTokenizer::TK_EOF) {
  666. String text;
  667. if (tk.get_token()==GDTokenizer::TK_IDENTIFIER)
  668. text="'"+tk.get_token_identifier()+"' (identifier)";
  669. else if (tk.get_token()==GDTokenizer::TK_CONSTANT) {
  670. Variant c= tk.get_token_constant();
  671. if (c.get_type()==Variant::STRING)
  672. text="\""+String(c)+"\"";
  673. else
  674. text=c;
  675. text=text+" ("+Variant::get_type_name(c.get_type())+" constant)";
  676. } else if (tk.get_token()==GDTokenizer::TK_ERROR)
  677. text="ERROR: "+tk.get_token_error();
  678. else if (tk.get_token()==GDTokenizer::TK_NEWLINE)
  679. text="newline ("+itos(tk.get_token_line())+") + indent: "+itos(tk.get_token_line_indent());
  680. else if (tk.get_token()==GDTokenizer::TK_BUILT_IN_FUNC)
  681. text="'"+String(GDFunctions::get_func_name(tk.get_token_built_in_func()))+"' (built-in function)";
  682. else
  683. text=tk.get_token_name(tk.get_token());
  684. if (tk.get_token_line()!=line) {
  685. int from=line+1;
  686. line = tk.get_token_line();;
  687. for(int i=from;i<=line;i++) {
  688. int l=i-1;
  689. if (l>=0 && l<lines.size()) {
  690. print_line("\n"+itos(i)+": "+lines[l]+"\n");
  691. }
  692. }
  693. }
  694. print_line("\t("+itos(tk.get_token_column())+"): "+text);
  695. tk.advance();
  696. }
  697. }
  698. if (p_test==TEST_PARSER) {
  699. GDParser parser;
  700. Error err = parser.parse(code);
  701. if (err) {
  702. print_line("Parse Error:\n"+itos(parser.get_error_line())+":"+itos(parser.get_error_column())+":"+parser.get_error());
  703. memdelete(fa);
  704. return NULL;
  705. }
  706. const GDParser::Node* root = parser.get_parse_tree();
  707. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,NULL);
  708. const GDParser::ClassNode *cnode=static_cast<const GDParser::ClassNode*>(root);
  709. _parser_show_class(cnode,0,lines);
  710. }
  711. if (p_test==TEST_COMPILER) {
  712. GDParser parser;
  713. Error err = parser.parse(code);
  714. if (err) {
  715. print_line("Parse Error:\n"+itos(parser.get_error_line())+":"+itos(parser.get_error_column())+":"+parser.get_error());
  716. memdelete(fa);
  717. return NULL;
  718. }
  719. GDScript *script = memnew( GDScript );
  720. GDCompiler gdc;
  721. err = gdc.compile(&parser,script);
  722. if (err) {
  723. print_line("Compile Error:\n"+itos(gdc.get_error_line())+":"+itos(gdc.get_error_column())+":"+gdc.get_error());
  724. memdelete(script);
  725. return NULL;
  726. }
  727. Ref<GDScript> gds =Ref<GDScript>( script );
  728. Ref<GDScript> current=gds;
  729. while(current.is_valid()) {
  730. print_line("** CLASS **");
  731. _disassemble_class(current,lines);
  732. current=current->get_base();
  733. }
  734. } else if (p_test==TEST_BYTECODE) {
  735. Vector<uint8_t> buf = GDTokenizerBuffer::parse_code_string(code);
  736. String dst = test.basename()+".gdc";
  737. FileAccess *fw = FileAccess::open(dst,FileAccess::WRITE);
  738. fw->store_buffer(buf.ptr(),buf.size());
  739. memdelete(fw);
  740. }
  741. #if 0
  742. Parser parser;
  743. Error err = parser.parse(code);
  744. if (err) {
  745. print_line("error:"+itos(parser.get_error_line())+":"+itos(parser.get_error_column())+":"+parser.get_error());
  746. } else {
  747. print_line("Parse O-K!");
  748. }
  749. #endif
  750. memdelete(fa);
  751. return NULL;
  752. }
  753. }
  754. #else
  755. namespace TestGDScript {
  756. MainLoop* test(TestType p_test) {
  757. return NULL;
  758. }
  759. }
  760. #endif