• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polserver / polserver / 16205400460

10 Jul 2025 08:32PM UTC coverage: 59.527% (+0.2%) from 59.304%
16205400460

push

github

web-flow
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

392 of 435 new or added lines in 16 files covered. (90.11%)

6 existing lines in 2 files now uncovered.

43324 of 72780 relevant lines covered (59.53%)

448442.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.02
/pol-core/bscript/compiler/optimizer/BinaryOperatorWithBooleanOptimizer.cpp
1
#include "BinaryOperatorWithBooleanOptimizer.h"
2

3
#include "bscript/compiler/Report.h"
4
#include "bscript/compiler/ast/BinaryOperator.h"
5
#include "bscript/compiler/ast/BooleanValue.h"
6
#include "bscript/compiler/ast/FloatValue.h"
7
#include "bscript/compiler/ast/IntegerValue.h"
8
#include "bscript/compiler/ast/StringValue.h"
9

10
namespace Pol::Bscript::Compiler
11
{
12
BinaryOperatorWithBooleanOptimizer::BinaryOperatorWithBooleanOptimizer( BooleanValue& lhs,
42✔
13
                                                                        BinaryOperator& op,
14
                                                                        Report& report )
42✔
15
    : lhs( lhs ), op( op ), report( report )
42✔
16
{
17
}
42✔
18

19
void BinaryOperatorWithBooleanOptimizer::visit_children( Node& ) {}
×
20

21
void BinaryOperatorWithBooleanOptimizer::visit_boolean_value( BooleanValue& rhs )
16✔
22
{
23
  bool bval = false;
16✔
24
  switch ( op.token_id )
16✔
25
  {
26
  case TOK_EQUAL:
2✔
27
    bval = lhs.value == rhs.value;
2✔
28
    break;
2✔
29
  case TOK_NEQ:
2✔
30
    bval = lhs.value != rhs.value;
2✔
31
    break;
2✔
32
  case TOK_OR:
6✔
33
    bval = lhs.value || rhs.value;
6✔
34
    break;
6✔
35
  case TOK_AND:
6✔
36
    bval = lhs.value && rhs.value;
6✔
37
    break;
6✔
38
  default:
×
39
    return;
×
40
  }
41

42
  // Boolean logic operators return 1/0 as BLong, ie. `true || false` == `1`
43
  optimized_result = std::make_unique<IntegerValue>( op.source_location, bval );
16✔
44
}
45

46
void BinaryOperatorWithBooleanOptimizer::visit_integer_value( IntegerValue& rhs )
8✔
47
{
48
  bool bval = false;
8✔
49
  switch ( op.token_id )
8✔
50
  {
51
  case TOK_EQUAL:
2✔
52
    bval = lhs.value == ( rhs.value != 0 );
2✔
53
    break;
2✔
54
  case TOK_NEQ:
2✔
55
    bval = lhs.value != ( rhs.value != 0 );
2✔
56
    break;
2✔
57
  case TOK_OR:
2✔
58
    bval = lhs.value || rhs.value;
2✔
59
    break;
2✔
60
  case TOK_AND:
2✔
61
    bval = lhs.value && rhs.value;
2✔
62
    break;
2✔
NEW
63
  default:
×
NEW
64
    return;
×
65
  }
66

67
  // Boolean logic operators return 1/0 as BLong, ie. `true || false` == `1`
68
  optimized_result = std::make_unique<IntegerValue>( op.source_location, bval );
8✔
69
}
70

71
void BinaryOperatorWithBooleanOptimizer::visit_float_value( FloatValue& rhs )
8✔
72
{
73
  bool bval = false;
8✔
74
  switch ( op.token_id )
8✔
75
  {
76
  case TOK_EQUAL:
2✔
77
    bval = lhs.value == ( rhs.value != 0.0 );
2✔
78
    break;
2✔
79
  case TOK_NEQ:
2✔
80
    bval = lhs.value != ( rhs.value != 0.0 );
2✔
81
    break;
2✔
82
  case TOK_OR:
2✔
83
    bval = lhs.value || ( rhs.value != 0.0 );
2✔
84
    break;
2✔
85
  case TOK_AND:
2✔
86
    bval = lhs.value && ( rhs.value != 0.0 );
2✔
87
    break;
2✔
NEW
88
  default:
×
NEW
89
    return;
×
90
  }
91

92
  // Boolean logic operators return 1/0 as BLong, ie. `true || false` == `1`
93
  optimized_result = std::make_unique<IntegerValue>( op.source_location, bval );
8✔
94
}
95

96
void BinaryOperatorWithBooleanOptimizer::visit_string_value( StringValue& rhs )
10✔
97
{
98
  auto setString = [&]( std::string&& val )
2✔
99
  { optimized_result = std::make_unique<StringValue>( op.source_location, val ); };
2✔
100
  auto setInt = [&]( int val )
8✔
101
  { optimized_result = std::make_unique<IntegerValue>( op.source_location, val ); };
8✔
102
  switch ( op.token_id )
10✔
103
  {
104
  case TOK_ADD:
2✔
105
    setString( fmt::format( "{}{}", lhs.value, rhs.value ) );
4✔
106
    break;
2✔
107
  case TOK_EQUAL:
2✔
108
    setInt( lhs.value == !rhs.value.empty() );
2✔
109
    break;
2✔
110
  case TOK_NEQ:
2✔
111
    setInt( lhs.value != !rhs.value.empty() );
2✔
112
    break;
2✔
113
  case TOK_OR:
2✔
114
    setInt( lhs.value || !rhs.value.empty() );
2✔
115
    break;
2✔
116
  case TOK_AND:
2✔
117
    setInt( lhs.value && !rhs.value.empty() );
2✔
118
    break;
2✔
NEW
119
  default:
×
NEW
120
    return;
×
121
  }
122
}
123

124
}  // namespace Pol::Bscript::Compiler
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc