This commit is contained in:
EXetoC 2014-05-03 23:32:14 +02:00
commit 8802688e9f
2 changed files with 22 additions and 13 deletions

View file

@ -811,7 +811,7 @@ template createVar(futSymName: string, asyncProc: PNimrodNode,
result.add generateExceptionCheck(futSym, exceptBranch, rootReceiver) result.add generateExceptionCheck(futSym, exceptBranch, rootReceiver)
proc processBody(node, retFutureSym: PNimrodNode, proc processBody(node, retFutureSym: PNimrodNode,
subtypeName: string, subTypeIsVoid: bool,
exceptBranch: PNimrodNode): PNimrodNode {.compileTime.} = exceptBranch: PNimrodNode): PNimrodNode {.compileTime.} =
#echo(node.treeRepr) #echo(node.treeRepr)
result = node result = node
@ -819,14 +819,14 @@ proc processBody(node, retFutureSym: PNimrodNode,
of nnkReturnStmt: of nnkReturnStmt:
result = newNimNode(nnkStmtList) result = newNimNode(nnkStmtList)
if node[0].kind == nnkEmpty: if node[0].kind == nnkEmpty:
if subtypeName != "void": if not subtypeIsVoid:
result.add newCall(newIdentNode("complete"), retFutureSym, result.add newCall(newIdentNode("complete"), retFutureSym,
newIdentNode("result")) newIdentNode("result"))
else: else:
result.add newCall(newIdentNode("complete"), retFutureSym) result.add newCall(newIdentNode("complete"), retFutureSym)
else: else:
result.add newCall(newIdentNode("complete"), retFutureSym, result.add newCall(newIdentNode("complete"), retFutureSym,
node[0].processBody(retFutureSym, subtypeName, exceptBranch)) node[0].processBody(retFutureSym, subtypeIsVoid, exceptBranch))
result.add newNimNode(nnkReturnStmt).add(newNilLit()) result.add newNimNode(nnkReturnStmt).add(newNilLit())
return # Don't process the children of this return stmt return # Don't process the children of this return stmt
@ -880,7 +880,7 @@ proc processBody(node, retFutureSym: PNimrodNode,
res: PNimrodNode): bool {.compileTime.} = res: PNimrodNode): bool {.compileTime.} =
result = false result = false
while i < n[0].len: while i < n[0].len:
var processed = processBody(n[0][i], retFutureSym, subtypeName, n[1]) var processed = processBody(n[0][i], retFutureSym, subtypeIsVoid, n[1])
if processed.kind != n[0][i].kind or processed.len != n[0][i].len: if processed.kind != n[0][i].kind or processed.len != n[0][i].len:
expectKind(processed, nnkStmtList) expectKind(processed, nnkStmtList)
expectKind(processed[2][1], nnkElse) expectKind(processed[2][1], nnkElse)
@ -900,7 +900,7 @@ proc processBody(node, retFutureSym: PNimrodNode,
else: discard else: discard
for i in 0 .. <result.len: for i in 0 .. <result.len:
result[i] = processBody(result[i], retFutureSym, subtypeName, exceptBranch) result[i] = processBody(result[i], retFutureSym, subtypeIsVoid, exceptBranch)
proc getName(node: PNimrodNode): string {.compileTime.} = proc getName(node: PNimrodNode): string {.compileTime.} =
case node.kind case node.kind
@ -920,35 +920,36 @@ macro async*(prc: stmt): stmt {.immediate.} =
hint("Processing " & prc[0].getName & " as an async proc.") hint("Processing " & prc[0].getName & " as an async proc.")
let returnType = prc[3][0] let returnType = prc[3][0]
var subtypeName = ""
# Verify that the return type is a PFuture[T] # Verify that the return type is a PFuture[T]
if returnType.kind == nnkIdent: if returnType.kind == nnkIdent:
error("Expected return type of 'PFuture' got '" & $returnType & "'") error("Expected return type of 'PFuture' got '" & $returnType & "'")
elif returnType.kind == nnkBracketExpr: elif returnType.kind == nnkBracketExpr:
if $returnType[0] != "PFuture": if $returnType[0] != "PFuture":
error("Expected return type of 'PFuture' got '" & $returnType[0] & "'") error("Expected return type of 'PFuture' got '" & $returnType[0] & "'")
subtypeName = $returnType[1].ident
elif returnType.kind == nnkEmpty: let subtypeIsVoid = returnType.kind == nnkEmpty
subtypeName = "void"
var outerProcBody = newNimNode(nnkStmtList) var outerProcBody = newNimNode(nnkStmtList)
# -> var retFuture = newFuture[T]() # -> var retFuture = newFuture[T]()
var retFutureSym = genSym(nskVar, "retFuture") var retFutureSym = genSym(nskVar, "retFuture")
var subRetType =
if returnType.kind == nnkEmpty: newIdentNode("void")
else: returnType[1]
outerProcBody.add( outerProcBody.add(
newVarStmt(retFutureSym, newVarStmt(retFutureSym,
newCall( newCall(
newNimNode(nnkBracketExpr).add( newNimNode(nnkBracketExpr).add(
newIdentNode(!"newFuture"), # TODO: Strange bug here? Remove the `!`. newIdentNode(!"newFuture"), # TODO: Strange bug here? Remove the `!`.
newIdentNode(subtypeName))))) # Get type from return type of this proc subRetType)))) # Get type from return type of this proc
# -> iterator nameIter(): PFutureBase {.closure.} = # -> iterator nameIter(): PFutureBase {.closure.} =
# -> var result: T # -> var result: T
# -> <proc_body> # -> <proc_body>
# -> complete(retFuture, result) # -> complete(retFuture, result)
var iteratorNameSym = genSym(nskIterator, $prc[0].getName & "Iter") var iteratorNameSym = genSym(nskIterator, $prc[0].getName & "Iter")
var procBody = prc[6].processBody(retFutureSym, subtypeName, nil) var procBody = prc[6].processBody(retFutureSym, subtypeIsVoid, nil)
if subtypeName != "void": if not subtypeIsVoid:
procBody.insert(0, newNimNode(nnkVarSection).add( procBody.insert(0, newNimNode(nnkVarSection).add(
newIdentDefs(newIdentNode("result"), returnType[1]))) # -> var result: T newIdentDefs(newIdentNode("result"), returnType[1]))) # -> var result: T
procBody.add( procBody.add(
@ -977,7 +978,7 @@ macro async*(prc: stmt): stmt {.immediate.} =
for i in 0 .. <result[4].len: for i in 0 .. <result[4].len:
if result[4][i].ident == !"async": if result[4][i].ident == !"async":
result[4].del(i) result[4].del(i)
if subtypeName == "void": if subtypeIsVoid:
# Add discardable pragma. # Add discardable pragma.
result[4].add(newIdentNode("discardable")) result[4].add(newIdentNode("discardable"))
if returnType.kind == nnkEmpty: if returnType.kind == nnkEmpty:

View file

@ -0,0 +1,8 @@
import asyncdispatch, asyncnet
proc main {.async.} =
proc f: PFuture[seq[int]] {.async.} =
await newAsyncSocket().connect("www.google.com", TPort(80))
let x = await f()
main()