Skip to content

Commit 002b1ba

Browse files
authored
fixes asNimCode for QuotedX (#1207)
The test is not irrelevant. It fixes some code with `quoted`, which is reported during sem phases
1 parent e26a182 commit 002b1ba

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/nimony/renderer.nim

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,27 @@ proc gpragmaBlock(g: var SrcGen, n: var Cursor) =
11421142
gstmts(g, n, c, doIndent = true)
11431143
skipParRi(n)
11441144

1145+
proc isUseSpace(n: Cursor): bool =
1146+
template isAlpha(s: Cursor): bool =
1147+
pool.strings[s.litId][0] in {'a'..'z', 'A'..'Z'}
1148+
1149+
result = true
1150+
var n = n
1151+
1152+
assert n.kind != ParRi
1153+
let firstSon = n
1154+
skip n
1155+
1156+
if n.kind != ParRi:
1157+
let secondSon = n
1158+
skip n
1159+
if n.kind == ParRi:
1160+
assert firstSon.kind == Ident and secondSon.kind == Ident
1161+
# handle `=destroy`, `'big' and handle setters, e.g. `foo=`
1162+
if (pool.strings[firstSon.litId] in ["=", "'"] and isAlpha(secondSon)) or
1163+
(pool.strings[secondSon.litId] == "=" and isAlpha(firstSon)):
1164+
result = false
1165+
11451166
proc gsub(g: var SrcGen, n: var Cursor, c: Context, fromStmtList = false, isTopLevel = false) =
11461167
case n.kind
11471168
of ParLe:
@@ -1732,8 +1753,19 @@ proc gsub(g: var SrcGen, n: var Cursor, c: Context, fromStmtList = false, isTopL
17321753

17331754
of QuotedX:
17341755
inc n
1756+
1757+
let useSpace = isUseSpace(n)
17351758
put(g, tkAccent, "`")
1736-
gsub(g, n, c)
1759+
1760+
var afterFirst = false
1761+
while n.kind != ParRi:
1762+
if afterFirst:
1763+
if useSpace:
1764+
put(g, tkSpaces, Space)
1765+
else:
1766+
afterFirst = true
1767+
gsub(g, n, c)
1768+
17371769
put(g, tkAccent, "`")
17381770
skipParRi(n)
17391771

tests/nimony/plugins/trenderer.nim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,14 @@ renderTree:
1616

1717
var s = @[1, 2, 3]
1818
echo s[0]
19+
20+
type
21+
Foo = object
22+
id: int
23+
24+
proc `=destroy`(x: Foo) =
25+
discard
26+
27+
block:
28+
var m = Foo(id: 12)
29+
`=destroy`(m)

tests/nimony/plugins/trenderer.output

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ discard foo(2)
1212
var s: seq = @([1, 2, 3])
1313
write stdout, s[0]
1414
write stdout, '\n'
15+
type
16+
Foo = object
17+
id: int
18+
proc =destroy(x: Foo) =
19+
discard
20+
21+
block:
22+
var m: Foo = Foo(id: 12)
23+
=destroy(m)

0 commit comments

Comments
 (0)