diff options
Diffstat (limited to 'tests/run/tstringinterp.nim')
-rw-r--r-- | tests/run/tstringinterp.nim | 72 |
1 files changed, 0 insertions, 72 deletions
diff --git a/tests/run/tstringinterp.nim b/tests/run/tstringinterp.nim deleted file mode 100644 index 423e7bb61..000000000 --- a/tests/run/tstringinterp.nim +++ /dev/null @@ -1,72 +0,0 @@ -discard """ - file: "tstringinterp.nim" - output: "Hello Alice, 64 | Hello Bob, 10$" -""" - -import macros, parseutils, strutils - -proc concat(strings: openarray[string]): string = - result = newString(0) - for s in items(strings): result.add(s) - -template ProcessInterpolations(e: expr) = - var s = e[1].strVal - for f in interpolatedFragments(s): - case f.kind - of ikStr: addString(f.value) - of ikDollar: addDollar() - of ikVar, ikExpr: addExpr(newCall("$", parseExpr(f.value))) - -macro formatStyleInterpolation(e: expr): expr = - var - formatString = "" - arrayNode = newNimNode(nnkBracket) - idx = 1 - - proc addString(s: string) = - formatString.add(s) - - proc addExpr(e: PNimrodNode) = - arrayNode.add(e) - formatString.add("$" & $(idx)) - inc idx - - proc addDollar() = - formatString.add("$$") - - ProcessInterpolations(e) - - result = parseExpr("\"x\" % [y]") - result[1].strVal = formatString - result[2] = arrayNode - -macro concatStyleInterpolation(e: expr): expr = - var args: seq[PNimrodNode] - newSeq(args, 0) - - proc addString(s: string) = args.add(newStrLitNode(s)) - proc addExpr(e: PNimrodNode) = args.add(e) - proc addDollar() = args.add(newStrLitNode"$") - - ProcessInterpolations(e) - - result = newCall("concat", args) - -### - -proc sum(a, b, c: int): int = - return (a + b + c) - -var - alice = "Alice" - bob = "Bob" - a = 10 - b = 20 - c = 34 - -var - s1 = concatStyleInterpolation"Hello ${alice}, ${sum(a, b, c)}" - s2 = formatStyleInterpolation"Hello ${bob}, ${sum(alice.len, bob.len, 2)}$$" - -write(stdout, s1 & " | " & s2) - |