KiCad PCB EDA Suite
PCB_EXPR_UCODE Class Referencefinal

#include <pcb_expr_evaluator.h>

Inheritance diagram for PCB_EXPR_UCODE:
LIBEVAL::UCODE

Public Member Functions

 PCB_EXPR_UCODE ()
 
virtual ~PCB_EXPR_UCODE ()
 
virtual std::unique_ptr< LIBEVAL::VAR_REFCreateVarRef (const wxString &aVar, const wxString &aField) override
 
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall (const wxString &aName) override
 
void AddOp (UOP *uop)
 
VALUE * Run (CONTEXT *ctx)
 
wxString Dump () const
 

Protected Attributes

std::vector< UOP * > m_ucode
 

Detailed Description

Definition at line 40 of file pcb_expr_evaluator.h.

Constructor & Destructor Documentation

◆ PCB_EXPR_UCODE()

PCB_EXPR_UCODE::PCB_EXPR_UCODE ( )
inline

Definition at line 43 of file pcb_expr_evaluator.h.

43 {};

◆ ~PCB_EXPR_UCODE()

virtual PCB_EXPR_UCODE::~PCB_EXPR_UCODE ( )
inlinevirtual

Definition at line 44 of file pcb_expr_evaluator.h.

44 {};

Member Function Documentation

◆ AddOp()

void LIBEVAL::UCODE::AddOp ( UOP uop)
inlineinherited

Definition at line 348 of file libeval_compiler.h.

349  {
350  m_ucode.push_back(uop);
351  }
std::vector< UOP * > m_ucode

References LIBEVAL::UCODE::m_ucode.

Referenced by LIBEVAL::COMPILER::generateUCode().

◆ CreateFuncCall()

LIBEVAL::FUNC_CALL_REF PCB_EXPR_UCODE::CreateFuncCall ( const wxString &  aName)
overridevirtual

Reimplemented from LIBEVAL::UCODE.

Definition at line 1027 of file pcb_expr_evaluator.cpp.

1028 {
1030 
1031  return registry.Get( aName.Lower() );
1032 }
LIBEVAL::FUNC_CALL_REF Get(const wxString &name)
static PCB_EXPR_BUILTIN_FUNCTIONS & Instance()

References PCB_EXPR_BUILTIN_FUNCTIONS::Get(), and PCB_EXPR_BUILTIN_FUNCTIONS::Instance().

◆ CreateVarRef()

std::unique_ptr< LIBEVAL::VAR_REF > PCB_EXPR_UCODE::CreateVarRef ( const wxString &  aVar,
const wxString &  aField 
)
overridevirtual

Reimplemented from LIBEVAL::UCODE.

Definition at line 1035 of file pcb_expr_evaluator.cpp.

1037 {
1039  std::unique_ptr<PCB_EXPR_VAR_REF> vref;
1040 
1041  // Check for a couple of very common cases and compile them straight to "object code".
1042 
1043  if( aField.CmpNoCase( "NetClass" ) == 0 )
1044  {
1045  if( aVar == "A" )
1046  return std::make_unique<PCB_EXPR_NETCLASS_REF>( 0 );
1047  else if( aVar == "B" )
1048  return std::make_unique<PCB_EXPR_NETCLASS_REF>( 1 );
1049  else
1050  return nullptr;
1051  }
1052  else if( aField.CmpNoCase( "NetName" ) == 0 )
1053  {
1054  if( aVar == "A" )
1055  return std::make_unique<PCB_EXPR_NETNAME_REF>( 0 );
1056  else if( aVar == "B" )
1057  return std::make_unique<PCB_EXPR_NETNAME_REF>( 1 );
1058  else
1059  return nullptr;
1060  }
1061  else if( aField.CmpNoCase( "Type" ) == 0 )
1062  {
1063  if( aVar == "A" )
1064  return std::make_unique<PCB_EXPR_TYPE_REF>( 0 );
1065  else if( aVar == "B" )
1066  return std::make_unique<PCB_EXPR_TYPE_REF>( 1 );
1067  else
1068  return nullptr;
1069  }
1070 
1071  if( aVar == "A" || aVar == "AB" )
1072  vref = std::make_unique<PCB_EXPR_VAR_REF>( 0 );
1073  else if( aVar == "B" )
1074  vref = std::make_unique<PCB_EXPR_VAR_REF>( 1 );
1075  else if( aVar == "L" )
1076  vref = std::make_unique<PCB_EXPR_VAR_REF>( 2 );
1077  else
1078  return nullptr;
1079 
1080  if( aField.length() == 0 ) // return reference to base object
1081  {
1082  return std::move( vref );
1083  }
1084 
1085  wxString field( aField );
1086  field.Replace( "_", " " );
1087 
1088  for( const PROPERTY_MANAGER::CLASS_INFO& cls : propMgr.GetAllClasses() )
1089  {
1090  if( propMgr.IsOfType( cls.type, TYPE_HASH( BOARD_ITEM ) ) )
1091  {
1092  PROPERTY_BASE* prop = propMgr.GetProperty( cls.type, field );
1093 
1094  if( prop )
1095  {
1096  vref->AddAllowedClass( cls.type, prop );
1097 
1098  if( prop->TypeHash() == TYPE_HASH( int ) )
1099  {
1100  vref->SetType( LIBEVAL::VT_NUMERIC );
1101  }
1102  else if( prop->TypeHash() == TYPE_HASH( wxString ) )
1103  {
1104  vref->SetType( LIBEVAL::VT_STRING );
1105  }
1106  else if ( prop->HasChoices() )
1107  { // it's an enum, we treat it as string
1108  vref->SetType( LIBEVAL::VT_STRING );
1109  vref->SetIsEnum ( true );
1110  }
1111  else
1112  {
1113  wxFAIL_MSG( "PCB_EXPR_UCODE::createVarRef: Unknown property type." );
1114  }
1115  }
1116  }
1117  }
1118 
1119  if( vref->GetType() == LIBEVAL::VT_UNDEFINED )
1120  vref->SetType( LIBEVAL::VT_PARSE_ERROR );
1121 
1122  return std::move( vref );
1123 }
virtual bool HasChoices() const
Return true if this PROPERTY has a limited set of possible values.
Definition: property.h:216
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:65
#define TYPE_HASH(x)
Definition: property.h:59
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
virtual size_t TypeHash() const =0
Return type-id of the property type.
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Return true if aDerived is inherited from aBase.
CLASSES_INFO GetAllClasses()
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:62

References PROPERTY_MANAGER::GetAllClasses(), PROPERTY_MANAGER::GetProperty(), PROPERTY_BASE::HasChoices(), PROPERTY_MANAGER::Instance(), PROPERTY_MANAGER::IsOfType(), TYPE_HASH, PROPERTY_BASE::TypeHash(), LIBEVAL::VT_NUMERIC, LIBEVAL::VT_PARSE_ERROR, LIBEVAL::VT_STRING, and LIBEVAL::VT_UNDEFINED.

◆ Dump()

wxString LIBEVAL::UCODE::Dump ( ) const
inherited

Definition at line 189 of file libeval_compiler.cpp.

190 {
191  wxString rv;
192 
193  for( auto op : m_ucode )
194  {
195  rv += op->Format();
196  rv += "\n";
197  }
198 
199  return rv;
200 };
std::vector< UOP * > m_ucode

References LIBEVAL::UCODE::m_ucode.

Referenced by LIBEVAL::COMPILER::generateUCode().

◆ Run()

VALUE * LIBEVAL::UCODE::Run ( CONTEXT ctx)
inherited

Definition at line 1210 of file libeval_compiler.cpp.

1211 {
1212  static VALUE g_false( 0 );
1213 
1214  try
1215  {
1216  for( UOP* op : m_ucode )
1217  op->Exec( ctx );
1218  }
1219  catch(...)
1220  {
1221  // rules which fail outright should not be fired
1222  return &g_false;
1223  }
1224 
1225  if( ctx->SP() == 1 )
1226  {
1227  return ctx->Pop();
1228  }
1229  else
1230  {
1231  // If stack is corrupted after execution it suggests a problem with the compiler, not
1232  // the rule....
1233 
1234  // do not use "assert"; it crashes outright on OSX
1235  wxASSERT( ctx->SP() == 1 );
1236 
1237  // non-well-formed rules should not be fired on a release build
1238  return &g_false;
1239  }
1240 }
std::vector< UOP * > m_ucode

References LIBEVAL::UCODE::m_ucode, LIBEVAL::CONTEXT::Pop(), and LIBEVAL::CONTEXT::SP().

Referenced by PCB_EXPR_EVALUATOR::Evaluate(), and testEvalExpr().

Member Data Documentation

◆ m_ucode

std::vector<UOP*> LIBEVAL::UCODE::m_ucode
protectedinherited

The documentation for this class was generated from the following files: