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 #include "nel/ai/script/object_unknown.h"
00029 #include "nel/ai/agent/performative.h"
00030 #include "nel/ai/script/param_container.h"
00031
00032 namespace NLAISCRIPT
00033 {
00034 bool CCompilateur::caseRunMsg()
00035 {
00036 if(isRunMsg && !haveReturn)
00037 {
00038 isRunMsg = false;
00039 char text[4096];
00040 strcpy(text,"run message methode must return an message value");
00041 yyerror(text);
00042 return false;
00043 }
00044 isRunMsg = false;
00045 haveReturn = false;
00046 return true;
00047 }
00048
00049 bool CCompilateur::classIsAnOperator()
00050 {
00051 return true;
00052 }
00053
00054 sint32 CCompilateur::castVariable(const NLAIAGENT::CStringVarName &v,NLAIAGENT::CStringVarName &t)
00055 {
00056 NLAIAGENT::IObjectIA *var = getVar(v.getString());
00057 if(var != NULL)
00058 {
00059 try
00060 {
00061 NLAIC::CIdentType id(t.getString());
00062 IOpType * c= new COperandSimple(new NLAIC::CIdentType(id));
00063 CObjectUnknown *o = (CObjectUnknown *)((CVarPStackParam *)var)->getObject();
00064 o->setBaseType(c);
00065 return true;
00066 }
00067 catch(NLAIE::IException &)
00068 {
00069 std::string text;
00070 text = NLAIC::stringGetBuild("can't find %s object in the class factory",t.getString());
00071 yyerror((char *)text.c_str());
00072 return false;
00073 }
00074
00075 }
00076 else
00077 {
00078 yyerror("cast service is reseved to the local variable");
00079 return false;
00080 }
00081 }
00082 sint32 CCompilateur::processingVar()
00083 {
00084 if(_LasVarStr.size() >= 1)
00085 {
00086 NLAIAGENT::IObjectIA *var;
00087 var = getVar(_LasVarStr.front().data());
00088 if(_LasVarStr.size() == 1)
00089 {
00090 if(var == NULL)
00091 {
00092 sint32 i = ((IClassInterpret *)_SelfClass.get())->getStaticMemberIndex(NLAIAGENT::CStringVarName(_LasVarStr.front().data()));
00093 if(i >= 0)
00094 {
00095 _LastFact.Member.clear();
00096 _LastFact.Member.push_back(i );
00097 _LastFact.ValueVar = NULL;
00098 _LastFact.VarType = varTypeMember;
00099 _LastFact.IsUsed = false;
00100 try
00101 {
00102 if(_FlotingExpressionType != NULL) delete _FlotingExpressionType;
00103 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType ((_SelfClass.get())->getStaticMember(i)->getType()));
00104 }
00105 catch(NLAIE::IException &a)
00106 {
00107 yyerror((char *)a.what());
00108 return 0;
00109 }
00110 }
00111 else
00112 {
00113 if(_TypeList.size())
00114 {
00115 IOpType *type = _TypeList.back();
00116 _TypeList.pop_back();
00117 _LastFact.Member.clear();
00118 _LastFact.MemberName.clear();
00119
00120 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00121
00122
00123 if(type->satisfied())
00124 {
00125 NLAIAGENT::IObjectIA *c = (NLAIAGENT::IObjectIA *)((NLAIC::IClassFactory *)type->getConstraintTypeOf()->getFactory())->getClass();
00126 sint32 i = c->getStaticMemberIndex(NLAIAGENT::CStringVarName(_LasVarStr.front().data()));
00127 if(i >= 0)
00128 {
00129
00130
00131
00132
00133
00134 {
00135 _LastFact.Member.push_back(i);
00136 }
00137
00138 }
00139 else
00140 {
00141 type->release();
00142 yyerror("attribut is not define");
00143 return 0;
00144 }
00145 NLAIC::CIdentType *id = new NLAIC::CIdentType (c->getType());
00146 _FlotingExpressionType = new COperandSimple(id);
00147 type->release();
00148 }
00149 else
00150 {
00151 _LastFact.MemberName = _LasVarStr;
00152 type->incRef();
00153 IOpType *constr = (IOpType *)getMethodConstraint(CConstraintStackComp(CConstraintStackComp::stackCall,0,_LastFact.MemberName ,type,0,0));
00154 if(constr == NULL)
00155 {
00156 constr = new CConstraintStackComp(CConstraintStackComp::stackCall,0,_LastFact.MemberName,type,yyLine,yyColone);
00157 }
00158 _FlotingExpressionType = constr;
00159 }
00160
00161 if(_LastFact.TypeStack) _LastFact.TypeStack->release();
00162 _LastFact.TypeStack = _FlotingExpressionType;
00163 _LastFact.TypeStack->incRef();
00164 _LastFact.ValueVar = NULL;
00165 _LastFact.VarType = varTypeStackMember;
00166 _LastFact.IsUsed = false;
00167
00168 }
00169 else
00170 {
00171 yyerror("variable used but never declared");
00172 return 0;
00173 }
00174 }
00175 }
00176 else
00177 {
00178 _LastFact.ValueVar = var;
00179 _LastFact.VarType = varTypeLocal;
00180 _LastFact.IsUsed = false;
00181 _LasVarStr.clear();
00182 if(var->getType() == CVarPStackParam::IdVarPStackParam)
00183 {
00184 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00185 try
00186 {
00187 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType(((CVarPStackParam *)var)->getVarType()));
00188 }
00189 catch(NLAIE::CExceptionUnReference &)
00190 {
00191 _FlotingExpressionType = (IOpType *)((const CObjectUnknown *)((CVarPStackParam *)var)->getObject())->getClassType();
00192 _FlotingExpressionType->incRef();
00193 }
00194 }
00195 else
00196 {
00197 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00198 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType(var->getType()));
00199 }
00200 }
00201 }
00202 else
00203 {
00204
00205 CConstraintStackComp::OpCodeType opCodeType = CConstraintStackComp::stackCall;
00206 sint32 posStack = 0;
00207 NLAIAGENT::CStringVarName varName(_LasVarStr.back().data());
00208 if(var != NULL)
00209 {
00210 _LasVarStr.pop_front();
00211 IOpType *cont = (IOpType *)((const CObjectUnknown *)((CVarPStackParam *)var)->getObject())->getBaseType();
00212 if(cont->satisfied())
00213 {
00214 const NLAIC::CIdentType &id = *cont->getConstraintTypeOf();
00215 IClassInterpret *cl;
00216
00217 if(strcmp((const char *)id,((IClassInterpret *)_SelfClass.get())->getClassName()->getString()) == 0)
00218 {
00219 cl = (IClassInterpret *)_SelfClass.get();
00220 }
00221 else cl = (IClassInterpret *)((CClassInterpretFactory *)id.getFactory())->getClass();
00222 IOpType *c;
00223 _LastFact.Member.clear();
00224 if(!isValidateVarName(cl,_LastFact.Member,_LasVarStr,c))
00225 {
00226 if(c) c->release();
00227 std::string text;
00228 text = NLAIC::stringGetBuild("var '%s' is not define",varName.getString());
00229 yyerror((char *)text.c_str());
00230 return false;
00231 }
00232 _FlotingExpressionType = c;
00233 if(_LastFact.TypeStack) _LastFact.TypeStack->release();
00234 _LastFact.TypeStack = _FlotingExpressionType;
00235 _LastFact.TypeStack->incRef();
00236 _LastFact.ValueVar = var;
00237 _LastFact.VarType = varTypeHeapMember;
00238 _LastFact.IsUsed = false;
00239 #ifdef NL_DEBUG
00240 std::list<sint32>::iterator i_dbg = _LastFact.Member.begin();
00241 while(i_dbg != _LastFact.Member.end())
00242 {
00243 int k = *i_dbg++;
00244 }
00245
00246 #endif
00247 return true;
00248 }
00249 else
00250 {
00251 opCodeType = CConstraintStackComp::heapCall;
00252 posStack = ((CVarPStackParam *)var)->getIndex();
00253 cont->incRef();
00254 _TypeList.push_back(cont);
00255 }
00256
00257 }
00258
00259 if(!_TypeList.size())
00260 {
00261 _LastFact.Member.clear();
00262 IOpType *c;
00263 if(!isValidateVarName(_LastFact.Member,_LasVarStr,c))
00264 {
00265 if(c) c->release();
00266 std::string text;
00267 text = NLAIC::stringGetBuild("var '%s' is not define",varName.getString());
00268 yyerror((char *)text.c_str());
00269 return false;
00270 }
00271 _LastFact.ValueVar = NULL;
00272 _LastFact.VarType = varTypeMember;
00273 _LastFact.IsUsed = false;
00274 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00275 _FlotingExpressionType = (IOpType *)c;
00276 }
00277 else
00278 {
00279 IOpType *type = _TypeList.back();
00280 _TypeList.pop_back();
00281 _LastFact.Member.clear();
00282 _LastFact.MemberName.clear();
00283
00284 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00285
00286 if(type->satisfied())
00287 {
00288 IClassInterpret *cl = (IClassInterpret *)type->getConstraintTypeOf()->allocClass();
00289 IOpType *c;
00290 if(!isValidateVarName(cl,_LastFact.Member,_LasVarStr,c))
00291 {
00292 if(c) c->release();
00293 type->release();
00294 cl->release();
00295 std::string text;
00296 text = NLAIC::stringGetBuild("var '%s' is not define",varName.getString());
00297 yyerror((char *)text.c_str());
00298 return false;
00299 }
00300 _FlotingExpressionType = (IOpType *)c;
00301 type->release();
00302 cl->release();
00303 }
00304 else
00305 {
00306 _LastFact.MemberName = _LasVarStr;
00307 type->incRef();
00308 IOpType *constr = (IOpType *)getMethodConstraint(CConstraintStackComp(opCodeType,posStack,_LastFact.MemberName ,type,0,0));
00309 if(constr == NULL)
00310 {
00311 constr = new CConstraintStackComp(opCodeType,posStack,_LastFact.MemberName,type,yyLine,yyColone);
00312 }
00313 else
00314 {
00315 constr->incRef();
00316 }
00317 _FlotingExpressionType = constr;
00318 }
00319
00320 if(_LastFact.TypeStack) _LastFact.TypeStack->release();
00321 _LastFact.TypeStack = _FlotingExpressionType;
00322 _LastFact.TypeStack->incRef();
00323 _LastFact.ValueVar = NULL;
00324 _LastFact.VarType = varTypeStackMember;
00325 _LastFact.IsUsed = false;
00326 }
00327 }
00328 }
00329 else
00330 {
00331 yyerror("erreur de definition de variable");
00332 return 0;
00333 }
00334 return true;
00335 }
00336
00337 bool CCompilateur::setParamVarName()
00338 {
00339 try
00340 {
00341 NLAIC::CIdentType idType = getTypeOfClass(_LastString);
00342 NLAIAGENT::IObjectIA *i;
00343
00344 if(_BaseObjectDef)
00345 {
00346 NLAIC::CIdentType idBase = getTypeOfClass(_LastBaseObjectDef);
00347 COperandSimple *t = new COperandSimple(new NLAIC::CIdentType(idType));
00348 COperandSimple *b = new COperandSimple(new NLAIC::CIdentType(idBase));
00349 _Param.back()->push(t);
00350 t->incRef();
00351 i = new CObjectUnknown(t,b);
00352 }
00353 else
00354 {
00355 COperandSimple *c = new COperandSimple(new NLAIC::CIdentType(idType));
00356 _Param.back()->push(c);
00357 c->incRef();
00358 i = new CObjectUnknown(c);
00359 }
00360
00361 NLAIAGENT::IVarName *s = new NLAIAGENT::CStringVarName (LastyyText[1]);
00362 _Attrib.push_back(pairType(s,i));
00363
00364 i->incRef();
00365 _Heap[(int)_Heap] = i;
00366 _Heap ++;
00367 }
00368 catch(NLAIE::IException &e)
00369 {
00370 std::string text;
00371 text = NLAIC::stringGetBuild("Bad reference or class reference undefined '%s'", e.what());
00372 yyerror((char *)text.c_str());
00373 return 0;
00374 }
00375 return true;
00376
00377 }
00378
00379 void CCompilateur::addParamRuleCont()
00380 {
00381 _LastBloc->addCode(new CLdbOpCode (NLAIAGENT::CGroupType()));
00382 }
00383
00384 void CCompilateur::addParamCont()
00385 {
00386 _LastBloc->addCode(new CLdbOpCode (CPramContainer()));
00387 }
00388
00389 void CCompilateur::pushParamExpression()
00390 {
00391 _LastBloc->addCode((new CAddOpCode));
00392 _Param.back()->push(_ExpressionType);
00393 if(!_ExpressionType->satisfied())
00394 {
00395 _ConstraintType.push_back(_ExpressionType);
00396 _ExpressionType->incRef();
00397 }
00398 _ExpressionType = NULL;
00399 }
00400
00401 void CCompilateur::setPerformative(NLAIAGENT::IMessageBase::TPerformatif p)
00402 {
00403 if(_LastFact.Value != NULL)
00404 _LastFact.Value->release();
00405 switch(p)
00406 {
00407 case NLAIAGENT::IMessageBase::PExec:
00408 _LastFact.Value = new NLAIAGENT::CPExec();
00409 break;
00410 case NLAIAGENT::IMessageBase::PAchieve:
00411 _LastFact.Value = new NLAIAGENT::CPAchieve();
00412 break;
00413 case NLAIAGENT::IMessageBase::PAsk:
00414 _LastFact.Value = new NLAIAGENT::CPAsk();
00415 break;
00416 case NLAIAGENT::IMessageBase::PBreak:
00417 _LastFact.Value = new NLAIAGENT::CPBreak();
00418 break;
00419 case NLAIAGENT::IMessageBase::PTell:
00420 _LastFact.Value = new NLAIAGENT::CPTell();
00421 break;
00422 case NLAIAGENT::IMessageBase::PKill:
00423 _LastFact.Value = new NLAIAGENT::CPKill();
00424 break;
00425 case NLAIAGENT::IMessageBase::PError:
00426 _LastFact.Value = new NLAIAGENT::CPError();
00427 break;
00428 case NLAIAGENT::IMessageBase::PEven:
00429 _LastFact.Value = new NLAIAGENT::CPEven();
00430 break;
00431 default: break;
00432 }
00433 _LastFact.VarType = varTypeImediate;
00434 _LastFact.IsUsed = false;
00435 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00436 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType(_LastFact.Value->getType()));
00437 }
00438
00439 void CCompilateur::setImediateVarNill()
00440 {
00441 if(_LastFact.Value != NULL)
00442 _LastFact.Value->release();
00443
00444 _LastFact.Value = &NLAIAGENT::DigitalType::NullOperator;
00445 _LastFact.Value->incRef();
00446 _LastFact.VarType = varTypeImediate;
00447 _LastFact.IsUsed = false;
00448 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00449 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType(_LastFact.Value->getType()));
00450 }
00451
00452 void CCompilateur::setImediateVar()
00453 {
00454 if(_LastFact.Value != NULL)
00455 _LastFact.Value->release();
00456 _LastFact.Value = new NLAIAGENT::DDigitalType(LastyyNum);
00457 _LastFact.VarType = varTypeImediate;
00458 _LastFact.IsUsed = false;
00459 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00460 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType(_LastFact.Value->getType()));
00461 }
00462
00463 void CCompilateur::setListVar()
00464 {
00465 _LastBloc->addCode(new CLdbOpCode (NLAIAGENT::CGroupType()));
00466 NLAIAGENT::CGroupType l;
00467 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00468 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType(l.getType()));
00469 }
00470
00471 void CCompilateur::setChaineVar()
00472 {
00473 if(_LastFact.Value != NULL)
00474 _LastFact.Value->release();
00475 _LastFact.Value = new NLAIAGENT::CStringType(NLAIAGENT::CStringVarName(LastyyText[1]));
00476 _LastFact.VarType = varTypeImediate;
00477 _LastFact.IsUsed = false;
00478 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00479 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType(_LastFact.Value->getType()));
00480 }
00481
00482 void CCompilateur::setStackVar(const NLAIC::CIdentType &type)
00483 {
00484 if(_LastFact.Value != NULL)
00485 _LastFact.Value->release();
00486 _LastFact.VarType = varForFunc;
00487 _LastFact.IsUsed = false;
00488 if(_FlotingExpressionType != NULL) _FlotingExpressionType->release();
00489 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType(type) );
00490 }
00491
00492 void CCompilateur::setMethodVar()
00493 {
00494
00495 #ifdef NL_DEBUG
00496 std::string mName;
00497 std::string pName;
00498 _LastStringParam.back()->getDebugString(mName);
00499 _Param.back()->getDebugString(pName);
00500 #endif
00501 if(_LastFact.Value != NULL)
00502 _LastFact.Value->release();
00503 _LastFact.Value = NULL;
00504 _LastFact.VarType = varForFunc;
00505 _LastFact.IsUsed = false;
00506
00507 if(_FlotingExpressionType != NULL)
00508 {
00509 _FlotingExpressionType->release();
00510 _FlotingExpressionType = NULL;
00511 }
00512 _LastStringParam.back()->incRef();
00513 _Param.back()->incRef();
00514 if(_LastbaseClass != NULL)_LastbaseClass->incRef();
00515 IConstraint *c = getMethodConstraint(CConstraintMethode((CConstraintMethode::TCallTypeOpCode) _LastTypeCall,_LastPosHeap,_LastbaseClass,_LastStringParam.back(),_Param.back(),0,0));
00516 if(c == NULL)
00517 {
00518 _LastStringParam.back()->incRef();
00519 _Param.back()->incRef();
00520 if(_LastbaseClass != NULL)_LastbaseClass->incRef();
00521 c = new CConstraintMethode((CConstraintMethode::TCallTypeOpCode) _LastTypeCall,_LastPosHeap,_LastbaseClass,_LastStringParam.back(),_Param.back(),yyLine,yyColone);
00522 _MethodConstraint.push_back(c);
00523 }
00524 c->incRef();
00525 IConstraint *cm = new CConstraintChkMethodeType(c,0,0);
00526 _FlotingExpressionType = new COperandUnknown(cm);
00527
00528 _LastStringParam.back()->release();
00529 _LastStringParam.pop_back();
00530 _Param.back()->release();
00531 _Param.pop_back();
00532 #ifdef NL_DEBUG
00533 if(_LastStringParam.size()) _LastStringParam.back()->getDebugString(mName);
00534 if(_Param.size())_Param.back()->getDebugString(pName);
00535
00536 #endif
00537 }
00538
00539 bool CCompilateur::buildObject()
00540 {
00541 NLAIAGENT::CStringType *s = (NLAIAGENT::CStringType *)_LastStringParam.back()->get();
00542 if(_LastStringParam.back()->size() == 1)
00543 {
00544
00545 try
00546 {
00547 NLAIC::CIdentType id(s->getStr().getString());
00548 if(_FlotingExpressionType) _FlotingExpressionType->release();
00549 _FlotingExpressionType = new COperandSimple(new NLAIC::CIdentType (id));
00550
00551 NLAIAGENT::IObjectIA *i = (NLAIAGENT::IObjectIA *)NLAIC::createInstance(id);
00552
00553 _LastBloc->addCode( new CLdbOpCode( *i ) );
00554 _LastBloc->addCode(new CHaltOpCode());
00555 CBagOfCode *b = _LastBloc->getBagOfCode();
00556
00557
00558 NLAIAGENT::IBaseGroupType *g = new NLAIAGENT::CGroupType();
00559 g->cpy(NLAIAGENT::CStringType(NLAIAGENT::CStringVarName(_CONSTRUCTOR_)));
00560
00561 _Param.back()->incRef();
00562 _FlotingExpressionType->incRef();
00563 CConstraintMethode *cm = new CConstraintMethode(CConstraintMethode::newCall,0,_FlotingExpressionType,g,_Param.back(),0,0);
00564 IConstraint *c = getMethodConstraint(*cm);
00565 if(c == NULL)
00566 {
00567 c = cm;
00568 _MethodConstraint.push_back(c);
00569 }
00570 else
00571 {
00572 cm->release();
00573 }
00574 b->addConstraint(c);
00575 i->release();
00576 _LastStringParam.back()->release();
00577 _LastStringParam.pop_back();
00578 _Param.back()->release();
00579 _Param.pop_back();
00580 }
00581 catch(NLAIE::IException &)
00582 {
00583 std::string text;
00584 text = NLAIC::stringGetBuild("can't find '%s'",s->getStr().getString());
00585 yyerror((char *)text.c_str());
00586 return false;
00587 }
00588 return true;
00589 }
00590 return false;
00591 }
00592
00593 void CCompilateur::setTypeExpression()
00594 {
00595 if(_ExpressionType)
00596 {
00597 COperandUnknown *gd = new COperandUnknown(_ExpressionType);
00598 _ExpressionType = gd;
00599 }
00600 else
00601 {
00602 _ExpressionType = new COperandUnknown(_FlotingExpressionType);
00603 _FlotingExpressionType = NULL;
00604 }
00605 }
00606
00607 void CCompilateur::setTypeExpression(NLAIC::CTypeOfOperator::TTypeOp op,const char *txtOp)
00608 {
00609 if ( _ExpressionType )
00610 {
00611 COperationType *gd = new COperationType;
00612 gd->setOperand(_ExpressionType);
00613 gd->setOp(op);
00614 _ExpressionType = gd;
00615 }
00616 else
00617 {
00618 _ExpressionType = new COperationType;
00619 ((COperationType *)_ExpressionType)->setOperand(_FlotingExpressionType);
00620 ((COperationType *)_ExpressionType)->setOp(op);
00621 _FlotingExpressionType = NULL;
00622 }
00623 }
00624
00625 void CCompilateur::setTypeExpressionG()
00626 {
00627 if(_ExpressionType)
00628 {
00629 COperationTypeGD *gd = new COperationTypeGD;
00630 gd->setOperationG(_ExpressionType);
00631 _ExpressionType = gd;
00632 }
00633 else
00634 {
00635 _ExpressionType = new COperationTypeGD;
00636 ((COperationTypeGD *)_ExpressionType)->setOperationG(_FlotingExpressionType);
00637 _FlotingExpressionType = NULL;
00638 }
00639 _ExpressionOp.push_back(_ExpressionType);
00640 _ExpressionType = NULL;
00641 }
00642
00643 void CCompilateur::setTypeExpressionD(NLAIC::CTypeOfOperator::TTypeOp op,const char *txtOp)
00644 {
00645 if(_ExpressionType != NULL && _FlotingExpressionType == NULL) _FlotingExpressionType = _ExpressionType;
00646
00647 _ExpressionType = _ExpressionOp.back();
00648 _ExpressionOp.pop_back();
00649
00650 ((COperationTypeGD *)_ExpressionType)->setOperationD(_FlotingExpressionType);
00651 ((COperationTypeGD *)_ExpressionType)->setOp(op);
00652 _FlotingExpressionType = NULL;
00653 }
00654 }