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;
153 str = wxString::Format(
"PUSH VAR [%p]",
m_ref.get() );
159 str = wxString::Format(
"PUSH nullptr" );
161 str = wxString::Format(
"PUSH NUM [%.10f]",
m_value->AsDouble() );
163 str = wxString::Format(
"PUSH STR [%ls]",
m_value->AsString() );
168 str = wxString::Format(
"MCALL" );
172 str = wxString::Format(
"FCALL" );
230 while( p <
m_str.length() && cond(
m_str[p] ) )
232 rv.append( 1,
m_str[p] );
240 const std::function<
bool( wxUniChar )>& stopCond )
const
244 if( remaining < (
int) match.length() )
247 if(
m_str.substr(
m_pos, match.length() ) == match )
248 return ( remaining == (
int) match.length() || stopCond(
m_str[
m_pos + match.length()] ) );
255 m_lexerState(
COMPILER::LS_DEFAULT )
261 m_parser = LIBEVAL::ParseAlloc( malloc );
269 LIBEVAL::ParseFree(
m_parser, free );
336 libeval_dbg(0,
"str: '%s' empty: %d\n", aString.c_str(), !!aString.empty() );
338 if( aString.empty() )
365 }
while( tok.
token );
407 aToken.
token = G_STRING;
419 for(
const wxString& unitName :
m_unitResolver->GetSupportedUnits() )
423 libeval_dbg(10,
"Match unit '%s'\n", unitName.c_str() );
445 retval.
token = G_ENDS;
453 auto isDecimalSeparator =
454 [&]( wxUniChar ch ) ->
bool
463 bool haveSeparator =
false;
468 if( isDecimalSeparator( ch ) && haveSeparator )
471 current.append( 1, ch );
473 if( isDecimalSeparator( ch ) )
474 haveSeparator =
true;
478 }
while( isdigit( ch ) || isDecimalSeparator( ch ) );
481 for(
int i = current.length(); i; i-- )
483 if( isDecimalSeparator( current[i - 1] ) )
508 else if( isdigit( ch ) )
512 retval.
token = G_VALUE;
513 retval.
value.
str =
new wxString( current );
523 retval.
token = G_UNIT;
526 else if( ch ==
'\'' )
532 else if( isalpha( ch ) || ch ==
'_' )
534 current =
m_tokenizer.
GetChars( [](
int c ) ->
bool {
return isalnum( c ) || c ==
'_'; } );
535 retval.
token = G_IDENTIFIER;
536 retval.
value.
str =
new wxString( current );
541 retval.
token = G_EQUAL;
546 retval.
token = G_NOT_EQUAL;
551 retval.
token = G_LESS_EQUAL_THAN;
556 retval.
token = G_GREATER_EQUAL_THAN;
561 retval.
token = G_BOOL_AND;
566 retval.
token = G_BOOL_OR;
574 case '+': retval.
token = G_PLUS;
break;
575 case '!': retval.
token = G_BOOL_NOT;
break;
576 case '-': retval.
token = G_MINUS;
break;
577 case '*': retval.
token = G_MULT;
break;
578 case '/': retval.
token = G_DIVIDE;
break;
579 case '<': retval.
token = G_LESS_THAN;
break;
580 case '>': retval.
token = G_GREATER_THAN;
break;
581 case '(': retval.
token = G_PARENL;
break;
582 case ')': retval.
token = G_PARENR;
break;
583 case ';': retval.
token = G_SEMCOL;
break;
584 case '.': retval.
token = G_STRUCT_REF;
break;
585 case ',': retval.
token = G_COMMA;
break;
614 str.Printf(
"\n[%p L0:%-20p L1:%-20p] ", tok, tok->
leaf[0], tok->
leaf[1] );
617 for(
int i = 0; i < 2 * depth; i++ )
673 str.Printf(
"UNIT: %d ", tok->
value.
idx );
725 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue );
726 uop =
new UOP( aOp, std::move( val ) );
734 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue, aStringIsWildcard );
735 uop =
new UOP( aOp, std::move( val ) );
743 uop =
new UOP( aOp, std::move( aRef ) );
751 uop =
new UOP( aOp, std::move( aFunc ), std::move( aRef ) );
776 std::vector<TREE_NODE*> args;
785 args.push_back( root );
793 args.push_back(n->
leaf[1]);
804 std::reverse( args.begin(), args.end() );
806 for(
size_t i = 0; i < args.size(); i++ )
815 std::vector<TREE_NODE*> stack;
820 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( 1.0 );
828 stack.push_back(
m_tree );
835 while( !stack.empty() )
839 libeval_dbg( 4,
"process node %p [op %d] [stack %lu]\n",
840 node, node->
op, (
unsigned long)stack.size() );
878 switch( node->
leaf[1]->
op )
887 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName, propName );
891 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
893 node->
leaf[0]->
srcPos - (
int) itemName.length() );
897 msg.Printf(
_(
"Unrecognized property '%s'" ), propName );
899 node->
leaf[1]->
srcPos - (
int) propName.length() );
917 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName,
"" );
921 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
923 node->
leaf[0]->
srcPos - (
int) itemName.length() );
930 libeval_dbg( 10,
"emit func call: %s\n", functionName );
934 msg.Printf(
_(
"Unrecognized function '%s'" ), functionName );
946 aPreflightContext->
Push( param );
950 [&](
const wxString& aMessage,
int aOffset )
958 func( aPreflightContext, vref.get() );
959 aPreflightContext->
Pop();
975 stack.push_back( node->
leaf[1] );
978 stack.push_back( pnode );
991 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName, propName );
995 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
997 node->
leaf[0]->
srcPos - (
int) itemName.length() );
1000 msg.Printf(
_(
"Unrecognized property '%s'" ), propName );
1027 msg.Printf(
_(
"Unexpected units for '%s'" ),
formatNode( node ) );
1039 msg.Printf(
_(
"Missing units for '%s'| (%s)" ),
1056 bool isWildcard = str.Contains(
"?") || str.Contains(
"*");
1068 msg.Printf(
_(
"Unrecognized item '%s'" ),
formatNode( node ) );
1088 stack.push_back( node->
leaf[0] );
1094 stack.push_back( node->
leaf[1] );
1106 node->
uop =
nullptr;
1124 VALUE* value =
nullptr;
1147#define AS_DOUBLE( arg ) ( arg ? arg->AsDouble() : 0.0 )
1159 ctx->
ReportError( wxString::Format(
_(
"Type mismatch between '%s' and %lf" ),
1165 ctx->
ReportError( wxString::Format(
_(
"Type mismatch between %lf and '%s'" ),
1198 if( !arg1 || !arg2 )
1199 result = arg1 == arg2 ? 1 : 0;
1201 result = arg2->
EqualTo( ctx, arg1 ) ? 1 : 0;
1203 result = arg1->
EqualTo( ctx, arg2 ) ? 1 : 0;
1206 if( !arg1 || !arg2 )
1207 result = arg1 != arg2 ? 1 : 0;
1209 result = arg2->
NotEqualTo( ctx, arg1 ) ? 1 : 0;
1211 result = arg1->
NotEqualTo( ctx, arg2 ) ? 1 : 0;
1232 double ARG1VALUE = arg1 ? arg1->
AsDouble() : 0.0;
1238 result = ARG1VALUE != 0.0 ? 0 : 1;
1241 result = ARG1VALUE != 0.0 ? 1 : 0;
1255 static VALUE g_false( 0 );
1268 if( ctx->
SP() == 1 )
1278 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.