SymTree *shallowCopyAndClear(SymTree *tree){ SymTree *returnMe = new SymTree(tree->root->bt); returnMe->ready = MODIFIED; tree->ready = MODIFIED; returnMe->root->t = tree->root->t; returnMe->root->addChildren(tree->root); tree->root->children.clear(); tree->root->destroyTree(); tree->root = newEmpty(); return returnMe; }
static void windup(void) { i_p=i_r=i_2=i_m=0; pattern[0]=P_ERROR; accept_p(); empty=newEmpty(); notAllowed=newNotAllowed(); any=newAny(); }
/////////////////////// // Post-condition (memory management): // Return a result which has pointers to whichever // nodes from this and other that are retained. // Nodes this and other are destroyed where appropriate // and replaced with newEmpty() nodes. // SymTree *SymTree::metaOp(SymTree *other,NType op){ assert(op == SOR || op == SAND); other->ready = MODIFIED; this->ready = MODIFIED; if(this->isEmpty()){ return shallowCopyAndClear(other); }else if(other->isEmpty()){ return shallowCopyAndClear(this); } if( op == SAND && root->t == FTYPE){ other->root->destroyTree(); other->root = newEmpty(); return shallowCopyAndClear(this); }else if(op == SAND && other->root->t == FTYPE){ this->root->destroyTree(); this->root = newEmpty(); return shallowCopyAndClear(other); } NType thisT = this->root->t; NType otherT = other->root->t; SymNode *newNodePtr; //For simplicity make sure that both trees have operators for roots if(otherT == BTERM){ newNodePtr = new SymNode( op, DUMMY ); newNodePtr->addChild(other->root); other->root = newNodePtr; otherT = op; } if(thisT == BTERM){ newNodePtr = new SymNode( op, DUMMY ); newNodePtr->addChild(this->root); this->root = newNodePtr; thisT = op; } if(thisT == op && otherT == op){ this->root->addChildren(other->root); other->root->children.clear(); delete other->root; other->root = newEmpty(); return shallowCopyAndClear(this); }else if(thisT != op && otherT != op ){ newNodePtr = new SymNode(op, -1); newNodePtr->addChild(this->root); newNodePtr->addChild(other->root); this->root = newNodePtr; other->root = new SymNode(ETYPE,-1); return shallowCopyAndClear(this); }else if( thisT != op && otherT == op ){ other->root->addChild(this->root); this->root = NULL; this->root = newEmpty(); return shallowCopyAndClear(other); } else if(thisT == op && otherT != op){ this->root->addChild(other->root); other->root = NULL; other->root = newEmpty(); return shallowCopyAndClear(this); } cerr <<"error in metaOp\n"<< endl; assert(0); return 0; }