Skip to content

Commit

Permalink
render post expr blocks better (nim-lang#20871)
Browse files Browse the repository at this point in the history
* render post expr blocks

* remove pointless diff

* fix PR split mistake
  • Loading branch information
metagn authored Nov 22, 2022
1 parent 09b7f90 commit 5adfaa2
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 23 deletions.
65 changes: 42 additions & 23 deletions compiler/renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ proc doParamsAux(g: var TSrcGen, params: PNode) =
put(g, tkParRi, ")")

if params.len > 0 and params[0].kind != nkEmpty:
put(g, tkSpaces, Space)
putWithSpace(g, tkOpr, "->")
gsub(g, params[0])

Expand Down Expand Up @@ -1001,6 +1002,30 @@ proc infixArgument(g: var TSrcGen, n: PNode, i: int) =
if needsParenthesis:
put(g, tkParRi, ")")

const postExprBlocks = {nkStmtList, nkStmtListExpr,
nkOfBranch, nkElifBranch, nkElse,
nkExceptBranch, nkFinally, nkDo}

proc postStatements(g: var TSrcGen, n: PNode, i: int, fromStmtList: bool) =
var i = i
if n[i].kind in {nkStmtList, nkStmtListExpr}:
if fromStmtList:
put(g, tkColon, ":")
else:
put(g, tkSpaces, Space)
put(g, tkDo, "do")
put(g, tkColon, ":")
gsub(g, n, i)
i.inc
for j in i ..< n.len:
if n[j].kind == nkDo:
optNL(g)
elif n[j].kind in {nkStmtList, nkStmtListExpr}:
optNL(g)
put(g, tkDo, "do")
put(g, tkColon, ":")
gsub(g, n, j)

proc isCustomLit(n: PNode): bool =
if n.len == 2 and n[0].kind == nkRStrLit:
let ident = n[1].getPIdent
Expand Down Expand Up @@ -1035,27 +1060,15 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
of nkCharLit: put(g, tkCharLit, atom(g, n))
of nkNilLit: put(g, tkNil, atom(g, n)) # complex expressions
of nkCall, nkConv, nkDotCall, nkPattern, nkObjConstr:
if n.len > 1 and n.lastSon.kind in {nkStmtList, nkStmtListExpr}:
if n.len > 1 and n.lastSon.kind in postExprBlocks:
accentedName(g, n[0])
var i = 1
while i < n.len and n[i].kind notin {nkStmtList, nkStmtListExpr}: i.inc
while i < n.len and n[i].kind notin postExprBlocks: i.inc
if i > 1:
put(g, tkParLe, "(")
gcomma(g, n, 1, i - 1 - n.len)
put(g, tkParRi, ")")
if fromStmtList:
put(g, tkColon, ":")
else:
put(g, tkSpaces, Space)
put(g, tkDo, "do")
put(g, tkColon, ":")
gsub(g, n, i)
i.inc
for j in i ..< n.len:
optNL(g)
put(g, tkDo, "do")
put(g, tkColon, ":")
gsub(g, n, j)
postStatements(g, n, i, fromStmtList)
elif n.len >= 1:
case bracketKind(g, n[0])
of bkBracket:
Expand Down Expand Up @@ -1155,14 +1168,12 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
of nkCommand:
accentedName(g, n[0])
put(g, tkSpaces, Space)
if n[^1].kind == nkStmtList:
for i, child in n:
if i > 1 and i < n.len - 1:
put(g, tkComma, ",")
elif i == n.len - 1:
put(g, tkColon, ":")
if i > 0:
gsub(g, child)
if n.len > 1 and n.lastSon.kind in postExprBlocks:
var i = 1
while i < n.len and n[i].kind notin postExprBlocks: i.inc
if i > 1:
gcomma(g, n, 1, i - 1 - n.len)
postStatements(g, n, i, fromStmtList)
else:
gcomma(g, n, 1)
of nkExprEqExpr, nkAsgn, nkFastAsgn:
Expand Down Expand Up @@ -1301,6 +1312,10 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
else:
put(g, tkSpaces, Space)
infixArgument(g, n, 2)
if n.len > 3 and n.lastSon.kind in postExprBlocks:
var i = 3
while i < n.len and n[i].kind notin postExprBlocks: i.inc
postStatements(g, n, i, fromStmtList)
of nkPrefix:
gsub(g, n, 0)
if n.len > 1:
Expand All @@ -1317,6 +1332,10 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
put(g, tkParRi, ")")
else:
gsub(g, n[1])
if n.len > 2 and n.lastSon.kind in postExprBlocks:
var i = 2
while i < n.len and n[i].kind notin postExprBlocks: i.inc
postStatements(g, n, i, fromStmtList)
of nkPostfix:
gsub(g, n, 1)
gsub(g, n, 0)
Expand Down
51 changes: 51 additions & 0 deletions tests/stdlib/trepr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,56 @@ func fn2(): int =
## comment
result = 1"""

block: # block calls
let a = deb:
foo(a, b, (c, d)):
e
f
do: g
of h: i
elif j: k
except m: n
do () -> u: v
finally: o

a + b:
c
d
do:
e
f
else: g

*a: b
do: c

doAssert a == """foo(a, b, (c, d)):
e
f
do:
g
of h:
i
elif j:
k
except m:
n
do -> u:
v
finally:
o
a + b:
c
d
do:
e
f
else:
g
*a:
b
do:
c"""

static: main()
main()

0 comments on commit 5adfaa2

Please sign in to comment.