underscored_calls.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2020 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This is an internal helper module. Do not use.
  10. import std/macros
  11. proc underscoredCalls*(result, calls, arg0: NimNode)
  12. proc underscoredCall(n, arg0: NimNode): NimNode =
  13. proc underscorePos(n: NimNode): int =
  14. for i in 1 ..< n.len:
  15. if n[i].eqIdent("_"): return i
  16. return 0
  17. if n.kind in nnkCallKinds:
  18. if n[0].kind in {nnkIdent, nnkSym} and n[0].eqIdent("with"):
  19. expectKind n[1], {nnkIdent, nnkSym}
  20. result = newStmtList()
  21. underscoredCalls(result, n[2 .. ^1].newStmtList, newDotExpr(arg0, n[1]))
  22. else:
  23. result = copyNimNode(n)
  24. result.add n[0]
  25. let u = underscorePos(n)
  26. for i in 1..u-1: result.add n[i]
  27. result.add arg0
  28. for i in u+1..n.len-1: result.add n[i]
  29. elif n.kind in {nnkAsgn, nnkExprEqExpr}:
  30. var field = n[0]
  31. if n[0].kind == nnkDotExpr and n[0][0].eqIdent("_"):
  32. # handle _.field = ...
  33. field = n[0][1]
  34. result = newDotExpr(arg0, field).newAssignment n[1]
  35. else:
  36. # handle e.g. 'x.dup(sort)'
  37. result = newNimNode(nnkCall, n)
  38. result.add n
  39. result.add arg0
  40. proc underscoredCalls*(result, calls, arg0: NimNode) =
  41. expectKind calls, {nnkArgList, nnkStmtList, nnkStmtListExpr}
  42. for call in calls:
  43. if call.kind in {nnkStmtList, nnkStmtListExpr}:
  44. underscoredCalls(result, call, arg0)
  45. else:
  46. result.add underscoredCall(call, arg0)