Re: loop do .. until end loop is surely incorrect!
- Posted by DerekParnell (admin) Feb 26, 2011
- 1804 views
loop do integer x = 3 until x = 3 end loop
According to the manual this is correct.
It does NOT compile. version 4.0.0 on linux (slackware 13.1).
I cannot find this in the manual. Can you tell me where you can see this construct in the manual?
Maybe the manual should make it clearer what part is the 'loop boundaries' and what part is the 'loop body'.Something like ...
LOOPSTMT ::= LOOP_PREFIX LOOP_BODY LOOP_SUFFIX LOOP_PREFIX ::= 'loop' [ 'with' 'entry' ] [ 'label' STRINGLIT ] 'do' LOOP_BODY ::= STATEMENTS [ 'entry' STATEMENTS ] LOOP_SUFFIX ::= 'until' CONDITION 'end' 'loop'That would make it clearer that any declarations made in the LOOP_BODY are no longer in scope by the time the LOOP_SUFFIX is parsed. Thus CONDITION cannot legally refer to variables declared in LOOP_BODY.
Is there something stopping you from moving the declaration outside of the loop body? ...
integer x = 3 loop do -- statements ... -- until x = 3 end loop
I do not like the loop scoping rules in Euphoria because they depend on syntax: where the loop ends, when the dynamics would indicate that the scope should be from declaration to the exit from the loop.
"depend on syntax" ... like all the other programming languages do?
Actually the scope in Euphoria, just like other languages, is from the declaration to the end of the block that contains the declaration, which is just prior to the 'until' clause.
This ambiguity is problematic: eg.
while TRUE with entry do integer x = 3 entry ? x end while
will compile but not run because x has on value on the 1st iteration.
A better parser/syntax analyser could prevent this sort of thing from compiling. But are you surprised that it fails to run? In effect it is the same as ...
? x integer x = 3