j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
178 stars 15 forks source link

Allow alphanumeric line labels as goto target #249

Open Beliavsky opened 2 years ago

Beliavsky commented 2 years ago

On Fortran Discourse it was suggested (and I agree) that alphanumeric line labels be allowed, since

go to error_controller
...
error_controller: If (error) then

is more legible than

go to 1234
...
1234 If (error) then
klausler commented 2 years ago

This will lead to ambiguity in cases like

character(80) fmt = "('do you mean this one?')"
fmt: format('or this one?')
write (6, fmt)
      integer to, toto, tototo
      to: continue
      toto: continue
      tototo: continue
      assigntototototo
      doiii=1,10
      i: continue
      ii: continue
integer label
assign 1 to label
go to label
1 print *, 'this one?'
return
label: print *, 'or this one?'
Beliavsky commented 2 years ago

Can the ambiguity be avoided by requiring that the line label not have the same name as any variable or named constant in the program unit?

klausler commented 2 years ago

That would invalidate existing conformant code.

certik commented 2 years ago

One could perhaps allow the same labels and variable names, but not allowing to jump to them if they are the same. But the parsing can indeed be quite tricky, we should only do it if it doesn't complicate the parser further.

egiovan commented 2 years ago

One may restrict the alphanumeric line label only to continue statements except when continue is terminating a loop. Strictly speaking the assign statement is deleted feature, but I understand that the compilers have to support it anyway. As a deleted feature it may continue to use only numeric labels. The same for the do, the label if present, should be numeric. Moreover the new syntax may have the precedence. So your first case will be illegal, the second compilation error as assign needs a numeric label, the third compilation error without an enddo, and the last one print *,"or this one" as the new syntax should take the precedence.