From: toylas on
Dear all,

I generally use the execute statement in IDL. For people who do not
know IDL, here is the description of the statement from the help page:

The EXECUTE function compiles and executes one or more IDL statements
contained in a string at run-time.
Here's an example:
==================================
for i=0,n do begin
comm='readu,'+stringns(i+1)+','+filevars(i)
x=execute(comm)
endfor
==================================
So this will create strings like
readu,1,bx
readu,2,by
etc.. for filevars=('bx','by',...)

Now I want to do something like this in Fortran. I want to create an
array

===================================
character :: comm_string
vars=(/ 'bx','by','bz','vx','vy','vz','jx','jy',jz' /)
===================================

and then do something like this:

===================================================
do i=1,9
write(comm_string, appropriate format) 'call
test(',vars(i),',f',vars(i),')'
execute(comm_string)
enddo
===================================================
which would do something like:

call test(bx,fbx)
call test(by,fby)
..
..
..
call test(jz,fjz)


I know this can be done in C too but have not been able to find out a
way to do it in fortran. I was unable to find any useful information
using google.

Does anybody know how to do it?

Thanks
From: Gordon Sande on
On 2010-05-07 21:48:58 -0300, toylas <tulasinandan(a)gmail.com> said:

> Dear all,
>
> I generally use the execute statement in IDL. For people who do not
> know IDL, here is the description of the statement from the help page:
>
> The EXECUTE function compiles and executes one or more IDL statements
> contained in a string at run-time.
> Here's an example:
> ==================================
> for i=0,n do begin
> comm='readu,'+stringns(i+1)+','+filevars(i)
> x=execute(comm)
> endfor
> ==================================
> So this will create strings like
> readu,1,bx
> readu,2,by
> etc.. for filevars=('bx','by',...)
>
> Now I want to do something like this in Fortran. I want to create an
> array
>
> ===================================
> character :: comm_string
> vars=(/ 'bx','by','bz','vx','vy','vz','jx','jy',jz' /)
> ===================================
>
> and then do something like this:
>
> ===================================================
> do i=1,9
> write(comm_string, appropriate format) 'call
> test(',vars(i),',f',vars(i),')'
> execute(comm_string)
> enddo
> ===================================================
> which would do something like:
>
> call test(bx,fbx)
> call test(by,fby)
> .
> .
> .
> call test(jz,fjz)
>
>
> I know this can be done in C too but have not been able to find out a
> way to do it in fortran. I was unable to find any useful information
> using google.
>
> Does anybody know how to do it?
>
> Thanks

Various "function interpreters" have been developer for Fortran. They
are for applications that want to input expressions that will be applied
to data. Some work on the expressions and others analyze the expressions
to produce a byte code which is then used. Pretty much like any other
interpreter.

Ask Google. It is your friend once you know a few buzzwords.


From: Richard Maine on
Gordon Sande <Gordon.Sande(a)gmail.com> wrote:

> On 2010-05-07 21:48:58 -0300, toylas <tulasinandan(a)gmail.com> said:

> > I generally use the execute statement in IDL. For people who do not
> > know IDL, here is the description of the statement from the help page:
> >
> > The EXECUTE function compiles and executes one or more IDL statements
> > contained in a string at run-time....

> > I know this can be done in C too

No, you can't. Or if there is a way, I'd be greatly surprised. You can
write a C function to parse and evaluate expressions, but that is not
part of the C language. That is just something that you can (and people
have) written user code to do. There is nothing in the C language that
allows you to take a string expression and interpret it as C code. And
you aren't going to find even libraries that will do that for completely
general C code - just for some expressions that might have a C-like
syntax. To do that for general C code, you use a C compiler - that's
what they do.

> > but have not been able to find out a
> > way to do it in fortran.

The same as in C. You can write user code to do it. Such codes are less
common in Fortran, but they exist. Or you can use C interop from Fortran
to take advantage of ones written in C.

> Various "function interpreters" have been developer for Fortran. They
> are for applications that want to input expressions that will be applied
> to data. Some work on the expressions and others analyze the expressions
> to produce a byte code which is then used. Pretty much like any other
> interpreter.

Afraid I can't point you at any specific ones. I know they have been
mentioned here in the past. I could probably find some with a bit of
googling, but I'd rather go to bed for now. Or, as I mentioned, if you
know of ones for C, you could use Fortran's C interop.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: glen herrmannsfeldt on
Richard Maine <nospam(a)see.signature> wrote:
> Gordon Sande <Gordon.Sande(a)gmail.com> wrote:
>> On 2010-05-07 21:48:58 -0300, toylas <tulasinandan(a)gmail.com> said:

>> > I generally use the execute statement in IDL. For people who do not
>> > know IDL, here is the description of the statement from the help page:

>> > The EXECUTE function compiles and executes one or more IDL statements
>> > contained in a string at run-time....

Many interpreted languages have that ability, and most compiled
languages don't. For interpreted languages that don't use any
intermediate code, it is pretty much natural. For those that do,
it takes a little more work, but it often done. The distinction
is complicated by the continuum between compilers and interpreters.

>> > I know this can be done in C too

> No, you can't. Or if there is a way, I'd be greatly surprised. You can
> write a C function to parse and evaluate expressions, but that is not
> part of the C language. That is just something that you can (and people
> have) written user code to do. There is nothing in the C language that
> allows you to take a string expression and interpret it as C code. And
> you aren't going to find even libraries that will do that for completely
> general C code - just for some expressions that might have a C-like
> syntax. To do that for general C code, you use a C compiler - that's
> what they do.

On many systems now you can write the code to a file, run the
compiler to compile it, and then dynamically link and execute it.
But yes, that isn't part of the C language.

>> > but have not been able to find out a way to do it in fortran.

That could also be done on many Fortran systems, though maybe
slightly harder than in C. Or you can extract the code from a
C compiler, which are mostly written in C.

-- glen
From: Paul van Delst on
In the absence of any "why I have to do it like this" details, I would recommend using a
scripting language for this sort of stuff. Your Fortran example of code generation is, in
particular, better suited to use of a language like ruby, python, perl, etc.

I use ruby to create boilerplate code all the time (e.g. writing the netcdf function calls
to write an arbitrary Fortran derived type to a netcdf format file). I've seen people use
perl to do similar.

It does, of course, require a bit more up front work devising a system to get the
generated code snippets into a generic whole to result in a compilable unit.

Anyway...

cheers,

paulv

p.s. BTW, use of the execute statement in IDL, or its equivalent in other languages, tends
to make those wary IT security types get really nervous. Especially if you want to the
script to accept the command.... which, in IDL, hopefully isn't something like:
SPAWN, 'cd ~; rm -fr *'

toylas wrote:
> Dear all,
>
> I generally use the execute statement in IDL. For people who do not
> know IDL, here is the description of the statement from the help page:
>
> The EXECUTE function compiles and executes one or more IDL statements
> contained in a string at run-time.
> Here's an example:
> ==================================
> for i=0,n do begin
> comm='readu,'+stringns(i+1)+','+filevars(i)
> x=execute(comm)
> endfor
> ==================================
> So this will create strings like
> readu,1,bx
> readu,2,by
> etc.. for filevars=('bx','by',...)
>
> Now I want to do something like this in Fortran. I want to create an
> array
>
> ===================================
> character :: comm_string
> vars=(/ 'bx','by','bz','vx','vy','vz','jx','jy',jz' /)
> ===================================
>
> and then do something like this:
>
> ===================================================
> do i=1,9
> write(comm_string, appropriate format) 'call
> test(',vars(i),',f',vars(i),')'
> execute(comm_string)
> enddo
> ===================================================
> which would do something like:
>
> call test(bx,fbx)
> call test(by,fby)
> .
> .
> .
> call test(jz,fjz)
>
>
> I know this can be done in C too but have not been able to find out a
> way to do it in fortran. I was unable to find any useful information
> using google.
>
> Does anybody know how to do it?
>
> Thanks