00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00029
00030 namespace NLAISCRIPT
00031 {
00032 void IConstraint::popConstraint(IConstraint *c)
00033 {
00034 std::list<IConstraint *>::iterator it = _ConstraintList.begin();
00035 while(it != _ConstraintList.end())
00036 {
00037 if(*it == c)
00038 {
00039 _ConstraintList.erase(it);
00040 break;
00041 }
00042 it ++ ;
00043 }
00044 }
00045 }
00046
00047
00048 namespace NLAISCRIPT
00049 {
00050 void CConstraintMethode::run(CCompilateur &comp)
00051 {
00052 if(_Satisfied) return;
00053 #ifdef NL_DEBUG
00054 std::string mtxt;
00055 std::string ptxt;
00056 std::string txt;
00057
00058 _MethodName->getDebugString(mtxt);
00059
00060
00061
00062
00063
00064 _Param->getDebugString(ptxt);
00065 txt = mtxt + ptxt;
00066 #endif
00067 const NLAIAGENT::IObjectIA *cl = NULL;
00068 bool isMember = true;
00069
00070 if(_BaseClass)
00071 {
00072 if(!_BaseClass->satisfied()) _BaseClass->run(comp);
00073 if(_BaseClass->satisfied())
00074 {
00075 cl = (const NLAIAGENT::IObjectIA *)(_BaseClass->getConstraintTypeOf()->getFactory())->getClass();
00076 _M = comp.findMethode(cl,*_MethodName,*_Param);
00077 }
00078 else
00079 {
00080 std::string txt;
00081 std::string param;
00082 std::string Method;
00083
00084 _Param->getDebugString(param);
00085 _MethodName->getDebugString(Method);
00086 txt = NLAIC::stringGetBuild("can't find the method '%s%s'",Method.c_str(),param.c_str());
00087 _Txt = new char [strlen(txt.c_str()) + 1];
00088 strcpy(_Txt,txt.c_str());
00089 return;
00090 }
00091 }
00092 else
00093 {
00094 try
00095 {
00096 _M = comp.findMethode(*_MethodName,*_Param);
00097 }
00098 catch(NLAIE::IException &e)
00099 {
00100 std::string txt;
00101 std::string param;
00102 std::string Method;
00103
00104 _Param->getDebugString(param);
00105 _MethodName->getDebugString(Method);
00106 txt += NLAIC::stringGetBuild("error '%s' when serch '%s%s'",(char *) e.what(),Method.c_str(),param.c_str());
00107 _Txt = new char [strlen(txt.c_str()) + 1];
00108 strcpy(_Txt,txt.c_str());
00109 return;
00110
00111 }
00112 if(_M.MethodName == NULL)
00113 {
00114 isMember = false;
00115 NLAIAGENT::CStringType *lib = (NLAIAGENT::CStringType *)_MethodName->getFront();
00116 try
00117 {
00118 NLAIC::CIdentType id(lib->getStr().getString());
00119 _M = comp.findMethode((NLAIAGENT::IObjectIA *)id.getFactory()->getClass() ,*_MethodName,*_Param);
00120
00121 }
00122 catch(NLAIE::IException &)
00123 {
00124 std::string txt;
00125 std::string param;
00126 std::string Method;
00127
00128 _Param->getDebugString(param);
00129 _MethodName->getDebugString(Method);
00130 txt += NLAIC::stringGetBuild("can't find the method '%s%s'",Method.c_str(),param.c_str());
00131 _Txt = new char [strlen(txt.c_str()) + 1];
00132 strcpy(_Txt,txt.c_str());
00133
00134 }
00135 }
00136 }
00137 if(_M.MethodName == NULL)
00138 {
00139
00140 std::string txt;
00141 std::string param;
00142 std::string Method;
00143
00144 _Param->getDebugString(param);
00145 _MethodName->getDebugString(Method);
00146 txt += NLAIC::stringGetBuild("can't find the method '%s%s'",Method.c_str(),param.c_str());
00147 _Txt = new char [strlen(txt.c_str()) + 1];
00148 strcpy(_Txt,txt.c_str());
00149 }
00150 else
00151 {
00152 _Satisfied = true;
00153 IOpCode *x=0;
00154
00155
00156 if(((const NLAIC::CTypeOfObject &)_M.Object->getType()) & NLAIC::CTypeOfObject::tInterpret)
00157 {
00158 IMethodContext *methodContex;
00159 if (comp.getDebugMode())
00160 {
00161 methodContex = new CMethodContextDebug();
00162 }
00163 else
00164 {
00165 methodContex = new CMethodContext();
00166 }
00167
00168 switch(_CallType)
00169 {
00170 case normalCall:
00171 if(!_M.Member.size())
00172 {
00173 x = new CCallMethod(methodContex,_M.Inheritance,_M.MethodNum);
00174 }
00175 else
00176 {
00177 x = new CCallMethodi(methodContex,_M.Inheritance,_M.MethodNum,_M.Member);
00178 }
00179 break;
00180 case stackCall:
00181 x = new CCallStackMethodi(methodContex,_M.Inheritance,_M.MethodNum,_M.Member);
00182 break;
00183 case heapCall:
00184 x = new CCallHeapMethodi(methodContex,_M.Inheritance,_M.MethodNum,_PosHeap,_M.Member);
00185 break;
00186 case newCall:
00187 x = new CCallStackNewMethodi(methodContex,_M.Inheritance,_M.MethodNum,_M.Member);
00188 break;
00189 case searchCall:
00190 x = NULL;
00191 delete methodContex;
00192 break;
00193 }
00194 }
00195 else
00196 {
00197 _Satisfied = true;
00198 switch(_CallType)
00199 {
00200 case normalCall:
00201 if(_M.Member.size())
00202 {
00203 if(!isMember) x = new CLibCallMethodi(_M.Inheritance,_M.MethodNum ,_M.Member , *_M.Object);
00204 else
00205 {
00206 x = new CLibMemberMethodi(_M.Inheritance,_M.MethodNum ,_M.Member );
00207 }
00208 }
00209 else
00210 {
00211 if(_M.Inheritance)
00212 {
00213 if(!isMember) x = new CLibCallInheritedMethod(_M.Inheritance,_M.MethodNum , *_M.Object);
00214 else
00215 {
00216 x = new CLibMemberInheritedMethod(_M.Inheritance,_M.MethodNum);
00217 }
00218 }
00219 else
00220 {
00221 if(!isMember) x = new CLibCallMethod(_M.MethodNum ,*_M.Object);
00222 else
00223 {
00224 x = new CLibMemberMethod(_M.MethodNum);
00225 }
00226 }
00227 }
00228 break;
00229 case stackCall:
00230 x = new CLibStackMemberMethod(_M.Inheritance,_M.MethodNum ,_M.Member);
00231 break;
00232 case heapCall:
00233 x = new CLibHeapMemberMethod(_M.Inheritance,_M.MethodNum ,_M.Member,_PosHeap);
00234 break;
00235 case newCall:
00236 x = new CLibStackNewMemberMethod(_M.Inheritance,_M.MethodNum ,_M.Member);
00237 break;
00238
00239 case searchCall:
00240 x = NULL;
00241 break;
00242 }
00243 }
00244 IConstraint *c;
00245
00246 if(_M.Method != NULL)
00247 {
00248 c = (IConstraint *)_M.Method->getTypeOfMethode();
00249 setOpCode(comp,x,c,false);
00250 if(_M.ReturnType != NULL)
00251 {
00252 _M.ReturnType->release();
00253 _M.ReturnType = NULL;
00254 }
00255 }
00256 else
00257 {
00258 if(_M.ReturnType != NULL)
00259 {
00260 try
00261 {
00262 NLAIC::CIdentType type = _M.ReturnType->getType();
00263 NLAIC::CIdentType *tmp = new NLAIC::CIdentType (type);
00264 c = new COperandSimple ( tmp );
00265 setOpCode(comp,x,c,true);
00266 }
00267 catch(NLAIE::IException &)
00268 {
00269 c = new COperandSimple (new NLAIC::CIdentType (_M.Object->getType()));
00270 setOpCode(comp,x,c,true);
00271 }
00272 _M.ReturnType->release();
00273 _M.ReturnType = NULL;
00274 }
00275 else
00276 {
00277 c = new COperandSimple (new NLAIC::CIdentType (_M.Object->getType()));
00278 setOpCode(comp,x,c,true);
00279 }
00280 }
00281
00282 }
00283 }
00284
00285 void CConstraintMethode::setOpCode(CCompilateur &comp,IOpCode *x,IConstraint *type,bool f)
00286 {
00287 if(x != NULL)
00288 {
00289 if(_Code.size())
00290 {
00291 while(_Code.size())
00292 {
00293 std::pair<int, CCodeBrancheRun *> &p = _Code.back();
00294 IOpCode *op = (*p.second)[p.first];
00295
00296
00297
00298
00299
00300 op->release();
00301 (*p.second)[p.first] = x;
00302 _Code.pop_back();
00303 if(_Code.size()) x->incRef();
00304 }
00305 }
00306 else
00307 {
00308 x->release();
00309 }
00310 }
00311 _Type = type;
00312 _Type->run(comp);
00313 _DelCType = f;
00314 runConnexcion();
00315 }
00316
00317 void CConstraintMethode::getError(char *txt) const
00318 {
00319 strcpy(txt,_Txt);
00320 }
00321
00322 bool CConstraintMethode::isEqual (const NLAIAGENT::IBaseGroupType &g,const CParam &p) const
00323 {
00324 if(((const NLAIAGENT::IBasicObjectIA &)*_MethodName) == ((const NLAIAGENT::IBasicObjectIA &)g) && *_Param == p) return true;
00325 return false;
00326 }
00327
00328 void CConstraintMethode::run(IConstraint *)
00329 {
00330 }
00331
00332 const IConstraint *CConstraintMethode::clone() const
00333 {
00334 IConstraint *x = new CConstraintMethode(_CallType,_PosHeap,_BaseClass,_MethodName,_Param,_Lin,_Col);
00335 _BaseClass->incRef();
00336 _MethodName->incRef();
00337 _Param->incRef();
00338 return x;
00339 }
00340 }