Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrap conditional branch expression in parentheses when optimizing IF THEN ELSE #153

Merged
merged 3 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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