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/constraint_stack_component.h"
00028
00029 namespace NLAISCRIPT
00030 {
00031
00032 CConstraintStackComp::CConstraintStackComp(OpCodeType opCodeType,sint32 stackPos,const std::list<NLAISCRIPT::CStringType > &memberName,IOpType *typeStack,sint32 lign,sint32 col):
00033 _TypeStack(typeStack),_MemberName(memberName),_Lin(lign),_Col(col)
00034 {
00035 char m[1028*8];
00036 std::list<NLAISCRIPT::CStringType >::const_iterator i = _MemberName.begin();
00037 m[0] = 0;
00038 while(i != _MemberName.end())
00039 {
00040 const NLAISCRIPT::CStringType &s = *i;
00041 strcat(m,s.data());
00042 i ++;
00043 if(i != _MemberName.end()) strcat(m,".");
00044 }
00045 std::string text;
00046 text = NLAIC::stringGetBuild("resolve constraint for the component '%s' at line %d",m,_Lin);
00047 _TxtInfo = new char [strlen(text.c_str()) + 1];
00048 strcpy(_TxtInfo,text.c_str());
00049 _Satisfied = false;
00050 _Id = NULL;
00051 _OpCodeType = opCodeType;
00052 _StackPos = stackPos;
00053
00054 #ifdef NL_DEBUG
00055 static int kkkkk = 0;
00056 nlinfo("meme %4x, %d",this, kkkkk ++);
00057 #endif
00058 }
00059
00060 CConstraintStackComp::~CConstraintStackComp()
00061 {
00062 _TypeStack->release();
00063 if(_Id) delete _Id;
00064 delete _TxtInfo;
00065 }
00066
00067 void CConstraintStackComp::run(CCompilateur &comp)
00068 {
00069 if(_Satisfied) return;
00070 _TypeStack->run(comp);
00071 if(_TypeStack->satisfied())
00072 {
00073 if(_MemberName.size() == 1)
00074 {
00075 const NLAIAGENT::IObjectIA *c = (NLAIAGENT::IObjectIA *)((NLAIC::IClassFactory *)_TypeStack->getConstraintTypeOf()->getFactory())->getClass();
00076 sint32 i = c->getStaticMemberIndex(NLAIAGENT::CStringVarName(_MemberName.front().data()));
00077 if(i >= 0)
00078 {
00079
00080
00081
00082
00083
00084 std::list<sint32> b;
00085 b.push_back(i);
00086 IOpCode *x=0;
00087 switch(_OpCodeType)
00088 {
00089 case stackCall:
00090 x = new CLdbStackMemberiOpCode(b);
00091 break;
00092 case heapCall:
00093 x = new CLdbHeapMemberiOpCode(b,_StackPos);
00094 break;
00095
00096 case heapAffectation:
00097 x = new CAffHeapMemberiOpCode(b,_StackPos);
00098 break;
00099
00100 }
00101
00102 std::pair<sint32, CCodeBrancheRun *> &p = _Code.back();
00103 IOpCode *op = (*p.second)[p.first];
00104 op->release();
00105 (*p.second)[p.first] = x;
00106 _Code.pop_back();
00107 _Satisfied = true;
00108 }
00109 else
00110 {
00111 return;
00112 }
00113 }
00114 else
00115 {
00116 std::list<sint32> b;
00117 IOpType *c;
00118 NLAIAGENT::IObjectIA *cl = (NLAIAGENT::IObjectIA *)((NLAIC::IClassFactory *)_TypeStack->getConstraintTypeOf()->getFactory())->getClass();
00119 if(!comp.isValidateVarName(cl,b,_MemberName,c) && c->getConstraintTypeOf() != NULL)
00120 {
00121 c->release();
00122 return;
00123 }
00124 _Id = new NLAIC::CIdentType (*c->getConstraintTypeOf());
00125 c->release();
00126 IOpCode *x=0;
00127
00128 switch(_OpCodeType)
00129 {
00130 case stackCall:
00131 x = new CLdbStackMemberiOpCode(b);
00132 break;
00133 case heapCall:
00134 x = new CLdbHeapMemberiOpCode(b,_StackPos);
00135 break;
00136
00137 case heapAffectation:
00138 x = new CAffHeapMemberiOpCode(b,_StackPos);
00139 break;
00140 }
00141
00142 std::pair<sint32, CCodeBrancheRun *> &p = _Code.back();
00143 IOpCode *op = (*p.second)[p.first];
00144 op->release();
00145 (*p.second)[p.first] = x;
00146 _Code.pop_back();
00147 _Satisfied = true;
00148
00149 }
00150 }
00151 }
00152
00153 void CConstraintStackComp::run(IConstraint *)
00154 {
00155 }
00156
00157 bool CConstraintStackComp::dependOn(const IConstraint *c) const
00158 {
00159 return _TypeStack == c;
00160 }
00161
00162 bool CConstraintStackComp::operator == (const IConstraint &c) const
00163 {
00164 if((getTypeOfClass() == c.getTypeOfClass()) && *((const CConstraintStackComp &)c)._TypeStack == *_TypeStack)
00165 {
00166 return ((const CConstraintStackComp &)c)._MemberName == _MemberName &&
00167 ((const CConstraintStackComp &)c)._OpCodeType == _OpCodeType &&
00168 ((const CConstraintStackComp &)c)._StackPos == _StackPos;
00169 }
00170 return false;
00171 }
00172
00173 void CConstraintStackComp::getError(char *txt) const
00174 {
00175 std::string text;
00176 text = NLAIC::stringGetBuild("can't %s",_TxtInfo);
00177 strcpy(txt, (char *)text.c_str());
00178 }
00179
00180 const IConstraint *CConstraintStackComp::clone() const
00181 {
00182 _TypeStack->incRef();
00183 IConstraint *x = new CConstraintStackComp(_OpCodeType,0,_MemberName,_TypeStack,_Lin,_Col);
00184 return x;
00185 }
00186
00187 const NLAIC::CIdentType *CConstraintStackComp::getConstraintTypeOf()
00188 {
00189 return _Id;
00190 }
00191 }