libeval_compiler: == operator now does wildcard string comparison

This commit is contained in:
Tomasz Wlostowski 2020-09-23 23:44:50 +02:00
parent 201a630740
commit 0c885c9c31
2 changed files with 39 additions and 22 deletions

View File

@ -23,6 +23,8 @@
#include <vector>
#include <algorithm>
#include <kicad_string.h>
#ifdef DEBUG
#include <cstdarg>
#endif
@ -105,6 +107,23 @@ static const wxString formatOpName( int op )
return "???";
}
bool VALUE::EqualTo( const VALUE* b ) const
{
if( m_type == VT_NUMERIC && b->m_type == VT_NUMERIC )
return m_valueDbl == b->m_valueDbl;
else if( m_type == VT_STRING && b->m_type == VT_STRING )
{
if( b->m_stringIsWildcard )
{
return WildCompareString( b->m_valueStr, m_valueStr, false );
}
else
return m_valueStr.CmpNoCase( b->m_valueStr );
}
return false;
}
wxString UOP::Format() const
{
@ -668,11 +687,11 @@ void TREE_NODE::SetUop( int aOp, double aValue )
}
void TREE_NODE::SetUop( int aOp, const wxString& aValue )
void TREE_NODE::SetUop( int aOp, const wxString& aValue, bool aStringIsWildcard )
{
delete uop;
std::unique_ptr<VALUE> val( new VALUE( aValue ) );
std::unique_ptr<VALUE> val( new VALUE( aValue, aStringIsWildcard ) );
uop = new UOP( aOp, std::move( val ) );
}
@ -900,6 +919,7 @@ bool COMPILER::generateUCode( UCODE* aCode, CONTEXT* aPreflightContext )
// a TR_OP_FUNC_CALL and its function parameter
stack.pop_back();
stack.push_back( node->leaf[1] );
//stack.push_back( node->leaf[1]->leaf[1] );
for( auto pnode : params )
{
stack.push_back( pnode );
@ -940,7 +960,9 @@ bool COMPILER::generateUCode( UCODE* aCode, CONTEXT* aPreflightContext )
case TR_STRING:
{
node->SetUop( TR_UOP_PUSH_VALUE, *node->value.str );
wxString str = *node->value.str;
bool isWildcard = str.Contains("?") || str.Contains("*");
node->SetUop( TR_UOP_PUSH_VALUE, str, isWildcard );
node->isTerminal = true;
break;
}
@ -1120,7 +1142,11 @@ VALUE* UCODE::Run( CONTEXT* ctx )
return &g_false;
}
assert( ctx->SP() == 1 );
// non-well-formed rules should not be fired
// fixme: not sure it's a good idea, if stack is corrupted after execution it means
// a problem with the compiler, not the rule...
if( ctx->SP() != 1 )
return &g_false;

View File

@ -141,7 +141,7 @@ public:
int srcPos;
void SetUop( int aOp, double aValue );
void SetUop( int aOp, const wxString& aValue );
void SetUop( int aOp, const wxString& aValue, bool aStringIsWildcard );
void SetUop( int aOp, std::unique_ptr<VAR_REF> aRef = nullptr );
void SetUop( int aOp, FUNC_CALL_REF aFunc, std::unique_ptr<VAR_REF> aRef = nullptr );
};
@ -180,18 +180,21 @@ class VALUE
public:
VALUE():
m_type(VT_UNDEFINED),
m_valueDbl( 0 )
m_valueDbl( 0 ),
m_stringIsWildcard( false )
{};
VALUE( const wxString& aStr ) :
VALUE( const wxString& aStr, bool aIsWildcard = false ) :
m_type( VT_STRING ),
m_valueDbl( 0 ),
m_valueStr( aStr )
m_valueStr( aStr ),
m_stringIsWildcard( aIsWildcard )
{};
VALUE( const double aVal ) :
m_type( VT_NUMERIC ),
m_valueDbl( aVal )
m_valueDbl( aVal ),
m_stringIsWildcard( false )
{};
double AsDouble() const
@ -204,15 +207,7 @@ public:
return m_valueStr;
}
bool operator==( const VALUE& b ) const
{
if( m_type == VT_NUMERIC && b.m_type == VT_NUMERIC )
return m_valueDbl == b.m_valueDbl;
else if( m_type == VT_STRING && b.m_type == VT_STRING )
return m_valueStr.CmpNoCase( b.m_valueStr ) == 0;
return false;
}
bool EqualTo( const VALUE* b ) const;
VAR_TYPE_T GetType() const { return m_type; };
@ -237,15 +232,11 @@ public:
m_valueStr = val.m_valueStr;
}
bool EqualTo( const VALUE* v2 ) const
{
return operator==( *v2 );
}
private:
VAR_TYPE_T m_type;
double m_valueDbl;
wxString m_valueStr;
bool m_stringIsWildcard;
};
class VAR_REF