Skip to content

Commit b82ae20

Browse files
authored
make interpreter's Conjunction a Compound node (#2459)
1 parent 49f511d commit b82ae20

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/interpreter/Engine.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,12 @@ RamDomain Engine::execute(const Node* node, Context& ctxt) {
982982
ESAC(False)
983983

984984
CASE(Conjunction)
985-
return execute(shadow.getLhs(), ctxt) && execute(shadow.getRhs(), ctxt);
985+
for (const auto& child : shadow.getChildren()) {
986+
if (!execute(child.get(), ctxt)) {
987+
return false;
988+
}
989+
}
990+
return true;
986991
ESAC(Conjunction)
987992

988993
CASE(Negation)

src/interpreter/Generator.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,21 @@ NodePtr NodeGenerator::visit_(type_identity<ram::False>, const ram::False& lfals
200200
}
201201

202202
NodePtr NodeGenerator::visit_(type_identity<ram::Conjunction>, const ram::Conjunction& conj) {
203-
return mk<Conjunction>(I_Conjunction, &conj, dispatch(conj.getLHS()), dispatch(conj.getRHS()));
203+
NodePtrVec children;
204+
std::stack<const ram::Node*> dfs;
205+
dfs.push(&conj.getRHS());
206+
dfs.push(&conj.getLHS());
207+
while (!dfs.empty()) {
208+
const ram::Node* term = dfs.top();
209+
dfs.pop();
210+
if (const ram::Conjunction* subconj = as<ram::Conjunction>(term)) {
211+
dfs.push(&subconj->getRHS());
212+
dfs.push(&subconj->getLHS());
213+
} else {
214+
children.emplace_back(std::move(dispatch(*term)));
215+
}
216+
}
217+
return mk<Conjunction>(I_Conjunction, &conj, std::move(children));
204218
}
205219

206220
NodePtr NodeGenerator::visit_(type_identity<ram::Negation>, const ram::Negation& neg) {

src/interpreter/Node.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,12 @@ class False : public Node {
578578

579579
/**
580580
* @class Conjunction
581+
*
582+
* It's a compound node so that conjunctions with hundreds of terms
583+
* do not overflow the engine stack with left/right recursion.
581584
*/
582-
class Conjunction : public BinaryNode {
583-
using BinaryNode::BinaryNode;
585+
class Conjunction : public CompoundNode {
586+
using CompoundNode::CompoundNode;
584587
};
585588

586589
/**

0 commit comments

Comments
 (0)