1
0
mirror of synced 2025-01-18 00:56:49 +01:00

patterns: Move builtin functions to namespace. Add string functions

This commit is contained in:
WerWolv 2021-08-27 09:56:20 +02:00
parent b7003d499c
commit d0aec62997
4 changed files with 171 additions and 133 deletions

View File

@ -16,8 +16,10 @@ namespace hex::plugin::builtin {
void registerPatternLanguageFunctions() {
using namespace hex::lang;
ContentRegistry::PatternLanguageFunctions::Namespace nsStd = { "std" };
{
/* findSequence(occurrenceIndex, byte...) */
ContentRegistry::PatternLanguageFunctions::add("findSequence", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 1, [](auto &ctx, auto params) {
ContentRegistry::PatternLanguageFunctions::add(nsStd, "findSequence", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 1, [](auto &ctx, auto params) {
auto& occurrenceIndex = AS_TYPE(ASTNodeIntegerLiteral, params[0])->getValue();
std::vector<u8> sequence;
for (u32 i = 1; i < params.size(); i++) {
@ -48,7 +50,7 @@ namespace hex::plugin::builtin {
});
/* readUnsigned(address, size) */
ContentRegistry::PatternLanguageFunctions::add("readUnsigned", 2, [](auto &ctx, auto params) {
ContentRegistry::PatternLanguageFunctions::add(nsStd, "readUnsigned", 2, [](auto &ctx, auto params) {
auto address = AS_TYPE(ASTNodeIntegerLiteral, params[0])->getValue();
auto size = AS_TYPE(ASTNodeIntegerLiteral, params[1])->getValue();
@ -74,7 +76,7 @@ namespace hex::plugin::builtin {
});
/* readSigned(address, size) */
ContentRegistry::PatternLanguageFunctions::add("readSigned", 2, [](auto &ctx, auto params) {
ContentRegistry::PatternLanguageFunctions::add(nsStd, "readSigned", 2, [](auto &ctx, auto params) {
auto address = AS_TYPE(ASTNodeIntegerLiteral, params[0])->getValue();
auto size = AS_TYPE(ASTNodeIntegerLiteral, params[1])->getValue();
@ -100,7 +102,7 @@ namespace hex::plugin::builtin {
});
/* assert(condition, message) */
ContentRegistry::PatternLanguageFunctions::add("assert", 2, [](auto &ctx, auto params) {
ContentRegistry::PatternLanguageFunctions::add(nsStd, "assert", 2, [](auto &ctx, auto params) {
auto condition = AS_TYPE(ASTNodeIntegerLiteral, params[0])->getValue();
auto message = AS_TYPE(ASTNodeStringLiteral, params[1])->getString();
@ -111,7 +113,7 @@ namespace hex::plugin::builtin {
});
/* warnAssert(condition, message) */
ContentRegistry::PatternLanguageFunctions::add("warnAssert", 2, [](auto ctx, auto params) {
ContentRegistry::PatternLanguageFunctions::add(nsStd, "warnAssert", 2, [](auto ctx, auto params) {
auto condition = AS_TYPE(ASTNodeIntegerLiteral, params[0])->getValue();
auto message = AS_TYPE(ASTNodeStringLiteral, params[1])->getString();
@ -122,7 +124,7 @@ namespace hex::plugin::builtin {
});
/* print(values...) */
ContentRegistry::PatternLanguageFunctions::add("print", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 0, [](auto &ctx, auto params) {
ContentRegistry::PatternLanguageFunctions::add(nsStd, "print", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 0, [](auto &ctx, auto params) {
std::string message;
for (auto& param : params) {
if (auto integerLiteral = dynamic_cast<ASTNodeIntegerLiteral*>(param); integerLiteral != nullptr) {
@ -152,7 +154,7 @@ namespace hex::plugin::builtin {
});
/* alignTo(alignment, value) */
ContentRegistry::PatternLanguageFunctions::add("alignTo", 2, [](auto &ctx, auto params) -> ASTNode* {
ContentRegistry::PatternLanguageFunctions::add(nsStd, "alignTo", 2, [](auto &ctx, auto params) -> ASTNode* {
auto alignment = AS_TYPE(ASTNodeIntegerLiteral, params[0])->getValue();
auto value = AS_TYPE(ASTNodeIntegerLiteral, params[1])->getValue();
@ -165,9 +167,39 @@ namespace hex::plugin::builtin {
});
/* dataSize() */
ContentRegistry::PatternLanguageFunctions::add("dataSize", ContentRegistry::PatternLanguageFunctions::NoParameters, [](auto &ctx, auto params) -> ASTNode* {
ContentRegistry::PatternLanguageFunctions::add(nsStd, "dataSize", ContentRegistry::PatternLanguageFunctions::NoParameters, [](auto &ctx, auto params) -> ASTNode* {
return new ASTNodeIntegerLiteral(u64(SharedData::currentProvider->getActualSize()));
});
}
ContentRegistry::PatternLanguageFunctions::Namespace nsStdStr = { "std", "str" };
{
/* length(string) */
ContentRegistry::PatternLanguageFunctions::add(nsStdStr, "length", 1, [](auto &ctx, auto params) {
auto string = AS_TYPE(ASTNodeStringLiteral, params[1])->getString();
return new ASTNodeIntegerLiteral(u32(string.length()));
});
/* at(string, index) */
ContentRegistry::PatternLanguageFunctions::add(nsStdStr, "at", 2, [](auto &ctx, auto params) {
auto string = AS_TYPE(ASTNodeStringLiteral, params[0])->getString();
auto index = AS_TYPE(ASTNodeIntegerLiteral, params[1])->getValue();
if (LITERAL_COMPARE(index, index >= string.length() || index < 0))
ctx.getConsole().abortEvaluation("character index out of bounds");
return std::visit([&](auto &&value) { return new ASTNodeIntegerLiteral(char(string[u32(value)])); }, index);
});
/* compare(left, right) */
ContentRegistry::PatternLanguageFunctions::add(nsStdStr, "compare", 2, [](auto &ctx, auto params) {
auto left = AS_TYPE(ASTNodeStringLiteral, params[0])->getString();
auto right = AS_TYPE(ASTNodeStringLiteral, params[1])->getString();
return new ASTNodeIntegerLiteral(bool(left == right));
});
}
}
}

View File

@ -85,12 +85,14 @@ namespace hex {
constexpr static u32 LessParametersThan = 0x4000'0000;
constexpr static u32 NoParameters = 0x0000'0000;
using Namespace = std::vector<std::string>;
struct Function {
u32 parameterCount;
std::function<hex::lang::ASTNode*(hex::lang::Evaluator&, std::vector<hex::lang::ASTNode*>&)> func;
};
static void add(std::string_view name, u32 parameterCount, const std::function<hex::lang::ASTNode*(hex::lang::Evaluator&, std::vector<hex::lang::ASTNode*>&)> &func);
static void add(const Namespace &ns, const std::string &name, u32 parameterCount, const std::function<hex::lang::ASTNode*(hex::lang::Evaluator&, std::vector<hex::lang::ASTNode*>&)> &func);
static std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function>& getEntries();
};

View File

@ -151,8 +151,15 @@ namespace hex {
/* Pattern Language Functions */
void ContentRegistry::PatternLanguageFunctions::add(std::string_view name, u32 parameterCount, const std::function<hex::lang::ASTNode*(hex::lang::Evaluator&, std::vector<hex::lang::ASTNode*>&)> &func) {
getEntries()[name.data()] = Function{ parameterCount, func };
void ContentRegistry::PatternLanguageFunctions::add(const Namespace &ns, const std::string &name, u32 parameterCount, const std::function<hex::lang::ASTNode*(hex::lang::Evaluator&, std::vector<hex::lang::ASTNode*>&)> &func) {
std::string functionName;
for (auto &scope : ns)
functionName += scope + "::";
functionName += name;
getEntries()[functionName] = Function { parameterCount, func };
}
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function>& ContentRegistry::PatternLanguageFunctions::getEntries() {

View File

@ -45,9 +45,6 @@ namespace hex {
paletteIndex = TextEditor::PaletteIndex::Default;
}
else if (TokenizeCStyleIdentifier(inBegin, inEnd, outBegin, outEnd)) {
if (SharedData::patternLanguageFunctions.contains(std::string(outBegin, outEnd - outBegin)))
paletteIndex = TextEditor::PaletteIndex::LineNumber;
else
paletteIndex = TextEditor::PaletteIndex::Identifier;
}
else if (TokenizeCStyleNumber(inBegin, inEnd, outBegin, outEnd))