KiCad PCB EDA Suite
libeval_compiler_test.cpp
Go to the documentation of this file.
1 #include <wx/wx.h>
2 #include <cstdio>
3 
4 #include "class_board.h"
5 #include "class_track.h"
6 
7 #include <pcb_expr_evaluator.h>
8 
9 #include <io_mgr.h>
10 #include <kicad_plugin.h>
11 
12 #include <unordered_set>
13 
14 #include <profile.h>
15 
16 bool testEvalExpr( const std::string expr, LIBEVAL::VALUE expectedResult, bool expectError = false, BOARD_ITEM* itemA = nullptr, BOARD_ITEM* itemB = nullptr )
17 {
18  PCB_EXPR_COMPILER compiler;
19  PCB_EXPR_UCODE ucode;
20  bool ok = true;
21 
22  ucode.SetItems( itemA, itemB );
23 
24  bool error = !compiler.Compile( expr, &ucode );
25 
26  if( error )
27  {
28  if ( expectError )
29  {
30  ok = true;
31  return ok;
32  }
33  else
34  {
35  ok = false;
36  }
37  }
38 
39  LIBEVAL::VALUE result;
40 
41  if( ok )
42  {
43  result = *ucode.Run();
44  ok = (result == expectedResult);
45  }
46 
47  return ok;
48 }
49 
50 bool EvaluatePCBExpression( const std::string& aExpr, int& aResult )
51 {
52  PCB_EXPR_COMPILER compiler;
53  PCB_EXPR_UCODE ucode;
54  if( !compiler.Compile( aExpr, &ucode ) )
55  return false;
56 
57  auto result = ucode.Run();
58  return true;
59 }
60 
61 int main( int argc, char *argv[] )
62 {
64  propMgr.Rebuild();
65 
66 
67  using VAL = LIBEVAL::VALUE;
68 
69 /* testEvalExpr( "10mm + 20 mm", VAL(30e6) );
70  testEvalExpr( "3*(7+8)", VAL(3*(7+8)) );
71  testEvalExpr( "3*7+8", VAL(3*7+8) );
72  testEvalExpr( "(3*7)+8", VAL(3*7+8) );
73  testEvalExpr( "10mm + 20)", VAL(0), true );
74  */
75 
76  BOARD brd;
77 
78  NETINFO_LIST& netInfo = brd.GetNetInfo();
79 
80  NETCLASSPTR netclass1( new NETCLASS("HV") );
81  NETCLASSPTR netclass2( new NETCLASS("otherClass" ) );
82 
83  auto net1info = new NETINFO_ITEM( &brd, "net1", 1);
84  auto net2info = new NETINFO_ITEM( &brd, "net2", 2);
85 
86  net1info->SetClass( netclass1 );
87  net2info->SetClass( netclass2 );
88 
89  TRACK trackA(&brd);
90  TRACK trackB(&brd);
91 
92  trackA.SetNet( net1info );
93  trackB.SetNet( net2info );
94 
95  trackB.SetLayer( F_Cu );
96 
97  trackA.SetWidth( Mils2iu( 10 ));
98  trackB.SetWidth( Mils2iu( 20 ));
99 
100 // testEvalExpr( "A.onlayer('F.Cu') || A.onlayer('B.Cu')", VAL( 1.0 ), false, &trackA, &trackB );
101  testEvalExpr( "A.type == 'Pad' && B.type == 'Pad' && (A.onLayer('F.Cu'))", VAL( 0.0 ), false, &trackA, &trackB );
102  return 0;
103  testEvalExpr( "A.Width > B.Width", VAL( 0.0 ), false, &trackA, &trackB );
104  testEvalExpr( "A.Width + B.Width", VAL( Mils2iu(10) + Mils2iu(20) ), false, &trackA, &trackB );
105 
106  testEvalExpr( "A.Netclass", VAL( (const char*) trackA.GetNetClassName().c_str() ), false, &trackA, &trackB );
107  testEvalExpr( "(A.Netclass == 'HV') && (B.netclass == 'otherClass') && (B.netclass != 'F.Cu')", VAL( 1.0 ), false, &trackA, &trackB );
108  testEvalExpr( "A.Netclass + 1.0", VAL( 1.0 ), false, &trackA, &trackB );
109  testEvalExpr( "A.type == 'Track' && B.type == 'Track' && A.layer == 'F.Cu'", VAL( 0.0 ), false, &trackA, &trackB );
110  testEvalExpr( "(A.type == 'Track') && (B.type == 'Track') && (A.layer == 'F.Cu')", VAL( 0.0 ), false, &trackA, &trackB );
111 
112  return 0;
113 }
void Rebuild()
Rebuilds the list of all registered properties.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:61
LIBEVAL::VALUE VAL
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
const NETINFO_LIST & GetNetInfo() const
Definition: class_board.h:681
bool EvaluatePCBExpression(const std::string &aExpr, int &aResult)
wxString GetNetClassName() const
Function GetNetClassName returns a pointer to the netclass of the zone.
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
A single base class (TRACK) represents both tracks and vias, with subclasses for curved tracks (ARC) ...
void SetWidth(int aWidth)
Definition: class_track.h:109
NETINFO_LIST is a container class for NETINFO_ITEM elements, which are the nets.
Definition: netinfo.h:422
NETCLASS handles a collection of nets and the parameters used to route or test these nets.
Definition: netclass.h:49
bool testEvalExpr(const std::string expr, LIBEVAL::VALUE expectedResult, bool expectError=false, BOARD_ITEM *itemA=nullptr, BOARD_ITEM *itemB=nullptr)
Field Value of part, i.e. "3.3K".
NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
void SetNet(NETINFO_ITEM *aNetInfo)
Function SetNet Sets a NET_INFO object for the item.
Information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:176
Provides class metadata.
Definition: property_mgr.h:58
VALUE * Run(CONTEXT *ctx)
int main(int argc, char *argv[])