got rid of 'accept' dir in the tests
This commit is contained in:
parent
d0772feb08
commit
a274f3bf5b
264 changed files with 5 additions and 7 deletions
|
|
@ -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: expr) =
|
||||
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: expr) = 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)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue