polserver/pol-core/bscript/compiler/ast/ConditionalOperator.cpp
turleypol c0bfd2f820
Escript Optimizer more types and operator (#794)
* compile time optimization:
int with doubles and strings
doubles with ints and strings
strings with ints and doubles

* bool with other types, more unary ops, float branch optimizer

* more tests
fixed bool to dbl compare

* output cleanup
more tests

* optimize string values in if statements, optimize ternary operator

* optimize elvis, addes missing files, code cleanup

* use array to keep unoptimized if branch in funcexpr tests

* missing include

* better readable testdata

* removed file

* optimize while and dowhile loops if predicate is a compile time known
value

* cleaner variant of loop optimization?

* added ConstantPredicateLoop Node used by the optimizer for constant loop
predicates
optimize repeat until loop
change tests to run the loops more then once to be sure they work
correctly

* test break/continue with label for constant-loop

* docs
2025-07-10 22:32:29 +02:00

59 lines
1.5 KiB
C++

#include "ConditionalOperator.h"
#include "bscript/compiler/ast/NodeVisitor.h"
#include "bscript/compiler/model/FlowControlLabel.h"
namespace Pol::Bscript::Compiler
{
ConditionalOperator::ConditionalOperator( const SourceLocation& source_location,
std::unique_ptr<Expression> conditional,
std::unique_ptr<Expression> consequent,
std::unique_ptr<Expression> alternate )
: Expression( source_location ),
consequent_label( std::make_shared<FlowControlLabel>() ),
alternate_label( std::make_shared<FlowControlLabel>() )
{
children.reserve( 3 );
children.push_back( std::move( conditional ) );
children.push_back( std::move( consequent ) );
children.push_back( std::move( alternate ) );
}
void ConditionalOperator::accept( NodeVisitor& visitor )
{
visitor.visit_conditional_operator( *this );
}
void ConditionalOperator::describe_to( std::string& w ) const
{
w += "conditional-expression";
}
Expression& ConditionalOperator::conditional()
{
return child<Expression>( 0 );
}
Expression& ConditionalOperator::consequent()
{
return child<Expression>( 1 );
}
Expression& ConditionalOperator::alternate()
{
return child<Expression>( 2 );
}
std::unique_ptr<Expression> ConditionalOperator::take_consequent()
{
return take_child<Expression>( 1 );
}
std::unique_ptr<Expression> ConditionalOperator::take_alternate()
{
return take_child<Expression>( 2 );
}
} // namespace Pol::Bscript::Compiler