KiCad PCB EDA Suite
numeric_evaluator.h
Go to the documentation of this file.
1 /*
2  This file is part of libeval, a simple math expression evaluator
3 
4  Copyright (C) 2017 Michael Geselbracht, mgeselbracht3@gmail.com
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19 
20 /*
21 An evaluator object is used to replace an input string that represents
22 a mathematical expression by its result.
23 
24 Example: Consider the input "3+4". The result of this expression is "7".
25 The NumericEvaluator can be used like this:
26 
27  NumericEvaluator eval;
28  eval.process("3+4");
29  printf("3+4", eval.result());
30 
31 The same example with error checking. Please note that even a valid input string may result
32 in an empty output string or "NaN".
33 
34  NumericEvaluator eval;
35  bool ret = eval.process("3+4");
36  assert(ret == eval.isValid()); // isValid() reflects return value of process().
37  if (eval.isValid()) printf("3+4=%s\n", eval.result());
38 
39 Using variables
40 Expressions can refer to variables if they were defined by previous expressions.
41 A variable can be defined by an expression or by the setVar() method.
42 Expressions that define/set variables do not have a result.
43 
44  eval.process("x=1; y=2"); // Result is NaN
45  eval.setVar("z", 3);
46  eval.process("x+y+z");
47  printf("x+y+z=%s\n", eval.result());
48 
49 Input string storage
50 An evaluator object can store and retrieve the original input string using a pointer
51 as key. This can be used to restore the input string of a text entry field.
52 
53  eval.process("25.4-0.7", &eval);
54  printf("%s = %s\n", eval.textInput(&eval), eval.result());
55 
56 Unit conversion
57 The evaluator uses a default unit and constants can be specified with a unit.
58 As long as no units are used the default unit is not relevant.
59 Supported units are millimeters (mm), Mil (mil) and inch (")
60 
61  eval.process("1\"");
62  printf("1\" = %s\n", eval.result());
63  eval.process("12.7 - 0.1\" - 50mil");
64  printf("12.7 - 0.1\" - 50mil = %s\n", eval.result());
65 */
66 
67 #ifndef NUMERIC_EVALUATOR_H_
68 #define NUMERIC_EVALUATOR_H_
69 
70 #include <stddef.h>
71 #include <string>
72 #include <string>
73 #include <map>
74 
75 #include <base_units.h>
76 
77 // This namespace is used for the lemon parser
78 namespace numEval
79 {
80 
81  struct TokenType
82  {
83  union
84  {
85  double dValue;
86  int iValue;
87  };
88 
89  bool valid;
90  char text[32];
91  };
92 
93 } // namespace numEval
94 
96 {
97  enum class Unit { Invalid, Metric, Inch, Mil };
98 
99 public:
100  NUMERIC_EVALUATOR( EDA_UNITS_T aUnits, bool aUseMils = false );
102 
103  /* clear() should be invoked by the client if a new input string is to be processed. It
104  * will reset the parser. User defined variables are retained.
105  */
106  void Clear();
107 
108  /* Used by the lemon parser */
109  void parseError(const char* s);
110  void parseOk();
111  void parseSetResult(double);
112 
113  /* Check if previous invokation of process() was successful */
114  inline bool IsValid() const { return !m_parseError; }
115 
116  /* Result of string processing. Undefined if !isValid() */
117  inline wxString Result() const { return wxString::FromUTF8( m_token.token ); }
118 
119  /* Evaluate input string.
120  * Result can be retrieved by result().
121  * Returns true if input string could be evaluated, otherwise false.
122  */
123  bool Process( const wxString& aString );
124 
125  /* Retrieve the original text before evaluation. */
126  wxString OriginalText() const;
127 
128  /* Add/set variable with value */
129  void SetVar( const wxString& aString, double aValue );
130 
131  /* Get value of variable. Returns 0.0 if not defined. */
132  double GetVar( const wxString& aString );
133 
134  /* Remove single variable */
135  void RemoveVar( const wxString& aString ) { m_varMap.erase( aString ); }
136 
137  /* Remove all variables */
138  void ClearVar() { m_varMap.clear(); }
139 
140 protected:
141  /* Token type used by the tokenizer */
142  struct Token
143  {
144  int token;
146  };
147 
148  /* Begin processing of a new input string */
149  void newString( const wxString& aString );
150 
151  /* Tokenizer: Next token/value taken from input string. */
152  Token getToken();
153 
154  /* Used by processing loop */
155  void parse( int token, numEval::TokenType value );
156 
157 private:
158  void* m_parser; // the current lemon parser state machine
159 
160  /* Token state for input string. */
161  struct TokenStat
162  {
163  enum { OutLen = 32 };
164  TokenStat() : input( 0 ), token( 0 ), inputLen( 0 ), pos( 0 ) { /* empty */ }
165  const char* input; // current input string ("var=4")
166  char* token; // output token ("var", type:VAR; "4", type:VALUE)
167  size_t inputLen; // strlen(input)
168  size_t pos; // current index
169  }
170  m_token;
171 
173 
174  /* Parse progress. Set by parser actions. */
177 
178  Unit m_defaultUnits; // Default unit for values
179 
180  wxString m_originalText;
181 
182  std::map<wxString, double> m_varMap;
183 };
184 
185 
186 #endif /* NUMERIC_EVALUATOR_H_ */
std::map< wxString, double > m_varMap
wxString Result() const
Implementation of conversion functions that require both schematic and board internal units...
numEval::TokenType value
void RemoveVar(const wxString &aString)
bool IsValid() const
EDA_UNITS_T
Definition: common.h:159