-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp.h
executable file
·374 lines (324 loc) · 13.3 KB
/
exp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/////////////////////////////////////////////////
// Expression-Value-Type
typedef enum {evtUndef, evtFlt, evtStr, evtVec, evtLst} TExpValType;
/////////////////////////////////////////////////
// Expression-Value
ClassTPVL(TExpVal, PExpVal, TExpValV, TExpValL, TExpValLN)//{
private:
static PExpVal UndefExpVal;
static PExpVal ZeroExpVal;
TInt ValType;
TAscFlt Flt;
TStr Str;
TExpValV ValV;
TExpValL ValL;
public:
TExpVal(const TExpValType& _ValType=evtUndef): ValType(_ValType){}
TExpVal(const TFlt& _Flt): ValType(evtFlt), Flt(_Flt){}
TExpVal(const double& _Flt): ValType(evtFlt), Flt(TFlt(_Flt)){}
TExpVal(const TStr& _Str): ValType(evtStr), Str(_Str){}
static PExpVal New(const TExpValType& ValType=evtUndef){
return PExpVal(new TExpVal(ValType));}
static PExpVal New(const TFlt& Flt){return PExpVal(new TExpVal(Flt));}
static PExpVal New(const double& Flt){return PExpVal(new TExpVal(Flt));}
static PExpVal New(const TStr& Str){return PExpVal(new TExpVal(Str));}
~TExpVal(){}
TExpVal(TSIn& SIn):
ValType(SIn), Flt(SIn), Str(SIn), ValV(SIn), ValL(SIn){}
static PExpVal Load(TSIn& SIn){return PExpVal(new TExpVal(SIn));}
void Save(TSOut& SOut) const {
ValType.Save(SOut);
Flt.Save(SOut); Str.Save(SOut); ValV.Save(SOut); ValL.Save(SOut);}
TExpVal& operator=(const TExpVal&){Fail; return *this;}
bool operator==(const TExpVal& ExpVal) const;
bool operator<(const TExpVal& ExpVal) const;
void AddToVec(const PExpVal& ExpVal){
IAssert(TExpValType(static_cast<int>(ValType))==evtVec); ValV.Add(ExpVal);}
void AddToLst(const PExpVal& ExpVal){
IAssert(TExpValType(static_cast<int>(ValType))==evtLst); ValL.AddBack(ExpVal);}
TExpValType GetValType() const {return TExpValType(int(ValType));}
bool IsFltVal(TFlt& _Flt) const {
if (GetValType()==evtFlt){_Flt=Flt; return true;} else {return false;}}
bool IsFltVal(double& _Flt) const {
if (GetValType()==evtFlt){_Flt=Flt; return true;} else {return false;}}
double GetFltVal() const {
if (GetValType()==evtFlt){return Flt;} else {return 0;}}
int GetFltValAsInt(const bool& ThrowExceptP=true) const;
TStr GetStrVal() const {IAssert(GetValType()==evtStr); return Str;}
void SaveTxt(TOLx& Lx) const;
void SaveTxt(const PSOut& SOut) const;
TStr GetStr() const;
static PExpVal MkClone(const PExpVal& ExpVal);
static PExpVal GetUndefExpVal();
static PExpVal GetZeroExpVal();
};
/////////////////////////////////////////////////
// Expression-Environment
typedef enum {
efatUndef, efatVoid,
efatFlt, efatFltFlt, efatFltStr,
efatStr, efatStrFlt, efatStrStr,
efatStrAny
} TExpFuncArgType;
ClassTP(TExpEnv, PExpEnv)//{
private:
TRnd Rnd;
public:
TExpEnv(): Rnd(){}
virtual ~TExpEnv(){}
TExpEnv(TSIn& SIn): Rnd(SIn){}
static PExpEnv Load(TSIn& SIn){return new TExpEnv(SIn);}
virtual void Save(TSOut& SOut) const {Rnd.Save(SOut);}
TExpEnv& operator=(const TExpEnv&){Fail; return *this;}
bool operator==(const TExpEnv&) const {Fail; return false;}
virtual void PutVarVal(const TStr& /*VarNm*/, const PExpVal& /*ExpVal*/){Fail;}
virtual PExpVal GetVarVal(const TStr& /*VarNm*/, bool& IsVar){
IsVar=false; return TExpVal::GetUndefExpVal();}
virtual PExpVal GetVarVal(const TStr& VarNm){
bool IsVar; return GetVarVal(VarNm, IsVar);}
virtual int GetVars() const {return 0;}
virtual void GetVarNmVal(
const int& /*VarN*/, TStr& /*VarNm*/, PExpVal& /*VarVal*/) const {Fail;}
virtual PExpVal GetFuncVal(
const TStr& /*FuncNm*/, const TExpValV& /*ArgValV*/, bool& IsFunc){
IsFunc=false; return TExpVal::GetUndefExpVal();}
virtual TRnd& GetRnd(){return Rnd;}
static bool IsFuncOk(
const TStr& RqFuncNm, const TExpFuncArgType& RqFuncArgType,
const TStr& FuncNm, const TExpValV& ArgValV);
static PExpEnv DfExpEnv;
};
/////////////////////////////////////////////////
// Expression-BuiltIn-Ids
typedef enum {
// constants
ebi_Undef, ebi_True, ebi_False, ebi_E, ebi_Pi,
// trigonometric funcions
ebi_Sin, ebi_Cos, ebi_Tan,
ebi_ASin, ebi_ACos, ebi_ATan,
ebi_SinH, ebi_CosH, ebi_TanH,
// exponential functions
ebi_Pow, ebi_Exp, ebi_Sqr, ebi_Sqrt,
ebi_Log, ebi_Log10,
// number manipulation functions
ebi_Ceil, ebi_Floor, ebi_Int, ebi_Frac, ebi_Abs,
// random deviates
ebi_UniDev, ebi_NrmDev, ebi_ExpDev,
ebi_GamDev, ebi_PoiDev, ebi_BinDev,
ebi_UniDevStep, ebi_NrmDevStep, ebi_ExpDevStep,
} TExpBiId;
/////////////////////////////////////////////////
// Expression-BuiltIn-Argument-Type
typedef enum {
ebatUndef, ebatVoid, ebatFlt, ebatFltFlt
} TExpBiArgType;
/////////////////////////////////////////////////
// Expression-BuiltIn
class TExpBi{
private:
TStrIntH ExpBiNmToIdH;
TIntIntH ExpBiIdToArgTypeH;
PExpVal Val_Undef, Val_True, Val_False, Val_E, Val_Pi;
void AddBi(const TStr& ExpBiNm, const TExpBiId& ExpBiId,
const TExpBiArgType& ExpBiArgType=ebatUndef);
public:
TExpBi();
TExpBi& operator=(const TExpBi&){Fail; return *this;}
bool operator==(const TExpBi&) const {Fail; return false;}
bool IsExpBiId(const TStr& ExpBiNm, TExpBiId& ExpBiId);
bool IsExpBiId(const TStr& ExpBiNm){
TExpBiId ExpBiId; return IsExpBiId(ExpBiNm, ExpBiId);}
TExpBiArgType GetExpBiArgType(const TExpBiId& ExpBiId);
void AssertArgs(const int& RqArgs, const int& ActArgs);
void AssertArgValType(const TExpValType& ExpValType, const PExpVal& ExpVal);
PExpVal GetBiConstVal(const TExpBiId& ExpBiId);
PExpVal GetBiFuncVal(
const TExpBiId& ExpBiId, const TExpValV& ArgValV, const PExpEnv& ExpEnv);
};
/////////////////////////////////////////////////
// Expression-Type
typedef enum {
etUndef,
etVal, etVec, etLst,
etOp, etVar, etBiConst, etFunc, etBiFunc} TExpType;
/////////////////////////////////////////////////
// Expression-Operator
typedef enum {
eoUndef,
eoUPlus, eoUMinus,
eoPlus, eoMinus, eoMul, eoDiv, eoIDiv, eoMod,
eoNot, eoAnd, eoOr,
eoEq, eoNEq, eoLss, eoGtr, eoLEq, eoGEq,
eoIf
} TExpOp;
/////////////////////////////////////////////////
// Expression
ClassTPV(TExp, PExp, TExpV)//{
private:
static TExpBi ExpBi;
TInt ExpType;
TBool IsParen;
PExpVal ExpVal;
TStr ExpNm;
TInt ExpOp;
TInt ExpBiId;
TExpV ArgExpV;
private:
static const TFSet MulOpSymSet, UAddOpSymSet, AddOpSymSet, RelOpSymSet;
static const TFSet FactExpExpect, MulExpExpect, AddExpExpect;
static const TFSet RelExpExpect, ExpExpect;
static TExpOp GetExpOpFromLxSym(const TLxSym& LxSym);
static TLxSym GetLxSymFromExpOp(const TExpOp& ExpOp);
static PExp LoadTxtFact(TILx& Lx, const TFSet& Expect);
static PExp LoadTxtMulExp(TILx& Lx, const TFSet& Expect);
static PExp LoadTxtAddExp(TILx& Lx, const TFSet& Expect);
static PExp LoadTxtRelExp(TILx& Lx, const TFSet& Expect);
static PExp LoadTxtExp(TILx& Lx, const TFSet& Expect);
void SaveTxtOp(TOLx& Lx) const;
PExpVal EvalExpOp(
const PExpEnv& ExpEnv, const bool& DbgP, TChA& DbgChA);
PExpVal EvalExp(
const PExpEnv& ExpEnv, const bool& DbgP, TChA& DbgChA);
public:
TExp(const TExpType& _ExpType=etUndef):
ExpType(_ExpType), IsParen(false),
ExpVal(), ExpNm(), ExpOp(), ExpBiId(), ArgExpV(){}
TExp(const TExpOp& _ExpOp,
const PExp& Exp1, const PExp& Exp2=NULL, const PExp& Exp3=NULL);
TExp(const PExpVal& _ExpVal);
TExp(const TStr& _VarNm);
TExp(const TStr& _FuncNm, const TExpV& _ArgExpV);
~TExp(){}
TExp(TSIn& SIn):
ExpType(SIn), IsParen(SIn),
ExpVal(SIn), ExpNm(SIn), ExpOp(SIn), ExpBiId(SIn), ArgExpV(SIn){}
static PExp Load(TSIn& SIn){return PExp(new TExp(SIn));}
void Save(TSOut& SOut) const {
ExpType.Save(SOut); IsParen.Save(SOut);
ExpVal.Save(SOut); ExpNm.Save(SOut); ExpOp.Save(SOut);
ExpBiId.Save(SOut); ArgExpV.Save(SOut);}
TExp& operator=(const TExp&){Fail; return *this;}
bool operator==(const TExp&) const {Fail; return false;}
static bool IsExpStrOk(const TStr& ExpStr, TStr& MsgStr){
bool Ok; LoadTxt(ExpStr, Ok, MsgStr); return Ok;}
static PExp LoadTxt(const PSIn& SIn, bool& Ok, TStr& MsgStr,
const TFSet& Expect=TFSet()|syEof);
static PExp LoadTxt(const TStr& ExpStr, bool& Ok, TStr& MsgStr){
PSIn SIn=TStrIn::New(ExpStr); return LoadTxt(SIn, Ok, MsgStr);}
static PExp LoadTxt(const TStr& ExpStr){
bool Ok; TStr MsgStr; return LoadTxt(ExpStr, Ok, MsgStr);}
void SaveTxt(TOLx& Lx) const;
void SaveTxt(const PSOut& SOut) const {
TOLx Lx(SOut, TFSet()|oloCmtAlw|oloSigNum|oloCsSens); SaveTxt(Lx);}
TStr GetStr() const;
TStr GetTopObjNm() const;
int GetArgExps() const;
TStr GetArgExpStr(const int& ArgExpN) const;
PExpVal Eval(bool& Ok, TStr& MsgStr, const bool& DbgP, TStr& DbgStr,
const PExpEnv& ExpEnv=TExpEnv::DfExpEnv);
PExpVal Eval(const PExpEnv& ExpEnv=TExpEnv::DfExpEnv){
bool Ok; TStr MsgStr; TStr DbgStr;
return Eval(Ok, MsgStr, false, DbgStr, ExpEnv);}
PExpVal Eval(bool& Ok, TStr& MsgStr, const PExpEnv& ExpEnv=TExpEnv::DfExpEnv){
TStr DbgStr; return Eval(Ok, MsgStr, false, DbgStr, ExpEnv);}
PExpVal Eval(const PExpEnv& ExpEnv, const bool& DbgP, TStr& DbgStr){
bool Ok; TStr MsgStr; return Eval(Ok, MsgStr, DbgP, DbgStr, ExpEnv);}
static PExpVal LoadAndEvalExpL(
const TStr& ExpLStr, bool& Ok, TStr& MsgStr,
const PExpEnv& ExpEnv=TExpEnv::DfExpEnv);
static TStr GetExpTypeStr(const TExpType& ExpType);
static TStr GetExpOpStr(const TExpOp& ExpOp){
return TLxSymStr::GetSymStr(TExp::GetLxSymFromExpOp(ExpOp));}
static void GetBiDescV(TStrPrV& BiDescV);
};
/////////////////////////////////////////////////
// Expression-Help-Item
ClassTPV(TExpHelpItem, PExpHelpItem, TExpHelpItemV)//{
private:
TStr Nm;
TStr TypeStr;
TStr DescStr;
TStr DfValStr;
public:
TExpHelpItem(): Nm(), TypeStr(), DescStr(), DfValStr(){}
TExpHelpItem(const TStr& _Nm, const TStr& _TypeStr,
const TStr& _DescStr, const TStr& _DfValStr):
Nm(_Nm), TypeStr(_TypeStr), DescStr(_DescStr), DfValStr(_DfValStr){}
static PExpHelpItem New(const TStr& Nm, const TStr& TypeStr,
const TStr& DescStr, const TStr& DfValStr){
return PExpHelpItem(new TExpHelpItem(Nm, TypeStr, DescStr, DfValStr));}
~TExpHelpItem(){}
TExpHelpItem(TSIn& SIn):
Nm(SIn), TypeStr(SIn), DescStr(SIn), DfValStr(SIn){}
static PExpHelpItem Load(TSIn& SIn){
return PExpHelpItem(new TExpHelpItem(SIn));}
void Save(TSOut& SOut) const {
Nm.Save(SOut); TypeStr.Save(SOut); DescStr.Save(SOut); DfValStr.Save(SOut);}
TExpHelpItem& operator=(const TExpHelpItem&){Fail; return *this;}
bool operator==(const TExpHelpItem&) const {Fail; return false;}
// component retrieval
TStr GetNm() const {return Nm;}
TStr GetTypeStr() const {return TypeStr;}
TStr GetDescStr() const {return DescStr;}
TStr GetDfValStr() const {return DfValStr;}
};
/////////////////////////////////////////////////
// Expression-Help-Object
typedef enum {ehotUndef, ehotOp, ehotVar, ehotFunc, ehotTempl} TExpHelpObjType;
ClassTPV(TExpHelpObj, PExpHelpObj, TExpHelpObjV)//{
private:
TInt Type; // TExpHelpObjType
TStr CatNm;
PExpHelpItem HdItem;
TExpHelpItemV ArgItemV;
public:
TExpHelpObj(){}
TExpHelpObj(const TExpHelpObjType& _Type, const TStr& _CatNm,
const PExpHelpItem& _HdItem, const TExpHelpItemV& _ArgItemV):
Type(_Type), CatNm(_CatNm), HdItem(_HdItem), ArgItemV(_ArgItemV){}
static PExpHelpObj New(const TExpHelpObjType& Type, const TStr& CatNm,
const PExpHelpItem& HdItem, const TExpHelpItemV& ArgItemV=TExpHelpItemV()){
return PExpHelpObj(new TExpHelpObj(Type, CatNm, HdItem, ArgItemV));}
~TExpHelpObj(){}
TExpHelpObj(TSIn& SIn):
Type(SIn), CatNm(SIn), HdItem(SIn), ArgItemV(SIn){}
static PExpHelpObj Load(TSIn& SIn){
return PExpHelpObj(new TExpHelpObj(SIn));}
void Save(TSOut& SOut) const {
Type.Save(SOut); CatNm.Save(SOut); HdItem.Save(SOut); ArgItemV.Save(SOut);}
TExpHelpObj& operator=(const TExpHelpObj&){Fail; return *this;}
bool operator==(const TExpHelpObj&) const {Fail; return false;}
// component retrieval
TExpHelpObjType GetType() const {return TExpHelpObjType(int(Type));}
TStr GetCatNm() const {return CatNm;}
PExpHelpItem GetHdItem() const {return HdItem;}
int GetArgItems() const {return ArgItemV.Len();}
PExpHelpItem GetArgItem(const int& ArgItemN) const {
return ArgItemV[ArgItemN];}
// strings
TStr GetHdArgNmStr() const;
// object type
static TExpHelpObjType GetObjTypeFromStr(const TStr& TypeStr);
};
/////////////////////////////////////////////////
// Expression-Help
ClassTP(TExpHelp, PExpHelp)//{
private:
TExpHelpObjV ObjV;
public:
TExpHelp(): ObjV(){}
static PExpHelp New(){return PExpHelp(new TExpHelp());}
~TExpHelp(){}
TExpHelp(TSIn& SIn): ObjV(SIn){}
static PExpHelp Load(TSIn& SIn){return PExpHelp(new TExpHelp(SIn));}
void Save(TSOut& SOut) const {ObjV.Save(SOut);}
TExpHelp& operator=(const TExpHelp&){Fail; return *this;}
bool operator==(const TExpHelp&) const {Fail; return false;}
static PExpHelp LoadXml(const PSIn& SIn);
static PExpHelp LoadXml(const TStr& FNm){
PSIn SIn=TFIn::New(FNm); return LoadXml(SIn);}
void AddObj(const PExpHelpObj& Obj){ObjV.Add(Obj);}
void GetCatNmV(TStrV& CatNmV) const;
void GetObjHdNmV(const TStr& CatNm, TStrV& ObjHdNmV) const;
PExpHelpObj GetObj(const TStr& ObjNm) const;
};