Skip to content

Commit 15975e6

Browse files
committed
Fix parentheses in subtractions (StdStr generator)
1 parent 922e9df commit 15975e6

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

src/stdstr.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function parenthesize(::ir.Sub, f, arg)
4040
end
4141

4242
function parenthesize(::ir.Sub, f, arg::ir.Add)
43-
return f(arg)
43+
return "(" * f(arg) * ")"
4444
end
4545

4646
function parenthesize(
@@ -126,9 +126,7 @@ function to_std_str(arg::ir.Add)
126126
end
127127

128128
function to_std_str(arg::ir.Sub)
129-
return parenthesize(arg, to_std_str, arg.l) *
130-
" - " *
131-
parenthesize(arg, to_std_str, arg.r)
129+
return to_std_str(arg.l) * " - " * parenthesize(arg, to_std_str, arg.r)
132130
end
133131

134132
function to_std_str(arg::ir.Product)

test/StdTest.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ end
284284
@test to_std(sum(y)) == "sum(yᵀ)"
285285
end
286286

287+
@testset "to_std output is correct with difference involving additive operations" begin
288+
x = Variable("x", Upper(1))
289+
y = Variable("y", Upper(1))
290+
z = Variable("z", Upper(1))
291+
292+
@test to_std(x - (y + z)) == "x - (y + z)"
293+
@test to_std((x + y) - z) == "x + y - z"
294+
end
295+
287296
@testset "to_std output is correct with abs" begin
288297
x = Variable("x", Upper(2))
289298
A = Variable("A", Upper(1), Lower(2))

0 commit comments

Comments
 (0)