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" );
209 while( p <
m_str.length() && cond(
m_str[p] ) )
211 rv.append( 1,
m_str[p] );
219 const std::function<
bool( wxUniChar )>& stopCond )
const
223 if( remaining < (
int) match.length() )
226 if(
m_str.substr(
m_pos, match.length() ) == match )
227 return ( remaining == (
int) match.length() || stopCond(
m_str[
m_pos + match.length()] ) );
234 m_lexerState(
COMPILER::LS_DEFAULT )
240 m_parser = LIBEVAL::ParseAlloc( malloc );
248 LIBEVAL::ParseFree(
m_parser, free );
315 libeval_dbg(0,
"str: '%s' empty: %d\n", aString.c_str(), !!aString.empty() );
317 if( aString.empty() )
344 }
while( tok.
token );
388 aToken.
token = G_STRING;
389 aToken.
value.
str =
new wxString( str );
401 for(
const wxString& unitName :
m_unitResolver->GetSupportedUnits() )
405 libeval_dbg(10,
"Match unit '%s'\n", unitName.c_str() );
427 retval.
token = G_ENDS;
435 auto isDecimalSeparator =
436 [&]( wxUniChar ch ) ->
bool
445 bool haveSeparator =
false;
450 if( isDecimalSeparator( ch ) && haveSeparator )
453 current.append( 1, ch );
455 if( isDecimalSeparator( ch ) )
456 haveSeparator =
true;
460 }
while( isdigit( ch ) || isDecimalSeparator( ch ) );
463 for(
int i = current.length(); i; i-- )
465 if( isDecimalSeparator( current[i - 1] ) )
490 else if( isdigit( ch ) )
494 retval.
token = G_VALUE;
495 retval.
value.
str =
new wxString( current );
505 retval.
token = G_UNIT;
508 else if( ch ==
'\'' )
514 else if( isalpha( ch ) || ch ==
'_' )
516 current =
m_tokenizer.
GetChars( [](
int c ) ->
bool {
return isalnum( c ) || c ==
'_'; } );
517 retval.
token = G_IDENTIFIER;
518 retval.
value.
str =
new wxString( current );
523 retval.
token = G_EQUAL;
528 retval.
token = G_NOT_EQUAL;
533 retval.
token = G_LESS_EQUAL_THAN;
538 retval.
token = G_GREATER_EQUAL_THAN;
543 retval.
token = G_BOOL_AND;
548 retval.
token = G_BOOL_OR;
556 case '+': retval.
token = G_PLUS;
break;
557 case '!': retval.
token = G_BOOL_NOT;
break;
558 case '-': retval.
token = G_MINUS;
break;
559 case '*': retval.
token = G_MULT;
break;
560 case '/': retval.
token = G_DIVIDE;
break;
561 case '<': retval.
token = G_LESS_THAN;
break;
562 case '>': retval.
token = G_GREATER_THAN;
break;
563 case '(': retval.
token = G_PARENL;
break;
564 case ')': retval.
token = G_PARENR;
break;
565 case ';': retval.
token = G_SEMCOL;
break;
566 case '.': retval.
token = G_STRUCT_REF;
break;
567 case ',': retval.
token = G_COMMA;
break;
596 str.Printf(
"\n[%p L0:%-20p L1:%-20p] ", tok, tok->
leaf[0], tok->
leaf[1] );
599 for(
int i = 0; i < 2 * depth; i++ )
655 str.Printf(
"UNIT: %d ", tok->
value.
idx );
707 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue );
708 uop =
new UOP( aOp, std::move( val ) );
716 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue, aStringIsWildcard );
717 uop =
new UOP( aOp, std::move( val ) );
725 uop =
new UOP( aOp, std::move( aRef ) );
733 uop =
new UOP( aOp, std::move( aFunc ), std::move( aRef ) );
758 std::vector<TREE_NODE*> args;
767 args.push_back( root );
775 args.push_back(n->
leaf[1]);
786 std::reverse( args.begin(), args.end() );
788 for(
size_t i = 0; i < args.size(); i++ )
789 libeval_dbg( 10,
"squash arg%d: %s\n",
int( i ), *args[i]->value.str );
797 std::vector<TREE_NODE*> stack;
802 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( 1.0 );
810 stack.push_back(
m_tree );
817 while( !stack.empty() )
821 libeval_dbg( 4,
"process node %p [op %d] [stack %lu]\n",
822 node, node->
op, (
unsigned long)stack.size() );
848 pos -=
static_cast<int>( node->
leaf[0]->
value.
str->length() );
860 switch( node->
leaf[1]->
op )
869 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName, propName );
873 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
875 node->
leaf[0]->
srcPos - (
int) itemName.length() );
879 msg.Printf(
_(
"Unrecognized property '%s'" ), propName );
881 node->
leaf[1]->
srcPos - (
int) propName.length() );
899 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName,
"" );
903 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
905 node->
leaf[0]->
srcPos - (
int) itemName.length() );
912 libeval_dbg( 10,
"emit func call: %s\n", functionName );
916 msg.Printf(
_(
"Unrecognized function '%s'" ), functionName );
927 param->
Set( *pnode->value.str );
928 aPreflightContext->
Push( param );
932 [&](
const wxString& aMessage,
int aOffset )
940 func( aPreflightContext, vref.get() );
941 aPreflightContext->
Pop();
957 stack.push_back( node->
leaf[1] );
960 stack.push_back( pnode );
973 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName, propName );
977 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
979 node->
leaf[0]->
srcPos - (
int) itemName.length() );
982 msg.Printf(
_(
"Unrecognized property '%s'" ), propName );
1009 msg.Printf(
_(
"Unexpected units for '%s'" ), *node->
value.
str );
1021 msg.Printf(
_(
"Missing units for '%s'| (%s)" ),
1038 bool isWildcard = str.Contains(
"?") || str.Contains(
"*");
1050 msg.Printf(
_(
"Unrecognized item '%s'" ), *node->
value.
str );
1070 stack.push_back( node->
leaf[0] );
1076 stack.push_back( node->
leaf[1] );
1088 node->
uop =
nullptr;
1106 VALUE* value =
nullptr;
1133 double arg2Value = arg2 ? arg2->
AsDouble() : 0.0;
1134 double arg1Value = arg1 ? arg1->
AsDouble() : 0.0;
1141 ctx->
ReportError( wxString::Format(
_(
"Type mismatch between '%s' and %lf" ),
1147 ctx->
ReportError( wxString::Format(
_(
"Type mismatch between %lf and '%s'" ),
1156 result = arg1Value + arg2Value;
1159 result = arg1Value - arg2Value;
1162 result = arg1Value * arg2Value;
1165 result = arg1Value / arg2Value;
1168 result = arg1Value <= arg2Value ? 1 : 0;
1171 result = arg1Value >= arg2Value ? 1 : 0;
1174 result = arg1Value < arg2Value ? 1 : 0;
1177 result = arg1Value > arg2Value ? 1 : 0;
1180 if( !arg1 || !arg2 )
1181 result = arg1 == arg2 ? 1 : 0;
1183 result = arg2->
EqualTo( ctx, arg1 ) ? 1 : 0;
1185 result = arg1->
EqualTo( ctx, arg2 ) ? 1 : 0;
1188 if( !arg1 || !arg2 )
1189 result = arg1 != arg2 ? 1 : 0;
1191 result = arg2->
NotEqualTo( ctx, arg1 ) ? 1 : 0;
1193 result = arg1->
NotEqualTo( ctx, arg2 ) ? 1 : 0;
1196 result = arg1Value != 0.0 && arg2Value != 0.0 ? 1 : 0;
1199 result = arg1Value != 0.0 || arg2Value != 0.0 ? 1 : 0;
1214 double arg1Value = arg1 ? arg1->
AsDouble() : 0.0;
1220 result = arg1Value != 0.0 ? 0 : 1;
1223 result = arg1Value != 0.0 ? 1 : 0;
1237 static VALUE g_false( 0 );
1250 if( ctx->
SP() == 1 )
1260 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
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.