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