kicad/include/libeval_compiler/libeval_compiler.h

485 lines
9.7 KiB
C
Raw Normal View History

2020-06-04 11:04:03 +00:00
/*
This file is part of libeval, a simple math expression evaluator
Copyright (C) 2007 Michael Geselbracht, mgeselbracht3@gmail.com
Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
2020-06-04 11:04:03 +00:00
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/>.
*/
#ifndef __LIBEVAL_COMPILER_H
#define __LIBEVAL_COMPILER_H
#include <cstddef>
#include <functional>
#include <map>
2020-06-04 11:04:03 +00:00
#include <string>
#include <stack>
2020-06-04 11:04:03 +00:00
#include <base_units.h>
#define TR_OP_BINARY_MASK 0x200
#define TR_OP_UNARY_MASK 0x100
#define TR_OP_MUL 0x201
#define TR_OP_DIV 0x202
#define TR_OP_ADD 0x203
#define TR_OP_SUB 0x204
#define TR_OP_LESS 0x25
#define TR_OP_GREATER 0x206
#define TR_OP_LESS_EQUAL 0x207
#define TR_OP_GREATER_EQUAL 0x208
#define TR_OP_EQUAL 0x209
#define TR_OP_NOT_EQUAL 0x20a
#define TR_OP_BOOL_AND 0x20b
#define TR_OP_BOOL_OR 0x20c
#define TR_OP_BOOL_NOT 0x100
#define TR_OP_FUNC_CALL 24
#define TR_OP_METHOD_CALL 25
2020-06-04 11:04:03 +00:00
#define TR_UOP_PUSH_VAR 1
#define TR_UOP_PUSH_VALUE 2
// This namespace is used for the lemon parser
namespace LIBEVAL
{
class COMPILER;
2020-06-04 11:04:03 +00:00
enum VAR_TYPE_T
{
VT_STRING = 1,
VT_NUMERIC,
VT_UNDEFINED,
VT_PARSE_ERROR
2020-06-04 11:04:03 +00:00
};
enum TOKEN_TYPE_T
{
TR_NUMBER = 1,
2020-06-04 11:04:03 +00:00
TR_IDENTIFIER = 2,
TR_ASSIGN = 3,
TR_STRUCT_REF = 4,
TR_STRING = 5,
TR_UNIT = 6
2020-06-04 11:04:03 +00:00
};
#define LIBEVAL_MAX_LITERAL_LENGTH 1024
class UOP;
2020-06-04 11:04:03 +00:00
struct TREE_NODE
{
struct value_s
{
2020-07-18 10:40:56 +00:00
char str[LIBEVAL_MAX_LITERAL_LENGTH];
int type;
2020-06-04 11:04:03 +00:00
} value;
2020-07-18 10:40:56 +00:00
int op;
TREE_NODE* leaf[2];
UOP* uop;
bool valid;
bool isTerminal;
int srcPos;
2020-06-04 11:04:03 +00:00
};
2020-07-18 10:40:56 +00:00
2020-06-04 11:04:03 +00:00
static inline TREE_NODE* copyNode( TREE_NODE& t )
{
auto t2 = new TREE_NODE();
t2->valid = t.valid;
2020-07-18 10:40:56 +00:00
snprintf( t2->value.str, LIBEVAL_MAX_LITERAL_LENGTH, "%s", t.value.str );
t2->op = t.op;
t2->value.type = t.value.type;
t2->leaf[0] = t.leaf[0];
t2->leaf[1] = t.leaf[1];
t2->isTerminal = false;
t2->srcPos = t.srcPos;
t2->uop = nullptr;
2020-06-04 11:04:03 +00:00
return t2;
}
2020-07-18 10:40:56 +00:00
static inline TREE_NODE* newNode( int op, int type, const std::string& value )
2020-06-04 11:04:03 +00:00
{
auto t2 = new TREE_NODE();
t2->valid = true;
2020-07-18 10:40:56 +00:00
snprintf( t2->value.str, LIBEVAL_MAX_LITERAL_LENGTH, "%s", value.c_str() );
t2->op = op;
t2->value.type = type;
t2->leaf[0] = nullptr;
t2->leaf[1] = nullptr;
t2->isTerminal = false;
t2->srcPos = -1;
t2->uop = nullptr;
2020-06-04 11:04:03 +00:00
return t2;
}
class UNIT_RESOLVER
{
public:
UNIT_RESOLVER()
{
}
2020-06-04 11:04:03 +00:00
virtual ~UNIT_RESOLVER()
{
}
2020-06-04 11:04:03 +00:00
virtual const std::vector<std::string>& GetSupportedUnits() const
{
static const std::vector<std::string> nullUnits;
2020-06-04 11:04:03 +00:00
return nullUnits;
}
2020-06-04 11:04:03 +00:00
virtual double Convert( const std::string& aString, int unitType ) const
{
return 0.0;
};
2020-06-04 11:04:03 +00:00
};
class VALUE
{
2020-06-04 11:04:03 +00:00
public:
VALUE():
m_type(VT_UNDEFINED),
m_valueDbl( 0 )
{};
2020-06-04 11:04:03 +00:00
VALUE( const wxString& aStr ) :
2020-06-04 11:04:03 +00:00
m_type( VT_STRING ),
m_valueDbl( 0 ),
m_valueStr( aStr )
{};
2020-06-04 11:04:03 +00:00
VALUE( const double aVal ) :
m_type( VT_NUMERIC ),
m_valueDbl( aVal )
{};
2020-06-04 11:04:03 +00:00
double AsDouble() const
{
return m_valueDbl;
}
2020-06-04 11:04:03 +00:00
const wxString& AsString() const
{
return m_valueStr;
}
bool operator==( const VALUE& b ) const
{
if( m_type == VT_NUMERIC && b.m_type == VT_NUMERIC )
return m_valueDbl == b.m_valueDbl;
else if( m_type == VT_STRING && b.m_type == VT_STRING )
return m_valueStr == b.m_valueStr;
return false;
}
VAR_TYPE_T GetType() const { return m_type; };
void Set( double aValue )
{
m_type = VT_NUMERIC;
m_valueDbl = aValue;
}
void Set( const wxString& aValue )
{
m_type = VT_STRING;
m_valueStr = aValue;
}
void Set( const VALUE &val )
{
m_type = val.m_type;
m_valueDbl = val.m_valueDbl;
if( m_type == VT_STRING )
m_valueStr = val.m_valueStr;
}
bool EqualTo( const VALUE* v2 ) const
{
return operator==( *v2 );
}
2020-06-04 11:04:03 +00:00
private:
VAR_TYPE_T m_type;
double m_valueDbl;
wxString m_valueStr;
};
2020-06-04 11:04:03 +00:00
class UCODE;
class CONTEXT;
class VAR_REF
{
public:
virtual VAR_TYPE_T GetType() = 0;
virtual VALUE GetValue( CONTEXT* aCtx ) = 0;
};
class CONTEXT
2020-06-04 11:04:03 +00:00
{
public:
2020-08-12 15:26:10 +00:00
virtual ~CONTEXT()
2020-06-04 11:04:03 +00:00
{
for( VALUE* value : m_ownedValues )
delete value;
}
VALUE* AllocValue()
{
VALUE* value = new VALUE();
m_ownedValues.push_back( value );
return value;
}
void Push( VALUE* v )
{
m_stack.push( v );
}
VALUE* Pop()
{
VALUE* value = m_stack.top();
m_stack.pop();
return value;
}
2020-06-04 11:04:03 +00:00
int SP() const
{
return m_stack.size();
}
void ReportError( const wxString& aErrorMsg ) { m_errorMessage = aErrorMsg; }
const wxString& GetError() const { return m_errorMessage; }
private:
std::vector<VALUE*> m_ownedValues;
std::stack<VALUE*> m_stack;
wxString m_errorMessage;
};
class UCODE
{
public:
2020-07-29 21:11:30 +00:00
virtual ~UCODE();
typedef std::function<void( CONTEXT*, void* )> FUNC_PTR;
2020-06-04 11:04:03 +00:00
void AddOp( UOP* uop )
2020-06-04 11:04:03 +00:00
{
m_ucode.push_back(uop);
2020-06-04 11:04:03 +00:00
}
VALUE* Run( CONTEXT* ctx );
2020-06-04 11:04:03 +00:00
std::string Dump() const;
virtual VAR_REF* CreateVarRef( const char* var, const char* field )
2020-06-04 11:04:03 +00:00
{
return nullptr;
2020-06-04 11:04:03 +00:00
};
virtual FUNC_PTR CreateFuncCall( const char* name )
{
return nullptr;
};
2020-06-04 11:04:03 +00:00
private:
std::vector<UOP*> m_ucode;
};
class UOP
{
public:
UOP( int op, void* arg ) :
m_op( op ),
m_arg( arg )
{};
UOP( int op, UCODE::FUNC_PTR func, void *arg ) :
m_op( op ),
m_arg(arg),
m_func( std::move( func ) )
{};
void Exec( CONTEXT* ctx );
std::string Format() const;
private:
int m_op;
void* m_arg;
UCODE::FUNC_PTR m_func;
};
2020-06-04 11:04:03 +00:00
class TOKENIZER
{
public:
void Restart( const std::string& aStr )
{
m_str = aStr;
m_pos = 0;
}
void Clear()
{
m_str = "";
m_pos = 0;
}
int GetChar() const
{
if( m_pos >= m_str.length() )
return 0;
2020-06-04 11:04:03 +00:00
return m_str[m_pos];
}
bool Done() const
{
return m_pos >= m_str.length();
}
void NextChar( int aAdvance = 1 )
{
m_pos += aAdvance;
}
size_t GetPos() const
{
return m_pos;
}
std::string GetChars( std::function<bool( int )> cond ) const;
bool MatchAhead( const std::string& match, std::function<bool( int )> stopCond ) const;
2020-06-04 11:04:03 +00:00
private:
std::string m_str;
size_t m_pos;
};
class COMPILER
{
public:
COMPILER( REPORTER* aReporter, int aSourceLine, int aSourceOffset );
2020-06-04 11:04:03 +00:00
virtual ~COMPILER();
/*
* clear() should be invoked by the client if a new input string is to be processed. It
2020-06-04 11:04:03 +00:00
* will reset the parser. User defined variables are retained.
*/
void Clear();
/* Used by the lemon parser */
void parseError( const char* s );
void parseOk();
int GetSourcePos() const { return m_sourcePos; }
void setRoot( LIBEVAL::TREE_NODE root );
void freeTree( LIBEVAL::TREE_NODE *tree );
bool Compile( const std::string& aString, UCODE* aCode, CONTEXT* aPreflightContext );
2020-06-04 11:04:03 +00:00
protected:
enum LEXER_STATE
{
LS_DEFAULT = 0,
LS_STRING = 1,
};
LEXER_STATE m_lexerState;
bool generateUCode( UCODE* aCode, CONTEXT* aPreflightContext );
2020-06-04 11:04:03 +00:00
void reportError( const wxString& aErrorMsg, int aPos = -1 );
2020-06-04 11:04:03 +00:00
/* Token type used by the tokenizer */
struct T_TOKEN
{
int token;
TREE_NODE value;
};
/* Begin processing of a new input string */
void newString( const std::string& aString );
/* Tokenizer: Next token/value taken from input string. */
T_TOKEN getToken();
bool lexDefault( T_TOKEN& aToken );
bool lexString( T_TOKEN& aToken );
int resolveUnits();
UOP* makeUop( int op, double value )
{
auto uop = new UOP( op, new VALUE( value ) );
return uop;
}
UOP* makeUop( int op, const wxString& value )
{
UOP* uop = new UOP( op, new VALUE( value ) );
return uop;
}
UOP* makeUop( int op, VAR_REF* aRef = nullptr )
{
UOP* uop = new UOP( op, aRef );
return uop;
}
UOP* makeUop( int op, UCODE::FUNC_PTR aFunc, void *arg = nullptr )
{
UOP* uop = new UOP( op, std::move( aFunc ), arg );
return uop;
}
protected:
2020-06-04 11:04:03 +00:00
/* Token state for input string. */
void* m_parser; // the current lemon parser state machine
TOKENIZER m_tokenizer;
char m_localeDecimalSeparator;
2020-06-04 11:04:03 +00:00
std::unique_ptr<UNIT_RESOLVER> m_unitResolver;
int m_sourcePos;
bool m_parseFinished;
2020-06-04 11:04:03 +00:00
REPORTER* m_reporter;
int m_originLine; // Location in the file of the start of the expression
int m_originOffset;
TREE_NODE* m_tree;
2020-06-04 11:04:03 +00:00
};
} // namespace LIBEVAL
#endif /* LIBEVAL_COMPILER_H_ */