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
/*
23
An evaluator object is used to replace an input string that represents
24
a mathematical expression by its result.
25
26
Example: Consider the input "3+4". The result of this expression is "7".
27
The NumericEvaluator can be used like this:
28
29
NumericEvaluator eval;
30
eval.process("3+4");
31
printf("3+4", eval.result());
32
33
The same example with error checking. Please note that even a valid input string may result
34
in 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
41
Using variables
42
Expressions can refer to variables if they were defined by previous expressions.
43
A variable can be defined by an expression or by the setVar() method.
44
Expressions 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
51
Input string storage
52
An evaluator object can store and retrieve the original input string using a pointer
53
as 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
58
Unit conversion
59
The evaluator uses a default unit and constants can be specified with a unit.
60
As long as no units are used the default unit is not relevant.
61
Supported 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
79
namespace
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
95
class
KICOMMON_API
NUMERIC_EVALUATOR
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
114
public
:
115
NUMERIC_EVALUATOR
(
EDA_UNITS
aUnits );
116
~NUMERIC_EVALUATOR
();
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
159
static
bool
IsOldSchoolDecimalSeparator( wxUniChar ch,
double
* siScaler );
160
static
bool
isOldSchoolDecimalSeparator(
char
ch,
double
* siScaler );
161
static
bool
IsDecimalSeparator(
char
ch,
char
localeSeparator,
bool
allowInfixNotation );
162
static
bool
IsDigit(
char
ch );
163
164
protected
:
165
/* Token type used by the tokenizer */
166
struct
Token
167
{
168
int
token
;
169
numEval::TokenType
value
;
170
};
171
172
/* Begin processing of a new input string */
173
void
newString
(
const
wxString& aString );
174
175
/* Tokenizer: Next token/value taken from input string. */
176
Token
getToken
();
177
178
/* Used by processing loop */
179
void
parse
(
int
token,
numEval::TokenType
value );
180
181
private
:
182
void
*
m_parser
;
// the current lemon parser state machine
183
184
/* Token state for input string. */
185
struct
TokenStat
186
{
187
TokenStat
() :
188
input
( nullptr ),
189
token
( nullptr ),
190
inputLen
( 0 ),
191
outputLen
( 0 ),
192
pos
( 0 )
193
{};
194
195
const
char
*
input
;
// current input string ("var=4")
196
char
*
token
;
// output token ("var", type:VAR; "4", type:VALUE)
197
size_t
inputLen
;
// strlen(input)
198
size_t
outputLen
;
// At least 64, up to input length
199
size_t
pos
;
// current index
200
} m_token;
201
202
char
m_localeDecimalSeparator
;
203
204
/* Parse progress. Set by parser actions. */
205
bool
m_parseError
;
206
bool
m_parseFinished
;
207
208
Unit
m_defaultUnits
;
// Default unit for values
209
210
wxString
m_originalText
;
211
212
std::map<wxString, double>
m_varMap
;
213
};
214
215
216
#endif
/* NUMERIC_EVALUATOR_H_ */
NUMERIC_EVALUATOR
Definition
numeric_evaluator.h:96
NUMERIC_EVALUATOR::parse
void parse(int token, numEval::TokenType value)
NUMERIC_EVALUATOR::Unit
Unit
Definition
numeric_evaluator.h:98
NUMERIC_EVALUATOR::Unit::Mil
@ Mil
Definition
numeric_evaluator.h:104
NUMERIC_EVALUATOR::Unit::UM
@ UM
Definition
numeric_evaluator.h:100
NUMERIC_EVALUATOR::Unit::Invalid
@ Invalid
Definition
numeric_evaluator.h:99
NUMERIC_EVALUATOR::Unit::PsPerInch
@ PsPerInch
Definition
numeric_evaluator.h:109
NUMERIC_EVALUATOR::Unit::CM
@ CM
Definition
numeric_evaluator.h:102
NUMERIC_EVALUATOR::Unit::Picoseconds
@ Picoseconds
Definition
numeric_evaluator.h:108
NUMERIC_EVALUATOR::Unit::Degrees
@ Degrees
Definition
numeric_evaluator.h:105
NUMERIC_EVALUATOR::Unit::MM
@ MM
Definition
numeric_evaluator.h:101
NUMERIC_EVALUATOR::Unit::PsPerMm
@ PsPerMm
Definition
numeric_evaluator.h:111
NUMERIC_EVALUATOR::Unit::Femtoseconds
@ Femtoseconds
Definition
numeric_evaluator.h:107
NUMERIC_EVALUATOR::Unit::Inch
@ Inch
Definition
numeric_evaluator.h:103
NUMERIC_EVALUATOR::Unit::SI
@ SI
Definition
numeric_evaluator.h:106
NUMERIC_EVALUATOR::Unit::PsPerCm
@ PsPerCm
Definition
numeric_evaluator.h:110
NUMERIC_EVALUATOR::m_token
struct NUMERIC_EVALUATOR::TokenStat m_token
NUMERIC_EVALUATOR::newString
void newString(const wxString &aString)
Definition
numeric_evaluator.cpp:179
NUMERIC_EVALUATOR::m_localeDecimalSeparator
char m_localeDecimalSeparator
Definition
numeric_evaluator.h:202
NUMERIC_EVALUATOR::m_parseError
bool m_parseError
Definition
numeric_evaluator.h:205
NUMERIC_EVALUATOR::m_originalText
wxString m_originalText
Definition
numeric_evaluator.h:210
NUMERIC_EVALUATOR::Result
wxString Result() const
Definition
numeric_evaluator.h:136
NUMERIC_EVALUATOR::m_parser
void * m_parser
Definition
numeric_evaluator.h:182
NUMERIC_EVALUATOR::NUMERIC_EVALUATOR
NUMERIC_EVALUATOR(EDA_UNITS aUnits)
Definition
numeric_evaluator.cpp:47
NUMERIC_EVALUATOR::m_parseFinished
bool m_parseFinished
Definition
numeric_evaluator.h:206
NUMERIC_EVALUATOR::RemoveVar
void RemoveVar(const wxString &aString)
Definition
numeric_evaluator.h:154
NUMERIC_EVALUATOR::getToken
Token getToken()
Definition
numeric_evaluator.cpp:259
NUMERIC_EVALUATOR::IsValid
bool IsValid() const
Definition
numeric_evaluator.h:133
NUMERIC_EVALUATOR::ClearVar
void ClearVar()
Definition
numeric_evaluator.h:157
NUMERIC_EVALUATOR::m_varMap
std::map< wxString, double > m_varMap
Definition
numeric_evaluator.h:212
NUMERIC_EVALUATOR::m_defaultUnits
Unit m_defaultUnits
Definition
numeric_evaluator.h:208
eda_units.h
EDA_UNITS
EDA_UNITS
Definition
eda_units.h:48
KICOMMON_API
#define KICOMMON_API
Definition
kicommon.h:28
numEval
Definition
numeric_evaluator.cpp:29
NUMERIC_EVALUATOR::TokenStat::inputLen
size_t inputLen
Definition
numeric_evaluator.h:197
NUMERIC_EVALUATOR::TokenStat::input
const char * input
Definition
numeric_evaluator.h:195
NUMERIC_EVALUATOR::TokenStat::outputLen
size_t outputLen
Definition
numeric_evaluator.h:198
NUMERIC_EVALUATOR::TokenStat::token
char * token
Definition
numeric_evaluator.h:196
NUMERIC_EVALUATOR::TokenStat::pos
size_t pos
Definition
numeric_evaluator.h:199
NUMERIC_EVALUATOR::TokenStat::TokenStat
TokenStat()
Definition
numeric_evaluator.h:187
NUMERIC_EVALUATOR::Token
Definition
numeric_evaluator.h:167
NUMERIC_EVALUATOR::Token::value
numEval::TokenType value
Definition
numeric_evaluator.h:169
NUMERIC_EVALUATOR::Token::token
int token
Definition
numeric_evaluator.h:168
numEval::TokenType
Definition
numeric_evaluator.h:82
numEval::TokenType::valid
bool valid
Definition
numeric_evaluator.h:89
numEval::TokenType::dValue
double dValue
Definition
numeric_evaluator.h:85
numEval::TokenType::iValue
int iValue
Definition
numeric_evaluator.h:86
numEval::TokenType::text
char text[32]
Definition
numeric_evaluator.h:90
src
include
libeval
numeric_evaluator.h
Generated on Sat Dec 13 2025 00:06:22 for KiCad PCB EDA Suite by
1.13.2