Fixes #11662: render ops priority (#11664)

(cherry picked from commit d1f6c820dd)
This commit is contained in:
cooldome
2019-07-05 20:25:36 +01:00
committed by narimiran
parent 88896194bd
commit cb3a920097
2 changed files with 12 additions and 4 deletions

View File

@@ -898,16 +898,19 @@ proc accentedName(g: var TSrcGen, n: PNode) =
gsub(g, n)
proc infixArgument(g: var TSrcGen, n: PNode, i: int) =
if i >= n.len: return
if i < 1 and i > 2: return
var needsParenthesis = false
let n_next = n[i].skipHiddenNodes
if n_next.kind == nkInfix:
if n_next[0].kind in {nkSym, nkIdent} and n[0].kind in {nkSym, nkIdent}:
let nextId = if n_next[0].kind == nkSym: n_next[0].sym.name else: n_next[0].ident
let nnId = if n[0].kind == nkSym: n[0].sym.name else: n[0].ident
if getPrecedence(nextId) < getPrecedence(nnId):
needsParenthesis = true
if i == 1:
if getPrecedence(nextId) < getPrecedence(nnId):
needsParenthesis = true
elif i == 2:
if getPrecedence(nextId) <= getPrecedence(nnId):
needsParenthesis = true
if needsParenthesis:
put(g, tkParLe, "(")
gsub(g, n, i)

View File

@@ -97,6 +97,9 @@ proc fn4(x: int): int =
if x mod 2 == 0: return x + 2
else: return 0
proc fn5(a, b: float): float =
result = - a * a / (b * b)
#------------------------------------
# bug #10807
proc fn_unsafeaddr(x: int): int =
@@ -108,12 +111,14 @@ static:
let fn2s = "proc fn2(x, y: float): float =\n result = (y + 2 * x) / (x - y)\n"
let fn3s = "proc fn3(x, y: int): bool =\n result = ((x and 3) div 4 or x mod (y xor -1)) == 0 or not contains([1, 2], y)\n"
let fn4s = "proc fn4(x: int): int =\n if x mod 2 == 0:\n return x + 2\n else:\n return 0\n"
let fn5s = "proc fn5(a, b: float): float =\n result = -a * a / (b * b)\n"
let fnAddr = "proc fn_unsafeaddr(x: int): int =\n result = cast[int](unsafeAddr(x))\n"
doAssert fn1.repr_to_string == fn1s
doAssert fn2.repr_to_string == fn2s
doAssert fn3.repr_to_string == fn3s
doAssert fn4.repr_to_string == fn4s
doAssert fn5.repr_to_string == fn5s
doAssert fn_unsafeaddr.repr_to_string == fnAddr
#------------------------------------