1
0
mirror of synced 2024-11-13 18:50:53 +01:00

fix: Always evaluate in/out variables correctly

Fixes #629
This commit is contained in:
WerWolv 2022-08-02 23:36:18 +02:00
parent 6de00b3a6a
commit 1d9e8dbda7
2 changed files with 26 additions and 35 deletions

View File

@ -100,7 +100,6 @@ namespace hex::plugin::builtin {
void drawPatternTooltip(pl::Pattern *pattern);
void loadPatternFile(const std::fs::path &path);
void clearPatterns();
void parsePattern(const std::string &code);
void evaluatePattern(const std::string &code);

View File

@ -345,8 +345,9 @@ namespace hex::plugin::builtin {
if (ImGui::IconButton(ICON_VS_DEBUG_STOP, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarRed)))
runtime.abort();
} else {
if (ImGui::IconButton(ICON_VS_DEBUG_START, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGreen)))
if (ImGui::IconButton(ICON_VS_DEBUG_START, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGreen))) {
this->evaluatePattern(this->m_textEditor.GetText());
}
}
@ -381,7 +382,7 @@ namespace hex::plugin::builtin {
if (this->m_runAutomatically)
this->evaluatePattern(this->m_textEditor.GetText());
else
this->parsePattern(this->m_textEditor.GetText());
std::thread([this, code = this->m_textEditor.GetText()] { this->parsePattern(code); }).detach();
}
}
@ -732,51 +733,40 @@ namespace hex::plugin::builtin {
this->evaluatePattern(code);
this->m_textEditor.SetText(code);
this->parsePattern(code);
std::thread([this, code] { this->parsePattern(code); }).detach();
}
}
void ViewPatternEditor::clearPatterns() {
if (!ImHexApi::Provider::isValid()) return;
ImHexApi::Provider::get()->getPatternLanguageRuntime().reset();
this->parsePattern("");
}
void ViewPatternEditor::parsePattern(const std::string &code) {
this->m_runningParsers++;
std::thread([this, code] {
auto ast = this->m_parserRuntime->parseString(code);
auto ast = this->m_parserRuntime->parseString(code);
this->m_patternVariables.clear();
this->m_patternVariables.clear();
if (ast) {
for (auto &node : *ast) {
if (auto variableDecl = dynamic_cast<pl::ASTNodeVariableDecl *>(node.get())) {
auto type = dynamic_cast<pl::ASTNodeTypeDecl *>(variableDecl->getType().get());
if (type == nullptr) continue;
if (ast) {
for (auto &node : *ast) {
if (auto variableDecl = dynamic_cast<pl::ASTNodeVariableDecl *>(node.get())) {
auto type = dynamic_cast<pl::ASTNodeTypeDecl *>(variableDecl->getType().get());
if (type == nullptr) continue;
auto builtinType = dynamic_cast<pl::ASTNodeBuiltinType *>(type->getType().get());
if (builtinType == nullptr) continue;
auto builtinType = dynamic_cast<pl::ASTNodeBuiltinType *>(type->getType().get());
if (builtinType == nullptr) continue;
PatternVariable variable = {
.inVariable = variableDecl->isInVariable(),
.outVariable = variableDecl->isOutVariable(),
.type = builtinType->getType(),
.value = { }
};
PatternVariable variable = {
.inVariable = variableDecl->isInVariable(),
.outVariable = variableDecl->isOutVariable(),
.type = builtinType->getType(),
.value = { }
};
if (variable.inVariable || variable.outVariable) {
if (!this->m_patternVariables.contains(variableDecl->getName()))
this->m_patternVariables[variableDecl->getName()] = variable;
}
if (variable.inVariable || variable.outVariable) {
if (!this->m_patternVariables.contains(variableDecl->getName()))
this->m_patternVariables[variableDecl->getName()] = variable;
}
}
}
}
this->m_runningParsers--;
}).detach();
this->m_runningParsers--;
}
void ViewPatternEditor::evaluatePattern(const std::string &code) {
@ -793,6 +783,8 @@ namespace hex::plugin::builtin {
for (const auto &[id, name, value, type] : this->m_envVarEntries)
envVars.insert({ name, value });
this->parsePattern(code);
std::map<std::string, pl::Token::Literal> inVariables;
for (auto &[name, variable] : this->m_patternVariables) {
if (variable.inVariable)