2020-08-30 17:03:26 -07:00
|
|
|
#include "IfThenElseStatement.h"
|
|
|
|
|
|
|
|
|
|
|
2021-02-25 16:53:22 +01:00
|
|
|
#include "bscript/compiler/ast/Block.h"
|
|
|
|
|
#include "bscript/compiler/ast/BranchSelector.h"
|
|
|
|
|
#include "bscript/compiler/ast/Expression.h"
|
|
|
|
|
#include "bscript/compiler/ast/NodeVisitor.h"
|
2020-08-30 17:03:26 -07:00
|
|
|
|
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
|
|
|
{
|
|
|
|
|
IfThenElseStatement::IfThenElseStatement( const SourceLocation& source_location,
|
2020-09-07 09:53:24 -07:00
|
|
|
std::unique_ptr<BranchSelector> branch_selector,
|
2020-08-30 17:03:26 -07:00
|
|
|
std::unique_ptr<Block> consequent,
|
|
|
|
|
std::unique_ptr<Node> alternative )
|
|
|
|
|
: Statement( source_location )
|
|
|
|
|
{
|
|
|
|
|
children.reserve( 3 );
|
2020-09-07 09:53:24 -07:00
|
|
|
children.push_back( std::move( branch_selector ) );
|
2020-08-30 17:03:26 -07:00
|
|
|
children.push_back( std::move( consequent ) );
|
|
|
|
|
children.push_back( std::move( alternative ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IfThenElseStatement::IfThenElseStatement( const SourceLocation& source_location,
|
2020-09-07 09:53:24 -07:00
|
|
|
std::unique_ptr<BranchSelector> branch_selector,
|
2020-08-30 17:03:26 -07:00
|
|
|
std::unique_ptr<Block> consequent )
|
|
|
|
|
: Statement( source_location )
|
|
|
|
|
{
|
|
|
|
|
children.reserve( 2 );
|
2020-09-07 09:53:24 -07:00
|
|
|
children.push_back( std::move( branch_selector ) );
|
2020-08-30 17:03:26 -07:00
|
|
|
children.push_back( std::move( consequent ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IfThenElseStatement::accept( NodeVisitor& visitor )
|
|
|
|
|
{
|
|
|
|
|
visitor.visit_if_then_else_statement( *this );
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-12 06:51:44 +01:00
|
|
|
void IfThenElseStatement::describe_to( std::string& w ) const
|
2020-08-30 17:03:26 -07:00
|
|
|
{
|
2024-01-12 06:51:44 +01:00
|
|
|
w += "if-then-else-statement";
|
2020-08-30 17:03:26 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-07 09:53:24 -07:00
|
|
|
BranchSelector& IfThenElseStatement::branch_selector()
|
2020-08-30 17:03:26 -07:00
|
|
|
{
|
2020-09-07 09:53:24 -07:00
|
|
|
return child<BranchSelector>( 0 );
|
2020-08-30 17:03:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Block& IfThenElseStatement::consequent()
|
|
|
|
|
{
|
|
|
|
|
return child<Block>( 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Node* IfThenElseStatement::alternative()
|
|
|
|
|
{
|
|
|
|
|
return ( children.size() >= 3 ) ? children.at( 2 ).get() : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<Block> IfThenElseStatement::take_consequent()
|
|
|
|
|
{
|
|
|
|
|
return take_child<Block>( 1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<Node> IfThenElseStatement::take_alternative()
|
|
|
|
|
{
|
|
|
|
|
if ( children.size() >= 3 )
|
|
|
|
|
return std::move( children[2] );
|
|
|
|
|
else
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Pol::Bscript::Compiler
|