Skip to content

Commit

Permalink
evaluator: Move literals to use shared_ptrs of patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jun 10, 2023
1 parent b7ab96a commit 16ed50d
Show file tree
Hide file tree
Showing 27 changed files with 128 additions and 143 deletions.
8 changes: 4 additions & 4 deletions lib/include/pl/core/ast/ast_node_array_variable_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace pl::core::ast {

evaluator->setReadOffset(std::visit(wolv::util::overloaded {
[this](const std::string &) -> u64 { err::E0005.throwError("Cannot use string as placement offset.", "Try using a integral value instead.", this); },
[this](ptrn::Pattern *) -> u64 { err::E0005.throwError("Cannot use string as placement offset.", "Try using a integral value instead.", this); },
[this](const std::shared_ptr<ptrn::Pattern>&) -> u64 { err::E0005.throwError("Cannot use string as placement offset.", "Try using a integral value instead.", this); },
[](auto &&offset) -> u64 { return offset; }
}, offset->getValue()));
}
Expand Down Expand Up @@ -122,7 +122,7 @@ namespace pl::core::ast {

auto entryCount = std::visit(wolv::util::overloaded {
[this](const std::string &) -> i128 { err::E0006.throwError("Cannot use string to index array.", "Try using an integral type instead.", this); },
[this](ptrn::Pattern *pattern) -> i128 {err::E0006.throwError(fmt::format("Cannot use custom type '{}' to index array.", pattern->getTypeName()), "Try using an integral type instead.", this); },
[this](const std::shared_ptr<ptrn::Pattern> &pattern) -> i128 {err::E0006.throwError(fmt::format("Cannot use custom type '{}' to index array.", pattern->getTypeName()), "Try using an integral type instead.", this); },
[](auto &&size) -> i128 { return size; }
}, sizeLiteral->getValue());

Expand Down Expand Up @@ -201,7 +201,7 @@ namespace pl::core::ast {
if (auto literal = dynamic_cast<ASTNodeLiteral *>(sizeNode.get()); literal != nullptr) {
entryCount = std::visit(wolv::util::overloaded {
[this](const std::string &) -> i128 { err::E0006.throwError("Cannot use string to index array.", "Try using an integral type instead.", this); },
[this](ptrn::Pattern *pattern) -> i128 {err::E0006.throwError(fmt::format("Cannot use custom type '{}' to index array.", pattern->getTypeName()), "Try using an integral type instead.", this); },
[this](const std::shared_ptr<ptrn::Pattern> &pattern) -> i128 {err::E0006.throwError(fmt::format("Cannot use custom type '{}' to index array.", pattern->getTypeName()), "Try using an integral type instead.", this); },
[](auto &&size) -> i128 { return size; }
}, literal->getValue());
} else if (auto whileStatement = dynamic_cast<ASTNodeWhileStatement *>(sizeNode.get())) {
Expand Down Expand Up @@ -323,7 +323,7 @@ namespace pl::core::ast {
if (auto literal = dynamic_cast<ASTNodeLiteral *>(sizeNode.get()); literal != nullptr) {
auto entryCount = std::visit(wolv::util::overloaded {
[this](const std::string &) -> u128 { err::E0006.throwError("Cannot use string to index array.", "Try using an integral type instead.", this); },
[this](ptrn::Pattern *pattern) -> u128 {err::E0006.throwError(fmt::format("Cannot use custom type '{}' to index array.", pattern->getTypeName()), "Try using an integral type instead.", this); },
[this](const std::shared_ptr<ptrn::Pattern> &pattern) -> u128 {err::E0006.throwError(fmt::format("Cannot use custom type '{}' to index array.", pattern->getTypeName()), "Try using an integral type instead.", this); },
[](auto &&size) -> u128 { return size; }
}, literal->getValue());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace pl::core::ast {
if (auto literalNode = dynamic_cast<ASTNodeLiteral *>(sizeNode.get()); literalNode != nullptr) {
boundsCondition = std::visit(wolv::util::overloaded {
[this](const std::string &) -> u128 { err::E0006.throwError("Cannot use string to index array.", "Try using an integral type instead.", this); },
[this](ptrn::Pattern *pattern) -> u128 {err::E0006.throwError(fmt::format("Cannot use custom type '{}' to index array.", pattern->getTypeName()), "Try using an integral type instead.", this); },
[this](const std::shared_ptr<ptrn::Pattern> &pattern) -> u128 {err::E0006.throwError(fmt::format("Cannot use custom type '{}' to index array.", pattern->getTypeName()), "Try using an integral type instead.", this); },
[](auto &&size) -> u128 { return size; }
}, literalNode->getValue());
} else if (auto whileStatement = dynamic_cast<ASTNodeWhileStatement *>(sizeNode.get()); whileStatement != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion lib/include/pl/core/ast/ast_node_bitfield_field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace pl::core::ast {

u8 bitSize = std::visit(wolv::util::overloaded {
[this](const std::string &) -> u8 { err::E0005.throwError("Cannot use string as bitfield field size.", "Try using a integral value instead.", this->m_size.get()); },
[this](ptrn::Pattern *) -> u8 { err::E0005.throwError("Cannot use string as bitfield field size.", "Try using a integral value instead.", this->m_size.get()); },
[this](const std::shared_ptr<ptrn::Pattern>&) -> u8 { err::E0005.throwError("Cannot use string as bitfield field size.", "Try using a integral value instead.", this->m_size.get()); },
[](auto &&offset) -> u8 { return static_cast<u8>(offset); }
}, literal->getValue());

Expand Down
53 changes: 30 additions & 23 deletions lib/include/pl/core/ast/ast_node_cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace pl::core::ast {
auto value = literal->getValue();

value = std::visit(wolv::util::overloaded {
[&](ptrn::Pattern *value) -> Token::Literal {
[&](const std::shared_ptr<ptrn::Pattern> &value) -> Token::Literal {
if (Token::isInteger(type) && value->getSize() <= Token::getTypeSize(type)) {
u128 result = 0;
evaluator->readData(value->getOffset(), &result, value->getSize(), value->getSection());
Expand All @@ -70,19 +70,26 @@ namespace pl::core::ast {
[](auto &value) -> Token::Literal { return value; }
}, value);

return std::unique_ptr<ASTNode>(std::visit(wolv::util::overloaded {
[&, this](ptrn::Pattern *value) -> ASTNode * { err::E0004.throwError(fmt::format("Cannot cast value of type '{}' to type '{}'.", value->getTypeName(), Token::getTypeName(type)), {}, this); },
[&, this](std::string &value) -> ASTNode * {
if (Token::isUnsigned(type)) {
if (value.size() > sizeof(u128))
err::E0004.throwError(fmt::format("Cannot cast value of type 'str' of size {} to type '{}' of size {}.", value.size(), Token::getTypeName(type), Token::getTypeSize(type)), {}, this);
u128 result = 0;
std::memcpy(&result, value.data(), value.size());
return castValue(value, type, typePattern, evaluator);
}

auto endianAdjustedValue = this->changeEndianess(evaluator, result & hlp::bitmask(Token::getTypeSize(type) * 8), value.size(), typePattern->getEndian());
return new ASTNodeLiteral(endianAdjustedValue);
} else
err::E0004.throwError(fmt::format("Cannot cast value of type 'str' to type '{}'.", Token::getTypeName(type)), {}, this);
private:
std::unique_ptr<ASTNode> castValue(const Token::Literal &literal, Token::ValueType type, const std::shared_ptr<ptrn::Pattern> &typePattern, Evaluator *evaluator) const {
return std::unique_ptr<ASTNode>(std::visit(wolv::util::overloaded {
[&, this](const std::shared_ptr<ptrn::Pattern> &value) -> ASTNode * {
return castValue(value->getValue(), type, typePattern, evaluator).release();
},
[&, this](const std::string &value) -> ASTNode * {
if (Token::isUnsigned(type)) {
if (value.size() > sizeof(u128))
err::E0004.throwError(fmt::format("Cannot cast value of type 'str' of size {} to type '{}' of size {}.", value.size(), Token::getTypeName(type), Token::getTypeSize(type)), {}, this);
u128 result = 0;
std::memcpy(&result, value.data(), value.size());

auto endianAdjustedValue = this->changeEndianess(evaluator, result & hlp::bitmask(Token::getTypeSize(type) * 8), value.size(), typePattern->getEndian());
return new ASTNodeLiteral(endianAdjustedValue);
} else
err::E0004.throwError(fmt::format("Cannot cast value of type 'str' to type '{}'.", Token::getTypeName(type)), {}, this);
},
[&, this](auto &&value) -> ASTNode * {
auto endianAdjustedValue = this->changeEndianess(evaluator, value, typePattern->getSize(), typePattern->getEndian());
Expand Down Expand Up @@ -118,24 +125,24 @@ namespace pl::core::ast {
case Token::ValueType::Boolean:
return new ASTNodeLiteral(bool(endianAdjustedValue));
case Token::ValueType::String:
{
std::string string(sizeof(value), '\x00');
std::memcpy(string.data(), &value, string.size());
{
std::string string(sizeof(value), '\x00');
std::memcpy(string.data(), &value, string.size());

// Remove trailing null bytes
string.erase(string.find('\x00'));
// Remove trailing null bytes
string.erase(string.find('\x00'));

if (typePattern->getEndian() != std::endian::native)
std::reverse(string.begin(), string.end());
if (typePattern->getEndian() != std::endian::native)
std::reverse(string.begin(), string.end());

return new ASTNodeLiteral(string);
}
return new ASTNodeLiteral(string);
}
default:
err::E0004.throwError(fmt::format("Cannot cast value of type '{}' to type '{}'.", typePattern->getTypeName(), Token::getTypeName(type)), {}, this);
}
},
},
value));
literal));
}

private:
Expand Down
9 changes: 3 additions & 6 deletions lib/include/pl/core/ast/ast_node_conditional_statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,14 @@ namespace pl::core::ast {
[](const auto &value) -> FunctionResult {
return value;
},
[evaluator](ptrn::Pattern *pattern) -> FunctionResult {
auto clonedPattern = pattern->clone();
auto result = clonedPattern.get();

[evaluator](const std::shared_ptr<ptrn::Pattern> &pattern) -> FunctionResult {
auto &prevScope = evaluator->getScope(-1);
auto &currScope = evaluator->getScope(0);

prevScope.savedPatterns.push_back(std::move(clonedPattern));
prevScope.savedPatterns.push_back(pattern);
prevScope.heapStartSize = currScope.heapStartSize = evaluator->getHeap().size();

return result;
return pattern;
}
}, result.value());
}
Expand Down
9 changes: 3 additions & 6 deletions lib/include/pl/core/ast/ast_node_control_flow_statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@ namespace pl::core::ast {
[](const auto &value) -> FunctionResult {
return value;
},
[evaluator](ptrn::Pattern *pattern) -> FunctionResult {
auto clonedPattern = pattern->clone();
auto result = clonedPattern.get();

[evaluator](const std::shared_ptr<ptrn::Pattern> &pattern) -> FunctionResult {
auto &prevScope = evaluator->getScope(-1);
auto &currScope = evaluator->getScope(0);

prevScope.savedPatterns.push_back(std::move(clonedPattern));
prevScope.savedPatterns.push_back(pattern);
currScope.heapStartSize = evaluator->getHeap().size();

return result;
return pattern;
}
}, literal->getValue());
}
Expand Down
9 changes: 3 additions & 6 deletions lib/include/pl/core/ast/ast_node_function_definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,14 @@ namespace pl::core::ast {
[](const auto &value) -> FunctionResult {
return value;
},
[ctx](ptrn::Pattern *pattern) -> FunctionResult {
auto clonedPattern = pattern->clone();
auto result = clonedPattern.get();

[ctx](const std::shared_ptr<ptrn::Pattern> &pattern) -> FunctionResult {
auto &prevScope = ctx->getScope(-1);
auto &currScope = ctx->getScope(0);

prevScope.savedPatterns.push_back(std::move(clonedPattern));
prevScope.savedPatterns.push_back(pattern);
prevScope.heapStartSize = currScope.heapStartSize = ctx->getHeap().size();

return result;
return pattern;
}
}, result.value());
}
Expand Down
9 changes: 3 additions & 6 deletions lib/include/pl/core/ast/ast_node_match_statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,14 @@ namespace pl::core::ast {
[](const auto &value) -> FunctionResult {
return value;
},
[evaluator](ptrn::Pattern *pattern) -> FunctionResult {
auto clonedPattern = pattern->clone();
auto result = clonedPattern.get();

[evaluator](const std::shared_ptr<ptrn::Pattern> &pattern) -> FunctionResult {
auto &prevScope = evaluator->getScope(-1);
auto &currScope = evaluator->getScope(0);

prevScope.savedPatterns.push_back(std::move(clonedPattern));
prevScope.savedPatterns.push_back(pattern);
prevScope.heapStartSize = currScope.heapStartSize = evaluator->getHeap().size();

return result;
return pattern;
}
}, result.value());
}
Expand Down
Loading

0 comments on commit 16ed50d

Please sign in to comment.