21#ifndef __LIBEVAL_COMPILER_H
22#define __LIBEVAL_COMPILER_H
39#define TR_OP_BINARY_MASK 0x200
40#define TR_OP_UNARY_MASK 0x100
42#define TR_OP_MUL 0x201
43#define TR_OP_DIV 0x202
44#define TR_OP_ADD 0x203
45#define TR_OP_SUB 0x204
46#define TR_OP_LESS 0x205
47#define TR_OP_GREATER 0x206
48#define TR_OP_LESS_EQUAL 0x207
49#define TR_OP_GREATER_EQUAL 0x208
50#define TR_OP_EQUAL 0x209
51#define TR_OP_NOT_EQUAL 0x20a
52#define TR_OP_BOOL_AND 0x20b
53#define TR_OP_BOOL_OR 0x20c
54#define TR_OP_BOOL_NOT 0x100
55#define TR_OP_FUNC_CALL 24
56#define TR_OP_METHOD_CALL 25
57#define TR_UOP_PUSH_VAR 1
58#define TR_UOP_PUSH_VALUE 2
75 bool pendingError =
false;
145 void SetUop(
int aOp,
double aValue );
146 void SetUop(
int aOp,
const wxString& aValue,
bool aStringIsWildcard );
147 void SetUop(
int aOp, std::unique_ptr<VAR_REF> aRef =
nullptr );
148 void SetUop(
int aOp,
FUNC_CALL_REF aFunc, std::unique_ptr<VAR_REF> aRef =
nullptr );
168 static const std::vector<wxString> nullUnits;
175 return wxEmptyString;
178 virtual double Convert(
const wxString& aString,
int unitType )
const
191 m_stringIsWildcard( false ),
192 m_isDeferredDbl( false ),
193 m_isDeferredStr( false )
196 VALUE(
const wxString& aStr,
bool aIsWildcard =
false ) :
200 m_stringIsWildcard( aIsWildcard ),
201 m_isDeferredDbl( false ),
202 m_isDeferredStr( false )
208 m_stringIsWildcard( false ),
209 m_isDeferredDbl( false ),
210 m_isDeferredStr( false )
218 if( m_isDeferredDbl )
220 m_valueDbl = m_lambdaDbl();
221 m_isDeferredDbl =
false;
229 if( m_isDeferredStr )
231 m_valueStr = m_lambdaStr();
232 m_isDeferredStr =
false;
238 virtual bool EqualTo(
CONTEXT* aCtx,
const VALUE* b )
const;
241 virtual bool NotEqualTo(
CONTEXT* aCtx,
const VALUE* b )
const;
254 m_lambdaDbl = aLambda;
255 m_isDeferredDbl =
true;
261 m_lambdaStr = aLambda;
262 m_isDeferredStr =
true;
265 void Set(
const wxString& aValue )
311 m_ownedValues.reserve( 20 );
316 for(
VALUE* v : m_ownedValues )
324 m_ownedValues.emplace_back(
new VALUE );
325 return m_ownedValues.back();
330 m_ownedValues.emplace_back( aValue );
331 return m_ownedValues.back();
336 m_stack[ m_stackPtr++ ] = v;
341 if( m_stackPtr == 0 )
343 ReportError(
_(
"Malformed expression" ) );
347 return m_stack[ --m_stackPtr ];
355 void SetErrorCallback( std::function<
void(
const wxString& aMessage,
int aOffset )> aCallback )
357 m_errorCallback = std::move( aCallback );
362 void ReportError(
const wxString& aErrorMsg );
380 m_ucode.push_back(uop);
384 wxString Dump()
const;
386 virtual std::unique_ptr<VAR_REF>
CreateVarRef(
const wxString& var,
const wxString& field )
405 UOP(
int op, std::unique_ptr<VALUE> value ) :
411 UOP(
int op, std::unique_ptr<VAR_REF> vref ) :
457 if( m_pos >= m_str.length() )
465 return m_pos >= m_str.length();
478 wxString GetString();
480 wxString GetChars(
const std::function<
bool( wxUniChar )>& cond )
const;
482 bool MatchAhead(
const wxString& match,
483 const std::function<
bool( wxUniChar )>& stopCond )
const;
504 void parseError(
const char* s );
512 bool Compile(
const wxString& aString,
UCODE* aCode,
CONTEXT* aPreflightContext );
514 void SetErrorCallback( std::function<
void(
const wxString& aMessage,
int aOffset )> aCallback )
516 m_errorCallback = std::move( aCallback );
523 void GcItem( wxString* aItem ) { m_gcStrings.push_back( aItem ); }
534 bool generateUCode(
UCODE* aCode,
CONTEXT* aPreflightContext );
536 void reportError(
COMPILATION_STAGE stage,
const wxString& aErrorMsg,
int aPos = -1 );
539 void newString(
const wxString& aString );
543 bool lexDefault(
T_TOKEN& aToken );
544 bool lexString(
T_TOKEN& aToken );
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
bool IsErrorPending() const
std::unique_ptr< UNIT_RESOLVER > m_unitResolver
void GcItem(wxString *aItem)
void GcItem(TREE_NODE *aItem)
char m_localeDecimalSeparator
std::function< void(const wxString &aMessage, int aOffset)> m_errorCallback
std::vector< TREE_NODE * > m_gcItems
const ERROR_STATUS & GetError() const
std::vector< wxString * > m_gcStrings
ERROR_STATUS m_errorStatus
std::vector< VALUE * > m_ownedValues
VALUE * StoreValue(VALUE *aValue)
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)
virtual std::unique_ptr< VAR_REF > CreateVarRef(const wxString &var, const wxString &field)
std::vector< UOP * > m_ucode
virtual FUNC_CALL_REF CreateFuncCall(const wxString &name)
virtual wxString GetSupportedUnitsMessage() const
virtual double Convert(const wxString &aString, int unitType) const
virtual const std::vector< wxString > & GetSupportedUnits() const
UOP(int op, FUNC_CALL_REF func, std::unique_ptr< VAR_REF > vref=nullptr)
std::unique_ptr< VAR_REF > m_ref
UOP(int op, std::unique_ptr< VALUE > value)
std::unique_ptr< VALUE > m_value
UOP(int op, std::unique_ptr< VAR_REF > vref)
virtual const wxString & AsString() const
void SetDeferredEval(std::function< wxString()> aLambda)
void Set(const wxString &aValue)
std::function< double()> m_lambdaDbl
virtual double AsDouble() const
VAR_TYPE_T GetType() const
void Set(const VALUE &val)
void SetDeferredEval(std::function< double()> aLambda)
std::function< wxString()> m_lambdaStr
VALUE(const wxString &aStr, bool aIsWildcard=false)
virtual VALUE * GetValue(CONTEXT *aCtx)=0
virtual VAR_TYPE_T GetType() const =0
TREE_NODE * newNode(LIBEVAL::COMPILER *compiler, int op, const T_TOKEN_VALUE &value)
constexpr T_TOKEN defaultToken
constexpr T_TOKEN_VALUE defaultTokenValue
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.