test_gdscript.cpp 31 KB

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