test_gdscript.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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-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 "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 "script/gdscript/gd_tokenizer.h"
  35. #include "script/gdscript/gd_parser.h"
  36. #include "script/gdscript/gd_compiler.h"
  37. #include "script/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_MEMBER: {
  339. return "member("+p_script->debug_get_member_by_index(addr)+")";
  340. } break;
  341. case GDFunction::ADDR_TYPE_CLASS_CONSTANT: {
  342. return "class_const("+func.get_global_name(addr)+")";
  343. } break;
  344. case GDFunction::ADDR_TYPE_LOCAL_CONSTANT: {
  345. Variant v=func.get_constant(addr);
  346. String txt;
  347. if (v.get_type()==Variant::STRING || v.get_type()==Variant::NODE_PATH)
  348. txt="\""+String(v)+"\"";
  349. else
  350. txt=v;
  351. return "const("+txt+")";
  352. } break;
  353. case GDFunction::ADDR_TYPE_STACK: {
  354. return "stack("+itos(addr)+")";
  355. } break;
  356. case GDFunction::ADDR_TYPE_STACK_VARIABLE: {
  357. return "var_stack("+itos(addr)+")";
  358. } break;
  359. case GDFunction::ADDR_TYPE_GLOBAL: {
  360. return "global("+func.get_global_name(addr)+")";
  361. } break;
  362. case GDFunction::ADDR_TYPE_NIL: {
  363. return "nil";
  364. } break;
  365. }
  366. return "<err>";
  367. }
  368. static void _disassemble_class(const Ref<GDScript>& p_class,const Vector<String>& p_code) {
  369. const Map<StringName,GDFunction>& mf = p_class->debug_get_member_functions();
  370. for(const Map<StringName,GDFunction>::Element *E=mf.front();E;E=E->next()) {
  371. const GDFunction &func=E->get();
  372. const int *code = func.get_code();
  373. int codelen=func.get_code_size();
  374. String defargs;
  375. if (func.get_default_argument_count()) {
  376. defargs="defarg at: ";
  377. for(int i=0;i<func.get_default_argument_count();i++) {
  378. if (i>0)
  379. defargs+=",";
  380. defargs+=itos(func.get_default_argument_addr(i));
  381. }
  382. defargs+=" ";
  383. }
  384. print_line("== function "+String(func.get_name())+"() :: stack size: "+itos(func.get_max_stack_size())+" "+defargs+"==");
  385. #define DADDR(m_ip) (_disassemble_addr(p_class,func,code[ip+m_ip]))
  386. for(int ip=0;ip<codelen;) {
  387. int incr=0;
  388. String txt=itos(ip)+" ";
  389. switch(code[ip]) {
  390. case GDFunction::OPCODE_OPERATOR: {
  391. int op = code[ip+1];
  392. txt+="op ";
  393. String opname = Variant::get_operator_name(Variant::Operator(op));
  394. txt+=DADDR(4);
  395. txt+=" = ";
  396. txt+=DADDR(2);
  397. txt+=" "+opname+" ";
  398. txt+=DADDR(3);
  399. incr+=5;
  400. } break;
  401. case GDFunction::OPCODE_SET: {
  402. txt+="set ";
  403. txt+=DADDR(1);
  404. txt+="[";
  405. txt+=DADDR(2);
  406. txt+="]=";
  407. txt+=DADDR(3);
  408. incr+=4;
  409. } break;
  410. case GDFunction::OPCODE_GET: {
  411. txt+=" get ";
  412. txt+=DADDR(3);
  413. txt+="=";
  414. txt+=DADDR(1);
  415. txt+="[";
  416. txt+=DADDR(2);
  417. txt+="]";
  418. incr+=4;
  419. } break;
  420. case GDFunction::OPCODE_SET_NAMED: {
  421. txt+=" set_named ";
  422. txt+=DADDR(1);
  423. txt+="[\"";
  424. txt+=func.get_global_name(code[ip+2]);
  425. txt+="\"]=";
  426. txt+=DADDR(3);
  427. incr+=4;
  428. } break;
  429. case GDFunction::OPCODE_GET_NAMED: {
  430. txt+=" get_named ";
  431. txt+=DADDR(3);
  432. txt+="=";
  433. txt+=DADDR(1);
  434. txt+="[\"";
  435. txt+=func.get_global_name(code[ip+2]);
  436. txt+="\"]";
  437. incr+=4;
  438. } break;
  439. case GDFunction::OPCODE_ASSIGN: {
  440. txt+=" assign ";
  441. txt+=DADDR(1);
  442. txt+="=";
  443. txt+=DADDR(2);
  444. incr+=3;
  445. } break;
  446. case GDFunction::OPCODE_ASSIGN_TRUE: {
  447. txt+=" assign ";
  448. txt+=DADDR(1);
  449. txt+="= true";
  450. incr+=2;
  451. } break;
  452. case GDFunction::OPCODE_ASSIGN_FALSE: {
  453. txt+=" assign ";
  454. txt+=DADDR(1);
  455. txt+="= false";
  456. incr+=2;
  457. } break;
  458. case GDFunction::OPCODE_CONSTRUCT: {
  459. Variant::Type t=Variant::Type(code[ip+1]);
  460. int argc=code[ip+2];
  461. txt+=" construct ";
  462. txt+=DADDR(3+argc);
  463. txt+=" = ";
  464. txt+=Variant::get_type_name(t)+"(";
  465. for(int i=0;i<argc;i++) {
  466. if (i>0)
  467. txt+=", ";
  468. txt+=DADDR(i+3);
  469. }
  470. txt+=")";
  471. incr=4+argc;
  472. } break;
  473. case GDFunction::OPCODE_CONSTRUCT_ARRAY: {
  474. int argc=code[ip+1];
  475. txt+=" make_array ";
  476. txt+=DADDR(2+argc);
  477. txt+=" = [ ";
  478. for(int i=0;i<argc;i++) {
  479. if (i>0)
  480. txt+=", ";
  481. txt+=DADDR(2+i);
  482. }
  483. txt+="]";
  484. incr+=3+argc;
  485. } break;
  486. case GDFunction::OPCODE_CONSTRUCT_DICTIONARY: {
  487. int argc=code[ip+1];
  488. txt+=" make_dict ";
  489. txt+=DADDR(2+argc*2);
  490. txt+=" = { ";
  491. for(int i=0;i<argc;i++) {
  492. if (i>0)
  493. txt+=", ";
  494. txt+=DADDR(2+i*2+0);
  495. txt+=":";
  496. txt+=DADDR(2+i*2+1);
  497. }
  498. txt+="}";
  499. incr+=3+argc*2;
  500. } break;
  501. case GDFunction::OPCODE_CALL:
  502. case GDFunction::OPCODE_CALL_RETURN: {
  503. bool ret=code[ip]==GDFunction::OPCODE_CALL_RETURN;
  504. if (ret)
  505. txt+=" call-ret ";
  506. else
  507. txt+=" call ";
  508. int argc=code[ip+1];
  509. if (ret) {
  510. txt+=DADDR(4+argc)+"=";
  511. }
  512. txt+=DADDR(2)+".";
  513. txt+=String(func.get_global_name(code[ip+3]));
  514. txt+="(";
  515. for(int i=0;i<argc;i++) {
  516. if (i>0)
  517. txt+=", ";
  518. txt+=DADDR(4+i);
  519. }
  520. txt+=")";
  521. incr=5+argc;
  522. } break;
  523. case GDFunction::OPCODE_CALL_BUILT_IN: {
  524. txt+=" call-built-in ";
  525. int argc=code[ip+2];
  526. txt+=DADDR(3+argc)+"=";
  527. txt+=GDFunctions::get_func_name(GDFunctions::Function(code[ip+1]));
  528. txt+="(";
  529. for(int i=0;i<argc;i++) {
  530. if (i>0)
  531. txt+=", ";
  532. txt+=DADDR(3+i);
  533. }
  534. txt+=")";
  535. incr=4+argc;
  536. } break;
  537. case GDFunction::OPCODE_CALL_SELF_BASE: {
  538. txt+=" call-self-base ";
  539. int argc=code[ip+2];
  540. txt+=DADDR(3+argc)+"=";
  541. txt+=func.get_global_name(code[ip+1]);
  542. txt+="(";
  543. for(int i=0;i<argc;i++) {
  544. if (i>0)
  545. txt+=", ";
  546. txt+=DADDR(3+i);
  547. }
  548. txt+=")";
  549. incr=4+argc;
  550. } break;
  551. case GDFunction::OPCODE_JUMP: {
  552. txt+=" jump ";
  553. txt+=itos(code[ip+1]);
  554. incr=2;
  555. } break;
  556. case GDFunction::OPCODE_JUMP_IF: {
  557. txt+=" jump-if ";
  558. txt+=DADDR(1);
  559. txt+=" to ";
  560. txt+=itos(code[ip+2]);
  561. incr=3;
  562. } break;
  563. case GDFunction::OPCODE_JUMP_IF_NOT: {
  564. txt+=" jump-if-not ";
  565. txt+=DADDR(1);
  566. txt+=" to ";
  567. txt+=itos(code[ip+2]);
  568. incr=3;
  569. } break;
  570. case GDFunction::OPCODE_JUMP_TO_DEF_ARGUMENT: {
  571. txt+=" jump-to-default-argument ";
  572. incr=1;
  573. } break;
  574. case GDFunction::OPCODE_RETURN: {
  575. txt+=" return ";
  576. txt+=DADDR(1);
  577. incr=2;
  578. } break;
  579. case GDFunction::OPCODE_ITERATE_BEGIN: {
  580. txt+=" for-init "+DADDR(4)+" in "+DADDR(2)+" counter "+DADDR(1)+" end "+itos(code[ip+3]);
  581. incr+=5;
  582. } break;
  583. case GDFunction::OPCODE_ITERATE: {
  584. txt+=" for-loop "+DADDR(4)+" in "+DADDR(2)+" counter "+DADDR(1)+" end "+itos(code[ip+3]);
  585. incr+=5;
  586. } break;
  587. case GDFunction::OPCODE_LINE: {
  588. int line = code[ip+1]-1;
  589. if (line>=0 && line <p_code.size())
  590. txt="\n"+itos(line+1)+": "+p_code[line]+"\n";
  591. else
  592. txt="";
  593. incr+=2;
  594. } break;
  595. case GDFunction::OPCODE_END: {
  596. txt+=" end";
  597. incr+=1;
  598. } break;
  599. }
  600. if (incr==0) {
  601. ERR_EXPLAIN("unhandled opcode: "+itos(code[ip]));
  602. ERR_BREAK(incr==0);
  603. }
  604. ip+=incr;
  605. if (txt!="")
  606. print_line(txt);
  607. }
  608. }
  609. }
  610. MainLoop* test(TestType p_test) {
  611. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  612. if (cmdlargs.empty()) {
  613. //try editor!
  614. return NULL;
  615. }
  616. String test = cmdlargs.back()->get();
  617. FileAccess *fa = FileAccess::open(test,FileAccess::READ);
  618. if (!fa) {
  619. ERR_EXPLAIN("Could not open file: "+test);
  620. ERR_FAIL_V(NULL);
  621. }
  622. Vector<uint8_t> buf;
  623. int flen = fa->get_len();
  624. buf.resize(fa->get_len()+1);
  625. fa->get_buffer(&buf[0],flen);
  626. buf[flen]=0;
  627. String code;
  628. code.parse_utf8((const char*)&buf[0]);
  629. Vector<String> lines;
  630. int last=0;
  631. for(int i=0;i<=code.length();i++) {
  632. if (code[i]=='\n' || code[i]==0) {
  633. lines.push_back(code.substr(last,i-last));
  634. last=i+1;
  635. }
  636. }
  637. if (p_test==TEST_TOKENIZER) {
  638. GDTokenizer tk;
  639. tk.set_code(code);
  640. int line=-1;
  641. while(tk.get_token()!=GDTokenizer::TK_EOF) {
  642. String text;
  643. if (tk.get_token()==GDTokenizer::TK_IDENTIFIER)
  644. text="'"+tk.get_token_identifier()+"' (identifier)";
  645. else if (tk.get_token()==GDTokenizer::TK_CONSTANT) {
  646. Variant c= tk.get_token_constant();
  647. if (c.get_type()==Variant::STRING)
  648. text="\""+String(c)+"\"";
  649. else
  650. text=c;
  651. text=text+" ("+Variant::get_type_name(c.get_type())+" constant)";
  652. } else if (tk.get_token()==GDTokenizer::TK_ERROR)
  653. text="ERROR: "+tk.get_token_error();
  654. else if (tk.get_token()==GDTokenizer::TK_NEWLINE)
  655. text="newline ("+itos(tk.get_token_line())+") + indent: "+itos(tk.get_token_line_indent());
  656. else if (tk.get_token()==GDTokenizer::TK_BUILT_IN_FUNC)
  657. text="'"+String(GDFunctions::get_func_name(tk.get_token_built_in_func()))+"' (built-in function)";
  658. else
  659. text=tk.get_token_name(tk.get_token());
  660. if (tk.get_token_line()!=line) {
  661. int from=line+1;
  662. line = tk.get_token_line();;
  663. for(int i=from;i<=line;i++) {
  664. int l=i-1;
  665. if (l>=0 && l<lines.size()) {
  666. print_line("\n"+itos(i)+": "+lines[l]+"\n");
  667. }
  668. }
  669. }
  670. print_line("\t("+itos(tk.get_token_column())+"): "+text);
  671. tk.advance();
  672. }
  673. }
  674. if (p_test==TEST_PARSER) {
  675. GDParser parser;
  676. Error err = parser.parse(code);
  677. if (err) {
  678. print_line("Parse Error:\n"+itos(parser.get_error_line())+":"+itos(parser.get_error_column())+":"+parser.get_error());
  679. memdelete(fa);
  680. return NULL;
  681. }
  682. const GDParser::Node* root = parser.get_parse_tree();
  683. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,NULL);
  684. const GDParser::ClassNode *cnode=static_cast<const GDParser::ClassNode*>(root);
  685. _parser_show_class(cnode,0,lines);
  686. }
  687. if (p_test==TEST_COMPILER) {
  688. GDParser parser;
  689. Error err = parser.parse(code);
  690. if (err) {
  691. print_line("Parse Error:\n"+itos(parser.get_error_line())+":"+itos(parser.get_error_column())+":"+parser.get_error());
  692. memdelete(fa);
  693. return NULL;
  694. }
  695. GDScript *script = memnew( GDScript );
  696. GDCompiler gdc;
  697. err = gdc.compile(&parser,script);
  698. if (err) {
  699. print_line("Compile Error:\n"+itos(gdc.get_error_line())+":"+itos(gdc.get_error_column())+":"+gdc.get_error());
  700. memdelete(script);
  701. return NULL;
  702. }
  703. Ref<GDScript> gds =Ref<GDScript>( script );
  704. Ref<GDScript> current=gds;
  705. while(current.is_valid()) {
  706. print_line("** CLASS **");
  707. _disassemble_class(current,lines);
  708. current=current->get_base();
  709. }
  710. }
  711. #if 0
  712. Parser parser;
  713. Error err = parser.parse(code);
  714. if (err) {
  715. print_line("error:"+itos(parser.get_error_line())+":"+itos(parser.get_error_column())+":"+parser.get_error());
  716. } else {
  717. print_line("Parse O-K!");
  718. }
  719. #endif
  720. memdelete(fa);
  721. return NULL;
  722. }
  723. }
  724. #else
  725. namespace TestGDScript {
  726. MainLoop* test(TestType p_test) {
  727. return NULL;
  728. }
  729. }
  730. #endif