diff options
author | Araq <rumpf_a@web.de> | 2014-04-06 22:05:42 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2014-04-06 22:05:42 +0200 |
commit | 7c065bfadf472bd569773c98a50b8986136afb43 (patch) | |
tree | 6054ad79c3e442931d9280e011bad91a64735ced /tests/trmacros | |
parent | 5d6053173a0db2b4d0dd1c334c41075fe8d9850e (diff) | |
download | Nim-7c065bfadf472bd569773c98a50b8986136afb43.tar.gz |
fixes #798
Diffstat (limited to 'tests/trmacros')
-rw-r--r-- | tests/trmacros/targlist.nim | 9 | ||||
-rw-r--r-- | tests/trmacros/tcse.nim | 13 | ||||
-rw-r--r-- | tests/trmacros/thoist.nim | 13 | ||||
-rw-r--r-- | tests/trmacros/tmatrix.nim | 29 | ||||
-rw-r--r-- | tests/trmacros/tnoalias.nim | 16 | ||||
-rw-r--r-- | tests/trmacros/tnoendlessrec.nim | 10 | ||||
-rw-r--r-- | tests/trmacros/tor.nim | 28 | ||||
-rw-r--r-- | tests/trmacros/tpartial.nim | 11 | ||||
-rw-r--r-- | tests/trmacros/tpatterns.nim | 17 | ||||
-rw-r--r-- | tests/trmacros/tstar.nim | 19 | ||||
-rw-r--r-- | tests/trmacros/tstmtlist.nim | 19 |
11 files changed, 184 insertions, 0 deletions
diff --git a/tests/trmacros/targlist.nim b/tests/trmacros/targlist.nim new file mode 100644 index 000000000..e416edf0a --- /dev/null +++ b/tests/trmacros/targlist.nim @@ -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" diff --git a/tests/trmacros/tcse.nim b/tests/trmacros/tcse.nim new file mode 100644 index 000000000..ff04f7d83 --- /dev/null +++ b/tests/trmacros/tcse.nim @@ -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] diff --git a/tests/trmacros/thoist.nim b/tests/trmacros/thoist.nim new file mode 100644 index 000000000..7d14c0abf --- /dev/null +++ b/tests/trmacros/thoist.nim @@ -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'") diff --git a/tests/trmacros/tmatrix.nim b/tests/trmacros/tmatrix.nim new file mode 100644 index 000000000..c825a7792 --- /dev/null +++ b/tests/trmacros/tmatrix.nim @@ -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 diff --git a/tests/trmacros/tnoalias.nim b/tests/trmacros/tnoalias.nim new file mode 100644 index 000000000..1d5671362 --- /dev/null +++ b/tests/trmacros/tnoalias.nim @@ -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 diff --git a/tests/trmacros/tnoendlessrec.nim b/tests/trmacros/tnoendlessrec.nim new file mode 100644 index 000000000..53891bcc0 --- /dev/null +++ b/tests/trmacros/tnoendlessrec.nim @@ -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) diff --git a/tests/trmacros/tor.nim b/tests/trmacros/tor.nim new file mode 100644 index 000000000..dc72a96cd --- /dev/null +++ b/tests/trmacros/tor.nim @@ -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 diff --git a/tests/trmacros/tpartial.nim b/tests/trmacros/tpartial.nim new file mode 100644 index 000000000..fdaa3414a --- /dev/null +++ b/tests/trmacros/tpartial.nim @@ -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) diff --git a/tests/trmacros/tpatterns.nim b/tests/trmacros/tpatterns.nim new file mode 100644 index 000000000..6bc8772e3 --- /dev/null +++ b/tests/trmacros/tpatterns.nim @@ -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] diff --git a/tests/trmacros/tstar.nim b/tests/trmacros/tstar.nim new file mode 100644 index 000000000..8443268f4 --- /dev/null +++ b/tests/trmacros/tstar.nim @@ -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 diff --git a/tests/trmacros/tstmtlist.nim b/tests/trmacros/tstmtlist.nim new file mode 100644 index 000000000..20cb5d688 --- /dev/null +++ b/tests/trmacros/tstmtlist.nim @@ -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" |