test_gdscript.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*************************************************************************/
  2. /* test_gdscript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "os/file_access.h"
  32. #include "os/main_loop.h"
  33. #include "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_PREINC: {
  156. } break;
  157. case GDScriptParser::OperatorNode::OP_PREDEC: {
  158. } break;
  159. case GDScriptParser::OperatorNode::OP_INC: {
  160. } break;
  161. case GDScriptParser::OperatorNode::OP_DEC: {
  162. } break;
  163. case GDScriptParser::OperatorNode::OP_IN: {
  164. txt = _parser_expr(c_node->arguments[0]) + " in " + _parser_expr(c_node->arguments[1]);
  165. } break;
  166. case GDScriptParser::OperatorNode::OP_EQUAL: {
  167. txt = _parser_expr(c_node->arguments[0]) + "==" + _parser_expr(c_node->arguments[1]);
  168. } break;
  169. case GDScriptParser::OperatorNode::OP_NOT_EQUAL: {
  170. txt = _parser_expr(c_node->arguments[0]) + "!=" + _parser_expr(c_node->arguments[1]);
  171. } break;
  172. case GDScriptParser::OperatorNode::OP_LESS: {
  173. txt = _parser_expr(c_node->arguments[0]) + "<" + _parser_expr(c_node->arguments[1]);
  174. } break;
  175. case GDScriptParser::OperatorNode::OP_LESS_EQUAL: {
  176. txt = _parser_expr(c_node->arguments[0]) + "<=" + _parser_expr(c_node->arguments[1]);
  177. } break;
  178. case GDScriptParser::OperatorNode::OP_GREATER: {
  179. txt = _parser_expr(c_node->arguments[0]) + ">" + _parser_expr(c_node->arguments[1]);
  180. } break;
  181. case GDScriptParser::OperatorNode::OP_GREATER_EQUAL: {
  182. txt = _parser_expr(c_node->arguments[0]) + ">=" + _parser_expr(c_node->arguments[1]);
  183. } break;
  184. case GDScriptParser::OperatorNode::OP_AND: {
  185. txt = _parser_expr(c_node->arguments[0]) + " and " + _parser_expr(c_node->arguments[1]);
  186. } break;
  187. case GDScriptParser::OperatorNode::OP_OR: {
  188. txt = _parser_expr(c_node->arguments[0]) + " or " + _parser_expr(c_node->arguments[1]);
  189. } break;
  190. case GDScriptParser::OperatorNode::OP_ADD: {
  191. txt = _parser_expr(c_node->arguments[0]) + "+" + _parser_expr(c_node->arguments[1]);
  192. } break;
  193. case GDScriptParser::OperatorNode::OP_SUB: {
  194. txt = _parser_expr(c_node->arguments[0]) + "-" + _parser_expr(c_node->arguments[1]);
  195. } break;
  196. case GDScriptParser::OperatorNode::OP_MUL: {
  197. txt = _parser_expr(c_node->arguments[0]) + "*" + _parser_expr(c_node->arguments[1]);
  198. } break;
  199. case GDScriptParser::OperatorNode::OP_DIV: {
  200. txt = _parser_expr(c_node->arguments[0]) + "/" + _parser_expr(c_node->arguments[1]);
  201. } break;
  202. case GDScriptParser::OperatorNode::OP_MOD: {
  203. txt = _parser_expr(c_node->arguments[0]) + "%" + _parser_expr(c_node->arguments[1]);
  204. } break;
  205. case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: {
  206. txt = _parser_expr(c_node->arguments[0]) + "<<" + _parser_expr(c_node->arguments[1]);
  207. } break;
  208. case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: {
  209. txt = _parser_expr(c_node->arguments[0]) + ">>" + _parser_expr(c_node->arguments[1]);
  210. } break;
  211. case GDScriptParser::OperatorNode::OP_ASSIGN: {
  212. txt = _parser_expr(c_node->arguments[0]) + "=" + _parser_expr(c_node->arguments[1]);
  213. } break;
  214. case GDScriptParser::OperatorNode::OP_ASSIGN_ADD: {
  215. txt = _parser_expr(c_node->arguments[0]) + "+=" + _parser_expr(c_node->arguments[1]);
  216. } break;
  217. case GDScriptParser::OperatorNode::OP_ASSIGN_SUB: {
  218. txt = _parser_expr(c_node->arguments[0]) + "-=" + _parser_expr(c_node->arguments[1]);
  219. } break;
  220. case GDScriptParser::OperatorNode::OP_ASSIGN_MUL: {
  221. txt = _parser_expr(c_node->arguments[0]) + "*=" + _parser_expr(c_node->arguments[1]);
  222. } break;
  223. case GDScriptParser::OperatorNode::OP_ASSIGN_DIV: {
  224. txt = _parser_expr(c_node->arguments[0]) + "/=" + _parser_expr(c_node->arguments[1]);
  225. } break;
  226. case GDScriptParser::OperatorNode::OP_ASSIGN_MOD: {
  227. txt = _parser_expr(c_node->arguments[0]) + "%=" + _parser_expr(c_node->arguments[1]);
  228. } break;
  229. case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: {
  230. txt = _parser_expr(c_node->arguments[0]) + "<<=" + _parser_expr(c_node->arguments[1]);
  231. } break;
  232. case GDScriptParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: {
  233. txt = _parser_expr(c_node->arguments[0]) + ">>=" + _parser_expr(c_node->arguments[1]);
  234. } break;
  235. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_AND: {
  236. txt = _parser_expr(c_node->arguments[0]) + "&=" + _parser_expr(c_node->arguments[1]);
  237. } break;
  238. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_OR: {
  239. txt = _parser_expr(c_node->arguments[0]) + "|=" + _parser_expr(c_node->arguments[1]);
  240. } break;
  241. case GDScriptParser::OperatorNode::OP_ASSIGN_BIT_XOR: {
  242. txt = _parser_expr(c_node->arguments[0]) + "^=" + _parser_expr(c_node->arguments[1]);
  243. } break;
  244. case GDScriptParser::OperatorNode::OP_BIT_AND: {
  245. txt = _parser_expr(c_node->arguments[0]) + "&" + _parser_expr(c_node->arguments[1]);
  246. } break;
  247. case GDScriptParser::OperatorNode::OP_BIT_OR: {
  248. txt = _parser_expr(c_node->arguments[0]) + "|" + _parser_expr(c_node->arguments[1]);
  249. } break;
  250. case GDScriptParser::OperatorNode::OP_BIT_XOR: {
  251. txt = _parser_expr(c_node->arguments[0]) + "^" + _parser_expr(c_node->arguments[1]);
  252. } break;
  253. default: {}
  254. }
  255. } break;
  256. case GDScriptParser::Node::TYPE_NEWLINE: {
  257. //skippie
  258. } break;
  259. default: {
  260. String error = "Parser bug at " + itos(p_expr->line) + ", invalid expression type: " + itos(p_expr->type);
  261. ERR_EXPLAIN(error);
  262. ERR_FAIL_V("");
  263. }
  264. }
  265. return txt;
  266. //return "("+txt+")";
  267. }
  268. static void _parser_show_block(const GDScriptParser::BlockNode *p_block, int p_indent) {
  269. for (int i = 0; i < p_block->statements.size(); i++) {
  270. const GDScriptParser::Node *statement = p_block->statements[i];
  271. switch (statement->type) {
  272. case GDScriptParser::Node::TYPE_CONTROL_FLOW: {
  273. const GDScriptParser::ControlFlowNode *cf_node = static_cast<const GDScriptParser::ControlFlowNode *>(statement);
  274. switch (cf_node->cf_type) {
  275. case GDScriptParser::ControlFlowNode::CF_IF: {
  276. ERR_FAIL_COND(cf_node->arguments.size() != 1);
  277. String txt;
  278. txt += "if ";
  279. txt += _parser_expr(cf_node->arguments[0]);
  280. txt += ":";
  281. _print_indent(p_indent, txt);
  282. ERR_FAIL_COND(!cf_node->body);
  283. _parser_show_block(cf_node->body, p_indent + 1);
  284. if (cf_node->body_else) {
  285. _print_indent(p_indent, "else:");
  286. _parser_show_block(cf_node->body_else, p_indent + 1);
  287. }
  288. } break;
  289. case GDScriptParser::ControlFlowNode::CF_FOR: {
  290. ERR_FAIL_COND(cf_node->arguments.size() != 2);
  291. String txt;
  292. txt += "for ";
  293. txt += _parser_expr(cf_node->arguments[0]);
  294. txt += " in ";
  295. txt += _parser_expr(cf_node->arguments[1]);
  296. txt += ":";
  297. _print_indent(p_indent, txt);
  298. ERR_FAIL_COND(!cf_node->body);
  299. _parser_show_block(cf_node->body, p_indent + 1);
  300. } break;
  301. case GDScriptParser::ControlFlowNode::CF_WHILE: {
  302. ERR_FAIL_COND(cf_node->arguments.size() != 1);
  303. String txt;
  304. txt += "while ";
  305. txt += _parser_expr(cf_node->arguments[0]);
  306. txt += ":";
  307. _print_indent(p_indent, txt);
  308. ERR_FAIL_COND(!cf_node->body);
  309. _parser_show_block(cf_node->body, p_indent + 1);
  310. } break;
  311. case GDScriptParser::ControlFlowNode::CF_SWITCH: {
  312. } break;
  313. case GDScriptParser::ControlFlowNode::CF_CONTINUE: {
  314. _print_indent(p_indent, "continue");
  315. } break;
  316. case GDScriptParser::ControlFlowNode::CF_BREAK: {
  317. _print_indent(p_indent, "break");
  318. } break;
  319. case GDScriptParser::ControlFlowNode::CF_RETURN: {
  320. if (cf_node->arguments.size())
  321. _print_indent(p_indent, "return " + _parser_expr(cf_node->arguments[0]));
  322. else
  323. _print_indent(p_indent, "return ");
  324. } break;
  325. }
  326. } break;
  327. case GDScriptParser::Node::TYPE_LOCAL_VAR: {
  328. const GDScriptParser::LocalVarNode *lv_node = static_cast<const GDScriptParser::LocalVarNode *>(statement);
  329. _print_indent(p_indent, "var " + String(lv_node->name));
  330. } break;
  331. default: {
  332. //expression i guess
  333. _print_indent(p_indent, _parser_expr(statement));
  334. }
  335. }
  336. }
  337. }
  338. static void _parser_show_function(const GDScriptParser::FunctionNode *p_func, int p_indent, GDScriptParser::BlockNode *p_initializer = NULL) {
  339. String txt;
  340. if (p_func->_static)
  341. txt = "static ";
  342. txt += "func ";
  343. if (p_func->name == "") // initializer
  344. txt += "[built-in-initializer]";
  345. else
  346. txt += String(p_func->name);
  347. txt += "(";
  348. for (int i = 0; i < p_func->arguments.size(); i++) {
  349. if (i != 0)
  350. txt += ", ";
  351. txt += "var " + String(p_func->arguments[i]);
  352. if (i >= (p_func->arguments.size() - p_func->default_values.size())) {
  353. int defarg = i - (p_func->arguments.size() - p_func->default_values.size());
  354. txt += "=";
  355. txt += _parser_expr(p_func->default_values[defarg]);
  356. }
  357. }
  358. txt += ")";
  359. //todo constructor check!
  360. txt += ":";
  361. _print_indent(p_indent, txt);
  362. if (p_initializer)
  363. _parser_show_block(p_initializer, p_indent + 1);
  364. _parser_show_block(p_func->body, p_indent + 1);
  365. }
  366. static void _parser_show_class(const GDScriptParser::ClassNode *p_class, int p_indent, const Vector<String> &p_code) {
  367. if (p_indent == 0 && (String(p_class->extends_file) != "" || p_class->extends_class.size())) {
  368. _print_indent(p_indent, _parser_extends(p_class));
  369. print_line("\n");
  370. }
  371. for (int i = 0; i < p_class->subclasses.size(); i++) {
  372. const GDScriptParser::ClassNode *subclass = p_class->subclasses[i];
  373. String line = "class " + subclass->name;
  374. if (String(subclass->extends_file) != "" || subclass->extends_class.size())
  375. line += " " + _parser_extends(subclass);
  376. line += ":";
  377. _print_indent(p_indent, line);
  378. _parser_show_class(subclass, p_indent + 1, p_code);
  379. print_line("\n");
  380. }
  381. for (int i = 0; i < p_class->constant_expressions.size(); i++) {
  382. const GDScriptParser::ClassNode::Constant &constant = p_class->constant_expressions[i];
  383. _print_indent(p_indent, "const " + String(constant.identifier) + "=" + _parser_expr(constant.expression));
  384. }
  385. for (int i = 0; i < p_class->variables.size(); i++) {
  386. const GDScriptParser::ClassNode::Member &m = p_class->variables[i];
  387. _print_indent(p_indent, "var " + String(m.identifier));
  388. }
  389. print_line("\n");
  390. for (int i = 0; i < p_class->static_functions.size(); i++) {
  391. _parser_show_function(p_class->static_functions[i], p_indent);
  392. print_line("\n");
  393. }
  394. for (int i = 0; i < p_class->functions.size(); i++) {
  395. if (String(p_class->functions[i]->name) == "_init") {
  396. _parser_show_function(p_class->functions[i], p_indent, p_class->initializer);
  397. } else
  398. _parser_show_function(p_class->functions[i], p_indent);
  399. print_line("\n");
  400. }
  401. //_parser_show_function(p_class->initializer,p_indent);
  402. print_line("\n");
  403. }
  404. static String _disassemble_addr(const Ref<GDScript> &p_script, const GDScriptFunction &func, int p_addr) {
  405. int addr = p_addr & GDScriptFunction::ADDR_MASK;
  406. switch (p_addr >> GDScriptFunction::ADDR_BITS) {
  407. case GDScriptFunction::ADDR_TYPE_SELF: {
  408. return "self";
  409. } break;
  410. case GDScriptFunction::ADDR_TYPE_CLASS: {
  411. return "class";
  412. } break;
  413. case GDScriptFunction::ADDR_TYPE_MEMBER: {
  414. return "member(" + p_script->debug_get_member_by_index(addr) + ")";
  415. } break;
  416. case GDScriptFunction::ADDR_TYPE_CLASS_CONSTANT: {
  417. return "class_const(" + func.get_global_name(addr) + ")";
  418. } break;
  419. case GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT: {
  420. Variant v = func.get_constant(addr);
  421. String txt;
  422. if (v.get_type() == Variant::STRING || v.get_type() == Variant::NODE_PATH)
  423. txt = "\"" + String(v) + "\"";
  424. else
  425. txt = v;
  426. return "const(" + txt + ")";
  427. } break;
  428. case GDScriptFunction::ADDR_TYPE_STACK: {
  429. return "stack(" + itos(addr) + ")";
  430. } break;
  431. case GDScriptFunction::ADDR_TYPE_STACK_VARIABLE: {
  432. return "var_stack(" + itos(addr) + ")";
  433. } break;
  434. case GDScriptFunction::ADDR_TYPE_GLOBAL: {
  435. return "global(" + func.get_global_name(addr) + ")";
  436. } break;
  437. case GDScriptFunction::ADDR_TYPE_NIL: {
  438. return "nil";
  439. } break;
  440. }
  441. return "<err>";
  442. }
  443. static void _disassemble_class(const Ref<GDScript> &p_class, const Vector<String> &p_code) {
  444. const Map<StringName, GDScriptFunction *> &mf = p_class->debug_get_member_functions();
  445. for (const Map<StringName, GDScriptFunction *>::Element *E = mf.front(); E; E = E->next()) {
  446. const GDScriptFunction &func = *E->get();
  447. const int *code = func.get_code();
  448. int codelen = func.get_code_size();
  449. String defargs;
  450. if (func.get_default_argument_count()) {
  451. defargs = "defarg at: ";
  452. for (int i = 0; i < func.get_default_argument_count(); i++) {
  453. if (i > 0)
  454. defargs += ",";
  455. defargs += itos(func.get_default_argument_addr(i));
  456. }
  457. defargs += " ";
  458. }
  459. print_line("== function " + String(func.get_name()) + "() :: stack size: " + itos(func.get_max_stack_size()) + " " + defargs + "==");
  460. #define DADDR(m_ip) (_disassemble_addr(p_class, func, code[ip + m_ip]))
  461. for (int ip = 0; ip < codelen;) {
  462. int incr = 0;
  463. String txt = itos(ip) + " ";
  464. switch (code[ip]) {
  465. case GDScriptFunction::OPCODE_OPERATOR: {
  466. int op = code[ip + 1];
  467. txt += "op ";
  468. String opname = Variant::get_operator_name(Variant::Operator(op));
  469. txt += DADDR(4);
  470. txt += " = ";
  471. txt += DADDR(2);
  472. txt += " " + opname + " ";
  473. txt += DADDR(3);
  474. incr += 5;
  475. } break;
  476. case GDScriptFunction::OPCODE_SET: {
  477. txt += "set ";
  478. txt += DADDR(1);
  479. txt += "[";
  480. txt += DADDR(2);
  481. txt += "]=";
  482. txt += DADDR(3);
  483. incr += 4;
  484. } break;
  485. case GDScriptFunction::OPCODE_GET: {
  486. txt += " get ";
  487. txt += DADDR(3);
  488. txt += "=";
  489. txt += DADDR(1);
  490. txt += "[";
  491. txt += DADDR(2);
  492. txt += "]";
  493. incr += 4;
  494. } break;
  495. case GDScriptFunction::OPCODE_SET_NAMED: {
  496. txt += " set_named ";
  497. txt += DADDR(1);
  498. txt += "[\"";
  499. txt += func.get_global_name(code[ip + 2]);
  500. txt += "\"]=";
  501. txt += DADDR(3);
  502. incr += 4;
  503. } break;
  504. case GDScriptFunction::OPCODE_GET_NAMED: {
  505. txt += " get_named ";
  506. txt += DADDR(3);
  507. txt += "=";
  508. txt += DADDR(1);
  509. txt += "[\"";
  510. txt += func.get_global_name(code[ip + 2]);
  511. txt += "\"]";
  512. incr += 4;
  513. } break;
  514. case GDScriptFunction::OPCODE_SET_MEMBER: {
  515. txt += " set_member ";
  516. txt += "[\"";
  517. txt += func.get_global_name(code[ip + 1]);
  518. txt += "\"]=";
  519. txt += DADDR(2);
  520. incr += 3;
  521. } break;
  522. case GDScriptFunction::OPCODE_GET_MEMBER: {
  523. txt += " get_member ";
  524. txt += DADDR(2);
  525. txt += "=";
  526. txt += "[\"";
  527. txt += func.get_global_name(code[ip + 1]);
  528. txt += "\"]";
  529. incr += 3;
  530. } break;
  531. case GDScriptFunction::OPCODE_ASSIGN: {
  532. txt += " assign ";
  533. txt += DADDR(1);
  534. txt += "=";
  535. txt += DADDR(2);
  536. incr += 3;
  537. } break;
  538. case GDScriptFunction::OPCODE_ASSIGN_TRUE: {
  539. txt += " assign ";
  540. txt += DADDR(1);
  541. txt += "= true";
  542. incr += 2;
  543. } break;
  544. case GDScriptFunction::OPCODE_ASSIGN_FALSE: {
  545. txt += " assign ";
  546. txt += DADDR(1);
  547. txt += "= false";
  548. incr += 2;
  549. } break;
  550. case GDScriptFunction::OPCODE_CONSTRUCT: {
  551. Variant::Type t = Variant::Type(code[ip + 1]);
  552. int argc = code[ip + 2];
  553. txt += " construct ";
  554. txt += DADDR(3 + argc);
  555. txt += " = ";
  556. txt += Variant::get_type_name(t) + "(";
  557. for (int i = 0; i < argc; i++) {
  558. if (i > 0)
  559. txt += ", ";
  560. txt += DADDR(i + 3);
  561. }
  562. txt += ")";
  563. incr = 4 + argc;
  564. } break;
  565. case GDScriptFunction::OPCODE_CONSTRUCT_ARRAY: {
  566. int argc = code[ip + 1];
  567. txt += " make_array ";
  568. txt += DADDR(2 + argc);
  569. txt += " = [ ";
  570. for (int i = 0; i < argc; i++) {
  571. if (i > 0)
  572. txt += ", ";
  573. txt += DADDR(2 + i);
  574. }
  575. txt += "]";
  576. incr += 3 + argc;
  577. } break;
  578. case GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY: {
  579. int argc = code[ip + 1];
  580. txt += " make_dict ";
  581. txt += DADDR(2 + argc * 2);
  582. txt += " = { ";
  583. for (int i = 0; i < argc; i++) {
  584. if (i > 0)
  585. txt += ", ";
  586. txt += DADDR(2 + i * 2 + 0);
  587. txt += ":";
  588. txt += DADDR(2 + i * 2 + 1);
  589. }
  590. txt += "}";
  591. incr += 3 + argc * 2;
  592. } break;
  593. case GDScriptFunction::OPCODE_CALL:
  594. case GDScriptFunction::OPCODE_CALL_RETURN: {
  595. bool ret = code[ip] == GDScriptFunction::OPCODE_CALL_RETURN;
  596. if (ret)
  597. txt += " call-ret ";
  598. else
  599. txt += " call ";
  600. int argc = code[ip + 1];
  601. if (ret) {
  602. txt += DADDR(4 + argc) + "=";
  603. }
  604. txt += DADDR(2) + ".";
  605. txt += String(func.get_global_name(code[ip + 3]));
  606. txt += "(";
  607. for (int i = 0; i < argc; i++) {
  608. if (i > 0)
  609. txt += ", ";
  610. txt += DADDR(4 + i);
  611. }
  612. txt += ")";
  613. incr = 5 + argc;
  614. } break;
  615. case GDScriptFunction::OPCODE_CALL_BUILT_IN: {
  616. txt += " call-built-in ";
  617. int argc = code[ip + 2];
  618. txt += DADDR(3 + argc) + "=";
  619. txt += GDScriptFunctions::get_func_name(GDScriptFunctions::Function(code[ip + 1]));
  620. txt += "(";
  621. for (int i = 0; i < argc; i++) {
  622. if (i > 0)
  623. txt += ", ";
  624. txt += DADDR(3 + i);
  625. }
  626. txt += ")";
  627. incr = 4 + argc;
  628. } break;
  629. case GDScriptFunction::OPCODE_CALL_SELF_BASE: {
  630. txt += " call-self-base ";
  631. int argc = code[ip + 2];
  632. txt += DADDR(3 + argc) + "=";
  633. txt += func.get_global_name(code[ip + 1]);
  634. txt += "(";
  635. for (int i = 0; i < argc; i++) {
  636. if (i > 0)
  637. txt += ", ";
  638. txt += DADDR(3 + i);
  639. }
  640. txt += ")";
  641. incr = 4 + argc;
  642. } break;
  643. case GDScriptFunction::OPCODE_YIELD: {
  644. txt += " yield ";
  645. incr = 1;
  646. } break;
  647. case GDScriptFunction::OPCODE_YIELD_SIGNAL: {
  648. txt += " yield_signal ";
  649. txt += DADDR(1);
  650. txt += ",";
  651. txt += DADDR(2);
  652. incr = 3;
  653. } break;
  654. case GDScriptFunction::OPCODE_YIELD_RESUME: {
  655. txt += " yield resume: ";
  656. txt += DADDR(1);
  657. incr = 2;
  658. } break;
  659. case GDScriptFunction::OPCODE_JUMP: {
  660. txt += " jump ";
  661. txt += itos(code[ip + 1]);
  662. incr = 2;
  663. } break;
  664. case GDScriptFunction::OPCODE_JUMP_IF: {
  665. txt += " jump-if ";
  666. txt += DADDR(1);
  667. txt += " to ";
  668. txt += itos(code[ip + 2]);
  669. incr = 3;
  670. } break;
  671. case GDScriptFunction::OPCODE_JUMP_IF_NOT: {
  672. txt += " jump-if-not ";
  673. txt += DADDR(1);
  674. txt += " to ";
  675. txt += itos(code[ip + 2]);
  676. incr = 3;
  677. } break;
  678. case GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT: {
  679. txt += " jump-to-default-argument ";
  680. incr = 1;
  681. } break;
  682. case GDScriptFunction::OPCODE_RETURN: {
  683. txt += " return ";
  684. txt += DADDR(1);
  685. incr = 2;
  686. } break;
  687. case GDScriptFunction::OPCODE_ITERATE_BEGIN: {
  688. txt += " for-init " + DADDR(4) + " in " + DADDR(2) + " counter " + DADDR(1) + " end " + itos(code[ip + 3]);
  689. incr += 5;
  690. } break;
  691. case GDScriptFunction::OPCODE_ITERATE: {
  692. txt += " for-loop " + DADDR(4) + " in " + DADDR(2) + " counter " + DADDR(1) + " end " + itos(code[ip + 3]);
  693. incr += 5;
  694. } break;
  695. case GDScriptFunction::OPCODE_LINE: {
  696. int line = code[ip + 1] - 1;
  697. if (line >= 0 && line < p_code.size())
  698. txt = "\n" + itos(line + 1) + ": " + p_code[line] + "\n";
  699. else
  700. txt = "";
  701. incr += 2;
  702. } break;
  703. case GDScriptFunction::OPCODE_END: {
  704. txt += " end";
  705. incr += 1;
  706. } break;
  707. case GDScriptFunction::OPCODE_ASSERT: {
  708. txt += " assert ";
  709. txt += DADDR(1);
  710. incr += 2;
  711. } break;
  712. }
  713. if (incr == 0) {
  714. ERR_EXPLAIN("unhandled opcode: " + itos(code[ip]));
  715. ERR_BREAK(incr == 0);
  716. }
  717. ip += incr;
  718. if (txt != "")
  719. print_line(txt);
  720. }
  721. }
  722. }
  723. MainLoop *test(TestType p_type) {
  724. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  725. if (cmdlargs.empty()) {
  726. //try editor!
  727. return NULL;
  728. }
  729. String test = cmdlargs.back()->get();
  730. FileAccess *fa = FileAccess::open(test, FileAccess::READ);
  731. if (!fa) {
  732. ERR_EXPLAIN("Could not open file: " + test);
  733. ERR_FAIL_V(NULL);
  734. }
  735. Vector<uint8_t> buf;
  736. int flen = fa->get_len();
  737. buf.resize(fa->get_len() + 1);
  738. fa->get_buffer(&buf[0], flen);
  739. buf[flen] = 0;
  740. String code;
  741. code.parse_utf8((const char *)&buf[0]);
  742. Vector<String> lines;
  743. int last = 0;
  744. for (int i = 0; i <= code.length(); i++) {
  745. if (code[i] == '\n' || code[i] == 0) {
  746. lines.push_back(code.substr(last, i - last));
  747. last = i + 1;
  748. }
  749. }
  750. if (p_type == TEST_TOKENIZER) {
  751. GDScriptTokenizerText tk;
  752. tk.set_code(code);
  753. int line = -1;
  754. while (tk.get_token() != GDScriptTokenizer::TK_EOF) {
  755. String text;
  756. if (tk.get_token() == GDScriptTokenizer::TK_IDENTIFIER)
  757. text = "'" + tk.get_token_identifier() + "' (identifier)";
  758. else if (tk.get_token() == GDScriptTokenizer::TK_CONSTANT) {
  759. Variant c = tk.get_token_constant();
  760. if (c.get_type() == Variant::STRING)
  761. text = "\"" + String(c) + "\"";
  762. else
  763. text = c;
  764. text = text + " (" + Variant::get_type_name(c.get_type()) + " constant)";
  765. } else if (tk.get_token() == GDScriptTokenizer::TK_ERROR)
  766. text = "ERROR: " + tk.get_token_error();
  767. else if (tk.get_token() == GDScriptTokenizer::TK_NEWLINE)
  768. text = "newline (" + itos(tk.get_token_line()) + ") + indent: " + itos(tk.get_token_line_indent());
  769. else if (tk.get_token() == GDScriptTokenizer::TK_BUILT_IN_FUNC)
  770. text = "'" + String(GDScriptFunctions::get_func_name(tk.get_token_built_in_func())) + "' (built-in function)";
  771. else
  772. text = tk.get_token_name(tk.get_token());
  773. if (tk.get_token_line() != line) {
  774. int from = line + 1;
  775. line = tk.get_token_line();
  776. for (int i = from; i <= line; i++) {
  777. int l = i - 1;
  778. if (l >= 0 && l < lines.size()) {
  779. print_line("\n" + itos(i) + ": " + lines[l] + "\n");
  780. }
  781. }
  782. }
  783. print_line("\t(" + itos(tk.get_token_column()) + "): " + text);
  784. tk.advance();
  785. }
  786. }
  787. if (p_type == TEST_PARSER) {
  788. GDScriptParser parser;
  789. Error err = parser.parse(code);
  790. if (err) {
  791. print_line("Parse Error:\n" + itos(parser.get_error_line()) + ":" + itos(parser.get_error_column()) + ":" + parser.get_error());
  792. memdelete(fa);
  793. return NULL;
  794. }
  795. const GDScriptParser::Node *root = parser.get_parse_tree();
  796. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, NULL);
  797. const GDScriptParser::ClassNode *cnode = static_cast<const GDScriptParser::ClassNode *>(root);
  798. _parser_show_class(cnode, 0, lines);
  799. }
  800. if (p_type == TEST_COMPILER) {
  801. GDScriptParser parser;
  802. Error err = parser.parse(code);
  803. if (err) {
  804. print_line("Parse Error:\n" + itos(parser.get_error_line()) + ":" + itos(parser.get_error_column()) + ":" + parser.get_error());
  805. memdelete(fa);
  806. return NULL;
  807. }
  808. GDScript *script = memnew(GDScript);
  809. GDScriptCompiler gdc;
  810. err = gdc.compile(&parser, script);
  811. if (err) {
  812. print_line("Compile Error:\n" + itos(gdc.get_error_line()) + ":" + itos(gdc.get_error_column()) + ":" + gdc.get_error());
  813. memdelete(script);
  814. return NULL;
  815. }
  816. Ref<GDScript> gds = Ref<GDScript>(script);
  817. Ref<GDScript> current = gds;
  818. while (current.is_valid()) {
  819. print_line("** CLASS **");
  820. _disassemble_class(current, lines);
  821. current = current->get_base();
  822. }
  823. } else if (p_type == TEST_BYTECODE) {
  824. Vector<uint8_t> buf = GDScriptTokenizerBuffer::parse_code_string(code);
  825. String dst = test.get_basename() + ".gdc";
  826. FileAccess *fw = FileAccess::open(dst, FileAccess::WRITE);
  827. fw->store_buffer(buf.ptr(), buf.size());
  828. memdelete(fw);
  829. }
  830. memdelete(fa);
  831. return NULL;
  832. }
  833. } // namespace TestGDScript
  834. #else
  835. namespace TestGDScript {
  836. MainLoop *test(TestType p_type) {
  837. return NULL;
  838. }
  839. } // namespace TestGDScript
  840. #endif