forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Find current directory

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
DanRRight



Joined: 10 Mar 2008
Posts: 2777
Location: South Pole, Antarctica

PostPosted: Tue Jan 24, 2023 12:27 pm    Post subject: Find current directory Reply with quote

Anything wrong with this code ?

Code:
module mod1
character*256 CurrentDirectoryName, CURDIR@
contains

integer function aa()
  CurrentDirectoryName = CURDIR@()
  aa = 2
end function

end module
!-------------------------------------------
program A
  use mod1
  i = aa ()
  print*, trim(CurrentDirectoryName) 
end


while this one works OK:

Code:
character*256 CurrentDirectoryName, CURDIR@
  CurrentDirectoryName = CURDIR@()
  print*, trim(CurrentDirectoryName) 
end


Very useful function but causing me headaches for decades.
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 682
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Tue Jan 24, 2023 12:48 pm    Post subject: Reply with quote

Try this instead:

Code:
module mod1
character(len=256) CurrentDirectoryName

contains

integer function aa()
  character(len=256) CURDIR@
  CurrentDirectoryName = CURDIR@()
  aa = 2
end function

end module
!-------------------------------------------
program A
  use mod1
  i = aa ()
  print*, trim(CurrentDirectoryName)
end
Back to top
View user's profile Send private message Visit poster's website
DanRRight



Joined: 10 Mar 2008
Posts: 2777
Location: South Pole, Antarctica

PostPosted: Tue Jan 24, 2023 3:05 pm    Post subject: Reply with quote

hack Laughing

But what's wrong with my code? I lost a half day with it again. Causing crashes, spurious diagnostics in different places...
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7774
Location: Salford, UK

PostPosted: Tue Jan 24, 2023 3:52 pm    Post subject: Reply with quote

The compiler needs to be informed that CURDIR@ is an external function.

character*256,external::CURDIR@
Back to top
View user's profile Send private message AIM Address
DanRRight



Joined: 10 Mar 2008
Posts: 2777
Location: South Pole, Antarctica

PostPosted: Tue Jan 24, 2023 4:31 pm    Post subject: Reply with quote

CURDIR@ is external??? Very anti-intuitive.
Should we declare now all Silverfrost function with @ as external?

Though may be formally we have to declare as external all of them if use them with external compilers...
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 682
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Tue Jan 24, 2023 11:03 pm    Post subject: Reply with quote

I find this confusing. If the EXTERNAL declaration is required in the module in Danís code, why is it not also necessary in my modified version of Danís code?
Back to top
View user's profile Send private message Visit poster's website
mecej4



Joined: 31 Oct 2006
Posts: 1840

PostPosted: Wed Jan 25, 2023 12:27 am    Post subject: Reply with quote

When an identifier appears in an expression with an argument list (with zero or more arguments), and the variable has not been declared to be an array, the identifier implicitly acquires the EXTERNAL attribute. This is why Dan's second version of the code in the first post does not require an EXTERNAL declaration for CURDIR@.

In the first version of Dan's code, the type declaration (character *xxx) and the appearance of CURDIR@(), i.e., variable followed by argument list (empty in this case) do not occur in the same scope, hence the need for a separate EXTERNAL declaration or an interface block.
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2777
Location: South Pole, Antarctica

PostPosted: Wed Jan 25, 2023 8:18 am    Post subject: Reply with quote

yea...this is obscure and is not a fun to hear for average programmer (and Fortran programming particularly with FTN95 supposed to be fun, isn't it?) . How about creating another such function which will not be error-prone no matter how you use or abuse it?
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7774
Location: Salford, UK

PostPosted: Wed Jan 25, 2023 8:48 am    Post subject: Reply with quote

Ken

In your version CURDIR@() appears on the RHS of an assignment within the function that is currently being compiled. This is enought information for it to assume that it must be an external function.

In Dan's "failing" code, CURDIR@ intially looks like a module variable and the compiler does not scan ahead to see how it is used within the module routines.
Back to top
View user's profile Send private message AIM Address
Kenneth_Smith



Joined: 18 May 2012
Posts: 682
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Wed Jan 25, 2023 11:02 am    Post subject: Reply with quote

Mecej4, Paul,

Thank you both for your explanations. I understand what is happening now.

I knew how to make Danís first example work Ė the result of brute force testing to find what works years ago, without really knowing very much about the language rules or considering what the compiler does. Still learning Very Happy

Ken
Back to top
View user's profile Send private message Visit poster's website
mecej4



Joined: 31 Oct 2006
Posts: 1840

PostPosted: Wed Jan 25, 2023 1:39 pm    Post subject: Reply with quote

The scoping rules in Fortran are somewhat complicated, because new facilities (contained subprograms, modules) were added piecewise over decades, while insisting on keeping old programs unaffected by the new features.

A very similar question to the one of this thread may be seen in a thread from 2016 here:

https://stackoverflow.com/questions/40696534/scoping-rules-for-variable-and-functions-in-contained-subroutines
Back to top
View user's profile Send private message
DanRRight



Joined: 10 Mar 2008
Posts: 2777
Location: South Pole, Antarctica

PostPosted: Wed Jan 25, 2023 2:50 pm    Post subject: Reply with quote

Steve Jobs would not approve such solution in the language for things simple like 2x2. Most basic elementary things do not have to require hacking or knowledge of scoping. This is same sort of things, even more simpler, like OPEN file, pity Fortran Standard does not seem to think this is the case. This narrow vision makes Fortran limped and irrelevant to modern use. And the utter example of Standard's blindness was the slow adoption of parallelization so that the third parties MPI and OpenMP are still dominating this field

But what was most confusing here is the Salford/Silverfrost specific @. Not in 100 years i would expect declaration of external for this function Smile

I am sure i will return to this problem in a few years. I see people already had problems with this darn function until like Ken or me after hours of hacking found some shady workaround.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group