KiCad PCB EDA Suite
pcb_expr_evaluator.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 
25 #ifndef __PCB_EXPR_EVALUATOR_H
26 #define __PCB_EXPR_EVALUATOR_H
27 
28 #include <unordered_map>
29 
30 #include <property.h>
31 #include <property_mgr.h>
32 
34 
35 class BOARD;
36 class BOARD_ITEM;
37 
38 class PCB_EXPR_VAR_REF;
39 
40 class PCB_EXPR_UCODE final : public LIBEVAL::UCODE
41 {
42 public:
44  virtual ~PCB_EXPR_UCODE() {};
45 
46  virtual std::unique_ptr<LIBEVAL::VAR_REF> CreateVarRef( const wxString& aVar,
47  const wxString& aField ) override;
48  virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
49 };
50 
51 
53 {
54 public:
56  m_layer( aLayer )
57  {
58  m_items[0] = nullptr;
59  m_items[1] = nullptr;
60  }
61 
62  void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
63  {
64  m_items[0] = a;
65  m_items[1] = b;
66  }
67 
68  BOARD* GetBoard() const;
69 
70  BOARD_ITEM* GetItem( int index ) const
71  {
72  return m_items[index];
73  }
74 
76  {
77  return m_layer;
78  }
79 
80 private:
83 };
84 
85 
87 {
88 public:
89  PCB_EXPR_VAR_REF( int aItemIndex ) :
90  m_itemIndex( aItemIndex ),
92  m_isEnum( false )
93  {
94  //printf("*** CreateVarRef %p %d\n", this, aItemIndex );
95  }
96 
98 
99  void SetIsEnum( bool s ) { m_isEnum = s; }
100  bool IsEnum() const { return m_isEnum; }
101 
102  void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
103  LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
104 
105  void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
106  {
107  m_matchingTypes[type_hash] = prop;
108  }
109 
110  virtual LIBEVAL::VALUE GetValue( LIBEVAL::CONTEXT* aCtx ) override;
111 
112  BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
113 
114 private:
115  std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
118  bool m_isEnum;
119 };
120 
121 
122 // "Object code" version of a netclass reference (for performance).
124 {
125 public:
126  PCB_EXPR_NETCLASS_REF( int aItemIndex ) :
127  PCB_EXPR_VAR_REF( aItemIndex )
128  {
130  //printf("*** CreateVarRef %p %d\n", this, aItemIndex );
131  }
132 
133  LIBEVAL::VALUE GetValue( LIBEVAL::CONTEXT* aCtx ) override;
134 };
135 
136 
137 // "Object code" version of a netname reference (for performance).
139 {
140 public:
141  PCB_EXPR_NETNAME_REF( int aItemIndex ) :
142  PCB_EXPR_VAR_REF( aItemIndex )
143  {
145  //printf("*** CreateVarRef %p %d\n", this, aItemIndex );
146  }
147 
148  LIBEVAL::VALUE GetValue( LIBEVAL::CONTEXT* aCtx ) override;
149 };
150 
151 
153 {
154 public:
155  PCB_EXPR_TYPE_REF( int aItemIndex ) :
156  PCB_EXPR_VAR_REF( aItemIndex )
157  {
159  //printf("*** CreateVarRef %p %d\n", this, aItemIndex );
160  }
161 
162  LIBEVAL::VALUE GetValue( LIBEVAL::CONTEXT* aCtx ) override;
163 };
164 
165 
167 {
168 public:
170 
172  {
173  static PCB_EXPR_BUILTIN_FUNCTIONS self;
174  return self;
175  }
176 
177  LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
178  {
179  return m_funcs[ name ];
180  }
181 
182  const wxArrayString GetSignatures() const
183  {
184  return m_funcSigs;
185  }
186 
187  void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr )
188  {
189  wxString funcName = funcSignature.BeforeFirst( '(' );
190  m_funcs[std::string( funcName.Lower() )] = std::move( funcPtr );
191  m_funcSigs.Add( funcSignature );
192  }
193 
194  void RegisterAllFunctions();
195 
196 private:
197  std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
198 
199  wxArrayString m_funcSigs;
200 };
201 
202 
204 {
205 public:
207 };
208 
209 
211 {
212 public:
215 
216  bool Evaluate( const wxString& aExpr );
217  int Result() const { return m_result; }
218 
219  void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
220  {
221  m_compiler.SetErrorCallback( aCallback );
222  }
223 
224  bool IsErrorPending() const { return m_errorStatus.pendingError; }
225  const LIBEVAL::ERROR_STATUS& GetError() const { return m_errorStatus; }
226 
227 private:
228  int m_result;
229 
233 };
234 
235 #endif
bool IsErrorPending() const
BOARD_ITEM * GetItem(int index) const
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
const wxArrayString GetSignatures() const
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
const LIBEVAL::ERROR_STATUS & GetError() const
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall(const wxString &aName) override
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:42
void RegisterFunc(const wxString &funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr)
PCB_EXPR_UCODE m_ucode
LIBEVAL::FUNC_CALL_REF Get(const wxString &name)
BOARD * GetBoard() const
void SetIsEnum(bool s)
std::map< wxString, LIBEVAL::FUNC_CALL_REF > m_funcs
PCB_EXPR_NETNAME_REF(int aItemIndex)
PCB_EXPR_COMPILER m_compiler
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
LIBEVAL::VAR_TYPE_T m_type
virtual std::unique_ptr< LIBEVAL::VAR_REF > CreateVarRef(const wxString &aVar, const wxString &aField) override
void SetType(LIBEVAL::VAR_TYPE_T type)
PCB_EXPR_CONTEXT(PCB_LAYER_ID aLayer=UNDEFINED_LAYER)
BOARD_ITEM * m_items[2]
PCB_EXPR_NETCLASS_REF(int aItemIndex)
PCB_LAYER_ID m_layer
PCB_EXPR_VAR_REF(int aItemIndex)
LIBEVAL::ERROR_STATUS m_errorStatus
LIBEVAL::VALUE GetValue(LIBEVAL::CONTEXT *aCtx) override
virtual LIBEVAL::VALUE GetValue(LIBEVAL::CONTEXT *aCtx) override
void AddAllowedClass(TYPE_ID type_hash, PROPERTY_BASE *prop)
const char * name
Definition: DXF_plotter.cpp:56
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:64
bool Evaluate(const wxString &aExpr)
virtual ~PCB_EXPR_UCODE()
LIBEVAL::VALUE GetValue(LIBEVAL::CONTEXT *aCtx) override
PCB_EXPR_TYPE_REF(int aItemIndex)
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
BOARD_ITEM * GetObject(const LIBEVAL::CONTEXT *aCtx) const
PCB_LAYER_ID GetLayer() const
std::unordered_map< TYPE_ID, PROPERTY_BASE * > m_matchingTypes
static PCB_EXPR_BUILTIN_FUNCTIONS & Instance()
LIBEVAL::VAR_TYPE_T GetType() const override
LIBEVAL::VALUE GetValue(LIBEVAL::CONTEXT *aCtx) override