KiCad PCB EDA Suite
LIBEVAL::UOP Class Reference

#include <libeval_compiler.h>

Public Member Functions

 UOP (int op, std::unique_ptr< VALUE > value)
 
 UOP (int op, std::unique_ptr< VAR_REF > vref)
 
 UOP (int op, FUNC_CALL_REF func, std::unique_ptr< VAR_REF > vref=nullptr)
 
 ~UOP ()
 
void Exec (CONTEXT *ctx)
 
wxString Format () const
 

Private Attributes

int m_op
 
FUNC_CALL_REF m_func
 
std::unique_ptr< VAR_REFm_ref
 
std::unique_ptr< VALUEm_value
 

Detailed Description

Definition at line 372 of file libeval_compiler.h.

Constructor & Destructor Documentation

◆ UOP() [1/3]

LIBEVAL::UOP::UOP ( int  op,
std::unique_ptr< VALUE value 
)
inline

Definition at line 375 of file libeval_compiler.h.

375  :
376  m_op( op ),
377  m_ref(nullptr),
378  m_value( std::move( value ) )
379  {};
std::unique_ptr< VALUE > m_value
std::unique_ptr< VAR_REF > m_ref

References move.

◆ UOP() [2/3]

LIBEVAL::UOP::UOP ( int  op,
std::unique_ptr< VAR_REF vref 
)
inline

Definition at line 381 of file libeval_compiler.h.

381  :
382  m_op( op ),
383  m_ref( std::move( vref ) ),
384  m_value(nullptr)
385  {};
std::unique_ptr< VALUE > m_value
std::unique_ptr< VAR_REF > m_ref

References move.

◆ UOP() [3/3]

LIBEVAL::UOP::UOP ( int  op,
FUNC_CALL_REF  func,
std::unique_ptr< VAR_REF vref = nullptr 
)
inline

Definition at line 387 of file libeval_compiler.h.

387  :
388  m_op( op ),
389  m_func( std::move( func ) ),
390  m_ref( std::move( vref ) ),
391  m_value(nullptr)
392  {};
std::unique_ptr< VALUE > m_value
FUNC_CALL_REF m_func
std::unique_ptr< VAR_REF > m_ref

References move.

◆ ~UOP()

LIBEVAL::UOP::~UOP ( )
inline

Definition at line 394 of file libeval_compiler.h.

395  {
396  }

Member Function Documentation

◆ Exec()

void LIBEVAL::UOP::Exec ( CONTEXT ctx)

Definition at line 1087 of file libeval_compiler.cpp.

1088 {
1089  switch( m_op )
1090  {
1091  case TR_UOP_PUSH_VAR:
1092  {
1093  VALUE* value = ctx->AllocValue();
1094 
1095  if( m_ref )
1096  value->Set( m_ref->GetValue( ctx ) );
1097 
1098  ctx->Push( value );
1099  }
1100  break;
1101 
1102  case TR_UOP_PUSH_VALUE:
1103  ctx->Push( m_value.get() );
1104  return;
1105 
1106  case TR_OP_METHOD_CALL:
1107  m_func( ctx, m_ref.get() );
1108  return;
1109 
1110  default:
1111  break;
1112  }
1113 
1114  if( m_op & TR_OP_BINARY_MASK )
1115  {
1116  LIBEVAL::VALUE* arg2 = ctx->Pop();
1117  LIBEVAL::VALUE* arg1 = ctx->Pop();
1118  double arg2Value = arg2 ? arg2->AsDouble() : 0.0;
1119  double arg1Value = arg1 ? arg1->AsDouble() : 0.0;
1120  double result;
1121 
1122  if( ctx->HasErrorCallback() )
1123  {
1124  if( arg1 && arg1->GetType() == VT_STRING && arg2 && arg2->GetType() == VT_NUMERIC )
1125  {
1126  ctx->ReportError( wxString::Format( _( "Type mismatch between '%s' and %lf" ),
1127  arg1->AsString(),
1128  arg2->AsDouble() ) );
1129  }
1130  else if( arg1 && arg1->GetType() == VT_NUMERIC && arg2 && arg2->GetType() == VT_STRING )
1131  {
1132  ctx->ReportError( wxString::Format( _( "Type mismatch between %lf and '%s'" ),
1133  arg1->AsDouble(),
1134  arg2->AsString() ) );
1135  }
1136  }
1137 
1138  switch( m_op )
1139  {
1140  case TR_OP_ADD:
1141  result = arg1Value + arg2Value;
1142  break;
1143  case TR_OP_SUB:
1144  result = arg1Value - arg2Value;
1145  break;
1146  case TR_OP_MUL:
1147  result = arg1Value * arg2Value;
1148  break;
1149  case TR_OP_DIV:
1150  result = arg1Value / arg2Value;
1151  break;
1152  case TR_OP_LESS_EQUAL:
1153  result = arg1Value <= arg2Value ? 1 : 0;
1154  break;
1155  case TR_OP_GREATER_EQUAL:
1156  result = arg1Value >= arg2Value ? 1 : 0;
1157  break;
1158  case TR_OP_LESS:
1159  result = arg1Value < arg2Value ? 1 : 0;
1160  break;
1161  case TR_OP_GREATER:
1162  result = arg1Value > arg2Value ? 1 : 0;
1163  break;
1164  case TR_OP_EQUAL:
1165  result = arg1 && arg2 && arg1->EqualTo( ctx, arg2 ) ? 1 : 0;
1166  break;
1167  case TR_OP_NOT_EQUAL:
1168  result = arg1 && arg2 && arg1->NotEqualTo( ctx, arg2 ) ? 1 : 0;
1169  break;
1170  case TR_OP_BOOL_AND:
1171  result = arg1Value != 0.0 && arg2Value != 0.0 ? 1 : 0;
1172  break;
1173  case TR_OP_BOOL_OR:
1174  result = arg1Value != 0.0 || arg2Value != 0.0 ? 1 : 0;
1175  break;
1176  default:
1177  result = 0.0;
1178  break;
1179  }
1180 
1181  auto rp = ctx->AllocValue();
1182  rp->Set( result );
1183  ctx->Push( rp );
1184  return;
1185  }
1186  else if( m_op & TR_OP_UNARY_MASK )
1187  {
1188  LIBEVAL::VALUE* arg1 = ctx->Pop();
1189  double arg1Value = arg1 ? arg1->AsDouble() : 0.0;
1190  double result;
1191 
1192  switch( m_op )
1193  {
1194  case TR_OP_BOOL_NOT:
1195  result = arg1Value != 0.0 ? 0 : 1;
1196  break;
1197  default:
1198  result = 0.0;
1199  break;
1200  }
1201 
1202  auto rp = ctx->AllocValue();
1203  rp->Set( result );
1204  ctx->Push( rp );
1205  return;
1206  }
1207 }
std::unique_ptr< VALUE > m_value
#define TR_OP_BINARY_MASK
#define TR_OP_METHOD_CALL
#define TR_OP_BOOL_OR
virtual bool EqualTo(CONTEXT *aCtx, const VALUE *b) const
#define TR_OP_BOOL_AND
#define TR_OP_GREATER_EQUAL
#define TR_OP_NOT_EQUAL
#define TR_OP_GREATER
VAR_TYPE_T GetType() const
#define TR_OP_MUL
#define TR_OP_DIV
#define TR_OP_SUB
#define _(s)
#define TR_OP_LESS_EQUAL
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
#define TR_OP_UNARY_MASK
virtual double AsDouble() const
#define TR_OP_ADD
#define TR_OP_BOOL_NOT
virtual const wxString & AsString() const
FUNC_CALL_REF m_func
std::unique_ptr< VAR_REF > m_ref
#define TR_UOP_PUSH_VAR
#define TR_OP_LESS
#define TR_UOP_PUSH_VALUE
#define TR_OP_EQUAL
virtual bool NotEqualTo(CONTEXT *aCtx, const VALUE *b) const

References _, LIBEVAL::CONTEXT::AllocValue(), LIBEVAL::VALUE::AsDouble(), LIBEVAL::VALUE::AsString(), LIBEVAL::VALUE::EqualTo(), Format(), LIBEVAL::VALUE::GetType(), LIBEVAL::CONTEXT::HasErrorCallback(), m_func, m_op, m_ref, m_value, LIBEVAL::VALUE::NotEqualTo(), LIBEVAL::CONTEXT::Pop(), LIBEVAL::CONTEXT::Push(), LIBEVAL::CONTEXT::ReportError(), LIBEVAL::VALUE::Set(), TR_OP_ADD, TR_OP_BINARY_MASK, TR_OP_BOOL_AND, TR_OP_BOOL_NOT, TR_OP_BOOL_OR, TR_OP_DIV, TR_OP_EQUAL, TR_OP_GREATER, TR_OP_GREATER_EQUAL, TR_OP_LESS, TR_OP_LESS_EQUAL, TR_OP_METHOD_CALL, TR_OP_MUL, TR_OP_NOT_EQUAL, TR_OP_SUB, TR_OP_UNARY_MASK, TR_UOP_PUSH_VALUE, TR_UOP_PUSH_VAR, LIBEVAL::VT_NUMERIC, and LIBEVAL::VT_STRING.

◆ Format()

wxString LIBEVAL::UOP::Format ( ) const

Definition at line 142 of file libeval_compiler.cpp.

143 {
144  wxString str;
145 
146  switch( m_op )
147  {
148  case TR_UOP_PUSH_VAR:
149  str = wxString::Format( "PUSH VAR [%p]", m_ref.get() );
150  break;
151 
152  case TR_UOP_PUSH_VALUE:
153  {
154  if( !m_value )
155  str = wxString::Format( "PUSH nullptr" );
156  else if( m_value->GetType() == VT_NUMERIC )
157  str = wxString::Format( "PUSH NUM [%.10f]", m_value->AsDouble() );
158  else
159  str = wxString::Format( "PUSH STR [%ls]", m_value->AsString() );
160  }
161  break;
162 
163  case TR_OP_METHOD_CALL:
164  str = wxString::Format( "MCALL" );
165  break;
166 
167  case TR_OP_FUNC_CALL:
168  str = wxString::Format( "FCALL" );
169  break;
170 
171  default:
172  str = wxString::Format( "%s %d", formatOpName( m_op ).c_str(), m_op );
173  break;
174  }
175 
176  return str;
177 }
std::unique_ptr< VALUE > m_value
#define TR_OP_FUNC_CALL
#define TR_OP_METHOD_CALL
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
static const wxString formatOpName(int op)
std::unique_ptr< VAR_REF > m_ref
#define TR_UOP_PUSH_VAR
#define TR_UOP_PUSH_VALUE

References Format(), LIBEVAL::formatOpName(), m_op, m_ref, m_value, TR_OP_FUNC_CALL, TR_OP_METHOD_CALL, TR_UOP_PUSH_VALUE, TR_UOP_PUSH_VAR, and LIBEVAL::VT_NUMERIC.

Member Data Documentation

◆ m_func

FUNC_CALL_REF LIBEVAL::UOP::m_func
private

Definition at line 405 of file libeval_compiler.h.

Referenced by Exec().

◆ m_op

int LIBEVAL::UOP::m_op
private

Definition at line 403 of file libeval_compiler.h.

Referenced by Exec(), and Format().

◆ m_ref

std::unique_ptr<VAR_REF> LIBEVAL::UOP::m_ref
private

Definition at line 406 of file libeval_compiler.h.

Referenced by Exec(), and Format().

◆ m_value

std::unique_ptr<VALUE> LIBEVAL::UOP::m_value
private

Definition at line 407 of file libeval_compiler.h.

Referenced by Exec(), and Format().


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