• 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

88.65
/pol-core/bscript/compiler/optimizer/BinaryOperatorWithFloatOptimizer.cpp
1
#include "BinaryOperatorWithFloatOptimizer.h"
2

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

11
namespace Pol::Bscript::Compiler
12
{
13
BinaryOperatorWithFloatOptimizer::BinaryOperatorWithFloatOptimizer( FloatValue& lhs,
108✔
14
                                                                    BinaryOperator& op,
15
                                                                    Report& report )
108✔
16
    : lhs( lhs ), op( op ), report( report )
108✔
17
{
18
}
108✔
19

20
void BinaryOperatorWithFloatOptimizer::visit_children( Node& ) {}
4✔
21

22
void BinaryOperatorWithFloatOptimizer::visit_float_value( FloatValue& rhs )
52✔
23
{
24
  auto setInt = [&]( int val )
32✔
25
  { optimized_result = std::make_unique<IntegerValue>( op.source_location, val ); };
32✔
26
  auto setDouble = [&]( double val )
20✔
27
  { optimized_result = std::make_unique<FloatValue>( op.source_location, val ); };
20✔
28
  auto eql = []( double a, double b ) { return fabs( a - b ) < 0.00000001; };
16✔
29

30
  switch ( op.token_id )
52✔
31
  {
32
  case TOK_ADD:
8✔
33
    setDouble( lhs.value + rhs.value );
8✔
34
    break;
8✔
35
  case TOK_SUBTRACT:
2✔
36
    setDouble( lhs.value - rhs.value );
2✔
37
    break;
2✔
38
  case TOK_MULT:
6✔
39
    setDouble( lhs.value * rhs.value );
6✔
40
    break;
6✔
41
  case TOK_DIV:
2✔
42
    if ( rhs.value == 0.0 )
2✔
43
    {
44
      report.error( op, "Expression would divide by zero." );
×
45
      return;
×
46
    }
47
    setDouble( lhs.value / rhs.value );
2✔
48
    break;
2✔
49
  case TOK_MODULUS:
2✔
50
    if ( rhs.value == 0 )
2✔
51
    {
NEW
52
      report.error( op, "Program would divide by zero" );
×
NEW
53
      return;
×
54
    }
55
    setDouble( fmod( lhs.value, rhs.value ) );
2✔
56
    break;
2✔
57

58
  case TOK_EQUAL:
4✔
59
    setInt( eql( lhs.value, rhs.value ) );
4✔
60
    break;
4✔
61
  case TOK_NEQ:
4✔
62
    setInt( !eql( lhs.value, rhs.value ) );
4✔
63
    break;
4✔
64
  case TOK_LESSTHAN:
4✔
65
    setInt( lhs.value < rhs.value );
4✔
66
    break;
4✔
67
  case TOK_LESSEQ:
4✔
68
    setInt( eql( lhs.value, rhs.value ) || ( lhs.value < rhs.value ) );
4✔
69
    break;
4✔
70
  case TOK_GRTHAN:
4✔
71
    setInt( lhs.value > rhs.value );
4✔
72
    break;
4✔
73
  case TOK_GREQ:
4✔
74
    setInt( eql( lhs.value, rhs.value ) || ( lhs.value > rhs.value ) );
4✔
75
    break;
4✔
76
  case TOK_AND:
4✔
77
    setInt( lhs.value != 0.0 && rhs.value != 0.0 );
4✔
78
    break;
4✔
79
  case TOK_OR:
4✔
80
    setInt( lhs.value != 0.0 || rhs.value != 0.0 );
4✔
81
    break;
4✔
82
  default:
×
83
    return;
×
84
  }
85
}
86

87
void BinaryOperatorWithFloatOptimizer::visit_integer_value( IntegerValue& rhs )
42✔
88
{
89
  auto setInt = [&]( int val )
32✔
90
  { optimized_result = std::make_unique<IntegerValue>( op.source_location, val ); };
32✔
91
  auto setDouble = [&]( double val )
10✔
92
  { optimized_result = std::make_unique<FloatValue>( op.source_location, val ); };
10✔
93
  auto eql = []( double a, int b ) { return fabs( a - b ) < 0.00000001; };
16✔
94
  switch ( op.token_id )
42✔
95
  {
96
  case TOK_ADD:
2✔
97
    setDouble( lhs.value + rhs.value );
2✔
98
    break;
2✔
99
  case TOK_SUBTRACT:
2✔
100
    setDouble( lhs.value - rhs.value );
2✔
101
    break;
2✔
102
  case TOK_MULT:
2✔
103
    setDouble( lhs.value * rhs.value );
2✔
104
    break;
2✔
105
  case TOK_DIV:
2✔
106
    if ( rhs.value == 0 )
2✔
107
    {
NEW
108
      report.error( op, "Program would divide by zero" );
×
NEW
109
      return;
×
110
    }
111
    setDouble( lhs.value / rhs.value );
2✔
112
    break;
2✔
113
  case TOK_MODULUS:
2✔
114
    if ( rhs.value == 0 )
2✔
115
    {
NEW
116
      report.error( op, "Program would divide by zero" );
×
NEW
117
      return;
×
118
    }
119
    setDouble( fmod( lhs.value, rhs.value ) );
2✔
120
    break;
2✔
121
  case TOK_EQUAL:
4✔
122
    setInt( eql( lhs.value, rhs.value ) );
4✔
123
    break;
4✔
124
  case TOK_NEQ:
4✔
125
    setInt( !eql( lhs.value, rhs.value ) );
4✔
126
    break;
4✔
127
  case TOK_LESSTHAN:
4✔
128
    setInt( lhs.value < rhs.value );
4✔
129
    break;
4✔
130
  case TOK_LESSEQ:
4✔
131
    setInt( eql( lhs.value, rhs.value ) || ( lhs.value < rhs.value ) );
4✔
132
    break;
4✔
133
  case TOK_GRTHAN:
4✔
134
    setInt( lhs.value > rhs.value );
4✔
135
    break;
4✔
136
  case TOK_GREQ:
4✔
137
    setInt( eql( lhs.value, rhs.value ) || ( lhs.value > rhs.value ) );
4✔
138
    break;
4✔
139
  case TOK_AND:
4✔
140
    setInt( lhs.value != 0.0 && rhs.value != 0 );
4✔
141
    break;
4✔
142
  case TOK_OR:
4✔
143
    setInt( lhs.value != 0.0 || rhs.value != 0 );
4✔
144
    break;
4✔
NEW
145
  default:
×
NEW
146
    break;
×
147
  }
148
}
149

150
void BinaryOperatorWithFloatOptimizer::visit_string_value( StringValue& rhs )
2✔
151
{
152
  auto setString = [&]( std::string&& val )
2✔
153
  { optimized_result = std::make_unique<StringValue>( op.source_location, val ); };
2✔
154
  switch ( op.token_id )
2✔
155
  {
156
  case TOK_ADD:
2✔
157
    setString( fmt::format( "{}{}", Bscript::Double::double_to_string( lhs.value ), rhs.value ) );
4✔
158
    break;
2✔
NEW
159
  default:
×
NEW
160
    break;
×
161
  }
162
}
2✔
163

164
void BinaryOperatorWithFloatOptimizer::visit_boolean_value( BooleanValue& rhs )
8✔
165
{
166
  bool bval = false;
8✔
167
  switch ( op.token_id )
8✔
168
  {
169
  case TOK_EQUAL:
2✔
170
    bval = ( lhs.value != 0.0 ) == rhs.value;
2✔
171
    break;
2✔
172
  case TOK_NEQ:
2✔
173
    bval = ( lhs.value != 0.0 ) != rhs.value;
2✔
174
    break;
2✔
175
  case TOK_OR:
2✔
176
    bval = ( lhs.value != 0.0 ) || rhs.value;
2✔
177
    break;
2✔
178
  case TOK_AND:
2✔
179
    bval = ( lhs.value != 0.0 ) && rhs.value;
2✔
180
    break;
2✔
NEW
181
  default:
×
NEW
182
    return;
×
183
  }
184

185
  // Boolean logic operators return 1/0 as BLong, ie. `true || false` == `1`
186
  optimized_result = std::make_unique<IntegerValue>( op.source_location, bval );
8✔
187
}
188
}  // 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