mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-02 03:02:31 +00:00
fixes a bug for 'dup' and 'with'; they can now handle nested statement lists that can result from macros
This commit is contained in:
@@ -216,14 +216,7 @@ when (NimMajor, NimMinor) >= (1, 1):
|
||||
result = newNimNode(nnkStmtListExpr, arg)
|
||||
let tmp = genSym(nskVar, "dupResult")
|
||||
result.add newVarStmt(tmp, arg)
|
||||
expectKind calls, nnkArgList
|
||||
let body =
|
||||
if calls.len == 1 and calls[0].kind in {nnkStmtList, nnkStmtListExpr}:
|
||||
calls[0]
|
||||
else:
|
||||
calls
|
||||
for call in body:
|
||||
result.add underscoredCall(call, tmp)
|
||||
underscoredCalls(result, calls, tmp)
|
||||
result.add tmp
|
||||
|
||||
|
||||
|
||||
@@ -12,27 +12,38 @@
|
||||
|
||||
import macros
|
||||
|
||||
proc underscoredCall*(n, arg0: NimNode): NimNode =
|
||||
proc underscoredCall(n, arg0: NimNode): NimNode =
|
||||
proc underscorePos(n: NimNode): int =
|
||||
for i in 1 ..< n.len:
|
||||
if n[i].eqIdent("_"): return i
|
||||
return -1
|
||||
return 0
|
||||
|
||||
if n.kind in nnkCallKinds:
|
||||
result = copyNimNode(n)
|
||||
result.add n[0]
|
||||
|
||||
let u = underscorePos(n)
|
||||
if u < 0:
|
||||
result.add arg0
|
||||
for i in 1..n.len-1: result.add n[i]
|
||||
else:
|
||||
for i in 1..u-1: result.add n[i]
|
||||
result.add arg0
|
||||
for i in u+1..n.len-1: result.add n[i]
|
||||
for i in 1..u-1: result.add n[i]
|
||||
result.add arg0
|
||||
for i in u+1..n.len-1: result.add n[i]
|
||||
else:
|
||||
# handle e.g. 'x.dup(sort)'
|
||||
result = newNimNode(nnkCall, n)
|
||||
result.add n
|
||||
result.add arg0
|
||||
|
||||
proc underscoredCalls*(result, calls, arg0: NimNode) =
|
||||
proc handleStmtList(result, n, arg0: NimNode) =
|
||||
for a in n:
|
||||
if a.kind in {nnkStmtList, nnkStmtListExpr}:
|
||||
handleStmtList(result, a, arg0)
|
||||
else:
|
||||
result.add underscoredCall(a, arg0)
|
||||
|
||||
expectKind calls, nnkArgList
|
||||
if calls.len == 1 and calls[0].kind in {nnkStmtList, nnkStmtListExpr}:
|
||||
# the 'macro: body' syntax is used:
|
||||
handleStmtList(result, calls[0], arg0)
|
||||
else:
|
||||
for call in calls:
|
||||
result.add underscoredCall(call, arg0)
|
||||
|
||||
@@ -35,14 +35,7 @@ macro with*(arg: typed; calls: varargs[untyped]): untyped =
|
||||
doAssert a == 43
|
||||
|
||||
result = newNimNode(nnkStmtList, arg)
|
||||
expectKind calls, nnkArgList
|
||||
let body =
|
||||
if calls.len == 1 and calls[0].kind in {nnkStmtList, nnkStmtListExpr}:
|
||||
calls[0]
|
||||
else:
|
||||
calls
|
||||
for call in body:
|
||||
result.add underscoredCall(call, arg)
|
||||
underscoredCalls(result, calls, arg)
|
||||
|
||||
when isMainModule:
|
||||
type
|
||||
|
||||
Reference in New Issue
Block a user