From 1740619c0cd3f94c2fe16560d44402daca449e53 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Sun, 18 Sep 2016 18:15:20 +0200 Subject: Implements {.multisync.} pragma for async and sync proc combos. --- lib/pure/asyncmacro.nim | 110 +++++++++++++++++++++++++++++++++++++++++++++ tests/async/tmultisync.nim | 8 ++++ 2 files changed, 118 insertions(+) create mode 100644 tests/async/tmultisync.nim diff --git a/lib/pure/asyncmacro.nim b/lib/pure/asyncmacro.nim index 6b565cd3b..1fdeb35f0 100644 --- a/lib/pure/asyncmacro.nim +++ b/lib/pure/asyncmacro.nim @@ -369,3 +369,113 @@ macro async*(prc: untyped): untyped = result = asyncSingleProc(prc) when defined(nimDumpAsync): echo repr result + + +# Multisync +proc emptyNoop[T](x: T): T = + # The ``await``s are replaced by a call to this for simplicity. + when T isnot void: + return x + +proc stripAwait(node: NimNode): NimNode = + ## Strips out all ``await`` commands from a procedure body, replaces them + ## with ``emptyNoop`` for simplicity. + result = node + + let emptyNoopSym = bindSym("emptyNoop") + + case node.kind + of nnkCommand, nnkCall: + if node[0].kind == nnkIdent and node[0].ident == !"await": + node[0] = emptyNoopSym + elif node.len > 1 and node[1].kind == nnkCommand and + node[1][0].kind == nnkIdent and node[1][0].ident == !"await": + # foo await x + node[1][0] = emptyNoopSym + of nnkVarSection, nnkLetSection: + case node[0][2].kind + of nnkCommand: + if node[0][2][0].kind == nnkIdent and node[0][2][0].ident == !"await": + # var x = await y + node[0][2][0] = emptyNoopSym + else: discard + of nnkAsgn: + case node[1].kind + of nnkCommand: + if node[1][0].ident == !"await": + # x = await y + node[1][0] = emptyNoopSym + else: discard + of nnkDiscardStmt: + # discard await x + if node[0].kind == nnkCommand and node[0][0].kind == nnkIdent and + node[0][0].ident == !"await": + node[0][0] = emptyNoopSym + else: discard + + for i in 0 ..