Skip to content

Commit

Permalink
fix: wrap conditional branch expression in parentheses when optimizin…
Browse files Browse the repository at this point in the history
…g IF THEN ELSE (#153)

Fixes #152
  • Loading branch information
chrispcampbell authored Oct 28, 2021
1 parent 6575ea9 commit bd42d54
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
20 changes: 20 additions & 0 deletions models/prune/prune.dat
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ Test 12 Result
10 1
Test 12 T
0 1
Test 13 Cond
0 1
Test 13 F
0 2
Test 13 Result
0 80
1 80
2 80
3 80
4 80
5 80
6 80
7 80
8 80
9 80
10 80
Test 13 T1
0 1
Test 13 T2
0 7
Test 2 F
0 2
Test 2 Result
Expand Down
22 changes: 22 additions & 0 deletions models/prune/prune.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,28 @@ Test 12 Result = IF THEN ELSE(Test 12 Cond :OR: ABS(Test 12 Cond), Test 12 T, Te
~ :SUPPLEMENTARY
|

Test 13 Cond = 1
~ dmnl
~ |

Test 13 T1 = 1
~ dmnl
~ |

Test 13 T2 = 7
~ dmnl
~ |

Test 13 F = 2
~ dmnl
~ |

Test 13 Result = IF THEN ELSE(Test 13 Cond, Test 13 T1 + Test 13 T2, Test 13 F) * 10.0
~ dmnl
~ Should generate "(Test 13 T1 + Test 13 T2) * 10.0" with parentheses included.
~ :SUPPLEMENTARY
|

********************************************************
.Control
********************************************************~
Expand Down
23 changes: 13 additions & 10 deletions models/prune/prune_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ expect_present "_constant_partial_2"
expect_present "_initial_partial"
expect_present "_partial"
expect_present "_test_1_result = _IF_THEN_ELSE(_input_1 == 10.0, _test_1_t, _test_1_f);"
expect_present "_test_2_result = _test_2_f;"
expect_present "_test_3_result = _test_3_t;"
expect_present "_test_4_result = _test_4_f;"
expect_present "_test_5_result = _test_5_t;"
expect_present "_test_6_result = _test_6_f;"
expect_present "_test_7_result = _test_7_t;"
expect_present "_test_8_result = _test_8_f;"
expect_present "_test_9_result = _test_9_t;"
expect_present "_test_2_result = (_test_2_f);"
expect_present "_test_3_result = (_test_3_t);"
expect_present "_test_4_result = (_test_4_f);"
expect_present "_test_5_result = (_test_5_t);"
expect_present "_test_6_result = (_test_6_f);"
expect_present "_test_7_result = (_test_7_t);"
expect_present "_test_8_result = (_test_8_f);"
expect_present "_test_9_result = (_test_9_t);"
expect_present "_test_10_result = _IF_THEN_ELSE(_ABS(_test_10_cond), _test_10_t, _test_10_f);"
expect_present "_test_11_result = _test_11_f;"
expect_present "_test_12_result = _test_12_t;"
expect_present "_test_11_result = (_test_11_f);"
expect_present "_test_12_result = (_test_12_t);"
expect_present "_test_13_result = (_test_13_t1 + _test_13_t2) \* 10.0;"

# Verify that unreferenced variables do not appear in the generated C file
expect_not_present "_input_3"
Expand Down Expand Up @@ -88,5 +89,7 @@ expect_not_present "_test_11_cond"
expect_not_present "_test_11_t"
expect_not_present "_test_12_cond"
expect_not_present "_test_12_f"
expect_not_present "_test_13_cond"
expect_not_present "_test_13_f"

echo "All validation checks passed!"
3 changes: 2 additions & 1 deletion models/prune/prune_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"Test 9 Result",
"Test 10 Result",
"Test 11 Result",
"Test 12 Result"
"Test 12 Result",
"Test 13 Result"
],
"externalDatfiles": ["prune_data.dat"]
}
2 changes: 2 additions & 0 deletions src/EquationGen.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ export default class EquationGen extends ModelReader {
const condText = ctx.expr(0).getText()
const condValue = Model.getConstantExprValue(condText)
if (condValue !== undefined) {
this.emit('(')
if (condValue !== 0) {
// Emit only the "if true" branch
this.setArgIndex(1)
Expand All @@ -890,6 +891,7 @@ export default class EquationGen extends ModelReader {
this.setArgIndex(2)
ctx.expr(2).accept(this)
}
this.emit(')')
} else {
// Emit a normal if/else with both branches
this.emit(fn)
Expand Down

0 comments on commit bd42d54

Please sign in to comment.