From a29e3789d249291e5df21b3a8cc564b4075907d7 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 8 Oct 2021 18:47:05 +0200 Subject: [PATCH] patterns: If a main function exists, automatically call it --- .../source/pattern_language/evaluator.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/plugins/libimhex/source/pattern_language/evaluator.cpp b/plugins/libimhex/source/pattern_language/evaluator.cpp index 80656aebf..798c49668 100644 --- a/plugins/libimhex/source/pattern_language/evaluator.cpp +++ b/plugins/libimhex/source/pattern_language/evaluator.cpp @@ -119,6 +119,7 @@ namespace hex::pl { try { pushScope(nullptr, patterns); + for (auto node : ast) { if (dynamic_cast(node)) { ;// Don't create patterns from type declarations @@ -130,8 +131,23 @@ namespace hex::pl { auto newPatterns = node->createPatterns(this); patterns.insert(patterns.end(), newPatterns.begin(), newPatterns.end()); } - } + + if (this->m_customFunctions.contains("main")) { + auto mainFunction = this->m_customFunctions["main"]; + + if (mainFunction.parameterCount > 0) + LogConsole::abortEvaluation("main function may not accept any arguments"); + + auto result = mainFunction.func(this, { }); + if (result) { + auto returnCode = Token::literalToSigned(*result); + + if (returnCode != 0) + LogConsole::abortEvaluation(hex::format("non-success value returned from main: {}", returnCode)); + } + } + popScope(); } catch (const LogConsole::EvaluateError &error) { this->m_console.log(LogConsole::Level::Error, error.second);