2020-06-04 11:04:03 +00:00
|
|
|
/*
|
|
|
|
This file is part of libeval, a simple math expression evaluator
|
|
|
|
|
|
|
|
Copyright (C) 2017 Michael Geselbracht, mgeselbracht3@gmail.com
|
2020-07-03 16:36:33 +00:00
|
|
|
Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-06-04 11:04:03 +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-07-19 21:22:49 +00:00
|
|
|
#include <memory>
|
2020-06-04 11:04:03 +00:00
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#include <stdarg.h>
|
|
|
|
#endif
|
|
|
|
|
2020-07-30 11:24:29 +00:00
|
|
|
#include <reporter.h>
|
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
|
|
|
|
|
|
|
|
#include "grammar.c"
|
|
|
|
#include "grammar.h"
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2020-06-13 23:19:22 +00:00
|
|
|
static void libeval_dbg( int level, const char* fmt, ... )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2020-06-18 16:54:30 +00:00
|
|
|
if(level < -10) // fixme: tom's debugging.
|
2020-06-13 23:19:22 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start( ap, fmt );
|
|
|
|
fprintf( stderr, "libeval: " );
|
|
|
|
vfprintf( stderr, fmt, ap );
|
|
|
|
va_end( ap );
|
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
static const std::string formatOpName( int op )
|
|
|
|
{
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
int op;
|
|
|
|
std::string 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-06-17 22:36:02 +00:00
|
|
|
std::string UOP::Format() const
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
char str[LIBEVAL_MAX_LITERAL_LENGTH];
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
switch( m_op )
|
|
|
|
{
|
|
|
|
case TR_UOP_PUSH_VAR:
|
2020-07-19 21:22:49 +00:00
|
|
|
snprintf( str, LIBEVAL_MAX_LITERAL_LENGTH, "PUSH VAR [%p]", m_arg );
|
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-07-19 21:22:49 +00:00
|
|
|
VALUE* val = reinterpret_cast<VALUE*>( m_arg );
|
|
|
|
|
|
|
|
if( !val )
|
|
|
|
snprintf( str, LIBEVAL_MAX_LITERAL_LENGTH, "PUSH nullptr" );
|
|
|
|
else if( val->GetType() == VT_NUMERIC )
|
|
|
|
snprintf( str, LIBEVAL_MAX_LITERAL_LENGTH, "PUSH NUM [%.10f]", val->AsDouble() );
|
2020-06-04 11:04:03 +00:00
|
|
|
else
|
2020-07-26 11:22:49 +00:00
|
|
|
snprintf( str, LIBEVAL_MAX_LITERAL_LENGTH, "PUSH STR [%ls]", GetChars( val->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-07-19 21:22:49 +00:00
|
|
|
snprintf( str, LIBEVAL_MAX_LITERAL_LENGTH, "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-07-19 21:22:49 +00:00
|
|
|
snprintf( str, LIBEVAL_MAX_LITERAL_LENGTH, "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-07-19 21:22:49 +00:00
|
|
|
snprintf( str, LIBEVAL_MAX_LITERAL_LENGTH, "%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 )
|
|
|
|
delete op;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
std::string UCODE::Dump() const
|
|
|
|
{
|
|
|
|
std::string 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-06-04 11:04:03 +00:00
|
|
|
std::string TOKENIZER::GetChars( std::function<bool( int )> cond ) const
|
|
|
|
{
|
|
|
|
std::string rv;
|
|
|
|
size_t p = m_pos;
|
|
|
|
// printf("p %d len %d\n", p, str.length() );
|
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-07-19 21:22:49 +00:00
|
|
|
bool TOKENIZER::MatchAhead( const std::string& match, std::function<bool( int )> stopCond ) const
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
int remaining = 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-07-30 11:24:29 +00:00
|
|
|
COMPILER::COMPILER( REPORTER* aReporter, int aSourceLine, int aSourceOffset ) :
|
2020-07-31 01:46:05 +00:00
|
|
|
m_lexerState( COMPILER::LS_DEFAULT ),
|
2020-07-30 11:24:29 +00:00
|
|
|
m_reporter( aReporter ),
|
|
|
|
m_originLine( aSourceLine ),
|
|
|
|
m_originOffset( aSourceOffset )
|
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-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
COMPILER::~COMPILER()
|
|
|
|
{
|
|
|
|
LIBEVAL::ParseFree( m_parser, free );
|
|
|
|
|
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_tree = nullptr;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPILER::parseError( const char* s )
|
|
|
|
{
|
2020-07-30 11:24:29 +00:00
|
|
|
reportError( s );
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPILER::parseOk()
|
|
|
|
{
|
|
|
|
m_parseFinished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
bool COMPILER::Compile( const std::string& 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-07-19 21:22:49 +00:00
|
|
|
m_tree = nullptr;
|
2020-06-04 11:04:03 +00:00
|
|
|
m_parseFinished = false;
|
|
|
|
T_TOKEN tok;
|
|
|
|
|
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-06-13 23:19:22 +00:00
|
|
|
libeval_dbg(10, "parse: tok %d\n", tok.token );
|
2020-06-04 11:04:03 +00:00
|
|
|
Parse( m_parser, tok.token, tok.value, this );
|
2020-07-18 20:33:23 +00:00
|
|
|
|
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.
|
|
|
|
Parse( m_parser, 0, tok.value, this );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while( tok.token );
|
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
return generateUCode( aCode, aPreflightContext );
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPILER::newString( const std::string& aString )
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
m_lexerState = LS_DEFAULT;
|
|
|
|
m_tokenizer.Restart( aString );
|
|
|
|
m_parseFinished = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER::T_TOKEN COMPILER::getToken()
|
|
|
|
{
|
|
|
|
T_TOKEN rv;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
//printf( "-> lstate %d done %d\n", m_lexerState, !!done );
|
|
|
|
} while( !done );
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool COMPILER::lexString( COMPILER::T_TOKEN& aToken )
|
|
|
|
{
|
2020-07-22 13:31:11 +00:00
|
|
|
std::string str = m_tokenizer.GetChars( []( int c ) -> bool { return c != '\''; } );
|
2020-06-04 11:04:03 +00:00
|
|
|
//printf("STR LIT '%s'\n", (const char *)str.c_str() );
|
|
|
|
|
|
|
|
aToken.token = G_STRING;
|
2020-07-18 10:40:56 +00:00
|
|
|
snprintf( aToken.value.value.str, LIBEVAL_MAX_LITERAL_LENGTH, "%s", str.c_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
|
|
|
|
|
|
|
for( const std::string& 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-06-04 11:04:03 +00:00
|
|
|
bool COMPILER::lexDefault( COMPILER::T_TOKEN& aToken )
|
|
|
|
{
|
|
|
|
T_TOKEN retval;
|
|
|
|
std::string current;
|
|
|
|
int convertFrom;
|
2020-07-30 11:24:29 +00:00
|
|
|
wxString msg;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
retval.token = G_ENDS;
|
|
|
|
|
|
|
|
//printf( "tokdone %d\n", !!m_tokenizer.Done() );
|
|
|
|
if( m_tokenizer.Done() )
|
|
|
|
{
|
|
|
|
aToken = retval;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
auto isDecimalSeparator =
|
|
|
|
[&]( char ch ) -> bool
|
|
|
|
{
|
|
|
|
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 =
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
bool haveSeparator = false;
|
|
|
|
int 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
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
//printf("-> NUM: '%s'\n", (const char *) current.c_str() );
|
|
|
|
};
|
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-07-18 10:40:56 +00:00
|
|
|
snprintf( retval.value.value.str, LIBEVAL_MAX_LITERAL_LENGTH, "%s", current.c_str() );
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
|
|
|
else if( ( convertFrom = resolveUnits() ) >= 0 )
|
|
|
|
{
|
|
|
|
//printf("unit\n");
|
|
|
|
// 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;
|
|
|
|
retval.value.value.type = convertFrom;
|
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
else if( ch == '\'' ) // string literal
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
//printf( "MATCH STRING LITERAL\n" );
|
|
|
|
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
|
|
|
{
|
|
|
|
//printf("ALPHA\n");
|
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
|
|
|
//printf("Len: %d\n", current.length() );
|
|
|
|
//printf("id '%s'\n", (const char *) current.c_str() );
|
2020-07-23 10:34:45 +00:00
|
|
|
//fflush( stdout );
|
2020-06-04 11:04:03 +00:00
|
|
|
retval.token = G_IDENTIFIER;
|
2020-07-18 10:40:56 +00:00
|
|
|
snprintf( retval.value.value.str, LIBEVAL_MAX_LITERAL_LENGTH, "%s", current.c_str() );
|
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-06-04 11:04:03 +00:00
|
|
|
default:
|
2020-07-30 11:24:29 +00:00
|
|
|
reportError( 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-06-04 11:04:03 +00:00
|
|
|
const std::string formatNode( TREE_NODE* tok )
|
|
|
|
{
|
|
|
|
// printf("fmt tok %p v %p ", tok, tok->value.v );
|
2020-07-22 13:31:11 +00:00
|
|
|
// fflush( stdout );
|
|
|
|
|
|
|
|
char str[LIBEVAL_MAX_LITERAL_LENGTH];
|
|
|
|
snprintf( str, LIBEVAL_MAX_LITERAL_LENGTH, "%s", (const char*) tok->value.str );
|
2020-06-04 11:04:03 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void dumpNode( std::string& buf, TREE_NODE* tok, int depth = 0 )
|
|
|
|
{
|
2020-07-22 13:31:11 +00:00
|
|
|
char str[LIBEVAL_MAX_LITERAL_LENGTH];
|
|
|
|
snprintf( str, LIBEVAL_MAX_LITERAL_LENGTH, "\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-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 '";
|
|
|
|
buf += tok->leaf[0]->value.str;
|
|
|
|
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:
|
|
|
|
sprintf( str, "UNIT: %d ", tok->value.type );
|
|
|
|
buf += str;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-07-30 11:24:29 +00:00
|
|
|
void COMPILER::reportError( 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;
|
|
|
|
|
|
|
|
wxString rest;
|
|
|
|
wxString first = aErrorMsg.BeforeFirst( '|', &rest );
|
|
|
|
wxString msg = wxString::Format( _( "ERROR: <a href='%d:%d'>%s</a>%s" ),
|
|
|
|
m_originLine,
|
|
|
|
m_originOffset + aPos,
|
|
|
|
first,
|
|
|
|
rest );
|
|
|
|
|
|
|
|
m_reporter->Report( msg, RPT_SEVERITY_ERROR );
|
2020-06-16 19:48:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:31:11 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
void COMPILER::setRoot( TREE_NODE root )
|
|
|
|
{
|
|
|
|
m_tree = copyNode( root );
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:16:15 +00:00
|
|
|
void COMPILER::freeTree( LIBEVAL::TREE_NODE *tree )
|
|
|
|
{
|
|
|
|
if ( tree->leaf[0] )
|
|
|
|
freeTree( tree->leaf[0] );
|
|
|
|
if ( tree->leaf[1] )
|
|
|
|
freeTree( tree->leaf[1] );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-29 21:11:30 +00:00
|
|
|
delete tree;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
std::set<TREE_NODE*> visitedNodes;
|
2020-07-30 11:24:29 +00:00
|
|
|
wxString msg;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
auto visited = [&]( TREE_NODE* node ) -> bool
|
|
|
|
{
|
|
|
|
return visitedNodes.find( node ) != visitedNodes.end();
|
|
|
|
};
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
assert( m_tree );
|
|
|
|
|
|
|
|
stack.push_back( m_tree );
|
|
|
|
|
2020-06-16 19:48:47 +00:00
|
|
|
std::string dump;
|
|
|
|
|
|
|
|
dumpNode( dump, m_tree, 0 );
|
2020-06-17 22:36:02 +00:00
|
|
|
libeval_dbg(3,"Tree dump:\n%s\n\n", 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:
|
|
|
|
break;
|
2020-07-20 12:51:17 +00:00
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
case TR_STRUCT_REF:
|
|
|
|
{
|
|
|
|
assert( node->leaf[0]->op == TR_IDENTIFIER );
|
2020-06-16 19:48:47 +00:00
|
|
|
//assert( node->leaf[1]->op == TR_IDENTIFIER );
|
|
|
|
|
|
|
|
switch( node->leaf[1]->op )
|
|
|
|
{
|
|
|
|
case TR_IDENTIFIER:
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
char* itemName = node->leaf[0]->value.str;
|
|
|
|
char* propName = node->leaf[1]->value.str;
|
2020-07-30 11:24:29 +00:00
|
|
|
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 );
|
|
|
|
reportError( msg, node->leaf[0]->srcPos - (int) strlen( itemName ) );
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-07-30 11:24:29 +00:00
|
|
|
if( vref->GetType() == VT_PARSE_ERROR )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Unrecognized property '%s'" ), propName );
|
|
|
|
reportError( msg, node->leaf[1]->srcPos - (int) strlen( propName ) );
|
2020-06-16 19:48:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
node->uop = makeUop( TR_UOP_PUSH_VAR, vref );
|
|
|
|
node->isTerminal = true;
|
2020-06-16 19:48:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TR_OP_FUNC_CALL:
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
char* itemName = node->leaf[0]->value.str;
|
2020-07-30 11:24:29 +00:00
|
|
|
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 );
|
|
|
|
reportError( msg, node->leaf[0]->srcPos - (int) strlen( itemName ) );
|
2020-07-19 21:22:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* functionName = node->leaf[1]->leaf[0]->value.str;
|
2020-07-30 11:24:29 +00:00
|
|
|
auto func = aCode->CreateFuncCall( functionName );
|
2020-06-16 19:48:47 +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 );
|
|
|
|
reportError( msg, node->leaf[1]->leaf[0]->srcPos + 1 );
|
2020-06-16 19:48:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:08:57 +00:00
|
|
|
// Preflight the function call
|
2020-07-23 20:47:38 +00:00
|
|
|
VALUE* param = aPreflightContext->AllocValue();
|
2020-07-22 13:08:57 +00:00
|
|
|
param->Set( node->value.str );
|
2020-07-23 20:47:38 +00:00
|
|
|
aPreflightContext->Push( param );
|
2020-07-22 13:08:57 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-07-23 20:47:38 +00:00
|
|
|
func( aPreflightContext, vref );
|
|
|
|
aPreflightContext->Pop(); // return value
|
2020-07-22 13:08:57 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-24 14:16:15 +00:00
|
|
|
/* SREF -> FUNC_CALL -> leaf0/1 */
|
|
|
|
node->leaf[1]->leaf[0]->leaf[0] = nullptr;
|
|
|
|
node->leaf[1]->leaf[0]->leaf[1] = nullptr;
|
|
|
|
|
2020-07-30 11:24:29 +00:00
|
|
|
if( !aPreflightContext->GetError().IsEmpty() )
|
|
|
|
{
|
|
|
|
reportError( aPreflightContext->GetError(),
|
|
|
|
node->leaf[1]->leaf[1]->srcPos
|
|
|
|
- (int) strlen( node->value.str ) - 1 );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-16 19:48:47 +00:00
|
|
|
visitedNodes.insert( node->leaf[0] );
|
2020-06-17 22:36:02 +00:00
|
|
|
visitedNodes.insert( node->leaf[1]->leaf[0] );
|
2020-06-16 19:48:47 +00:00
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
node->uop = makeUop( TR_OP_METHOD_CALL, func, vref );
|
|
|
|
node->isTerminal = false;
|
2020-07-19 21:22:49 +00:00
|
|
|
}
|
2020-06-16 19:48:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_NUMBER:
|
|
|
|
{
|
2020-07-22 14:11:00 +00:00
|
|
|
int units = 1;
|
2020-07-22 13:31:11 +00:00
|
|
|
TREE_NODE* son = node->leaf[0];
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
if( son && son->op == TR_UNIT )
|
|
|
|
{
|
|
|
|
//printf( "HandleUnit: %s unit %d\n", node->value.str, son->value.type );
|
2020-07-22 14:11:00 +00:00
|
|
|
units = son->value.type;
|
2020-06-04 11:04:03 +00:00
|
|
|
visitedNodes.insert( son );
|
|
|
|
}
|
|
|
|
|
2020-07-22 14:11:00 +00:00
|
|
|
double value = m_unitResolver->Convert( node->value.str, units );
|
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
node->uop = makeUop( TR_UOP_PUSH_VALUE, value );
|
|
|
|
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:
|
|
|
|
{
|
2020-06-17 22:36:02 +00:00
|
|
|
node->uop = makeUop( TR_UOP_PUSH_VALUE, node->value.str );
|
|
|
|
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:
|
|
|
|
{
|
2020-07-30 11:24:29 +00:00
|
|
|
VAR_REF* vref = aCode->CreateVarRef( node->value.str, "" );
|
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
|
|
|
{
|
2020-07-30 11:24:29 +00:00
|
|
|
msg.Printf( _( "Unrecognized item '%s'" ), node->value.str );
|
2020-08-03 21:16:53 +00:00
|
|
|
reportError( msg, node->srcPos - (int) strlen( node->value.str ) );
|
2020-07-27 18:51:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->uop = makeUop( TR_UOP_PUSH_VALUE, vref );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:04:03 +00:00
|
|
|
default:
|
2020-06-17 22:36:02 +00:00
|
|
|
node->uop = makeUop( node->op );
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
if( !node->isTerminal && node->leaf[0] && !visited( node->leaf[0] ) )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
stack.push_back( node->leaf[0] );
|
2020-06-17 22:36:02 +00:00
|
|
|
visitedNodes.insert( node->leaf[0] );
|
|
|
|
continue;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
2020-06-17 22:36:02 +00:00
|
|
|
else if( !node->isTerminal && node->leaf[1] && !visited( node->leaf[1] ) )
|
2020-06-04 11:04:03 +00:00
|
|
|
{
|
|
|
|
stack.push_back( node->leaf[1] );
|
2020-06-17 22:36:02 +00:00
|
|
|
visitedNodes.insert( node->leaf[1] );
|
|
|
|
continue;
|
2020-06-04 11:04:03 +00:00
|
|
|
}
|
2020-07-18 20:33:23 +00:00
|
|
|
|
2020-06-17 22:36:02 +00:00
|
|
|
visitedNodes.insert( node );
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-07-22 13:31:11 +00:00
|
|
|
if( node->uop )
|
|
|
|
aCode->AddOp( node->uop );
|
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-06-17 22:36:02 +00:00
|
|
|
libeval_dbg(2,"DUMp: \n%s\n", aCode->Dump().c_str() );
|
|
|
|
|
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:
|
|
|
|
{
|
|
|
|
auto value = ctx->AllocValue();
|
2020-07-23 20:47:38 +00:00
|
|
|
value->Set( reinterpret_cast<VAR_REF*>( m_arg )->GetValue( ctx ) );
|
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:
|
|
|
|
ctx->Push( reinterpret_cast<VALUE*>( m_arg ) );
|
|
|
|
return;
|
2020-06-16 19:48:47 +00:00
|
|
|
|
|
|
|
case TR_OP_METHOD_CALL:
|
|
|
|
//printf("CALL METHOD %s\n" );
|
2020-07-23 20:47:38 +00:00
|
|
|
m_func( ctx, m_arg );
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2020-07-27 17:50:21 +00:00
|
|
|
double arg2Value = arg2 ? arg2->AsDouble() : 0.0;
|
|
|
|
double arg1Value = arg1 ? arg1->AsDouble() : 0.0;
|
2020-07-19 21:22:49 +00:00
|
|
|
double result;
|
2020-06-04 11:04:03 +00:00
|
|
|
|
|
|
|
switch( m_op )
|
|
|
|
{
|
|
|
|
case TR_OP_ADD:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value + arg2Value;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_SUB:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value - arg2Value;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_MUL:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value * arg2Value;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_DIV:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value / arg2Value;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_LESS_EQUAL:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value <= arg2Value ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_GREATER_EQUAL:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value >= arg2Value ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_LESS:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value < arg2Value ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_GREATER:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value > arg2Value ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_EQUAL:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1 && arg2 && arg1->EqualTo( arg2 ) ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_NOT_EQUAL:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1 && arg2 && arg1->EqualTo( arg2 ) ? 0 : 1;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_BOOL_AND:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value != 0.0 && arg2Value != 0.0 ? 1 : 0;
|
2020-06-04 11:04:03 +00:00
|
|
|
break;
|
|
|
|
case TR_OP_BOOL_OR:
|
2020-07-27 17:50:21 +00:00
|
|
|
result = arg1Value != 0.0 || arg2Value != 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 )
|
|
|
|
{
|
|
|
|
// fixme : not operator
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-19 21:22:49 +00:00
|
|
|
for( UOP* op : m_ucode )
|
2020-07-23 20:47:38 +00:00
|
|
|
op->Exec( ctx );
|
2020-06-04 11:04:03 +00:00
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
assert( ctx->SP() == 1 );
|
|
|
|
return ctx->Pop();
|
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
|