File: numeric_evaluator.h

package info (click to toggle)
kicad 9.0.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 771,448 kB
  • sloc: cpp: 969,355; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 228; javascript: 167; perl: 10
file content (196 lines) | stat: -rw-r--r-- 5,863 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * This file is part of KiCad, a free EDA CAD application.
 * Derived from libeval, a simple math expression evaluator.
 *
 * Copyright (C) 2017 Michael Geselbracht, mgeselbracht3@gmail.com
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/*
An evaluator object is used to replace an input string that represents
a mathematical expression by its result.

Example: Consider the input "3+4". The result of this expression is "7".
The NumericEvaluator can be used like this:

  NumericEvaluator eval;
  eval.process("3+4");
  printf("3+4", eval.result());

The same example with error checking. Please note that even a valid input string may result
in an empty output string or "NaN".

  NumericEvaluator eval;
  bool ret = eval.process("3+4");
  assert(ret == eval.isValid()); // isValid() reflects return value of process().
  if (eval.isValid()) printf("3+4=%s\n", eval.result());

Using variables
Expressions can refer to variables if they were defined by previous expressions.
A variable can be defined by an expression or by the setVar() method.
Expressions that define/set variables do not have a result.

  eval.process("x=1; y=2"); // Result is NaN
  eval.setVar("z", 3);
  eval.process("x+y+z");
  printf("x+y+z=%s\n", eval.result());

Input string storage
An evaluator object can store and retrieve the original input string using a pointer
as key. This can be used to restore the input string of a text entry field.

  eval.process("25.4-0.7", &eval);
  printf("%s = %s\n", eval.textInput(&eval), eval.result());

Unit conversion
The evaluator uses a default unit and constants can be specified with a unit.
As long as no units are used the default unit is not relevant.
Supported units are millimeters (mm), Mil (mil) and inch (")

  eval.process("1\"");
  printf("1\" = %s\n", eval.result());
  eval.process("12.7 - 0.1\" - 50mil");
  printf("12.7 - 0.1\" - 50mil = %s\n", eval.result());
*/

#ifndef NUMERIC_EVALUATOR_H_
#define NUMERIC_EVALUATOR_H_

#include <cstddef>
#include <map>
#include <string>

#include <eda_units.h>

// This namespace is used for the lemon parser
namespace numEval
{
    struct TokenType
    {
        union
        {
            double dValue;
            int    iValue;
        };

        bool valid;
        char text[32];
    };

} // namespace numEval

class KICOMMON_API NUMERIC_EVALUATOR
{
    enum class Unit { Invalid, UM, MM, CM, Inch, Mil, Degrees, SI };

public:
    NUMERIC_EVALUATOR( EDA_UNITS aUnits );
    ~NUMERIC_EVALUATOR();

    /* clear() should be invoked by the client if a new input string is to be processed. It
     * will reset the parser. User defined variables are retained.
     */
    void Clear();

    void SetDefaultUnits( EDA_UNITS aUnits );

    void LocaleChanged();

    /* Used by the lemon parser */
    void parseError(const char* s);
    void parseOk();
    void parseSetResult(double);

    /* Check if previous invocation of process() was successful */
    inline bool IsValid() const { return !m_parseError; }

    /* Result of string processing. Undefined if !isValid() */
    inline wxString Result() const { return wxString::FromUTF8( m_token.token ); }

    /* Evaluate input string.
     * Result can be retrieved by result().
     * Returns true if input string could be evaluated, otherwise false.
     */
    bool Process( const wxString& aString );

    /* Retrieve the original text before evaluation. */
    wxString OriginalText() const;

    /* Add/set variable with value */
    void SetVar( const wxString& aString, double aValue );

    /* Get value of variable. Returns 0.0 if not defined. */
    double GetVar( const wxString& aString );

    /* Remove single variable */
    void RemoveVar( const wxString& aString ) { m_varMap.erase( aString ); }

    /* Remove all variables */
    void ClearVar() { m_varMap.clear(); }

protected:
    /* Token type used by the tokenizer */
    struct Token
    {
        int token;
        numEval::TokenType value;
    };

    /* Begin processing of a new input string */
    void newString( const wxString& aString );

    /* Tokenizer: Next token/value taken from input string. */
    Token getToken();

    /* Used by processing loop */
    void parse( int token, numEval::TokenType value );

private:
    void* m_parser; // the current lemon parser state machine

    /* Token state for input string. */
    struct TokenStat
    {
        TokenStat() :
            input( nullptr ),
            token( nullptr ),
            inputLen( 0 ),
            outputLen( 0 ),
            pos( 0 )
        {};

        const char* input;      // current input string ("var=4")
        char*       token;      // output token ("var", type:VAR; "4", type:VALUE)
        size_t      inputLen;   // strlen(input)
        size_t      outputLen;  // At least 64, up to input length
        size_t      pos;        // current index
    }    m_token;

    char m_localeDecimalSeparator;

    /* Parse progress. Set by parser actions. */
    bool m_parseError;
    bool m_parseFinished;

    Unit m_defaultUnits;      // Default unit for values

    wxString m_originalText;

    std::map<wxString, double> m_varMap;
};


#endif /* NUMERIC_EVALUATOR_H_ */