SNAP Library 2.3, Developer Reference  2014-06-16 11:58:46
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
exp.cpp
Go to the documentation of this file.
1 // Expression-Value
5 
6 bool TExpVal::operator==(const TExpVal& ExpVal) const {
7  if (ValType!=ExpVal.ValType){return false;}
8  switch (ValType){
9  case evtUndef: return true;
10  case evtFlt: return Flt==ExpVal.Flt;
11  case evtStr: return Str==ExpVal.Str;
12  case evtVec:{
13  if (ValV.Len()!=ExpVal.ValV.Len()){return false;}
14  for (int VecValN=0; VecValN<ExpVal.ValV.Len(); VecValN++){
15  if (*ValV[VecValN]!=*ExpVal.ValV[VecValN]){return false;}}
16  return true;}
17  case evtLst:{
18  if (ValL.Len()!=ExpVal.ValL.Len()){return false;}
19  TExpValLN ValLN=ValL.First();
20  TExpValLN ExpValLN=ExpVal.ValL.First();
21  while (ValLN!=NULL){
22  if (*ValLN->GetVal()!=*ExpValLN->GetVal()){return false;}
23  ValLN=ValLN->Next();
24  ExpValLN=ExpValLN->Next();
25  }
26  return true;}
27  default: Fail; return false;
28  }
29 }
30 
31 bool TExpVal::operator<(const TExpVal& ExpVal) const {
32  if (ValType!=ExpVal.ValType){
33  return ValType<ExpVal.ValType;}
34  switch (ValType){
35  case evtUndef: return false;
36  case evtFlt: return Flt<ExpVal.Flt;
37  case evtStr: return Str<ExpVal.Str;
38  case evtVec:{
39  int VecValN=0;
40  while ((VecValN<ValV.Len())&&(VecValN<ExpVal.ValV.Len())){
41  if (*ValV[VecValN]<*ExpVal.ValV[VecValN]){return true;}
42  else if (*ValV[VecValN]==*ExpVal.ValV[VecValN]){VecValN++;}
43  else {return false;}
44  }
45  return ValV.Len()<ExpVal.ValV.Len();}
46  case evtLst:{
47  if (ValL.Len()!=ExpVal.ValL.Len()){return false;}
48  TExpValLN ValLN=ValL.First();
49  TExpValLN ExpValLN=ExpVal.ValL.First();
50  while ((ValLN!=NULL)&&(ExpValLN!=NULL)){
51  if (*ValLN->GetVal()<*ExpValLN->GetVal()){
52  return true;}
53  else if (*ValLN->GetVal()==*ExpValLN->GetVal()){
54  ValLN=ValLN->Next(); ExpValLN=ExpValLN->Next();}
55  else {return false;}
56  }
57  return ValLN==NULL;}
58  default: Fail; return false;
59  }
60 }
61 
62 int TExpVal::GetFltValAsInt(const bool& ThrowExceptP) const {
63  double Flt=GetFltVal();
64  if ((Flt<double(TInt::Mn))&&(double(TInt::Mx)<Flt)){
65  if (ThrowExceptP){TExcept::Throw("Float too big for integer.");}
66  else {Flt=0;}
67  }
68  return int(Flt);
69 }
70 
71 void TExpVal::SaveTxt(TOLx& Lx) const {
72  TExpValType _ValType=TExpValType(int(ValType));
73  switch (_ValType){
74  case evtUndef: Lx.PutIdStr("Undef"); break;
75  case evtFlt: Lx.PutFlt(Flt); break;
76  case evtStr: Lx.PutQStr(Str); break;
77  case evtVec:{
78  Lx.PutSym(syLBrace);
79  for (int ValN=0; ValN<ValV.Len(); ValN++){
80  ValV[ValN]->SaveTxt(Lx);}
81  Lx.PutSym(syRBrace);
82  break;}
83  case evtLst:{
84  Lx.PutSym(syLBracket);
85  TExpValLN ValLN=ValL.First();
86  for (int ValN=0; ValN<ValL.Len(); ValN++){
87  ValLN->GetVal()->SaveTxt(Lx); ValLN=ValLN->Next();}
88  Lx.PutSym(syRBracket);
89  break;}
90  default: Fail;
91  }
92 }
93 
94 void TExpVal::SaveTxt(const PSOut& SOut) const {
96  SaveTxt(Lx);
97 }
98 
100  PSOut SOut=TMOut::New(); TMOut& MOut=*(TMOut*)SOut();
101  SaveTxt(SOut);
102  TStr ExpValStr=TStr::LoadTxt(MOut.GetSIn());
103  return ExpValStr;
104 }
105 
107  PExpVal CloneExpVal=TExpVal::New();
108  CloneExpVal->ValType=ExpVal->ValType;
109  CloneExpVal->Flt=ExpVal->Flt;
110  CloneExpVal->Str=ExpVal->Str;
111  CloneExpVal->ValV.Gen(ExpVal->ValV.Len(), 0);
112  for (int VecValN=0; VecValN<ExpVal->ValV.Len(); VecValN++){
113  CloneExpVal->ValV.Add(MkClone(ExpVal->ValV[VecValN]));
114  }
115  TExpValLN ExpValLN=ExpVal->ValL.First();
116  while (ExpValLN!=NULL){
117  CloneExpVal->ValL.AddBack(MkClone(ExpValLN->GetVal()));
118  ExpValLN=ExpValLN->Next();
119  }
120  return CloneExpVal;
121 }
122 
124  if (UndefExpVal.Empty()){
126  return UndefExpVal;
127 }
128 
130  if (ZeroExpVal.Empty()){
131  ZeroExpVal=TExpVal::New(double(0));}
132  return ZeroExpVal;
133 }
134 
136 // Expression-Environment
138  const TStr& RqFuncNm, const TExpFuncArgType& RqFuncArgType,
139  const TStr& FuncNm, const TExpValV& ArgValV){
140  if (RqFuncNm.GetUc()!=FuncNm.GetUc()){return false;}
141  switch (RqFuncArgType){
142  case efatVoid: return ArgValV.Len()==0;
143  case efatFlt:
144  if (ArgValV.Len()!=1){return false;}
145  if (ArgValV[0]->GetValType()!=evtFlt){return false;}
146  return true;
147  case efatStr:
148  if (ArgValV.Len()!=1){return false;}
149  if (ArgValV[0]->GetValType()!=evtStr){return false;}
150  return true;
151  case efatFltFlt:
152  if (ArgValV.Len()!=2){return false;}
153  if (ArgValV[0]->GetValType()!=evtFlt){return false;}
154  if (ArgValV[1]->GetValType()!=evtFlt){return false;}
155  return true;
156  case efatFltStr:
157  if (ArgValV.Len()!=2){return false;}
158  if (ArgValV[0]->GetValType()!=evtFlt){return false;}
159  if (ArgValV[1]->GetValType()!=evtStr){return false;}
160  return true;
161  case efatStrFlt:
162  if (ArgValV.Len()!=2){return false;}
163  if (ArgValV[0]->GetValType()!=evtStr){return false;}
164  if (ArgValV[1]->GetValType()!=evtFlt){return false;}
165  return true;
166  case efatStrStr:
167  if (ArgValV.Len()!=2){return false;}
168  if (ArgValV[0]->GetValType()!=evtStr){return false;}
169  if (ArgValV[1]->GetValType()!=evtStr){return false;}
170  return true;
171  case efatStrAny:
172  if (ArgValV.Len()!=2){return false;}
173  if (ArgValV[0]->GetValType()!=evtStr){return false;}
174  return true;
175  default: Fail; return false;
176  }
177 }
178 
180 
182 // Expression-Built-Ins
184  ExpBiNmToIdH(100), ExpBiIdToArgTypeH(100){
185  // constants
186  AddBi("Undef", ebi_Undef);
187  AddBi("True", ebi_True);
188  AddBi("False", ebi_False);
189  AddBi("E", ebi_E);
190  AddBi("Pi", ebi_Pi);
191 
192  // trigonometric funcions
193  AddBi("Sin", ebi_Sin, ebatFlt);
194  AddBi("Cos", ebi_Cos, ebatFlt);
195  AddBi("Tan", ebi_Tan, ebatFlt);
196  AddBi("ASin", ebi_ASin, ebatFlt);
197  AddBi("ACos", ebi_ACos, ebatFlt);
198  AddBi("ATan", ebi_ATan, ebatFlt);
199  AddBi("SinH", ebi_SinH, ebatFlt);
200  AddBi("CosH", ebi_CosH, ebatFlt);
201  AddBi("TanH", ebi_TanH, ebatFlt);
202 
203  // exponential functions
204  AddBi("Pow", ebi_Pow, ebatFltFlt);
205  AddBi("Exp", ebi_Exp, ebatFlt);
206  AddBi("Sqr", ebi_Sqr, ebatFlt);
207  AddBi("Sqrt", ebi_Sqrt, ebatFlt);
208  AddBi("Log", ebi_Log, ebatFlt);
209  AddBi("Log10", ebi_Log10, ebatFlt);
210 
211  // number manipulation functions
212  AddBi("Ceil", ebi_Ceil, ebatFlt);
213  AddBi("Floor", ebi_Floor, ebatFlt);
214  AddBi("Int", ebi_Int, ebatFlt);
215  AddBi("Frac", ebi_Frac, ebatFlt);
216  AddBi("Abs", ebi_Abs, ebatFlt);
217 
218  // random deviates
219  AddBi("UniDev", ebi_UniDev, ebatVoid);
220  AddBi("NrmDev", ebi_NrmDev, ebatVoid);
221  AddBi("ExpDev", ebi_ExpDev, ebatVoid);
222  AddBi("GamDev", ebi_GamDev, ebatFlt);
223  AddBi("PoiDev", ebi_PoiDev, ebatFlt);
224  AddBi("BinDev", ebi_BinDev, ebatFltFlt);
225  AddBi("UniDevStep", ebi_UniDevStep, ebatFltFlt);
226 
227  // assign values to constants
229  Val_True=TExpVal::New(double(1));
230  Val_False=TExpVal::New(double(0));
233 }
234 
235 void TExpBi::AddBi(const TStr& ExpBiNm, const TExpBiId& ExpBiId,
236  const TExpBiArgType& ExpBiArgType){
237  ExpBiNmToIdH.AddDat(ExpBiNm.GetUc(), TInt(int(ExpBiId)));
238  ExpBiIdToArgTypeH.AddDat(TInt(int(ExpBiId)), TInt(int(ExpBiArgType)));
239 }
240 
241 bool TExpBi::IsExpBiId(const TStr& ExpBiNm, TExpBiId& ExpBiId){
242  int ExpBiIdP;
243  if (ExpBiNmToIdH.IsKey(ExpBiNm.GetUc(), ExpBiIdP)){
244  ExpBiId=TExpBiId(int(ExpBiNmToIdH[ExpBiIdP])); return true;
245  } else {
246  ExpBiId=ebi_Undef; return false;
247  }
248 }
249 
251  TInt ExpBiArgType=ExpBiIdToArgTypeH.GetDat(TInt(int(ExpBiId)));
252  return TExpBiArgType(int(ExpBiArgType));
253 }
254 
255 void TExpBi::AssertArgs(const int& RqArgs, const int& ActArgs){
256  if (RqArgs!=ActArgs){
257  TExcept::Throw("Invalid number of arguments.");
258  }
259 }
260 
262  const TExpValType& ExpValType, const PExpVal& ExpVal){
263  if (ExpValType!=ExpVal->GetValType()){
264  TExcept::Throw("Invalid type of argument.");
265  }
266 }
267 
269  switch (ExpBiId){
270  case ebi_Undef: return Val_Undef;
271  case ebi_True: return Val_True;
272  case ebi_False: return Val_False;
273  case ebi_E: return Val_E;
274  case ebi_Pi: return Val_Pi;
275  default: TExcept::Throw("Invalid constant."); return Val_Undef;
276  }
277 }
278 
280  const TExpBiId& ExpBiId, const TExpValV& ArgValV, const PExpEnv& ExpEnv){
281  TExpBiArgType ExpBiArgType=TExpBi::GetExpBiArgType(ExpBiId);
282  int Args=ArgValV.Len();
283  double ArgFlt1=0; double ArgFlt2=0;
284  switch (ExpBiArgType){
285  case ebatUndef: Fail; break;
286  case ebatVoid:
287  AssertArgs(0, Args); break;
288  case ebatFlt:
289  AssertArgs(1, Args);
290  AssertArgValType(evtFlt, ArgValV[0]);
291  ArgFlt1=ArgValV[0]->GetFltVal(); break;
292  case ebatFltFlt:
293  AssertArgs(2, Args);
294  AssertArgValType(evtFlt, ArgValV[0]);
295  AssertArgValType(evtFlt, ArgValV[1]);
296  ArgFlt1=ArgValV[0]->GetFltVal();
297  ArgFlt2=ArgValV[1]->GetFltVal(); break;
298  default: Fail;
299  }
300  PExpVal ExpVal;
301  switch (ExpBiId){
302  // trigonometric funcions
303  case ebi_Sin: ExpVal=TExpVal::New(sin(ArgFlt1)); break;
304  case ebi_Cos: ExpVal=TExpVal::New(cos(ArgFlt1)); break;
305  case ebi_Tan: ExpVal=TExpVal::New(tan(ArgFlt1)); break;
306  case ebi_ASin: ExpVal=TExpVal::New(asin(ArgFlt1)); break;
307  case ebi_ACos: ExpVal=TExpVal::New(acos(ArgFlt1)); break;
308  case ebi_ATan: ExpVal=TExpVal::New(atan(ArgFlt1)); break;
309  case ebi_SinH: ExpVal=TExpVal::New(sinh(ArgFlt1)); break;
310  case ebi_CosH: ExpVal=TExpVal::New(cosh(ArgFlt1)); break;
311  case ebi_TanH: ExpVal=TExpVal::New(tanh(ArgFlt1)); break;
312 
313  // exponential functions
314  case ebi_Pow: ExpVal=TExpVal::New(pow(ArgFlt1, ArgFlt2)); break;
315  case ebi_Exp: ExpVal=TExpVal::New(exp(ArgFlt1)); break;
316  case ebi_Sqr: ExpVal=TExpVal::New(TMath::Sqr(ArgFlt1)); break;
317  case ebi_Sqrt: ExpVal=TExpVal::New(sqrt(ArgFlt1)); break;
318  case ebi_Log: ExpVal=TExpVal::New(log(ArgFlt1)); break;
319  case ebi_Log10: ExpVal=TExpVal::New(log10(ArgFlt1)); break;
320 
321  // number manipulation functions
322  case ebi_Ceil: ExpVal=TExpVal::New(ceil(ArgFlt1)); break;
323  case ebi_Floor: ExpVal=TExpVal::New(floor(ArgFlt1)); break;
324  case ebi_Int:{
325  double Int; modf(ArgFlt1, &Int);
326  ExpVal=TExpVal::New(Int); break;}
327  case ebi_Frac:{
328  double Frac, Int; Frac=modf(ArgFlt1, &Int);
329  ExpVal=TExpVal::New(Frac); break;}
330  case ebi_Abs: ExpVal=TExpVal::New(fabs(ArgFlt1)); break;
331 
332  // random deviates
333  case ebi_UniDev: ExpVal=TExpVal::New(ExpEnv->GetRnd().GetUniDev()); break;
334  case ebi_NrmDev: ExpVal=TExpVal::New(ExpEnv->GetRnd().GetNrmDev()); break;
335  case ebi_ExpDev: ExpVal=TExpVal::New(ExpEnv->GetRnd().GetExpDev()); break;
336  case ebi_GamDev:{
337  int ArgInt1=int(ArgFlt1);
338  ExpVal=TExpVal::New(ExpEnv->GetRnd().GetGammaDev(ArgInt1)); break;}
339  case ebi_PoiDev:{
340  ExpVal=TExpVal::New(ExpEnv->GetRnd().GetPoissonDev(ArgFlt1)); break;}
341  case ebi_BinDev:{
342  int ArgInt2=int(ArgFlt2);
343  ExpVal=TExpVal::New(ExpEnv->GetRnd().GetBinomialDev(ArgFlt1, ArgInt2)); break;}
344  case ebi_UniDevStep:{
345  int ArgInt1=int(ArgFlt1); if (ArgInt1<0){ArgInt1=0;}
346  int ArgInt2=int(ArgFlt2);
347  ExpVal=TExpVal::New(TRnd::GetUniDevStep(ArgInt1, ArgInt2)); break;}
348  case ebi_NrmDevStep:{
349  int ArgInt1=int(ArgFlt1); if (ArgInt1<0){ArgInt1=0;}
350  int ArgInt2=int(ArgFlt2);
351  ExpVal=TExpVal::New(TRnd::GetNrmDevStep(ArgInt1, ArgInt2)); break;}
352  case ebi_ExpDevStep:{
353  int ArgInt1=int(ArgFlt1); if (ArgInt1<0){ArgInt1=0;}
354  int ArgInt2=int(ArgFlt2);
355  ExpVal=TExpVal::New(TRnd::GetExpDevStep(ArgInt1, ArgInt2)); break;}
356 
357  default: TExcept::Throw("Invalid function.");
358  }
359  return ExpVal;
360 }
361 
363 // Expression
365 
370 
372 const TFSet TExp::MulExpExpect(FactExpExpect);
373 const TFSet TExp::AddExpExpect(MulExpExpect, UAddOpSymSet);
374 const TFSet TExp::RelExpExpect(AddExpExpect);
375 const TFSet TExp::ExpExpect(RelExpExpect);
376 
378  const PExpEnv& ExpEnv, const bool& DbgP, TChA& DbgChA){
379  PExpVal OutExpVal;
380  TExpOp _ExpOp=TExpOp(int(ExpOp));
381  switch (_ExpOp){
382  case eoUPlus:
383  case eoUMinus:
384  case eoNot:{
385  PExpVal ExpVal=ArgExpV[0]->EvalExp(ExpEnv, DbgP, DbgChA);
386  TExpValType ExpValType=ExpVal->GetValType();
387  if (ExpValType==evtFlt){
388  TFlt Flt;
389  switch (_ExpOp){
390  case eoUPlus: Flt=ExpVal->GetFltVal(); break;
391  case eoUMinus: Flt=-ExpVal->GetFltVal(); break;
392  case eoNot: Flt=double(ExpVal->GetFltValAsInt()==0);
393  default: Fail; Flt=0;
394  }
395  OutExpVal=TExpVal::New(Flt);
396  } else {
397  TExcept::Throw("Bad argument types.");
398  }
399  break;}
400  case eoPlus:
401  case eoMinus:
402  case eoMul:
403  case eoDiv:
404  case eoIDiv:
405  case eoMod:
406  case eoAnd:
407  case eoOr:{
408  PExpVal LExpVal=ArgExpV[0]->EvalExp(ExpEnv, DbgP, DbgChA);
409  PExpVal RExpVal=ArgExpV[1]->EvalExp(ExpEnv, DbgP, DbgChA);
410  TExpValType LExpValType=LExpVal->GetValType();
411  TExpValType RExpValType=RExpVal->GetValType();
412  if ((LExpValType==evtFlt)&&(RExpValType==evtFlt)){
413  // check left expression
414  double LVal=LExpVal->GetFltVal();
415  int LValExpon; frexp(LVal, &LValExpon);
416  if (LValExpon>150){LExpVal=TExpVal::GetZeroExpVal();}
417  // check right expression
418  double RVal=LExpVal->GetFltVal();
419  int RValExpon; frexp(RVal, &RValExpon);
420  if (RValExpon>150){RExpVal=TExpVal::GetZeroExpVal();}
421  // calculate
422  TFlt Flt;
423  switch (_ExpOp){
424  case eoPlus: Flt=LExpVal->GetFltVal()+RExpVal->GetFltVal(); break;
425  case eoMinus: Flt=LExpVal->GetFltVal()-RExpVal->GetFltVal(); break;
426  case eoMul: Flt=LExpVal->GetFltVal()*RExpVal->GetFltVal(); break;
427  case eoDiv:
428  if (RExpVal->GetFltVal()==0){TExcept::Throw("Division by zero.");}
429  else {Flt=LExpVal->GetFltVal()/RExpVal->GetFltVal();}
430  break;
431  case eoIDiv:
432  if (RExpVal->GetFltValAsInt()==0){TExcept::Throw("Division by zero.");}
433  else {Flt=LExpVal->GetFltValAsInt()/RExpVal->GetFltValAsInt();}
434  break;
435  case eoMod:
436  if (RExpVal->GetFltValAsInt()==0){TExcept::Throw("Division by zero.");}
437  else {Flt=LExpVal->GetFltValAsInt()%RExpVal->GetFltValAsInt();}
438  break;
439  case eoAnd:
440  Flt=(LExpVal->GetFltValAsInt()!=0)&&(RExpVal->GetFltValAsInt()!=0); break;
441  case eoOr:
442  Flt=(LExpVal->GetFltValAsInt()!=0)||(RExpVal->GetFltValAsInt()!=0); break;
443  default: Fail; Flt=0;
444  }
445  OutExpVal=TExpVal::New(Flt);
446  } else
447  if ((_ExpOp==eoPlus)&&(LExpValType==evtStr)&&(RExpValType==evtStr)){
448  TStr Str=LExpVal->GetStrVal()+RExpVal->GetStrVal();
449  OutExpVal=TExpVal::New(Str);
450  } else {
451  TExcept::Throw("Bad argument types.");
452  }
453  break;}
454  case eoEq:
455  case eoNEq:
456  case eoLss:
457  case eoGtr:
458  case eoLEq:
459  case eoGEq:{
460  PExpVal LExpVal=ArgExpV[0]->EvalExp(ExpEnv, DbgP, DbgChA);
461  PExpVal RExpVal=ArgExpV[1]->EvalExp(ExpEnv, DbgP, DbgChA);
462  TExpValType LExpValType=LExpVal->GetValType();
463  TExpValType RExpValType=RExpVal->GetValType();
464  if ((LExpValType==evtFlt)&&(RExpValType==evtFlt)){
465  TFlt Flt;
466  switch (_ExpOp){
467  case eoEq: Flt=double(LExpVal->GetFltVal()==RExpVal->GetFltVal()); break;
468  case eoNEq: Flt=double(LExpVal->GetFltVal()!=RExpVal->GetFltVal()); break;
469  case eoLss: Flt=double(LExpVal->GetFltVal()<RExpVal->GetFltVal()); break;
470  case eoGtr: Flt=double(LExpVal->GetFltVal()>RExpVal->GetFltVal()); break;
471  case eoLEq: Flt=double(LExpVal->GetFltVal()<=RExpVal->GetFltVal()); break;
472  case eoGEq: Flt=double(LExpVal->GetFltVal()>=RExpVal->GetFltVal()); break;
473  default: Fail; Flt=0;
474  }
475  OutExpVal=TExpVal::New(Flt);
476  } else
477  if ((LExpValType==evtStr)&&(RExpValType==evtStr)){
478  TFlt Flt;
479  switch (_ExpOp){
480  case eoEq: Flt=double(LExpVal->GetStrVal()==RExpVal->GetStrVal()); break;
481  case eoNEq: Flt=double(LExpVal->GetStrVal()!=RExpVal->GetStrVal()); break;
482  case eoLss: Flt=double(LExpVal->GetStrVal()<RExpVal->GetStrVal()); break;
483  case eoGtr: Flt=double(LExpVal->GetStrVal()>RExpVal->GetStrVal()); break;
484  case eoLEq: Flt=double(LExpVal->GetStrVal()<=RExpVal->GetStrVal()); break;
485  case eoGEq: Flt=double(LExpVal->GetStrVal()>=RExpVal->GetStrVal()); break;
486  default: Fail; Flt=0;
487  }
488  OutExpVal=TExpVal::New(Flt);
489  } else {
490  TExcept::Throw("Bad argument types.");
491  }
492  break;}
493  case eoIf:{
494  PExpVal CondExpVal=ArgExpV[0]->EvalExp(ExpEnv, DbgP, DbgChA);
495  TExpValType CondExpValType=CondExpVal->GetValType();
496  if (CondExpValType==evtFlt){
497  PExpVal ExpVal;
498  if (CondExpVal->GetFltVal()!=0){
499  ExpVal=ArgExpV[1]->EvalExp(ExpEnv, DbgP, DbgChA);
500  } else {
501  ExpVal=ArgExpV[2]->EvalExp(ExpEnv, DbgP, DbgChA);
502  }
503  OutExpVal=ExpVal;
504  } else {
505  TExcept::Throw("Bad argument types.");
506  }
507  break;}
508  default: Fail; OutExpVal=NULL;
509  }
510  if (DbgP){
511  DbgChA+="['"; DbgChA+=TExp::GetExpOpStr(_ExpOp);
512  DbgChA+="'='"; DbgChA+=OutExpVal->GetStr(); DbgChA+="'] ";
513  }
514  return OutExpVal;
515 }
516 
518  const PExpEnv& ExpEnv, const bool& DbgP, TChA& DbgChA){
519  PExpVal OutExpVal;
520  TExpType _ExpType=TExpType(int(ExpType));
521  switch (_ExpType){
522  case etUndef:
523  OutExpVal=TExpVal::GetUndefExpVal();
524  break;
525  case etVal:
526  OutExpVal=ExpVal;
527  break;
528  case etVec:{
530  for (int ArgExpN=0; ArgExpN<ArgExpV.Len(); ArgExpN++){
531  PExpVal ArgExpVal=
532  PExpVal(ArgExpV[ArgExpN]->EvalExp(ExpEnv, DbgP, DbgChA));
533  ExpVal->AddToVec(ArgExpVal);
534  }
535  OutExpVal=ExpVal;
536  break;}
537  case etLst:{
539  for (int ArgExpN=0; ArgExpN<ArgExpV.Len(); ArgExpN++){
540  PExpVal ArgExpVal=
541  PExpVal(ArgExpV[ArgExpN]->EvalExp(ExpEnv, DbgP, DbgChA));
542  ExpVal->AddToLst(ArgExpVal);
543  }
544  OutExpVal=ExpVal;
545  break;}
546  case etOp:
547  OutExpVal=EvalExpOp(ExpEnv, DbgP, DbgChA); break;
548  case etVar:{
549  bool IsVar=false;
550  PExpVal ExpVal=ExpEnv->GetVarVal(ExpNm.GetUc(), IsVar);
551  if (!IsVar){TExcept::Throw(TStr("Variable not defined (")+ExpNm+").");}
552  OutExpVal=ExpVal;
553  break;}
554  case etBiConst:
555  OutExpVal=ExpBi.GetBiConstVal(TExpBiId(int(ExpBiId)));
556  break;
557  case etFunc:
558  case etBiFunc:{
559  TExpValV ArgExpValV(ArgExpV.Len(), 0);
560  for (int ArgExpN=0; ArgExpN<ArgExpV.Len(); ArgExpN++){
561  PExpVal ArgExpVal=
562  PExpVal(ArgExpV[ArgExpN]->EvalExp(ExpEnv, DbgP, DbgChA));
563  ArgExpValV.Add(ArgExpVal);
564  }
565  switch (_ExpType){
566  case etFunc:{
567  bool IsFunc=false;
568  PExpVal ExpVal=ExpEnv->GetFuncVal(ExpNm.GetUc(), ArgExpValV, IsFunc);
569  if (!IsFunc){
570  TExcept::Throw(TStr("Function not defined (")+ExpNm+").");}
571  OutExpVal=ExpVal;
572  break;}
573  case etBiFunc:
574  OutExpVal=ExpBi.GetBiFuncVal(TExpBiId(int(ExpBiId)), ArgExpValV, ExpEnv);
575  break;
576  default: Fail; OutExpVal=NULL;
577  }
578  break;}
579  default: Fail; OutExpVal=NULL;
580  }
581  if (DbgP){
582  switch (_ExpType){
583  case etVal:
584  case etOp:
585  break;
586  case etUndef:
587  case etVec:
588  case etLst:{
589  TStr ExpTypeStr=TExp::GetExpTypeStr(_ExpType);
590  DbgChA+='['; DbgChA+=ExpTypeStr; DbgChA+='=';
591  DbgChA+=OutExpVal->GetStr(); DbgChA+="] ";
592  break;}
593  case etVar:
594  case etBiConst:
595  case etFunc:
596  case etBiFunc:
597  DbgChA+='['; DbgChA+=ExpNm; DbgChA+='=';
598  DbgChA+=OutExpVal->GetStr(); DbgChA+="] ";
599  break;
600  default: Fail;
601  }
602  }
603  return OutExpVal;
604 }
605 
607  switch (LxSym){
608  case syPlus: return eoPlus;
609  case syMinus: return eoMinus;
610  case syAsterisk: return eoMul;
611  case sySlash: return eoDiv;
612  case syPercent: return eoMod;
613  case syExclamation: return eoNot;
614  case syVBar: return eoOr;
615  case syAmpersand: return eoAnd;
616  case syQuestion: return eoIf;
617  case syHash: return eoIDiv;
618  case syEq: return eoEq;
619  case syNEq: return eoNEq;
620  case syLss: return eoLss;
621  case syGtr: return eoGtr;
622  case syLEq: return eoLEq;
623  case syGEq: return eoGEq;
624  default: Fail; return eoUndef;
625  }
626 }
627 
629  switch (ExpOp){
630  case eoUPlus: return syPlus;
631  case eoUMinus: return syMinus;
632  case eoNot: return syExclamation;
633  case eoPlus: return syPlus;
634  case eoMinus: return syMinus;
635  case eoMul: return syAsterisk;
636  case eoDiv: return sySlash;
637  case eoIDiv: return syHash;
638  case eoMod: return syPercent;
639  case eoAnd: return syAmpersand;
640  case eoOr: return syVBar;
641  case eoEq: return syEq;
642  case eoNEq: return syNEq;
643  case eoLss: return syLss;
644  case eoGtr: return syGtr;
645  case eoLEq: return syLEq;
646  case eoGEq: return syGEq;
647  case eoIf: return syQuestion;
648  default: Fail; return syUndef;
649  }
650 }
651 
652 PExp TExp::LoadTxtFact(TILx& Lx, const TFSet& Expect){
653  PExp Exp;
654  switch (Lx.Sym){
655  case syFlt:{
657  Exp=PExp(new TExp(ExpVal));
658  Lx.GetSym(Expect);
659  break;}
660  case syIdStr:{
661  TStr ExpNm=Lx.Str;
662  Lx.GetSym(TFSet(Expect)|syLParen);
663  if (Lx.Sym==syLParen){
664  TExpV ArgExpV;
666  while (Lx.Sym!=syRParen){
667  if (Lx.Sym==syComma){Lx.GetSym(ExpExpect);}
668  PExp ArgExp=LoadTxtExp(Lx, TFSet()|syComma|syRParen);
669  ArgExpV.Add(ArgExp);
670  }
671  Lx.GetSym(Expect);
672  Exp=PExp(new TExp(ExpNm, ArgExpV));
673  } else {
674  Exp=PExp(new TExp(ExpNm));
675  }
676  break;}
677  case syQStr:{
679  Exp=PExp(new TExp(ExpVal));
680  Lx.GetSym(Expect);
681  break;}
682  case syLParen:{
683  Lx.GetSym(ExpExpect);
684  Exp=LoadTxtExp(Lx, TFSet()|syRParen);
685  Exp->IsParen=true;
686  Lx.GetSym(Expect);
687  break;}
688  default: Fail;
689  }
690  return Exp;
691 }
692 
693 PExp TExp::LoadTxtMulExp(TILx& Lx, const TFSet& Expect){
694  PExp Exp=LoadTxtFact(Lx, TFSet(Expect)|MulOpSymSet);
695  while (MulOpSymSet.In(Lx.Sym)){
697  Lx.GetSym(FactExpExpect);
698  PExp RExp=LoadTxtFact(Lx, TFSet(Expect)|MulOpSymSet);
699  Exp=PExp(new TExp(ExpOp, Exp, RExp));
700  }
701  return Exp;
702 }
703 
704 PExp TExp::LoadTxtAddExp(TILx& Lx, const TFSet& Expect){
705  TExpOp PrefExpOp=eoUndef;
706  if (Lx.Sym==syPlus){PrefExpOp=eoUPlus; Lx.GetSym(MulExpExpect);}
707  else if (Lx.Sym==syMinus){PrefExpOp=eoUMinus; Lx.GetSym(MulExpExpect);}
708  PExp Exp=LoadTxtMulExp(Lx, TFSet(Expect)|AddOpSymSet);
709  if (PrefExpOp!=eoUndef){
710  Exp=PExp(new TExp(PrefExpOp, Exp));}
711  while (AddOpSymSet.In(Lx.Sym)){
713  Lx.GetSym(MulExpExpect);
714  PExp RExp=LoadTxtMulExp(Lx, TFSet(Expect)|AddOpSymSet);
715  Exp=PExp(new TExp(ExpOp, Exp, RExp));
716  }
717  return Exp;
718 }
719 
720 PExp TExp::LoadTxtRelExp(TILx& Lx, const TFSet& Expect){
721  PExp Exp=LoadTxtAddExp(Lx, TFSet(Expect)|RelOpSymSet);
722  if (RelOpSymSet.In(Lx.Sym)){
724  Lx.GetSym(AddExpExpect);
725  PExp RExp=LoadTxtAddExp(Lx, Expect);
726  Exp=PExp(new TExp(ExpOp, Exp, RExp));
727  }
728  return Exp;
729 }
730 
731 PExp TExp::LoadTxtExp(TILx& Lx, const TFSet& Expect){
732  PExp Exp=LoadTxtRelExp(Lx, TFSet(Expect)|syQuestion);
733  if (Lx.Sym==syQuestion){
735  Lx.GetSym(ExpExpect);
736  PExp ThenExp=LoadTxtExp(Lx, TFSet()|syColon);
737  Lx.GetSym(ExpExpect);
738  PExp ElseExp=LoadTxtExp(Lx, Expect);
739  Exp=PExp(new TExp(ExpOp, Exp, ThenExp, ElseExp));
740  }
741  return Exp;
742 }
743 
744 void TExp::SaveTxtOp(TOLx& Lx) const {
745  IAssert(TExpType(static_cast<int>(ExpType))==etOp);
746  TExpOp _ExpOp=TExpOp(int(ExpOp));
747  TLxSym OpSym=GetLxSymFromExpOp(_ExpOp);
748  switch (_ExpOp){
749  case eoUPlus:
750  case eoUMinus:
751  case eoNot:
752  Lx.PutSym(OpSym); ArgExpV[0]->SaveTxt(Lx);
753  break;
754  case eoPlus: case eoMinus:
755  case eoMul: case eoDiv:
756  case eoIDiv: case eoMod:
757  case eoAnd: case eoOr:
758  case eoEq: case eoNEq:
759  case eoLss: case eoGtr:
760  case eoLEq: case eoGEq:
761  ArgExpV[0]->SaveTxt(Lx); Lx.PutSym(OpSym); ArgExpV[1]->SaveTxt(Lx);
762  break;
763  case eoIf:
764  ArgExpV[0]->SaveTxt(Lx); Lx.PutSym(OpSym);
765  ArgExpV[1]->SaveTxt(Lx); Lx.PutSym(syColon); ArgExpV[2]->SaveTxt(Lx);
766  break;
767  default: Fail;
768  }
769 }
770 
771 TExp::TExp(const TExpOp& _ExpOp,
772  const PExp& Exp1, const PExp& Exp2, const PExp& Exp3):
773  ExpType(etOp), IsParen(false),
774  ExpVal(), ExpNm(), ExpOp(_ExpOp), ExpBiId(), ArgExpV(){
775  ArgExpV.Add(Exp1);
776  if (!Exp2.Empty()){ArgExpV.Add(Exp2);}
777  if (!Exp3.Empty()){ArgExpV.Add(Exp3);}
778 }
779 
780 TExp::TExp(const PExpVal& _ExpVal):
781  ExpType(etVal), IsParen(false),
782  ExpVal(_ExpVal), ExpNm(), ExpOp(), ExpBiId(), ArgExpV(){}
783 
784 TExp::TExp(const TStr& _VarNm):
785  ExpType(), IsParen(false),
786  ExpVal(), ExpNm(_VarNm), ExpOp(), ExpBiId(), ArgExpV(){
787  TExpBiId _ExpBiId;
788  if (ExpBi.IsExpBiId(ExpNm, _ExpBiId)){
790  ExpBiId=TInt(int(_ExpBiId));
791  } else {
792  ExpType=etVar;
793  }
794 }
795 
796 TExp::TExp(const TStr& _FuncNm, const TExpV& _ArgExpV):
797  ExpType(), IsParen(false),
798  ExpVal(), ExpNm(_FuncNm), ExpOp(), ExpBiId(), ArgExpV(_ArgExpV){
799  TExpBiId _ExpBiId;
800  if (ExpBi.IsExpBiId(ExpNm, _ExpBiId)){
802  ExpBiId=TInt(int(_ExpBiId));
803  } else {
804  ExpType=etFunc;
805  }
806 }
807 
809  const PSIn& SIn, bool& Ok, TStr& MsgStr, const TFSet& Expect){
811  PExp Exp; Ok=true; MsgStr="Ok";
812  try {
813  Lx.GetSym(ExpExpect);
814  Exp=LoadTxtExp(Lx, Expect);
815  }
816  catch (PExcept Except){
817  Ok=false; MsgStr=Except->GetMsgStr();
818  Exp=PExp(new TExp(etUndef));
819  }
820  return Exp;
821 }
822 
823 void TExp::SaveTxt(TOLx& Lx) const {
824  if (IsParen){Lx.PutSym(syLParen);}
825  TExpType _ExpType=TExpType(int(ExpType));
826  switch (_ExpType){
827  case etVal:
828  ExpVal->SaveTxt(Lx); break;
829  case etVec:{
830  Lx.PutSym(syLBrace);
831  for (int ArgExpN=0; ArgExpN<ArgExpV.Len(); ArgExpN++){
832  if (ArgExpN>0){Lx.PutSym(syComma);}
833  ArgExpV[ArgExpN]->SaveTxt(Lx);
834  }
835  Lx.PutSym(syRBrace);
836  break;}
837  case etLst:{
838  Lx.PutSym(syLBracket);
839  for (int ArgExpN=0; ArgExpN<ArgExpV.Len(); ArgExpN++){
840  if (ArgExpN>0){Lx.PutSym(syComma);}
841  ArgExpV[ArgExpN]->SaveTxt(Lx);
842  }
843  Lx.PutSym(syRBracket);
844  break;}
845  case etOp:
846  SaveTxtOp(Lx); break;
847  case etVar:
848  case etBiConst:
849  Lx.PutIdStr(ExpNm); break;
850  case etFunc:
851  case etBiFunc:{
852  Lx.PutIdStr(ExpNm);
853  Lx.PutSym(syLParen);
854  for (int ArgExpN=0; ArgExpN<ArgExpV.Len(); ArgExpN++){
855  if (ArgExpN>0){Lx.PutSym(syComma);}
856  ArgExpV[ArgExpN]->SaveTxt(Lx);
857  }
858  Lx.PutSym(syRParen);
859  break;}
860  default: Fail;
861  }
862  if (IsParen){Lx.PutSym(syRParen);}
863 }
864 
866  PSOut SOut=TMOut::New(); TMOut& MOut=*(TMOut*)SOut();
867  SaveTxt(SOut);
868  TStr ExpStr=TStr::LoadTxt(MOut.GetSIn());
869  return ExpStr;
870 }
871 
873  TStr TopObjNm;
874  TExpType _ExpType=TExpType(int(ExpType));
875  switch (_ExpType){
876  case etOp:{
877  TExpOp _ExpOp=TExpOp(int(ExpOp));
878  TopObjNm=GetExpOpStr(_ExpOp);
879  break;}
880  case etVar:
881  case etBiConst:
882  case etFunc:
883  case etBiFunc:{
884  TopObjNm=ExpNm; break;}
885  default: break;
886  }
887  return TopObjNm;
888 }
889 
890 int TExp::GetArgExps() const {
891  return ArgExpV.Len();
892 }
893 
894 TStr TExp::GetArgExpStr(const int& ArgExpN) const {
895  return ArgExpV[ArgExpN]->GetStr();
896 }
897 
899  bool& Ok, TStr& MsgStr, const bool& DbgP, TStr& DbgStr, const PExpEnv& ExpEnv){
900  Ok=true; MsgStr="Ok";
901  PExpVal ExpVal; TChA DbgChA;
902  if (DbgP){DbgChA+="Debug Expression: ";}
903  try {
904  ExpVal=EvalExp(ExpEnv, DbgP, DbgChA);
905  }
906  catch (PExcept E){
907  Ok=false; MsgStr=E->GetMsgStr();
908  }
909  if (!Ok){return TExpVal::GetUndefExpVal();}
910  if (DbgP){
911  DbgChA+='['; DbgChA+=GetStr(); DbgChA+=" -> ";
912  DbgChA+=ExpVal->GetStr(); DbgChA+="] "; DbgChA+=MsgStr;
913  DbgStr=DbgChA;
914  }
915  return ExpVal;
916 }
917 
919  const TStr& ExpLStr, bool& Ok, TStr& MsgStr, const PExpEnv& ExpEnv){
920  // create final expression value
921  PExpVal ExpVal;
922  // transform exp. str. to input stream
923  PSIn SIn=TStrIn::New(ExpLStr);
924  // create lexical
926  TFSet Expect=TFSet()|sySemicolon|syEof;
927  // load & evaluate expression separated by semicolon
928  while (Lx.Sym!=syEof){
929  // create expression
930  PExp Exp; Ok=true; MsgStr="Ok";
931  try {
932  Lx.GetSym(ExpExpect);
933  Exp=LoadTxtExp(Lx, Expect);
934  }
935  catch (PExcept Except){
936  Ok=false; MsgStr=Except->GetMsgStr();
937  Exp=PExp(new TExp(etUndef));
938  }
939  // evaluate expression
940  if (Ok){
941  ExpVal=Exp->Eval(Ok, MsgStr, ExpEnv);
942  //printf("%s\n", ExpVal->GetStr().CStr());
943  if (!Ok){
944  return NULL;}
945  } else {
946  return NULL;
947  }
948  }
949  return ExpVal;
950 }
951 
953  switch (ExpType){
954  case etUndef: return "Undef";
955  case etVal: return "Val";
956  case etVec: return "Vec";
957  case etLst: return "Lst";
958  case etOp: return "Op";
959  case etVar: return "Var";
960  case etBiConst: return "BiConst";
961  case etFunc: return "Func";
962  case etBiFunc: return "BiFunc";
963  default: Fail; return "";
964  }
965 }
966 
967 void TExp::GetBiDescV(TStrPrV& BiDescV){
968  BiDescV.Clr();
969  // constants
970  BiDescV.Add(TStrPr("True", "Logical 'True' == 1."));
971  BiDescV.Add(TStrPr("False", "Logical 'False' == 0."));
972  BiDescV.Add(TStrPr("E", "Nat. logarithm basis (2.7182...)."));
973  BiDescV.Add(TStrPr("Pi", "Constant pi (3.1415...)."));
974 
975  // trigonometric funcions
976  BiDescV.Add(TStrPr("Sin(X)", "Sine of angle in radians."));
977  BiDescV.Add(TStrPr("Cos(X)", "Cosine of angle in radians."));
978  BiDescV.Add(TStrPr("Tan(X)", "Tangent of angle in radians."));
979  BiDescV.Add(TStrPr("ASin(X)", "Arc sine of (-1..+1)."));
980  BiDescV.Add(TStrPr("ACos(X)", "Arc cosine of (-1..+1)."));
981  BiDescV.Add(TStrPr("ATan(X)", "Arc tangent of (-inf..+inf)."));
982  BiDescV.Add(TStrPr("SinH(X)", "Hyperbolic sine."));
983  BiDescV.Add(TStrPr("CosH(X)", "Hyperbolic cosine."));
984  BiDescV.Add(TStrPr("TanH(X)", "Hyperbolic tangent."));
985 
986  // exponential functions
987  BiDescV.Add(TStrPr("Pow(X, Y)", "X to the power of Y."));
988  BiDescV.Add(TStrPr("Exp(X)", "Exponential E to the power of X."));
989  BiDescV.Add(TStrPr("Sqr(X)", "X squared."));
990  BiDescV.Add(TStrPr("Sqrt(X)", "Positive square root."));
991  BiDescV.Add(TStrPr("Log(X)", "Natural logarithm."));
992  BiDescV.Add(TStrPr("Log10(X)", "Base 10 logarithm."));
993 
994  // number manipulation functions
995  BiDescV.Add(TStrPr("Ceil(X)", "The smallest integer not less than X."));
996  BiDescV.Add(TStrPr("Floor(X)", "The largest integer not greater than X."));
997  BiDescV.Add(TStrPr("Int(X)", "Integer part of X."));
998  BiDescV.Add(TStrPr("Frac(X)", "Fractional part of X."));
999  BiDescV.Add(TStrPr("Abs(X)", "Absolute value of X."));
1000 
1001  // random deviates
1002  BiDescV.Add(TStrPr("UniDev()", "Uniform deviate (0..1)."));
1003  BiDescV.Add(TStrPr("NrmDev()", "Normal deviate (0, 1)."));
1004  BiDescV.Add(TStrPr("ExpDev()", "Exponential deviate."));
1005  BiDescV.Add(TStrPr("GamDev(Order)", "Gamma deviate of Order."));
1006  BiDescV.Add(TStrPr("PoiDev(Mean)", "Poisson deviate."));
1007  BiDescV.Add(TStrPr("BinDev(Prb, Trials)", "Binomial deviate."));
1008 
1009  // operators
1010  BiDescV.Add(TStrPr("+N", "Unary plus."));
1011  BiDescV.Add(TStrPr("-N", "Unary minus."));
1012  BiDescV.Add(TStrPr("!L", "Not."));
1013  BiDescV.Add(TStrPr("N1+N2", "Plus."));
1014  BiDescV.Add(TStrPr("N1-N2", "Minus."));
1015  BiDescV.Add(TStrPr("N1*N2", "Multiply."));
1016  BiDescV.Add(TStrPr("N1/N2", "Division."));
1017  BiDescV.Add(TStrPr("N1#N2", "Integer division."));
1018  BiDescV.Add(TStrPr("N1%N2", "Modulo."));
1019  BiDescV.Add(TStrPr("L1&L2", "And."));
1020  BiDescV.Add(TStrPr("L1|L2", "Or."));
1021  BiDescV.Add(TStrPr("E1=E2", "Equal."));
1022  BiDescV.Add(TStrPr("E1<>E2", "Not equal."));
1023  BiDescV.Add(TStrPr("E1<E2", "Less."));
1024  BiDescV.Add(TStrPr("E1>E2", "Greater."));
1025  BiDescV.Add(TStrPr("E1<=E2", "Less or equal."));
1026  BiDescV.Add(TStrPr("E1>=E2", "Greater or equal."));
1027  BiDescV.Add(TStrPr("L?E1:E2", "If L then return E1 else return E2."));
1028 }
1029 
1031 // Expression-Help-Object
1033  TChA ChA;
1034  switch (Type){
1035  case ehotOp: {// operator
1036  TStr OpStr=HdItem->GetNm();
1037  if (ArgItemV.Len()==1){
1038  ChA+=OpStr; ChA+=" "; ChA+=ArgItemV[0]->GetNm();
1039  } else
1040  if (ArgItemV.Len()==2){
1041  ChA+=ArgItemV[0]->GetNm();
1042  ChA+=" "; ChA+=OpStr; ChA+=" ";
1043  ChA+=ArgItemV[1]->GetNm();
1044  } else
1045  if (ArgItemV.Len()==3){
1046  ChA+=ArgItemV[0]->GetNm();
1047  ChA+=" "; ChA+=OpStr; ChA+=" ";
1048  ChA+=ArgItemV[1]->GetNm();
1049  ChA+=" "; ChA+=":"; ChA+=" ";
1050  ChA+=ArgItemV[2]->GetNm();
1051  } else {
1052  Fail;
1053  }
1054  break;}
1055  case ehotVar: // variable
1056  ChA+=HdItem->GetNm(); break;
1057  case ehotFunc: // function
1058  ChA+=HdItem->GetTypeStr(); ChA+=" <- ";
1059  ChA+=HdItem->GetNm();
1060  ChA+="(";
1061  {for (int ArgN=0; ArgN<ArgItemV.Len(); ArgN++){
1062  if (ArgN>0){ChA+=", ";}
1063  ChA+=ArgItemV[ArgN]->GetNm();
1064  }}
1065  ChA+=")";
1066  break;
1067  case ehotTempl: // template
1068  ChA+=HdItem->GetTypeStr(); break;
1069  default: Fail;
1070  }
1071  return ChA;
1072 }
1073 
1075  if (TypeStr=="Op"){return ehotOp;}
1076  else if (TypeStr=="Var"){return ehotVar;}
1077  else if (TypeStr=="Func"){return ehotFunc;}
1078  else if (TypeStr=="Templ"){return ehotTempl;}
1079  else {TExcept::Throw("Invalid object type.", TypeStr); return ehotUndef;}
1080 }
1081 
1083 // Expression-Help
1085  // create expression help
1086  PExpHelp ExpHelp=TExpHelp::New();
1087  // load xml with expression help
1088  PXmlDoc Doc=TXmlDoc::LoadTxt(SIn);
1089  // retrieve objects
1090  TXmlTokV ObjTokV; Doc->GetTagTokV("ExpHelp|Obj", ObjTokV);
1091  for (int ObjTokN=0; ObjTokN<ObjTokV.Len(); ObjTokN++){
1092  PXmlTok ObjTok=ObjTokV[ObjTokN];
1093  // type
1094  TStr TypeStr=ObjTok->GetTagTok("Type")->GetTokStr(false);
1095  // category
1096  TStr CatNm=ObjTok->GetTagTok("Cat")->GetTokStr(false);
1097  // header
1098  TStr HdNm=ObjTok->GetTagTok("Head|Name")->GetTokStr(false);
1099  TStr HdTypeStr=ObjTok->GetTagTok("Head|Type")->GetTokStr(false);
1100  TStr HdDescStr=ObjTok->GetTagTok("Head|Desc")->GetTokStr(false);
1101  PExpHelpItem HdItem=
1102  TExpHelpItem::New(HdNm, HdTypeStr, HdDescStr, "");
1103  // arguments
1104  TXmlTokV ArgTokV; ObjTok->GetTagTokV("Args|Arg", ArgTokV);
1105  TExpHelpItemV ArgItemV;
1106  for (int ArgTokN=0; ArgTokN<ArgTokV.Len(); ArgTokN++){
1107  PXmlTok ArgTok=ArgTokV[ArgTokN];
1108  // argument
1109  TStr ArgNm=ArgTok->GetTagTok("Name")->GetTokStr(false);
1110  TStr ArgTypeStr=ArgTok->GetTagTok("Type")->GetTokStr(false);
1111  TStr ArgDescStr=ArgTok->GetTagTok("Desc")->GetTokStr(false);
1112  TStr ArgDfValStr=ArgTok->GetTagTok("Default")->GetTokStr(false);
1113  PExpHelpItem ArgItem=
1114  TExpHelpItem::New(ArgNm, ArgTypeStr, ArgDescStr, ArgDfValStr);
1115  ArgItemV.Add(ArgItem);
1116  }
1117  // create & add object
1119  PExpHelpObj Obj=TExpHelpObj::New(Type, CatNm, HdItem, ArgItemV);
1120  ExpHelp->AddObj(Obj);
1121  }
1122  // return result
1123  return ExpHelp;
1124 }
1125 
1126 void TExpHelp::GetCatNmV(TStrV& CatNmV) const {
1127  CatNmV.Clr();
1128  for (int ObjN=0; ObjN<ObjV.Len(); ObjN++){
1129  TStr CatNm=ObjV[ObjN]->GetCatNm();
1130  CatNmV.AddUnique(CatNm);
1131  }
1132  CatNmV.Ins(0, "All");
1133 }
1134 
1135 void TExpHelp::GetObjHdNmV(const TStr& CatNm, TStrV& ObjHdNmV) const {
1136  ObjHdNmV.Clr();
1137  for (int ObjN=0; ObjN<ObjV.Len(); ObjN++){
1138  TStr ObjCatNm=ObjV[ObjN]->GetCatNm();
1139  TStr ObjHdNm=ObjV[ObjN]->GetHdItem()->GetNm();
1140  if ((CatNm.Empty())||(CatNm=="All")||(CatNm==ObjCatNm)){
1141  ObjHdNmV.AddUnique(ObjHdNm);}
1142  }
1143  ObjHdNmV.Sort();
1144 }
1145 
1146 PExpHelpObj TExpHelp::GetObj(const TStr& ObjNm) const {
1147  PExpHelpObj Obj;
1148  for (int ObjN=0; ObjN<ObjV.Len(); ObjN++){
1149  if (ObjV[ObjN]->GetHdItem()->GetNm().GetUc()==ObjNm.GetUc()){
1150  return ObjV[ObjN];}
1151  }
1152  return NULL;
1153 }
1154 
TSizeTy AddUnique(const TVal &Val)
Adds element Val to a vector only if the element Val is not already in the vector.
Definition: ds.h:1068
Definition: exp.h:171
Definition: exp.h:69
Definition: exp.h:5
#define IAssert(Cond)
Definition: bd.h:262
Definition: exp.h:121
Definition: exp.h:70
Definition: exp.h:169
PExpVal GetBiConstVal(const TExpBiId &ExpBiId)
Definition: exp.cpp:268
Definition: lx.h:248
Definition: lx.h:49
Definition: lx.h:49
Definition: lx.h:127
Definition: lx.h:50
static const TFSet ExpExpect
Definition: exp.h:199
static TExpOp GetExpOpFromLxSym(const TLxSym &LxSym)
Definition: exp.cpp:606
TExpBi()
Definition: exp.cpp:183
Definition: exp.h:170
static PExpHelp LoadXml(const PSIn &SIn)
Definition: exp.cpp:1084
Definition: exp.h:116
Definition: exp.h:67
static TLxSym GetLxSymFromExpOp(const TExpOp &ExpOp)
Definition: exp.cpp:628
TPair< TStr, TStr > TStrPr
Definition: ds.h:107
Definition: exp.h:180
Definition: lx.h:50
TExpOp
Definition: exp.h:175
Definition: lx.h:49
void AddBi(const TStr &ExpBiNm, const TExpBiId &ExpBiId, const TExpBiArgType &ExpBiArgType=ebatUndef)
Definition: exp.cpp:235
TStr GetStr() const
Definition: exp.cpp:865
Definition: exp.h:5
TStr GetHdArgNmStr() const
Definition: exp.cpp:1032
void SaveTxt(TOLx &Lx) const
Definition: exp.cpp:71
static PExpVal GetUndefExpVal()
Definition: exp.cpp:123
Definition: lx.h:48
Definition: exp.h:179
PSIn GetSIn(const bool &IsCut=true, const int &CutBfL=-1)
Definition: fl.cpp:666
Definition: exp.h:180
void SaveTxt(TOLx &Lx) const
Definition: exp.cpp:823
TExpBiArgType
Definition: exp.h:134
static const int Mx
Definition: dt.h:1046
#define Fail
Definition: bd.h:238
bool Empty() const
Definition: bd.h:501
TStr GetUc() const
Definition: dt.h:493
PExpVal Eval(bool &Ok, TStr &MsgStr, const bool &DbgP, TStr &DbgStr, const PExpEnv &ExpEnv=TExpEnv::DfExpEnv)
Definition: exp.cpp:898
Definition: exp.h:117
TLxSym
Definition: lx.h:44
Definition: lx.h:48
Definition: exp.h:180
Definition: bits.h:119
int Len() const
Definition: ds.h:3601
Definition: lx.h:47
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:535
void GetCatNmV(TStrV &CatNmV) const
Definition: exp.cpp:1126
Definition: exp.h:176
Definition: exp.h:116
static PExpVal GetZeroExpVal()
Definition: exp.cpp:129
Definition: exp.h:5
Definition: exp.h:9
TStr GetStr() const
Definition: exp.cpp:99
Definition: exp.h:120
Definition: lx.h:45
TInt ExpBiId
Definition: exp.h:194
Definition: lx.h:45
static PExpHelpObj New(const TExpHelpObjType &Type, const TStr &CatNm, const PExpHelpItem &HdItem, const TExpHelpItemV &ArgItemV=TExpHelpItemV())
Definition: exp.h:320
Definition: exp.h:120
Definition: exp.h:307
static const TFSet AddExpExpect
Definition: exp.h:198
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
void PutSym(const TLxSym &Sym)
Definition: lx.cpp:751
Definition: exp.h:178
static double Sqr(const double &x)
Definition: xmath.h:12
Definition: lx.h:47
void PutQStr(const TStr &Str)
Definition: lx.h:289
TPt< TExpVal > PExpVal
Definition: exp.h:9
Definition: exp.h:181
PExpHelpObj GetObj(const TStr &ObjNm) const
Definition: exp.cpp:1146
Definition: lx.h:49
int GetArgExps() const
Definition: exp.cpp:890
static PSOut New(const int &MxBfL=1024)
Definition: fl.h:428
Definition: exp.h:135
TPt< TExp > PExp
Definition: exp.h:186
static const TFSet MulOpSymSet
Definition: exp.h:197
TLxSym Sym
Definition: lx.h:149
static double GetExpDevStep(const int &Seed, const int &Steps)
Definition: dt.h:68
Definition: dt.h:1289
Definition: exp.h:178
Definition: lx.h:126
Definition: exp.h:180
static const TFSet FactExpExpect
Definition: exp.h:198
TStr GetTokStr(const bool &XmlP=true) const
Definition: xml.h:316
TExpHelpItemV ArgItemV
Definition: exp.h:314
Definition: exp.h:115
TExp(const TExpType &_ExpType=etUndef)
Definition: exp.h:213
static PExp LoadTxtFact(TILx &Lx, const TFSet &Expect)
Definition: exp.cpp:652
Definition: exp.h:112
TBool IsParen
Definition: exp.h:190
static PExp LoadTxt(const PSIn &SIn, bool &Ok, TStr &MsgStr, const TFSet &Expect=TFSet()|syEof)
Definition: exp.cpp:808
Definition: lx.h:50
TExpFuncArgType
Definition: exp.h:66
Definition: exp.h:179
TStr GetArgExpStr(const int &ArgExpN) const
Definition: exp.cpp:894
static PExp LoadTxtRelExp(TILx &Lx, const TFSet &Expect)
Definition: exp.cpp:720
static const int Mn
Definition: dt.h:1045
static PExp LoadTxtMulExp(TILx &Lx, const TFSet &Expect)
Definition: exp.cpp:693
void AssertArgs(const int &RqArgs, const int &ActArgs)
Definition: exp.cpp:255
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:953
Definition: lx.h:45
static TExpHelpObjType GetObjTypeFromStr(const TStr &TypeStr)
Definition: exp.cpp:1074
Definition: exp.h:5
PExpVal Val_E
Definition: exp.h:144
PExpVal EvalExp(const PExpEnv &ExpEnv, const bool &DbgP, TChA &DbgChA)
Definition: exp.cpp:517
static PExpVal ZeroExpVal
Definition: exp.h:12
Definition: exp.h:112
static TStr GetExpOpStr(const TExpOp &ExpOp)
Definition: exp.h:266
static void GetBiDescV(TStrPrV &BiDescV)
Definition: exp.cpp:967
Definition: exp.h:135
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1218
TExpBiArgType GetExpBiArgType(const TExpBiId &ExpBiId)
Definition: exp.cpp:250
static void Throw(const TStr &MsgStr)
Definition: ut.h:187
void GetObjHdNmV(const TStr &CatNm, TStrV &ObjHdNmV) const
Definition: exp.cpp:1135
Definition: exp.h:73
Definition: exp.h:124
Definition: lx.h:47
bool operator<(const TExpVal &ExpVal) const
Definition: exp.cpp:31
static double GetNrmDevStep(const int &Seed, const int &Steps)
Definition: dt.h:66
Definition: lx.h:49
static const TFSet MulExpExpect
Definition: exp.h:198
Definition: exp.h:307
Definition: lx.h:51
TStr Str
Definition: exp.h:15
static PExp LoadTxtAddExp(TILx &Lx, const TFSet &Expect)
Definition: exp.cpp:704
static PSIn New(const TStr &Str)
Definition: dt.h:709
Definition: lx.h:129
Definition: exp.h:68
Definition: exp.h:170
bool operator==(const TExpVal &ExpVal) const
Definition: exp.cpp:6
static PExpHelp New()
Definition: exp.h:356
Definition: exp.h:180
Definition: exp.h:171
TStr GetStrVal() const
Definition: exp.h:53
Definition: exp.h:178
Definition: lx.h:251
TPt< TExpEnv > PExpEnv
Definition: exp.h:73
Definition: exp.h:115
Definition: exp.h:5
PExpVal Val_False
Definition: exp.h:144
Definition: exp.h:140
TExpValType GetValType() const
Definition: exp.h:45
TStrIntH ExpBiNmToIdH
Definition: exp.h:142
static const TFSet UAddOpSymSet
Definition: exp.h:197
Definition: exp.h:179
Definition: exp.h:124
Definition: exp.h:69
TExpBiId
Definition: exp.h:110
Definition: lx.h:50
TExpValType
Definition: exp.h:5
Definition: exp.h:180
Definition: exp.h:178
void PutFlt(const TFlt &Flt, const int &Width=-1, const int &Prec=-1)
Definition: lx.h:280
Definition: lx.h:49
static double Pi
Definition: xmath.h:8
double GetFltVal() const
Definition: exp.h:50
Definition: exp.h:121
Definition: lx.h:45
PExpVal GetBiFuncVal(const TExpBiId &ExpBiId, const TExpValV &ArgValV, const PExpEnv &ExpEnv)
Definition: exp.cpp:279
TChA Str
Definition: lx.h:150
bool IsExpBiId(const TStr &ExpBiNm, TExpBiId &ExpBiId)
Definition: exp.cpp:241
Definition: exp.h:177
Definition: exp.h:112
TStr GetTopObjNm() const
Definition: exp.cpp:872
static double GetUniDevStep(const int &Seed, const int &Steps)
Definition: dt.h:64
Definition: dt.h:1041
static PExpVal New(const TExpValType &ValType=evtUndef)
Definition: exp.h:23
Definition: fl.h:417
PExpHelpItem HdItem
Definition: exp.h:313
Definition: exp.h:112
Definition: lx.h:47
Definition: dt.h:201
TStr GetNm() const
Definition: exp.h:299
void Ins(const TSizeTy &ValN, const TVal &Val)
Inserts new element Val before the element at position ValN.
Definition: ds.h:1086
void SaveTxtOp(TOLx &Lx) const
Definition: exp.cpp:744
Definition: exp.h:117
void AddToVec(const PExpVal &ExpVal)
Definition: exp.h:40
Definition: exp.h:124
void AssertArgValType(const TExpValType &ExpValType, const PExpVal &ExpVal)
Definition: exp.cpp:261
Definition: exp.h:115
Definition: exp.h:307
TInt ExpType
Definition: exp.h:189
Definition: lx.h:48
Definition: exp.h:178
PLstNd AddBack(const TVal &Val)
Definition: ds.h:3651
Definition: exp.h:307
TInt Type
Definition: exp.h:311
static PExpEnv DfExpEnv
Definition: exp.h:105
Definition: exp.h:307
PXmlTok GetTagTok(const TStr &TagPath) const
Definition: xml.cpp:1142
static const TFSet RelExpExpect
Definition: exp.h:199
Definition: exp.h:69
Definition: lx.h:50
TInt ExpOp
Definition: exp.h:193
Definition: dt.h:412
Definition: exp.h:135
bool Empty() const
Definition: dt.h:488
PExpVal Val_Undef
Definition: exp.h:144
Definition: exp.h:120
PExpVal ExpVal
Definition: exp.h:191
Definition: lx.h:249
void GetTagTokV(const TStr &TagPath, TXmlTokV &XmlTokV) const
Definition: xml.cpp:1158
PExpVal EvalExpOp(const PExpEnv &ExpEnv, const bool &DbgP, TChA &DbgChA)
Definition: exp.cpp:377
static TStr LoadTxt(const PSIn &SIn)
Definition: dt.h:667
void AddToLst(const PExpVal &ExpVal)
Definition: exp.h:42
Definition: lx.h:46
static PXmlDoc LoadTxt(TXmlLx &Lx)
Definition: xml.cpp:1401
Definition: exp.h:68
Definition: exp.h:170
static TStr GetExpTypeStr(const TExpType &ExpType)
Definition: exp.cpp:952
Definition: exp.h:171
PExpVal Val_True
Definition: exp.h:144
static PExp LoadTxtExp(TILx &Lx, const TFSet &Expect)
Definition: exp.cpp:731
Definition: exp.h:120
static bool IsFuncOk(const TStr &RqFuncNm, const TExpFuncArgType &RqFuncArgType, const TStr &FuncNm, const TExpValV &ArgValV)
Definition: exp.cpp:137
Definition: lx.h:47
static PExpHelpItem New(const TStr &Nm, const TStr &TypeStr, const TStr &DescStr, const TStr &DfValStr)
Definition: exp.h:284
Definition: lx.h:46
TExpValL ValL
Definition: exp.h:17
static const TFSet RelOpSymSet
Definition: exp.h:197
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
Definition: exp.h:177
Definition: exp.h:171
TVal & GetVal()
Definition: ds.h:3572
TAscFlt Flt
Definition: exp.h:14
static PExpVal MkClone(const PExpVal &ExpVal)
Definition: exp.cpp:106
static const TFSet AddOpSymSet
Definition: exp.h:197
Definition: lx.h:248
TLxSym GetSym(const TFSet &Expect)
Definition: lx.cpp:315
static TExpBi ExpBi
Definition: exp.h:188
Definition: exp.h:117
Definition: exp.h:124
Definition: exp.h:116
TStr GetTypeStr() const
Definition: exp.h:300
static PExpVal LoadAndEvalExpL(const TStr &ExpLStr, bool &Ok, TStr &MsgStr, const PExpEnv &ExpEnv=TExpEnv::DfExpEnv)
Definition: exp.cpp:918
bool IsKey(const TKey &Key) const
Definition: hash.h:216
Definition: lx.h:46
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
TExpType
Definition: exp.h:168
TStr ExpNm
Definition: exp.h:192
int GetFltValAsInt(const bool &ThrowExceptP=true) const
Definition: exp.cpp:62
TExpHelpObjV ObjV
Definition: exp.h:353
Definition: exp.h:178
TExpHelpObjType
Definition: exp.h:307
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
Definition: lx.h:48
Definition: lx.h:50
TExpV ArgExpV
Definition: exp.h:195
double Flt
Definition: lx.h:151
static double E
Definition: xmath.h:7
Definition: exp.h:112
TIntIntH ExpBiIdToArgTypeH
Definition: exp.h:143
PLstNd First() const
Definition: ds.h:3602
Definition: exp.h:171
Definition: lx.h:126
Definition: exp.h:124
Definition: exp.h:68
static PExpVal UndefExpVal
Definition: exp.h:11
TInt ValType
Definition: exp.h:13
PExpVal Val_Pi
Definition: exp.h:144
void PutIdStr(const TStr &Str, const bool &CheckIdStr=true)
Definition: lx.h:286
TLstNd * Next() const
Definition: ds.h:3571
TExpValV ValV
Definition: exp.h:16
bool In(const int &FlagN) const
Definition: bits.h:156