mirror of
https://github.com/CPunch/Cosmo.git
synced 2025-11-11 01:50:07 +00:00
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.
This commit is contained in:
22
examples/iterator.cosmo
Normal file
22
examples/iterator.cosmo
Normal file
@@ -0,0 +1,22 @@
|
||||
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
|
||||
Reference in New Issue
Block a user