unsigned int ExpressionBuilder::EBBinaryTermNode::substitute(const EBSubstitutionRuleList & rules) { unsigned int nrule = rules.size(); unsigned int success = 0; for (unsigned int i = 0; i < nrule; ++i) { EBTermNode * replace = rules[i]->apply(left); if (replace != NULL) { delete left; left = replace; success = 1; break; } } if (success == 0) success += left->substitute(rules); for (unsigned int i = 0; i < nrule; ++i) { EBTermNode * replace = rules[i]->apply(right); if (replace != NULL) { delete right; right = replace; return success + 1; } } return success + right->substitute(rules); }
unsigned int ExpressionBuilder::EBTernaryTermNode::substitute(const EBSubstitutionRuleList & rules) { unsigned int nrule = rules.size(); bool left_success = false, middle_success = false, right_success = false; EBTermNode * replace; for (unsigned int i = 0; i < nrule; ++i) { replace = rules[i]->apply(left); if (replace) { delete left; left = replace; left_success = true; break; } } for (unsigned int i = 0; i < nrule; ++i) { replace = rules[i]->apply(middle); if (replace) { delete middle; middle = replace; middle_success = true; break; } } for (unsigned int i = 0; i < nrule; ++i) { replace = rules[i]->apply(right); if (replace) { delete right; right = replace; right_success = true; break; } } if (!left_success) left_success = left->substitute(rules); if (!middle_success) middle_success = middle->substitute(rules); if (!right_success) right_success = right->substitute(rules); return left_success + middle_success + right_success; }
unsigned int ExpressionBuilder::EBUnaryTermNode::substitute(const EBSubstitutionRuleList & rules) { unsigned int nrule = rules.size(); for (unsigned int i = 0; i < nrule; ++i) { EBTermNode * replace = rules[i]->apply(subnode); if (replace != NULL) { delete subnode; subnode = replace; return 1; } } return subnode->substitute(rules); }
unsigned int ExpressionBuilder::EBTerm::substitute(const EBSubstitutionRuleList & rules) { unsigned int nrule = rules.size(); if (root == NULL) return 0; for (unsigned int i = 0; i < nrule; ++i) { EBTermNode * replace = rules[i]->apply(root); if (replace != NULL) { delete root; root = replace; return 1; } } return root->substitute(rules); }