summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGeorge Marques <george@gmarqu.es>2020-08-17 10:00:42 -0300
committerGeorge Marques <george@gmarqu.es>2020-09-01 14:36:30 -0300
commit635c6a0a185e590d041f5fee8297b98ec50b24b4 (patch)
treead9443c4de0a8fc22cfe3e48773ab68b050b8fc7 /tests
parent745ca3059dc415907123673492be00e741d51f6c (diff)
Add GDScript disassembler
Diffstat (limited to 'tests')
-rw-r--r--tests/test_gdscript.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/test_gdscript.cpp b/tests/test_gdscript.cpp
index a50311972f..a909f216ee 100644
--- a/tests/test_gdscript.cpp
+++ b/tests/test_gdscript.cpp
@@ -36,8 +36,11 @@
#include "core/string_builder.h"
#include "modules/modules_enabled.gen.h"
+
#ifdef MODULE_GDSCRIPT_ENABLED
+#include "modules/gdscript/gdscript_analyzer.h"
+#include "modules/gdscript/gdscript_compiler.h"
#include "modules/gdscript/gdscript_parser.h"
#include "modules/gdscript/gdscript_tokenizer.h"
@@ -122,6 +125,64 @@ static void test_parser(const String &p_code, const String &p_script_path, const
printer.print_tree(parser);
}
+static void test_compiler(const String &p_code, const String &p_script_path, const Vector<String> &p_lines) {
+ GDScriptParser parser;
+ Error err = parser.parse(p_code, p_script_path, false);
+
+ if (err != OK) {
+ print_line("Error in parser:");
+ const List<GDScriptParser::ParserError> &errors = parser.get_errors();
+ for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) {
+ const GDScriptParser::ParserError &error = E->get();
+ print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
+ }
+ return;
+ }
+
+ GDScriptAnalyzer analyzer(&parser);
+ err = analyzer.analyze();
+
+ if (err != OK) {
+ print_line("Error in analyzer:");
+ const List<GDScriptParser::ParserError> &errors = parser.get_errors();
+ for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) {
+ const GDScriptParser::ParserError &error = E->get();
+ print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message));
+ }
+ return;
+ }
+
+ GDScriptCompiler compiler;
+ Ref<GDScript> script;
+ script.instance();
+ script->set_path(p_script_path);
+
+ err = compiler.compile(&parser, script.ptr(), false);
+
+ if (err) {
+ print_line("Error in compiler:");
+ print_line(vformat("%02d:%02d: %s", compiler.get_error_line(), compiler.get_error_column(), compiler.get_error()));
+ return;
+ }
+
+ for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
+ const GDScriptFunction *func = E->value();
+
+ String signature = "Disassembling " + func->get_name().operator String() + "(";
+ for (int i = 0; i < func->get_argument_count(); i++) {
+ if (i > 0) {
+ signature += ", ";
+ }
+ signature += func->get_argument_name(i);
+ }
+ print_line(signature + ")");
+
+ func->disassemble(p_lines);
+ print_line("");
+ print_line("");
+ }
+}
+
MainLoop *test(TestType p_type) {
List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
@@ -164,6 +225,8 @@ MainLoop *test(TestType p_type) {
test_parser(code, test, lines);
break;
case TEST_COMPILER:
+ test_compiler(code, test, lines);
+ break;
case TEST_BYTECODE:
print_line("Not implemented.");
}