2017-11-23 10:36:24 +00:00
|
|
|
/*
|
|
|
|
This file is part of libeval, a simple math expression evaluator
|
|
|
|
|
|
|
|
Copyright (C) 2017 Michael Geselbracht, mgeselbracht3@gmail.com
|
|
|
|
|
|
|
|
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.
|
2018-06-17 13:50:49 +00:00
|
|
|
As long as no units are used the default unit is not relevant.
|
2017-11-23 10:36:24 +00:00
|
|
|
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_
|
|
|
|
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cstddef>
|
2017-11-23 10:36:24 +00:00
|
|
|
#include <map>
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <string>
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2022-09-17 01:07:36 +00:00
|
|
|
#include <eda_units.h>
|
2018-02-03 09:09:53 +00:00
|
|
|
|
2017-11-23 10:36:24 +00:00
|
|
|
// This namespace is used for the lemon parser
|
|
|
|
namespace numEval
|
|
|
|
{
|
2018-02-25 16:14:24 +00:00
|
|
|
struct TokenType
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
double dValue;
|
|
|
|
int iValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool valid;
|
|
|
|
char text[32];
|
2018-06-04 17:48:17 +00:00
|
|
|
};
|
|
|
|
|
2017-11-23 10:36:24 +00:00
|
|
|
} // namespace numEval
|
|
|
|
|
2018-02-25 16:14:24 +00:00
|
|
|
class NUMERIC_EVALUATOR
|
|
|
|
{
|
2022-12-05 02:07:45 +00:00
|
|
|
enum class Unit { Invalid, MM, CM, Inch, Mil, Degrees, SI };
|
2017-11-23 10:36:24 +00:00
|
|
|
|
|
|
|
public:
|
2020-10-02 22:46:12 +00:00
|
|
|
NUMERIC_EVALUATOR( EDA_UNITS aUnits );
|
2018-02-25 16:14:24 +00:00
|
|
|
~NUMERIC_EVALUATOR();
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-02-03 09:09:53 +00:00
|
|
|
/* 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.
|
2018-06-04 17:48:17 +00:00
|
|
|
*/
|
2018-02-25 16:14:24 +00:00
|
|
|
void Clear();
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2022-10-11 17:01:47 +00:00
|
|
|
void SetDefaultUnits( EDA_UNITS aUnits );
|
|
|
|
|
2022-12-06 22:56:25 +00:00
|
|
|
void LocaleChanged();
|
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Used by the lemon parser */
|
|
|
|
void parseError(const char* s);
|
|
|
|
void parseOk();
|
|
|
|
void parseSetResult(double);
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
/* Check if previous invocation of process() was successful */
|
2018-02-25 16:14:24 +00:00
|
|
|
inline bool IsValid() const { return !m_parseError; }
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Result of string processing. Undefined if !isValid() */
|
2018-02-25 16:14:24 +00:00
|
|
|
inline wxString Result() const { return wxString::FromUTF8( m_token.token ); }
|
2018-02-03 09:09:53 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Evaluate input string.
|
|
|
|
* Result can be retrieved by result().
|
|
|
|
* Returns true if input string could be evaluated, otherwise false.
|
|
|
|
*/
|
2018-02-25 16:14:24 +00:00
|
|
|
bool Process( const wxString& aString );
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-02-25 16:14:24 +00:00
|
|
|
/* Retrieve the original text before evaluation. */
|
|
|
|
wxString OriginalText() const;
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Add/set variable with value */
|
2018-02-25 16:14:24 +00:00
|
|
|
void SetVar( const wxString& aString, double aValue );
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Get value of variable. Returns 0.0 if not defined. */
|
2018-02-25 16:14:24 +00:00
|
|
|
double GetVar( const wxString& aString );
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Remove single variable */
|
2018-02-25 16:14:24 +00:00
|
|
|
void RemoveVar( const wxString& aString ) { m_varMap.erase( aString ); }
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Remove all variables */
|
2018-02-25 16:14:24 +00:00
|
|
|
void ClearVar() { m_varMap.clear(); }
|
2017-11-23 10:36:24 +00:00
|
|
|
|
|
|
|
protected:
|
2018-02-25 16:14:24 +00:00
|
|
|
/* Token type used by the tokenizer */
|
|
|
|
struct Token
|
|
|
|
{
|
2018-06-04 17:48:17 +00:00
|
|
|
int token;
|
|
|
|
numEval::TokenType value;
|
|
|
|
};
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Begin processing of a new input string */
|
2018-02-25 16:14:24 +00:00
|
|
|
void newString( const wxString& aString );
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Tokenizer: Next token/value taken from input string. */
|
|
|
|
Token getToken();
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Used by processing loop */
|
2018-02-25 16:14:24 +00:00
|
|
|
void parse( int token, numEval::TokenType value );
|
2018-02-15 11:38:33 +00:00
|
|
|
|
2017-11-23 10:36:24 +00:00
|
|
|
private:
|
2018-02-25 16:14:24 +00:00
|
|
|
void* m_parser; // the current lemon parser state machine
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Token state for input string. */
|
2018-02-25 16:14:24 +00:00
|
|
|
struct TokenStat
|
|
|
|
{
|
2022-10-01 18:29:34 +00:00
|
|
|
TokenStat() :
|
2022-12-05 02:07:45 +00:00
|
|
|
input( nullptr ),
|
|
|
|
token( nullptr ),
|
|
|
|
inputLen( 0 ),
|
|
|
|
outputLen( 0 ),
|
|
|
|
pos( 0 )
|
|
|
|
{};
|
2022-10-01 18:29:34 +00:00
|
|
|
|
2018-02-25 16:14:24 +00:00
|
|
|
const char* input; // current input string ("var=4")
|
|
|
|
char* token; // output token ("var", type:VAR; "4", type:VALUE)
|
|
|
|
size_t inputLen; // strlen(input)
|
2022-10-01 18:29:34 +00:00
|
|
|
size_t outputLen; // At least 64, up to input length
|
2018-02-25 16:14:24 +00:00
|
|
|
size_t pos; // current index
|
2022-12-05 02:07:45 +00:00
|
|
|
} m_token;
|
2018-02-25 16:14:24 +00:00
|
|
|
|
|
|
|
char m_localeDecimalSeparator;
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-06-04 17:48:17 +00:00
|
|
|
/* Parse progress. Set by parser actions. */
|
2018-02-25 16:14:24 +00:00
|
|
|
bool m_parseError;
|
|
|
|
bool m_parseFinished;
|
2018-02-03 09:09:53 +00:00
|
|
|
|
2018-02-25 16:14:24 +00:00
|
|
|
Unit m_defaultUnits; // Default unit for values
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-02-25 16:14:24 +00:00
|
|
|
wxString m_originalText;
|
2017-11-23 10:36:24 +00:00
|
|
|
|
2018-02-25 16:14:24 +00:00
|
|
|
std::map<wxString, double> m_varMap;
|
2017-11-23 10:36:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-07-07 18:59:26 +00:00
|
|
|
#endif /* NUMERIC_EVALUATOR_H_ */
|