44#pragma GCC diagnostic push
45#pragma GCC diagnostic ignored "-Wunused-variable"
46#pragma GCC diagnostic ignored "-Wsign-compare"
49#include <libeval_compiler/grammar.c>
50#include <libeval_compiler/grammar.h>
53#pragma GCC diagnostic pop
57#define libeval_dbg(level, fmt, ...) \
58 wxLogTrace( "libeval_compiler", fmt, __VA_ARGS__ );
66 t2->value.str = value.
str ?
new wxString( *value.
str ) :
nullptr;
67 t2->value.num = value.
num;
68 t2->value.idx = value.
idx;
70 t2->leaf[0] =
nullptr;
71 t2->leaf[1] =
nullptr;
72 t2->isTerminal =
false;
76 libeval_dbg(10,
" ostr %p nstr %p nnode %p op %d", value.
str, t2->value.str, t2, t2->op );
79 compiler->
GcItem( t2->value.str );
103 for(
int i = 0; simpleOps[i].op >= 0; i++ )
105 if( simpleOps[i].op == op )
106 return simpleOps[i].mnemonic;
150 str = wxString::Format(
"PUSH VAR [%p]",
m_ref.get() );
156 str = wxString::Format(
"PUSH nullptr" );
158 str = wxString::Format(
"PUSH NUM [%.10f]",
m_value->AsDouble() );
160 str = wxString::Format(
"PUSH STR [%ls]",
m_value->AsString() );
165 str = wxString::Format(
"MCALL" );
169 str = wxString::Format(
"FCALL" );
227 while( p <
m_str.length() && cond(
m_str[p] ) )
229 rv.append( 1,
m_str[p] );
237 const std::function<
bool( wxUniChar )>& stopCond )
const
241 if( remaining < (
int) match.length() )
244 if(
m_str.substr(
m_pos, match.length() ) == match )
245 return ( remaining == (
int) match.length() || stopCond(
m_str[
m_pos + match.length()] ) );
252 m_lexerState(
COMPILER::LS_DEFAULT )
258 m_parser = LIBEVAL::ParseAlloc( malloc );
266 LIBEVAL::ParseFree(
m_parser, free );
333 libeval_dbg(0,
"str: '%s' empty: %d\n", aString.c_str(), !!aString.empty() );
335 if( aString.empty() )
362 }
while( tok.
token );
404 aToken.
token = G_STRING;
416 for(
const wxString& unitName :
m_unitResolver->GetSupportedUnits() )
420 libeval_dbg(10,
"Match unit '%s'\n", unitName.c_str() );
442 retval.
token = G_ENDS;
450 auto isDecimalSeparator =
451 [&]( wxUniChar ch ) ->
bool
460 bool haveSeparator =
false;
465 if( isDecimalSeparator( ch ) && haveSeparator )
468 current.append( 1, ch );
470 if( isDecimalSeparator( ch ) )
471 haveSeparator =
true;
475 }
while( isdigit( ch ) || isDecimalSeparator( ch ) );
478 for(
int i = current.length(); i; i-- )
480 if( isDecimalSeparator( current[i - 1] ) )
505 else if( isdigit( ch ) )
509 retval.
token = G_VALUE;
510 retval.
value.
str =
new wxString( current );
520 retval.
token = G_UNIT;
523 else if( ch ==
'\'' )
529 else if( isalpha( ch ) || ch ==
'_' )
531 current =
m_tokenizer.
GetChars( [](
int c ) ->
bool {
return isalnum( c ) || c ==
'_'; } );
532 retval.
token = G_IDENTIFIER;
533 retval.
value.
str =
new wxString( current );
538 retval.
token = G_EQUAL;
543 retval.
token = G_NOT_EQUAL;
548 retval.
token = G_LESS_EQUAL_THAN;
553 retval.
token = G_GREATER_EQUAL_THAN;
558 retval.
token = G_BOOL_AND;
563 retval.
token = G_BOOL_OR;
571 case '+': retval.
token = G_PLUS;
break;
572 case '!': retval.
token = G_BOOL_NOT;
break;
573 case '-': retval.
token = G_MINUS;
break;
574 case '*': retval.
token = G_MULT;
break;
575 case '/': retval.
token = G_DIVIDE;
break;
576 case '<': retval.
token = G_LESS_THAN;
break;
577 case '>': retval.
token = G_GREATER_THAN;
break;
578 case '(': retval.
token = G_PARENL;
break;
579 case ')': retval.
token = G_PARENR;
break;
580 case ';': retval.
token = G_SEMCOL;
break;
581 case '.': retval.
token = G_STRUCT_REF;
break;
582 case ',': retval.
token = G_COMMA;
break;
611 str.Printf(
"\n[%p L0:%-20p L1:%-20p] ", tok, tok->
leaf[0], tok->
leaf[1] );
614 for(
int i = 0; i < 2 * depth; i++ )
670 str.Printf(
"UNIT: %d ", tok->
value.
idx );
722 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue );
723 uop =
new UOP( aOp, std::move( val ) );
731 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue, aStringIsWildcard );
732 uop =
new UOP( aOp, std::move( val ) );
740 uop =
new UOP( aOp, std::move( aRef ) );
748 uop =
new UOP( aOp, std::move( aFunc ), std::move( aRef ) );
773 std::vector<TREE_NODE*> args;
782 args.push_back( root );
790 args.push_back(n->
leaf[1]);
801 std::reverse( args.begin(), args.end() );
803 for(
size_t i = 0; i < args.size(); i++ )
812 std::vector<TREE_NODE*> stack;
817 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( 1.0 );
825 stack.push_back(
m_tree );
832 while( !stack.empty() )
836 libeval_dbg( 4,
"process node %p [op %d] [stack %lu]\n",
837 node, node->
op, (
unsigned long)stack.size() );
875 switch( node->
leaf[1]->
op )
884 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName, propName );
888 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
890 node->
leaf[0]->
srcPos - (
int) itemName.length() );
894 msg.Printf(
_(
"Unrecognized property '%s'" ), propName );
896 node->
leaf[1]->
srcPos - (
int) propName.length() );
914 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName,
"" );
918 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
920 node->
leaf[0]->
srcPos - (
int) itemName.length() );
927 libeval_dbg( 10,
"emit func call: %s\n", functionName );
931 msg.Printf(
_(
"Unrecognized function '%s'" ), functionName );
943 aPreflightContext->
Push( param );
947 [&](
const wxString& aMessage,
int aOffset )
955 func( aPreflightContext, vref.get() );
956 aPreflightContext->
Pop();
972 stack.push_back( node->
leaf[1] );
975 stack.push_back( pnode );
988 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName, propName );
992 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
994 node->
leaf[0]->
srcPos - (
int) itemName.length() );
997 msg.Printf(
_(
"Unrecognized property '%s'" ), propName );
1024 msg.Printf(
_(
"Unexpected units for '%s'" ),
formatNode( node ) );
1036 msg.Printf(
_(
"Missing units for '%s'| (%s)" ),
1053 bool isWildcard = str.Contains(
"?") || str.Contains(
"*");
1065 msg.Printf(
_(
"Unrecognized item '%s'" ),
formatNode( node ) );
1085 stack.push_back( node->
leaf[0] );
1091 stack.push_back( node->
leaf[1] );
1103 node->
uop =
nullptr;
1121 VALUE* value =
nullptr;
1144#define AS_DOUBLE( arg ) ( arg ? arg->AsDouble() : 0.0 )
1156 ctx->
ReportError( wxString::Format(
_(
"Type mismatch between '%s' and %lf" ),
1162 ctx->
ReportError( wxString::Format(
_(
"Type mismatch between %lf and '%s'" ),
1195 if( !arg1 || !arg2 )
1196 result = arg1 == arg2 ? 1 : 0;
1198 result = arg2->
EqualTo( ctx, arg1 ) ? 1 : 0;
1200 result = arg1->
EqualTo( ctx, arg2 ) ? 1 : 0;
1203 if( !arg1 || !arg2 )
1204 result = arg1 != arg2 ? 1 : 0;
1206 result = arg2->
NotEqualTo( ctx, arg1 ) ? 1 : 0;
1208 result = arg1->
NotEqualTo( ctx, arg2 ) ? 1 : 0;
1229 double ARG1VALUE = arg1 ? arg1->
AsDouble() : 0.0;
1235 result = ARG1VALUE != 0.0 ? 0 : 1;
1238 result = ARG1VALUE != 0.0 ? 1 : 0;
1252 static VALUE g_false( 0 );
1265 if( ctx->
SP() == 1 )
1275 wxASSERT( ctx->
SP() == 1 );
std::unique_ptr< UNIT_RESOLVER > m_unitResolver
void newString(const wxString &aString)
void freeTree(LIBEVAL::TREE_NODE *tree)
bool lexString(T_TOKEN &aToken)
void GcItem(TREE_NODE *aItem)
bool generateUCode(UCODE *aCode, CONTEXT *aPreflightContext)
char m_localeDecimalSeparator
std::function< void(const wxString &aMessage, int aOffset)> m_errorCallback
bool lexDefault(T_TOKEN &aToken)
std::vector< TREE_NODE * > m_gcItems
void reportError(COMPILATION_STAGE stage, const wxString &aErrorMsg, int aPos=-1)
void setRoot(LIBEVAL::TREE_NODE *root)
std::vector< wxString * > m_gcStrings
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
ERROR_STATUS m_errorStatus
void parseError(const char *s)
VALUE * StoreValue(VALUE *aValue)
void ReportError(const wxString &aErrorMsg)
std::function< void(const wxString &aMessage, int aOffset)> m_errorCallback
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
void NextChar(int aAdvance=1)
void Restart(const wxString &aStr)
wxString GetChars(const std::function< bool(wxUniChar)> &cond) const
bool MatchAhead(const wxString &match, const std::function< bool(wxUniChar)> &stopCond) const
void SetUop(int aOp, double aValue)
virtual std::unique_ptr< VAR_REF > CreateVarRef(const wxString &var, const wxString &field)
std::vector< UOP * > m_ucode
VALUE * Run(CONTEXT *ctx)
virtual FUNC_CALL_REF CreateFuncCall(const wxString &name)
std::unique_ptr< VAR_REF > m_ref
std::unique_ptr< VALUE > m_value
virtual const wxString & AsString() const
virtual bool NotEqualTo(CONTEXT *aCtx, const VALUE *b) const
virtual double AsDouble() const
VAR_TYPE_T GetType() const
virtual bool EqualTo(CONTEXT *aCtx, const VALUE *b) const
#define libeval_dbg(level, fmt,...)
#define TR_OP_GREATER_EQUAL
#define TR_UOP_PUSH_VALUE
#define TR_OP_METHOD_CALL
#define TR_OP_BINARY_MASK
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Function DoubleValueFromString converts aTextValue to a double.
TREE_NODE * newNode(LIBEVAL::COMPILER *compiler, int op, const T_TOKEN_VALUE &value)
static std::vector< TREE_NODE * > squashParamList(TREE_NODE *root)
constexpr T_TOKEN defaultToken
void dumpNode(wxString &buf, TREE_NODE *tok, int depth=0)
constexpr T_TOKEN_VALUE defaultTokenValue
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
static const wxString formatOpName(int op)
const wxString formatNode(TREE_NODE *node)
static void prepareTree(LIBEVAL::TREE_NODE *node)
bool WildCompareString(const wxString &pattern, const wxString &string_to_tst, bool case_sensitive)
Compare a string against wild card (* and ?) pattern using the usual rules.
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.