From a333eb080f89a035415a17d583a651d40b309b12 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Sun, 24 May 2020 00:49:12 +0300 Subject: [PATCH] Add options to storeMacroResults that improve the results in NBC --- stew/shims/macros.nim | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/stew/shims/macros.nim b/stew/shims/macros.nim index c25f447..f6736c5 100644 --- a/stew/shims/macros.nim +++ b/stew/shims/macros.nim @@ -26,22 +26,14 @@ var macroLocations {.compileTime.} = newSeq[LineInfo]() macroOutputs {.compileTime.} = newSeq[NimNode]() -proc storeMacroResult*(callSite: LineInfo, macroResult: NimNode) = - macroLocations.add callSite - macroOutputs.add macroResult - -proc storeMacroResult*(macroResult: NimNode) = - let usageSite = callsite().lineInfoObj - storeMacroResult(usageSite, macroResult) - -macro dumpMacroResults*: untyped = +proc writeMacroResultsNow* {.compileTime.} = var files = initTable[string, NimNode]() proc addToFile(file: var NimNode, location: LineInfo, macroOutput: NimNode) = if file == nil: file = newNimNode(nnkStmtList, macroOutput) - file.add newCommentStmtNode($location) + file.add newCommentStmtNode("Generated at line " & $location.line) file.add macroOutput for i in 0..< macroLocations.len: @@ -53,6 +45,22 @@ macro dumpMacroResults*: untyped = writeFile(targetFile, repr(contents)) hint "Wrote macro output to " & targetFile, contents +proc storeMacroResult*(callSite: LineInfo, + macroResult: NimNode, + writeOutputImmediately = false) = + macroLocations.add callSite + macroOutputs.add macroResult + if writeOutputImmediately: + # echo macroResult.repr + writeMacroResultsNow() + +proc storeMacroResult*(macroResult: NimNode, writeOutputImmediately = false) = + let usageSite = callsite().lineInfoObj + storeMacroResult(usageSite, macroResult, writeOutputImmediately) + +macro dumpMacroResults*: untyped = + writeMacroResultsNow() + proc findPragma*(pragmas: NimNode, pragmaSym: NimNode): NimNode = for p in pragmas: if p.kind in {nnkSym, nnkIdent} and eqIdent(p, pragmaSym):