summary refs log tree commit diff stats
path: root/compiler/semstmts.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-02-28 20:30:17 +0100
committerAndreas Rumpf <rumpf_a@web.de>2018-02-28 20:30:17 +0100
commit0495e6cf3a1cf0f5f71622a8408d24fbc27642a0 (patch)
tree4e70338483c08aef22dc62e7f0a551d8906637a9 /compiler/semstmts.nim
parenta897371797154844f96906e72fa013b8205d0393 (diff)
downloadNim-0495e6cf3a1cf0f5f71622a8408d24fbc27642a0.tar.gz
steps to get for loops as expressions
Diffstat (limited to 'compiler/semstmts.nim')
-rw-r--r--compiler/semstmts.nim16
1 files changed, 13 insertions, 3 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index 1e3265eae..a86787a8e 100644
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -679,7 +679,7 @@ proc semForVars(c: PContext, n: PNode): PNode =
         addForVarDecl(c, v)
   inc(c.p.nestedLoopCounter)
   openScope(c)
-  n.sons[length-1] = semStmt(c, n.sons[length-1])
+  n.sons[length-1] = semExprBranch(c, n.sons[length-1])
   closeScope(c)
   dec(c.p.nestedLoopCounter)
 
@@ -732,8 +732,18 @@ proc semFor(c: PContext, n: PNode): PNode =
   else:
     result = semForVars(c, n)
   # propagate any enforced VoidContext:
-  if n.sons[length-1].typ == enforceVoidContext:
-    result.typ = enforceVoidContext
+  let bodyType = n.sons[length-1].typ
+  if bodyType == enforceVoidContext or isEmptyType(bodyType):
+    result.typ = bodyType
+  else:
+    # if the body of a for loop is of type 'T', the
+    # loop's type is 'iterator (): T'
+    proc createForLoopExpr(c: PContext; t: PType; info: TLineInfo): PType {.nimcall.} =
+      result = newType(tyGenericInvocation, c.module)
+      addSonSkipIntLit(result, magicsys.getCompilerProc("ForLoopExpr").typ)
+      addSonSkipIntLit(result, t)
+      result = instGenericContainer(c, info, result, allowMetaTypes = false)
+    result.typ = createForLoopExpr(c, bodyType, result.info)
   closeScope(c)
 
 proc semRaise(c: PContext, n: PNode): PNode =