This commit is contained in:
Araq 2014-04-06 22:05:42 +02:00
commit 7c065bfadf
12 changed files with 10 additions and 2 deletions

View file

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

13
tests/trmacros/tcse.nim Normal file
View file

@ -0,0 +1,13 @@
discard """
output: "4"
"""
template cse{f(a, a, x)}(a: expr{(nkDotExpr|call|nkBracketExpr)&noSideEffect},
f: expr, x: varargs[expr]): expr =
let aa = a
f(aa, aa, x)+4
var
a: array[0..10, int]
i = 3
echo a[i] + a[i]

13
tests/trmacros/thoist.nim Normal file
View file

@ -0,0 +1,13 @@
discard """
output: '''true
true'''
"""
import pegs
template optPeg{peg(pattern)}(pattern: string{lit}): TPeg =
var gl {.global, gensym.} = peg(pattern)
gl
echo match("(a b c)", peg"'(' @ ')'")
echo match("W_HI_Le", peg"\y 'while'")

View file

@ -0,0 +1,29 @@
discard """
output: "21"
"""
import macros
type
TMat = object
dummy: int
proc `*`(a, b: TMat): TMat = nil
proc `+`(a, b: TMat): TMat = nil
proc `-`(a, b: TMat): TMat = nil
proc `$`(a: TMat): string = result = $a.dummy
proc mat21(): TMat =
result.dummy = 21
macro optOps{ (`+`|`-`|`*`) ** a }(a: TMat): expr =
echo treeRepr(a)
result = newCall(bindSym"mat21")
#macro optPlus{ `+` * a }(a: varargs[TMat]): expr =
# result = newIntLitNode(21)
var x, y, z: TMat
echo x + y * z - x
#echo x + y + z

View file

@ -0,0 +1,16 @@
discard """
output: "23"
"""
template optslice{a = b + c}(a: expr{noalias}, b, c: expr): stmt =
a = b
inc a, c
var
x = 12
y = 10
z = 13
x = y+z
echo x

View file

@ -0,0 +1,10 @@
discard """
output: "4"
"""
# test that an endless recursion is avoided:
template optLen{len(x)}(x: expr): expr = len(x)
var s = "lala"
echo len(s)

28
tests/trmacros/tor.nim Normal file
View file

@ -0,0 +1,28 @@
discard """
output: '''3060
true
3'''
"""
template arithOps: expr = (`+` | `-` | `*`)
template testOr{ (arithOps{f})(a, b) }(a, b, f: expr): expr = f(a+1, b)
let xx = 10
echo 10*xx
template t{x = (~x){y} and (~x){z}}(x, y, z: bool): stmt =
x = y
if x: x = z
var
a = true
b = true
c = false
a = b and a
echo a
# bug #798
template t012{(0|1|2){x}}(x: expr): expr = x+1
let z = 1
# outputs 3 thanks to fixpoint iteration:
echo z

View file

@ -0,0 +1,11 @@
discard """
output: '''-2'''
"""
proc p(x, y: int; cond: bool): int =
result = if cond: x + y else: x - y
template optP{p(x, y, true)}(x, y: expr): expr = x - y
template optP{p(x, y, false)}(x, y: expr): expr = x + y
echo p(2, 4, true)

View file

@ -0,0 +1,17 @@
discard """
output: '''48
hel'''
"""
template optZero{x+x}(x: int): int = x*3
template andthen{`*`(x,3)}(x: int): int = x*4
template optSubstr1{x = substr(x, a, b)}(x: string, a, b: int) = setlen(x, b+1)
var y = 12
echo y+y
var s: array[0..2, string]
s[0] = "hello"
s[0] = substr(s[0], 0, 2)
echo s[0]

19
tests/trmacros/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: string): expr = &&a
let space = " "
echo "my" && (space & "awe" && "some " ) && "concat"
# check that it's been optimized properly:
doAssert calls == 1

View file

@ -0,0 +1,19 @@
discard """
output: '''0
|12|
34
'''
"""
template optWrite{
write(f, x)
((write|writeln){w})(f, y)
}(x, y: varargs[expr], f, w: expr) =
w(f, "|", x, y, "|")
if true:
echo "0"
write stdout, "1"
writeln stdout, "2"
write stdout, "3"
echo "4"