Avoid tail recursion, as with -O0 compiler doesn't do it and stack overflows

This commit is contained in:
Wilson Snyder 2010-01-08 10:27:02 -05:00
parent 2950f77dbc
commit 85ba590fa9

View File

@ -693,14 +693,17 @@ AstNode::~AstNode() {
void AstNode::deleteTreeIter() { void AstNode::deleteTreeIter() {
if (!this) return; if (!this) return;
// MUST be depth first! for (AstNode* nodep=this, *nnextp; nodep; nodep=nnextp) {
this->m_op1p->deleteTreeIter(); nnextp = nodep->m_nextp;
this->m_op2p->deleteTreeIter(); // MUST be depth first!
this->m_op3p->deleteTreeIter(); nodep->m_op1p->deleteTreeIter();
this->m_op4p->deleteTreeIter(); nodep->m_op2p->deleteTreeIter();
this->m_nextp->deleteTreeIter(); nodep->m_op3p->deleteTreeIter();
this->m_backp = NULL; nodep->m_op4p->deleteTreeIter();
deleteNode(); nodep->m_nextp = NULL;
nodep->m_backp = NULL;
nodep->deleteNode();
}
} }
void AstNode::deleteTree() { void AstNode::deleteTree() {
@ -843,12 +846,13 @@ AstNode* AstNode::acceptSubtreeReturnEdits(AstNVisitor& v, AstNUser* vup) {
void AstNode::cloneRelinkTree() { void AstNode::cloneRelinkTree() {
if (!this) return; if (!this) return;
this->cloneRelink(); for (AstNode* nodep=this; nodep; nodep=nodep->m_nextp) {
m_op1p->cloneRelinkTree(); nodep->cloneRelink();
m_op2p->cloneRelinkTree(); nodep->m_op1p->cloneRelinkTree();
m_op3p->cloneRelinkTree(); nodep->m_op2p->cloneRelinkTree();
m_op4p->cloneRelinkTree(); nodep->m_op3p->cloneRelinkTree();
m_nextp->cloneRelinkTree(); // Tail recursion nodep->m_op4p->cloneRelinkTree();
}
} }
//====================================================================== //======================================================================