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;
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 );
387 aToken.
token = G_STRING;
388 aToken.
value.
str =
new wxString( str );
400 for(
const wxString& unitName :
m_unitResolver->GetSupportedUnits() )
404 libeval_dbg(10,
"Match unit '%s'\n", unitName.c_str() );
426 retval.
token = G_ENDS;
434 auto isDecimalSeparator =
435 [&]( wxUniChar ch ) ->
bool
444 bool haveSeparator =
false;
449 if( isDecimalSeparator( ch ) && haveSeparator )
452 current.append( 1, ch );
454 if( isDecimalSeparator( ch ) )
455 haveSeparator =
true;
459 }
while( isdigit( ch ) || isDecimalSeparator( ch ) );
462 for(
int i = current.length(); i; i-- )
464 if( isDecimalSeparator( current[i - 1] ) )
489 else if( isdigit( ch ) )
493 retval.
token = G_VALUE;
494 retval.
value.
str =
new wxString( current );
504 retval.
token = G_UNIT;
507 else if( ch ==
'\'' )
513 else if( isalpha( ch ) || ch ==
'_' )
515 current =
m_tokenizer.
GetChars( [](
int c ) ->
bool {
return isalnum( c ) || c ==
'_'; } );
516 retval.
token = G_IDENTIFIER;
517 retval.
value.
str =
new wxString( current );
522 retval.
token = G_EQUAL;
527 retval.
token = G_NOT_EQUAL;
532 retval.
token = G_LESS_EQUAL_THAN;
537 retval.
token = G_GREATER_EQUAL_THAN;
542 retval.
token = G_BOOL_AND;
547 retval.
token = G_BOOL_OR;
555 case '+': retval.
token = G_PLUS;
break;
556 case '!': retval.
token = G_BOOL_NOT;
break;
557 case '-': retval.
token = G_MINUS;
break;
558 case '*': retval.
token = G_MULT;
break;
559 case '/': retval.
token = G_DIVIDE;
break;
560 case '<': retval.
token = G_LESS_THAN;
break;
561 case '>': retval.
token = G_GREATER_THAN;
break;
562 case '(': retval.
token = G_PARENL;
break;
563 case ')': retval.
token = G_PARENR;
break;
564 case ';': retval.
token = G_SEMCOL;
break;
565 case '.': retval.
token = G_STRUCT_REF;
break;
566 case ',': retval.
token = G_COMMA;
break;
595 str.Printf(
"\n[%p L0:%-20p L1:%-20p] ", tok, tok->
leaf[0], tok->
leaf[1] );
598 for(
int i = 0; i < 2 * depth; i++ )
654 str.Printf(
"UNIT: %d ", tok->
value.
idx );
706 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue );
707 uop =
new UOP( aOp, std::move( val ) );
715 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue, aStringIsWildcard );
716 uop =
new UOP( aOp, std::move( val ) );
724 uop =
new UOP( aOp, std::move( aRef ) );
732 uop =
new UOP( aOp, std::move( aFunc ), std::move( aRef ) );
757 std::vector<TREE_NODE*> args;
766 args.push_back( root );
774 args.push_back(n->
leaf[1]);
785 std::reverse( args.begin(), args.end() );
787 for(
size_t i = 0; i < args.size(); i++ )
788 libeval_dbg( 10,
"squash arg%d: %s\n",
int( i ), *args[i]->value.str );
796 std::vector<TREE_NODE*> stack;
801 std::unique_ptr<VALUE> val = std::make_unique<VALUE>( 1.0 );
809 stack.push_back(
m_tree );
816 while( !stack.empty() )
820 libeval_dbg( 4,
"process node %p [op %d] [stack %lu]\n",
821 node, node->
op, (
unsigned long)stack.size() );
847 pos -=
static_cast<int>( node->
leaf[0]->
value.
str->length() );
859 switch( node->
leaf[1]->
op )
868 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName, propName );
872 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
874 node->
leaf[0]->
srcPos - (
int) itemName.length() );
878 msg.Printf(
_(
"Unrecognized property '%s'" ), propName );
880 node->
leaf[1]->
srcPos - (
int) propName.length() );
898 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName,
"" );
902 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
904 node->
leaf[0]->
srcPos - (
int) itemName.length() );
911 libeval_dbg( 10,
"emit func call: %s\n", functionName );
915 msg.Printf(
_(
"Unrecognized function '%s'" ), functionName );
926 param->
Set( *pnode->value.str );
927 aPreflightContext->
Push( param );
931 [&](
const wxString& aMessage,
int aOffset )
939 func( aPreflightContext, vref.get() );
940 aPreflightContext->
Pop();
956 stack.push_back( node->
leaf[1] );
959 stack.push_back( pnode );
972 std::unique_ptr<VAR_REF> vref = aCode->
CreateVarRef( itemName, propName );
976 msg.Printf(
_(
"Unrecognized item '%s'" ), itemName );
978 node->
leaf[0]->
srcPos - (
int) itemName.length() );
981 msg.Printf(
_(
"Unrecognized property '%s'" ), propName );
1008 msg.Printf(
_(
"Unexpected units for '%s'" ), *node->
value.
str );
1020 msg.Printf(
_(
"Missing units for '%s'| (%s)" ),
1037 bool isWildcard = str.Contains(
"?") || str.Contains(
"*");
1049 msg.Printf(
_(
"Unrecognized item '%s'" ), *node->
value.
str );
1069 stack.push_back( node->
leaf[0] );
1075 stack.push_back( node->
leaf[1] );
1087 node->
uop =
nullptr;
1105 VALUE* value =
nullptr;
1132 double arg2Value = arg2 ? arg2->
AsDouble() : 0.0;
1133 double arg1Value = arg1 ? arg1->
AsDouble() : 0.0;
1155 result = arg1Value + arg2Value;
1158 result = arg1Value - arg2Value;
1161 result = arg1Value * arg2Value;
1164 result = arg1Value / arg2Value;
1167 result = arg1Value <= arg2Value ? 1 : 0;
1170 result = arg1Value >= arg2Value ? 1 : 0;
1173 result = arg1Value < arg2Value ? 1 : 0;
1176 result = arg1Value > arg2Value ? 1 : 0;
1179 if( !arg1 || !arg2 )
1180 result = arg1 == arg2 ? 1 : 0;
1182 result = arg2->
EqualTo( ctx, arg1 ) ? 1 : 0;
1184 result = arg1->
EqualTo( ctx, arg2 ) ? 1 : 0;
1187 if( !arg1 || !arg2 )
1188 result = arg1 != arg2 ? 1 : 0;
1190 result = arg2->
NotEqualTo( ctx, arg1 ) ? 1 : 0;
1192 result = arg1->
NotEqualTo( ctx, arg2 ) ? 1 : 0;
1195 result = arg1Value != 0.0 && arg2Value != 0.0 ? 1 : 0;
1198 result = arg1Value != 0.0 || arg2Value != 0.0 ? 1 : 0;
1213 double arg1Value = arg1 ? arg1->
AsDouble() : 0.0;
1219 result = arg1Value != 0.0 ? 0 : 1;
1236 static VALUE g_false( 0 );
1249 if( ctx->
SP() == 1 )
1259 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)
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)
PARSE_RESULT Parse(const std::string &aString, NOTATION aNotation=NOTATION::SI, SIM_VALUE::TYPE aValueType=SIM_VALUE::TYPE_FLOAT)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
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.