Skip to content

Commit

Permalink
Optimize AND operator
Browse files Browse the repository at this point in the history
  • Loading branch information
SingularityT3 committed Oct 24, 2023
1 parent 9e74697 commit edaf2d2
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/nodes/eval/logic.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "pch.h"

#include "lexer/tokens.h"
#include "psc/error.h"
#include "psc/types/datatypes.h"
#include "psc/types/types.h"
#include "nodes/eval/logic.h"

Expand All @@ -20,7 +22,15 @@ LogicNode::LogicNode(const Token &token, Node &left, Node &right)
}

std::unique_ptr<NodeResult> LogicNode::evaluate(PSC::Context &ctx) {
PSC::Boolean *res = new PSC::Boolean();

auto leftRes = left.evaluate(ctx);
// Don't check 2nd condition if first is false when using AND
if (token.type == TokenType::AND && leftRes->type == PSC::DataType::BOOLEAN && !leftRes->get<PSC::Boolean>().value) {
*res = false;
return std::make_unique<NodeResult>(res, PSC::DataType::BOOLEAN);
}

auto rightRes = right.evaluate(ctx);

if (leftRes->type != PSC::DataType::BOOLEAN || rightRes->type != PSC::DataType::BOOLEAN) {
Expand All @@ -30,7 +40,6 @@ std::unique_ptr<NodeResult> LogicNode::evaluate(PSC::Context &ctx) {
const PSC::Boolean &leftBool = leftRes->get<PSC::Boolean>();
const PSC::Boolean &rightBool = rightRes->get<PSC::Boolean>();

PSC::Boolean *res = new PSC::Boolean();
switch (token.type) {
case TokenType::AND:
*res = leftBool && rightBool;
Expand Down

0 comments on commit edaf2d2

Please sign in to comment.