# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

compilateur.cpp

Go to the documentation of this file.
00001 
00006 /* Copyright, 2000 Nevrax Ltd.
00007  *
00008  * This file is part of NEVRAX NEL.
00009  * NEVRAX NEL is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013 
00014  * NEVRAX NEL is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * General Public License for more details.
00018 
00019  * You should have received a copy of the GNU General Public License
00020  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00022  * MA 02111-1307, USA.
00023  */
00024 #include "nel/ai/script/compilateur.h"
00025 #include "nel/ai/script/constraint.h"
00026 #include "nel/ai/script/interpret_object_agent.h"
00027 #include "nel/ai/script/type_def.h"
00028 #include "nel/ai/script/object_unknown.h"
00029 #include <queue>
00030 #include <stdarg.h>
00031 
00032 namespace NLAISCRIPT
00033 {       
00034         /*struct BestP
00035         {
00036                 sint32 i;
00037                 double poid;
00038 
00039                 BestP(sint32 n, double d)
00040                 {
00041                         i = n;
00042                         poid = d;
00043                 }
00044 
00045                 bool operator < (const BestP &p) const
00046                 {
00047                         return poid < p.poid;
00048                 }
00049 
00050         };*/
00051 
00052         void CCompilateur::onEndClass()
00053         {
00054                 NLAIAGENT::IObjectIA *o = (NLAIAGENT::IObjectIA *)_SelfClass.pop();
00055                 ((IClassInterpret *)o)->classIsMounted();
00056                 o->release();           
00057         }
00058 
00059         NLAIC::CIdentType CCompilateur::getTypeOfClass(const NLAIAGENT::IVarName &className)
00060         {
00061 #ifdef NL_DEBUG
00062                 const char *nameDB = className.getString();
00063 #endif
00064 
00065                 NLAIC::CRegistry *reg = NLAIC::getRegistry();
00066                 if ( reg->existsClass( className.getString() ) )
00067                 {                               
00068                         NLAIC::CIdentType id(className.getString());
00069                         return id;
00070                 }
00071                 else
00072                 {
00073                         IClassInterpret *cl= _SelfClass.find(&className);                                                               
00074                         if(cl == NULL) 
00075                         {
00076                                 throw CExceptionHaveNoType("Can't find class");
00077                         }
00078                         else
00079                         {
00080                                 return getTypeOfClass(*cl->getInheritanceName());
00081                         }                               
00082                 }
00083                 
00084 /*
00085                 try
00086                 catch (NLAIE::IException &err)
00087                 {                               
00088                 }*/
00089         }
00090 
00091         void CCompilateur::Echo(char *Er,...)
00092         {
00093                 std::string Temp;
00094                 va_list marker;
00095                 va_start (marker, Er);
00096                 char LaseErrorCodeOrdreInterprete[32*1024];
00097                 vsprintf (LaseErrorCodeOrdreInterprete, Er, marker);
00098                 _Iterface.Echo("%s",LaseErrorCodeOrdreInterprete);
00099         }
00100 
00101 
00102         NLAIAGENT::IObjectIA::CProcessResult CCompilateur::Compile()
00103         {
00104                 NLAISCRIPT::InitDico();
00105                 (void)yywrap();
00106                 yyLine = yyColone = 1;
00107                 _Error = false;
00108                 
00109                 (void)yyparse();
00110                 while(yylex());
00111                 
00112                 NLAISCRIPT::EraseDico();
00113 
00114                 NLAIAGENT::IObjectIA::CProcessResult r;
00115                 if(_Error)
00116                 {
00117                         r.ResultState = NLAIAGENT::processError;
00118                 }
00119                 else r.ResultState = NLAIAGENT::processIdle;
00120                 r.Result = _ResultCompile;
00121                 return r;
00122         }
00123         
00124         NLAIAGENT::CIdMethod CCompilateur::findMethode(sint32 inheritance,NLAIAGENT::CStringType *baseName, const NLAIAGENT::IObjectIA *classType,const NLAIAGENT::IVarName &methodeName,const CParam &param)
00125         {
00126 
00127                 NLAIAGENT::tQueue q;
00128                 if(baseName != NULL && baseName->getStr() == NLAIAGENT::CStringVarName((const char *)CAgentClass::IdAgentClass))
00129                 {
00130                          q = classType->isMember(&baseName->getStr(),&methodeName,param);
00131                 }
00132                 else
00133                 {
00134                         /*std::string s;
00135                         methodeName.getDebugString(s);                  
00136                         param.getDebugString(s);                                                
00137 
00138                         s += "  ";
00139                         classType->getDebugString(s);
00140 
00141                         NLAIC::Out("%s\n",s.c_str());*/
00142                         q = classType->isMember(NULL,&methodeName,param);
00143                 }
00144 
00145                 if(q.size())
00146                 {
00147                         return q.top();                 
00148                 }
00149                 else
00150                 {                                               
00151                         return NLAIAGENT::CIdMethod(-1,0.0,NULL,NULL);
00152                 }
00153         }
00154 
00155         CFunctionTag CCompilateur::findMethode(NLAIAGENT::IBaseGroupType &listName,const CParam &param)
00156         {
00157                 return findMethode((IClassInterpret *)_SelfClass.get(),listName,param);
00158         }
00159 
00160         CFunctionTag CCompilateur::findMethode(const NLAIAGENT::IObjectIA *classType,NLAIAGENT::IBaseGroupType &listName,const CParam &param)
00161         {                                               
00162                 CFunctionTag method;
00163                 if(!listName.size())
00164                 {                       
00165                         method.Inheritance = -1;
00166                         method.MethodNum = -1;
00167                         method.MethodName = NULL;
00168                         return method;
00169                 }
00170 
00171                 sint32 h = listName.size() - 1;
00172 
00173                 NLAIAGENT::CIdMethod r;
00174                 
00175                 if(!h)
00176                 {
00177                         method.Inheritance = 0;//classType->sizeVTable() - 1;
00178                         method.MethodName = &((const NLAIAGENT::CStringType *)listName.get())->getStr();
00179 
00180                         r = findMethode(method.Inheritance,NULL,classType, *method.MethodName,param);
00181                         if( r.Index <0)
00182                         {
00183                                 method.MethodName = NULL;
00184                                 method.Inheritance = -1;
00185                                 method.MethodNum = -1;
00186                                 
00187                         }
00188                         else
00189                         {
00190                                 method.MethodNum = r.Index;
00191                                 method.Object = classType;
00192                                 method.setTypeObject(r.ReturnType);                             
00193                                 method.Method = (CMethodeName *)r.Method;                               
00194                         }
00195                         
00196                 }
00197                 else
00198                 {       
00199                         NLAIAGENT::CStringType *baseName = (NLAIAGENT::CStringType *)listName.getFront()->clone();                      
00200 
00201                         if((classType = validateHierarchyMethode(method.Member,method.Inheritance,classType,listName)) != NULL)
00202                         {                                                       
00203                                 if(method.Inheritance >= 0)
00204                                 {                       
00205                                         method.MethodName = &((const NLAIAGENT::CStringType *)listName.get())->getStr();
00206 
00207                                         r = findMethode(method.Inheritance,baseName,classType,*method.MethodName,param);
00208                                         if(  r.Index < 0)
00209                                         {
00210                                                 method.MethodName = NULL;
00211                                                 method.Inheritance = -1;
00212                                                 method.MethodNum = -1;
00213                                         }
00214                                         else
00215                                         {
00216 
00217                                                 method.Object = classType;
00218                                                 method.MethodNum = r.Index;
00219                                                 method.Method = (CMethodeName *)r.Method;
00220                                                 method.setTypeObject(r.ReturnType);
00221                                 
00222                                         }
00223                                 }
00224                                 else
00225                                 {
00226                                         method.MethodName = NULL;
00227                                         method.Inheritance = -1;
00228                                         method.MethodNum = -1;
00229                                 }
00230                         }
00231                         else
00232                         {
00233                                 
00234                                 method.MethodName = NULL;
00235                                 method.Inheritance = -1;
00236                                 method.MethodNum = -1;
00237                                 
00238                         }
00239                         baseName->release();
00240                 }       
00241                 
00242                 return method;
00243         }
00244 
00245         const NLAIAGENT::IObjectIA *CCompilateur::validateHierarchyMethode(std::list<sint32> &listH,sint32 &h,const NLAIAGENT::IObjectIA *classType,NLAIAGENT::IBaseGroupType &listName)
00246         {
00247                 std::list<NLAISCRIPT::CStringType> listClassName;               
00248                         
00249                 NLAIAGENT::CIteratorContener it = listName.getIterator();                                               
00250 
00251                 while(!it.isInEnd())
00252                 {
00253                         NLAIAGENT::CStringType &name = *((NLAIAGENT::CStringType *)it++);
00254                         listClassName.push_back(name.getStr().getString());                     
00255                 }               
00256                 listClassName.pop_back();               
00257                 return getValidateHierarchyBase(classType,listH,h,listClassName);                               
00258         }
00259 
00260         sint32 CCompilateur::runTypeConstraint()
00261         {
00262                 sint32 n = 1;   
00263                 sint32 did = _ConstraintType.size();
00264                 std::list<IConstraint *>::iterator j,i;         
00265                 while(n != 0)
00266                 {
00267                         i = _ConstraintType.begin();
00268                         n = 0;
00269                         while(i != _ConstraintType.end())
00270                         {
00271                                 j = i ++;
00272                                 IConstraint *c = *j;
00273                                 c->run(*this);
00274                                 if( c->satisfied() ) 
00275                                 {                               
00276                                         _ConstraintType.erase(j);
00277                                         n++;
00278                                         c->release();
00279                                 }                       
00280                         }               
00281                         did -= n;
00282                 }
00283                 return did;
00284         }
00285 
00286         sint32 CCompilateur::runMethodConstraint()
00287         {                       
00288 
00289 
00290                 sint32 n = 1;
00291                 while(n)
00292                 {               
00293                         std::list<IConstraint *>::iterator i = _MethodConstraint.begin();
00294                         n = 0;
00295 
00296                         while(i != _MethodConstraint.end())
00297                         {                       
00298                                 IConstraint *c = *i++;                          
00299                                 if(!c->satisfied())
00300                                 {
00301                                         c->run(*this);
00302                                         if(c->satisfied()) n++;
00303                                 }                               
00304                         }                       
00305                         if(runTypeConstraint()) errorTypeConstraint();
00306                 }
00307                 
00308                 
00309                 std::list<IConstraint *>::iterator j,i = _MethodConstraint.begin();             
00310                 n = 0;
00311                 while(i != _MethodConstraint.end())
00312                 {                       
00313                         j = i;
00314                         i ++;
00315                         IConstraint *c = *j;                    
00316                         if( c->satisfied() ) 
00317                         {                               
00318                                 _MethodConstraint.erase(j);
00319                                 n++;
00320                                 c->release();
00321                         }
00322                 }               
00323                 return n;
00324         }
00325         void CCompilateur::errorTypeConstraint()
00326         {
00327                 std::list<IConstraint *>::iterator j,i = _ConstraintType.begin();
00328                 while(i != _ConstraintType.end())
00329                 {
00330                         j = i ++;
00331                         IConstraint *c = *j;                    
00332                         if( !c->satisfied() ) 
00333                         {                               
00334                                 _ConstraintType.erase(j);
00335                                 //yyerror((char *)c->getInfo());
00336                                 c->release();                           
00337                         }                       
00338                 }
00339         }
00340         void CCompilateur::errorMethodConstraint()
00341         {
00342                 char txt[1024*16];
00343 
00344                 std::list<IConstraint *>::iterator j,i = _MethodConstraint.begin();             
00345                 while(i != _MethodConstraint.end())
00346                 {
00347                         j = i ++;
00348                         IConstraint *c = *j;                    
00349                         c->getError(txt);                       
00350                         yyLine = c->getLine();
00351                         yyColone = c->getColone();
00352                         yyerror(txt);
00353                         if(_ConstraintType.begin() != _ConstraintType.end()) _ConstraintType.erase(j);
00354                         c->release();
00355                 }
00356         }
00357         
00358         void CCompilateur::cleanTypeList()
00359         {
00360                 while(_TypeList.size())
00361                 {
00362                         _TypeList.back()->release();
00363                         _TypeList.pop_back();
00364                 }
00365         }
00366         void CCompilateur::clean()
00367         {
00368                 sint32 i;
00369                 i = _LastBloc1.size();
00370                 while(i --)
00371                 {       
00372                         if(_LastBloc != _LastBloc1.back()) _LastBloc1.back()->release();
00373                         _LastBloc1.pop_back();
00374                 }
00375                 i = _LastBloc2.size();
00376                 while(i --)
00377                 {       
00378                         if(_LastBloc != _LastBloc2.back()) _LastBloc2.back()->release();
00379                         _LastBloc2.pop_back();
00380                 }               
00381 
00382                 i = _LastBloc3.size();
00383                 while(i --)
00384                 {       
00385                         if(_LastBloc != _LastBloc3.back()) _LastBloc3.back()->release();
00386                         _LastBloc3.pop_back();
00387                 }               
00388 
00389                 //sint32 i = _LastSaveBloc.size();
00390                 i = _LastSaveBloc.size();
00391                 while(i --)
00392                 {       
00393                         IBlock *k = _LastSaveBloc.back();                               
00394                         if(_LastBloc != k) k->release();
00395                         _LastSaveBloc.pop_back();
00396                 }
00397                 if(_LastBloc !=NULL) _LastBloc->release();
00398                 _LastBloc = NULL;                       
00399                 while(_Attrib.size())
00400                 {       
00401                         _Attrib.back().first->release();
00402                         _Attrib.back().second->release();
00403                         _Attrib.pop_back();
00404                 }                               
00405                 
00406                 while(_Param.size())
00407                 {
00408                         _Param.back()->release();
00409                         _Param.pop_back();
00410                 }
00411 
00412                 while(_LastStringParam.size())
00413                 {
00414                         _LastStringParam.back()->release();
00415                         _LastStringParam.pop_back();
00416                 }               
00417                 
00418 
00419                 if(_ExpressionType != NULL) 
00420                 {
00421                         _ExpressionType->release();
00422                         _ExpressionType = NULL;
00423                 }
00424                 if(_FlotingExpressionType != NULL)
00425                 {
00426                         _FlotingExpressionType->release();
00427                         _FlotingExpressionType = NULL;
00428                 }
00429 
00430                 cleanTypeList();                
00431         }
00432 
00433         void CCompilateur::cleanMethodConstraint()
00434         {
00435                 while(_MethodConstraint.size())
00436                 {       
00437                         //IConstraint *c = _MethodConstraint.back();
00438                         //c->release();
00439                         _MethodConstraint.pop_back();
00440                 }
00441         }
00442 
00443         void CCompilateur::cleanTypeConstraint()
00444         {
00445                 while(_ConstraintType.size())
00446                 {       
00447                         _ConstraintType.back()->release();
00448                         _ConstraintType.pop_back();
00449                 }
00450         }
00451 
00452         IConstraint *CCompilateur::getMethodConstraint(const NLAIAGENT::IBaseGroupType &g,const CParam &p) const
00453         {
00454                 std::list<IConstraint *>::const_iterator i = _MethodConstraint.begin();
00455                 while(i != _MethodConstraint.end())
00456                 {
00457                         if((*i)->getTypeOfClass() == CConstraintMethode::constraintMethode)
00458                         {
00459                                 const CConstraintMethode *o = (const CConstraintMethode *)*i;
00460                                 if(o->isEqual(g,p)) return *i;
00461                         }                       
00462                         i++;
00463                 }
00464                 return false;
00465         }
00466 
00467         IConstraint *CCompilateur::getMethodConstraint(const IConstraint&c) const
00468         {
00469                 std::list<IConstraint *>::const_iterator i = _MethodConstraint.begin();
00470                 while(i != _MethodConstraint.end())
00471                 {                       
00472                         const IConstraint *o = *i;
00473                         if(*o == c) return *i;                  
00474                         i++;
00475                 }
00476                 return false;
00477         }
00478                 
00479         void CCompilateur::setNewLine()
00480         {
00481                 if (_Debug)
00482                 {
00483                         if (_LastBloc != NULL)
00484                         {
00485                                 _LastBloc->setCurrentLine(yyLine);
00486                                 _LastBloc->setFirstOpCodeInLine(true);
00487                         }
00488                 }
00489         }
00490 
00491         void CCompilateur::addOpCode(IOpCode *x)
00492         {
00493 
00494                 //CLdbOpCode *x = new CLdbOpCode (NLAIAGENT::CGroupType());
00495                 _LastBloc->addCode(x);
00496         }
00497 }