summary refs log tree commit diff stats
path: root/tests/modules/m9627
Commit message (Expand)AuthorAgeFilesLines
* Fix compilation of test on linux and macNeelesh Chandola2018-12-092-0/+2
ef='#n19'>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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
discard """
action: compile
"""

# this test should ensure that the AST doesn't change slightly without it getting noticed.

import ../ast_pattern_matching

template expectNimNode(arg: untyped): NimNode = arg
  ## This template here is just to be injected by `myquote`, so that
  ## a nice error message appears when the captured symbols are not of
  ## type `NimNode`.

proc substitudeComments(symbols, values, n: NimNode): NimNode =
  ## substitutes all nodes of kind nnkCommentStmt to parameter
  ## symbols. Consumes the argument `n`.
  if n.kind == nnkCommentStmt:
    values.add newCall(bindSym"newCommentStmtNode", newLit(n.strVal))
    # Gensym doesn't work for parameters. These identifiers won't
    # clash unless an argument is constructed to clash here.
    symbols.add ident("comment" & $values.len & "_XObBdOnh6meCuJK2smZV")
    return symbols[^1]
  for i in 0 ..< n.len:
    n[i] = substitudeComments(symbols, values, n[i])
  return n

macro myquote*(args: varargs[untyped]): untyped =
  expectMinLen(args, 1)

  # This is a workaround for #10430 where comments are removed in
  # template expansions. This workaround lifts all comments
  # statements to be arguments of the temporary template.

  let extraCommentSymbols = newNimNode(nnkBracket)
  let extraCommentGenExpr = newNimNode(nnkBracket)
  let body = substitudeComments(
    extraCommentSymbols, extraCommentGenExpr, args[^1]
  )

  let formalParams = nnkFormalParams.newTree(ident"untyped")
  for i in 0 ..< args.len-1:
    formalParams.add nnkIdentDefs.newTree(
      args[i], ident"untyped", newEmptyNode()
    )
  for sym in extraCommentSymbols:
    formalParams.add nnkIdentDefs.newTree(
      sym, ident"untyped", newEmptyNode()
    )

  let templateSym = genSym(nskTemplate)
  let templateDef = nnkTemplateDef.newTree(
    templateSym,
    newEmptyNode(),
    newEmptyNode(),
    formalParams,
    nnkPragma.newTree(ident"dirty"),
    newEmptyNode(),
    args[^1]
  )

  let templateCall = newCall(templateSym)
  for i in 0 ..< args.len-1:
    let symName = args[i]
    # identifiers and quoted identifiers are allowed.
    if symName.kind == nnkAccQuoted:
      symName.expectLen 1
      symName[0].expectKind nnkIdent
    else:
      symName.expectKind nnkIdent
    templateCall.add newCall(bindSym"expectNimNode", symName)
  for expr in extraCommentGenExpr:
    templateCall.add expr
  let getAstCall = newCall(bindSym"getAst", templateCall)
  result = newStmtList(templateDef, getAstCall)


macro testAddrAst(arg: typed): bool =
  arg.expectKind nnkStmtListExpr
  arg[0].expectKind(nnkVarSection)
  arg[1].expectKind({nnkAddr, nnkCall})
  result = newLit(arg[1].kind == nnkCall)

const newAddrAst: bool = testAddrAst((var x: int; addr(x)))

static:
  echo "new addr ast: ", newAddrAst

# TODO test on matching failures

proc peelOff*(arg: NimNode, kinds: set[NimNodeKind]): NimNode {.compileTime.} =
  ## Peel off  nodes of a specific kinds.
  if arg.len == 1 and arg.kind in kinds:
    arg[0].peelOff(kinds)
  else:
    arg

proc peelOff*(arg: NimNode, kind: NimNodeKind): NimNode {.compileTime.} =
  ## Peel off nodes of a specific kind.
  if arg.len == 1 and arg.kind == kind:
    arg[0].peelOff(kind)
  else:
    arg

static:
  template testPattern(pattern, astArg: untyped): untyped =
    let ast = quote do: `astArg`
    ast.matchAst:
    of `pattern`:
      echo "ok"

  template testPatternFail(pattern, astArg: untyped): untyped =
    let ast = quote do: `astArg`
    ast.matchAst:
    of `pattern`:
      error("this should not match", ast)
    else:
      echo "OK"


  testPattern nnkIntLit(intVal = 42), 42
  testPattern nnkInt8Lit(intVal = 42), 42'i8
  testPattern nnkInt16Lit(intVal = 42), 42'i16
  testPattern nnkInt32Lit(intVal = 42), 42'i32
  testPattern nnkInt64Lit(intVal = 42), 42'i64
  testPattern nnkUInt8Lit(intVal = 42), 42'u8
  testPattern nnkUInt16Lit(intVal = 42), 42'u16
  testPattern nnkUInt32Lit(intVal = 42), 42'u32
  testPattern nnkUInt64Lit(intVal = 42), 42'u64
  #testPattern nnkFloat64Lit(floatVal = 42.0), 42.0
  testPattern nnkFloat32Lit(floatVal = 42.0), 42.0'f32
  #testPattern nnkFloat64Lit(floatVal = 42.0), 42.0'f64
  testPattern nnkStrLit(strVal = "abc"), "abc"
  testPattern nnkRStrLit(strVal = "abc"), r"abc"
  testPattern nnkTripleStrLit(strVal = "abc"), """abc"""
  testPattern nnkCharLit(intVal = 32), ' '
  testPattern nnkNilLit(), nil
  testPattern nnkIdent(strVal = "myIdentifier"), myIdentifier

  testPatternFail nnkInt8Lit(intVal = 42), 42'i16
  testPatternFail nnkInt16Lit(intVal = 42), 42'i8



# this should be just `block` but it doesn't work that way anymore because of VM.
macro scope(arg: untyped): untyped =
  let procSym = genSym(nskProc)
  result = quote do:
    proc `procSym`() {.compileTime.} =
      `arg`

    `procSym`()

static:
  ## Command call
  scope:

    let ast = myquote:
      echo "abc", "xyz"

    ast.matchAst:
    of nnkCommand(ident"echo", "abc", "xyz"):
      echo "ok"

  ## Call with ``()``

  scope:
    let ast = myquote:
      echo("abc", "xyz")

    ast.matchAst:
    of nnkCall(ident"echo", "abc", "xyz"):
      echo "ok"

  ## Infix operator call

  macro testInfixOperatorCall(ast: untyped): untyped =
    ast.matchAst(errorSym):
    of nnkInfix(
      ident"&",
      nnkStrLit(strVal = "abc"),
      nnkStrLit(strVal = "xyz")
    ):
      echo "ok1"
    of nnkInfix(
      ident"+",
      nnkIntLit(intVal = 5),
      nnkInfix(
        ident"*",
        nnkIntLit(intVal = 3),
        nnkIntLit(intVal = 4)
      )
    ):
      echo "ok2"
    of nnkCall(
      nnkAccQuoted(
        ident"+"
      ),
      nnkIntLit(intVal = 3),
      nnkIntLit(intVal = 4)
    ):
      echo "ok3"

  testInfixOperatorCall("abc" & "xyz")
  testInfixOperatorCall(5 + 3 * 4)
  testInfixOperatorCall(`+`(3, 4))


  ## Prefix operator call

  scope:

    let ast = myquote:
      ? "xyz"

    ast.matchAst(err):
    of nnkPrefix(
      ident"?",
      nnkStrLit(strVal = "xyz")
    ):
      echo "ok"


  ## Postfix operator call

  scope:

    let ast = myquote:
      proc identifier*

    ast[0].matchAst(err):
    of nnkPostfix(
      ident"*",
      ident"identifier"
    ):
      echo "ok"


  ## Call with named arguments

  macro testCallWithNamedArguments(ast: untyped): untyped =
    ast.peelOff(nnkStmtList).matchAst:
    of nnkCall(
      ident"writeLine",
      nnkExprEqExpr(
        ident"file",
        ident"stdout"
      ),
      nnkStrLit(strVal = "hallo")
    ):
      echo "ok"

  testCallWithNamedArguments:
    writeLine(file=stdout, "hallo")

  ## Call with raw string literal
  scope:
    let ast = myquote:
      echo"abc"


    ast.matchAst(err):
    of nnkCallStrLit(
      ident"echo",
      nnkRStrLit(strVal = "abc")
    ):
      echo "ok"

  ## Dereference operator ``[]``

  scope:
    # The dereferece operator exists only on a typed ast.
    macro testDereferenceOperator(ast: typed): untyped =
      ast.matchAst(err):
      of nnkDerefExpr(_):
        echo "ok"

    var x: ptr int
    testDereferenceOperator(x[])



  ## Addr operator

  scope:
    # The addr operator exists only on a typed ast.
    macro testAddrOperator(ast: untyped): untyped =
      echo ast.treeRepr
      ast.matchAst(err):
      of nnkAddr(ident"x"):
        echo "old nim"
      of nnkCall(ident"addr", ident"x"):
        echo "ok"

    var x: int
    testAddrOperator(addr(x))


  ## Cast operator

  scope:

    let ast = myquote:
      cast[T](x)

    ast.matchAst:
    of nnkCast(ident"T", ident"x"):
      echo "ok"


  ## Object access operator ``.``

  scope:

    let ast = myquote:
      x.y

    ast.matchAst:
    of nnkDotExpr(ident"x", ident"y"):
      echo "ok"

  ## Array access operator ``[]``

  macro testArrayAccessOperator(ast: untyped): untyped =
    ast.matchAst:
    of nnkBracketExpr(ident"x", ident"y"):
      echo "ok"

  testArrayAccessOperator(x[y])



  ## Parentheses

  scope:

    let ast = myquote:
      (1, 2, (3))

    ast.matchAst:
    of nnkPar(nnkIntLit(intVal = 1), nnkIntLit(intVal = 2), nnkPar(nnkIntLit(intVal = 3))):
      echo "ok"


  ## Curly braces

  scope:

    let ast = myquote:
      {1, 2, 3}

    ast.matchAst:
    of nnkCurly(nnkIntLit(intVal = 1), nnkIntLit(intVal = 2), nnkIntLit(intVal = 3)):
      echo "ok"

  scope:

    let ast = myquote:
      {a: 3, b: 5}

    ast.matchAst:
    of nnkTableConstr(
      nnkExprColonExpr(ident"a", nnkIntLit(intVal = 3)),
      nnkExprColonExpr(ident"b", nnkIntLit(intVal = 5))
    ):
      echo "ok"


  ## Brackets

  scope:

    let ast = myquote:
      [1, 2, 3]

    ast.matchAst:
    of nnkBracket(nnkIntLit(intVal = 1), nnkIntLit(intVal = 2), nnkIntLit(intVal = 3)):
      echo "ok"


  ## Ranges

  scope:

    let ast = myquote:
      1..3

    ast.matchAst:
    of nnkInfix(
      ident"..",
      nnkIntLit(intVal = 1),
      nnkIntLit(intVal = 3)
    ):
      echo "ok"


  ## If expression

  scope:

    let ast = myquote:
      if cond1: expr1 elif cond2: expr2 else: expr3

    ast.matchAst:
    of {nnkIfExpr, nnkIfStmt}(
      {nnkElifExpr, nnkElifBranch}(`cond1`, `expr1`),
      {nnkElifExpr, nnkElifBranch}(`cond2`, `expr2`),
      {nnkElseExpr, nnkElse}(`expr3`)
    ):
      echo "ok"

  ## Documentation Comments

  scope:

    let ast = myquote:
      ## This is a comment
      ## This is part of the first comment
      stmt1
      ## Yet another

    ast.matchAst:
    of nnkStmtList(
      nnkCommentStmt(),
      `stmt1`,
      nnkCommentStmt()
    ):
      echo "ok"
    else:
      echo "warning!"
      echo ast.treeRepr
      echo "TEST causes no fail, because of a regression in Nim."

  scope:
    let ast = myquote:
      {.emit: "#include <stdio.h>".}

    ast.matchAst:
    of nnkPragma(
      nnkExprColonExpr(
        ident"emit",
        nnkStrLit(strVal = "#include <stdio.h>") # the "argument"
      )
    ):
      echo "ok"

  scope:
    let ast = myquote:
      {.pragma: cdeclRename, cdecl.}

    ast.matchAst:
    of nnkPragma(
      nnkExprColonExpr(
        ident"pragma", # this is always first when declaring a new pragma
        ident"cdeclRename" # the name of the pragma
      ),
      ident"cdecl"
    ):
      echo "ok"



  scope:
    let ast = myquote:
      if cond1:
        stmt1
      elif cond2:
        stmt2
      elif cond3:
        stmt3
      else:
        stmt4

    ast.matchAst:
    of nnkIfStmt(
      nnkElifBranch(`cond1`, `stmt1`),
      nnkElifBranch(`cond2`, `stmt2`),
      nnkElifBranch(`cond3`, `stmt3`),
      nnkElse(`stmt4`)
    ):
      echo "ok"



  scope:
    let ast = myquote:
      x = 42

    ast.matchAst:
    of nnkAsgn(ident"x", nnkIntLit(intVal = 42)):
      echo "ok"



  scope:
    let ast = myquote:
      stmt1
      stmt2
      stmt3

    ast.matchAst:
    of nnkStmtList(`stmt1`, `stmt2`, `stmt3`):
      assert stmt1.strVal == "stmt1"
      assert stmt2.strVal == "stmt2"
      assert stmt3.strVal == "stmt3"
      echo "ok"

  ## Case statement

  scope:

    let ast = myquote:
      case expr1
      of expr2, expr3..expr4:
        stmt1
      of expr5:
        stmt2
      elif cond1:
        stmt3
      else:
        stmt4

    ast.matchAst:
    of nnkCaseStmt(
      `expr1`,
      nnkOfBranch(`expr2`, {nnkRange, nnkInfix}(_, `expr3`, `expr4`), `stmt1`),
      nnkOfBranch(`expr5`, `stmt2`),
      nnkElifBranch(`cond1`, `stmt3`),
      nnkElse(`stmt4`)
    ):
      echo "ok"

  ## While statement

  scope:

    let ast = myquote:
      while expr1:
        stmt1

    ast.matchAst:
    of nnkWhileStmt(`expr1`, `stmt1`):
      echo "ok"


  ## For statement

  scope:

    let ast = myquote:
      for ident1, ident2 in expr1:
        stmt1

    ast.matchAst:
    of nnkForStmt(`ident1`, `ident2`, `expr1`, `stmt1`):
      echo "ok"


  ## Try statement

  scope:

    let ast = myquote:
      try:
        stmt1
      except e1, e2:
        stmt2
      except e3:
        stmt3
      except:
        stmt4
      finally:
        stmt5

    ast.matchAst:
    of nnkTryStmt(
      `stmt1`,
      nnkExceptBranch(`e1`, `e2`, `stmt2`),
      nnkExceptBranch(`e3`, `stmt3`),
      nnkExceptBranch(`stmt4`),
      nnkFinally(`stmt5`)
    ):
      echo "ok"


  ## Return statement

  scope:

    let ast = myquote:
      return expr1

    ast.matchAst:
    of nnkReturnStmt(`expr1`):
      echo "ok"


  ## Continue statement

  scope:
    let ast = myquote:
      continue

    ast.matchAst:
    of nnkContinueStmt:
      echo "ok"

  ## Break statement

  scope:

    let ast = myquote:
      break otherLocation

    ast.matchAst:
    of nnkBreakStmt(ident"otherLocation"):
      echo "ok"

  ## Block statement

  scope:

    template blockStatement {.dirty.} =
      block name:
        discard

    let ast = getAst(blockStatement())

    ast.matchAst:
    of nnkBlockStmt(ident"name", nnkStmtList):
      echo "ok"

  ## Asm statement

  scope:

    let ast = myquote:
      asm """some asm"""

    ast.matchAst:
    of nnkAsmStmt(
      nnkEmpty(), # for pragmas
      nnkTripleStrLit(strVal = "some asm"),
    ):
      echo "ok"

  ## Import section

  scope:

    let ast = myquote:
      import math

    ast.matchAst:
    of nnkImportStmt(ident"math"):
      echo "ok"

  scope:

    let ast = myquote:
      import math except pow

    ast.matchAst:
    of nnkImportExceptStmt(ident"math",ident"pow"):
      echo "ok"

  scope:

    let ast = myquote:
      import strutils as su

    ast.matchAst:
    of nnkImportStmt(
      nnkInfix(
        ident"as",
        ident"strutils",
        ident"su"
      )
    ):
      echo "ok"

  ## From statement

  scope:

    let ast = myquote:
      from math import pow

    ast.matchAst:
    of nnkFromStmt(ident"math", ident"pow"):
      echo "ok"

  ## Export statement

  scope:

    let ast = myquote:
      export unsigned

    ast.matchAst:
    of nnkExportStmt(ident"unsigned"):
      echo "ok"

  scope:

    let ast = myquote:
      export math except pow # we're going to implement our own exponentiation

    ast.matchAst:
    of nnkExportExceptStmt(ident"math",ident"pow"):
      echo "ok"

  ## Include statement

  scope:

    let ast = myquote:
      include blocks

    ast.matchAst:
    of nnkIncludeStmt(ident"blocks"):
      echo "ok"

  ## Var section

  scope:

    let ast = myquote:
      var a = 3

    ast.matchAst:
    of nnkVarSection(
      nnkIdentDefs(
        ident"a",
        nnkEmpty(), # or nnkIdent(...) if the variable declares the type
        nnkIntLit(intVal = 3),
      )
    ):
      echo "ok"

  ## Let section

  scope:

    let ast = myquote:
      let a = 3

    ast.matchAst:
    of nnkLetSection(
      nnkIdentDefs(
        ident"a",
        nnkEmpty(), # or nnkIdent(...) for the type
        nnkIntLit(intVal = 3),
      )
    ):
      echo "ok"

  ## Const section

  scope:

    let ast = myquote:
      const a = 3

    ast.matchAst:
    of nnkConstSection(
      nnkConstDef( # not nnkConstDefs!
        ident"a",
        nnkEmpty(), # or nnkIdent(...) if the variable declares the type
        nnkIntLit(intVal = 3), # required in a const declaration!
      )
    ):
      echo "ok"

  ## Type section

  scope:

    let ast = myquote:
      type A = int

    ast.matchAst:
    of nnkTypeSection(
      nnkTypeDef(
        ident"A",
        nnkEmpty(),
        ident"int"
      )
    ):
      echo "ok"

  scope:

    let ast = myquote:
      type MyInt = distinct int

    ast.peelOff({nnkTypeSection}).matchAst:
    of# ...
      nnkTypeDef(
      ident"MyInt",
      nnkEmpty(),
      nnkDistinctTy(
        ident"int"
      )
    ):
      echo "ok"

  scope:

    let ast = myquote:
      type A[T] = expr1

    ast.matchAst:
    of nnkTypeSection(
      nnkTypeDef(
        ident"A",
        nnkGenericParams(
          nnkIdentDefs(
            ident"T",
            nnkEmpty(), # if the type is declared with options, like
                        # ``[T: SomeInteger]``, they are given here
            nnkEmpty()
          )
        ),
        `expr1`
      )
    ):
      echo "ok"

  scope:

    let ast = myquote:
      type IO = object of RootObj

    ast.peelOff(nnkTypeSection).matchAst:
    of nnkTypeDef(
      ident"IO",
      nnkEmpty(),
      nnkObjectTy(
        nnkEmpty(), # no pragmas here
        nnkOfInherit(
          ident"RootObj" # inherits from RootObj
        ),
        nnkEmpty()
      )
    ):
      echo "ok"

  scope:
    macro testRecCase(ast: untyped): untyped =
      ast.peelOff({nnkStmtList, nnkTypeSection})[2].matchAst:
      of nnkObjectTy(
        nnkPragma(
          ident"inheritable"
        ),
        nnkEmpty(),
        nnkRecList( # list of object parameters
          nnkIdentDefs(
            ident"name",
            ident"string",
            nnkEmpty()
          ),
          nnkRecCase( # case statement within object (not nnkCaseStmt)
            nnkIdentDefs(
              ident"isFat",
              ident"bool",
              nnkEmpty()
            ),
            nnkOfBranch(
              ident"true",
              nnkRecList( # again, a list of object parameters
                nnkIdentDefs(
                  ident"m",
                  nnkBracketExpr(
                    ident"array",
                    nnkIntLit(intVal = 100000),
                    ident"T"
                  ),
                  nnkEmpty()
                )
              )
            ),
            nnkOfBranch(
              ident"false",
              nnkRecList(
                nnkIdentDefs(
                  ident"m",
                  nnkBracketExpr(
                    ident"array",
                    nnkIntLit(intVal = 10),
                    ident"T"
                  ),
                  nnkEmpty()
                )
              )
            )
          )
        )
      ):
        echo "ok"



    testRecCase:
      type Obj[T] = object {.inheritable.}
        name: string
        case isFat: bool
        of true:
          m: array[100_000, T]
        of false:
          m: array[10, T]

  scope:

    let ast = myquote:
      type X = enum
        First

    ast.peelOff({nnkStmtList, nnkTypeSection})[2].matchAst:
    of nnkEnumTy(
      nnkEmpty(),
      ident"First" # you need at least one nnkIdent or the compiler complains
    ):
      echo "ok"

  scope:

    let ast = myquote:
      type Con = concept x,y,z
        (x & y & z) is string

    ast.peelOff({nnkStmtList, nnkTypeSection}).matchAst:
    of nnkTypeDef(_, _, nnkTypeClassTy(nnkArglist, _, _, nnkStmtList)):
      # note this isn't nnkConceptTy!
      echo "ok"


  scope:

    let astX = myquote:
      type
        A[T: static[int]] = object

    let ast = astX.peelOff({nnkStmtList, nnkTypeSection})

    ast.matchAst(err):  # this is a sub ast for this a findAst or something like that is useful
    of nnkTypeDef(_, nnkGenericParams( nnkIdentDefs( ident"T", nnkCall( ident"[]", ident"static", _ ), _ )), _):
      echo "ok"
    else:
      echo "foobar"
      echo ast.treeRepr


  scope:
    let ast = myquote:
      type MyProc[T] = proc(x: T)

    ast.peelOff({nnkStmtList, nnkTypeSection}).matchAst(err):
    of nnkTypeDef(
      ident"MyProc",
      nnkGenericParams, # here, not with the proc
      nnkProcTy( # behaves like a procedure declaration from here on
        nnkFormalParams, _
      )
    ):
      echo "ok"

  ## Mixin statement

  macro testMixinStatement(ast: untyped): untyped =
    ast.peelOff(nnkStmtList).matchAst:
    of nnkMixinStmt(ident"x"):
      echo "ok"

  testMixinStatement:
    mixin x

  ## Bind statement


  macro testBindStmt(ast: untyped): untyped =
    ast[0].matchAst:
    of `node` @ nnkBindStmt(ident"x"):
      echo "ok"

  testBindStmt:
    bind x

  ## Procedure declaration

  macro testProcedureDeclaration(ast: untyped): untyped =
    # NOTE this is wrong in astdef

    ast.peelOff(nnkStmtList).matchAst:
    of nnkProcDef(
      nnkPostfix(ident"*", ident"hello"), # the exported proc name
      nnkEmpty, # patterns for term rewriting in templates and macros (not procs)
      nnkGenericParams( # generic type parameters, like with type declaration
        nnkIdentDefs(
          ident"T",
          ident"SomeInteger", _
        )
      ),
      nnkFormalParams(
        ident"int", # the first FormalParam is the return type. nnkEmpty if there is none
        nnkIdentDefs(
          ident"x",
          ident"int", # type type (required for procs, not for templates)
          nnkIntLit(intVal = 3) # a default value
        ),
        nnkIdentDefs(
          ident"y",
          ident"float32",
          nnkEmpty
        )
      ),
      nnkPragma(ident"inline"),
      nnkEmpty, # reserved slot for future use
      `meat` @ nnkStmtList # the meat of the proc
    ):
      echo "ok got meat: ", meat.lispRepr

  testProcedureDeclaration:
    proc hello*[T: SomeInteger](x: int = 3, y: float32): int {.inline.} = discard

  scope:
    var ast = myquote:
      proc foobar(a, b: int): void

    ast = ast[3]

    ast.matchAst:  # sub expression
    of nnkFormalParams(
      _, # return would be here
      nnkIdentDefs(
        ident"a", # the first parameter
        ident"b", # directly to the second parameter
        ident"int", # their shared type identifier
        nnkEmpty, # default value would go here
      )
    ):
      echo "ok"

  scope:

    let ast = myquote:
      proc hello(): var int

    ast[3].matchAst: # subAst
    of nnkFormalParams(
      nnkVarTy(
        ident"int"
      )
    ):
      echo "ok"

  ## Iterator declaration

  scope:

    let ast = myquote:
      iterator nonsense[T](x: seq[T]): float {.closure.} =
        discard

    ast.matchAst:
    of nnkIteratorDef(ident"nonsense", nnkEmpty, _, _, _, _, _):
      echo "ok"

  ## Converter declaration

  scope:

    let ast = myquote:
      converter toBool(x: float): bool

    ast.matchAst:
    of nnkConverterDef(ident"toBool",_,_,_,_,_,_):
      echo "ok"

  ## Template declaration

  scope:
    let ast = myquote:
      template optOpt{expr1}(a: int): int

    ast.matchAst:
    of nnkTemplateDef(ident"optOpt", nnkStmtList(`expr1`), _, _, _, _, _):
      echo "ok"