2020-06-04 11:04:03 +00:00
|
|
|
/*
|
2021-07-08 12:33:08 +00:00
|
|
|
* This file is part of libeval, a simple math expression evaluator
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Michael Geselbracht, mgeselbracht3@gmail.com
|
2023-03-04 18:02:05 +00:00
|
|
|
* Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-07-08 12:33:08 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
#include <memory>
|
2020-06-04 11:04:03 +00:00
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
2020-09-22 12:56:54 +00:00
|
|
|
#include <algorithm>
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2022-09-17 01:07:36 +00:00
|
|
|
#include <eda_units.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2021-06-01 05:17:57 +00:00
|
|
|
#include <wx/log.h>
|
2020-09-23 21:44:50 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
#ifdef DEBUG
|
2020-07-30 19:33:41 +00:00
|
|
|
#include <cstdarg>
|
2020-06-04 11:04:03 +00:00
|
|
|
#endif
|
|
|
|
|
2020-06-13 23:19:22 +00:00
|
|
|
#include <libeval_compiler/libeval_compiler.h>
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
/* The (generated) lemon parser is written in C.
|
|
|
|
* In order to keep its symbol from the global namespace include the parser code with
|
|
|
|
* a C++ namespace.
|
|
|
|
*/
|
|
|
|
namespace LIBEVAL
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-variable"
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-compare"
|
|
|
|
#endif
|
|
|
|
|
2020-07-30 19:33:41 +00:00
|
|
|
#include <libeval_compiler/grammar.c>
|
|
|
|
#include <libeval_compiler/grammar.h>
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
|
|
|
|
#define libeval_dbg(level, fmt, ...) \
|
2020-08-10 12:47:01 +00:00
|
|
|
wxLogTrace( "libeval_compiler", fmt, __VA_ARGS__ );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
TREE_NODE* newNode( LIBEVAL::COMPILER* compiler, int op, const T_TOKEN_VALUE& value )
|
2020-08-08 22:39:05 +00:00
|
|
|
{
|
|
|
|
auto t2 = new TREE_NODE();
|
2020-08-10 12:47:01 +00:00
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
t2->valid = true;
|
2020-08-10 12:47:01 +00:00
|
|
|
t2->value.str = value.str ? new wxString( *value.str ) : nullptr;
|
|
|
|
t2->value.num = value.num;
|
|
|
|
t2->value.idx = value.idx;
|
2020-08-08 22:39:05 +00:00
|
|
|
t2->op = op;
|
|
|
|
t2->leaf[0] = nullptr;
|
|
|
|
t2->leaf[1] = nullptr;
|
|
|
|
t2->isTerminal = false;
|
2020-08-10 12:47:01 +00:00
|
|
|
t2->srcPos = compiler->GetSourcePos();
|
2020-08-08 22:39:05 +00:00
|
|
|
t2->uop = nullptr;
|
2020-08-10 12:47:01 +00:00
|
|
|
|
2020-08-10 14:19:18 +00:00
|
|
|
libeval_dbg(10, " ostr %p nstr %p nnode %p op %d", value.str, t2->value.str, t2, t2->op );
|
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
if(t2->value.str)
|
|
|
|
compiler->GcItem( t2->value.str );
|
|
|
|
|
|
|
|
compiler->GcItem( t2 );
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
return t2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const wxString formatOpName( int op )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
int op;
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString mnemonic;
|
2020-07-19 21:22:49 +00:00
|
|
|
}
|
|
|
|
simpleOps[] =
|
|
|
|
{
|
|
|
|
{ TR_OP_MUL, "MUL" }, { TR_OP_DIV, "DIV" }, { TR_OP_ADD, "ADD" },
|
2020-06-04 11:04:03 +00:00
|
|
|
{ TR_OP_SUB, "SUB" }, { TR_OP_LESS, "LESS" }, { TR_OP_GREATER, "GREATER" },
|
|
|
|
{ TR_OP_LESS_EQUAL, "LESS_EQUAL" }, { TR_OP_GREATER_EQUAL, "GREATER_EQUAL" },
|
|
|
|
{ TR_OP_EQUAL, "EQUAL" }, { TR_OP_NOT_EQUAL, "NEQUAL" }, { TR_OP_BOOL_AND, "AND" },
|
2020-07-19 21:22:49 +00:00
|
|
|
{ TR_OP_BOOL_OR, "OR" }, { TR_OP_BOOL_NOT, "NOT" }, { -1, "" }
|
|
|
|
};
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
for( int i = 0; simpleOps[i].op >= 0; i++ )
|
|
|
|
{
|
|
|
|
if( simpleOps[i].op == op )
|
|
|
|
return simpleOps[i].mnemonic;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2020-11-15 17:48:25 +00:00
|
|
|
|
2021-08-16 09:53:27 +00:00
|
|
|
bool VALUE::EqualTo( CONTEXT* aCtx, const VALUE* b ) const
|
2020-09-23 21:44:50 +00:00
|
|
|
{
|
2020-11-15 17:48:25 +00:00
|
|
|
if( m_type == VT_UNDEFINED || b->m_type == VT_UNDEFINED )
|
|
|
|
return false;
|
|
|
|
|
2020-09-23 21:44:50 +00:00
|
|
|
if( m_type == VT_NUMERIC && b->m_type == VT_NUMERIC )
|
2020-11-15 17:48:25 +00:00
|
|
|
{
|
2021-11-08 14:22:18 +00:00
|
|
|
return AsDouble() == b->AsDouble();
|
2020-11-15 17:48:25 +00:00
|
|
|
}
|
2020-09-23 21:44:50 +00:00
|
|
|
else if( m_type == VT_STRING && b->m_type == VT_STRING )
|
|
|
|
{
|
|
|
|
if( b->m_stringIsWildcard )
|
2021-11-08 14:22:18 +00:00
|
|
|
return WildCompareString( b->AsString(), AsString(), false );
|
2020-09-23 21:44:50 +00:00
|
|
|
else
|
2022-11-16 21:09:46 +00:00
|
|
|
return AsString().IsSameAs( b->AsString(), false );
|
2020-09-23 21:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2021-08-16 09:53:27 +00:00
|
|
|
bool VALUE::NotEqualTo( CONTEXT* aCtx, const VALUE* b ) const
|
2020-11-15 17:48:25 +00:00
|
|
|
{
|
|
|
|
if( m_type == VT_UNDEFINED || b->m_type == VT_UNDEFINED )
|
|
|
|
return false;
|
|
|
|
|
2021-08-16 09:53:27 +00:00
|
|
|
return !EqualTo( aCtx, b );
|
2020-11-15 17:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString UOP::Format() const
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString str;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
switch( m_op )
|
|
|
|
{
|
|
|
|
case TR_UOP_PUSH_VAR:
|
2020-08-10 12:47:01 +00:00
|
|
|
str = wxString::Format( "PUSH VAR [%p]", m_ref.get() );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
case TR_UOP_PUSH_VALUE:
|
|
|
|
{
|
2020-08-10 12:47:01 +00:00
|
|
|
if( !m_value )
|
2020-08-08 22:39:05 +00:00
|
|
|
str = wxString::Format( "PUSH nullptr" );
|
2020-08-10 12:47:01 +00:00
|
|
|
else if( m_value->GetType() == VT_NUMERIC )
|
|
|
|
str = wxString::Format( "PUSH NUM [%.10f]", m_value->AsDouble() );
|
2020-06-04 11:04:03 +00:00
|
|
|
else
|
2020-10-16 00:39:55 +00:00
|
|
|
str = wxString::Format( "PUSH STR [%ls]", m_value->AsString() );
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
break;
|
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
case TR_OP_METHOD_CALL:
|
2020-08-08 22:39:05 +00:00
|
|
|
str = wxString::Format( "MCALL" );
|
2020-06-17 22:36:02 +00:00
|
|
|
break;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
case TR_OP_FUNC_CALL:
|
2020-08-08 22:39:05 +00:00
|
|
|
str = wxString::Format( "FCALL" );
|
2020-06-17 22:36:02 +00:00
|
|
|
break;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
default:
|
2020-08-08 22:39:05 +00:00
|
|
|
str = wxString::Format( "%s %d", formatOpName( m_op ).c_str(), m_op );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-07-24 14:16:15 +00:00
|
|
|
UCODE::~UCODE()
|
|
|
|
{
|
|
|
|
for ( auto op : m_ucode )
|
2020-08-10 12:47:01 +00:00
|
|
|
{
|
2020-07-24 14:16:15 +00:00
|
|
|
delete op;
|
2020-08-10 12:47:01 +00:00
|
|
|
}
|
2020-07-24 14:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString UCODE::Dump() const
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString rv;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
for( auto op : m_ucode )
|
|
|
|
{
|
|
|
|
rv += op->Format();
|
|
|
|
rv += "\n";
|
|
|
|
}
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
return rv;
|
|
|
|
};
|
|
|
|
|
2020-07-22 13:08:57 +00:00
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
wxString TOKENIZER::GetChars( const std::function<bool( wxUniChar )>& cond ) const
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString rv;
|
2020-06-04 11:04:03 +00:00
|
|
|
size_t p = m_pos;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
while( p < m_str.length() && cond( m_str[p] ) )
|
|
|
|
{
|
|
|
|
rv.append( 1, m_str[p] );
|
|
|
|
p++;
|
|
|
|
}
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
bool TOKENIZER::MatchAhead( const wxString& match,
|
|
|
|
const std::function<bool( wxUniChar )>& stopCond ) const
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-14 11:34:44 +00:00
|
|
|
int remaining = (int) m_str.Length() - m_pos;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
if( remaining < (int) match.length() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( m_str.substr( m_pos, match.length() ) == match )
|
|
|
|
return ( remaining == (int) match.length() || stopCond( m_str[m_pos + match.length()] ) );
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
COMPILER::COMPILER() :
|
|
|
|
m_lexerState( COMPILER::LS_DEFAULT )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
m_localeDecimalSeparator = '.';
|
2020-07-19 21:22:49 +00:00
|
|
|
m_sourcePos = 0;
|
|
|
|
m_parseFinished = false;
|
|
|
|
m_unitResolver = std::make_unique<UNIT_RESOLVER>();
|
2020-06-04 11:04:03 +00:00
|
|
|
m_parser = LIBEVAL::ParseAlloc( malloc );
|
2020-07-20 12:51:17 +00:00
|
|
|
m_tree = nullptr;
|
2020-08-08 22:39:05 +00:00
|
|
|
m_errorStatus.pendingError = false;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
COMPILER::~COMPILER()
|
|
|
|
{
|
|
|
|
LIBEVAL::ParseFree( m_parser, free );
|
|
|
|
|
2020-09-27 15:49:26 +00:00
|
|
|
if( m_tree )
|
|
|
|
{
|
|
|
|
freeTree( m_tree );
|
2022-11-22 14:09:19 +00:00
|
|
|
m_tree = nullptr;
|
2020-09-27 15:49:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
// Allow explicit call to destructor
|
|
|
|
m_parser = nullptr;
|
|
|
|
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPILER::Clear()
|
|
|
|
{
|
|
|
|
//free( current.token );
|
|
|
|
m_tokenizer.Clear();
|
2020-07-18 20:33:23 +00:00
|
|
|
|
2020-07-24 14:16:15 +00:00
|
|
|
if( m_tree )
|
|
|
|
{
|
|
|
|
freeTree( m_tree );
|
2020-09-27 15:49:26 +00:00
|
|
|
m_tree = nullptr;
|
2020-07-24 14:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_tree = nullptr;
|
2020-08-10 12:47:01 +00:00
|
|
|
|
|
|
|
for( auto tok : m_gcItems )
|
|
|
|
delete tok;
|
2020-08-13 14:15:43 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
for( auto tok: m_gcStrings )
|
|
|
|
delete tok;
|
|
|
|
|
|
|
|
m_gcItems.clear();
|
|
|
|
m_gcStrings.clear();
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPILER::parseError( const char* s )
|
|
|
|
{
|
2020-08-08 22:39:05 +00:00
|
|
|
reportError( CST_PARSE, s );
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPILER::parseOk()
|
|
|
|
{
|
|
|
|
m_parseFinished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
bool COMPILER::Compile( const wxString& aString, UCODE* aCode, CONTEXT* aPreflightContext )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
// Feed parser token after token until end of input.
|
|
|
|
|
|
|
|
newString( aString );
|
2020-07-24 14:16:15 +00:00
|
|
|
|
|
|
|
if( m_tree )
|
|
|
|
{
|
|
|
|
freeTree( m_tree );
|
2020-09-27 15:49:26 +00:00
|
|
|
m_tree = nullptr;
|
2020-07-24 14:16:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
m_tree = nullptr;
|
2020-06-04 11:04:03 +00:00
|
|
|
m_parseFinished = false;
|
2020-08-14 11:34:44 +00:00
|
|
|
T_TOKEN tok( defaultToken );
|
2020-08-10 12:47:01 +00:00
|
|
|
|
2020-06-13 23:19:22 +00:00
|
|
|
libeval_dbg(0, "str: '%s' empty: %d\n", aString.c_str(), !!aString.empty() );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
if( aString.empty() )
|
|
|
|
{
|
|
|
|
m_parseFinished = true;
|
2020-07-23 20:47:38 +00:00
|
|
|
return generateUCode( aCode, aPreflightContext );
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
m_sourcePos = m_tokenizer.GetPos();
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
tok = getToken();
|
2020-08-10 12:47:01 +00:00
|
|
|
|
|
|
|
if( tok.value.str )
|
|
|
|
GcItem( tok.value.str );
|
|
|
|
|
2020-08-10 14:19:18 +00:00
|
|
|
libeval_dbg(10, "parse: tok %d valstr %p\n", tok.token, tok.value.str );
|
2020-08-10 12:47:01 +00:00
|
|
|
Parse( m_parser, tok.token, tok, this );
|
2020-07-18 20:33:23 +00:00
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
if ( m_errorStatus.pendingError )
|
|
|
|
return false;
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
if( m_parseFinished || tok.token == G_ENDS )
|
|
|
|
{
|
|
|
|
// Reset parser by passing zero as token ID, value is ignored.
|
2020-08-10 12:47:01 +00:00
|
|
|
Parse( m_parser, 0, tok, this );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while( tok.token );
|
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
return generateUCode( aCode, aPreflightContext );
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
void COMPILER::newString( const wxString& aString )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
m_lexerState = LS_DEFAULT;
|
|
|
|
m_tokenizer.Restart( aString );
|
|
|
|
m_parseFinished = false;
|
|
|
|
}
|
|
|
|
|
2023-03-04 18:02:05 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
T_TOKEN COMPILER::getToken()
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
T_TOKEN rv;
|
2023-03-04 18:02:05 +00:00
|
|
|
rv.value = defaultTokenValue;
|
2020-08-10 12:47:01 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
bool done = false;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
switch( m_lexerState )
|
|
|
|
{
|
|
|
|
case LS_DEFAULT:
|
|
|
|
done = lexDefault( rv );
|
|
|
|
break;
|
|
|
|
case LS_STRING:
|
|
|
|
done = lexString( rv );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while( !done );
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
bool COMPILER::lexString( T_TOKEN& aToken )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString str = m_tokenizer.GetChars( []( int c ) -> bool { return c != '\''; } );
|
2020-08-13 14:15:43 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
aToken.token = G_STRING;
|
2020-08-10 12:47:01 +00:00
|
|
|
aToken.value.str = new wxString( str );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
m_tokenizer.NextChar( str.length() + 1 );
|
|
|
|
m_lexerState = LS_DEFAULT;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int COMPILER::resolveUnits()
|
|
|
|
{
|
|
|
|
int unitId = 0;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
for( const wxString& unitName : m_unitResolver->GetSupportedUnits() )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
if( m_tokenizer.MatchAhead( unitName, []( int c ) -> bool { return !isalnum( c ); } ) )
|
|
|
|
{
|
2020-06-13 23:19:22 +00:00
|
|
|
libeval_dbg(10, "Match unit '%s'\n", unitName.c_str() );
|
2020-06-04 11:04:03 +00:00
|
|
|
m_tokenizer.NextChar( unitName.length() );
|
|
|
|
return unitId;
|
|
|
|
}
|
|
|
|
|
|
|
|
unitId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
bool COMPILER::lexDefault( T_TOKEN& aToken )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-15 16:30:55 +00:00
|
|
|
T_TOKEN retval;
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString current;
|
2020-08-15 16:30:55 +00:00
|
|
|
int convertFrom;
|
|
|
|
wxString msg;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
retval.value.str = nullptr;
|
2021-07-08 12:33:08 +00:00
|
|
|
retval.value.num = 0.0;
|
2021-07-09 14:56:17 +00:00
|
|
|
retval.value.idx = -1;
|
2020-06-04 11:04:03 +00:00
|
|
|
retval.token = G_ENDS;
|
|
|
|
|
|
|
|
if( m_tokenizer.Done() )
|
|
|
|
{
|
|
|
|
aToken = retval;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
auto isDecimalSeparator =
|
2020-08-08 22:39:05 +00:00
|
|
|
[&]( wxUniChar ch ) -> bool
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
|
|
|
return ( ch == m_localeDecimalSeparator || ch == '.' || ch == ',' );
|
|
|
|
};
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
// Lambda: get value as string, store into clToken.token and update current index.
|
2020-07-19 21:22:49 +00:00
|
|
|
auto extractNumber =
|
|
|
|
[&]()
|
|
|
|
{
|
2020-08-15 16:30:55 +00:00
|
|
|
bool haveSeparator = false;
|
|
|
|
wxUniChar ch = m_tokenizer.GetChar();
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if( isDecimalSeparator( ch ) && haveSeparator )
|
|
|
|
break;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
current.append( 1, ch );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
if( isDecimalSeparator( ch ) )
|
|
|
|
haveSeparator = true;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
m_tokenizer.NextChar();
|
|
|
|
ch = m_tokenizer.GetChar();
|
|
|
|
} while( isdigit( ch ) || isDecimalSeparator( ch ) );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
// Ensure that the systems decimal separator is used
|
|
|
|
for( int i = current.length(); i; i-- )
|
|
|
|
{
|
|
|
|
if( isDecimalSeparator( current[i - 1] ) )
|
|
|
|
current[i - 1] = m_localeDecimalSeparator;
|
|
|
|
}
|
|
|
|
};
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
// Start processing of first/next token: Remove whitespace
|
|
|
|
for( ;; )
|
|
|
|
{
|
|
|
|
ch = m_tokenizer.GetChar();
|
|
|
|
|
|
|
|
if( ch == ' ' )
|
|
|
|
m_tokenizer.NextChar();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:51:17 +00:00
|
|
|
libeval_dbg(10, "LEX ch '%c' pos %lu\n", ch, (unsigned long)m_tokenizer.GetPos() );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
if( ch == 0 )
|
|
|
|
{
|
|
|
|
/* End of input */
|
|
|
|
}
|
|
|
|
else if( isdigit( ch ) )
|
|
|
|
{
|
|
|
|
// VALUE
|
|
|
|
extractNumber();
|
|
|
|
retval.token = G_VALUE;
|
2020-08-10 12:47:01 +00:00
|
|
|
retval.value.str = new wxString( current );
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
else if( ( convertFrom = resolveUnits() ) >= 0 )
|
|
|
|
{
|
|
|
|
// UNIT
|
|
|
|
// Units are appended to a VALUE.
|
|
|
|
// Determine factor to default unit if unit for value is given.
|
|
|
|
// Example: Default is mm, unit is inch: factor is 25.4
|
|
|
|
// The factor is assigned to the terminal UNIT. The actual
|
|
|
|
// conversion is done within a parser action.
|
|
|
|
retval.token = G_UNIT;
|
2020-08-10 12:47:01 +00:00
|
|
|
retval.value.idx = convertFrom;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
else if( ch == '\'' ) // string literal
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
m_lexerState = LS_STRING;
|
|
|
|
m_tokenizer.NextChar();
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-23 10:34:45 +00:00
|
|
|
else if( isalpha( ch ) || ch == '_' )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-07-23 10:34:45 +00:00
|
|
|
current = m_tokenizer.GetChars( []( int c ) -> bool { return isalnum( c ) || c == '_'; } );
|
2020-06-04 11:04:03 +00:00
|
|
|
retval.token = G_IDENTIFIER;
|
2020-08-10 12:47:01 +00:00
|
|
|
retval.value.str = new wxString( current );
|
2020-06-04 11:04:03 +00:00
|
|
|
m_tokenizer.NextChar( current.length() );
|
|
|
|
}
|
|
|
|
else if( m_tokenizer.MatchAhead( "==", []( int c ) -> bool { return c != '='; } ) )
|
|
|
|
{
|
|
|
|
retval.token = G_EQUAL;
|
2020-07-18 20:33:23 +00:00
|
|
|
m_tokenizer.NextChar( 2 );
|
2020-06-16 19:48:47 +00:00
|
|
|
}
|
|
|
|
else if( m_tokenizer.MatchAhead( "!=", []( int c ) -> bool { return c != '='; } ) )
|
|
|
|
{
|
|
|
|
retval.token = G_NOT_EQUAL;
|
2020-06-04 11:04:03 +00:00
|
|
|
m_tokenizer.NextChar( 2 );
|
|
|
|
}
|
|
|
|
else if( m_tokenizer.MatchAhead( "<=", []( int c ) -> bool { return c != '='; } ) )
|
|
|
|
{
|
|
|
|
retval.token = G_LESS_EQUAL_THAN;
|
|
|
|
m_tokenizer.NextChar( 2 );
|
|
|
|
}
|
|
|
|
else if( m_tokenizer.MatchAhead( ">=", []( int c ) -> bool { return c != '='; } ) )
|
|
|
|
{
|
|
|
|
retval.token = G_GREATER_EQUAL_THAN;
|
|
|
|
m_tokenizer.NextChar( 2 );
|
|
|
|
}
|
|
|
|
else if( m_tokenizer.MatchAhead( "&&", []( int c ) -> bool { return c != '&'; } ) )
|
|
|
|
{
|
|
|
|
retval.token = G_BOOL_AND;
|
|
|
|
m_tokenizer.NextChar( 2 );
|
|
|
|
}
|
|
|
|
else if( m_tokenizer.MatchAhead( "||", []( int c ) -> bool { return c != '|'; } ) )
|
|
|
|
{
|
|
|
|
retval.token = G_BOOL_OR;
|
|
|
|
m_tokenizer.NextChar( 2 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Single char tokens
|
|
|
|
switch( ch )
|
|
|
|
{
|
2020-07-22 13:31:11 +00:00
|
|
|
case '+': retval.token = G_PLUS; break;
|
|
|
|
case '!': retval.token = G_BOOL_NOT; break;
|
|
|
|
case '-': retval.token = G_MINUS; break;
|
|
|
|
case '*': retval.token = G_MULT; break;
|
|
|
|
case '/': retval.token = G_DIVIDE; break;
|
|
|
|
case '<': retval.token = G_LESS_THAN; break;
|
|
|
|
case '>': retval.token = G_GREATER_THAN; break;
|
|
|
|
case '(': retval.token = G_PARENL; break;
|
|
|
|
case ')': retval.token = G_PARENR; break;
|
|
|
|
case ';': retval.token = G_SEMCOL; break;
|
|
|
|
case '.': retval.token = G_STRUCT_REF; break;
|
2020-09-22 12:56:54 +00:00
|
|
|
case ',': retval.token = G_COMMA; break;
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
default:
|
2020-10-16 23:14:59 +00:00
|
|
|
reportError( CST_PARSE, wxString::Format( _( "Unrecognized character '%c'" ),
|
|
|
|
(char) ch ) );
|
2020-07-22 13:31:11 +00:00
|
|
|
break;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_tokenizer.NextChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
aToken = retval;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-30 11:24:29 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
const wxString formatNode( TREE_NODE* node )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2023-02-12 21:29:26 +00:00
|
|
|
return node->value.str ? *(node->value.str) : wxString( wxEmptyString );
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
void dumpNode( wxString& buf, TREE_NODE* tok, int depth = 0 )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString str;
|
|
|
|
|
2020-08-26 22:00:42 +00:00
|
|
|
if( !tok )
|
|
|
|
return;
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
str.Printf( "\n[%p L0:%-20p L1:%-20p] ", tok, tok->leaf[0], tok->leaf[1] );
|
2020-06-04 11:04:03 +00:00
|
|
|
buf += str;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
for( int i = 0; i < 2 * depth; i++ )
|
|
|
|
buf += " ";
|
|
|
|
|
|
|
|
if( tok->op & TR_OP_BINARY_MASK )
|
|
|
|
{
|
2020-07-18 20:33:23 +00:00
|
|
|
buf += formatOpName( tok->op );
|
2020-06-04 11:04:03 +00:00
|
|
|
dumpNode( buf, tok->leaf[0], depth + 1 );
|
|
|
|
dumpNode( buf, tok->leaf[1], depth + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( tok->op )
|
|
|
|
{
|
|
|
|
case TR_NUMBER:
|
2020-07-18 20:33:23 +00:00
|
|
|
buf += "NUMERIC: ";
|
|
|
|
buf += formatNode( tok );
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
if( tok->leaf[0] )
|
|
|
|
dumpNode( buf, tok->leaf[0], depth + 1 );
|
2020-07-18 20:33:23 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-09-22 12:56:54 +00:00
|
|
|
case TR_ARG_LIST:
|
|
|
|
buf += "ARG_LIST: ";
|
|
|
|
buf += formatNode( tok );
|
|
|
|
|
|
|
|
if( tok->leaf[0] )
|
|
|
|
dumpNode( buf, tok->leaf[0], depth + 1 );
|
|
|
|
if( tok->leaf[1] )
|
|
|
|
dumpNode( buf, tok->leaf[1], depth + 1 );
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
case TR_STRING:
|
2020-07-18 20:33:23 +00:00
|
|
|
buf += "STRING: ";
|
|
|
|
buf += formatNode( tok );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
case TR_IDENTIFIER:
|
2020-07-18 20:33:23 +00:00
|
|
|
buf += "ID: ";
|
|
|
|
buf += formatNode( tok );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
case TR_STRUCT_REF:
|
2020-07-18 20:33:23 +00:00
|
|
|
buf += "SREF: ";
|
2020-06-04 11:04:03 +00:00
|
|
|
dumpNode( buf, tok->leaf[0], depth + 1 );
|
|
|
|
dumpNode( buf, tok->leaf[1], depth + 1 );
|
|
|
|
break;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-16 19:48:47 +00:00
|
|
|
case TR_OP_FUNC_CALL:
|
2020-07-18 20:33:23 +00:00
|
|
|
buf += "CALL '";
|
2023-06-18 19:27:54 +00:00
|
|
|
buf += formatNode( tok->leaf[0] );
|
2020-07-18 20:33:23 +00:00
|
|
|
buf += "': ";
|
2020-06-16 19:48:47 +00:00
|
|
|
dumpNode( buf, tok->leaf[1], depth + 1 );
|
|
|
|
break;
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
case TR_UNIT:
|
2020-08-10 12:47:01 +00:00
|
|
|
str.Printf( "UNIT: %d ", tok->value.idx );
|
2020-06-04 11:04:03 +00:00
|
|
|
buf += str;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
void CONTEXT::ReportError( const wxString& aErrorMsg )
|
|
|
|
{
|
|
|
|
if( m_errorCallback )
|
2021-05-20 22:05:27 +00:00
|
|
|
m_errorCallback( aErrorMsg, -1 );
|
2020-08-08 22:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPILER::reportError( COMPILATION_STAGE stage, const wxString& aErrorMsg, int aPos )
|
2020-06-16 19:48:47 +00:00
|
|
|
{
|
2020-07-30 11:24:29 +00:00
|
|
|
if( aPos == -1 )
|
|
|
|
aPos = m_sourcePos;
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
m_errorStatus.pendingError = true;
|
|
|
|
m_errorStatus.stage = stage;
|
|
|
|
m_errorStatus.message = aErrorMsg;
|
|
|
|
m_errorStatus.srcPos = aPos;
|
|
|
|
|
|
|
|
if( m_errorCallback )
|
2020-08-13 17:47:41 +00:00
|
|
|
m_errorCallback( aErrorMsg, aPos );
|
2020-06-16 19:48:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
void COMPILER::setRoot( TREE_NODE *root )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-10 12:47:01 +00:00
|
|
|
m_tree = root;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
|
2020-07-24 14:16:15 +00:00
|
|
|
void COMPILER::freeTree( LIBEVAL::TREE_NODE *tree )
|
|
|
|
{
|
|
|
|
if ( tree->leaf[0] )
|
|
|
|
freeTree( tree->leaf[0] );
|
2020-08-13 14:15:43 +00:00
|
|
|
|
2020-07-24 14:16:15 +00:00
|
|
|
if ( tree->leaf[1] )
|
|
|
|
freeTree( tree->leaf[1] );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
delete tree->uop;
|
2022-11-22 14:09:19 +00:00
|
|
|
tree->uop = nullptr;
|
2020-07-29 21:11:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
void TREE_NODE::SetUop( int aOp, double aValue )
|
|
|
|
{
|
2020-08-14 11:34:44 +00:00
|
|
|
delete uop;
|
2020-08-10 12:47:01 +00:00
|
|
|
|
2020-10-26 23:49:11 +00:00
|
|
|
std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue );
|
2020-08-10 12:47:01 +00:00
|
|
|
uop = new UOP( aOp, std::move( val ) );
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
|
2020-09-23 21:44:50 +00:00
|
|
|
void TREE_NODE::SetUop( int aOp, const wxString& aValue, bool aStringIsWildcard )
|
2020-08-10 12:47:01 +00:00
|
|
|
{
|
2020-08-14 11:34:44 +00:00
|
|
|
delete uop;
|
2020-08-10 12:47:01 +00:00
|
|
|
|
2020-10-26 23:49:11 +00:00
|
|
|
std::unique_ptr<VALUE> val = std::make_unique<VALUE>( aValue, aStringIsWildcard );
|
2020-08-10 12:47:01 +00:00
|
|
|
uop = new UOP( aOp, std::move( val ) );
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
void TREE_NODE::SetUop( int aOp, std::unique_ptr<VAR_REF> aRef )
|
|
|
|
{
|
2020-08-14 11:34:44 +00:00
|
|
|
delete uop;
|
2020-08-10 12:47:01 +00:00
|
|
|
|
|
|
|
uop = new UOP( aOp, std::move( aRef ) );
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:34:44 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
void TREE_NODE::SetUop( int aOp, FUNC_CALL_REF aFunc, std::unique_ptr<VAR_REF> aRef )
|
|
|
|
{
|
2020-08-14 11:34:44 +00:00
|
|
|
delete uop;
|
2020-08-10 12:47:01 +00:00
|
|
|
|
|
|
|
uop = new UOP( aOp, std::move( aFunc ), std::move( aRef ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
static void prepareTree( LIBEVAL::TREE_NODE *node )
|
|
|
|
{
|
|
|
|
node->isVisited = false;
|
|
|
|
|
|
|
|
// fixme: for reasons I don't understand the lemon parser isn't initializing the
|
|
|
|
// leaf node pointers of function name nodes. -JY
|
|
|
|
if( node->op == TR_OP_FUNC_CALL && node->leaf[0] )
|
|
|
|
{
|
|
|
|
node->leaf[0]->leaf[0] = nullptr;
|
|
|
|
node->leaf[0]->leaf[1] = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( node->leaf[0] )
|
|
|
|
prepareTree( node->leaf[0] );
|
|
|
|
|
|
|
|
if ( node->leaf[1] )
|
|
|
|
prepareTree( node->leaf[1] );
|
|
|
|
}
|
|
|
|
|
2020-09-22 12:56:54 +00:00
|
|
|
static std::vector<TREE_NODE*> squashParamList( TREE_NODE* root )
|
|
|
|
{
|
|
|
|
std::vector<TREE_NODE*> args;
|
|
|
|
|
|
|
|
if( !root )
|
|
|
|
{
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( root->op != TR_ARG_LIST && root->op != TR_NULL )
|
|
|
|
{
|
|
|
|
args.push_back( root );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TREE_NODE *n = root;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( n->leaf[1] )
|
|
|
|
args.push_back(n->leaf[1]);
|
|
|
|
|
|
|
|
n = n->leaf[0];
|
|
|
|
} while ( n && n->op == TR_ARG_LIST );
|
|
|
|
|
|
|
|
if( n )
|
|
|
|
{
|
2021-07-08 12:33:08 +00:00
|
|
|
args.push_back( n );
|
2020-09-22 12:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::reverse( args.begin(), args.end() );
|
|
|
|
|
2020-09-29 00:59:03 +00:00
|
|
|
for( size_t i = 0; i < args.size(); i++ )
|
2023-06-18 19:27:54 +00:00
|
|
|
libeval_dbg( 10, "squash arg%d: %s\n", int( i ), formatNode( args[i] ) );
|
2020-09-22 12:56:54 +00:00
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
bool COMPILER::generateUCode( UCODE* aCode, CONTEXT* aPreflightContext )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
std::vector<TREE_NODE*> stack;
|
2020-07-30 11:24:29 +00:00
|
|
|
wxString msg;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-12 11:36:40 +00:00
|
|
|
if( !m_tree )
|
|
|
|
{
|
2020-10-26 23:49:11 +00:00
|
|
|
std::unique_ptr<VALUE> val = std::make_unique<VALUE>( 1.0 );
|
2020-08-12 11:36:40 +00:00
|
|
|
// Empty expression returns true
|
2020-08-13 10:08:16 +00:00
|
|
|
aCode->AddOp( new UOP( TR_UOP_PUSH_VALUE, std::move(val) ) );
|
2020-08-12 11:36:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
prepareTree( m_tree );
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
stack.push_back( m_tree );
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
wxString dump;
|
2020-06-16 19:48:47 +00:00
|
|
|
|
|
|
|
dumpNode( dump, m_tree, 0 );
|
2020-08-13 14:15:43 +00:00
|
|
|
libeval_dbg( 3, "Tree dump:\n%s\n\n", (const char*) dump.c_str() );
|
2020-06-16 19:48:47 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
while( !stack.empty() )
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
TREE_NODE* node = stack.back();
|
2020-06-16 19:48:47 +00:00
|
|
|
|
2020-07-20 12:51:17 +00:00
|
|
|
libeval_dbg( 4, "process node %p [op %d] [stack %lu]\n",
|
|
|
|
node, node->op, (unsigned long)stack.size() );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
// process terminal nodes first
|
|
|
|
switch( node->op )
|
|
|
|
{
|
2020-06-17 22:36:02 +00:00
|
|
|
case TR_OP_FUNC_CALL:
|
2020-08-13 14:15:43 +00:00
|
|
|
// Function call's uop was generated inside TR_STRUCT_REF
|
2020-09-18 21:29:15 +00:00
|
|
|
if( !node->uop )
|
|
|
|
{
|
|
|
|
reportError( CST_CODEGEN, _( "Unknown parent of function parameters" ),
|
|
|
|
node->srcPos );
|
|
|
|
}
|
2020-08-13 14:15:43 +00:00
|
|
|
|
|
|
|
node->isTerminal = true;
|
2020-06-17 22:36:02 +00:00
|
|
|
break;
|
2020-07-20 12:51:17 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
case TR_STRUCT_REF:
|
|
|
|
{
|
2020-08-13 14:15:43 +00:00
|
|
|
// leaf[0]: object
|
|
|
|
// leaf[1]: field (TR_IDENTIFIER) or TR_OP_FUNC_CALL
|
|
|
|
|
2020-09-18 21:29:15 +00:00
|
|
|
if( node->leaf[0]->op != TR_IDENTIFIER )
|
|
|
|
{
|
2021-02-28 19:46:13 +00:00
|
|
|
int pos = node->leaf[0]->srcPos;
|
|
|
|
|
|
|
|
if( node->leaf[0]->value.str )
|
2023-06-18 19:27:54 +00:00
|
|
|
pos -= static_cast<int>( formatNode( node->leaf[0] ).length() );
|
2021-02-28 19:46:13 +00:00
|
|
|
|
|
|
|
reportError( CST_CODEGEN, _( "Unknown parent of property" ), pos );
|
|
|
|
|
|
|
|
node->leaf[0]->isVisited = true;
|
|
|
|
node->leaf[1]->isVisited = true;
|
|
|
|
|
|
|
|
node->SetUop( TR_UOP_PUSH_VALUE, 0.0 );
|
|
|
|
node->isTerminal = true;
|
|
|
|
break;
|
2020-09-18 21:29:15 +00:00
|
|
|
}
|
2020-06-16 19:48:47 +00:00
|
|
|
|
|
|
|
switch( node->leaf[1]->op )
|
|
|
|
{
|
|
|
|
case TR_IDENTIFIER:
|
|
|
|
{
|
2020-08-13 14:15:43 +00:00
|
|
|
// leaf[0]: object
|
|
|
|
// leaf[1]: field
|
|
|
|
|
2023-06-18 19:27:54 +00:00
|
|
|
wxString itemName = formatNode( node->leaf[0] );
|
|
|
|
wxString propName = formatNode( node->leaf[1] );
|
2020-08-10 12:47:01 +00:00
|
|
|
std::unique_ptr<VAR_REF> vref = aCode->CreateVarRef( itemName, propName );
|
2020-06-16 19:48:47 +00:00
|
|
|
|
2020-07-30 11:24:29 +00:00
|
|
|
if( !vref )
|
2020-06-16 19:48:47 +00:00
|
|
|
{
|
2020-07-30 11:24:29 +00:00
|
|
|
msg.Printf( _( "Unrecognized item '%s'" ), itemName );
|
2021-07-08 12:33:08 +00:00
|
|
|
reportError( CST_CODEGEN, msg,
|
|
|
|
node->leaf[0]->srcPos - (int) itemName.length() );
|
2020-07-30 11:24:29 +00:00
|
|
|
}
|
2020-08-14 19:42:21 +00:00
|
|
|
else if( vref->GetType() == VT_PARSE_ERROR )
|
2020-07-30 11:24:29 +00:00
|
|
|
{
|
|
|
|
msg.Printf( _( "Unrecognized property '%s'" ), propName );
|
2021-07-08 12:33:08 +00:00
|
|
|
reportError( CST_CODEGEN, msg,
|
|
|
|
node->leaf[1]->srcPos - (int) propName.length() );
|
2020-06-16 19:48:47 +00:00
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
node->leaf[0]->isVisited = true;
|
|
|
|
node->leaf[1]->isVisited = true;
|
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
node->SetUop( TR_UOP_PUSH_VAR, std::move( vref ) );
|
2020-06-17 22:36:02 +00:00
|
|
|
node->isTerminal = true;
|
2020-06-16 19:48:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TR_OP_FUNC_CALL:
|
|
|
|
{
|
2020-08-13 14:15:43 +00:00
|
|
|
// leaf[0]: object
|
|
|
|
// leaf[1]: TR_OP_FUNC_CALL
|
|
|
|
// leaf[0]: function name
|
|
|
|
// leaf[1]: parameter
|
|
|
|
|
2023-06-18 19:27:54 +00:00
|
|
|
wxString itemName = formatNode( node->leaf[0] );
|
2020-08-10 12:47:01 +00:00
|
|
|
std::unique_ptr<VAR_REF> vref = aCode->CreateVarRef( itemName, "" );
|
2020-06-16 19:48:47 +00:00
|
|
|
|
2020-07-30 11:24:29 +00:00
|
|
|
if( !vref )
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
2020-07-30 11:24:29 +00:00
|
|
|
msg.Printf( _( "Unrecognized item '%s'" ), itemName );
|
2021-07-08 12:33:08 +00:00
|
|
|
reportError( CST_CODEGEN, msg,
|
|
|
|
node->leaf[0]->srcPos - (int) itemName.length() );
|
2020-07-19 21:22:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 19:27:54 +00:00
|
|
|
wxString functionName = formatNode( node->leaf[1]->leaf[0] );
|
2020-07-30 11:24:29 +00:00
|
|
|
auto func = aCode->CreateFuncCall( functionName );
|
2020-09-22 12:56:54 +00:00
|
|
|
std::vector<TREE_NODE*> params = squashParamList( node->leaf[1]->leaf[1] );
|
2020-06-16 19:48:47 +00:00
|
|
|
|
2020-08-13 23:55:11 +00:00
|
|
|
libeval_dbg( 10, "emit func call: %s\n", functionName );
|
2020-08-08 22:39:05 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
if( !func )
|
2020-06-16 19:48:47 +00:00
|
|
|
{
|
2020-07-30 11:24:29 +00:00
|
|
|
msg.Printf( _( "Unrecognized function '%s'" ), functionName );
|
2020-08-13 14:15:43 +00:00
|
|
|
reportError( CST_CODEGEN, msg, node->leaf[0]->srcPos + 1 );
|
2020-06-16 19:48:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
if( func )
|
2020-07-22 13:08:57 +00:00
|
|
|
{
|
2020-08-13 14:15:43 +00:00
|
|
|
// Preflight the function call
|
|
|
|
|
2020-10-15 15:58:35 +00:00
|
|
|
for( TREE_NODE* pnode : params )
|
2020-08-27 12:52:01 +00:00
|
|
|
{
|
|
|
|
VALUE* param = aPreflightContext->AllocValue();
|
2023-06-18 19:27:54 +00:00
|
|
|
param->Set( formatNode( pnode ) );
|
2020-08-27 12:52:01 +00:00
|
|
|
aPreflightContext->Push( param );
|
|
|
|
}
|
2020-08-13 14:15:43 +00:00
|
|
|
|
2020-08-26 11:55:43 +00:00
|
|
|
aPreflightContext->SetErrorCallback(
|
|
|
|
[&]( const wxString& aMessage, int aOffset )
|
|
|
|
{
|
2020-09-22 12:56:54 +00:00
|
|
|
size_t loc = node->leaf[1]->leaf[1]->srcPos;
|
2020-08-26 11:55:43 +00:00
|
|
|
reportError( CST_CODEGEN, aMessage, (int) loc - 1 );
|
|
|
|
} );
|
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
func( aPreflightContext, vref.get() );
|
|
|
|
aPreflightContext->Pop(); // return value
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
}
|
2020-07-22 13:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
node->leaf[0]->isVisited = true;
|
|
|
|
node->leaf[1]->isVisited = true;
|
2020-10-15 15:58:35 +00:00
|
|
|
node->leaf[1]->leaf[0]->isVisited = true;
|
2020-08-13 14:15:43 +00:00
|
|
|
node->leaf[1]->leaf[1]->isVisited = true;
|
2020-07-24 14:16:15 +00:00
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
// Our non-terminal-node stacking algorithm can't handle doubly-nested
|
|
|
|
// structures so we need to pop a level by replacing the TR_STRUCT_REF with
|
|
|
|
// a TR_OP_FUNC_CALL and its function parameter
|
|
|
|
stack.pop_back();
|
|
|
|
stack.push_back( node->leaf[1] );
|
2020-10-15 15:58:35 +00:00
|
|
|
|
|
|
|
for( TREE_NODE* pnode : params )
|
2020-09-22 12:56:54 +00:00
|
|
|
stack.push_back( pnode );
|
2020-06-16 19:48:47 +00:00
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
node->leaf[1]->SetUop( TR_OP_METHOD_CALL, func, std::move( vref ) );
|
2020-06-17 22:36:02 +00:00
|
|
|
node->isTerminal = false;
|
2020-06-16 19:48:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-10-15 15:58:35 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// leaf[0]: object
|
|
|
|
// leaf[1]: malformed syntax
|
|
|
|
|
2023-06-18 19:27:54 +00:00
|
|
|
wxString itemName = formatNode( node->leaf[0] );
|
|
|
|
wxString propName = formatNode( node->leaf[1] );
|
2020-10-15 15:58:35 +00:00
|
|
|
std::unique_ptr<VAR_REF> vref = aCode->CreateVarRef( itemName, propName );
|
|
|
|
|
|
|
|
if( !vref )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Unrecognized item '%s'" ), itemName );
|
2021-07-08 12:33:08 +00:00
|
|
|
reportError( CST_CODEGEN, msg,
|
|
|
|
node->leaf[0]->srcPos - (int) itemName.length() );
|
2020-10-15 15:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg.Printf( _( "Unrecognized property '%s'" ), propName );
|
|
|
|
reportError( CST_CODEGEN, msg, node->leaf[0]->srcPos + 1 );
|
|
|
|
|
|
|
|
node->leaf[0]->isVisited = true;
|
|
|
|
node->leaf[1]->isVisited = true;
|
|
|
|
|
|
|
|
node->SetUop( TR_UOP_PUSH_VALUE, 0.0 );
|
|
|
|
node->isTerminal = true;
|
|
|
|
break;
|
2020-06-16 19:48:47 +00:00
|
|
|
}
|
2021-07-08 12:33:08 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
2020-08-13 14:15:43 +00:00
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
case TR_NUMBER:
|
|
|
|
{
|
2020-07-22 13:31:11 +00:00
|
|
|
TREE_NODE* son = node->leaf[0];
|
2020-08-13 14:15:43 +00:00
|
|
|
double value;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-08-10 14:54:45 +00:00
|
|
|
if( !node->value.str )
|
|
|
|
{
|
|
|
|
value = 0.0;
|
|
|
|
}
|
|
|
|
else if( son && son->op == TR_UNIT )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2022-11-29 14:18:44 +00:00
|
|
|
if( m_unitResolver->GetSupportedUnits().empty() )
|
|
|
|
{
|
2023-06-18 19:27:54 +00:00
|
|
|
msg.Printf( _( "Unexpected units for '%s'" ), formatNode( node ) );
|
2022-11-29 14:18:44 +00:00
|
|
|
reportError( CST_CODEGEN, msg, node->srcPos );
|
|
|
|
}
|
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
int units = son->value.idx;
|
2023-06-18 19:27:54 +00:00
|
|
|
value = m_unitResolver->Convert( formatNode( node ), units );
|
2020-08-13 14:15:43 +00:00
|
|
|
son->isVisited = true;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
2020-08-08 22:39:05 +00:00
|
|
|
else
|
|
|
|
{
|
2022-11-29 14:18:44 +00:00
|
|
|
if( !m_unitResolver->GetSupportedUnitsMessage().empty() )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Missing units for '%s'| (%s)" ),
|
2023-06-18 19:27:54 +00:00
|
|
|
formatNode( node ),
|
2022-11-29 14:18:44 +00:00
|
|
|
m_unitResolver->GetSupportedUnitsMessage() );
|
|
|
|
reportError( CST_CODEGEN, msg, node->srcPos );
|
|
|
|
}
|
2020-10-16 23:14:59 +00:00
|
|
|
|
2023-06-18 19:27:54 +00:00
|
|
|
value = EDA_UNIT_UTILS::UI::DoubleValueFromString( formatNode( node ) );
|
2020-08-08 22:39:05 +00:00
|
|
|
}
|
2020-07-22 14:11:00 +00:00
|
|
|
|
2020-08-10 12:47:01 +00:00
|
|
|
node->SetUop( TR_UOP_PUSH_VALUE, value );
|
2020-06-17 22:36:02 +00:00
|
|
|
node->isTerminal = true;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-20 12:51:17 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
case TR_STRING:
|
|
|
|
{
|
2023-06-18 19:27:54 +00:00
|
|
|
wxString str = formatNode( node );
|
2020-09-23 21:44:50 +00:00
|
|
|
bool isWildcard = str.Contains("?") || str.Contains("*");
|
|
|
|
node->SetUop( TR_UOP_PUSH_VALUE, str, isWildcard );
|
2020-06-17 22:36:02 +00:00
|
|
|
node->isTerminal = true;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-20 12:51:17 +00:00
|
|
|
|
2020-07-27 18:51:54 +00:00
|
|
|
case TR_IDENTIFIER:
|
|
|
|
{
|
2023-06-18 19:27:54 +00:00
|
|
|
std::unique_ptr<VAR_REF> vref = aCode->CreateVarRef( formatNode( node ), "" );
|
2020-07-27 18:51:54 +00:00
|
|
|
|
2020-07-30 11:24:29 +00:00
|
|
|
if( !vref )
|
2020-07-27 18:51:54 +00:00
|
|
|
{
|
2023-06-18 19:27:54 +00:00
|
|
|
msg.Printf( _( "Unrecognized item '%s'" ), formatNode( node ) );
|
|
|
|
reportError( CST_CODEGEN, msg, node->srcPos - (int) formatNode( node ).length() );
|
2020-07-27 18:51:54 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 13:13:34 +00:00
|
|
|
node->SetUop( TR_UOP_PUSH_VAR, std::move( vref ) );
|
2020-08-13 14:15:43 +00:00
|
|
|
node->isTerminal = true;
|
2020-07-27 18:51:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
default:
|
2020-08-10 12:47:01 +00:00
|
|
|
node->SetUop( node->op );
|
2020-08-13 14:15:43 +00:00
|
|
|
node->isTerminal = ( !node->leaf[0] || node->leaf[0]->isVisited )
|
|
|
|
&& ( !node->leaf[1] || node->leaf[1]->isVisited );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
if( !node->isTerminal )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-08-13 14:15:43 +00:00
|
|
|
if( node->leaf[0] && !node->leaf[0]->isVisited )
|
|
|
|
{
|
|
|
|
stack.push_back( node->leaf[0] );
|
2020-10-15 15:58:35 +00:00
|
|
|
node->leaf[0]->isVisited = true;
|
2020-08-13 14:15:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if( node->leaf[1] && !node->leaf[1]->isVisited )
|
|
|
|
{
|
|
|
|
stack.push_back( node->leaf[1] );
|
2020-10-15 15:58:35 +00:00
|
|
|
node->leaf[1]->isVisited = true;
|
2020-08-13 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
continue;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
2020-07-18 20:33:23 +00:00
|
|
|
|
2020-08-13 14:15:43 +00:00
|
|
|
node->isVisited = true;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-07-22 13:31:11 +00:00
|
|
|
if( node->uop )
|
2020-08-10 12:47:01 +00:00
|
|
|
{
|
2020-07-22 13:31:11 +00:00
|
|
|
aCode->AddOp( node->uop );
|
2020-08-10 12:47:01 +00:00
|
|
|
node->uop = nullptr;
|
|
|
|
}
|
2020-07-18 20:33:23 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
stack.pop_back();
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 22:39:05 +00:00
|
|
|
libeval_dbg(2,"dump: \n%s\n", aCode->Dump().c_str() );
|
2020-06-17 22:36:02 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
void UOP::Exec( CONTEXT* ctx )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
switch( m_op )
|
|
|
|
{
|
|
|
|
case TR_UOP_PUSH_VAR:
|
|
|
|
{
|
2022-09-16 13:13:34 +00:00
|
|
|
VALUE* value = nullptr;
|
2021-03-20 11:04:36 +00:00
|
|
|
|
|
|
|
if( m_ref )
|
2022-09-16 13:13:34 +00:00
|
|
|
value = ctx->StoreValue( m_ref->GetValue( ctx ) );
|
|
|
|
else
|
|
|
|
value = ctx->AllocValue();
|
2021-03-20 11:04:36 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
ctx->Push( value );
|
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
break;
|
2020-06-16 19:48:47 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
case TR_UOP_PUSH_VALUE:
|
2020-08-10 12:47:01 +00:00
|
|
|
ctx->Push( m_value.get() );
|
2020-06-04 11:04:03 +00:00
|
|
|
return;
|
2020-06-16 19:48:47 +00:00
|
|
|
|
|
|
|
case TR_OP_METHOD_CALL:
|
2020-08-10 12:47:01 +00:00
|
|
|
m_func( ctx, m_ref.get() );
|
2020-06-16 19:48:47 +00:00
|
|
|
return;
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-08-21 12:05:16 +00:00
|
|
|
#define AS_DOUBLE( arg ) ( arg ? arg->AsDouble() : 0.0 )
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
if( m_op & TR_OP_BINARY_MASK )
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
LIBEVAL::VALUE* arg2 = ctx->Pop();
|
|
|
|
LIBEVAL::VALUE* arg1 = ctx->Pop();
|
|
|
|
double result;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2021-05-20 22:05:27 +00:00
|
|
|
if( ctx->HasErrorCallback() )
|
|
|
|
{
|
2021-05-21 13:38:15 +00:00
|
|
|
if( arg1 && arg1->GetType() == VT_STRING && arg2 && arg2->GetType() == VT_NUMERIC )
|
2021-05-20 22:05:27 +00:00
|
|
|
{
|
|
|
|
ctx->ReportError( wxString::Format( _( "Type mismatch between '%s' and %lf" ),
|
|
|
|
arg1->AsString(),
|
|
|
|
arg2->AsDouble() ) );
|
|
|
|
}
|
2021-05-21 13:38:15 +00:00
|
|
|
else if( arg1 && arg1->GetType() == VT_NUMERIC && arg2 && arg2->GetType() == VT_STRING )
|
2021-05-20 22:05:27 +00:00
|
|
|
{
|
|
|
|
ctx->ReportError( wxString::Format( _( "Type mismatch between %lf and '%s'" ),
|
|
|
|
arg1->AsDouble(),
|
|
|
|
arg2->AsString() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
switch( m_op )
|
|
|
|
{
|
|
|
|
case TR_OP_ADD:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) + AS_DOUBLE( arg2 );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_SUB:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) - AS_DOUBLE( arg2 );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_MUL:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) * AS_DOUBLE( arg2 );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_DIV:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) / AS_DOUBLE( arg2 );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_LESS_EQUAL:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) <= AS_DOUBLE( arg2 ) ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_GREATER_EQUAL:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) >= AS_DOUBLE( arg2 ) ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_LESS:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) < AS_DOUBLE( arg2 ) ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_GREATER:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) > AS_DOUBLE( arg2 ) ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_EQUAL:
|
2022-09-16 13:13:34 +00:00
|
|
|
if( !arg1 || !arg2 )
|
|
|
|
result = arg1 == arg2 ? 1 : 0;
|
|
|
|
else if( arg2->GetType() == VT_UNDEFINED )
|
|
|
|
result = arg2->EqualTo( ctx, arg1 ) ? 1 : 0;
|
|
|
|
else
|
|
|
|
result = arg1->EqualTo( ctx, arg2 ) ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_NOT_EQUAL:
|
2022-09-16 13:13:34 +00:00
|
|
|
if( !arg1 || !arg2 )
|
|
|
|
result = arg1 != arg2 ? 1 : 0;
|
|
|
|
else if( arg2->GetType() == VT_UNDEFINED )
|
|
|
|
result = arg2->NotEqualTo( ctx, arg1 ) ? 1 : 0;
|
|
|
|
else
|
|
|
|
result = arg1->NotEqualTo( ctx, arg2 ) ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_BOOL_AND:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) != 0.0 && AS_DOUBLE( arg2 ) != 0.0 ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_BOOL_OR:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = AS_DOUBLE( arg1 ) != 0.0 || AS_DOUBLE( arg2 ) != 0.0 ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = 0.0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rp = ctx->AllocValue();
|
|
|
|
rp->Set( result );
|
|
|
|
ctx->Push( rp );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if( m_op & TR_OP_UNARY_MASK )
|
|
|
|
{
|
2020-09-18 23:09:53 +00:00
|
|
|
LIBEVAL::VALUE* arg1 = ctx->Pop();
|
2023-08-21 12:05:16 +00:00
|
|
|
double ARG1VALUE = arg1 ? arg1->AsDouble() : 0.0;
|
2020-09-18 23:09:53 +00:00
|
|
|
double result;
|
|
|
|
|
|
|
|
switch( m_op )
|
|
|
|
{
|
|
|
|
case TR_OP_BOOL_NOT:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = ARG1VALUE != 0.0 ? 0 : 1;
|
2020-09-18 23:09:53 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-08-21 12:05:16 +00:00
|
|
|
result = ARG1VALUE != 0.0 ? 1 : 0;
|
2020-09-18 23:09:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rp = ctx->AllocValue();
|
|
|
|
rp->Set( result );
|
|
|
|
ctx->Push( rp );
|
|
|
|
return;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
VALUE* UCODE::Run( CONTEXT* ctx )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-09-01 16:34:35 +00:00
|
|
|
static VALUE g_false( 0 );
|
|
|
|
|
2020-09-18 23:09:53 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
for( UOP* op : m_ucode )
|
|
|
|
op->Exec( ctx );
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
// rules which fail outright should not be fired
|
|
|
|
return &g_false;
|
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-10-15 15:58:35 +00:00
|
|
|
if( ctx->SP() == 1 )
|
|
|
|
{
|
|
|
|
return ctx->Pop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If stack is corrupted after execution it suggests a problem with the compiler, not
|
|
|
|
// the rule....
|
2020-09-23 21:44:50 +00:00
|
|
|
|
2020-10-15 15:58:35 +00:00
|
|
|
// do not use "assert"; it crashes outright on OSX
|
|
|
|
wxASSERT( ctx->SP() == 1 );
|
2020-09-01 16:34:35 +00:00
|
|
|
|
2020-10-15 15:58:35 +00:00
|
|
|
// non-well-formed rules should not be fired on a release build
|
|
|
|
return &g_false;
|
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 23:19:22 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
} // namespace LIBEVAL
|