diff options
author | metagn <metagngn@gmail.com> | 2022-11-22 22:40:05 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-22 20:40:05 +0100 |
commit | 5adfaa2a9247322f5dd3a0f911e0dea25a616b14 (patch) | |
tree | ae618418d9f07e631777bc6875de70dfb857c7df /compiler/renderer.nim | |
parent | 09b7f90475006c58139da5b959b6ea3f47c0e5c1 (diff) | |
download | Nim-5adfaa2a9247322f5dd3a0f911e0dea25a616b14.tar.gz |
render post expr blocks better (#20871)
* render post expr blocks * remove pointless diff * fix PR split mistake
Diffstat (limited to 'compiler/renderer.nim')
-rw-r--r-- | compiler/renderer.nim | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/compiler/renderer.nim b/compiler/renderer.nim index f975dc896..61bd9cf62 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -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]) @@ -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 @@ -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: @@ -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: @@ -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: @@ -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) |