# 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.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_COMPILATEUR_H
00027 #define NL_COMPILATEUR_H
00028 
00029 #include "nel/ai/agent/agentpack.h"
00030 #include "nel/ai/agent/agent_string.h"
00031 #include "nel/ai/agent/agent_script.h"
00032 
00033 #include "nel/ai/script/interpret_object.h"
00034 #include "nel/ai/script/interpret_methodes.h"
00035 #include "nel/ai/script/interpret_object_list.h"
00036 
00037 //#include "nel/ai/pyserver/pylib.h"
00038 #include "nel/ai/script/lexsupport.h"
00039 #include "nel/ai/script/varstack.h"
00040 #include "nel/ai/script/code_branche_run_debug.h"
00041 
00042 #include "nel/ai/script/codage.h"
00043 #include "nel/ai/script/constraint_debug.h"
00044 #include "nel/ai/script/module.h"
00045 #include "nel/ai/script/lex.h"
00046 #include "nel/ai/logic/logic.h"
00047 #include "nel/ai/fuzzy/fuzzy.h"
00048 #include <fstream.h>
00049 
00050 
00051 
00052 namespace NLAISCRIPT
00053 {
00054         class IConstraint;
00055 
00059         enum TVarOfType {
00061                 varTypeImediate,
00063                 varTypeMember,
00065                 varTypeStackMember,
00067                 varTypeHeapMember,
00069                 varTypeLocal,
00071                 varForFunc,
00072                 varUnknown,
00073                 varTypeUndef
00074         };
00075 
00082         struct CFactorType
00083         {
00085                 NLAIAGENT::IObjetOp     *Value;
00087                 const NLAIAGENT::IObjectIA      *ValueVar;
00088                 TVarOfType              VarType;
00090                 bool IsUsed;
00092                 std::list<sint32>       Member;
00094                 std::list<NLAISCRIPT::CStringType > MemberName;
00096                 IOpType                 *TypeStack;             
00097                 CFactorType();          
00098 
00099                 CFactorType(const CFactorType &v);
00100 
00101                 virtual ~CFactorType()
00102                 {
00103                         del();
00104                 }
00105 
00106                 void del();             
00107 
00108                 void set(const CFactorType &v);         
00109 
00110                 const CFactorType *clone() const 
00111                 {
00112                         return new CFactorType(*this);          
00113                 }
00114         };
00115         
00116         class IOpType;
00117 
00125         class CCompilateur: public yyFlexLexer
00126         {       
00127         private:
00129                 typedef std::pair<NLAIAGENT::IVarName *,NLAIAGENT::IObjectIA *> pairType;
00130 
00131         private:
00133                 std::istrstream*                                _StreamBuffer;
00135                 NLAIC::IIO&                                             _Iterface;
00137                 CFactorType                                             _LastFact;
00139                 IBlock*                                                 _LastBloc;
00141                 NLAIAGENT::CStringVarName               _LastString,_LastBaseObjectDef;
00142                 bool                                                    _BaseObjectDef;
00144                 CListClass                                              _SelfClass;
00146                 std::list<IBlock*>                              _LastSaveBloc,_LastBloc1,_LastBloc2,_LastBloc3;
00148                 NLAIAGENT::IObjectIA*                   _LastAffectation;
00150                 CIndexStackPointer                              _VarState;                      
00151                 CStackPointer                                   _Stack,_Heap;
00152                 std::list<NLAISCRIPT::CStringType>      _LasVarStr,_LasAffectationVarStr;
00153                 std::list<IOpType *>                    _TypeList;
00154                 bool                                                    _Error;
00155                 sint32                                                  _LastRegistered;                
00156                 std::list<pairType>                             _Attrib;
00157                 sint32                                                  _DecalageHeap;
00158                 std::list<IConstraint *>                _MethodConstraint;
00159                 std::list<IConstraint *>                _ConstraintType;                
00160                 std::list<NLAIAGENT::IBaseGroupType *>  _LastStringParam;
00161                 std::list<CParam *>                             _Param;
00162                 std::list<NLAIAGENT::CStringVarName >   _MethodName;
00163                 IOpType                                                 *_ExpressionType;               
00164                 IOpType                                                 *_FlotingExpressionType;        
00165                 std::list<IOpType *>                    _ExpressionTypeTmp;
00166                 std::list<IOpType *>                    _ExpressionOp;
00167                 bool                                                    _IsFacteurIsExpression;
00168                 bool                                                    _FacteurEval;
00169                 bool                                                    _IsVoid;
00170                 sint32                                                  _LastPosHeap;
00171                 sint32                                                  _LastTypeCall;
00172                 IConstraint                                             *_LastbaseClass;
00173                 bool                                                    _Debug;
00174                 IScriptDebugSource                              *_SourceFileName;
00175                 NLAIAGENT::IObjectIA                    *_ResultCompile;
00176 
00177                 // Logique
00178                 std::list< std::list<const NLAIAGENT::IVarName *> > _LastLogicParams;
00179                 std::list<const NLAIAGENT::CStringVarName *> _LastBooleanConds;
00180                 std::list<NLAIAGENT::CStringVarName *> _LastFuzzyConds;
00181                 std::list<const NLAIAGENT::CStringVarName *> _LastAsserts;
00182                 std::list<IOpCode *>    _LastCodeBranche;
00183                 bool is_cond;
00184 
00185                 NLAILOGIC::CFactBase *_FactBase;
00186                 NLAILOGIC::CFactPattern *_LastFactPattern;
00187                 NLAIAGENT::IVarName *_Goal;
00188                 
00189                 std::list<NLAIFUZZY::CFuzzyVar *> _FuzzVars;
00190                 sint32 _LastFVarIndex;
00191                 sint32 _NbLogicParams;
00192                 bool _InCond;
00193                 NLAIAGENT::IMessageBase::TPerformatif _LastPerf;
00195                 
00196                 bool   _InLineParse;
00197                 std::list<bool>   _FromStack;
00198                 tDicoStr *_Dictionary;
00199                 bool isRunMsg;
00200                 bool haveReturn;
00201 
00202         public: 
00203 
00208                 CCompilateur(NLAIC::IIO &Iterface,const char *str, sint32 size, IScriptDebugSource* fileName):
00209                         _StreamBuffer(NULL),
00210                         _Iterface(Iterface),                                            
00211                         _LastBloc((IBlock*)NULL),                       
00212                         _LastString("_"),
00213                         _LastBaseObjectDef("_"),
00214                         _Debug(false),
00215                         _Goal(NULL),
00216                         _InCond(false)
00217                 {
00218                         _SourceFileName = fileName;
00219                         _SourceFileName->incRef();
00220                         LastyyText[0][0] = 0;
00221                         LastyyText[1][0] = 0;
00222                         //_VarState = NULL;
00223                         InitStream((char *)str,size);
00224                         yyLine = yyColone = 1;
00225                         _Error = false;
00226                         CCodeBrancheRun::InputOutput = &Iterface;
00227                         _DecalageHeap = 0;
00228                         _ExpressionType = NULL;
00229                         _FlotingExpressionType = NULL;
00230                         _IsFacteurIsExpression = false;
00231                         _FacteurEval = false;
00232                         _IsVoid = true;
00233                         _ResultCompile = NULL;
00234                         _InLineParse = false;
00235                         isRunMsg = false;
00236                         haveReturn = false;
00237                         _LastFactPattern = NULL;
00238                 }
00239 
00244                 CCompilateur(NLAIC::IIO &Iterface,const char *str, IScriptDebugSource* fileName):
00245                         _StreamBuffer(NULL),
00246                         _Iterface(Iterface),                    
00247                         _LastBloc((IBlock*)NULL),                       
00248                         _LastString("_"),
00249                         _LastBaseObjectDef("_"),
00250                         _Debug(false),
00251                         _Goal(NULL),
00252                         _InCond(false)
00253                 {                                       
00254                         _SourceFileName = fileName;
00255                         _SourceFileName->incRef();
00256                         //_VarState = NULL;
00257                         InitFromFile(str);
00258                         yyLine = yyColone = 1;
00259                         _Error = false;
00260                         _DecalageHeap = 0;
00261                         _ExpressionType = NULL;
00262                         _FlotingExpressionType = NULL;
00263                         _IsFacteurIsExpression = false;
00264                         _FacteurEval = false;
00265                         _IsVoid = true; 
00266                         _ResultCompile = NULL;
00267                         _InLineParse = false;
00268                         isRunMsg = false;
00269                         haveReturn = false;
00270                         _LastFactPattern = NULL;
00271                 }
00272 
00274                 void InitFromFile(const char *FileName)
00275                 {
00276 
00277                         ifstream file;
00278                         file.open(FileName,ios::in);
00279                         sint32 Size = file.gcount();
00280                         char *Chaine = NULL;
00281                         if(Size)
00282                         {
00283                                 Chaine = new char [Size];
00284                                 file.read(Chaine,Size);
00285                                 file.close();
00286                                 if(_StreamBuffer) delete _StreamBuffer;
00287                                 _StreamBuffer = new std::istrstream(Chaine,Size);                       
00288                         }               
00289                 }
00290 
00292                 NLAIAGENT::IObjectIA *getVar(const char *name)
00293                 {
00294                         NLAIAGENT::IObjectIA *var = _LastBloc->getVar(name);
00295                         if(var == NULL)
00296                         {
00297                                 std::list<IBlock *>::reverse_iterator  i = _LastSaveBloc.rbegin();
00298                                 while(i != _LastSaveBloc.rend())
00299                                 {
00300                                         IBlock *k = *(i);
00301                                         var = k->getVar(name);
00302                                         k = *(i);
00303                                         if(var != NULL) break;
00304                                         i ++;
00305                                 }
00306                         }
00307                         return var;
00308                 }
00309 
00311                 void InitStream(char *str, sint32 size)
00312                 {
00313                         if(_StreamBuffer) delete _StreamBuffer;
00314                         _StreamBuffer = new std::istrstream(str,size);
00315                 }               
00316 
00317                 virtual ~CCompilateur()
00318                 {                       
00319 
00320                         clean();
00321                         if(_StreamBuffer) delete _StreamBuffer;
00322                         cleanMethodConstraint();
00323                         cleanTypeConstraint();                  
00324                         //_Heap -= (sint32)_Heap;
00325                         _SourceFileName->release();
00326                 }
00327                 
00329                 virtual int     yywrap()
00330                 {
00331                         if(_StreamBuffer != NULL && yyin == NULL)
00332                         {                       
00333                                 yyin = _StreamBuffer;                   
00334                                 return 0;
00335                         }               
00336                         return 1;
00337                 }
00338                 
00340                 void cleanTypeList();
00342                 void clean();
00344                 void cleanMethodConstraint();
00345                 void cleanTypeConstraint();             
00346 
00354                 virtual NLAIAGENT::IObjectIA::CProcessResult Compile();
00355 
00357                 int     yyparse();
00358 
00360                 void Echo(char *Er,...);                
00361 
00363                 void yyerror(char *Er)
00364                 {
00365 
00366                         _Iterface.Echo("%s at line %d et colone %d\n",Er,yyLine,yyColone);
00367                         
00368                         //_StreamBuffer->close();
00369                          _Error = true;                  
00370                         //yyclearin;
00371                 }
00372 
00374                 bool parseError()
00375                 {
00376                         return _Error == true;
00377                 }
00378 
00379 
00385                 CFunctionTag findMethode(const NLAIAGENT::IObjectIA *baseClass,NLAIAGENT::IBaseGroupType &listName,const CParam &param);
00391                 CFunctionTag findMethode(NLAIAGENT::IBaseGroupType &listName,const CParam &param);
00392 
00398                 sint32 isValidateVarName(const NLAIAGENT::IObjectIA *baseClass,std::list<sint32> &ref,std::list<NLAISCRIPT::CStringType> &nameHierarchy,IOpType *&type);
00404                 sint32 isValidateVarName(std::list<sint32> &ref,std::list<NLAISCRIPT::CStringType> &nameHierarchy,IOpType *&type);              
00405 
00407                 void setDebugMode(bool bmode){ _Debug = bmode;}
00409                 bool getDebugMode() const {return _Debug;}
00410 
00411 
00412                 const NLAIAGENT::IObjectIA *validateHierarchyMethode(std::list<sint32> &,sint32 &,const NLAIAGENT::IObjectIA *,NLAIAGENT::IBaseGroupType &listName);
00413 
00414         protected:
00415                 NLAIC::CIdentType getTypeOfClass(const NLAIAGENT::IVarName &className);
00416 
00417                 CCodeBrancheRun *getCode(bool isMain = false);
00418                 sint32 runTypeConstraint();
00419                 sint32 runMethodConstraint();
00420                 void errorMethodConstraint();
00421                 void errorTypeConstraint();
00422                 
00423                 sint32 getNMethodConstraint()   
00424                 {
00425                         return  _MethodConstraint.size();
00426                 }
00427 
00428                 NLAIAGENT::CIdMethod findMethode(sint32 inheritance,NLAIAGENT::CStringType *baseName,const NLAIAGENT::IObjectIA *className,const NLAIAGENT::IVarName &methodeName,const CParam &param);         
00429                 //sint32 getInheritanceRange(const IClassInterpret *,NLAIAGENT::IBaseGroupType &) const;
00430                 const IClassInterpret *getInheritanceRange(sint32 &,const IClassInterpret *,NLAIAGENT::IBaseGroupType &) const;
00431                 const IClassInterpret *getInheritanceRange(sint32 &,const IClassInterpret *,const NLAIAGENT::IVarName &) const;
00432                 sint32 findMethodei(NLAIAGENT::IBaseGroupType &listName,const CParam &param);           
00433 
00435                 virtual void setNewLine();
00436                 
00437                 
00438                 //sint32 getCompementShift(IClassInterpret *classType);
00439 
00440                 //Function for the yacc interpreter.            
00441                 sint32 castVariable(const NLAIAGENT::CStringVarName&,NLAIAGENT::CStringVarName&);
00442                 void ifInterrogation();
00443                 void ifInterrogationPoint();
00444                 void ifInterrogationEnd();                              
00445                 void interrogationEnd();
00446                 sint32 affectation();
00447                 sint32 processingVar();
00448                 void allocExpression(IOpCode *op,bool gd = false);
00449                 bool computContraint();
00450                 void RegisterClass();
00451                 void onEndClass();
00452                 sint32 affectationMember(sint32 member);
00453                 sint32 affectationMember(IClassInterpret *,sint32 member);
00454                 sint32 affectationiMember(const IClassInterpret *baseClass,std::list<NLAISCRIPT::CStringType> &varHName);
00455                 sint32 PrivateError();
00456                 const NLAIAGENT::IObjectIA *getValidateHierarchyBase(std::list<sint32> &ref,sint32 &h,std::list<NLAISCRIPT::CStringType> &listName);
00457                 const NLAIAGENT::IObjectIA *getValidateHierarchyBase(const NLAIAGENT::IObjectIA *base,std::list<sint32> &ref,sint32 &h,std::list<NLAISCRIPT::CStringType> &listName);           
00458                 void nameMethodeProcessing();           
00459                 void initParam();
00460                 bool registerMethod();
00461 
00462                 IConstraint *getMethodConstraint(const NLAIAGENT::IBaseGroupType &g,const CParam &p) const;
00463                 IConstraint *getMethodConstraint(const IConstraint &c) const;
00464                 sint32 typeOfMethod();
00465                 bool setParamVarName();
00466                 //void setParamVarName(NLAIC::CIdentType &id);
00467                 void pushParamExpression();
00468                 void addParamCont();
00469                 void addParamRuleCont();
00470                 void setImediateVar();
00471                 void setImediateVarNill();
00472                 void setPerformative(NLAIAGENT::IMessageBase::TPerformatif p);
00473                 void setListVar();
00474                 void setChaineVar();
00475                 void setStackVar(const NLAIC::CIdentType &);
00476                 void setMethodVar();
00477                 bool buildObject();             
00478                 void setTypeExpression();
00479                 void setTypeExpression(NLAIC::CTypeOfOperator::TTypeOp op,const char *txtOp);
00480                 void setTypeExpressionG();
00481                 void setTypeExpressionD(NLAIC::CTypeOfOperator::TTypeOp op,const char *txtOp);
00482                 bool callFunction();
00483                 bool checkReturnMethod();
00484                 void callSend();
00485                 void initMessageManager();
00486                 bool endMessageManager();
00487                 bool caseRunMsg();
00488                 bool definClass(const char *className);
00489 
00490 
00492                 bool classIsAnOperator();
00493                 void CompileFactPattern();
00494                 void CompileOperator();
00495 
00496                 void addOpCode(IOpCode *x);
00497         };
00498 }
00499 #endif