From 7c065bfadf472bd569773c98a50b8986136afb43 Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 6 Apr 2014 22:05:42 +0200 Subject: fixes #798 --- compiler/hlo.nim | 3 ++- tests/patterns/targlist.nim | 9 --------- tests/patterns/tcse.nim | 13 ------------- tests/patterns/thoist.nim | 13 ------------- tests/patterns/tmatrix.nim | 29 ----------------------------- tests/patterns/tnoalias.nim | 16 ---------------- tests/patterns/tnoendlessrec.nim | 10 ---------- tests/patterns/tor.nim | 21 --------------------- tests/patterns/tpartial.nim | 11 ----------- tests/patterns/tpatterns.nim | 17 ----------------- tests/patterns/tstar.nim | 19 ------------------- tests/patterns/tstmtlist.nim | 19 ------------------- tests/trmacros/targlist.nim | 9 +++++++++ tests/trmacros/tcse.nim | 13 +++++++++++++ tests/trmacros/thoist.nim | 13 +++++++++++++ tests/trmacros/tmatrix.nim | 29 +++++++++++++++++++++++++++++ tests/trmacros/tnoalias.nim | 16 ++++++++++++++++ tests/trmacros/tnoendlessrec.nim | 10 ++++++++++ tests/trmacros/tor.nim | 28 ++++++++++++++++++++++++++++ tests/trmacros/tpartial.nim | 11 +++++++++++ tests/trmacros/tpatterns.nim | 17 +++++++++++++++++ tests/trmacros/tstar.nim | 19 +++++++++++++++++++ tests/trmacros/tstmtlist.nim | 19 +++++++++++++++++++ 23 files changed, 186 insertions(+), 178 deletions(-) delete mode 100644 tests/patterns/targlist.nim delete mode 100644 tests/patterns/tcse.nim delete mode 100644 tests/patterns/thoist.nim delete mode 100644 tests/patterns/tmatrix.nim delete mode 100644 tests/patterns/tnoalias.nim delete mode 100644 tests/patterns/tnoendlessrec.nim delete mode 100644 tests/patterns/tor.nim delete mode 100644 tests/patterns/tpartial.nim delete mode 100644 tests/patterns/tpatterns.nim delete mode 100644 tests/patterns/tstar.nim delete mode 100644 tests/patterns/tstmtlist.nim create mode 100644 tests/trmacros/targlist.nim create mode 100644 tests/trmacros/tcse.nim create mode 100644 tests/trmacros/thoist.nim create mode 100644 tests/trmacros/tmatrix.nim create mode 100644 tests/trmacros/tnoalias.nim create mode 100644 tests/trmacros/tnoendlessrec.nim create mode 100644 tests/trmacros/tor.nim create mode 100644 tests/trmacros/tpartial.nim create mode 100644 tests/trmacros/tpatterns.nim create mode 100644 tests/trmacros/tstar.nim create mode 100644 tests/trmacros/tstmtlist.nim diff --git a/compiler/hlo.nim b/compiler/hlo.nim index 7982d4aa1..c75d6519f 100644 --- a/compiler/hlo.nim +++ b/compiler/hlo.nim @@ -68,7 +68,8 @@ proc hlo(c: PContext, n: PNode): PNode = result = n else: if n.kind in {nkFastAsgn, nkAsgn, nkIdentDefs, nkVarTuple} and - n.sons[0].kind == nkSym and sfGlobal in n.sons[0].sym.flags: + n.sons[0].kind == nkSym and + {sfGlobal, sfPure} * n.sons[0].sym.flags == {sfGlobal, sfPure}: # do not optimize 'var g {.global} = re(...)' again! return n result = applyPatterns(c, n) diff --git a/tests/patterns/targlist.nim b/tests/patterns/targlist.nim deleted file mode 100644 index e416edf0a..000000000 --- a/tests/patterns/targlist.nim +++ /dev/null @@ -1,9 +0,0 @@ -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/patterns/tcse.nim b/tests/patterns/tcse.nim deleted file mode 100644 index ff04f7d83..000000000 --- a/tests/patterns/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/patterns/thoist.nim b/tests/patterns/thoist.nim deleted file mode 100644 index 7d14c0abf..000000000 --- a/tests/patterns/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/patterns/tmatrix.nim b/tests/patterns/tmatrix.nim deleted file mode 100644 index c825a7792..000000000 --- a/tests/patterns/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/patterns/tnoalias.nim b/tests/patterns/tnoalias.nim deleted file mode 100644 index 1d5671362..000000000 --- a/tests/patterns/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/patterns/tnoendlessrec.nim b/tests/patterns/tnoendlessrec.nim deleted file mode 100644 index 53891bcc0..000000000 --- a/tests/patterns/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/patterns/tor.nim b/tests/patterns/tor.nim deleted file mode 100644 index 833418919..000000000 --- a/tests/patterns/tor.nim +++ /dev/null @@ -1,21 +0,0 @@ -discard """ - output: '''3060 -true''' -""" - -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 diff --git a/tests/patterns/tpartial.nim b/tests/patterns/tpartial.nim deleted file mode 100644 index fdaa3414a..000000000 --- a/tests/patterns/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/patterns/tpatterns.nim b/tests/patterns/tpatterns.nim deleted file mode 100644 index 6bc8772e3..000000000 --- a/tests/patterns/tpatterns.nim +++ /dev/null @@ -1,17 +0,0 @@ -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/patterns/tstar.nim b/tests/patterns/tstar.nim deleted file mode 100644 index 8443268f4..000000000 --- a/tests/patterns/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/patterns/tstmtlist.nim b/tests/patterns/tstmtlist.nim deleted file mode 100644 index 20cb5d688..000000000 --- a/tests/patterns/tstmtlist.nim +++ /dev/null @@ -1,19 +0,0 @@ -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" 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" -- cgit 1.4.1-2-gfad0