1 #ifndef CALCPARSER_H
2 #define CALCPARSER_H
3 
4 #include <QObject>
5 #include <locale.h>
6 #include "consts.h"
7 #include "def.h"
8 #include "token.h"
9 #include "variable.h"
10 #include "cvalue.h"
11 #include "function.h"
12 
13 
14 class CalcParser
15 {
16 public:
17     CalcParser(String *pexpr = nullptr);
18     ~CalcParser();
19     double GetResult();
20     String GetResultStr();
21     bool HasErr();
22     const String listErrors();
23     bool Run(void);
24     void SetParams(String *pexpr, int scale = 10, Drg DRG_mode = Drg::Rad);
25     String GetExpression(String eq = "", bool html = false);
26     bool AddToken(String *pexpr);
27     bool AddPrefixOp(String op);
28     bool AddPrefixInverse();
29     Token* LastToken();
30     String LastTokenValue();
31     void ToBack(bool lastdigit = true);
32     e_type_var TypeRes();
33     void SetScale(int scale);
34     int Scale();
35     void SetDRG(Drg drg_mode);
36     Drg DRG();
37     String DoubleToString(double n, int precision = 200);
38     const TokenList& RefTokens(void);
39     void SetVariable(String name, String value, e_type_var type = e_type_var::FLOAT, bool readonly = false);
40     String GetStrValueVariable(String varname);
41 
42 
43 private:
44     bool Calculate(CValue *loc_result);
45     void GetToken();
46     bool Space(Char c);
47     bool strchr(String t, Char c);
48     void Error(errors c_err, Token *current_token = nullptr);
49     bool isdigit(Char c);
50     bool isalpha(Char c);
51     void Add_exp(CValue *res);
52     void Mul_exp(CValue *res);
53     void Step_exp(CValue *res);
54     void Sign_exp(CValue *res);
55     void Scob_exp(CValue *res);
56     void GetNumber(CValue *res);
57     double ScaleToVal(String s, int scale = 10);
58     void ReadVariableToken(Token *loc_token);
59     void LoadTokens();
60     Token* LoadToken();
61     bool InitVariableFromExpression();
62     void InitExpr(String *pexpr);
63     void EraseErrors();
64     Variable VariableByIterator(VarList::const_iterator vit);
65     int CheckParentheses();
66     TokenList::iterator FindParentheses();
67     void InitMapToksHtml();
68     bool CheckNumberZero(double n);
GetCheckedNumberZero(double n)69     double GetCheckedNumberZero(double n) {if(CheckNumberZero(n))return 0; else return n;}
70     double Dms(double arg, bool invert);
71     bool InvExpInNumber(Token* tok);
72     double RoundS(double arg, int precision);
73     void InitFuncs();
74 
75     CValue *result = new CValue(e_type_var::FLOAT);
76     String expr;
77     Token *token;
78     Token token_end;
79     String::iterator exp_p;
80     errors err;
81     int scale;
82     Drg DRG_mode;
83     lconv *lc;
84     String dec_point = "";
85     bool binit_var;
86     String eq = "";
87 
88     TokenList Tokens;
89     TokenList::iterator i_toks;
90 
91     VarList Vars;
92     MapStrings ToksHtml;
93 
94     std::map<QString, Function*> map_funcs;
95 
96     double PI = static_cast<double>(acos(-1));
97 
98 };
99 
100 #endif // CALCPARSER_H
101