itersgen.nim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Plugin to transform an inline iterator into a data structure.
  10. import ".." / [ast, astalgo,
  11. magicsys, lookups, semdata,
  12. lambdalifting, msgs]
  13. proc iterToProcImpl*(c: PContext, n: PNode): PNode =
  14. result = newNodeI(nkStmtList, n.info)
  15. let iter = n[1]
  16. if iter.kind != nkSym or iter.sym.kind != skIterator:
  17. localError(c.config, iter.info, "first argument needs to be an iterator")
  18. return
  19. if n[2].typ.isNil:
  20. localError(c.config, n[2].info, "second argument needs to be a type")
  21. return
  22. if n[3].kind != nkIdent:
  23. localError(c.config, n[3].info, "third argument needs to be an identifier")
  24. return
  25. let t = n[2].typ.skipTypes({tyTypeDesc, tyGenericInst})
  26. if t.kind notin {tyRef, tyPtr} or t.lastSon.kind != tyObject:
  27. localError(c.config, n[2].info,
  28. "type must be a non-generic ref|ptr to object with state field")
  29. return
  30. let body = liftIterToProc(c.graph, iter.sym, iter.sym.getBody, t)
  31. let prc = newSym(skProc, n[3].ident, iter.sym.owner, iter.sym.info)
  32. prc.typ = copyType(iter.sym.typ, prc, false)
  33. excl prc.typ.flags, tfCapturesEnv
  34. prc.typ.n.add newSymNode(getEnvParam(iter.sym))
  35. prc.typ.rawAddSon t
  36. let orig = iter.sym.ast
  37. prc.ast = newProcNode(nkProcDef, n.info,
  38. body = body, params = orig[paramsPos], name = newSymNode(prc),
  39. pattern = c.graph.emptyNode, genericParams = c.graph.emptyNode,
  40. pragmas = orig[pragmasPos], exceptions = c.graph.emptyNode)
  41. prc.ast.add iter.sym.ast.sons[resultPos]
  42. addInterfaceDecl(c, prc)