From 38b836b49e772aed38077d6aac810da183d03255 Mon Sep 17 00:00:00 2001 From: Ico Doornekamp Date: Fri, 12 Jul 2019 14:41:54 +0200 Subject: [PATCH] Moved 'Case statement macros' documentation out of manual into manual_experimental (#11716) --- doc/manual.rst | 51 ------------------------------------- doc/manual_experimental.rst | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/doc/manual.rst b/doc/manual.rst index 31ca1e7735..36b56f8507 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -5149,57 +5149,6 @@ Currently for loop macros must be enabled explicitly via ``{.experimental: "forLoopMacros".}``. -Case statement macros ---------------------- - -A macro that needs to be called `match`:idx: can be used to rewrite -``case`` statements in order to implement `pattern matching`:idx: for -certain types. The following example implements a simplistic form of -pattern matching for tuples, leveraging the existing equality operator -for tuples (as provided in ``system.==``): - -.. code-block:: nim - :test: "nim c $1" - - {.experimental: "caseStmtMacros".} - - import macros - - macro match(n: tuple): untyped = - result = newTree(nnkIfStmt) - let selector = n[0] - for i in 1 ..< n.len: - let it = n[i] - case it.kind - of nnkElse, nnkElifBranch, nnkElifExpr, nnkElseExpr: - result.add it - of nnkOfBranch: - for j in 0..it.len-2: - let cond = newCall("==", selector, it[j]) - result.add newTree(nnkElifBranch, cond, it[^1]) - else: - error "'match' cannot handle this node", it - echo repr result - - case ("foo", 78) - of ("foo", 78): echo "yes" - of ("bar", 88): echo "no" - else: discard - - -Currently case statement macros must be enabled explicitly -via ``{.experimental: "caseStmtMacros".}``. - -``match`` macros are subject to overload resolution. First the -``case``'s selector expression is used to determine which ``match`` -macro to call. To this macro is then passed the complete ``case`` -statement body and the macro is evaluated. - -In other words, the macro needs to transform the full ``case`` statement -but only the statement's selector expression is used to determine which -macro to call. - - Special Types ============= diff --git a/doc/manual_experimental.rst b/doc/manual_experimental.rst index a341f33dec..1b007722ca 100644 --- a/doc/manual_experimental.rst +++ b/doc/manual_experimental.rst @@ -886,6 +886,57 @@ The builtin ``deepCopy`` can even clone closures and their environments. See the documentation of `spawn`_ for details. +Case statement macros +===================== + +A macro that needs to be called `match`:idx: can be used to rewrite +``case`` statements in order to implement `pattern matching`:idx: for +certain types. The following example implements a simplistic form of +pattern matching for tuples, leveraging the existing equality operator +for tuples (as provided in ``system.==``): + +.. code-block:: nim + :test: "nim c $1" + + {.experimental: "caseStmtMacros".} + + import macros + + macro match(n: tuple): untyped = + result = newTree(nnkIfStmt) + let selector = n[0] + for i in 1 ..< n.len: + let it = n[i] + case it.kind + of nnkElse, nnkElifBranch, nnkElifExpr, nnkElseExpr: + result.add it + of nnkOfBranch: + for j in 0..it.len-2: + let cond = newCall("==", selector, it[j]) + result.add newTree(nnkElifBranch, cond, it[^1]) + else: + error "'match' cannot handle this node", it + echo repr result + + case ("foo", 78) + of ("foo", 78): echo "yes" + of ("bar", 88): echo "no" + else: discard + + +Currently case statement macros must be enabled explicitly +via ``{.experimental: "caseStmtMacros".}``. + +``match`` macros are subject to overload resolution. First the +``case``'s selector expression is used to determine which ``match`` +macro to call. To this macro is then passed the complete ``case`` +statement body and the macro is evaluated. + +In other words, the macro needs to transform the full ``case`` statement +but only the statement's selector expression is used to determine which +macro to call. + + Term rewriting macros =====================