test_gdscript.cpp 30 KB

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