2017-10-11 19:41:29 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2017- Statoil ASA
|
2026-02-23 11:19:16 +01:00
|
|
|
//
|
2017-10-11 19:41:29 +02:00
|
|
|
// ResInsight 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.
|
2026-02-23 11:19:16 +01:00
|
|
|
//
|
2017-10-11 19:41:29 +02:00
|
|
|
// ResInsight 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.
|
2026-02-23 11:19:16 +01:00
|
|
|
//
|
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
2017-10-11 19:41:29 +02:00
|
|
|
// for more details.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#include "ExpressionParser.h"
|
|
|
|
|
|
|
|
|
|
#include "ExpressionParserImpl.h"
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
///
|
2017-10-11 19:41:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
ExpressionParser::ExpressionParser()
|
|
|
|
|
{
|
2026-02-23 11:19:16 +01:00
|
|
|
m_expressionParserImpl = std::unique_ptr<ExpressionParserImpl>( new ExpressionParserImpl );
|
2017-10-11 19:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
///
|
2017-10-11 19:41:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
ExpressionParser::~ExpressionParser()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
///
|
2017-10-11 19:41:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
std::vector<QString> ExpressionParser::detectReferencedVariables( const QString& expression )
|
2017-10-11 19:41:29 +02:00
|
|
|
{
|
2026-02-23 11:19:16 +01:00
|
|
|
return ExpressionParserImpl::detectReferencedVariables( expression );
|
2017-10-11 19:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
///
|
2017-10-11 19:41:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
void ExpressionParser::assignVector( const QString& variableName, std::vector<double>& vector )
|
2017-10-11 19:41:29 +02:00
|
|
|
{
|
2026-02-23 11:19:16 +01:00
|
|
|
m_expressionParserImpl->assignVector( variableName, vector );
|
2017-10-11 19:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
///
|
2017-10-11 19:41:29 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
std::expected<void, QString> ExpressionParser::evaluate( const QString& expressionText )
|
2017-10-11 19:41:29 +02:00
|
|
|
{
|
2026-02-23 11:19:16 +01:00
|
|
|
return m_expressionParserImpl->evaluate( expressionText );
|
2017-10-11 19:41:29 +02:00
|
|
|
}
|
2021-02-01 18:38:52 +01:00
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
///
|
2021-02-01 18:38:52 +01:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-02-23 11:19:16 +01:00
|
|
|
std::expected<void, QString> ExpressionParser::expandIfStatementsAndEvaluate( const QString& expressionText )
|
2021-02-01 18:38:52 +01:00
|
|
|
{
|
2026-02-23 11:19:16 +01:00
|
|
|
if ( expressionText.contains( "if", Qt::CaseInsensitive ) )
|
2021-02-01 18:38:52 +01:00
|
|
|
{
|
2026-02-23 11:19:16 +01:00
|
|
|
QString expanded = ExpressionParserImpl::expandIfStatements( expressionText );
|
|
|
|
|
return m_expressionParserImpl->evaluate( expanded );
|
2021-02-01 18:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-23 11:19:16 +01:00
|
|
|
return m_expressionParserImpl->evaluate( expressionText );
|
2021-02-01 18:38:52 +01:00
|
|
|
}
|