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 { Invalid, UM, MM, CM, Inch, Mil, Degrees, SI };
98
99public:
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 void SetDefaultUnits( EDA_UNITS aUnits );
109
110 void LocaleChanged();
111
112 /* Used by the lemon parser */
113 void parseError(const char* s);
114 void parseOk();
115 void parseSetResult(double);
116
117 /* Check if previous invocation of process() was successful */
118 inline bool IsValid() const { return !m_parseError; }
119
120 /* Result of string processing. Undefined if !isValid() */
121 inline wxString Result() const { return wxString::FromUTF8( m_token.token ); }
122
123 /* Evaluate input string.
124 * Result can be retrieved by result().
125 * Returns true if input string could be evaluated, otherwise false.
126 */
127 bool Process( const wxString& aString );
128
129 /* Retrieve the original text before evaluation. */
130 wxString OriginalText() const;
131
132 /* Add/set variable with value */
133 void SetVar( const wxString& aString, double aValue );
134
135 /* Get value of variable. Returns 0.0 if not defined. */
136 double GetVar( const wxString& aString );
137
138 /* Remove single variable */
139 void RemoveVar( const wxString& aString ) { m_varMap.erase( aString ); }
140
141 /* Remove all variables */
142 void ClearVar() { m_varMap.clear(); }
143
144protected:
145 /* Token type used by the tokenizer */
146 struct Token
147 {
148 int token;
150 };
151
152 /* Begin processing of a new input string */
153 void newString( const wxString& aString );
154
155 /* Tokenizer: Next token/value taken from input string. */
156 Token getToken();
157
158 /* Used by processing loop */
159 void parse( int token, numEval::TokenType value );
160
161private:
162 void* m_parser; // the current lemon parser state machine
163
164 /* Token state for input string. */
166 {
168 input( nullptr ),
169 token( nullptr ),
170 inputLen( 0 ),
171 outputLen( 0 ),
172 pos( 0 )
173 {};
174
175 const char* input; // current input string ("var=4")
176 char* token; // output token ("var", type:VAR; "4", type:VALUE)
177 size_t inputLen; // strlen(input)
178 size_t outputLen; // At least 64, up to input length
179 size_t pos; // current index
180 } m_token;
181
183
184 /* Parse progress. Set by parser actions. */
187
188 Unit m_defaultUnits; // Default unit for values
189
191
192 std::map<wxString, double> m_varMap;
193};
194
195
196#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:46
#define KICOMMON_API
Definition: kicommon.h:28
numEval::TokenType value