Skip to content

Commit 7fcff5a

Browse files
committed
implemented short-circuiting of conditionals that supports type conversions. This addresses WerWolv#201 feature request.
1 parent 7908b49 commit 7fcff5a

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

lib/source/pl/core/ast/ast_node_mathematical_expression.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,26 @@ namespace pl::core::ast {
6767
};
6868

6969
auto leftNode = this->getLeftOperand()->evaluate(evaluator);
70-
auto rightNode = this->getRightOperand()->evaluate(evaluator);
71-
7270
auto leftLiteral = dynamic_cast<ASTNodeLiteral *>(leftNode.get());
73-
auto rightLiteral = dynamic_cast<ASTNodeLiteral *>(rightNode.get());
71+
if (leftLiteral == nullptr)
72+
throwInvalidOperandError();
7473

75-
if (leftLiteral == nullptr || rightLiteral == nullptr)
74+
if (this->getOperator() == Token::Operator::BoolAnd || this->getOperator() == Token::Operator::BoolOr) {
75+
auto leftBool = std::visit(wolv::util::overloaded {
76+
[](const std::string &value) -> bool { return !value.empty(); },
77+
[this](ptrn::Pattern *const &pattern) -> bool { err::E0004.throwError(fmt::format("Cannot cast value of type '{}' to type 'bool'.", pattern->getTypeName()), {}, this->getLocation()); },
78+
[](auto &&value) -> bool { return value != 0; }
79+
}, leftLiteral->getValue());
80+
81+
if (this->getOperator() == Token::Operator::BoolAnd && !leftBool)
82+
return std::unique_ptr<ASTNode>(new ASTNodeLiteral(false));
83+
if (this->getOperator() == Token::Operator::BoolOr && leftBool)
84+
return std::unique_ptr<ASTNode>(new ASTNodeLiteral(true));
85+
}
86+
87+
auto rightNode = this->getRightOperand()->evaluate(evaluator);
88+
auto rightLiteral = dynamic_cast<ASTNodeLiteral *>(rightNode.get());
89+
if (rightLiteral == nullptr)
7690
throwInvalidOperandError();
7791

7892
auto leftValue = leftLiteral->getValue();

0 commit comments

Comments
 (0)