# 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  

yacc.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/type_def.h"
00027 #include "nel/ai/script/object_unknown.h"
00028 #include "nel/ai/script/constraint_stack_component.h"
00029 #include "nel/ai/script/constraint_find_run.h"
00030 #include "nel/ai/agent/main_agent_script.h"
00031 #include "nel/ai/script/interpret_object_manager.h"
00032 #include "nel/ai/agent/performative.h"
00033 
00034 namespace NLAISCRIPT
00035 {               
00036         void CCompilateur::interrogationEnd()
00037         {               
00038                 _VarState -= (sint32)_VarState  - _VarState.popMark();          
00039                 if (_Debug)
00040                 {
00041                         _LastBloc1.back()->addCode((new CFreeAllocDebug));
00042                 }
00043                 else
00044                 {
00045                         _LastBloc1.back()->addCode((new CFreeAlloc));
00046                 }
00047                 sint32 i =_LastBloc1.back()->listCode().size();
00048                 _LastBloc = _LastSaveBloc.back();
00049                 _LastBloc->release();
00050                 _LastBloc->addCode(new CJFalseOpCode (i));
00051                 while(i --)
00052                 {                                                                       
00053                         _LastBloc->pushCode(_LastBloc1.back()->getFront());
00054                         CBagOfCode *b = _LastBloc1.back()->listCode().front();
00055                         (_LastBloc->getBagOfCode())->addConstraint(b);
00056                         delete b;
00057                         _LastBloc1.back()->listCode().pop_front();
00058                 }
00059                 _LastBloc1.back()->release();
00060                 _LastBloc1.pop_back();
00061         }
00062 
00063         void CCompilateur::ifInterrogation()
00064         {
00065                 _LastSaveBloc.push_back(_LastBloc);
00066                 _LastSaveBloc.back()->incRef();
00067                 _LastBloc1.push_back(new IBlock(_Debug));
00068                 _LastBloc1.back()->incRef();
00069                 _LastBloc = _LastBloc1.back();
00070                 _VarState.pushMark();
00071                 _LastBloc->addCode((new CMarkAlloc));
00072                 if(_ExpressionType)
00073                 {
00074                         _ExpressionType->release();
00075                         _ExpressionType = NULL;
00076                 }               
00077         }
00078 
00079         void CCompilateur::ifInterrogationPoint()
00080         {
00081                 _VarState -= (sint32)_VarState  - _VarState.popMark();          
00082                 if (_Debug)
00083                 {
00084                         _LastBloc1.back()->addCode((new CFreeAllocDebug));
00085                 }
00086                 else
00087                 {
00088                         _LastBloc1.back()->addCode((new CFreeAlloc));
00089                 }
00090                 _LastBloc2.push_back(new IBlock(_Debug));
00091                 _LastBloc2.back()->incRef();
00092                 _LastBloc = _LastBloc2.back();
00093                 _VarState.pushMark();
00094                 _LastBloc->addCode((new CMarkAlloc));
00095         }
00096 
00097         void CCompilateur::ifInterrogationEnd()
00098         {
00099                 _VarState -= (sint32)_VarState  - _VarState.popMark();
00100                 
00101                 if (_Debug)
00102                 {
00103                         _LastBloc2.back()->addCode((new CFreeAllocDebug));
00104                 }
00105                 else
00106                 {
00107                         _LastBloc2.back()->addCode((new CFreeAlloc));
00108                 }
00109                 _LastBloc = _LastSaveBloc.back();
00110                 _LastBloc->release();
00111                 _LastSaveBloc.pop_back();
00112                 sint32 i =_LastBloc1.back()->listCode().size();
00113                 _LastBloc->addCode(new CJFalseOpCode (i + 1));
00114                 while(i --)
00115                 {
00116                         _LastBloc->pushCode(_LastBloc1.back()->getFront());
00117                         CBagOfCode *b = _LastBloc1.back()->listCode().front();
00118                         (_LastBloc->getBagOfCode())->addConstraint(b);
00119                         delete b;
00120                         _LastBloc1.back()->listCode().pop_front();
00121                 }
00122                 i =_LastBloc2.back()->listCode().size();
00123                 _LastBloc->addCode(new CJmpOpCode (i));
00124                 while(i --)
00125                 {
00126                         _LastBloc->pushCode(_LastBloc2.back()->getFront());
00127                         CBagOfCode *b = _LastBloc2.back()->listCode().front();
00128                         (_LastBloc->getBagOfCode())->addConstraint(b);
00129                         delete b;
00130                         _LastBloc2.back()->listCode().pop_front();
00131                 }
00132                 _LastBloc1.back()->release();
00133                 _LastBloc1.pop_back();
00134                 _LastBloc2.back()->release();
00135                 _LastBloc2.pop_back();
00136         }
00137         
00138         sint32 CCompilateur::affectation()
00139         {
00140                 if(_LasAffectationVarStr.size() >= 1)
00141                 {                       
00142                         if(_LasAffectationVarStr.size() == 1)
00143                         {
00144                                 _LastAffectation = getVar(_LasAffectationVarStr.front().data());
00145                                 if(_LastAffectation == NULL)
00146                                 {
00147                                         sint32 member = ((IClassInterpret *)_SelfClass.get())->getStaticMemberIndex(NLAIAGENT::CStringVarName(_LasAffectationVarStr.front().data()));
00148                                         if(member < 0)
00149                                         {
00150                                                 NLAIAGENT::IObjectIA *i;
00151                                                 if(_ExpressionType->satisfied())
00152                                                 {
00153                                                         NLAIC::CIdentType *id = new NLAIC::CIdentType(*_ExpressionType->getConstraintTypeOf());
00154                                                         IOpType * c= new COperandSimple(id);
00155                                                         i = new CObjectUnknown(c);
00156                                                         _ExpressionType->release();
00157                                                         _ExpressionType = NULL;
00158                                                 }
00159                                                 else
00160                                                 {                                                                                       
00161                                                         _ExpressionType->incRef();
00162                                                         i = new CObjectUnknown(_ExpressionType);                                                        
00163                                                         _ConstraintType.push_back(_ExpressionType);
00164                                                         _ExpressionType = NULL;
00165                                                 }                                               
00166                                                 
00167                                                 #ifdef NL_DEBUG
00168                                                 _LastAffectation = new CVarPStackParam((sint32)_VarState,_LasAffectationVarStr.front().data());
00169                                                 #else
00170                                                 _LastAffectation = new CVarPStackParam((sint32)_VarState);
00171                                                 #endif
00172 
00173                                                 _LastBloc->allocLocVar(_LasAffectationVarStr.front().data(), _LastAffectation);
00174 
00175                                                 _Heap[(int)_Heap] = i;
00176                                                 _Heap ++;
00177                                                 _VarState ++;
00178                                                 if(_LastAffectation)
00179                                                 {
00180                                                         if (_Debug)
00181                                                         {
00182                                                                 _LastBloc->addCode(new CLocAllocDebug());
00183                                                                 _LastBloc->addCode(new CAffOpCodeDebug( ((CVarPStackParam *)_LastAffectation)->getIndex(), _LasAffectationVarStr.front().data()));
00184                                                         }
00185                                                         else
00186                                                         {
00187                                                                 _LastBloc->addCode(new CLocAlloc());
00188                                                                 _LastBloc->addCode(new CAffOpCode( ((CVarPStackParam *)_LastAffectation)->getIndex()));
00189                                                         }
00190                                                 }
00191                                         }
00192                                         else
00193                                         {
00194                                                 
00195                                                 if(!affectationMember(member)) return false;
00196                                         }
00197                                 }
00198                                 else
00199                                 {
00200                                         if (_Debug)
00201                                         {
00202                                                 _LastBloc->addCode(new CAffOpCodeDebug( ((CVarPStackParam *)_LastAffectation)->getIndex(), _LasAffectationVarStr.front().data()));
00203                                         }
00204                                         else
00205                                         {
00206                                                 _LastBloc->addCode(new CAffOpCode( ((CVarPStackParam *)_LastAffectation)->getIndex()));
00207                                         }
00208                                         sint index = ((CVarPStackParam *)_LastAffectation)->getIndex();
00209 
00210                                         _LastBloc->eraseVar(_LasAffectationVarStr.front().data());                                      
00211 
00212                                         #ifdef NL_DEBUG
00213                                         _LastAffectation = new CVarPStackParam(index,_LasAffectationVarStr.front().data());
00214                                         #else
00215                                         _LastAffectation = new CVarPStackParam(index);
00216                                         #endif
00217 
00218                                         _LastBloc->allocLocVar(_LasAffectationVarStr.front().data(), _LastAffectation);
00219 
00220                                         NLAIAGENT::IObjectIA *i;
00221                                         if(_ExpressionType->satisfied())
00222                                         {
00223                                                 IOpType * c= new COperandSimple(new NLAIC::CIdentType(*_ExpressionType->getConstraintTypeOf()));                                                        
00224                                                 i = new CObjectUnknown(c);
00225                                                 _ExpressionType->release();
00226                                                 _ExpressionType = NULL;
00227                                         }
00228                                         else
00229                                         {                                                                                       
00230                                                 _ExpressionType->incRef();
00231                                                 i = new CObjectUnknown(_ExpressionType);
00232                                                 _ExpressionType->incRef();
00233                                                 _ConstraintType.push_back(_ExpressionType);
00234                                                 _ExpressionType = NULL;
00235                                         }
00236                                         _Heap[index]->release();
00237                                         _Heap[index] = i;
00238 
00239                                 }       
00240                         }
00241                         else
00242                         {
00243                                 _LastAffectation = getVar(_LasAffectationVarStr.front().data());
00244                                 if(_LastAffectation == NULL)
00245                                 {                               
00246                                         if(!PrivateError())
00247                                         {                                       
00248                                                 return false;
00249                                         }
00250                                         
00251                                         if(_ExpressionType->satisfied())
00252                                         {
00253                                                 _ExpressionType->release();
00254                                         }
00255                                         else
00256                                         {
00257                                                 _ConstraintType.push_back(_ExpressionType);
00258                                         }
00259                                         _ExpressionType = NULL;
00260                                 }
00261                                 else
00262                                 {                                       
00263                                         _LasAffectationVarStr.pop_front();                                      
00264                                         IOpType *cont = (IOpType *)((const CObjectUnknown *)((CVarPStackParam *)_LastAffectation)->getObject())->getBaseType();
00265                                         if(cont->satisfied())
00266                                         {
00267                                                 std::list<sint32> ref;
00268                                                 IOpType *constr;
00269                                                 IClassInterpret *cl = (IClassInterpret *)((CClassInterpretFactory *)cont->getConstraintTypeOf()->getFactory())->getClass();
00270                                                 if(!isValidateVarName(cl,ref,_LasAffectationVarStr,constr))
00271                                                 {
00272                                                         if(constr) constr->release();
00273                                                         return false;           
00274                                                 }
00275                                                 COperationTypeGD *dg = new COperationTypeGD();
00276                                                 dg->setOperationD(constr);
00277                                                 dg->setOperationG(_ExpressionType);
00278                                                 dg->setOp(NLAIC::CTypeOfOperator::opAff);
00279                                                 _ConstraintType.push_back(dg);                                          
00280                                                 _ExpressionType = NULL;                                         
00281                                                 _LastBloc->addCode(new CAffHeapMemberiOpCode( ref,((CVarPStackParam *)_LastAffectation)->getIndex()));
00282                                                 
00283 
00284                                         }
00285                                         else
00286                                         {
00287                                                 _LastBloc->addCode(new CHaltOpCode());
00288                                                 CBagOfCode *b = _LastBloc->getBagOfCode();
00289                                                 cont->incRef();
00290                                                 IOpType *c = (IOpType *)getMethodConstraint(CConstraintStackComp(CConstraintStackComp::heapAffectation,
00291                                                                                                                                                                                 ((CVarPStackParam *)_LastAffectation)->getIndex(),
00292                                                                                                                                                                                 _LasAffectationVarStr ,cont,0,0));
00293                                                 if(c == NULL)
00294                                                 {
00295                                                         cont->incRef();
00296                                                         c = new CConstraintStackComp(CConstraintStackComp::heapAffectation,((CVarPStackParam *)_LastAffectation)->getIndex(),
00297                                                                                                                                                                                         _LasAffectationVarStr ,cont,0,0);
00298                                                         _ConstraintType.push_back(c);
00299                                                 }
00300                                                 
00301                                                 c->incRef();
00302                                                 COperationTypeGD *dg = new COperationTypeGD();
00303                                                 dg->setOperationD(c);
00304                                                 dg->setOperationG(_ExpressionType);
00305                                                 dg->setOp(NLAIC::CTypeOfOperator::opAff);                                               
00306                                                 _ConstraintType.push_back(dg);                                          
00307                                                 _ExpressionType = NULL;                                         
00308                                                 b->addConstraint(c);
00309                                         }
00310                                 }
00311                                 
00312                         }                                                                       
00313                 }
00314                 else
00315                 {
00316                         yyerror("erreur de definition de variable");
00317                         return 0;
00318                 }                               
00319                 return true;
00320         }
00321 
00322         sint32 CCompilateur::affectationMember(sint32 member)
00323         {
00324                 return affectationMember((IClassInterpret *)_SelfClass.get(),member);
00325         }
00326 
00327         sint32 CCompilateur::affectationMember(IClassInterpret *base,sint32 member)
00328         {               
00329                 try
00330                 {                                                                                                                       
00331                         _LastBloc->addCode(new CAffMemberOpCode(member));
00332                         COperandSimple *a = new COperandSimple(new NLAIC::CIdentType(base->getStaticMember(member)->getType()));
00333                         COperationTypeGD *gd = new COperationTypeGD();
00334                         gd->setOperationG(a);
00335                         gd->setOperationD(_ExpressionType);
00336                         gd->setOp(NLAIC::CTypeOfOperator::opAff);
00337                         if(gd->satisfied())
00338                         {
00339                                 gd->release();
00340                         }
00341                         else
00342                         {                               
00343                                 _ConstraintType.push_back(gd);  
00344                         }
00345                         _ExpressionType = NULL;
00346                         
00347                 }
00348                 catch(NLAIE::IException &)
00349                 {
00350                         std::string text;
00351                         text = NLAIC::stringGetBuild("Bad reference or class reference undefined");
00352                         yyerror((char *)text.c_str());
00353                         return 0;
00354                 }
00355                 return true;
00356         }
00357 
00358         sint32 CCompilateur::PrivateError()
00359         {               
00360                 std::list<sint32> ref;
00361                 IOpType *c;
00362                 if(!isValidateVarName(ref,_LasAffectationVarStr,c))
00363                 {
00364                         if(c) c->release();
00365                         return false;                   
00366                 }
00367                 if(ref.size() == 1)
00368                 {
00369                         _LastBloc->addCode(new CAffMemberOpCode(ref.back()));                   
00370                 }
00371                 else
00372                 {
00373                         _LastBloc->addCode(new CAffMemberiOpCode        (ref));
00374                 }
00375                 
00376                 if(c) c->release();
00377                 return true;
00378         }       
00379 
00380         sint32 CCompilateur::isValidateVarName(std::list<sint32> &ref,std::list<NLAISCRIPT::CStringType> &listName,IOpType *&type)
00381         {               
00382                 return isValidateVarName(_SelfClass.get(),ref,listName,type);
00383         }
00384         
00385         sint32 CCompilateur::isValidateVarName(const NLAIAGENT::IObjectIA *base,std::list<sint32> &ref,std::list<NLAISCRIPT::CStringType> &listName,IOpType *&type)
00386         {               
00387                 NLAIAGENT::CStringVarName varName(listName.back().data());
00388                 sint32 h;
00389                 listName.pop_back();
00390                 type = NULL;            
00391                 const NLAIAGENT::IObjectIA *baseClass = getValidateHierarchyBase(base,ref,h,listName);          
00392 
00393                 if(baseClass)
00394                 {               
00395                         sint32 i = baseClass->getStaticMemberIndex(varName);
00396                         if(i>=0)
00397                         {                               
00398                                 const NLAIAGENT::IObjectIA *c = baseClass->getStaticMember(i);
00399                                 /*if((const NLAIC::CTypeOfObject &)baseClass->getType() & NLAIC::CTypeOfObject::tAgentInterpret) 
00400                                 {
00401                                         i += getCompementShift((IClassInterpret *)baseClass);
00402                                 }*/
00403                                 if(c != NULL)
00404                                 {
00405                                         ref.push_back(i);                                       
00406                                         type = new COperandSimple(new NLAIC::CIdentType(c->getType()));
00407                                         return true;
00408                                 }
00409                                 else 
00410                                 {
00411                                         return false;
00412                                 }
00413                                 
00414                         }                                       
00415                 }
00416                 return false;           
00417         }
00418 
00419         sint32 CCompilateur::affectationiMember(const IClassInterpret *baseClass,std::list<NLAISCRIPT::CStringType> &varHName)
00420         {
00421 
00422                 return true;            
00423         }       
00424 
00425         void CCompilateur::allocExpression(IOpCode *op,bool gd)
00426         {
00427 #ifdef NL_DEBUG
00428         std::list<sint32>::iterator i_dbg = _LastFact.Member.begin();
00429         while(i_dbg != _LastFact.Member.end())
00430         {
00431                 int k = *i_dbg++; 
00432         }
00433 
00434 #endif
00435                 switch(_LastFact.VarType)
00436                 {
00437                         case varTypeImediate:
00438                                 if(!_LastFact.IsUsed)
00439                                 {
00440                                         CLdbOpCode *x = new CLdbOpCode (*_LastFact.Value);
00441                                         _LastBloc->addCode(x);
00442                                         _LastFact.IsUsed = true;
00443                                 }
00444                                 if(op) _LastBloc->addCode(op);
00445                                 break;
00446                                 
00447                         case varTypeLocal:
00448                                 if(!_LastFact.IsUsed)
00449                                 {                                                                                               
00450                                         if(((NLAIAGENT::IObjectIA       *)_LastFact.ValueVar)->getType() == CVarPStackParam::IdVarPStackParam)
00451                                         {
00452                                                 _LastBloc->addCode(new CLdbRefOpCode ( ((CVarPStackParam *)((NLAIAGENT::IObjectIA *)_LastFact.ValueVar))->getIndex()));
00453                                         }
00454                                         else 
00455                                         {
00456                                                 _LastBloc->addCode(new CLdbOpCode (*((NLAIAGENT::IObjectIA *)_LastFact.ValueVar)));
00457                                         }                                                                                       
00458                                         _LastFact.IsUsed = true;
00459                                 }
00460                                 if(op) _LastBloc->addCode(op);
00461                                 break;
00462                         case varTypeMember:
00463                                 if(!_LastFact.IsUsed)
00464                                 {
00465                                         if(_LastFact.Member.size() == 1)
00466                                         {
00467                                                 _LastBloc->addCode(new CLdbMemberOpCode(_LastFact.Member.back()));                      
00468                                         }
00469                                         else
00470                                         {
00471                                                 _LastBloc->addCode(new CLdbMemberiOpCode(_LastFact.Member));
00472                                         }
00473                                         
00474                                         _LastFact.IsUsed = true;
00475                                 }
00476                                 if(op) _LastBloc->addCode(op);
00477                                 break;
00478 
00479                         case varForFunc:
00480                                 if(op) _LastBloc->addCode(op);
00481                                 break;
00482 
00483                         case varTypeHeapMember:
00484                                 if(!_LastFact.IsUsed)
00485                                 {
00486                                         if(_LastFact.Member.size())
00487                                         {
00488                                                 _LastBloc->addCode(new CLdbHeapMemberiOpCode(_LastFact.Member,((CVarPStackParam *)_LastFact.ValueVar)->getIndex()));
00489                                         }
00490                                         else
00491                                         {
00492                                                 _ConstraintType.push_back(_LastFact.TypeStack);                                         
00493                                                 _LastBloc->addCode(new CHaltOpCode());
00494                                                 CBagOfCode *b = _LastBloc->getBagOfCode();
00495                                                 b->addConstraint(_LastFact.TypeStack);
00496                                                 _LastFact.TypeStack = NULL;
00497                                         }
00498                                         _LastFact.IsUsed = true;
00499                                 }
00500                                 if(op) _LastBloc->addCode(op);
00501                                 break;
00502                                 
00503                         case varTypeStackMember:
00504                                 if(!_LastFact.IsUsed)
00505                                 {
00506                                         if(_LastFact.Member.size())
00507                                         {
00508                                                 _LastBloc->addCode(new CLdbStackMemberiOpCode(_LastFact.Member));
00509                                         }
00510                                         else
00511                                         {
00512                                                 _ConstraintType.push_back(_LastFact.TypeStack);                                         
00513                                                 _LastBloc->addCode(new CHaltOpCode());
00514                                                 CBagOfCode *b = _LastBloc->getBagOfCode();
00515                                                 b->addConstraint(_LastFact.TypeStack);
00516                                                 _LastFact.TypeStack = NULL;
00517                                         }
00518                                         _LastFact.IsUsed = true;
00519                                 }
00520                                 if(op) _LastBloc->addCode(op);
00521                                 break;
00522                         default: break;
00523                 }
00524         }
00525 
00526         void CCompilateur::nameMethodeProcessing()
00527         {       
00528                 NLAIAGENT::IBaseGroupType *g = new NLAIAGENT::CGroupType;
00529                 _LastStringParam.push_back(g);          
00530                 
00531                 std::list<NLAISCRIPT::CStringType>::iterator i = _LasVarStr.begin();
00532                 while(i != _LasVarStr.end())
00533                 {
00534                         NLAISCRIPT::CStringType &s = *i++;
00535                         g->cpy(NLAIAGENT::CStringType(NLAIAGENT::CStringVarName(s.data())));
00536                 }
00537         }
00538 
00539         void CCompilateur::RegisterClass()
00540         {
00541                 IClassInterpret *c = (IClassInterpret *)_SelfClass.get();
00542 //              c->initStatics();
00543                 const NLAIAGENT::IVarName &className = *c->getClassName();              
00544 #ifdef NL_DEBUG
00545         className.getString();
00546         _ConstraintType.size();
00547 #endif
00548                 NLAIC::CIdentType id(className.getString(),(const NLAIC::IClassFactory &)CClassInterpretFactory(c), 
00549                                                         NLAIC::CTypeOfObject(c->getTypeClass()), NLAIC::CTypeOfOperator(0));
00550 
00551 
00552                 NLAIC::CIdentType *x = new NLAIC::CIdentType(id);
00553 
00554                 c->setType(x);
00555         }
00556 
00557         bool CCompilateur::computContraint()
00558         {                               
00559 
00560                 sint32 constraint = getNMethodConstraint();
00561                 if((constraint - runMethodConstraint()))
00562                 {                       
00563                         errorMethodConstraint();
00564                         cleanMethodConstraint();
00565                         errorTypeConstraint();
00566                         cleanTypeConstraint();
00567                         return 0;                                       
00568                 }
00569                 
00570                 /*constraint = _ConstraintType.size() - runTypeConstraint();
00571                 while(_ConstraintType.size())
00572                 {
00573                         delete _ConstraintType.back();
00574                         _ConstraintType.pop_back();
00575                 }*/
00576                 
00577                 cleanMethodConstraint();
00578                 cleanTypeConstraint();                  
00579                 IClassInterpret *c = (IClassInterpret *)_SelfClass.get();
00580                 try
00581                 {
00582 //                      ((CAgentClass *)c)->buildChildsMessageMap();
00583                         ((CAgentClass *)c)->initStatics();
00584                 }
00585                 catch(NLAIE::IException &e)
00586                 {
00587                         std::string text;
00588                         text = NLAIC::stringGetBuild("can't find '%s'",e.what());
00589                         yyerror((char *)text.c_str());  
00590                         return false;                                   
00591                 }
00592                 return true;
00593         }
00594 
00595 
00596         void CCompilateur::initParam()
00597         {
00598                 clean();
00599                 _LastBloc = new IBlock(_Debug);
00600                 _Heap -= (sint32)_Heap;
00601                 CVarPStack::_LocalTableRef = &_Heap[0];
00602                 _VarState.clear();                                                              
00603                 _VarState.pushMark();
00604                 if(!_InLineParse) _LastBloc->addCode((new CMarkAlloc));
00605                 _Param.push_back(new CParam);
00606         }
00607 
00608         bool CCompilateur::registerMethod()
00609         {
00610                 bool isRun = false;
00611                 sint i;
00612                 const char *r = (const char *)_RUN_;
00613 
00614                 if(r[0] == _MethodName.back().getString()[0] && r[1] == _MethodName.back().getString()[1])
00615                 {
00616                         std::list<const char *> listRun;
00617                 
00618                         listRun.push_back((const char *)NLAIAGENT::CPExec::IdPExec);
00619                         listRun.push_back((const char *)NLAIAGENT::CPAchieve::IdPAchieve);
00620                         listRun.push_back((const char *)NLAIAGENT::CPAsk::IdPAsk);
00621                         listRun.push_back((const char *)NLAIAGENT::CPBreak::IdPBreak);
00622                         listRun.push_back((const char *)NLAIAGENT::CPTell::IdPTell);
00623                         listRun.push_back((const char *)NLAIAGENT::CPKill::IdPKill);
00624                         listRun.push_back((const char *)NLAIAGENT::CPError::IdPError);
00625                         listRun.push_back((const char *)NLAIAGENT::CPExec::IdPExec);
00626                         
00627                         char nameRun[1024];                     
00628                         for(i = 0; i < (sint)listRun.size(); i ++)
00629                         {
00630                                 strcpy(nameRun,_RUN_);
00631                                 strcat(nameRun,listRun.back());
00632                                 listRun.pop_back();
00633                                 if(!strcmp(_MethodName.back().getString(),nameRun))
00634                                 {
00635                                         isRun = true;
00636                                         isRunMsg = true;
00637                                         break;
00638                                 }
00639                         }       
00640                         if(!isRun)
00641                         {
00642                                 strcpy(nameRun,_RUN_);                                                          
00643                                 if(!strcmp(_MethodName.back().getString(),nameRun))
00644                                 {
00645                                         isRun = true;                                   
00646                                 }
00647                         }
00648                 }
00649                                 
00650                 bool isSend =  !strcmp(_MethodName.back().getString(),_SEND_);
00651                 bool runProcces = false;
00652                 if( isRun || isSend )
00653                 {
00654                         CParam p;                       
00655                         COperandSimple *x = new COperandSimple(new NLAIC::CIdentType("Message"));
00656                         p.push(x);                                              
00657                         
00658                         if( ( isRun && _Param.back()->size() > 1 )  ||  isSend)
00659                         {
00660                                 std::string text;
00661                                 text = NLAIC::stringGetBuild("method %s have more than 1 parametre",_MethodName.back().getString());
00662                                 yyerror((char *)text.c_str());
00663                                 return false;
00664                         }
00665                         else
00666                         if(_Param.back()->size())
00667                         {
00668                                 if(p.eval(*_Param.back()) < 0.0)
00669                                 {
00670                                         std::string text;
00671                                         text = NLAIC::stringGetBuild("the method %s have a parametre not derived from Message class",_MethodName.back().getString());
00672                                         yyerror((char *)text.c_str());
00673                                         return false;
00674                                 }
00675                         }
00676                         else
00677                         if(isRun)
00678                         {
00679                                 runProcces = true;                              
00680                         }
00681                 }
00682                 
00683                 sint32 indexMethod;
00684                 try
00685                 {
00686                         indexMethod = ((IClassInterpret *)_SelfClass.get())->addBrancheCode(_MethodName.back(),*_Param.back());
00687                 }
00688                 catch(NLAIE::IException &err)
00689                 {
00690                         yyerror((char *)err.what());
00691 
00692                         return false;
00693                 }
00694 
00695                 if(runProcces)
00696                 {
00697                         if( ((IClassInterpret *)_SelfClass.get())->getRunMethod() >=0 )
00698                         {
00699                                 std::string text;
00700                                 text = NLAIC::stringGetBuild("the method Run() have all ready defined");
00701                                 yyerror((char *)text.c_str());
00702                                 return false;
00703                         }
00704                         ((IClassInterpret *)_SelfClass.get())->setRunMethod(indexMethod);
00705                 }
00706                 i = 0;
00707                 _DecalageHeap = _Attrib.size();
00708                 _Heap.setShift(_DecalageHeap);
00709                 CVarPStackParam::_Shift = _DecalageHeap;
00710 
00711                 NLAIAGENT::CStringType* debugStringAttrib;
00712                 NLAIAGENT::CVectorGroupType* debugAttrib;
00713                 if (_Debug)
00714                 {
00715                         debugAttrib = new NLAIAGENT::CVectorGroupType();                        
00716                 }
00717                 else debugAttrib = NULL;
00718 
00719                 while(_Attrib.size() != 0)
00720                 {
00721                         i--;
00722 #ifdef NL_DEBUG
00723                         _LastAffectation = new CVarPStackParam(i,_Attrib.back().first->getString());
00724 #else
00725                         _LastAffectation = new CVarPStackParam(i);
00726 #endif
00727                         // We put the functions atributs in the _LastBloc dico.
00728                         if(!_LastBloc->allocLocVar(_Attrib.back().first->getString(),_LastAffectation))
00729                         {
00730                                 std::string text;
00731                                 text = NLAIC::stringGetBuild("variable '%s' already defined",_Attrib.back().first->getString());
00732                                 yyerror((char *)text.c_str());
00733                                 _Heap -= (sint32)_Heap;
00734                                 if(_Heap.restoreStackState()) _Heap.restoreStack();
00735                                 if(_Heap.restoreShiftState()) _Heap.restoreShift();
00736                                 _Heap -= (sint32)_Heap;                                                 
00737                                 _LastAffectation->release();
00738                                 return 0;
00739                         }
00740                         if (_Debug)
00741                         {
00742                                 // We store the function var name;
00743                                 debugStringAttrib = new NLAIAGENT::CStringType(*(_Attrib.back().first));
00744                                 debugAttrib->pushFront(debugStringAttrib);
00745                         }
00746                         _Attrib.back().first->release();
00747                         _Attrib.back().second->release();
00748                         _Attrib.pop_back();
00749                 }
00750 
00751                 CMethodeName &thisMethod = ((IClassInterpret *)_SelfClass.get())->getBrancheCode();
00752                 thisMethod.getParam().setInfo(debugAttrib);
00753                 /*if (_Debug)
00754                 {
00755                         _LastBloc->addCode(new CAddParamNameDebug(*debugAttrib));
00756                         debugAttrib->release();
00757                 }*/
00758                 //_Param.back()->clear();
00759                 _Param.back()->release();
00760                 _Param.pop_back();
00761                 return true;
00762         }
00763 
00764         
00765         sint32 CCompilateur::typeOfMethod()
00766         {                               
00767 
00768                 const IOpType *a;
00769                 if((a = ((IClassInterpret *)_SelfClass.get())->getBrancheCode().getTypeOfMethode()) == NULL)
00770                 {                                               
00771                         ((IClassInterpret *)_SelfClass.get())->getBrancheCode().setTypeOfMethode(_ExpressionType);
00772                         if(!_ExpressionType->satisfied())
00773                         {
00774                                 _ConstraintType.push_back(_ExpressionType);
00775                                 _ExpressionType->incRef();
00776                         }
00777                         haveReturn = true;
00778                 }
00779                 else
00780                 {                        
00781                         if(a->getTypeOfClass() == IConstraint::operandListType)
00782                         {
00783                                 ((COperandListType *)a)->add(_ExpressionType);
00784                         }
00785                         else
00786                         {
00787                                 COperandListType *c = new COperandListType();
00788                                 ((IOpType *)a)->incRef();
00789                                 c->add((IOpType *)a);
00790                                 c->add(_ExpressionType);                                
00791                                 ((IClassInterpret *)_SelfClass.get())->getBrancheCode().setTypeOfMethode(c);
00792                                 if(!c->satisfied())
00793                                 {
00794                                         c->incRef();
00795                                         _ConstraintType.push_back(c);                                   
00796                                 }
00797                                 haveReturn = true;
00798                         }                       
00799                 }
00800                 _ExpressionType = NULL;
00801                 _IsVoid = false;
00802                 return true;
00803         }       
00804 
00805         void CCompilateur::callSend()
00806         {
00807 
00808                 //IConstraint *c = getMethodConstraint(CConstraintMethode(CConstraintMethode::normalCall, 0 , _LastbaseClass,_LastStringParam.back(),_Param.back(),0,0));
00809         }
00810 
00811         bool CCompilateur::checkReturnMethod()
00812         {
00813                 return true;
00814         }
00815 
00816         bool CCompilateur::callFunction()
00817         {
00818 #ifdef NL_DEBUG
00819         std::string mName;
00820         std::string pName;
00821         _LastStringParam.back()->getDebugString(mName);
00822         _Param.back()->getDebugString(pName);   
00823         //sint32 i = _TypeList.size();
00824 #endif
00825                 NLAIAGENT::CStringType *s = (NLAIAGENT::CStringType *)_LastStringParam.back()->get();
00826                 if(     !strcmp(s->getStr().getString(),_SEND_)/* && _Param.back()->size() == 1*/)
00827                 {                       
00828                         _LastBloc->addCode(new CMsgSetSender());
00829                         _LastBloc->addCode(new CNopOpCode());
00830                         //sendOp = _LastBloc->getBagOfCode();
00831                 }
00832         
00833                 _LastBloc->addCode(new CNopOpCode());
00834                 CBagOfCode *b = _LastBloc->getBagOfCode();
00835                 _LastbaseClass = NULL;
00836                 _LastPosHeap = 0;
00837                 _LastTypeCall = CConstraintMethode::normalCall;
00838 
00839                 IConstraint *c;         
00840                 _LastPosHeap = 0;
00841                 _LastTypeCall = (sint32)CConstraintMethode::normalCall;
00842                 if(_FromStack.back() && _TypeList.size()) 
00843                 {
00844                         _LastbaseClass = _TypeList.back();
00845                         _TypeList.pop_back();
00846                         _LastStringParam.back()->incRef();
00847                         _Param.back()->incRef();
00848                         _LastbaseClass->incRef();
00849                         _LastTypeCall = CConstraintMethode::stackCall;
00850                         c = getMethodConstraint(CConstraintMethode((CConstraintMethode::TCallTypeOpCode)_LastTypeCall,_LastPosHeap,_LastbaseClass,_LastStringParam.back(),_Param.back(),0,0));                  
00851                 }
00852                 else
00853                 {
00854                         NLAIAGENT::CStringType *name = (NLAIAGENT::CStringType *)(_LastStringParam.back()->getFront());
00855                         NLAIAGENT::IObjectIA *var = getVar(name->getStr().getString());
00856                         if(var)
00857                         {
00858                                 ((NLAIAGENT::IObjectIA *)_LastStringParam.back()->popFront())->release();
00859                                 _LastTypeCall = CConstraintMethode::heapCall;
00860                                 _LastPosHeap = ((CVarPStackParam *)var)->getIndex();
00861                                 _LastbaseClass = (IOpType *)((const CObjectUnknown *)((CVarPStackParam *)var)->getObject())->getBaseType();
00862                                 _LastStringParam.back()->incRef();
00863                                 _Param.back()->incRef();
00864                                 _LastbaseClass->incRef();
00865                                 c = getMethodConstraint(CConstraintMethode((CConstraintMethode::TCallTypeOpCode)_LastTypeCall,_LastPosHeap,_LastbaseClass,_LastStringParam.back(),_Param.back(),0,0));
00866                                 if(c == NULL) _LastbaseClass->incRef();
00867                         }
00868                         else
00869                         {
00870                                 _LastStringParam.back()->incRef();
00871                                 _Param.back()->incRef();                        
00872                                 _LastTypeCall = CConstraintMethode::normalCall;
00873                                 c = getMethodConstraint(CConstraintMethode((CConstraintMethode::TCallTypeOpCode)_LastTypeCall,_LastPosHeap,_LastbaseClass,_LastStringParam.back(),_Param.back(),0,0));
00874                         }
00875                 }               
00876                 if(c == NULL)
00877                 {
00878                         c = new CConstraintMethode((CConstraintMethode::TCallTypeOpCode)_LastTypeCall,_LastPosHeap,_LastbaseClass,_LastStringParam.back(),_Param.back(),yyLine,yyColone);
00879                         _LastStringParam.back()->incRef();
00880                         _Param.back()->incRef();
00881                         _MethodConstraint.push_back(c);
00882                 }
00883                 b->addConstraint(c);
00884                 /*if(sendOp != NULL)
00885                 {
00886                         NLAIAGENT::IBaseGroupType *nameRun = (NLAIAGENT::IBaseGroupType *)_LastStringParam.back()->clone();
00887                         ((NLAIAGENT::IObjectIA *)nameRun->pop())->release();                    
00888 
00889                         //_Param.back()->incRef();
00890                         CParam *paramRun = new CParam;                  
00891                         IOpType *p = (IOpType *)(*_Param.back())[1];
00892                         p->incRef();
00893                         paramRun->push(p);
00894 
00895                         const NLAIC::CIdentType *id = NULL;
00896                         if(_Param.back()->size() == 2)
00897                         {
00898                                 id = ((IOpType *)(*_Param.back())[0])->getConstraintTypeOf();
00899                                 if(id == NULL || !(((const NLAIC::CTypeOfObject &)*id) & NLAIC::CTypeOfObject::tPerformative) )
00900                                 {                                                               
00901                                                 yyerror("argument 1 of send is not an performatif");    
00902                                                 return false;
00903                                 }
00904                         }
00905                         else
00906                         {
00907                                 if(_Param.back()->size() == 3)
00908                                 {
00909                                         id = ((IOpType *)(*_Param.back())[1])->getConstraintTypeOf();
00910                                         if(id == NULL || !(((const NLAIC::CTypeOfObject &)*id) & NLAIC::CTypeOfObject::tPerformative) )
00911                                         {                                                               
00912                                                         yyerror("argument 2 of send is not an performatif");    
00913                                                         return false;
00914                                         }
00915                                 }
00916                         }
00917                         
00918                                                 
00919                         NLAIAGENT::IPerformative *perf = (NLAIAGENT::IPerformative *)id->getFactory()->getClass();
00920                         char runName[1024*4]; 
00921                         sprintf(runName,"%s%s",_RUN_,perf->getName());
00922                         nameRun->cpy(NLAIAGENT::CStringType ((NLAIAGENT::CStringVarName(runName))));
00923                         nameRun->incRef();                      
00924 
00925 #ifdef NL_DEBUG 
00926         nameRun->getDebugString(mName);
00927 #endif                  
00928 
00929                         int baseIsNew = false;
00930                         if(_LastbaseClass == NULL)
00931                         {
00932                                 _LastbaseClass = new COperandSimple(new NLAIC::CIdentType (_SelfClass.get()->getType()));
00933                                 baseIsNew = true;
00934                         }
00935                         _LastbaseClass->incRef();
00936                         paramRun->incRef();
00937                         c = getMethodConstraint(CConstraintFindRun((CConstraintMethode::TCallTypeOpCode)_LastTypeCall,_LastPosHeap,_LastbaseClass,nameRun,paramRun,0,0));
00938                         if(c == NULL)
00939                         {                                                               
00940                                 if(_LastbaseClass && !baseIsNew) _LastbaseClass->incRef();
00941                                 c = new CConstraintFindRun((CConstraintMethode::TCallTypeOpCode)_LastTypeCall,_LastPosHeap,_LastbaseClass,nameRun,paramRun,yyLine,yyColone);                            
00942                                 _MethodConstraint.push_back(c);
00943                         }
00944                         else
00945                         {
00946                                 nameRun->release();
00947                         }
00948                         sendOp->addConstraint(c);
00949                 }*/
00950                 return true;
00951         }
00952 
00953         CCodeBrancheRun *CCompilateur::getCode(bool isMain)
00954         {
00955                 if(_LastBloc != NULL && !_LastBloc->isCodeMonted())
00956                 {                                                                       
00957                         _VarState.popMark();
00958                         IOpCode *x;
00959                         if (_Debug)
00960                         {
00961                                 x = new CFreeAllocDebug();
00962                         }
00963                         else
00964                         {
00965                                 x = new CFreeAlloc();
00966                         }
00967                         _LastBloc->addCode(x);
00968                         x = new CHaltOpCode();                                                                  
00969                         _LastBloc->addCode(x);
00970 
00971                         CCodeBrancheRun* listCode;
00972                         if (_Debug)
00973                         {
00974                                 listCode = _LastBloc->getCodeDebug(_SourceFileName);
00975                         }
00976                         else
00977                         {
00978                                 listCode = _LastBloc->getCode();
00979                         }
00980 
00981                         if(listCode != NULL)
00982                         {
00983                                 if(!isMain) ((IClassInterpret *)_SelfClass.get())->getBrancheCode().setCode((IOpCode *)listCode);
00984                                 _Heap -= (sint32)_Heap;
00985                                 if(_Heap.restoreStackState()) _Heap.restoreStack();
00986                                 if(_Heap.restoreShiftState()) _Heap.restoreShift();
00987                                 _Heap -= (sint32)_Heap;                                                                 
00988                                 //listCode->release();
00989                         }                                                               
00990                         _DecalageHeap = 0;
00991                         CVarPStackParam::_Shift = 0;
00992                         
00993                         return listCode;
00994                         
00995                 }
00996                 return NULL;
00997         }
00998 
00999         void CCompilateur::initMessageManager()
01000         {
01001                 IClassInterpret *o = (IClassInterpret *)(CManagerClass::IdManagerClass.getFactory())->getClass();
01002                 o->incRef();
01003                 _SelfClass.push(o);             
01004                 initParam();
01005                 setNewLine();
01006                 /*if (_Debug)
01007                 {
01008                         _LastBloc->addCode(new CAddParamNameDebug(NLAIAGENT::CGroupType()));                    
01009                 }*/
01010         }
01011 
01012         bool CCompilateur::endMessageManager()
01013         {
01014                 _ResultCompile = getCode(true);
01015                 bool state = computContraint();
01016                 ((NLAIAGENT::IObjectIA *)_SelfClass.pop())->release();
01017                 return state;
01018         }
01019 
01020         bool CCompilateur::definClass(const char *className)
01021         {
01022                 try
01023                 {
01024                         NLAIC::CIdentType id(className);
01025                         return false;
01026                 }
01027                 catch(NLAIE::IException &)
01028                 {
01029                         ( (IClassInterpret *) _SelfClass.get() )->setClassName(NLAIAGENT::CStringVarName(className));
01030                         ( (IClassInterpret *) _SelfClass.get() )->buildVTable();
01031                         RegisterClass();
01032                         return true;
01033                 }
01034         }
01035 }