You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let input ="Hello \"Apple\" I am the \"Evil Sausage\""block foobar:
var readPos =0iteratorreadInput(): char {.exportc.} =echo"readInput: readPos: ", readPos, " input.len: ", input.len
while readPos < input.len:
readPos +=1yield input[readPos-1]
procparseString(): string {.exportc.} =result=""for c inreadInput():
if c =='"':
breakelse:
result.add c
for c inreadInput():
if c =='"':
let str =parseString()
echo"got string token: \"", str, "\""
The iterator is called three times, and both string tokens, "Aplle" and "Evil Sausage" could be identified.
This version here does not work, and the only difference is, the block is changed to be a function.
let input ="Hello \"Apple\" I am the \"Evil Sausage\""procfoobar(): void {.exportc.} =var readPos =0iteratorreadInput(): char {.exportc.} =echo"readInput: readPos: ", readPos, " input.len: ", input.len
while readPos < input.len:
readPos +=1yield input[readPos-1]
procparseString(): string {.exportc.} =result=""for c inreadInput():
if c =='"':
breakelse:
result.add c
for c inreadInput():
if c =='"':
let str =parseString()
echo"got string token: \"", str, "\""foobar()
The output is expected to be identical to the previous version. But you can see that for some reason readInput is only called once and all the other iterations are left empty. The program parses an empty string token at every " character in the input. There are four " characters in the input therefore there are four empty strings. The loop in parseString does not process a single character.
Here is the generated C code of the function parseString, you can easily see that the loop is not even generated:
Edit:
The exportc pragma is just to make the generated C code more readable. But it is not causing the problem or is something that I want to test here. It is debugging purpose only.
The text was updated successfully, but these errors were encountered:
In the first output there are readInput: readPos: 7 and readInput: readPos: 24 - meaning that iterator readInput() has started from the beginning, and not from where it has previously stopped in the while loop, right?
If that's the case, this seems like a candidate for another example in #7047.
@narimiran The iteratator from parse string should continue where the iterator in the outer loop left off. So it's a continuation of the same loop with different logic.
Hello "Apple" I am the "Evil Sausage"
^ ^ ^
| | `-- start of secord parseString: readPos: 24
| |
| `-- start of first parseString: readPos: 7
|
`-- outer loop start: readPos: 0
This code here works as expected
The output is:
The iterator is called three times, and both string tokens, "Aplle" and "Evil Sausage" could be identified.
This version here does not work, and the only difference is, the block is changed to be a function.
The output is:
The output is expected to be identical to the previous version. But you can see that for some reason
readInput
is only called once and all the other iterations are left empty. The program parses an empty string token at every"
character in the input. There are four"
characters in the input therefore there are four empty strings. The loop inparseString
does not process a single character.Here is the generated C code of the function
parseString
, you can easily see that the loop is not even generated:Here is the generated C code of the exact same
parseString
function but from the upper code section. This is how it should look like.Edit:
The exportc pragma is just to make the generated C code more readable. But it is not causing the problem or is something that I want to test here. It is debugging purpose only.
The text was updated successfully, but these errors were encountered: