diff options
author | George Marques <george@gmarqu.es> | 2020-09-01 18:26:52 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-01 18:26:52 -0300 |
commit | 8e052e1a06942816de113bfc0834eef9f5fc67a2 (patch) | |
tree | 4aa18cbea1399ffa6cda339b5b84771b5b31ee15 /tests/test_gdscript.cpp | |
parent | 23ce1dbfd590341cb9439bfd04fede3f70336d0a (diff) | |
parent | 635c6a0a185e590d041f5fee8297b98ec50b24b4 (diff) | |
download | redot-engine-8e052e1a06942816de113bfc0834eef9f5fc67a2.tar.gz |
Merge pull request #41338 from vnen/gdscript-compiler-abstraction
Add GDScript code generation abstraction
Diffstat (limited to 'tests/test_gdscript.cpp')
-rw-r--r-- | tests/test_gdscript.cpp | 63 |
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."); } |