1
0
mirror of synced 2025-01-09 13:11:38 +01:00

patterns: Updated pattern language

This commit is contained in:
WerWolv 2024-11-30 13:49:27 +01:00
parent a639dddd19
commit 01af2f364c
3 changed files with 42 additions and 1 deletions

@ -1 +1 @@
Subproject commit 173fed3182b2b2d82f952f41cc9d9ce2d19fa2e7 Subproject commit 37b4bc7f2c0c342b6c70e6757d402aceb0d4a35b

View File

@ -450,6 +450,14 @@ namespace hex {
bool dangerous; bool dangerous;
}; };
struct TypeDefinition {
pl::api::Namespace ns;
std::string name;
pl::api::FunctionParameterCount parameterCount;
pl::api::TypeCallback callback;
};
struct Visualizer { struct Visualizer {
pl::api::FunctionParameterCount parameterCount; pl::api::FunctionParameterCount parameterCount;
VisualizerFunctionCallback callback; VisualizerFunctionCallback callback;
@ -459,6 +467,7 @@ namespace hex {
const std::map<std::string, Visualizer>& getInlineVisualizers(); const std::map<std::string, Visualizer>& getInlineVisualizers();
const std::map<std::string, pl::api::PragmaHandler>& getPragmas(); const std::map<std::string, pl::api::PragmaHandler>& getPragmas();
const std::vector<FunctionDefinition>& getFunctions(); const std::vector<FunctionDefinition>& getFunctions();
const std::vector<TypeDefinition>& getTypes();
} }
@ -517,6 +526,20 @@ namespace hex {
const pl::api::FunctionCallback &func const pl::api::FunctionCallback &func
); );
/**
* @brief Adds a new type to the pattern language
* @param ns The namespace of the type
* @param name The name of the type
* @param parameterCount The amount of non-type template parameters the type takes
* @param func The type callback
*/
void addType(
const pl::api::Namespace &ns,
const std::string &name,
pl::api::FunctionParameterCount parameterCount,
const pl::api::TypeCallback &func
);
/** /**
* @brief Adds a new visualizer to the pattern language * @brief Adds a new visualizer to the pattern language
* @note Visualizers are extensions to the [[hex::visualize]] attribute, used to visualize data * @note Visualizers are extensions to the [[hex::visualize]] attribute, used to visualize data

View File

@ -552,6 +552,11 @@ namespace hex {
return *s_functions; return *s_functions;
} }
static AutoReset<std::vector<TypeDefinition>> s_types;
const std::vector<TypeDefinition>& getTypes() {
return *s_types;
}
} }
static std::string getFunctionName(const pl::api::Namespace &ns, const std::string &name) { static std::string getFunctionName(const pl::api::Namespace &ns, const std::string &name) {
@ -605,6 +610,10 @@ namespace hex {
runtime.addFunction(ns, name, paramCount, callback); runtime.addFunction(ns, name, paramCount, callback);
} }
for (const auto &[ns, name, paramCount, callback] : impl::getTypes()) {
runtime.addType(ns, name, paramCount, callback);
}
for (const auto &[name, callback] : impl::getPragmas()) { for (const auto &[name, callback] : impl::getPragmas()) {
runtime.addPragma(name, callback); runtime.addPragma(name, callback);
} }
@ -639,6 +648,15 @@ namespace hex {
}); });
} }
void addType(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::TypeCallback &func) {
log::debug("Registered new pattern language type: {}", getFunctionName(ns, name));
impl::s_types->push_back({
ns, name,
parameterCount, func
});
}
void addVisualizer(const std::string &name, const impl::VisualizerFunctionCallback &function, pl::api::FunctionParameterCount parameterCount) { void addVisualizer(const std::string &name, const impl::VisualizerFunctionCallback &function, pl::api::FunctionParameterCount parameterCount) {
log::debug("Registered new pattern visualizer function: {}", name); log::debug("Registered new pattern visualizer function: {}", name);