Commit graph

5 commits

Author SHA1 Message Date
Eric Swanson
cc1b0c6ee3 fix optimizer bug: can stop optimizing prematurely if pre- or post-decrement operators are present
optimize_token() is meant to return true only if it performs an optimization.

By returning true when it did not, it makes the optimizer stop trying.

A script like this can trigger the condition:

var i := 3;
var ii;

ii := 1 - (--i);
print(i);
print(ii);

Notice that line 4 does not optimize to an 'assign-consume' and therefore does not optimize to 'assign global`

Listfile before (see instructions 6-12):

/vagrant/testsuite/escript/opt/opt005-stopped-optimizing-early.src, Line 1
var i := 3;
0: decl global #0
1: 3L
2: :=
3: #
var ii;
4: decl global #1
5: #
ii := 1 - (--i);
6: global #1
7: 1L
8: global #0
9: unary --
10: -
11: :=
12: #
print(i);
13: global #0
14: Func(1,0): Print
15: #
print(ii);
16: global #1
17: Func(1,0): Print
18: #
19: progend

Listfile after (see instructions 6-10):

/vagrant/testsuite/escript/opt/opt005-stopped-optimizing-early.src, Line 1
var i := 3;
0: decl global #0
1: 3L
2: :=
3: #
var ii;
4: decl global #1
5: #
ii := 1 - (--i);
6: 1L
7: global #0
8: unary --
9: -
10: global1 :=
print(i);
11: global #0
12: Func(1,0): Print
13: #
print(ii);
14: global #1
15: Func(1,0): Print
16: #
17: progend
2020-07-29 20:42:07 -07:00
Eric Swanson
36d8603666 Add elvis operator:
a ?: b

Evaluates to a if a is true (not: false, 0, undefined, etc)
Otherwise evaluates to b

Short-circuit evaluation
2020-04-29 02:50:11 -07:00
Eric Swanson
0e4d54bf2b
Remove TYP_USERFUNC tokens during expression optimization (#136)
* Remove TYP_USERFUNC tokens during expression optimization, rather than when emitting tokens
2020-04-29 02:38:52 -07:00
Eric Swanson
88e22c1fcb
Make getUserArgs optimize arguments, rename Expression::eat2 -> consume_tokens (#134)
* getUserArgs: optimize arguments immediately

This would have also happened when the whole expression (function call + arguments) was processed.

Doing it here reduces the number of places that call IIP directly.

* remove dead code Expression::eat

* rename Expression::eat2() to consume_tokens(), and modernize
2020-04-28 14:02:16 -07:00
Eric Swanson
df5c3ced7b Move Expression class to expression.cpp/h 2020-04-24 13:37:26 -07:00