2020-07-03 16:36:33 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-04-02 14:08:32 +00:00
|
|
|
* Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-07-03 16:36:33 +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 2
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
#ifndef PCBEXPR_EVALUATOR_H
|
|
|
|
#define PCBEXPR_EVALUATOR_H
|
2020-06-13 23:22:08 +00:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2022-11-03 02:50:49 +00:00
|
|
|
#include <properties/property.h>
|
|
|
|
#include <properties/property_mgr.h>
|
2020-06-13 23:22:08 +00:00
|
|
|
|
|
|
|
#include <libeval_compiler/libeval_compiler.h>
|
|
|
|
|
2021-08-16 09:53:27 +00:00
|
|
|
class BOARD;
|
2020-06-13 23:22:08 +00:00
|
|
|
class BOARD_ITEM;
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_VAR_REF;
|
2020-06-13 23:22:08 +00:00
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_UCODE final : public LIBEVAL::UCODE
|
2020-06-13 23:22:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_UCODE() {};
|
|
|
|
virtual ~PCBEXPR_UCODE() {};
|
2020-07-24 14:17:54 +00:00
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
virtual std::unique_ptr<LIBEVAL::VAR_REF> CreateVarRef( const wxString& aVar,
|
|
|
|
const wxString& aField ) override;
|
2020-08-11 13:33:16 +00:00
|
|
|
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
|
2020-07-23 20:47:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_CONTEXT : public LIBEVAL::CONTEXT
|
2020-07-23 20:47:38 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_CONTEXT( int aConstraint, PCB_LAYER_ID aLayer ) :
|
2022-04-02 14:08:32 +00:00
|
|
|
m_constraint( aConstraint ),
|
2020-08-07 20:18:33 +00:00
|
|
|
m_layer( aLayer )
|
2020-07-23 20:47:38 +00:00
|
|
|
{
|
|
|
|
m_items[0] = nullptr;
|
|
|
|
m_items[1] = nullptr;
|
|
|
|
}
|
2020-06-16 19:49:09 +00:00
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
|
|
|
|
{
|
|
|
|
m_items[0] = a;
|
|
|
|
m_items[1] = b;
|
|
|
|
}
|
|
|
|
|
2021-08-16 09:53:27 +00:00
|
|
|
BOARD* GetBoard() const;
|
|
|
|
|
2022-04-02 14:08:32 +00:00
|
|
|
int GetConstraint() const { return m_constraint; }
|
|
|
|
BOARD_ITEM* GetItem( int index ) const { return m_items[index]; }
|
|
|
|
PCB_LAYER_ID GetLayer() const { return m_layer; }
|
2020-08-07 20:18:33 +00:00
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
private:
|
2022-04-02 14:08:32 +00:00
|
|
|
int m_constraint;
|
2020-08-13 17:47:41 +00:00
|
|
|
BOARD_ITEM* m_items[2];
|
2020-08-07 20:18:33 +00:00
|
|
|
PCB_LAYER_ID m_layer;
|
2020-06-13 23:22:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_VAR_REF : public LIBEVAL::VAR_REF
|
2020-06-13 23:22:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_VAR_REF( int aItemIndex ) :
|
|
|
|
m_itemIndex( aItemIndex ),
|
|
|
|
m_type( LIBEVAL::VT_UNDEFINED ),
|
|
|
|
m_isEnum( false )
|
2023-04-20 22:19:04 +00:00
|
|
|
{}
|
2020-06-13 23:22:08 +00:00
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
~PCBEXPR_VAR_REF() {};
|
2020-08-11 13:33:16 +00:00
|
|
|
|
2020-06-16 19:49:09 +00:00
|
|
|
void SetIsEnum( bool s ) { m_isEnum = s; }
|
|
|
|
bool IsEnum() const { return m_isEnum; }
|
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
|
2020-10-27 11:03:35 +00:00
|
|
|
LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
|
2020-06-13 23:22:08 +00:00
|
|
|
|
|
|
|
void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
|
|
|
|
{
|
|
|
|
m_matchingTypes[type_hash] = prop;
|
|
|
|
}
|
|
|
|
|
2022-09-16 13:13:34 +00:00
|
|
|
LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
|
2020-06-13 23:22:08 +00:00
|
|
|
|
2020-10-27 11:03:35 +00:00
|
|
|
BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
|
2020-06-16 19:49:09 +00:00
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
private:
|
|
|
|
std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
|
|
|
|
int m_itemIndex;
|
|
|
|
LIBEVAL::VAR_TYPE_T m_type;
|
2020-07-19 21:22:49 +00:00
|
|
|
bool m_isEnum;
|
2020-06-13 23:22:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-11 22:08:07 +00:00
|
|
|
// "Object code" version of a netclass reference (for performance).
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_NETCLASS_REF : public PCBEXPR_VAR_REF
|
2021-01-11 22:08:07 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_NETCLASS_REF( int aItemIndex ) :
|
|
|
|
PCBEXPR_VAR_REF( aItemIndex )
|
2021-01-11 22:08:07 +00:00
|
|
|
{
|
|
|
|
SetType( LIBEVAL::VT_STRING );
|
|
|
|
}
|
|
|
|
|
2022-09-16 13:13:34 +00:00
|
|
|
LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
|
2021-01-11 22:08:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// "Object code" version of a netname reference (for performance).
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_NETNAME_REF : public PCBEXPR_VAR_REF
|
2021-01-11 22:08:07 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_NETNAME_REF( int aItemIndex ) :
|
|
|
|
PCBEXPR_VAR_REF( aItemIndex )
|
2021-01-11 22:08:07 +00:00
|
|
|
{
|
|
|
|
SetType( LIBEVAL::VT_STRING );
|
|
|
|
}
|
|
|
|
|
2022-09-16 13:13:34 +00:00
|
|
|
LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
|
2021-01-11 22:08:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_TYPE_REF : public PCBEXPR_VAR_REF
|
2021-08-16 09:53:27 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_TYPE_REF( int aItemIndex ) :
|
|
|
|
PCBEXPR_VAR_REF( aItemIndex )
|
2021-08-16 09:53:27 +00:00
|
|
|
{
|
|
|
|
SetType( LIBEVAL::VT_STRING );
|
|
|
|
}
|
|
|
|
|
2022-09-16 13:13:34 +00:00
|
|
|
LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
|
2021-08-16 09:53:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_BUILTIN_FUNCTIONS
|
2020-07-22 20:27:38 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_BUILTIN_FUNCTIONS();
|
2020-07-22 20:27:38 +00:00
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
static PCBEXPR_BUILTIN_FUNCTIONS& Instance()
|
2020-07-22 20:27:38 +00:00
|
|
|
{
|
2023-08-21 14:26:03 +00:00
|
|
|
static PCBEXPR_BUILTIN_FUNCTIONS self;
|
2020-07-22 20:27:38 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
|
2020-07-22 20:27:38 +00:00
|
|
|
{
|
2020-08-11 22:16:45 +00:00
|
|
|
return m_funcs[ name ];
|
2020-07-22 20:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const wxArrayString GetSignatures() const
|
|
|
|
{
|
|
|
|
return m_funcSigs;
|
|
|
|
}
|
|
|
|
|
2020-09-23 21:50:06 +00:00
|
|
|
void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr )
|
|
|
|
{
|
|
|
|
wxString funcName = funcSignature.BeforeFirst( '(' );
|
|
|
|
m_funcs[std::string( funcName.Lower() )] = std::move( funcPtr );
|
|
|
|
m_funcSigs.Add( funcSignature );
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegisterAllFunctions();
|
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
private:
|
2020-08-11 22:16:45 +00:00
|
|
|
std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
|
2020-07-22 20:27:38 +00:00
|
|
|
|
|
|
|
wxArrayString m_funcSigs;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_UNIT_RESOLVER : public LIBEVAL::UNIT_RESOLVER
|
2022-11-29 14:18:44 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
const std::vector<wxString>& GetSupportedUnits() const override;
|
|
|
|
|
|
|
|
wxString GetSupportedUnitsMessage() const override;
|
|
|
|
|
|
|
|
double Convert( const wxString& aString, int unitId ) const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_UNITLESS_RESOLVER : public LIBEVAL::UNIT_RESOLVER
|
2022-11-29 14:18:44 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
const std::vector<wxString>& GetSupportedUnits() const override;
|
|
|
|
|
|
|
|
double Convert( const wxString& aString, int unitId ) const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_COMPILER : public LIBEVAL::COMPILER
|
2020-06-13 23:22:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_COMPILER( LIBEVAL::UNIT_RESOLVER* aUnitResolver );
|
2020-06-13 23:22:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
class PCBEXPR_EVALUATOR
|
2020-06-13 23:22:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_EVALUATOR( LIBEVAL::UNIT_RESOLVER* aUnitResolver );
|
|
|
|
~PCBEXPR_EVALUATOR();
|
2020-06-13 23:22:08 +00:00
|
|
|
|
|
|
|
bool Evaluate( const wxString& aExpr );
|
2020-07-19 21:22:49 +00:00
|
|
|
int Result() const { return m_result; }
|
2020-08-13 17:47:41 +00:00
|
|
|
|
|
|
|
void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
|
|
|
|
{
|
|
|
|
m_compiler.SetErrorCallback( aCallback );
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:33:16 +00:00
|
|
|
bool IsErrorPending() const { return m_errorStatus.pendingError; }
|
|
|
|
const LIBEVAL::ERROR_STATUS& GetError() const { return m_errorStatus; }
|
2020-07-21 15:49:55 +00:00
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
private:
|
|
|
|
int m_result;
|
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
PCBEXPR_COMPILER m_compiler;
|
|
|
|
PCBEXPR_UCODE m_ucode;
|
2020-08-11 13:33:16 +00:00
|
|
|
LIBEVAL::ERROR_STATUS m_errorStatus;
|
2020-06-13 23:22:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|