mirror of
https://github.com/polserver/polserver
synced 2026-08-13 08:23:08 -04:00
* 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
124 lines
3.3 KiB
C++
124 lines
3.3 KiB
C++
#include "BinaryOperatorWithBooleanOptimizer.h"
|
|
|
|
#include "bscript/compiler/Report.h"
|
|
#include "bscript/compiler/ast/BinaryOperator.h"
|
|
#include "bscript/compiler/ast/BooleanValue.h"
|
|
#include "bscript/compiler/ast/FloatValue.h"
|
|
#include "bscript/compiler/ast/IntegerValue.h"
|
|
#include "bscript/compiler/ast/StringValue.h"
|
|
|
|
namespace Pol::Bscript::Compiler
|
|
{
|
|
BinaryOperatorWithBooleanOptimizer::BinaryOperatorWithBooleanOptimizer( BooleanValue& lhs,
|
|
BinaryOperator& op,
|
|
Report& report )
|
|
: lhs( lhs ), op( op ), report( report )
|
|
{
|
|
}
|
|
|
|
void BinaryOperatorWithBooleanOptimizer::visit_children( Node& ) {}
|
|
|
|
void BinaryOperatorWithBooleanOptimizer::visit_boolean_value( BooleanValue& rhs )
|
|
{
|
|
bool bval = false;
|
|
switch ( op.token_id )
|
|
{
|
|
case TOK_EQUAL:
|
|
bval = lhs.value == rhs.value;
|
|
break;
|
|
case TOK_NEQ:
|
|
bval = lhs.value != rhs.value;
|
|
break;
|
|
case TOK_OR:
|
|
bval = lhs.value || rhs.value;
|
|
break;
|
|
case TOK_AND:
|
|
bval = lhs.value && rhs.value;
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
|
|
// Boolean logic operators return 1/0 as BLong, ie. `true || false` == `1`
|
|
optimized_result = std::make_unique<IntegerValue>( op.source_location, bval );
|
|
}
|
|
|
|
void BinaryOperatorWithBooleanOptimizer::visit_integer_value( IntegerValue& rhs )
|
|
{
|
|
bool bval = false;
|
|
switch ( op.token_id )
|
|
{
|
|
case TOK_EQUAL:
|
|
bval = lhs.value == ( rhs.value != 0 );
|
|
break;
|
|
case TOK_NEQ:
|
|
bval = lhs.value != ( rhs.value != 0 );
|
|
break;
|
|
case TOK_OR:
|
|
bval = lhs.value || rhs.value;
|
|
break;
|
|
case TOK_AND:
|
|
bval = lhs.value && rhs.value;
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
|
|
// Boolean logic operators return 1/0 as BLong, ie. `true || false` == `1`
|
|
optimized_result = std::make_unique<IntegerValue>( op.source_location, bval );
|
|
}
|
|
|
|
void BinaryOperatorWithBooleanOptimizer::visit_float_value( FloatValue& rhs )
|
|
{
|
|
bool bval = false;
|
|
switch ( op.token_id )
|
|
{
|
|
case TOK_EQUAL:
|
|
bval = lhs.value == ( rhs.value != 0.0 );
|
|
break;
|
|
case TOK_NEQ:
|
|
bval = lhs.value != ( rhs.value != 0.0 );
|
|
break;
|
|
case TOK_OR:
|
|
bval = lhs.value || ( rhs.value != 0.0 );
|
|
break;
|
|
case TOK_AND:
|
|
bval = lhs.value && ( rhs.value != 0.0 );
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
|
|
// Boolean logic operators return 1/0 as BLong, ie. `true || false` == `1`
|
|
optimized_result = std::make_unique<IntegerValue>( op.source_location, bval );
|
|
}
|
|
|
|
void BinaryOperatorWithBooleanOptimizer::visit_string_value( StringValue& rhs )
|
|
{
|
|
auto setString = [&]( std::string&& val )
|
|
{ optimized_result = std::make_unique<StringValue>( op.source_location, val ); };
|
|
auto setInt = [&]( int val )
|
|
{ optimized_result = std::make_unique<IntegerValue>( op.source_location, val ); };
|
|
switch ( op.token_id )
|
|
{
|
|
case TOK_ADD:
|
|
setString( fmt::format( "{}{}", lhs.value, rhs.value ) );
|
|
break;
|
|
case TOK_EQUAL:
|
|
setInt( lhs.value == !rhs.value.empty() );
|
|
break;
|
|
case TOK_NEQ:
|
|
setInt( lhs.value != !rhs.value.empty() );
|
|
break;
|
|
case TOK_OR:
|
|
setInt( lhs.value || !rhs.value.empty() );
|
|
break;
|
|
case TOK_AND:
|
|
setInt( lhs.value && !rhs.value.empty() );
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
} // namespace Pol::Bscript::Compiler
|