patterns: Added std::env and ability to pass parameters to patterns from the UI

This commit is contained in:
WerWolv
2021-12-10 11:55:27 +01:00
parent 6a0ad22774
commit 8f2e382c8a
8 changed files with 133 additions and 8 deletions

View File

@@ -14,6 +14,7 @@
#include <optional>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
#include <nfd.hpp>
@@ -227,6 +228,15 @@ namespace hex {
return iter != a.end();
}
template<typename T, typename ... VariantTypes>
T get_or(const std::variant<VariantTypes...> &variant, T alt) {
const T *value = std::get_if<T>(&variant);
if (value == nullptr)
return alt;
else
return *value;
}
namespace scope_guard {
#define SCOPE_GUARD ::hex::scope_guard::ScopeGuardOnExit() + [&]()

View File

@@ -149,6 +149,18 @@ namespace hex::pl {
LogConsole::abortEvaluation("evaluation aborted by user");
}
[[nodiscard]]
std::optional<Token::Literal> getEnvVariable(const std::string &name) const {
if (this->m_envVariables.contains(name))
return this->m_envVariables.at(name);
else
return std::nullopt;
}
void setEnvVariable(const std::string &name, const Token::Literal &value) {
this->m_envVariables[name] = value;
}
private:
void patternCreated();
@@ -173,6 +185,7 @@ namespace hex::pl {
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function> m_customFunctions;
std::vector<ASTNode*> m_customFunctionDefinitions;
std::vector<Token::Literal> m_stack;
std::map<std::string, Token::Literal> m_envVariables;
friend class PatternCreationLimiter;
};

View File

@@ -3,12 +3,14 @@
#include <hex.hpp>
#include <bit>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include <hex/pattern_language/log_console.hpp>
#include <hex/pattern_language/token.hpp>
namespace hex::prv { class Provider; }
@@ -28,8 +30,8 @@ namespace hex::pl {
PatternLanguage();
~PatternLanguage();
std::optional<std::vector<PatternData*>> executeString(prv::Provider *provider, const std::string &string);
std::optional<std::vector<PatternData*>> executeFile(prv::Provider *provider, const std::string &path);
std::optional<std::vector<PatternData*>> executeString(prv::Provider *provider, const std::string &string, const std::map<std::string, Token::Literal> &envVars = { });
std::optional<std::vector<PatternData*>> executeFile(prv::Provider *provider, const std::string &path, const std::map<std::string, Token::Literal> &envVars = { });
void abort();