further improvements for term rewriting macros

This commit is contained in:
Araq 2012-09-04 00:55:13 +02:00
commit 6753d36854
8 changed files with 74 additions and 28 deletions

View file

@ -0,0 +1,9 @@
discard """
output: "12false3ha"
"""
proc f(x: varargs[string, `$`]) = nil
template optF{f(X)}(x: varargs[expr]) =
writeln(stdout, x)
f 1, 2, false, 3, "ha"

9
tests/patterns/tor.nim Normal file
View file

@ -0,0 +1,9 @@
discard """
output: "110"
"""
template arithOps: expr = (`+` | `-` | `*`)
template testOr{ (arithOps{f})(a, b) }(a, b, f: expr): expr = f(a+1, b)
let xx = 10
echo 10*xx

19
tests/patterns/tstar.nim Normal file
View file

@ -0,0 +1,19 @@
discard """
output: "my awesome concat"
"""
var
calls = 0
proc `&&`(s: varargs[string]): string =
result = s[0]
for i in 1..len(s)-1: result.add s[i]
inc calls
template optConc{ `&&` * a }(a: expr): expr = &&a
let space = " "
echo "my" && (space & "awe" && "some " ) && "concat"
# check that it's been properly optimized:
doAssert calls == 1

View file

@ -0,0 +1,18 @@
discard """
output: '''0
|12|34
'''
"""
template optWrite{
write(stdout, x)
write(stdout, y)
}(x, y: string) =
write(stdout, "|", x, y, "|")
if true:
echo "0"
write stdout, "1"
write stdout, "2"
write stdout, "3"
echo "4"

View file

@ -5,7 +5,7 @@ discard """
import strutils
proc foo(s: expr{string}): string =
proc foo(s: expr[string]): string =
static: echo s
const R = s.toUpper

View file

@ -5,18 +5,18 @@ type
x: T
y: U
proc foo(T: typedesc{float}, a: expr): string =
proc foo(T: typedesc[float], a: expr): string =
result = "float " & $(a.len > 5)
proc foo(T: typedesc{TFoo}, a: int): string =
proc foo(T: typedesc[TFoo], a: int): string =
result = "TFoo " & $(a)
proc foo(T: typedesc{int or bool}): string =
proc foo(T: typedesc[int or bool]): string =
var a: T
a = 10
result = "int or bool " & ($a)
template foo(T: typedesc{seq}): expr = "seq"
template foo(T: typedesc[seq]): expr = "seq"
test "types can be used as proc params":
check foo(TFoo[int, float], 1000) == "TFoo 1000"
@ -31,5 +31,5 @@ test "types can be used as proc params":
check foo(seq[TFoo[bool, string]]) == "seq"
when false:
proc foo(T: typedesc{seq}, s: T) = nil
proc foo(T: typedesc[seq], s: T) = nil