Skip to content

Commit

Permalink
make interpreter's Conjunction a Compound node (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin authored Jun 26, 2024
1 parent 49f511d commit b82ae20
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/interpreter/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,12 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {
ESAC(False)

CASE(Conjunction)
return execute(shadow.getLhs(), ctxt) && execute(shadow.getRhs(), ctxt);
for (const auto& child : shadow.getChildren()) {
if (!execute(child.get(), ctxt)) {
return false;
}
}
return true;
ESAC(Conjunction)

CASE(Negation)
Expand Down
16 changes: 15 additions & 1 deletion src/interpreter/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,21 @@ NodePtr NodeGenerator::visit_(type_identity<ram::False>, const ram::False& lfals
}

NodePtr NodeGenerator::visit_(type_identity<ram::Conjunction>, const ram::Conjunction& conj) {
return mk<Conjunction>(I_Conjunction, &conj, dispatch(conj.getLHS()), dispatch(conj.getRHS()));
NodePtrVec children;
std::stack<const ram::Node*> dfs;
dfs.push(&conj.getRHS());
dfs.push(&conj.getLHS());
while (!dfs.empty()) {
const ram::Node* term = dfs.top();
dfs.pop();
if (const ram::Conjunction* subconj = as<ram::Conjunction>(term)) {
dfs.push(&subconj->getRHS());
dfs.push(&subconj->getLHS());
} else {
children.emplace_back(std::move(dispatch(*term)));
}
}
return mk<Conjunction>(I_Conjunction, &conj, std::move(children));
}

NodePtr NodeGenerator::visit_(type_identity<ram::Negation>, const ram::Negation& neg) {
Expand Down
7 changes: 5 additions & 2 deletions src/interpreter/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,12 @@ class False : public Node {

/**
* @class Conjunction
*
* It's a compound node so that conjunctions with hundreds of terms
* do not overflow the engine stack with left/right recursion.
*/
class Conjunction : public BinaryNode {
using BinaryNode::BinaryNode;
class Conjunction : public CompoundNode {
using CompoundNode::CompoundNode;
};

/**
Expand Down

0 comments on commit b82ae20

Please sign in to comment.