1. Phix loop variables and scope

This little program (looptest.exw) came about because of a forum question for another language entirely.

 
integer x=0 
 
for x=2 to 7 do 
    ?x 
end for 
 
?x 

I believe the program is legal but it will not run. I get this...

<13:47:53>p looptest.exw 
 
c:\Users\Dev\phix\looptest.exw:5 
    ?x 
     ^ a namespace qualifier is required 
x is defined in: 
    c:\Users\Dev\phix\looptest.exw, 
    c:\Users\Dev\phix\looptest.exw. 
 
 
Press Enter, or d for diagnostics... 
I realised that I have never (ever) tried to use a loop variable that wasn't auto-declared in RDS-Eu or Phix.
Is the request for a namespace the expected response and, if so, how to resolve it? (Phix 0.7.9 - 32 bit Win)
Disclaimer: Since I haven't tried to use a 'global' loop variable in 12 years of Eu / Phix and I believe such things to be the devil's work (I have Ada development in my background) this doesn't cause me any difficulty at all.

new topic     » topic index » view message » categorize

2. Re: Phix loop variables and scope

Hi,

Yeah, that'll never work. The loop variable ahouldn't be declared, and once it's out of the loop, it's lost. you can jump out of the loop earlier, recording the loop variable into a local or global variable just before you do though, but the scope of x is only in the loop.

Cheers

Chris

new topic     » goto parent     » topic index » view message » categorize

3. Re: Phix loop variables and scope

Both languages say the same thing.

  • 'x' is defined in the first line
  • at the "top-level" you can not use 'x' in another definition (a loop variable is another definition of x)

The error messages compared:

  • OE simply says you have used 'x' already
  • Phix says because 'x' is already used, you better get a namespace qualifier if you try to define it in two places

So, the Phix wording is just an indirect way of showing you the same error.

integer x = 0 
 
for x = 2 to 7 do 
    ? x 
end for 
 
? x 
 
/* 

OE 
 
<0031>:: attempt to redefine x. 
for x = 2 to 7 do 
                ^ 
*/ 
 
 
/* 

PHIX 
 
    ? x 
      ^ a namespace qualifier is required 
x is defined in: 
    /media/oe/ORIGINAL/forum/loop variable/t01.ex, 
    /media/oe/ORIGINAL/forum/loop variable/t01.ex. 
*/ 

Both languages are ornary (stubbornly obstructive and unwilling to cooperate) about namespace use:

namespace mine 
 
integer mine:x 
 
/* 

OE error 
 
<0025>:: found ... variable ... but was expecting an identifier name 
integer mine:x 
             ^ 
*/ 
 
 
 
/* 

PHIX error 
 
integer mine:x 
             ^ namespace qualifier not specific enough 
x is defined in: 
*/ 
 
 
x = 0 
 
? x 
 
x += 1 
 
? mine:x 

_tom

new topic     » goto parent     » topic index » view message » categorize

4. Re: Phix loop variables and scope

This works fine in phix:

procedure p() 
integer x=0  
  
for x=2 to 7 do  
    ?x  
end for  
  
?x  
end procedure 
p() 

So I have classed this as a bug, which needed the following change to psym.e, line 3085 in routine InTable():

--25/5/18: 
--          if spNTyp=S_TVar then 
            if spNTyp=S_TVar  
            or (returnvar=-1 and spNTyp=S_GVar2) then 

which makes it work the same without the p() wrapper, and does not break any of the tests.

Note that using local namespaces does not work very well in phix: I had no idea they were even supposed to until last year,
and I'm still not sure, as in theory they should not make anything behave any differently whether present or not anyway.

Pete

PS:

namespace mine  
  
integer mine:x  

I would not expect that to work at all. Namespaces qualify references, not declarations.
I concede it is an odd error message, but there would be little benefit in fixing that.

PPS: In phix, pre-declaring the control loop variable makes it persist beyond the end for, in this case with a value of 8.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Phix loop variables and scope

petelomax said...

PS:

namespace mine  
  
integer mine:x  

I would not expect that to work at all. Namespaces qualify references, not declarations.
I concede it is an odd error message, but there would be little benefit in fixing that.

That explains it. But, looks like this "namespace" idea is incomplete.

petelomax said...

PPS: In phix, pre-declaring the control loop variable makes it persist beyond the end for, in this case with a value of 8.

What do you mean by "pre-declaring" ? (I only understand examples.)

_tom

new topic     » goto parent     » topic index » view message » categorize

6. Re: Phix loop variables and scope

_tom said...

looks like this "namespace" idea is incomplete.

They do what they were intended for, perfectly well, at least in my book.
[EDIT: Actually, some people do tend to abuse them, and fair point, they don't always withstand mistreatment too well...]

_tom said...

What do you mean by "pre-declaring" ? (I only understand examples.)

pre-declared:

integer x=0   
for x=2 to 7 do 
end for 
?x -- valid 

not pre-declared:

for y=2 to 7 do   
end for 
--?y -- invalid 

both (should) work equally, except as shown x can be referenced after the end for but y cannot.

new topic     » goto parent     » topic index » view message » categorize

7. Re: Phix loop variables and scope

pre-declared one

integer x = 0 
for x=2 to 7 do 
end for 
 
? x 
--> 0 

Phix runs. OE crashes.

The roundabout (aka loop) runs, but I can't use the variable 'x'

pre-declared two

atom x = 0 
 
for x=2 to 7 do 
   ? x 
end for 
 
? x 
 

Both crash.

_tom

new topic     » goto parent     » topic index » view message » categorize

8. Re: Phix loop variables and scope

I can confirm that the predeclared loop variable case works as implied in the help file after making the psym.e modification. Thanks Pete.

However, I'll keep using auto-declared loop variables for 'real' programs. Because I use many languages which tend to do different things with for-loop control variables at the end, I prefer while-loops if I really need the count value at the end.

Les

new topic     » goto parent     » topic index » view message » categorize

9. Re: Phix loop variables and scope

_tom said...

The roundabout (aka loop) runs, but I can't use the variable 'x'

Have you made the above change to psym.e and run p -cp?

_tom said...

pre-declared two

atom x = 0 
for x=2 to 7 do 
    ^ type error (for loop control variable must be an INTEGER) 

Phix does not allow floating point for-loops - however, that error is a bit harsh. I've changed pmain.e/DoFor() line 9291 as follows:

--25/5/18: 
--      elsif symtab[CN][S_vtype]!=T_integer then 
--          Aborp("type error (for loop control variable must be an INTEGER)") 
        else 
            integer cnvtyp = symtab[CN][S_vtype] 
            if cnvtyp>T_object 
            or not and_bits(cnvtyp,T_integer) then 
                Aborp("type error (for loop control variable must be an INTEGER)") 
            end if 
new topic     » goto parent     » topic index » view message » categorize

10. Re: Phix loop variables and scope

Is there a case for why you would want to pre declare loop variables?

Cheers

Chris

new topic     » goto parent     » topic index » view message » categorize

11. Re: Phix loop variables and scope

ChrisB said...

Is there a case for why you would want to pre declare loop variables?

Purely so that you can refer to/examine them on exit from the loop.

The only real difference should be tweaking the scope.

Pete

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu