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-2020 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 
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, const wxString& aField ) override;
47  virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
48 };
49 
50 
52 {
53 public:
55  m_layer( aLayer )
56  {
57  m_items[0] = nullptr;
58  m_items[1] = nullptr;
59  }
60 
61  void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
62  {
63  m_items[0] = a;
64  m_items[1] = b;
65  }
66 
67  BOARD_ITEM* GetItem( int index ) const
68  {
69  return m_items[index];
70  }
71 
73  {
74  return m_layer;
75  }
76 
77 private:
80 };
81 
82 
84 {
85 public:
86  PCB_EXPR_VAR_REF( int aItemIndex ) :
87  m_itemIndex( aItemIndex ),
89  m_isEnum( false )
90  {
91  //printf("*** CreateVarRef %p %d\n", this, aItemIndex );
92  }
93 
95 
96  void SetIsEnum( bool s ) { m_isEnum = s; }
97  bool IsEnum() const { return m_isEnum; }
98 
99  void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
100  LIBEVAL::VAR_TYPE_T GetType() override { return m_type; }
101 
102  void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
103  {
104  m_matchingTypes[type_hash] = prop;
105  }
106 
107  virtual LIBEVAL::VALUE GetValue( LIBEVAL::CONTEXT* aCtx ) override;
108 
109  BOARD_ITEM* GetObject( LIBEVAL::CONTEXT* aCtx ) const;
110 
111 private:
112  std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
115  bool m_isEnum;
116 };
117 
118 
120 {
121 public:
123 
125  {
126  static PCB_EXPR_BUILTIN_FUNCTIONS self;
127  return self;
128  }
129 
130  LIBEVAL::FUNC_CALL_REF Get( const wxString &name )
131  {
132  return m_funcs[ name ];
133  }
134 
135  const wxArrayString GetSignatures() const
136  {
137  return m_funcSigs;
138  }
139 
140 private:
141  std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
142 
143  wxArrayString m_funcSigs;
144 };
145 
146 
148 {
149 public:
151 };
152 
153 
155 {
156 public:
159 
160  bool Evaluate( const wxString& aExpr );
161  int Result() const { return m_result; }
162 
163  void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
164  {
165  m_compiler.SetErrorCallback( aCallback );
166  }
167 
168  bool IsErrorPending() const { return m_errorStatus.pendingError; }
169  const LIBEVAL::ERROR_STATUS& GetError() const { return m_errorStatus; }
170 
171 private:
172  int m_result;
173 
177 };
178 
179 #endif
bool IsErrorPending() const
BOARD_ITEM * GetItem(int index) const
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
const wxArrayString GetSignatures() const
BOARD_ITEM * GetObject(LIBEVAL::CONTEXT *aCtx) const
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
const LIBEVAL::ERROR_STATUS & GetError() const
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall(const wxString &aName) override
size_t TYPE_ID
Unique type identifier
Definition: property_mgr.h:38
PCB_EXPR_UCODE m_ucode
LIBEVAL::FUNC_CALL_REF Get(const wxString &name)
void SetIsEnum(bool s)
std::map< wxString, LIBEVAL::FUNC_CALL_REF > m_funcs
PCB_LAYER_ID
A quick note on layer IDs:
PCB_EXPR_COMPILER m_compiler
LIBEVAL::VAR_TYPE_T GetType() override
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_LAYER_ID m_layer
PCB_EXPR_VAR_REF(int aItemIndex)
LIBEVAL::ERROR_STATUS m_errorStatus
virtual LIBEVAL::VALUE GetValue(LIBEVAL::CONTEXT *aCtx) override
void AddAllowedClass(TYPE_ID type_hash, PROPERTY_BASE *prop)
const char * name
Definition: DXF_plotter.cpp:60
bool Evaluate(const wxString &aExpr)
virtual ~PCB_EXPR_UCODE()
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
PCB_LAYER_ID GetLayer() const
std::unordered_map< TYPE_ID, PROPERTY_BASE * > m_matchingTypes
static PCB_EXPR_BUILTIN_FUNCTIONS & Instance()