Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment showing how iterators can (almost) be implemented with a library solution #15655

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lib/std/iterates.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import std/macros

type IteratorError = object of Exception

proc replace(n: NimNode): NimNode =
case n.kind
of nnkContinueStmt:
result = newTree(nnkReturnStmt, newEmptyNode())
of nnkBreakStmt:
result = quote do:
raise newException(IteratorError, "")
else:
result = n
for i in 0..<n.len:
result[i] = n[i].replace

macro iterate*(x: ForLoopStmt): untyped =
let lhs = x[0]
let body = x[^1].replace
let iterateArgs = x[^2]
doAssert iterateArgs.len >= 2
let call = iterateArgs[1]

let formal = nnkFormalParams.newTree(newEmptyNode())
doAssert iterateArgs.len == 2
for i in 0..<x.len - 2:
formal.add nnkIdentDefs.newTree(x[i], ident"auto", newEmptyNode())

let anon = nnkLambda.newTree(
newEmptyNode(),
newEmptyNode(),
newEmptyNode(),
formal,
newEmptyNode(),
newEmptyNode(),
body
)

let par = nnkPar.newTree(anon)
if false:
call.insert(1, par)
else:
let par2 = quote do:
# cont = `par` # bug: doesn't work: Error: undeclared identifier: 'cont' (and would be ugly)
`par` # bug: downside, is it requires all optional args passed
call.add(par2)
result = quote do:
try:
`call`
except IteratorError:
discard
54 changes: 54 additions & 0 deletions tests/stdlib/titerates.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
discard """
targets: "c cpp js"
"""

import std/iterates

proc myIter[T](s: seq[T], cont: proc(_: T)) =
for ai in s: cont ai*10

proc myIter2(a, b: int, cont: proc(a1: int, a2: string)) =
for ai in a..b:
cont ai, $ai

template toSeq(T, a: untyped): untyped =
# type T = typeof(block: (for ai in iterate(a): ai))
var ret = newSeq[T]()
for x in iterate a:
ret.add x
ret

template main() =
block:
var ret: seq[int]
for x in iterate myIter(@[2,3]):
ret.add x
doAssert ret == @[20, 30]

doAssert toSeq(float, myIter(@[1.5, 2.0])) == @[15.0, 20.0]
doAssert toSeq(int, myIter(@[3])) == @[30]

block:
var ret: seq[(int, string)]
for k,v in iterate myIter2(2,5):
ret.add (k,v)
doAssert ret == @[(2, "2"), (3, "3"), (4, "4"), (5, "5")]

block: # continue, break
proc fn1(n: int, cont: proc(_: int)) =
for ai in 0..<n: cont ai

var ret: seq[string]
for x in iterate fn1(5):
ret.add $x
if x==2:
ret.add "continue"
continue
if x==3:
ret.add "break"
break
ret.add "after"
doAssert ret == @["0", "after", "1", "after", "2", "continue", "3", "break"]

static: main()
main()