# 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  

codage.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 
00025 #include "nel/ai/script/compilateur.h"
00026 #include "nel/ai/script/constraint.h"
00027 #ifdef NL_DEBUG 
00028 #ifdef NL_OS_WINDOWS
00029 #include "windows.h"
00030 #endif
00031 #endif
00032 
00033 //#define PROFILE
00034 #ifdef PROFILE
00035 #include "nel/misc/time_nl.h"
00036 #endif
00037 
00038 namespace NLAISCRIPT
00039 {       
00040         NLAIC::IIO      *CCodeBrancheRun::InputOutput = NULL;   
00041 
00042         void CCodeBrancheRun::save(NLMISC::IStream &os)
00043         {
00044                 if(_TableCode != NULL)
00045                 {
00046                         sint32 count = _Count;
00047                         os.serial( count );
00048                         for(int i = 0 ;i < _Count; i ++)
00049                         {               
00050                                 std::string text;
00051                                 text = NLAIC::stringGetBuild("Opcode '%s' No: %d",(const char *)_TableCode[i]->getType(),i);
00052                                 count = i;
00053                                 std::string x( text );
00054                                 os.serial(x );
00055                                 os.serial( (NLAIC::CIdentType &)  _TableCode[i]->getType() );
00056                                 os.serial( *_TableCode[i] );
00057                         }
00058                 }
00059         }
00060 
00061         void CCodeBrancheRun::load(NLMISC::IStream &is)
00062         {
00063                 NLAIC::CIdentTypeAlloc id;              
00064                 del();
00065 
00066                 sint32 count;
00067                 is.serial( count );
00068                 _Count = count;
00069                 _TableCode = new IOpCode * [_Count];
00070                 for(int i = 0 ;i < _Count; i ++)
00071                 {
00072                         std::string s;
00073                         is.serial( s );
00074 #ifdef NL_DEBUG                 
00075                         const char *ss = s.data();
00076 #endif                  
00077                         is.serial( id );
00078                         _TableCode[i] = (IOpCode *)id.allocClass();
00079                         _TableCode[i]->load(is);                        
00080                 }
00081 
00082         }
00083 
00084         const NLAIC::IBasicType *CCodeBrancheRun::newInstance() const           
00085         {
00086                 NLAIC::IBasicType *x = new CCodeBrancheRun(1,CHaltOpCode());
00087                 return x;
00088         }
00089 
00090         void CCodeBrancheRun::getDebugString(std::string &t) const
00091         {
00092                 t += NLAIC::stringGetBuild("<CCodeBrancheRun _count = %d>", _Count);
00093         }
00094 
00095         const NLAIAGENT::IObjectIA::CProcessResult &CCodeBrancheRun::run()
00096         {
00097                 _RunState.ResultState = NLAIAGENT::processBuzzy;
00098                 _RunState.Result = NULL;
00099                 CStackPointer   stack,heap;
00100                 CCodeContext p(stack,heap,this,NULL,InputOutput);
00101                 
00102                 CVarPStack::_LocalTableRef = &heap[0];
00103                 stack --;
00104 
00105                 run(p);
00106 
00107                 stack -= (int)stack; 
00108                 heap -= (int)heap; 
00109                 return _RunState;
00110         }       
00111 
00112         const NLAIAGENT::IObjectIA::CProcessResult &CCodeBrancheRun::run(NLAIAGENT::IObjectIA &self)
00113         {
00114                 _RunState.ResultState = NLAIAGENT::processBuzzy;
00115                 _RunState.Result = NULL;
00116                 CStackPointer   stack,heap;
00117                 CCodeContext p(stack,heap,this,&self,InputOutput);      
00118                 CVarPStack::_LocalTableRef = &heap[0];
00119                 stack --;
00120 
00121                 run(p);
00122 
00123                 stack -= (int)stack; 
00124                 heap -= (int)heap; 
00125                 return _RunState;
00126         }
00127 
00128 
00129 
00130 #ifdef NL_DEBUG 
00131         bool NL_AI_DEBUG_SERVER = 0;
00132         std::string Marker;
00133         sint kMarker = 0;
00134 #endif
00135 
00136 
00137 
00138         const NLAIAGENT::IObjectIA::CProcessResult &CCodeBrancheRun::run(CCodeContext &p)
00139         {               
00140                 NLAIAGENT::TProcessStatement i = NLAIAGENT::processIdle;
00141 #ifdef NL_DEBUG
00142                 if(NL_AI_DEBUG_SERVER)
00143                 {
00144                         Marker += "\t";
00145                         kMarker ++;
00146 #ifdef NL_OS_WINDOWS
00147                         std::string chaine = std::string("\n\n") + Marker;
00148                         chaine += NLAIC::stringGetBuild("Begin with sp = %d \n\n", p.Stack.CIndexStackPointer::operator int());
00149                         OutputDebugString(chaine.c_str());
00150 #endif
00151                 }
00152 #endif
00153                 
00154                 while(i != NLAIAGENT::processEnd)
00155                 {       
00156                         i = runOpCode(p);
00157                 }
00158 
00159 
00160 #ifdef NL_DEBUG
00161                 if(NL_AI_DEBUG_SERVER)
00162                 {
00163                         std::string chaine = std::string("\n\n") + Marker;
00164 #ifdef NL_OS_WINDOWS                    
00165                         chaine += NLAIC::stringGetBuild("End with sp = %d \n\n", p.Stack.CIndexStackPointer::operator int());
00166                         OutputDebugString(chaine.c_str());
00167 #endif
00168                         kMarker --;                     
00169                         sint i;
00170                         chaine = "";
00171                         for(i = 0; i < (sint)kMarker; i ++)
00172                         {
00173                                 chaine += Marker [i];                           
00174                         }
00175                         Marker = chaine;
00176                 }
00177 #endif
00178                 _Ip = 0;
00179                 _RunState.ResultState = NLAIAGENT::processIdle;
00180                 return _RunState;
00181         }
00182         
00183         NLAIAGENT::TProcessStatement CCodeBrancheRun::runOpCode(CCodeContext &p)
00184         {               
00185                 IOpCode &op = nextCode();
00186 
00187 #ifdef NL_DEBUG
00188                 if(NL_AI_DEBUG_SERVER)
00189                 {
00190                         std::string chaine,chainedebug;
00191                         op.getDebugResult(chainedebug,p);
00192                         if(kMarker) chaine = Marker + chainedebug;
00193                         else  chaine = chainedebug;
00194                         chaine += "\n";
00195 
00196 #ifdef NL_OS_WINDOWS
00197                         OutputDebugString(chaine.c_str());
00198 #endif
00199                 }
00200 #endif
00201 
00202                 return op.runOpCode(p);
00203         }
00204 
00205         CCodeBrancheRun::CCodeBrancheRun(int N,const IOpCode &op):_TableCode(NULL),_Ip(0),_Count(N)
00206         {               
00207                 _TableCode = new IOpCode * [N];
00208                 for(int i = 0 ;i < _Count; i ++) 
00209                 {
00210                         _TableCode[i] = (IOpCode *)op.clone();                  
00211                 }
00212 
00213         }
00214 
00215         CCodeBrancheRun::CCodeBrancheRun(int N):_TableCode(NULL),_Ip(0),_Count(N)
00216         {               
00217                 _TableCode = new IOpCode * [N];
00218                 for(int i = 0 ;i < _Count; i ++) 
00219                 {
00220                         _TableCode[i] = NULL;                   
00221                 }
00222 
00223         }
00224 
00225         void CCodeBrancheRun::getDebugResult(std::string &str,CCodeContext &P) const
00226         {               
00227                 _TableCode[_Ip]->getDebugResult(str,P);
00228         }
00229         
00230         const NLAIC::CIdentType &CCodeBrancheRun::getType() const
00231         {               
00232                 return IdCodeBrancheRun;
00233         }
00234 
00235 
00236         void CBagOfCode::setConstraintIndex(int i,CCodeBrancheRun *c)
00237         {
00238                 std::list<IConstraint *>::iterator it = _Constraint.begin();
00239                 while(it != _Constraint.end())
00240                 {
00241                         (*it++)->addIndex(i,c);
00242                 }
00243         }
00244 }