Cosmo/examples/iterator.cosmo
CPunch e5eca7bed6 Added iterable objects
__iter and __next are now reserved IStrings, OP_NEXT and OP_ITER have also been added. A new token (TOKEN_IN) has been added to the lexer. The parser now supports the for each loop (for i, ... in <object> do ... end).
savedPushed has been removed from the CCompilerState struct.
2020-12-15 21:21:51 -06:00

22 lines
377 B
Plaintext

proto Range
function __init(self, x)
self.max = x
end
function __iter(self)
self.i = 0
return self
end
function __next(self)
if self.i >= self.max then
return nil // exit iterator loop
end
return self.i++, self.i
end
end
for i, x in Range(10) do
print("was " .. i .. ", now " .. x)
end