summary refs log tree commit diff stats
path: root/tests/trmacros/trmacros_various.nim
blob: 74b248739b2de0d1a5f839ef2d57b1817dc78597 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k 
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 =
    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()
s">"PreProc">break-if-&gt;=</span> <span id="L15" class="LineNr">15 </span> <span class="PreProc">var</span> x/<span class="muRegEdx">edx</span>: int <span class="Special">&lt;-</span> copy <span class="Constant">0</span> <span id="L16" class="LineNr">16 </span> <span class="Delimiter">{</span> <span id="L17" class="LineNr">17 </span> compare x, <span class="Constant">0x400</span>/screen-width=<span class="Constant">1024</span> <span id="L18" class="LineNr">18 </span> <span class="PreProc">break-if-&gt;=</span> <span id="L19" class="LineNr">19 </span> <span class="PreProc">var</span> color/<span class="muRegEcx">ecx</span>: int <span class="Special">&lt;-</span> copy x <span id="L20" class="LineNr">20 </span> color <span class="Special">&lt;-</span> and <span class="Constant">0xff</span> <span id="L21" class="LineNr">21 </span> <a href='../500fake-screen.mu.html#L483'>pixel</a> <a href='../500fake-screen.mu.html#L14'>screen</a> x, y, color <span id="L22" class="LineNr">22 </span> x <span class="Special">&lt;-</span> increment <span id="L23" class="LineNr">23 </span> <span class="PreProc">loop</span> <span id="L24" class="LineNr">24 </span> <span class="Delimiter">}</span> <span id="L25" class="LineNr">25 </span> y <span class="Special">&lt;-</span> increment <span id="L26" class="LineNr">26 </span> <span class="PreProc">loop</span> <span id="L27" class="LineNr">27 </span> <span class="Delimiter">}</span> <span id="L28" class="LineNr">28 </span><span class="Delimiter">}</span> </pre> </body> </html> <!-- vim: set foldmethod=manual : -->