def __init__(self, ast, name): self._schedule = None self._name = name # Store the whole fparser2 AST self._ast = ast # A temporary workaround for the fact that we don't yet have a # symbol table to store information on the variable declarations. # TODO (#255) remove this workaround. self._loop_vars = [] self._name_space_manager = NameSpaceFactory().create() from fparser.two.Fortran2003 import Execution_Part, Specification_Part # Find the section of the tree containing the execution part # of the code exe_part = get_child(ast, Execution_Part) if not exe_part: # This subroutine has no execution part so we skip it # TODO log this event return # Store the root of this routine's specification in the AST self._spec_part = get_child(ast, Specification_Part) # We now walk through the AST produced by fparser2 and construct a # new AST using objects from the nemo module. self._schedule = NemoSchedule(self, exe_part)
def __init__(self, ast): # pylint: disable=super-init-not-called from fparser.two.Fortran2003 import Main_Program, \ Subroutine_Subprogram, Function_Subprogram, Function_Stmt, Name self.invoke_map = {} self.invoke_list = [] # Keep a pointer to the whole fparser2 AST self._ast = ast # Find all the subroutines contained in the file routines = walk_ast(ast.content, [Subroutine_Subprogram, Function_Subprogram]) # Add the main program as a routine to analyse - take care # here as the Fortran source file might not contain a # main program (might just be a subroutine in a module) main_prog = get_child(ast, Main_Program) if main_prog: routines.append(main_prog) # Analyse each routine we've found for subroutine in routines: # Get the name of this subroutine, program or function substmt = subroutine.content[0] if isinstance(substmt, Function_Stmt): for item in substmt.items: if isinstance(item, Name): sub_name = str(item) break else: sub_name = str(substmt.get_name()) my_invoke = NemoInvoke(subroutine, name=sub_name) self.invoke_map[sub_name] = my_invoke self.invoke_list.append(my_invoke)
def test_get_child(): ''' Test the get_child() utility. ''' from fparser.two import Fortran2003 from fparser.two.utils import walk, get_child reader = get_reader("program hello\n" "write(*,*) 'hello'\n" "write(*,*) 'goodbye'\n" "end program hello\n") main = Fortran2003.Program(reader) prog = get_child(main, Fortran2003.Main_Program) exe = get_child(prog, Fortran2003.Execution_Part) assert isinstance(exe, Fortran2003.Execution_Part) write_stmt = get_child(exe, Fortran2003.Write_Stmt) # Check that we got the first write and not the second assert "goodbye" not in str(write_stmt) # The top level has no Io_Control_Spec children assert not get_child(main, Fortran2003.Io_Control_Spec) # Check functionality when node has children in `items` and # not in `content` io_nodes = walk(main.content, Fortran2003.Io_Control_Spec) assert not hasattr(io_nodes[0], "content") io_unit = get_child(io_nodes[0], Fortran2003.Io_Unit) assert isinstance(io_unit, Fortran2003.Io_Unit) missing = get_child(io_nodes[0], Fortran2003.Execution_Part) assert missing is None
def __init__(self, ast): # pylint: disable=super-init-not-called from fparser.two.Fortran2003 import Main_Program, \ Subroutine_Subprogram, Function_Subprogram, Function_Stmt, Name self.invoke_map = {} self.invoke_list = [] # Keep a pointer to the whole fparser2 AST self._ast = ast # TODO #737 - this routine should really process generic PSyIR to # create domain-specific PSyIR (D-PSyIR) for the NEMO domain. # Use the fparser2 frontend to construct the PSyIR from the parse tree processor = NemoFparser2Reader() # First create a Container representing any Fortran module # contained in the parse tree. self._container = processor.generate_container(ast) # Find all the subroutines contained in the file routines = walk(ast.content, (Subroutine_Subprogram, Function_Subprogram)) # Add the main program as a routine to analyse - take care # here as the Fortran source file might not contain a # main program (might just be a subroutine in a module) main_prog = get_child(ast, Main_Program) if main_prog: routines.append(main_prog) # Analyse each routine we've found for subroutine in routines: # Get the name of this subroutine, program or function substmt = subroutine.content[0] if isinstance(substmt, Function_Stmt): for item in substmt.items: if isinstance(item, Name): sub_name = str(item) break else: sub_name = str(substmt.get_name()) my_invoke = NemoInvoke(subroutine, sub_name, self) self.invoke_map[sub_name] = my_invoke self.invoke_list.append(my_invoke)
def test_action_stmts(): ''' Tests for comments within action statements such as allocate ''' # We have to have our allocate() within some other block otherwise # we lose the comment (it is deferred until after the current, # continuted line is complete but once that happens, the match for # the allocate() is done and the reader/parser returns without # ever dealing with the comment). source = '''\ if(.true.)then allocate(my_array(size), & ! that's a big array my_array2(size)) end if ''' from fparser.two.Fortran2003 import If_Construct, Allocate_Stmt from fparser.two.utils import get_child reader = get_reader(source, isfree=True, ignore_comments=False) ifstmt = If_Construct(reader) assert isinstance(ifstmt, If_Construct) assert isinstance(ifstmt.content[1], Allocate_Stmt) assert "a big array" in str(ifstmt) cmt = get_child(ifstmt, Comment) assert cmt.parent is ifstmt