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;
146 void SetUop(
int aOp,
double aValue );
147 void SetUop(
int aOp,
const wxString& aValue,
bool aStringIsWildcard );
148 void SetUop(
int aOp, std::unique_ptr<VAR_REF> aRef =
nullptr );
149 void SetUop(
int aOp,
FUNC_CALL_REF aFunc, std::unique_ptr<VAR_REF> aRef =
nullptr );
169 static const std::vector<wxString> nullUnits;
176 return wxEmptyString;
179 virtual double Convert(
const wxString& aString,
int unitType )
const
192 m_stringIsWildcard( false ),
193 m_isDeferredDbl( false ),
194 m_isDeferredStr( false )
197 VALUE(
const wxString& aStr,
bool aIsWildcard =
false ) :
201 m_stringIsWildcard( aIsWildcard ),
202 m_isDeferredDbl( false ),
203 m_isDeferredStr( false )
209 m_stringIsWildcard( false ),
210 m_isDeferredDbl( false ),
211 m_isDeferredStr( false )
226 if( m_isDeferredDbl )
228 m_valueDbl = m_lambdaDbl();
229 m_isDeferredDbl =
false;
237 if( m_isDeferredStr )
239 m_valueStr = m_lambdaStr();
240 m_isDeferredStr =
false;
246 virtual bool EqualTo(
CONTEXT* aCtx,
const VALUE* b )
const;
249 virtual bool NotEqualTo(
CONTEXT* aCtx,
const VALUE* b )
const;
262 m_lambdaDbl = aLambda;
263 m_isDeferredDbl =
true;
269 m_lambdaStr = aLambda;
270 m_isDeferredStr =
true;
273 void Set(
const wxString& aValue )
319 m_ownedValues.reserve( 20 );
324 for(
VALUE* v : m_ownedValues )
332 m_ownedValues.emplace_back(
new VALUE );
333 return m_ownedValues.back();
338 m_ownedValues.emplace_back( aValue );
339 return m_ownedValues.back();
344 m_stack[ m_stackPtr++ ] = v;
349 if( m_stackPtr == 0 )
351 ReportError(
_(
"Malformed expression" ) );
355 return m_stack[ --m_stackPtr ];
363 void SetErrorCallback( std::function<
void(
const wxString& aMessage,
int aOffset )> aCallback )
365 m_errorCallback = std::move( aCallback );
370 void ReportError(
const wxString& aErrorMsg );
388 m_ucode.push_back(uop);
392 wxString Dump()
const;
394 virtual std::unique_ptr<VAR_REF>
CreateVarRef(
const wxString& var,
const wxString& field )
413 UOP(
int op, std::unique_ptr<VALUE> value ) :
419 UOP(
int op, std::unique_ptr<VAR_REF> vref ) :
465 if( m_pos >= m_str.length() )
473 return m_pos >= m_str.length();
486 wxString GetString();
488 wxString GetChars(
const std::function<
bool( wxUniChar )>& cond )
const;
490 bool MatchAhead(
const wxString& match,
491 const std::function<
bool( wxUniChar )>& stopCond )
const;
512 void parseError(
const char* s );
520 bool Compile(
const wxString& aString,
UCODE* aCode,
CONTEXT* aPreflightContext );
522 void SetErrorCallback( std::function<
void(
const wxString& aMessage,
int aOffset )> aCallback )
524 m_errorCallback = std::move( aCallback );
531 void GcItem( wxString* aItem ) { m_gcStrings.push_back( aItem ); }
542 bool generateUCode(
UCODE* aCode,
CONTEXT* aPreflightContext );
544 void reportError(
COMPILATION_STAGE stage,
const wxString& aErrorMsg,
int aPos = -1 );
547 void newString(
const wxString& aString );
551 bool lexDefault(
T_TOKEN& aToken );
552 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)
static VALUE * MakeNullValue()
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.