travellhyne / f2py

Automatically exported from code.google.com/p/f2py
Other
0 stars 0 forks source link

How to extract block.content recursively? #12

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi pearu,
I wanted to know how to extract instances recursively.
For the purpose, I made a function,

def toreprx(block, depth=-1, incrtab=''):
    if depth==0 or not block.content:return
    for c in block.content:
        print 'xxxxxx ', depth, c.__class__.__name__
        toreprx(c,depth-1,incrtab)
    return

I think this should work when we supply parser.block to this routine. 
But it gives strange answer. block.content of Comment is not None. 
Why so? For me it is easy (e.g. to set up call-caller trees) if this works.

What I did is
1. Put takao_blocktest.py just above f2py/
2. do
>takao_blocktest.py takao_blocktest.F

takao

Original issue reported on code.google.com by TakaoKot...@gmail.com on 24 Mar 2010 at 11:56

Attachments:

GoogleCodeExporter commented 9 years ago
Note that not all Statement instances have content attribute,
and if they have, then the content attribute does not need to be a list
(in the Comment case the content attribute is a string).
The proper way to check if a statement has "substatements" is to
check if the statement is BeginStatement instance.

I have implemented a walk function that recursivly yields statements
and their depths from the block till given depth. See

  fparser.api.walk.__doc__

for more information.

Original comment by pearu.peterson on 25 Mar 2010 at 10:21

GoogleCodeExporter commented 9 years ago
Thank you for your suggestions. I understand what you said.

To make things easier, I think your fparser should include an API
which makes array of statement classes. 
API should be together with some simple examples. 
I now have def stackclass(block) below. 
I hope your package will include something like this.

Then people do "f_arrayc.py *.F", and understand how it works.

takao

--------f_arrayc.py -----------
#!/usr/bin/env python
import sys,os
thisdir= os.path.dirname(os.path.abspath(__file__))
sys.path.append(thisdir+'/f2py/fparser')
from base_classes import EndStatement,classes

def stackclass(block):
    arrayc=[]
    toreprx(block,arrayc)
    return arrayc

def toreprx(block,arrayc, depth=-1, incrtab=''):
    if isinstance(block, classes.BeginStatement):
        for c in block.content:
            #print 'xxxxxx ', depth, c.__class__.__name__
            depthx=depth
            if isinstance(c, classes.EndStatement):
                depthx=depth+1
            arrayc.append([depthx,c])
            toreprx(c,arrayc,depth-1,incrtab)
    return

########## main #########
from api import parse
from readfortran import *
from parsefortran import FortranParser
#nargv = len(sys.argv) -1
argset= sys.argv[1:]
print argset
from inspect import *

for ffile in argset:
    print '---- @@@@@ '+ffile+' @@@@@ start -----'
    reader = FortranFileReader(ffile)
    reader.set_mode(isfree=False,isstrict=False)
    parser=FortranParser(reader,ignore_comments=False)
    parser.parse()
    parser.analyze()

    arrayc = stackclass(parser.block)
    for c in arrayc:
        if(isinstance(c[1],classes.Comment)):
            print c[0],c[1].__class__.__name__,c[1].item.comment, c[1].item.span
        else:
            print c[0],c[1].__class__.__name__,c[1].item.line, c[1].item.span
        #print dir(c[1].item)

    print '---- @@@@@ '+ffile+' @@@@@ end   -----'
    print 
    print 
--------end of f_arrayc.py -----------

Original comment by TakaoKot...@gmail.com on 26 Mar 2010 at 9:46

GoogleCodeExporter commented 9 years ago
fparser.api.walk is doing already this, try:

..
for c in fparser.api.walk(parser.block):
..

That is, list(fparser.api.walk(parser.block)) should be equal
to stackclass(parser.block). 

Using walk function is more memory efficient.

Original comment by pearu.peterson on 26 Mar 2010 at 10:15

GoogleCodeExporter commented 9 years ago
I see. I should have examined your code first. 
Thank you.

Original comment by TakaoKot...@gmail.com on 26 Mar 2010 at 12:05

GoogleCodeExporter commented 9 years ago

Original comment by pearu.peterson on 26 Mar 2010 at 7:58