mirror of
https://github.com/Redot-Engine/redot-engine.git
synced 2025-12-06 07:17:42 -05:00
Add GDScript disassembler
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user