00001 #include "FlexLexer.h"
00002 #include "parser.h"
00003 #include "exception.h"
00004 #include <typeinfo>
00005 #include <sstream>
00006
00007
00008 extern int yylex();
00009 extern void set_extParser(Parser* extP);
00010 extern void set_result(Node **r);
00011 extern yyFlexLexer* lexer;
00012 extern int numLine;
00013 extern int yyparse (void);
00014
00015 int Node::ndterms;
00016
00020 Parser::Parser(Configuration *config)
00021 {
00022 this->config = config;
00023 autoNumericalValCur = 0;
00024 autoContstantValCur = 0;
00025
00026 set_extParser(this);
00027 this->result = NULL;
00028 set_result(&this->result);
00029
00030 lexer = new yyFlexLexer(config->inputdatas,mycout);
00031
00032 numLine = 1;
00033 if((yyparse())||(!this->result))
00034 exit(1);
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 checkSoundness();
00140 }
00141
00145 string Parser::genNumericalValName()
00146 {
00147 ostringstream text;
00148 if(this->config->writeMode==Configuration::LATEX)
00149 text << "\\_N";
00150 else
00151 text << "_N";
00152 text << autoNumericalValCur;
00153 autoNumericalValCur++;
00154 return text.str();
00155 }
00156
00160 string Parser::genContstantValName()
00161 {
00162 ostringstream text;
00163 if(this->config->writeMode==Configuration::LATEX)
00164 text << "\\_C";
00165 else
00166 text << "_C";
00167 text << autoContstantValCur;
00168 autoContstantValCur++;
00169 return text.str();
00170 }
00171
00172 void Parser::checkSoundness()
00173 {
00174 Node::ndterms = 0;
00175 result->checkSoundnessAndCalcul();
00176 }
00177
00178
00179
00180 void Node::print(std::ostream& o)
00181 {
00182 o << "<?>";
00183 }
00184
00185 void Condition::print(std::ostream& o)
00186 {
00187 o << "<?>";
00188 }
00189
00190 void And::printNoBracket(std::ostream& o)
00191 {
00192 list<Condition*>::iterator it = conditions.begin();
00193 while(it!=conditions.end())
00194 {
00195 (*it)->print(o);
00196 if((*it!=conditions.back())||(!branchs.empty()))
00197 if(globconfig->writeMode==Configuration::LATEX)
00198 o << "\\wedge ";
00199 else
00200 o << " and ";
00201 it++;
00202 }
00203
00204 list<Node*>::iterator it2 = branchs.begin();
00205 while(it2!=branchs.end())
00206 {
00207 (*it2)->print(o);
00208 if(*it2!=branchs.back())
00209 if(globconfig->writeMode==Configuration::LATEX)
00210 o << "\\wedge ";
00211 else
00212 o << " and ";
00213 it2++;
00214 }
00215 }
00216
00217 void And::print(std::ostream& o)
00218 {
00219 o << " (";
00220 printNoBracket(o);
00221 o << ") ";
00222 }
00223
00224 void Equal::print(std::ostream& o)
00225 {
00226 n1->print(o);
00227 o << " = ";
00228 n2->print(o);
00229 }
00230
00231 void Variable::print(std::ostream& o)
00232 {
00233 o << parser->variabletab[id].name ;
00234 }
00235
00236 void Function::print(std::ostream& o)
00237 {
00238 o << parser->fonctiontab[id].name ;
00239 if(!arguments.empty())
00240 {
00241 o << "(";
00242 vector<Node*>::iterator it = arguments.begin();
00243 while(it!=arguments.end())
00244 {
00245 (*it)->print(o);
00246 if((*it)!=arguments.back())
00247 o << ",";
00248 it++;
00249 }
00250 o << ")";
00251 }
00252 }
00253
00254 void NumericalVar::print(std::ostream& o)
00255 {
00256 o << parser->numericalvariabletab[id].name ;
00257 }
00258
00259 void CondA::print(std::ostream& o)
00260 {
00261 n1->print(o);
00262 o << "=" << a ;
00263 }
00264
00265 void CondANplusB::print(std::ostream& o)
00266 {
00267 n1->print(o);
00268 o << "=" ;
00269 if(a!=1)
00270 o << a << "*";
00271 n2->print(o);
00272 if(b!=0)
00273 o << "+" << b;
00274 }
00275
00276 void CondNplusN::print(std::ostream& o)
00277 {
00278 n1->print(o);
00279 o << "=";
00280 n2->print(o);
00281 o << "+";
00282 n3->print(o);
00283 }
00284
00285 void CondN::print(std::ostream& o)
00286 {
00287 n1->print(o);
00288 o << "=";
00289 n2->print(o);
00290 }
00291
00292 void Expo::print(std::ostream& o)
00293 {
00294 if(typeid(*n1)==typeid(Expo))
00295 o << "(";
00296 n1->print(o);
00297 if(typeid(*n1)==typeid(Expo))
00298 o << ")";
00299 o << "^{";
00300 if(free)
00301 var->print(o);
00302 else
00303 o << expNum;
00304 o << "}.";
00305 if(typeid(*n2)==typeid(Expo))
00306 o << "(";
00307 n2->print(o);
00308 if(typeid(*n2)==typeid(Expo))
00309 o << ")";
00310 }
00311
00312 void Or::print(std::ostream& o)
00313 {
00314 o << " (" <<endl;
00315 list<Node*>::iterator it = branchs.begin();
00316 while(it!=branchs.end())
00317 {
00318 (*it)->print(o);
00319 if(*it!=branchs.back())
00320 if(globconfig->writeMode==Configuration::LATEX)
00321 o << "\\vee ";
00322 else
00323 o << " or " << endl;
00324 it++;
00325 }
00326 o << endl <<" ) ";
00327 }
00328
00329
00330
00331 Node::Node()
00332 {
00333 rhoterm = false;
00334 ndHole = false;
00335 holePosition = NULL;
00336 }
00337
00338 Node::~Node()
00339 {
00340 }
00341
00342
00343 Hole::~Hole()
00344 {
00345 if(holePosition)
00346 delete holePosition;
00347 }
00348
00349 Hole::Hole()
00350 : Node()
00351 {
00352 holePosition = new list<unsigned int>();
00353 }
00354
00355 Condition::~Condition()
00356 {
00357 }
00358
00359 NumericalVar::NumericalVar(const char* name,Parser *parser,bool automatic)
00360 {
00361 this->parser = parser;
00362 vector<NumericalVariableTabElement>::iterator it = parser->numericalvariabletab.begin();
00363 int num=0;
00364 while(it!=parser->numericalvariabletab.end())
00365 {
00366 if(it->name==name)
00367 {
00368 this->id = num;
00369 if(automatic!=(*it).automatic)
00370 throw SytaxException("using of reserved name for numerical variable");
00371 return;
00372 }
00373 it++;
00374 num++;
00375 }
00376 NumericalVariableTabElement add;
00377 add.name = name;
00378 add.automatic = automatic;
00379 parser->numericalvariabletab.push_back(add);
00380 this->id = num;
00381 }
00382
00383 Function::Function(int id,Parser *parser)
00384 : Node()
00385 {
00386 this->id = id;
00387 this->parser = parser;
00388 }
00389
00390 Function::Function()
00391 : Node()
00392 {
00393 this->id = -1;
00394 this->parser = NULL;
00395 }
00396
00397 void Function::setName(const char* name,Parser *parser)
00398 {
00399 this->parser = parser;
00400 vector<FonctionTabElement>::iterator it = parser->fonctiontab.begin();
00401 int num=0;
00402 while(it!=parser->fonctiontab.end())
00403 {
00404 if(it->name==name)
00405 {
00406 this->id = num;
00407 return;
00408 }
00409 it++;
00410 num++;
00411 }
00412 FonctionTabElement add;
00413 add.name = name;
00414 add.arity = -1;
00415 parser->fonctiontab.push_back(add);
00416 this->id = num;
00417 }
00418
00419 Or::Or()
00420 : Node()
00421 {
00422
00423 }
00424
00425 void Or::addBranch(Node *n)
00426 {
00427 branchs.push_back(n);
00428 }
00429
00430 CondA::CondA(NumericalVar* n1,int a)
00431 {
00432 this->n1 = n1;
00433 this->a = a;
00434 }
00435
00436 CondANplusB::CondANplusB(NumericalVar* n1,int a,NumericalVar* n2,int b)
00437 {
00438 this->n1 = n1;
00439 this->n2 = n2;
00440 this->a = a;
00441 this->b = b;
00442 }
00443
00444 CondNplusN::CondNplusN(NumericalVar* n1,NumericalVar* n2,NumericalVar* n3)
00445 {
00446 this->n1 = n1;
00447 this->n2 = n2;
00448 this->n3 = n3;
00449 }
00450
00451 CondN::CondN(NumericalVar* n1,NumericalVar* n2)
00452 {
00453 this->n1 = n1;
00454 this->n2 = n2;
00455 }
00456
00457 And::And()
00458 : Node()
00459 {
00460
00461 }
00462
00463 void And::addBranch(Node *n)
00464 {
00465 branchs.push_back(n);
00466 }
00467
00468 void And::addCondition(Condition *c)
00469 {
00470 conditions.push_back(c);
00471 }
00472
00473 Equal::Equal(Node *n1,Node *n2)
00474 : Node()
00475 {
00476 this->n1 = n1;
00477 this->n2 = n2;
00478 }
00479
00480 Variable::Variable(const char *name,Parser *parser)
00481 : Node()
00482 {
00483 this->parser = parser;
00484 vector<VariableTabElement>::iterator it = parser->variabletab.begin();
00485 int num=0;
00486 while(it!=parser->variabletab.end())
00487 {
00488 if(it->name==name)
00489 {
00490 this->id = num;
00491 return;
00492 }
00493 it++;
00494 num++;
00495 }
00496 VariableTabElement add;
00497 add.name = name;
00498 parser->variabletab.push_back(add);
00499 this->id = num;
00500 }
00501
00502 Function::Function(const char* name,Parser *parser)
00503 : Node()
00504 {
00505 this->parser = parser;
00506 vector<FonctionTabElement>::iterator it = parser->fonctiontab.begin();
00507 int num=0;
00508 while(it!=parser->fonctiontab.end())
00509 {
00510 if(it->name==name)
00511 {
00512 this->id = num;
00513 return;
00514 }
00515 it++;
00516 num++;
00517 }
00518 FonctionTabElement add;
00519 add.name = name;
00520 add.arity = -1;
00521 parser->fonctiontab.push_back(add);
00522 this->id = num;
00523 }
00524
00525 void Function::addArgument(Node *n)
00526 {
00527 arguments.push_back(n);
00528 }
00529
00530 Expo::Expo(Node *n1,int num,Node *n2)
00531 : Node()
00532 {
00533 this->n1 = n1;
00534 this->n2 = n2;
00535 this->expNum = num;
00536 free = false;
00537 }
00538
00539 Expo::Expo(Node *n1,NumericalVar *num,Node *n2)
00540 : Node()
00541 {
00542 this->n1 = n1;
00543 this->n2 = n2;
00544 this->var = num;
00545 free = true;
00546 }
00547
00548 void Hole::print(std::ostream& o)
00549 {
00550 if(globconfig->writeMode==Configuration::LATEX)
00551 o << "\\diamond ";
00552 else
00553 o << "@";
00554 }
00555
00556 void Clash::print(std::ostream& o)
00557 {
00558 if(globconfig->writeMode==Configuration::LATEX)
00559 o << "\\bot ";
00560 else
00561 o << "<Clash>";
00562 }
00563
00564 void Trivial::print(std::ostream& o)
00565 {
00566 if(globconfig->writeMode==Configuration::LATEX)
00567 o << "\\top ";
00568 else
00569 o << "<Triv>";
00570 }
00571
00572 void Node::checkSoundnessAndCalcul()
00573 {
00574 Node::ndterms++;
00575 this->ndHole = 0;
00576 this->rhoterm = false;
00577 }
00578
00579 void Hole::checkSoundnessAndCalcul()
00580 {
00581 Node::ndterms++;
00582 this->ndHole = 1;
00583 this->rhoterm = false;
00584 this->holePosition->clear();
00585 }
00586
00587 void And::checkSoundnessAndCalcul()
00588 {
00589 Node::ndterms++;
00590 list<Node*>::iterator it = branchs.begin();
00591 rhoterm = false;
00592 ndHole = 0;
00593 while(it!=branchs.end())
00594 {
00595 (*it)->checkSoundnessAndCalcul();
00596 if((*it)->rhoterm)
00597 rhoterm = true;
00598 ndHole += (*it)->ndHole;
00599 it++;
00600 }
00601 }
00602
00603 void Or::checkSoundnessAndCalcul()
00604 {
00605 Node::ndterms++;
00606 list<Node*>::iterator it = branchs.begin();
00607 rhoterm = false;
00608 ndHole = 0;
00609 while(it!=branchs.end())
00610 {
00611 (*it)->checkSoundnessAndCalcul();
00612 if((*it)->rhoterm)
00613 rhoterm = true;
00614 ndHole += (*it)->ndHole;
00615 it++;
00616 }
00617 }
00618
00619 void Function::checkSoundnessAndCalcul()
00620 {
00621 Node::ndterms++;
00622 rhoterm = false;
00623 ndHole = 0;
00624 if(parser->fonctiontab[id].arity==-1)
00625 parser->fonctiontab[id].arity = (int)arguments.size();
00626 else
00627 if(arguments.size()!=(unsigned int)parser->fonctiontab[id].arity)
00628 throw SytaxException("fonction \"" + parser->fonctiontab[id].name + "\" with several arities");
00629
00630 vector<Node*>::iterator it = arguments.begin();
00631 int numArg = 0;
00632 while(it!=arguments.end())
00633 {
00634 (*it)->checkSoundnessAndCalcul();
00635 if((*it)->rhoterm)
00636 rhoterm = true;
00637 ndHole += (*it)->ndHole;
00638 if((*it)->ndHole>0)
00639 {
00640 this->holePosition = (*it)->holePosition;
00641 this->holePosition->push_front(numArg);
00642 }
00643 it++;
00644 numArg++;
00645 }
00646 }
00647
00648 void Variable::checkSoundnessAndCalcul()
00649 {
00650 Node::ndterms++;
00651 rhoterm = false;
00652 ndHole = 0;
00653 }
00654
00655 void Equal::checkSoundnessAndCalcul()
00656 {
00657 Node::ndterms++;
00658 rhoterm = false;
00659 ndHole = 0;
00660 n1->checkSoundnessAndCalcul();
00661 n2->checkSoundnessAndCalcul();
00662
00663 if(n1->rhoterm)
00664 rhoterm = true;
00665 ndHole += n1->ndHole;
00666
00667 if(n2->rhoterm)
00668 rhoterm = true;
00669 ndHole += n2->ndHole;
00670 }
00671
00672 void Expo::checkSoundnessAndCalcul()
00673 {
00674 Node::ndterms++;
00675 rhoterm = true;
00676 ndHole = 0;
00677 n1->checkSoundnessAndCalcul();
00678 n2->checkSoundnessAndCalcul();
00679 this->holePosition = n1->holePosition;
00680
00681 if(n1->ndHole==0)
00682 {
00683 this->print(cerr);
00684 throw SytaxException("Hole not found");
00685 }
00686 if(n1->ndHole>1)
00687 {
00688 this->print(cerr);
00689 throw SytaxException("Too much hole");
00690 }
00691 if(n1->rhoterm)
00692
00693 {
00694 this->print(cerr);
00695 throw SytaxException("Do not tolerate rho-terms in iterated part");
00696 }
00697
00698 if(n1->holePosition->empty())
00699 {
00700 this->print(cerr);
00701 throw SytaxException("Do not tolerate single hole in iterated part");
00702 }
00703
00704 }
00705
00706 NodeIterator::NodeIterator(Node *n,Node **direct,Mode mode)
00707 {
00708 this->mode = mode;
00709 this->n = n;
00710 this->direct = direct;
00711 StackElement e(n,direct);
00712 stack.push_back(e);
00713 allowUnderEqual = false;
00714 }
00715
00716 void NodeIterator::setUnderEqual(bool u)
00717 {
00718 allowUnderEqual = u;
00719 }
00720
00721 const list<unsigned int> &NodeIterator::getPosition()
00722 {
00723 return last.position;
00724 }
00725
00726 Node* NodeIterator::next()
00727 {
00728 if(stack.empty())
00729 return NULL;
00730
00731 StackElement e;
00732 if(mode==NodeIterator::BREADTH_FIRST)
00733 {
00734 e = stack.front();
00735 stack.pop_front();
00736 }
00737 else
00738 {
00739 e = stack.back();
00740 stack.pop_back();
00741 }
00742
00743 last = e;
00744
00745 if(typeid(*e.n)==typeid(And))
00746 {
00747 list<Node*>::iterator it = ((And*)(e.n))->branchs.begin();
00748 while(it!=((And*)(e.n))->branchs.end())
00749 {
00750 stack.push_back(StackElement(*it,&*it));
00751 it++;
00752 }
00753 }
00754 else if(typeid(*e.n)==typeid(Or))
00755 {
00756 list<Node*>::iterator it = ((Or*)(e.n))->branchs.begin();
00757 while(it!=((Or*)(e.n))->branchs.end())
00758 {
00759 stack.push_back(StackElement(*it,&*it));
00760 it++;
00761 }
00762 }
00763 else if(typeid(*e.n)==typeid(Function))
00764 {
00765 vector<Node*>::iterator it = ((Function*)(e.n))->arguments.begin();
00766 int num = 0;
00767 while(it!=((Function*)(e.n))->arguments.end())
00768 {
00769 stack.push_back(StackElement(*it,&*it,e.position,num));
00770 num++;
00771 it++;
00772 }
00773 }
00774 else if(typeid(*e.n)==typeid(Equal))
00775 {
00776 if(allowUnderEqual)
00777 {
00778 stack.push_back(StackElement(((Equal*)(e.n))->n1,&((Equal*)(e.n))->n1));
00779 stack.push_back(StackElement(((Equal*)(e.n))->n2,&((Equal*)(e.n))->n2));
00780 }
00781 }
00782 return e.n;
00783 }
00784
00785 StackElement::StackElement(Node *n,Node **direct)
00786 {
00787 this->n = n;
00788 this->direct = direct;
00789 }
00790
00791 StackElement::StackElement(Node *n,Node **direct,list<unsigned int> &parentpos,int me)
00792 {
00793 this->n = n;
00794 this->direct = direct;
00795 position = parentpos;
00796 position.push_back(me);
00797 }
00798
00799 StackElement::StackElement()
00800 {
00801 }
00802
00806 void NodeIterator::replaceBy(Node *n)
00807 {
00808 if(last.direct)
00809 (*last.direct) = n;
00810 else
00811 assert(false);
00812 }
00813
00817 void NodeIterator::reset()
00818 {
00819 stack.clear();
00820 StackElement e(n,direct);
00821 stack.push_back(e);
00822 }
00823
00827 bool Node::containVar(int idvar)
00828 {
00829 return false;
00830 }
00831
00832 bool Variable::containVar(int idvar)
00833 {
00834 return id==idvar;
00835 }
00836
00837 bool Function::containVar(int idvar)
00838 {
00839 vector<Node*>::iterator it = arguments.begin();
00840 while(it!=arguments.end())
00841 {
00842 if((*it)->containVar(idvar))
00843 return true;
00844 it++;
00845 }
00846 return false;
00847 }
00848
00849 bool And::containVar(int idvar)
00850 {
00851 list<Node*>::iterator it = branchs.begin();
00852 while(it!=branchs.end())
00853 {
00854 if((*it)->containVar(idvar))
00855 return true;
00856 it++;
00857 }
00858 return false;
00859 }
00860
00861 bool Or::containVar(int idvar)
00862 {
00863 list<Node*>::iterator it = branchs.begin();
00864 while(it!=branchs.end())
00865 {
00866 if((*it)->containVar(idvar))
00867 return true;
00868 it++;
00869 }
00870 return false;
00871 }
00872
00873 bool Equal::containVar(int idvar)
00874 {
00875 return n1->containVar(idvar) || n2->containVar(idvar);
00876 }
00877
00878 bool Expo::containVar(int idvar)
00879 {
00880 return n1->containVar(idvar) || n2->containVar(idvar);
00881 }
00882
00886 void Node::replace(int idvar,Node *n)
00887 {
00888 }
00889
00890 void Function::replace(int idvar,Node *n)
00891 {
00892 vector<Node*>::iterator it = arguments.begin();
00893 while(it!=arguments.end())
00894 {
00895 if((typeid(**it)==typeid(Variable))&&(((Variable*)(*it))->id==idvar))
00896 *it = n->duplicate();
00897 (*it)->replace(idvar,n);
00898 it++;
00899 }
00900 }
00901
00902 void Or::replace(int idvar,Node *n)
00903 {
00904 list<Node*>::iterator it = branchs.begin();
00905 while(it!=branchs.end())
00906 {
00907 if((typeid(**it)==typeid(Variable))&&(((Variable*)(*it))->id==idvar))
00908 *it = n->duplicate();
00909 (*it)->replace(idvar,n);
00910 it++;
00911 }
00912 }
00913
00914 void And::replace(int idvar,Node *n)
00915 {
00916 list<Node*>::iterator it = branchs.begin();
00917 while(it!=branchs.end())
00918 {
00919 if((typeid(**it)==typeid(Variable))&&(((Variable*)(*it))->id==idvar))
00920 *it = n->duplicate();
00921 (*it)->replace(idvar,n);
00922 it++;
00923 }
00924 }
00925
00926 void Equal::replace(int idvar,Node *n)
00927 {
00928 if((typeid(*n1)==typeid(Variable))&&(((Variable*)n1)->id==idvar))
00929 n1 = n->duplicate();
00930
00931 if((typeid(*n2)==typeid(Variable))&&(((Variable*)n2)->id==idvar))
00932 n2 = n->duplicate();
00933
00934 n1->replace(idvar,n);
00935 n2->replace(idvar,n);
00936 }
00937
00938 void Expo::replace(int idvar,Node *n)
00939 {
00940 if((typeid(*n1)==typeid(Variable))&&(((Variable*)n1)->id==idvar))
00941 n1 = n->duplicate();
00942
00943 if((typeid(*n2)==typeid(Variable))&&(((Variable*)n2)->id==idvar))
00944 n2 = n->duplicate();
00945
00946 n1->replace(idvar,n);
00947 n2->replace(idvar,n);
00948 }
00949
00950 Node* Hole::duplicate()
00951 {
00952 return new Hole();
00953 }
00954
00955 Node* Clash::duplicate()
00956 {
00957 return new Clash();
00958 }
00959
00960 Node* Trivial::duplicate()
00961 {
00962 return new Trivial();
00963 }
00964
00965 NumericalVar* NumericalVar::duplicate()
00966 {
00967 return new NumericalVar(*this);
00968 }
00969
00970 Condition* CondANplusB::duplicate()
00971 {
00972 return new CondANplusB(n1->duplicate(),a,n2->duplicate(),b);
00973 }
00974
00975 Condition* CondA::duplicate()
00976 {
00977 return new CondA(n1->duplicate(),a);
00978 }
00979
00980 Condition* CondNplusN::duplicate()
00981 {
00982 return new CondNplusN(n1->duplicate(),n2->duplicate(),n3->duplicate());
00983 }
00984
00985 Condition* CondN::duplicate()
00986 {
00987 return new CondN(n1->duplicate(),n2->duplicate());
00988 }
00989
00990 Node* Variable::duplicate()
00991 {
00992 return new Variable(*this);
00993 }
00994
00995 Node* Function::duplicate()
00996 {
00997 Function *dme= new Function(id,parser);
00998 vector<Node*>::iterator it = arguments.begin();
00999 while(it!=arguments.end())
01000 {
01001 dme->addArgument((*it)->duplicate());
01002 it++;
01003 }
01004 return dme;
01005 }
01006
01007 Node* Or::duplicate()
01008 {
01009 Or *dme= new Or();
01010 list<Node*>::iterator it = branchs.begin();
01011 while(it!=branchs.end())
01012 {
01013 dme->addBranch((*it)->duplicate());
01014 it++;
01015 }
01016 return dme;
01017 }
01018
01019 Node* And::duplicate()
01020 {
01021 And *dme= new And();
01022 list<Node*>::iterator it = branchs.begin();
01023 while(it!=branchs.end())
01024 {
01025 dme->addBranch((*it)->duplicate());
01026 it++;
01027 }
01028 return dme;
01029 }
01030
01031 Node* Equal::duplicate()
01032 {
01033 return new Equal(n1->duplicate(),n2->duplicate());
01034 }
01035
01036 Node* Expo::duplicate()
01037 {
01038 if(free)
01039 return new Expo(n1->duplicate(),var->duplicate(),n2->duplicate());
01040 else
01041 return new Expo(n1->duplicate(),expNum,n2->duplicate());
01042 }
01043
01047 Node* Expo::buildTwo(Node *sub)
01048 {
01049 return buildOne(buildOne(sub));
01050 }
01051
01055 Node* Expo::buildOne(Node *sub)
01056 {
01057 if(typeid(*n1)==typeid(Hole))
01058 return ((sub)?sub:(n2->duplicate()));
01059
01060 Node *ret = n1->duplicate();
01061
01062 Node *fonction = ret;
01063 Node **hole;
01064 list<unsigned int>::iterator it = holePosition->begin();
01065 while(it!=holePosition->end())
01066 {
01067 assert(typeid(*fonction)==typeid(Function));
01068 hole = &((Function*)fonction)->arguments[*it];
01069 fonction = ((Function*)fonction)->arguments[*it];
01070 it++;
01071 }
01072 assert(typeid(**hole)==typeid(Hole));
01073
01074 delete fonction;
01075 *hole = ((sub)?sub:(n2->duplicate()));
01076 return ret;
01077 }
01078
01082 Node* Node::buildOneAndReplace(list<unsigned int> &path,Node *sub)
01083 {
01084 assert(path.empty());
01085 return sub;
01086 }
01087
01088 Node* Function::buildOneAndReplace(list<unsigned int> &path,Node *sub)
01089 {
01090 assert(typeid(*this)==typeid(Function));
01091 if(path.empty())
01092 return sub;
01093
01094 unsigned int top = path.back();
01095 path.pop_back();
01096
01097 Function *ret = new Function(id,parser);
01098 vector<Node*>::iterator it = arguments.begin();
01099 unsigned int num = 0;
01100 while(it!=arguments.end())
01101 {
01102 if(num==top)
01103 {
01104 if(path.empty())
01105 ret->addArgument(sub);
01106 else
01107 ret->addArgument(((Function*)(*it))->buildOneAndReplace(path,sub));
01108 }
01109 else
01110 ret->addArgument((*it)->duplicate());
01111 it++;
01112 num++;
01113 }
01114 return ret;
01115 }
01116
01120 Node *Node::find(list<unsigned int> &path)
01121 {
01122 assert(path.empty());
01123 return this;
01124 }
01125
01126 Node *Function::find(list<unsigned int> &path)
01127 {
01128 assert(typeid(*this)==typeid(Function));
01129
01130 if(path.empty())
01131 return this;
01132
01133 unsigned int top = path.back();
01134 path.pop_back();
01135
01136 assert(top<arguments.size());
01137 if(path.empty())
01138 return arguments[top];
01139 else
01140 {
01141 assert(typeid(*((Function*)arguments[top]))==typeid(Function));
01142 return ((Function*)arguments[top])->find(path);
01143 }
01144 }
01145
01149 bool Node::isEqual(Node *n)
01150 {
01151 return (typeid(*this)==typeid(*n));
01152 }
01153
01154 bool NumericalVar::isEqual(Node *n)
01155 {
01156 if(typeid(*this)!=typeid(*n))
01157 return false;
01158 return (this->id==((NumericalVar*)n)->id);
01159 }
01160
01161 bool Variable::isEqual(Node *n)
01162 {
01163 if(typeid(*this)!=typeid(*n))
01164 return false;
01165 return (this->id==((Variable*)n)->id);
01166 }
01167
01168 bool Function::isEqual(Node *n)
01169 {
01170 if(typeid(*this)!=typeid(*n))
01171 return false;
01172
01173 if(this->id!=((Function*)n)->id)
01174 return false;
01175
01176 vector<Node*>::iterator it = arguments.begin();
01177 vector<Node*>::iterator it2 = ((Function*)n)->arguments.begin();
01178 while(it!=arguments.end())
01179 {
01180 if(!(*it)->isEqual(*it2))
01181 return false;
01182 it2++;
01183 it++;
01184 }
01185
01186 return true;
01187 }
01188
01189 bool Or::isEqual(Node *n)
01190 {
01191 if(typeid(*this)!=typeid(*n))
01192 return false;
01193
01194 list<Node*>::iterator it = branchs.begin();
01195 list<Node*>::iterator it2 = ((Or*)n)->branchs.begin();
01196 while(it!=branchs.end())
01197 {
01198 if(!(*it)->isEqual(*it2))
01199 return false;
01200 it2++;
01201 it++;
01202 }
01203 return true;
01204 }
01205
01206 bool And::isEqual(Node *n)
01207 {
01208 if(typeid(*this)!=typeid(*n))
01209 return false;
01210
01211 list<Node*>::iterator it = branchs.begin();
01212 list<Node*>::iterator it2 = ((And*)n)->branchs.begin();
01213 while(it!=branchs.end())
01214 {
01215 if(!(*it)->isEqual(*it2))
01216 return false;
01217 it2++;
01218 it++;
01219 }
01220
01221 list<Condition*>::iterator it3 = conditions.begin();
01222 list<Condition*>::iterator it4 = ((And*)n)->conditions.begin();
01223 while(it3!=conditions.end())
01224 {
01225 if(!(*it3)->isEqual(*it4))
01226 return false;
01227 it3++;
01228 it4++;
01229 }
01230
01231 return true;
01232 }
01233
01234 bool Equal::isEqual(Node *n)
01235 {
01236 if(typeid(*this)!=typeid(*n))
01237 return false;
01238 if(!this->n1->isEqual(((Equal*)n)->n1))
01239 return false;
01240 if(!this->n2->isEqual(((Equal*)n)->n2))
01241 return false;
01242 return true;
01243 }
01244
01245 bool Expo::isEqual(Node *n)
01246 {
01247 if(typeid(*this)!=typeid(*n))
01248 return false;
01249 if(!this->n1->isEqual(((Expo*)n)->n1))
01250 return false;
01251 if(!this->n2->isEqual(((Expo*)n)->n2))
01252 return false;
01253
01254 if(this->free==((Expo*)n)->free)
01255 if(free)
01256 return this->var->id == ((Expo*)n)->var->id;
01257 else
01258 return this->expNum == ((Expo*)n)->expNum;
01259
01260 return false;
01261 }
01262
01263 bool CondA::isEqual(Condition *n)
01264 {
01265 if(typeid(*this)!=typeid(*n))
01266 return false;
01267 return false;
01268 }
01269
01270 bool CondANplusB::isEqual(Condition *n)
01271 {
01272 if(typeid(*this)!=typeid(*n))
01273 return false;
01274 return false;
01275 }
01276
01277 bool CondNplusN::isEqual(Condition *n)
01278 {
01279 if(typeid(*this)!=typeid(*n))
01280 return false;
01281 return false;
01282 }
01283
01284 bool CondN::isEqual(Condition *n)
01285 {
01286 if(typeid(*this)!=typeid(*n))
01287 return false;
01288 return false;
01289 }
01290
01291 Node* Expo::build(int num,Node *sub)
01292 {
01293 Node *ret = sub;
01294 int i;
01295 for(i=0;i<num;i++)
01296 ret = buildOne(ret);
01297 return ret;
01298 }
01299