diff options
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/tdisallowif.nim | 5 | ||||
-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/tnoalias2.nim | 19 | ||||
-rw-r--r-- | tests/trmacros/tnoendlessrec.nim | 10 | ||||
-rw-r--r-- | tests/trmacros/tnorewrite.nim | 20 | ||||
-rw-r--r-- | tests/trmacros/tor.nim | 28 | ||||
-rw-r--r-- | tests/trmacros/tpartial.nim | 11 | ||||
-rw-r--r-- | tests/trmacros/tpatterns.nim | 23 | ||||
-rw-r--r-- | tests/trmacros/trmacros_various.nim | 111 | ||||
-rw-r--r-- | tests/trmacros/trmacros_various2.nim | 91 | ||||
-rw-r--r-- | tests/trmacros/tstar.nim | 19 | ||||
-rw-r--r-- | tests/trmacros/tstatic_t_bug.nim | 24 | ||||
-rw-r--r-- | tests/trmacros/tstmtlist.nim | 17 |
17 files changed, 257 insertions, 201 deletions
diff --git a/tests/trmacros/targlist.nim b/tests/trmacros/targlist.nim deleted file mode 100644 index f9d2cb6c6..000000000 --- a/tests/trmacros/targlist.nim +++ /dev/null @@ -1,9 +0,0 @@ -discard """ - output: "12false3ha" -""" - -proc f(x: varargs[string, `$`]) = discard -template optF{f(x)}(x: varargs[expr]) = - writeLine(stdout, x) - -f 1, 2, false, 3, "ha" diff --git a/tests/trmacros/tcse.nim b/tests/trmacros/tcse.nim deleted file mode 100644 index 023a8f298..000000000 --- a/tests/trmacros/tcse.nim +++ /dev/null @@ -1,13 +0,0 @@ -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/tdisallowif.nim b/tests/trmacros/tdisallowif.nim index 18dfd1c82..8d83f6ddf 100644 --- a/tests/trmacros/tdisallowif.nim +++ b/tests/trmacros/tdisallowif.nim @@ -1,7 +1,6 @@ discard """ + errormsg: "usage of 'disallowIf' is an {.error.} defined at tdisallowif.nim(10, 1)" line: 24 - errormsg: "usage of 'disallowIf' is a user-defined error" - disabled: true """ template optZero{x+x}(x: int): int = x*3 @@ -11,7 +10,7 @@ template optSubstr1{x = substr(x, 0, b)}(x: string, b: int) = setlen(x, b+1) template disallowIf{ if cond: action else: action2 -}(cond: bool, action, action2: stmt) {.error.} = action +}(cond: bool, action, action2: typed) {.error.} = action var y = 12 echo y+y diff --git a/tests/trmacros/thoist.nim b/tests/trmacros/thoist.nim deleted file mode 100644 index 7d14c0abf..000000000 --- a/tests/trmacros/thoist.nim +++ /dev/null @@ -1,13 +0,0 @@ -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 deleted file mode 100644 index f409434c5..000000000 --- a/tests/trmacros/tmatrix.nim +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index 1d5671362..000000000 --- a/tests/trmacros/tnoalias.nim +++ /dev/null @@ -1,16 +0,0 @@ -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/tnoalias2.nim b/tests/trmacros/tnoalias2.nim deleted file mode 100644 index 5a816acb9..000000000 --- a/tests/trmacros/tnoalias2.nim +++ /dev/null @@ -1,19 +0,0 @@ -discard """ - output: '''0''' -""" - -# bug #206 -template optimizeOut{testFunc(a, b)}(a: int, b: int{alias}) : expr = 0 - -proc testFunc(a, b: int): int = result = a + b -var testVar = 1 -echo testFunc(testVar, testVar) - - -template ex{a = b + c}(a : int{noalias}, b, c : int) = - a = b - inc a, b - echo "came here" - -var x = 5 -x = x + x diff --git a/tests/trmacros/tnoendlessrec.nim b/tests/trmacros/tnoendlessrec.nim deleted file mode 100644 index 53891bcc0..000000000 --- a/tests/trmacros/tnoendlessrec.nim +++ /dev/null @@ -1,10 +0,0 @@ -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/tnorewrite.nim b/tests/trmacros/tnorewrite.nim new file mode 100644 index 000000000..e6769246f --- /dev/null +++ b/tests/trmacros/tnorewrite.nim @@ -0,0 +1,20 @@ +block: + proc get(x: int): int = x + + template t{get(a)}(a: int): int = + {.noRewrite.}: + get(a) + 1 + + doAssert get(0) == 1 + +block: + var x: int + + template asgn{a = b}(a: int{lvalue}, b: int) = + let newVal = b + 1 + # ^ this is needed but should it be? + {.noRewrite.}: + a = newVal + + x = 10 + doAssert x == 11, $x diff --git a/tests/trmacros/tor.nim b/tests/trmacros/tor.nim index 500851582..9defc4d1b 100644 --- a/tests/trmacros/tor.nim +++ b/tests/trmacros/tor.nim @@ -1,16 +1,28 @@ discard """ - output: '''3030 + output: ''' +3 +30 true -3''' +''' """ -template arithOps: expr = (`+` | `-` | `*`) -template testOr{ (arithOps{f})(a, b) }(a, b, f: expr): expr = f(a+1, b) + +# bug #798 +template t012{(0|1|2){x}}(x: untyped): untyped = x+1 +let z = 1 +# outputs 3 thanks to fixpoint iteration: +echo z + + +template arithOps: untyped = (`+` | `-` | `*`) +template testOr{ (arithOps{f})(a, b) }(a, b, f: untyped): untyped = + {.noRewrite.}: + f(a mod 10, b) let xx = 10 echo 10*xx -template t{x = (~x){y} and (~x){z}}(x, y, z: bool): stmt = +template t{x = (~x){y} and (~x){z}}(x, y, z: bool): typed = x = y if x: x = z @@ -20,9 +32,3 @@ var 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 deleted file mode 100644 index fdaa3414a..000000000 --- a/tests/trmacros/tpartial.nim +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index 907973637..000000000 --- a/tests/trmacros/tpatterns.nim +++ /dev/null @@ -1,23 +0,0 @@ -discard """ - output: '''48 -hel -lo''' -""" - -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] - -# Test varargs matching -proc someVarargProc(k: varargs[string]) = doAssert(false) # this should not get called -template someVarargProcSingleArg{someVarargProc([a])}(a: string) = echo a -someVarargProc("lo") diff --git a/tests/trmacros/trmacros_various.nim b/tests/trmacros/trmacros_various.nim new file mode 100644 index 000000000..8fe51e548 --- /dev/null +++ b/tests/trmacros/trmacros_various.nim @@ -0,0 +1,111 @@ +discard """ +output: ''' +12false3ha +21 +optimized +''' +""" + +import macros, pegs + + +block arglist: + proc f(x: varargs[string, `$`]) = discard + template optF{f(x)}(x: varargs[untyped]) = + writeLine(stdout, x) + + f 1, 2, false, 3, "ha" + + + +block tcse: + template cse{f(a, a, x)}(a: typed{(nkDotExpr|call|nkBracketExpr)&noSideEffect}, + f: typed, x: varargs[typed]): untyped = + let aa = a + f(aa, aa, x)+4 + + var + a: array[0..10, int] + i = 3 + doAssert a[i] + a[i] == 4 + + + +block hoist: + template optPeg{peg(pattern)}(pattern: string{lit}): Peg = + {.noRewrite.}: + var gl {.global, gensym.} = peg(pattern) + gl + doAssert match("(a b c)", peg"'(' @ ')'") + doAssert match("W_HI_Le", peg"\y 'while'") + + + +block tmatrix: + 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): untyped = + result = newCall(bindSym"mat21") + + #macro optPlus{ `+` * a }(a: varargs[TMat]): expr = + # result = newIntLitNode(21) + + var x, y, z: TMat + echo x + y * z - x + + + +block tnoalias: + template optslice{a = b + c}(a: untyped{noalias}, b, c: untyped): typed = + a = b + inc a, c + var + x = 12 + y = 10 + z = 13 + x = y+z + doAssert x == 23 + + + +block tnoendlessrec: + # test that an endless recursion is avoided: + template optLen{len(x)}(x: typed): int = len(x) + + var s = "lala" + doAssert len(s) == 4 + + + +block tstatic_t_bug: + # bug #4227 + type Vector64[N: static[int]] = array[N, int] + + proc `*`[N: static[int]](a: Vector64[N]; b: float64): Vector64[N] = + result = a + + proc `+=`[N: static[int]](a: var Vector64[N]; b: Vector64[N]) = + echo "regular" + + proc linearCombinationMut[N: static[int]](a: float64, v: var Vector64[N], w: Vector64[N]) {. inline .} = + echo "optimized" + + template rewriteLinearCombinationMut{v += `*`(w, a)}(a: float64, v: var Vector64, w: Vector64): auto = + linearCombinationMut(a, v, w) + + proc main() = + const scaleVal = 9.0 + var a, b: Vector64[7] + a += b * scaleval + + main() + diff --git a/tests/trmacros/trmacros_various2.nim b/tests/trmacros/trmacros_various2.nim new file mode 100644 index 000000000..981df4ca6 --- /dev/null +++ b/tests/trmacros/trmacros_various2.nim @@ -0,0 +1,91 @@ +discard """ +output: ''' +0 +-2 +48 +hel +lo +my awesome concat +''' +""" + + +block tnoalias2: + # bug #206 + template optimizeOut{testFunc(a, b)}(a: int, b: int{alias}): untyped = 0 + + proc testFunc(a, b: int): int = result = a + b + var testVar = 1 + echo testFunc(testVar, testVar) + + + template ex{a = b + c}(a : int{noalias}, b, c : int) = + a = b + inc a, b + echo "came here" + + var x = 5 + x = x + x + + + +block tpartial: + proc p(x, y: int; cond: bool): int = + result = if cond: x + y else: x - y + + template optPTrue{p(x, y, true)}(x, y): untyped = x - y + template optPFalse{p(x, y, false)}(x, y): untyped = x + y + + echo p(2, 4, true) + + + +block tpatterns: + 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] + + # Test varargs matching + proc someVarargProc(k: varargs[string]) = doAssert(false) # this should not get called + template someVarargProcSingleArg{someVarargProc([a])}(a: string) = echo a + someVarargProc("lo") + + + +block tstar: + 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): string = + {.noRewrite.}: + &&a + + let space = " " + echo "my" && (space & "awe" && "some " ) && "concat" + + # check that it's been optimized properly: + doAssert calls == 1 + +# bug #7524 +template in_to_out(typIn, typOut: typedesc) = + proc to_out(x: typIn{lit}): typOut = result = ord(x) + +# Generating the proc via template doesn't work +in_to_out(char, int) + +# This works +proc to_out2(x: char{lit}): int = result = ord(x) diff --git a/tests/trmacros/tstar.nim b/tests/trmacros/tstar.nim deleted file mode 100644 index 536289ff0..000000000 --- a/tests/trmacros/tstar.nim +++ /dev/null @@ -1,19 +0,0 @@ -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/tstatic_t_bug.nim b/tests/trmacros/tstatic_t_bug.nim deleted file mode 100644 index cdfa53514..000000000 --- a/tests/trmacros/tstatic_t_bug.nim +++ /dev/null @@ -1,24 +0,0 @@ -discard """ - output: "optimized" -""" -# bug #4227 -type Vector64[N: static[int]] = array[N, int] - -proc `*`*[N: static[int]](a: Vector64[N]; b: float64): Vector64[N] = - result = a - -proc `+=`*[N: static[int]](a: var Vector64[N]; b: Vector64[N]) = - echo "regular" - -proc linearCombinationMut[N: static[int]](a: float64, v: var Vector64[N], w: Vector64[N]) {. inline .} = - echo "optimized" - -template rewriteLinearCombinationMut*{v += `*`(w, a)}(a: float64, v: var Vector64, w: Vector64): auto = - linearCombinationMut(a, v, w) - -proc main() = - const scaleVal = 9.0 - var a, b: Vector64[7] - a += b * scaleval - -main() diff --git a/tests/trmacros/tstmtlist.nim b/tests/trmacros/tstmtlist.nim index 5202f778b..8261e7c45 100644 --- a/tests/trmacros/tstmtlist.nim +++ b/tests/trmacros/tstmtlist.nim @@ -8,7 +8,7 @@ discard """ template optWrite{ write(f, x) ((write|writeLine){w})(f, y) -}(x, y: varargs[expr], f, w: expr) = +}(x, y: varargs[untyped], f, w: untyped) = w(f, "|", x, y, "|") if true: @@ -17,3 +17,18 @@ if true: writeLine stdout, "2" write stdout, "3" echo "4" + +# bug #7972 + +template optimizeLogWrites*{ + write(f, x) + write(f, y) +}(x, y: string{lit}, f: File) = + write(f, x & y) + +proc foo() = + const N = 1 + stdout.write("") + stdout.write("") + +foo() |