delphi.translators.for2py package

Submodules

delphi.translators.for2py.arrays module

File: for2py_arrays.py Purpose: Code to handle array manipulation in the Python code generated by for2py.

Usage: see the document “for2py: Handling Fortran Arrays”

class Array(types, bounds)[source]

Bases: object

bounds is a list [(lo1,hi1), (lo2,hi2), …, (loN, hiN)] of pairs of lower and upper bounds for the dimensions of the array. The length of the list bounds gives the number of dimensions of the array.

bounds()[source]

bounds() returns a list of pairs (lo,hi) giving the lower and upper bounds of the array.

lower_bd(i)[source]

lower_bd(i) returns the lower bound of the array in dimension i. Dimensions are numbered from 0 up.

upper_bd(i)[source]

upper_bd(i) returns the upper bound of the array in dimension i. Dimensions are numbered from 0 up.

set_(subs, val)[source]

set_() sets the value of the array element specified by the given tuple of array subscript values to the argument val.

get_(subs)[source]

get_() returns the value of the array element specified by the given tuple of array subscript values.

get_elems(subs_list)[source]

get_elems(subs_list) returns a list of values of the array elements specified by the list of subscript values subs_list (each element of subs_list is a tuple of subscripts identifying an array element).

set_elems(subs, vals)[source]

set_elems(subs, vals) sets the array elements specified by the list of subscript values subs (each element of subs is a tuple of subscripts identifying an array element) to the corresponding value in vals.

all_subs(bounds)[source]

given a list of tuples specifying the bounds of an array, all_subs() returns a list of all the tuples of subscripts for that array.

idx2subs(idx_list)[source]

Given a list idx_list of index values for each dimension of an array, idx2subs() returns a list of the tuples of subscripts for all of the array elements specified by those index values.

Note: This code adapted from that posted by jfs at https://stackoverflow.com/questions/533905/get-the-cartesian-product- of-a-series-of-lists

array_values(expr)[source]

Given an expression expr denoting a list of values, array_values(expr) returns a list of values for that expression.

array_subscripts(expr)[source]

Given a subscript expression expr (i.e., an expression that denotes the set of elements of some array that are to be accessed), array_subscripts() returns a list of the elements denoted by expr.

flatten(in_list)[source]

given a list of values in_list, flatten returns the list obtained by flattening the top-level elements of in_list.

implied_loop_expr(expr, start, end, delta)[source]

given the parameters of an implied loop – namely, the start and end values together with the delta per iteration – implied_loop_expr() returns a list of values of the lambda expression expr applied to successive values of the implied loop.

delphi.translators.for2py.f2grfn module

This program exists to replacement the autoTranslate bash script. Instead of creating and using each file for next operation like in the autoTranslate bash script, it creates Python object and passes it to the next function. Thus, it works as calling a multiple functions in a single program. This new f2grfn.py does not invoke main functions in each program.

In simplicity, it’s a single program that integrates the functionality of test_program_analysis.py and autoTranslate.

fortran_file: An original input file to a program that is to be

translated to GrFN.

root_dir: A root directory where module log file should be created or

found.

Author: Terrence J. Lim

OFP_JAR_FILES = ['antlr-3.3-complete.jar', 'commons-cli-1.4.jar', 'OpenFortranParser-0.8.4-3.jar', 'OpenFortranParserXML-0.4.1.jar']

OFP_JAR_FILES is a list of JAR files used by the Open Fortran Parser (OFP).

MODULE_FILE_PREFIX = 'm_'

Module file prefix that all generated Python module files will be specified with.

MODULE_FILE_NAME = 'modFileLog.json'

A file that holds log of all files with module(s)

generate_ofp_xml(preprocessed_fortran_file, temp_dir, save_intermediate_files=False) str[source]

This function runs Open Fortran Parser to generate initial AST XML from the preprocessed Fortran file.

Parameters:

preprocessed_fortran_file (str) – A preprocessed fortran file name.

Returns:

str – OFP-generated XML as a string

generate_preprocessed_fortran(original_fortran_file, temp_dir)[source]

This function generates preprocessed fortran file.

Parameters:
  • original_fortran_file (str) – Original fortran file path.

  • temp_dir (str) – Target directory where temporary files will be

  • stored.

Returns:

str – Preprocessed file path.

generate_rectified_xml(ofp_xml_string, original_fortran_file, module_log_file_path, temp_dir)[source]

This function rectifies XML that was generated by OFP. Then, it will generate an output file, but also returns rectified element tree object back to the caller.

Parameters:
  • ofp_xml (str) – A string of XML that was generated by OFP.

  • rectified_file (str) – A file name that rectified XML

  • to. (will be written) –

  • original_fortran_file (str) – Original fortran file path.

  • module_log_file_path (str) – Path to module log file.

Returns:

A two-tuple with the following elements – Element Tree (ET) Object: An object of generated rectified XML. module_files_to_process: a list of module files to process.

generate_outputdict(rectified_tree, preprocessed_fortran_file) Dict[source]

This function generates a dictionary of ast and generates a dict with XML generated by translate.py and comments obtained with get_comments.py.

Parameters:
  • ( (rectified_tree) – obj: ‘ET’): An object of rectified XML.

  • preprocessed_fortran_file (str) – Path to preprocessed fortran file

Returns:

dict – A dictionary of XML generated by translate.py

generate_python_sources(output_dictionary, python_files, main_python_file, temp_dir) List[Tuple][source]

This function generates Python source file from generated Python source list. This function will return this list back to the caller for GrFN generation.

Parameters:
  • output_dictionary (dict) – A dictionary of XML generated

  • translate.py. (by) –

  • python_files – A list of python file names.

  • python_file_name (str) – A file name where translated python strings

  • to. (will be written) –

  • temp_dir (str) – Temporary directory to store the translated files.

Returns:

str – A string of generated Python code.

generate_grfn(python_source_string, python_file_path, lambdas_file_path, mode_mapper_dictionary, original_fortran_file, mod_log_file_path, processing_modules) Dict[source]

This function generates GrFN dictionary object and file.

Parameters:
  • python_source_string (str) – A string of Python code.

  • python_file_path (str) – A generated Python file path.

  • lambdas_file_path (str) – A lambdas file path.

  • mode_mapper_dictionary (list) – A mapper of file info (i.e. filename,

  • module

  • exports (and) –

  • etc).

  • original_fortran_file (str) – The path to the original

  • analyzed. (Fortran file being) –

  • mod_log_file_path (str) – A path to module log file.

  • processing_modules (bool) – A boolean condition marker to indicate

  • module. (whether current GrFN generation is for a) –

Returns:

dict – A dictionary of generated GrFN.

is_module_file(filename)[source]

This function is to check whether the handling file is a module file or not. :param filename: Name of a file. :type filename: str

Returns:

(bool) True if it is a module file. (bool) False, if it is not a module file.

check_classpath()[source]

check_classpath() checks whether the files in OFP_JAR_FILES can all be found in via the environment variable CLASSPATH.

indent(elem, level=0)[source]

This function indents each level of XML.

Source: https://stackoverflow.com/questions/3095434/inserting-newlines -in-xml-file-generated-via-xml-etree-elementstree-in-python

Parameters:
  • ( (elem) – obj: ‘ET’): An element tree XML object.

  • level (int) – A root level of XML.

fortran_to_grfn(original_fortran=None, temp_dir=None, root_dir_path='.', module_file_name='modFileLog.json', processing_modules=False, save_intermediate_files=False)[source]

This function invokes other appropriate functions to process and generate objects to translate fortran to python IR. This function will either be invoked by local main function or the outer tester functions, such as test_program_analysis.py or network.py.

Parameters:
  • original_fortran (str) – A file name of original fortran script.

  • temp_dir (str) – A default temporary directory where output

  • stored. (files will be) –

  • root_dir_path (str) – A root directory of the program.

  • module_file_name (str) – A module log file name.

  • processing_modules (bool) – A boolean condition marker to indicate

  • module. (whether current fortran_to_grfn execution is for processing a) –

Returns:

{

‘python_src’: A string of Python code, ‘python_file’: A file name of generated python script, ‘lambdas_file’: A file name where lambdas will be, ‘mode_mapper_dict’: mapper of file info (i.e. filename, module, and exports, etc).

}

delphi.translators.for2py.format module

File: format.py

Purpose: Process a string (obtained from a data file) according to

a Fortran FORMAT.

NOTE: Not all format specifiers are supported at this time. The input formats supported are shown in the method match_input_fmt_1(), while the output formats supported are shown in the method gen_output_fmt_1().

Usage:
  1. FORMATTED I/O

Given a Fortran READ or WRITE statement that should be processed according to a format list fmt_list (where fmt_list is a Python list of Fortran FORMAT descriptors), do the following:

  1. Create a Format object as follows:

    my_fmt_obj = Format(fmt_list)

  2. INPUT: To process a line of input inp_ln according to this format and assign values to a tuple of variables var_list:

    var_list = my_fmt_obj.read_line(inp_ln)

    OUTPUT: To construct a line to be printed out given a set of values val1, …, valN:

    out_string = my_fmt_obj.write_line([val1, …, valN])

  1. LIST_DIRECTED I/O

At this time only list-directed output has been implemented.

For list-directed output, e.g.: WRITE (,) X, Y, Z do the following:

  1. Construct a list of the types of the values to be written. Let this list be denoted by out_type_list.

  2. Construct a list of format specifiers for these types using:

    fmt_list = list_output_format(out_type_list)

  3. Use fmt_list as described above.

There are some examples towards the end of this file.

class Format(format_list)[source]

Bases: object

init_read_line()[source]

init_read_line() initializes fields relevant to input matching

init_write_line()[source]

init_write_line() initializes fields relevant to output generation

read_line(line)[source]

Match a line of input according to the format specified and return a tuple of the resulting values

write_line(values)[source]

Process a list of values according to the format specified to generate a line of output.

match_input_fmt(fmt_list)[source]

Given a list of Fortran format specifiers, e.g., [‘I5’, ‘2X’, ‘F4.1’], this function constructs a list of tuples for matching an input string against those format specifiers.

match_input_fmt_1(fmt)[source]

Given a single format specifier, e.g., ‘2X’, ‘I5’, etc., this function constructs a list of tuples for matching against that specifier. Each element of this list is a tuple

(xtract_re, cvt_re, divisor, cvt_fn)

where:

xtract_re is a regular expression that extracts an input field of

the requisite width;

cvt_re is a regular expression that matches the character sequence

extracted by xtract_re against the specified format;

divisor is the value to divide by in order to get the appropriate

number of decimal places if a decimal point is not given in the input value (meaningful only for floats); and

cvt_fn is a string denoting the function to be used to convert the

matched string to a value.

gen_output_fmt(fmt_list)[source]

given a list of Fortran format specifiers, e.g., [‘I5’, ‘2X’, ‘F4.1’], this function constructs a list of tuples for constructing an output string based on those format specifiers.

gen_output_fmt_1(fmt)[source]

given a single format specifier, get_output_fmt_1() constructs and returns a list of tuples for matching against that specifier.

Each element of this list is a tuple

(gen_fmt, cvt_fmt, sz)

where:

gen_fmt is the Python format specifier for assembling this value into the string constructed for output; cvt_fmt is the Python format specifier for converting this value into a string that will be assembled into the output string; and sz is the width of this field.

default_output_format(type_item)[source]
list_output_formats(type_list)[source]

This function takes a list of type names and returns a list of format specifiers for list-directed output of values of those types.

list_input_formats(type_list)[source]
list_data_type(type_list)[source]

This function takes a list of format specifiers and returns a list of data types represented by the format specifiers.

example_1()[source]
example_2()[source]
example_3()[source]

delphi.translators.for2py.genCode module

class PrintState(sep=None, add=None)[source]

Bases: object

copy(sep=None, add=None)[source]
class genCode(use_numpy=False, string_length=None, lambda_string='')[source]

Bases: object

generate_code(node, state, length=None)[source]

This function parses the ast node of the python file and generates python code relevant to the information in the ast. This is used as the statements inside the lambda functions.

process_function_definition(node, state)[source]
process_arguments(node, state)[source]
process_index(node, state)[source]
process_list_ast(node, state)[source]
process_tuple(node, state)[source]
process_for(node, state)[source]
process_if(node, state)[source]
process_unary_operation(node, state)[source]
process_binary_operation(node, state)[source]
process_expression(node, state)[source]
process_compare(node, state)[source]
process_subscript(node, state)[source]
process_name(node, *_)[source]
process_annotated_assign(node, state)[source]
process_assign(node, state)[source]
process_call(node, state)[source]
process_import(node, state)[source]
process_module(node, state)[source]
process_boolean_operation(node, state)[source]
process_attribute(node, *_)[source]
static process_ast(node, *_)[source]

delphi.translators.for2py.genModFileLog module

This program will scan all Fortran files in the given path searching for files that hold modules. Then, it will create a log file in JSON format.

Example

This script can be executed as below: $ python genModFileLog.py -d <root_directory> -f <log_file_name>

fortran_file_path: Original input file that uses module file. log_file_name: User does not have to provide the name as it is default to “modFileLog.json”, but (s)he can specify it with -f option follow by the file name in string.

Currently, this program assumes that module files reside in the same directory as use program file.

Author: Terrence J. Lim

parse_args()[source]

This function is for a safe command line input. It should receive the fortran file name and returns it back to the caller.

Returns:

str – A file name of original fortran script.

get_file_list_in_directory(root_dir_path)[source]

This function lists all Fortran files (excluding directories) in the specified directory.

Parameters:

dir_path (str) – Directory path.

Returns:

List – List of Fortran files.

modules_from_file(file_path, file_to_mod_mapper, mod_to_file_mapper, mod_info_dict)[source]

This function checks whether the module and file path already exist int the log file. If they do, then it compares the last_modified_time in the log file with the last modified time of file in disk. Then, it will call ‘populate_mapper’ function if file was not already looked before or the file was modified since last analyzed. :param file_path: File path that is guaranteed to exist in the

directory.

Parameters:
  • file_to_mod_mapper (dict) – Dictionary of lists that will hold file-to-module_name mappings.

  • mod_to_file_mapper (dict) – Dictionary that holds a module to its residing file path.

Returns:

None.

populate_mappers(file_path, file_to_mod_mapper, mod_to_file_mapper, mod_info_dict)[source]

This function populates two mappers by checking and extracting module names, if exist, from the file, and map it to the file name. :param file_path: File of a path that will be scanned. :type file_path: str :param file_to_mod_mapper: Dictionary of lists that will

hold file-to-module_name mappings.

Parameters:

mod_to_file_mapper (dict) – Dictionary that holds a module to its residing file path.

Returns:

None.

populate_procedure_functions(procedure_functions, module_summary)[source]

This function completes procedure_functions dictionary.

Params:
procedure_functions (dict): A dictionary to hold interface-to-procedure

function mappings.

module_summary (dict): A dictionary for holding module-to-subroutine-to-

arguments mappings.

Returns:

None.

populate_module_summary(f, module_summary, procedure_functions, derived_types)[source]

This function extracts module, derived type, and interface information, and populates module summary, procedure functions, and derived types dictionaries.

Params:

f (str): File content. module_summary (dict): A dictionary for holding module-to-subroutine-to-

arguments mappings.

procedure_functions (dict): A dictionary to hold interface-to-procedure

function mappings.

derived_types (dict): Dictionary that will hold module-to-derived type

mapping.

Returns:

None.

extract_subroutine_info(pgm, end_pgm, module_summary, current_modu, subroutine, current_subr, line)[source]

This function extracts information of subroutine declared within the module, and stores those information to module_summary dictionary.

Params:

pgm (tuple): Current program information. end_pgm (typle): End of current program indicator. module_summary (dict): A dictionary for holding module-to-subroutine-to- arguments mappings. current_modu (str): Module name that current interface is located under. subroutine (tuple): Holds information of the subroutine. current_subr (str): Current subroutine name. line (str): A line from Fortran source code.

Returns:

(current_subr) Currently handling subroutine name.

extract_interface_info(pgm, end_pgm, procedure_functions, current_modu, current_intr, line)[source]

This function extracts INTERFACE information, such as the name of interface and procedure function names, and populates procedure_functions dictionary.

Params:

pgm (tuple): Current program information. end_pgm (typle): End of current program indicator. procedure_functions (dict): A dictionary to hold interface-to-procedure function mappings. current_modu (str): Module name that current interface is located under. current_intr (str): Current interface name. line (str): A line from Fortran source code.

Returns:

(current_intr) Currently handling interface name.

extract_derived_type_info(end_pgm, current_modu, derived_types)[source]

This function extracts derived types declared under current module.

Params:

end_pgm (tuple): End of current program indicator. current_modu (str): Current module name. derived_types (dict): Dictionary that will hold module-to-derived type mapping.

Returns:

None.

get_file_last_modified_time(file_path)[source]

This function retrieves the file status and assigns the last modified time of a file at the end of the file_to_mod_mapper[file_path] list.

Params:
file_path (str): File path that is assumed to exist in the

directory.

Returns:

int – Last modified time represented as an integer.

update_mod_info_json(module_log_file_path, mode_mapper_dict)[source]

This function updates each module’s information, such as the declared variables and their types, so that genPGM.py can simply reference this dictionary rather than processing the file again.

Params:

module_log_file_path (str): Path to module log file. mode_mapper_dict (dict): A dictionary that holds all information of a module(s).

mod_file_log_generator(root_dir_path=None, module_log_file_name=None)[source]

This function is like a main function to invoke other functions to perform all checks and population of mappers. Though, loading of and writing to JSON file will happen in this function.

Parameters:
  • root_dir_path – Directory to examine for Fortran files

  • module_log_file_name – Path to module log file.

Returns:

None.

delphi.translators.for2py.genPGM module

class GrFNState(lambda_strings: Optional[List[str]], last_definitions: Optional[Dict] = {}, next_definitions: Optional[Dict] = {}, last_definition_default=0, function_name=None, variable_types: Optional[Dict] = {}, start: Optional[Dict] = {}, scope_path: Optional[List] = [], arrays: Optional[Dict] = {}, array_types: Optional[Dict] = {}, array_assign_name: Optional = None, string_assign_name: Optional = None)[source]

Bases: object

copy(lambda_strings: Optional[List[str]] = None, last_definitions: Optional[Dict] = None, next_definitions: Optional[Dict] = None, last_definition_default=None, function_name=None, variable_types: Optional[Dict] = None, start: Optional[Dict] = None, scope_path: Optional[List] = None, arrays: Optional[Dict] = None, array_types: Optional[Dict] = None, array_assign_name: Optional = None, string_assign_name: Optional = None)[source]
class GrFNGenerator(annotated_assigned=[], function_definitions=[])[source]

Bases: object

gen_grfn(node, state, call_source)[source]

This function generates the GrFN structure by parsing through the python AST

process_list(node, state, call_source)[source]

If there are one or more ast nodes inside the body of a node, they appear as a list. Process each node in the list and chain them together into a single list of GrFN dictionaries.

process_function_definition(node, state, *_)[source]

This function processes the function definition i.e. functionDef instance. It appends GrFN dictionaries to the functions key in the main GrFN JSON. These dictionaries consist of the function_assign_grfn of the function body and the function_container_grfn of the function. Every call to this function adds these along with the identifier_spec_grfn to the main GrFN JSON.

process_arguments(node, state, call_source)[source]

This function returns a list of arguments defined in a function definition. node.args is a list of arg nodes which are iteratively processed to get the argument name.

process_arg(node, state, call_source)[source]

This function processes a function argument.

process_index(node, state, *_)[source]

This function handles the Index node of the ast. The Index node is a slice value which appears when a [] indexing occurs. For example: x[Real], a[0], etc. So, the value of the index can either be an ast.Name (x[Real]) or an ast.Num (a[0]), or any other ast type. So, we forward the value to its respective ast handler.

process_num(node, *_)[source]

This function handles the ast.Num of the ast tree. This node only contains a numeric value in its body. For example: Num(n=0), Num(n=17.27), etc. So, we return the numeric value in a <function_assign_body_literal_spec> form.

process_list_ast(node, state, *_)[source]

This function handles ast.List which represents Python lists. The ast.List has an elts element which is a list of all the elements of the list. This is most notably encountered in annotated assignment of variables to [None] (Example: day: List[int] = [ None]). This is handled by calling gen_grfn on every element of the list i.e. every element of elts.

static process_str(node, *_)[source]

This function handles the ast.Str of the ast tree. This node only contains a string value in its body. For example: Str(s=’lorem’), Str(s=’Estimate: ‘), etc. So, we return the string value in a <function_assign_body_literal_spec> form where the dtype is a string.

process_for(node, state, *_)[source]

This function handles the ast.For node of the AST.

process_while(node, state, *_)[source]

This function handles the while loop. The functionality will be very similar to that of the for loop described in process_for

process_if(node, state, call_source)[source]

This function handles the ast.IF node of the AST. It goes through the IF body and generates the decision and condition type of the <function_assign_def>.

process_unary_operation(node, state, *_)[source]

This function processes unary operations in Python represented by ast.UnaryOp. This node has an op key which contains the operation (e.g. USub for -, UAdd for +, Not, Invert) and an operand key which contains the operand of the operation. This operand can in itself be any Python object (Number, Function call, Binary Operation, Unary Operation, etc. So, iteratively call the respective ast handler for the operand.

process_binary_operation(node, state, *_)[source]

This function handles binary operations i.e. ast.BinOp

process_boolean_operation(node, state, *_)[source]

This function will process the ast.BoolOp node that handles boolean operations i.e. AND, OR, etc.

static process_unnecessary_types(node, *_)[source]

This function handles various ast tags which are unnecessary and need not be handled since we do not require to parse them

process_expression(node, state, *_)[source]

This function handles the ast.Expr node i.e. the expression node. This node appears on function calls such as when calling a function, calling print(), etc.

process_compare(node, state, *_)[source]

This function handles ast.Compare i.e. the comparator tag which appears on logical comparison i.e. ==, <, >, <=, etc. This generally occurs within an if statement but can occur elsewhere as well.

process_subscript(node, state, *_)[source]

This function handles the ast.Subscript i.e. subscript tag of the ast. This tag appears on variable names that are indexed i.e. x[0], y[5], var[float], etc. Subscript nodes will have a slice tag which gives a information inside the [] of the call.

process_name(node, state, call_source)[source]

This function handles the ast.Name node of the AST. This node represents any variable in the code.

process_annotated_assign(node, state, *_)[source]

This function handles annotated assignment operations i.e. ast.AnnAssign. This tag appears when a variable has been assigned with an annotation e.g. x: int = 5, y: List[float] = [None], etc.

process_assign(node, state, *_)[source]

This function handles an assignment operation (ast.Assign).

process_tuple(node, state, *_)[source]

This function handles the ast.Tuple node of the AST. This handled in the same way process_list_ast is handled.

process_call(node, state, *_)[source]

This function handles the ast.Call node of the AST. This node denotes the call to a function. The body contains of the function name and its arguments.

process_module(node, state, *_)[source]

This function handles the ast.Module node in the AST. The module node is the starting point of the AST and its body consists of the entire ast of the python code.

process_attribute(node, state, call_source)[source]

Handle Attributes: This is a fix on feature_save branch to bypass the SAVE statement feature where a SAVEd variable is referenced as <function_name>.<variable_name>. So the code below only returns the <variable_name> which is stored under node.attr. The node.id stores the <function_name> which is being ignored.

process_return_value(node, state, *_)[source]

This function handles the return value from a function.

process_class_def(node, state, *_)[source]

This function handles user defined type (class) by populating types grfn attribute.

static process_break(*_)[source]

Process the breaks in the file, adding an EXIT node

static process_ast(node, *_)[source]
process_load(node, state, call_source)[source]
process_store(node, state, call_source)[source]
static process_nomatch(node, *_)[source]
make_source_list_dict(source_dictionary)[source]
make_fn_dict(name, target, sources, state)[source]
make_call_body_dict(source)[source]

We are going to remove addition of functions such as “max”, “exp”, “sin”, etc to the source list. The following two lines when commented helps us do that. If user-defined functions come up as sources, some other approach might be required.

process_decorators(node, state)[source]

Go through each decorator and extract relevant information. Currently this function only checks for the static_vars decorator for the SAVEd variables and updates variable_types with the data type of each variable.

get_last_definition(var, last_definitions, last_definition_default)[source]

This function returns the last (current) definition (index) of a variable.

get_variable_type(annotation_node)[source]

This function returns the data type of a variable using the annotation information used to define that variable

generate_gensym(tag)[source]

The gensym is used to uniquely identify any identifier in the program. Python’s uuid library is used to generate a unique 12 digit HEX string. The uuid4() function of ‘uuid’ focuses on randomness. Each and every bit of a UUID v4 is generated randomly and with no inherent logic. To every gensym, we add a tag signifying the data type it represents. ‘v’: variables ‘c’: containers ‘f’: functions

generate_lambda_function(node, function_name: str, return_value: bool, array_assign: bool, string_assign: bool, d_type_assign: bool, inputs, state, is_custom: bool)[source]
generate_container_id_name(namespace_file: str, scope_path, container_basename: str) str[source]
generate_variable_definition(variables, reference, d_type_object_assign, state)[source]

This function generates the GrFN structure for a variable definition, of the form: variable: {

name: source_refs: gensym: domain: domain_constraints: }

Parameters:
  • variables (list) – List of variables.

  • reference (str) – Either array’s indexing variable (i.e. i

  • class (for array[i]) or derived type object's referencing) –

  • variable (member) –

Returns:

list – Generated GrFN.

get_domain_dictionary(variable, state)[source]
generate_function_name(function_type, variable, arr_index)[source]

This function generates the name of the function inside the container wiring within the body of a container.

load_updated(grfn_dict)[source]

This function parses through the GrFN once and finds the container spec of functions whose updated fields needs to be filled in that functions’ function call spec.

get_derived_type_attributes(node, state)[source]

This function retrieves the derived type attributes from the ast and return the updated last definition dict and populated attribute list

static replace_multiple(main_string, to_be_replaced, new_string)[source]

Replace a set of multiple sub strings with a new string in main string.

check_io_variables(variable_name)[source]

This function scans the variable and checks if it is an io variable. It returns the status of this check i.e. True or False.

get_variables(condition_sources, state)[source]
get_path(file_name: str, instance: str)[source]

This function returns the path of a file starting from the root of the delphi repository. The returned path varies depending on whether it is for a namespace or a source variable, which is denoted by the instance argument variable. It is important to note that the path refers to that of the original system being analyzed i.e. the Fortran code and not the intermediate Python file which is used to generate the AST.

dump_ast(node, annotate_fields=True, include_attributes=False, indent='  ')[source]

Return a formatted dump of the tree in node. This is mainly useful for debugging purposes. The returned string will show the names and the values for fields. This makes the code impossible to evaluate, so if evaluation is wanted annotate_fields must be set to False. Attributes such as line numbers and column offsets are not dumped by default. If this is wanted, include_attributes can be set to True.

process_comments(source_comment_dict, generator_object)[source]

This function replaces the keys in the source comments that are function names in the source files into their container id name.

create_grfn_dict(lambda_file: str, asts: List, file_name: str, mode_mapper_dict: list, original_file: str, mod_log_file_path: str, module_file_exist=False, module_import_paths={}) Dict[source]

Create a Python dict representing the GrFN, with additional metadata for JSON output.

generate_ast(filename: str)[source]

This function generates the AST of a python file using Python’s ast module.

get_asts_from_files(file_list: List[str], printast=False) List[source]

This function returns the AST of each python file in the python_file_list.

get_system_name(pyfile_list: List[str])[source]

This function returns the name of the system under analysis. Generally, the system is the one which is not prefixed by m_ (which represents modules).

generate_system_def(python_list: List[str], module_grfn_list: List[str], import_grfn_paths: List[str], module_logs: Dict, original_file_path: str)[source]

This function generates the system definition for the system under analysis and writes this to the main system file.

process_files(python_list: List[str], grfn_tail: str, lambda_tail: str, original_file_path: str, print_ast_flag=False)[source]

This function takes in the list of python files to convert into GrFN and generates each file’s AST along with starting the GrFN generation process.

get_original_file_name(original_file_path)[source]

delphi.translators.for2py.get_comments module

Purpose:

Read the Fortran source file specified and return subprogram names together with the associated subprogram-level comments.

Author:

Saumya Debray

Example

Command-line invocation::

./get_comments.py <src_file_name>

Programmatic invocation::

comments = get_comments(src_file_name)

The returned value is a dictionary that maps each subprogram name to a comment dictionary; the comment dictionary maps each of the following categories to a list of comment strings:

– ‘head’ : whole-line comments just before the subprogram start; – ‘neck’ : whole-line comments just after the subprogram start; – ‘foot’ : whole-line comments just after the subprogram ends; and – ‘internal’ : comments internal to the function (curently marked by “marker statements” of the form i_g_n_o_r_e__m_e___NNN = .True. where NNN is the line number of the comment. Internal comments are maintained as a dictionary that maps the variables i_g_n_o_r_e__m_e___NNN to lists of comment strings.

If a subprogram does not have comments for any of these categories, the corresponding entry in the comment dictionary is [].

In addition to the subprogram-level comments mentioned above, the returned dictionary also has entries for two “file-level” comments:

– any comment at the beginning of the file (before the first function) can be accessed using the key “$file_head” (this comment is also the head-comment for the first subprogram in the file); and – any comment at the end of the file (after the last function) can be accessed using the key “$file_foot” (this comment is also the foot-comment for the last subprogram in the file).

If either the file-head or the file-foot comment is missing, the corresponding entries in the comment dictionary are [].

get_comments(src_file_name: str)[source]
init_comment_map(head_cmt, neck_cmt, foot_cmt, internal_cmt)[source]
print_comments(comments)[source]

delphi.translators.for2py.loop_handle module

File: loop_handle.py

Purpose: Read the Fortran AST (obtained from rectify.py) and refactor it

to remove the breaks, returns and continues from within loop statements.

Called from: translate.py Calls: None

Author: Pratik Bhandari

class ContinueState[source]

Bases: object

class BreakState[source]

Bases: object

class RefactorConstructs[source]

Bases: object

This class defines the refactor state of the intermediate AST while removing the breaks and returns inside while loops

refactor(ast: Dict, loop_constructs) Dict[source]
search_cycles(body, cycle_state)[source]
tag_cycle(body, state)[source]
static append_cycle(body, state)[source]
search_breaks(body, break_state)[source]
tag_break(body, if_depth, state)[source]
append_break(body, if_depth, state)[source]
search_while(body)[source]
modify_shifted()[source]
search_tags(item)[source]
start_while(while_body)[source]
parse_args()[source]

delphi.translators.for2py.math_ext module

apply_op(num, op)[source]
cos(num)[source]
sin(num)[source]
tan(num)[source]
acos(num)[source]
exp(num)[source]
log(num)[source]
sqrt(num)[source]

delphi.translators.for2py.measure-coverage module

delphi.translators.for2py.mod_index_generator module

This module contains code to generate a module index file for a set of Fortran files that the program analysis pipeline runs over. The file describes each module used in a program run. The information about each module is represented as a JSON dictionary and has the following fields:

name: <module_name> file: <file_containing_the_module> module: <list_of_used_modules> symbol_export: <list_of_symbols_exported_by_module> subprogram_list: <procedure_mapping_for_module>

The procedure mapping for each subprogram p defined in module M is a mapping from each possible tuple of argument types for p to the function to invoke for that argument type tuple.

Author: Pratik Bhandari

class ModuleGenerator[source]

Bases: object

populate_symbols()[source]

This function populates the dictionary self.symbols which stores the set of symbols declared in each module. This is the union of all public variables, private variables and subprograms for each module.

populate_exports()[source]

This function populates the self.exports dictionary which holds the set of symbols exported by each module. The set of exported symbols is given by: (imports U symbols) - private

populate_imports(module_logs)[source]

This function populates the self.imports dictionary which holds all the private variables defined in each module.

populate_symbol_types()[source]
parse_tree(root, module_logs) bool[source]

This function parses the XML tree of a Fortran file and tracks and maps relevant object relationships

analyze(tree: xml.etree.ElementTree.ElementTree, mod_log_path: str) List[source]

Parse the XML file from the root and keep track of all important data structures and object relationships between files.

get_index(xml_file: str, module_log_file_path: str)[source]

Get the root of the XML ast, instantiate the moduleGenerator and start the analysis process.

delphi.translators.for2py.preprocessor module

This module implements functions to preprocess Fortran source files prior to parsing to fix up some constructs (such as continuation lines) that are problematic for the OpenFortranParser front end.

Author:

Saumya Debray

separate_trailing_comments(lines: List[str]) List[Tuple[int, str]][source]

Given a list of Fortran source code linesseparate_trailing_comments() removes partial-line comments and returns the resulting list of lines.

merge_continued_lines(lines, f_ext)[source]

Given a list of Fortran source code lines, merge_continued_lines() merges sequences of lines that are indicated to be continuation lines and returns the resulting list of source lines. The argument f_ext gives the file extension of the input file: this determines whether we have fixed-form or free-form syntax, which determines how continuation lines are written.

discard_comments(lines)[source]
split_trailing_comment(line: str) str[source]

Takes a line and splits it into two parts (code_part, comment_part) where code_part is the line up to but not including any trailing comment (the ‘!’ comment character and subsequent characters to the end of the line), while comment_part is the trailing comment. :param line: A line of Fortran source code.

Returns:

A pair (code_part, comment_part) where comment_part is the trailing comment. If the line does not contain any trailing comment, then comment_part is None.

path_to_target(infile, target)[source]
process_includes(lines, infile)[source]

process_includes() processes INCLUDE statements, which behave like the #include preprocessor directive in C.

refactor_select_case(lines)[source]

Search for lines that are CASE statements and refactor their structure such that they are always in a i:j form. This means any CASE statement that is in the form <:3> will be <Inf:3>. This is done so that the FortranOFP recognizes the <:3> and <3:> structures properly.

implicit_array_decl_parameters(line)[source]

If line contains an implicit array declaration, extract and return the following parameters: the initial indentation, the type of the array, and the rest of the line after the type; otherwise return None.

fix_implicit_array_decls(lines)[source]
preprocess_lines(lines, infile, forModLogGen=False)[source]
get_preprocessed_lines_from_file(infile, forModLogGen=False)[source]

delphi.translators.for2py.pyTranslate module

This module contains code to convert a Fortran AST representation into a Python script having the same functionalities and performing the same operations as the original Fortran file.

class PrintState(sep='\n', add='    ', printFirst=True, callSource=None, definedVars=[], globalVars=[], functionScope='', indexRef=True)[source]

Bases: object

copy(sep=None, add=None, printFirst=None, callSource=None, definedVars=None, globalVars=None, functionScope=None, indexRef=None)[source]
class PythonCodeGenerator[source]

Bases: object

printSubroutine(node: Dict[str, str], printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

prints Fortran subroutine in python function syntax

printFunction(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

prints Fortran function in python function syntax

printModule(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

prints the module syntax

printProgram(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

prints Fortran program in Python function syntax by calling printSubroutine function

proc_intrinsic(node)[source]

Processes calls to intrinsic functions and returns a string that is the corresponding Python code.

get_arg_list(node)[source]

Get_arg_list() returns the list of arguments or subscripts at a node. If there are no arguments or subscripts, it returns the empty list.

proc_call(node)[source]

Processes function calls, including calls to intrinsics, and returns a string that is the corresponding Python code. This code assumes that proc_expr() has used type info to correctly identify array references, and that proc_call() is therefore correctly called only on function calls.

proc_print(arg_strs)[source]
proc_literal(node)[source]

Processes a literal value and returns a string that is the corresponding Python code.

proc_ref(node, wrapper)[source]

Processes a reference node and returns a string that is the corresponding Python code. The argument “wrapper” indicates whether or not the Python expression should refer to the list wrapper for (scalar) variables.

proc_op(node)[source]

Processes expressions involving operators and returns a string that is the corresponding Python code.

proc_expr(node, wrapper)[source]

Processes an expression node and returns a string that is the corresponding Python code. The argument “wrapper” indicates whether or not the Python expression should refer to the list wrapper for (scalar) variables.

printCall(node: Dict[str, str], printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printAst(root, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printPrivate(node, prinState)[source]
printArg(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printDo(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printDoWhile(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printIndex(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printIf(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printOp(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printLiteral(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printRef(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printAssignment(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printUse(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printFuncReturn(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printExit(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printReturn(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printOpen(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printRead(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printWrite(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printFormat(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printClose(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printVariable(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printArray(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

Prints out the array declaration in a format of Array class object declaration. ‘arrayName = Array(Type, [bounds])’

printDerivedType(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
printSave(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

This function adds the Python string to handle Fortran SAVE statements. It adds a decorator above a function definition and makes a call to static_save function with the list of saved variables as its argument.

printContinue(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

This function simply adds python statement “continue” that is equivalent to “cycle” in Fortran.

printSelect(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

This function converts the select-case statement in Fortran into an if-else statement block in Python

printCase(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

This function handles each CASE statement block. This relates to one if-block in Python

printInterface(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]

This function definition is simply a place holder for INTERFACE just in case of any possible usage in the future. For now, it does nothing and pass. Since translate.py also passes interface, this should not be encountered in any case.

initializeFileVars(node, printState: delphi.translators.for2py.pyTranslate.PrintState)[source]
nameMapping(ast)[source]
get_python_source()[source]
get_range(node)[source]

This function will construct the range string in ‘loBound, Upbound’ format and return to the called function

get_bound(bounds, retrieved_bounds)[source]

This function will fill the bounds list with appropriately retrieved bounds from the node.

get_derived_type_ref(node, numPartRef, is_assignment)[source]

This function forms a derived type reference and return to the caller

get_type(node)[source]

This function checks the type of a variable and returns the appropriate Python syntax type name.

set_default_var_type(var_name, var_type)[source]

This function sets the default variable type of the declared variable.

get_array_dimension(node)[source]

This function is for extracting the dimensions’ range information from the AST. This function is needed for handling a multi-dimensional array(s).

index_modules(root) Dict[source]

Counts the number of modules in the Fortran file including the program file. Each module is written out into a separate Python file.

get_python_sources_and_variable_map(outputDict: Dict)[source]
parse_args()[source]

delphi.translators.for2py.rectify module

The purpose of this module is to do all the clean up for translate.py. This (rectify.py) module contains functions that receive OFP generated XML as an input. Then, the functions removes any unnecessary elements and refactor randomly structured (nested) elements into a correct structure. The output file will be approximately 30%~40% lighter in terms of number of lines than the OFP XML.

Author: Terrence J. Lim

class RectifiedXMLGenerator[source]

Bases: object

file_child_tags = ['program', 'subroutine', 'module', 'declaration', 'function', 'prefix']
program_child_tags = ['header', 'body']
statement_child_tags = ['assignment', 'write', 'format', 'stop', 'execution-part', 'print', 'open', 'read', 'close', 'call', 'statement', 'label', 'literal', 'continue-stmt', 'do-term-action-stmt', 'return', 'contains-stmt', 'declaration', 'prefix', 'function', 'internal-subprogram', 'internal-subprogram-part', 'prefix', 'exit', 'cycle']
loop_child_tags = ['header', 'body', 'format']
specification_child_tags = ['declaration', 'use']
declaration_child_tags = ['type', 'dimensions', 'variables', 'format', 'name', 'type-declaration-stmt', 'prefix-spec', 'save-stmt', 'saved-entity', 'access-spec', 'attr-spec', 'access-stmt', 'access-id-list', 'constants', 'interface', 'subroutine', 'intent', 'names', 'procedure-stmt', 'literal', 'values']
value_child_tags = ['literal', 'operation', 'name']
derived_type_child_tags = ['declaration-type-spec', 'type-param-or-comp-def-stmt-list', 'component-decl-list__begin', 'component-initialization', 'data-component-def-stmt', 'component-def-stmt', 'component-attr-spec-list', 'component-attr-spec-list__begin', 'explicit-shape-spec-list__begin', 'explicit-shape-spec', 'explicit-shape-spec-list', 'component-attr-spec', 'component-attr-spec-list__begin', 'component-shape-spec-list__begin', 'explicit-shape-spec-list__begin', 'explicit-shape-spec', 'component-attr-spec', 'component-attr-spec-list', 'end-type-stmt', 'derived-type-def']
header_child_tags = ['index-variable', 'operation', 'arguments', 'names', 'name', 'loop-control', 'label', 'literal', 'equiv-operand__equiv-op', 'subroutine-stmt', 'value-ranges']
body_child_tags = ['specification', 'statement', 'loop', 'if', 'label', 'stop', 'do-term-action-stmt', 'select', 'case']
operand_child_tags = ['name', 'literal', 'operation']
subscripts_child_tags = ['name', 'literal', 'operation', 'argument', 'range']
index_range_child_tags = ['lower-bound', 'upper-bound', 'step']
bound_child_tags = ['literal', 'name', 'operation']
module_child_tags = ['header', 'body', 'module-stmt', 'members', 'end-module-stmt', 'contains-stmt']
members_child_tags = ['subroutine', 'module-subprogram', 'module-subprogram-part', 'declaration', 'prefix', 'function']
only_child_tags = ['name', 'only', 'only-list']
select_child_tags = ['header', 'body']
case_child_tags = ['header', 'body']
unnecessary_tags = ['do-variable', 'end-program-stmt', 'main-program', 'char-selector', 'declaration-type-spec', 'type-param-or-comp-def-stmt-list', 'component-decl-list__begin', 'data-component-def-stmt', 'component-def-stmt', 'component-initialization', 'attr-spec', 'attr-id', 'designator', 'int-literal-constant', 'char-literal-constant', 'real-literal-constant', 'io-control-spec', 'array-spec-element', 'print-stmt', 'print-format', 'keyword-argument', 'end-subroutine-stmt', 'logical-literal-constant', 'equiv-op', 'equiv-operand', 'saved-entity-list__begin', 'saved-entity-list', 'access-id', 'parameter-stmt', 'type-param-value', 'char-selector', 'interface-block', 'interface-stmt', 'interface-body', 'interface-specification', 'end-interface-stmt', 'select-case-stmt', 'case-selector', 'case-stmt', 'end-select-stmt', 'component-attr-spec-list__begin', 'explicit-shape-spec-list__begin', 'explicit-shape-spec', 'explicit-shape-spec-list', 'component-attr-spec', 'component-attr-spec-list', 'sequence-stmt', 'private-or-sequence', 'data-stmt-set', 'data-stmt', 'signed-real-literal-constant', 'signed-int-literal-constant', 'data-stmt-constant', 'data-i-do-object-list__begin']
output_child_tags = ['name', 'literal', 'operation']
dtype_var_declaration_tags = ['component-decl', 'component-decl-list', 'component-decl-list__begin']
variable_child_tags = ['initial-value', 'length', 'dimensions']
handle_tag_file(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the file elements.

In order to control new sub-element creation under the current element, if child.tag == “__tag_name__” has been added. If any new tag(s) that is not being handled currently, appears in the future, add child.tag == “__tag_name__” at the end of the last condition. This applies all other handler functions.

<file> </file>

handle_tag_program(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the program elements.

<program> </program>

handle_tag_header(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the header elements.

<header> </header>

handle_tag_body(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the body elements.

<body> </body>

handle_tag_specification(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the specification elements.

<specification> </specification>

handle_tag_declaration(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the declaration elements.

<declaration> </declaration>

handle_tag_type(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the variables elements.

<type> </type>

handle_tag_variables(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the variables elements.

<variables> </variables>

handle_tag_variable(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the variables elements.

<variable> </variable>

handle_tag_constants(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the constants elements.

<constants> </constants>

handle_tag_constant(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the constants elements.

<constant> </constant>

handle_tag_statement(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the statement elements.

<statement> </statement>

handle_tag_assignment(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the assignment elements.

<assignment> </assignment>

handle_tag_target(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the target elements.

<target> </target>

handle_tag_names(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the names elements.

<names> <names>

handle_tag_name(root, current, parent, _, traverse)[source]
This function handles cleaning up the XML elements between

the name elements.

<name> <name>

There are three different types of names that the type attribute can

hold:
  1. variable - Simple (single) variable or an array

  2. procedure - Function (or procedure) call

  3. ambiguous - None of the above two type

handle_tag_value(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the value elements.

<value> </value>

handle_tag_literal(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the literal elements.

<literal> </literal>

handle_tag_dimensions(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the dimensions elements.

<dimensions> </dimensions>

handle_tag_dimension(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the dimension elements.

<dimension> </dimension>

handle_tag_loop(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the do loop elements.

<loop> </loop>

handle_tag_index_variable_or_range(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the index_variable or range elements.

<index_variable> or <range> </index_variable> </range>

handle_tag_bound(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the upper_bound elements.

<upper_bound> </upper_bound>

handle_tag_subscripts(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the subscripts elements.

<subscripts> </subscripts>

handle_tag_subscript(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the subscript elements.

<subscript> </subscript>

handle_tag_operation(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the operation elements.

<operation> </operation>

handle_tag_operand(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the operation elements.

<operand> </operand>

handle_tag_write(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the operation elements.

<operand> </operand>

handle_tag_io_controls(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the io-controls elements.

<io-controls> </io-controls>

handle_tag_io_control(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the io-control elements.

<io-control> </io-control>

handle_tag_outputs(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the outputs elements.

<outputs> </outputs>

handle_tag_output(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the output elements.

<output> </output>

handle_tag_format(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the format elements.

<format> </format>

handle_tag_format_items(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the format_items and its sub-elements

<format_items> ____<format_item> ____</format_item> </format_items>

handle_tag_print(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the print tags.

<print> </print>

handle_tag_open(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the open elements.

<open> </open>

handle_tag_keyword_arguments(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the keyword-arguments and keyword-argument elements.

<keyword-arguments> ____<keyword-argument> ____</keyword-argument> </keyword-arguments>

handle_tag_read(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the read elements.

<read> </read>

handle_tag_inputs(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the inputs and input elements.

<inputs> ____<input> ____</input> </inputs>

handle_tag_close(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the close elements.

<close> </close>

handle_tag_call(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the call elements.

<call> </call>

handle_tag_subroutine(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the subroutine elements.

<subroutine> </subroutine>

handle_tag_arguments(root, current, _, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the arguments.

<arguments> </arguments>

handle_tag_argument(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the argument.

<argument> </argument>

handle_tag_if(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the if elements.

<if> </if>

handle_tag_stop(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the stop elements

<stop> </stop>

handle_tag_step(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the step elements.

<step> </step>

handle_tag_return(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the return and return-stmt elements. However, since ‘return-stmt’ is an empty elements with no sub-elements, the function will not keep the elements, but move the attribute to its parent elements, return.

<return> </return>

handle_tag_function(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the function elements.

<function> </function>

handle_tag_use(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the use elements.

<use> </use>

handle_tag_module(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the module elements.

<module> </module>

handle_tag_initial_value(root, current, parent, _, traverse)[source]

This function handles cleaning up the XML elements between the initial-value elements.

<initial-value> </initial-value>

handle_tag_members(root, current, parent, grandparnet, traverse)[source]

This function handles cleaning up the XML elements between the members elements.

<members> or <member> </members> </member>

handle_tag_only(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the only elements.

<only> </only>

handle_tag_length(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the length elements.

<length> </length>

handle_tag_interface(root, current, parent, grandparent, traverse)[source]

This function handles rectifying the elements between interface tag.

<interface> </interface>

handle_tag_select(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the select elements.

<select> </select>

handle_tag_case(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the case elements.

<case> </case>

handle_tag_value_ranges(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the value-ranges elements.

<value-ranges> </value-ranges>

handle_tag_value_range(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the value-range elements.

<value-range> </value-range>

handle_tag_values(root, current, parent, grandparent, traverse)[source]

This function handles cleaning up the XML elements between the values elements.

<values> </values>

parseXMLTree(root, current, parent, grandparent, traverse)[source]

Recursively traverse through the nested XML AST tree and calls appropriate tag handler, which will generate a cleaned version of XML tree for translate.py. Any new tags handlers must be added under this this function.

parseXMLTree

Parameters:
  • root – The current root of the tree.

  • current – Current element.

  • parent – Parent element of the current.

  • grandparent – A parent of parent statement of current.

  • traverse – Keeps the track of number of traverse time.

Returns:

None

reconstruct_derived_type_declaration()[source]

This function reconstructs the derived type with the collected derived type declaration elements in the handle_tag_declaration and handle_tag_type.

Parameters:

None.

Returns:

None.

reconstruct_derived_type_ref(current)[source]

This function reconstruct the id into x.y.k form from the messy looking id. One thing to notice is that this new form was generated in the python syntax, so it is a pre-process for translate.py and even pyTranslate.py that

Parameters:

( (current) – obj: ‘ET’): Current element object.

Returns:

None.

reconstruct_format(grandparent, traverse)[source]

This function is for reconstructing the <format> under the <statement> element.

The OFP XML nests formats under:
  1. statement

  2. declaration

  3. loop

tags, which are wrong except one that is declared under the statement. Therefore, those formats declared under (2) and (3) will be extracted and reconstructed to be nested under (1) in this function.

Parameters:
  • ( (grandparent) – obj: ‘ET’): Grand parent element object.

  • traverse (int) – Current traverse number.

Returns:

None.

reconstruct_derived_type_names(current)[source]

This function reconstructs derived type reference syntax tree. However, this functions is actually a preprocessor for the real final reconstruction.

Parameters:

( (current) – obj: ‘ET’): Current element object.

Returns:

None.

reconstruct_name_element(cur_elem, current)[source]

This function performs a final reconstruction of derived type name element that was preprocessed by ‘reconstruct_derived_type_names’ function. This function traverses the preprocessed name element (including sub-elements) and split & store <name> and <subscripts> into separate lists. Then, it comibines and reconstructs two lists appropriately.

Parameters:
  • ( (current) – obj: ‘ET’): Newly generated element

  • object. (for current element) –

  • ( – obj: ‘ET’): Current element object.

Returns:

None.

reconstruct_goto_after_label(parent, traverse, reconstruct_target)[source]

This function gets called when goto appears after the corresponding label and all necessary statements are collected for the reconstruction.

Parameters:
  • ( (parent) – obj: ‘ET’): A parent ET object that current

  • under. (element will be nested) –

  • header (list) – A header tht holds conditional header.

  • traverse (int) – A traverse counter.

  • reconstruct_target (dict) – A dictionary that holds statements

  • counter. (for goto and label as well as the number of goto) –

Returns:

None.

reconstruct_goto_before_label(parent, traverse, reconstruct_target)[source]

This function gets called when goto appears before the corresponding label and all necessary statements are collected for the reconstruction.

Parameters:
  • ( (parent) – obj: ‘ET’): A parent ET object that current

  • under. (element will be nested) –

  • traverse (int) – A traverse counter.

  • reconstruct_target (dict) – A dictionary that holds statements

  • counter. (for goto and label as well as the number of goto) –

Returns:

None.

reconstruct_header(temp_elem_holder, parent)[source]

This function is for reconstructing the oddly generated header AST to have an uniform structure with other multiary type operation nested headers.

Parameters:
  • temp_elem_holder (list) – A temporary holder that

  • swap. (holds statements under header for) – parent (:obj: ‘ET’): A parent ET object that current

Returns:

None.

goto_boundary_corrector(reconstruct_target, stmts_follow_goto, stmts_follow_label)[source]

This function is for correcting the boundaries of goto statements in case of a multiple gotos are nested and crossing each other.

Parameters:
  • reconstruct_target (dict) – A dictionary that holds statements

  • counter. (for goto and label as well as the number of goto) –

  • stmts_follow_goto (list) – A list that holds statements

  • statement. (after goto) –

  • stmts_follow_label (list) – A list that holds statements

  • statements. (after label) –

Returns:

None.

reconstruct_stmts_follow_goto_after_case(header, parent, stmts_follow_goto, next_goto, traverse, reconstructed_goto_elem, index)[source]

This function generates a new if statement to nests statements that follow goto-stmt based on condition or non-condition status to eliminate goto.

Parameters:
  • next_goto (list) – A list to hold next goto-stmt that may exist

  • goto. (within the boundary of currently handling) –

  • reconstructed_goto_elem (list) – A list that will hold

  • statements. (reconstructed if) –

  • header (list) – A header tht holds conditional header.

  • ( (parent) – obj: ‘ET’): A parent ET object that current

  • under. (element will be nested) –

  • stmts_follow_goto (list) – A list that holds statements

  • goto.

  • traverse (int) – A current traverse counter.

  • index (int) – An index of goto.

Returns:

None.

handle_in_outward_movement(stmts_follow_goto, stmts_follow_label, parent)[source]

This function checks the lexical level of goto and label. Then, generate and add (remove) statements to the statement holders, so they can be handled appropriately.

Parameters:
  • stmts_follow_goto (list) – It holds all the statements

  • original (that appeared after the label statement in the) –

  • code.

  • stmts_follow_label (list) – It holds all the statements

  • original

  • code.

  • ( (parent) – obj: ‘ET’): A parent ET object that current

  • under. (element will be nested) –

Returns:

None.

reconstruct_next_goto(next_goto, reconstructed_goto_elem, parent)[source]

This function reconstruct a goto statement that appears after the currently handling goto case. The default case is that the next goto is a backward goto case, which requires reconstruction by reconstruct_goto_before function. Thus, this function prepares the ingredient for it.

Parameters:
  • next_goto (list) – Holds statement and goto-stmt elements.

  • reconstructed_goto_elem (list) – Holds reconstructed if statements

  • goto. (that was generated after eliminating the) –

  • header (list) – A header tht holds conditional header.

Returns:

None.

check_conditional_goto(header, stmts_follow_goto)[source]

This function checks whether the goto is conditional or unconditional. If it’s conditional, it extracts conditional operation (header).

Parameters:
  • header (list) – A header tht holds conditional header.

  • stmts_follow_goto (list) – It holds all the statements

  • original (that appeared after the goto statement in the) –

  • code.

Returns:

None.

reconstruct_stmts_follow_label_after_case(stmts_follow_label, next_goto, reconstructed_goto_elem, header, traverse, parent, index)[source]

This function generates a new statements to nest statements that follow label based on condition or non-condition status to eliminate goto.

Parameters:
  • next_goto (list) – A list to hold next goto-stmt that may exist

  • goto. (follow label statement for currently handling) –

  • reconstructed_goto_elem (list) – A list that will hold

  • statements. (reconstructed if) –

  • header (list) – A header tht holds conditional header.

  • ( (parent) – obj: ‘ET’): A parent ET object that current

  • under. (element will be nested) –

  • stmts_follow_label (list) – A list that holds statements

  • goto.

  • traverse (int) – A current traverse counter.

  • index (int) – An index of goto.

Returns:

None.

restruct_declaration(elem_declaration, parent)[source]

This function is to restructure declaration to have an uniform xml structure.

generate_element(current_elem, parent_elem)[source]

This function is to traverse the existing xml and generate a new copy to the given parent element - This is a recursive function.

generate_declaration_element(parent, default_name, number_of_gotos, declared_flag_num, traverse)[source]

A flag declaration and assignment xml generation. This will generate N number of label_flag_i or goto_i, where N is the number of gotos in the Fortran code and i is the number assigned to the flag

Parameters:
  • ( (parent) – obj: ‘ET’): Parent element object.

  • default_name (str) – A default name given for

  • variable. (new) –

  • number_of_gotos (int) – A number of gotos. Amount

  • number. (of variables will be generated based on this) –

  • declared_flag_num (list) – A list to hold the number

  • varaibles (of delcared) –

  • traverse (int) – A current traverse counter.

Returns:

None.

generate_assignment_element(parent, name_id, condition, value_type, value, traverse)[source]

This is a function for generating new assignment element xml for goto reconstruction.

Parameters:
  • ( (parent) – obj: ‘ET’): Parent element object.

  • name_id (str) – Name of a target variable.

  • value_type (str) – Type of value that will be assigned.

  • traverse (int) – A current traverse counter.

Returns:

None.

generate_operation_element(parent, op_type, operator, name)[source]

This is a function for generating new operation element and its nested subelements with the passes arguments.

Currently, it generates only a unary operation syntax only. It may require update in the future.

Parameters:
  • ( (parent) – obj: ‘ET’): Parent element object.

  • op_type (str) – Operation type.

  • operator (str) – Operator.

  • name (str) – Name of a variable for new element.

Returns:

None.

generate_name_element(parent, hasSubscripts, name_id, is_array, numPartRef, name_type)[source]

This is a function for generating new name element based on the provided arguments.

Parameters:
  • ( (parent) – obj: ‘ET’): Parent element object.

  • hasSubscripts (str) – “true” or “false” status in string.

  • name_id (str) – Name of a variable.

  • numPartRef (str) – Number of references.

  • type (str) – Type of a variable.

Returns:

None.

generate_if_element(header, parent, stored_stmts, next_goto, need_operation, op_type, lhs, rhs, operator, traverse, reconstructed_goto_elem)[source]

This is a function generating new if element. Since header can hold unary, multiary, or name, some arguments may be passed with None. Check them to generate an appropriate XML.

Parameters:
  • ( (parent) – obj: ‘ET’): Header element from if.

  • ( – obj: ‘ET’): Parent element object.

  • stored_stmts (list) – List of statements.

  • next_goto (list) – Another gotos appear while

  • stmt. (handling current goto) –

  • need_operation (bool) – Boolean to state whether

  • header. (new if needs operation) –

  • op_type (str) – Operation type.

  • lhs (str) – Left hand side variabel name.

  • rhs (str) – Right hand side variabel name.

  • operator (str) – Operator.

  • traverse (int) – Current traverse counter.

  • reconstructed_goto_elem (list) – A list to

  • elimination. (hold reconstructed AST after goto) –

Returns:

None.

clean_derived_type_ref(current)[source]

This function will clean up the derived type referencing syntax, which is stored in a form of “id=’x’%y” in the id attribute. Once the id gets cleaned, it will call the reconstruc_derived_type_ref function to reconstruct and replace the messy version of id with the cleaned version.

Parameters:

( (current) – obj: ‘ET’): Current element object.

Returns:

None.

clean_id(unrefined_id)[source]

This function refines id (or value) with quotation marks included by removing them and returns only the variable name. For example, from “OUTPUT” to OUTPUT and “x” to x.

Thus, the id name will be modified as below:

Unrefined id - id = “”OUTPUT”” Refined id - id = “OUTPUT”

Parameters:
  • unrefined_id (str) – Id of name element that holds

  • strings. (unnecessary) –

Returns:

None

clean_attrib(current)[source]

The original XML elements holds ‘eos’ and ‘rule’ attributes that are not necessary and being used. Thus, this function will remove them in the rectified version of XML.

Parameters:

( (current) – obj: ‘ET’): Current element object.

Returns:

None.

boundary_identifier()[source]

This function will be called to identify the boundary for each goto-and-label. The definition of scope here is that whether one goto-label is nested under another goto-label.

For example:

<label with lbl = 111> ____<goto-stmt with lbl = 222> ____<label with lbl = 222> <goto-stmt with lbl = 111>

In this case, “goto-label with lbl = 222” is within the scope of “lbl = 111” Thus, the elements will be assigned with “parent-goto” attribute with 111.

Parameters:

None.

Returns:

None.

update_function_arguments(current)[source]

This function handles function definition’s arguments with array status based on the information that was observed during the function call

Parameters:

( (current) – obj: ‘ET’): Current node (either call or value)

Returns:

None.

update_call_argument_type(current, update, scope, arguments_info)[source]

This function updates call statement function argument xml with variable type.

replace_interface_function_to_target(current, arguments_info)[source]

This function will check whether replacing function name is needed or not. That is if the Fortran source code has module with interface and does dynamic dispatching to functions.

case_availability(boundary)[source]

This function checks for the goto cases in the code based on the boundary. If any unhandled case encountered, then it will assert and halt the program.

Parameters:
  • boundary (dict) – A dictonary of goto label

  • label. (and boundary) –

Returns:

None.

parent_goto_assigner(boundary, boundary_for_label, statements_to_reconstruct)[source]

This function actually assigns boundary(s) to each goto and label statements.

Parameters:
  • boundary (list) – A list of boundaries.

  • boundary_for_label (dict) – A dictionary of

  • label. (label as a key and its parent boundary) –

  • statements_to_reconstruct (list) – A list of

  • reconstruction. (statements that require) –

Returns:

None.

remove_dup_stmt(stmts_follow_label)[source]

This removes the statement that’s a child statement of if body being seprately re-added to the list.

Parameters:
  • ( (stmts_follow_label) – obj: ‘ET’): A list that holds

  • for (statements appeard under the label-statement) –

  • reconstruction.

Returns:

None.

boundary_identifier_for_backward_goto(stmts_follow_label, goto_index_holder, number_of_gotos, target_label_lbl)[source]

This function identifies the boundary from label to goto. Remove any statements that are not within the boundary. Then, store those removed statements seprately for later restoration.

Parameters:
  • stmts_follow_label (list) – A list holding the

  • label-statement (statements that appear after the) –

  • reconstruction. (for) –

  • goto_index_holder (list) – A list of index of goto

  • stmts_follow_label. (in the) –

  • number_of_gotos (int) – Number of gotos in the

  • stmts_follow_label.

  • target_label_lbl (list) – A list that should

  • value. (only hold one value of label-stmt's label) –

Returns:

(list) – A list of statements that requires restoration after loop generation.

multiple_goto_identifier(goto_index_holder, multiple_goto_stmts, stmts_follow_label)[source]

This function identifies any additional goto statements may appear within the boundary of currently handling backward goto case.

Parameters:
  • stmts_follow_label (list) – A list holding the

  • label-statement (statements that appear after the) –

  • reconstruction. (for) –

  • goto_index_holder (list) – A list of index of goto

  • stmts_follow_label. (in the) –

  • multiple_goto_stmts (list) – A list that will hold

  • current (additional gotos within the boundary of) –

  • goto.

Returns:

None.

nested_forward_goto_identifier(multiple_goto_stmts)[source]

This function identifies any existing forward goto case nested under the backward goto case.

Parameters:
  • multiple_goto_stmts (list) – A list that will hold

  • current (additional gotos within the boundary of) –

  • goto.

  • index_boundary (list) – A list that will hold

  • <goto-stmt>. (the indices of label of <label> and) –

Returns:

(bool) – A boolean status indicating whether the nested forward goto exists within the boundary.

construct_goto_loop(parent, reconstruct_target, nested_gotos_exist, multiple_goto_stmts, number_of_gotos, declared_label_flag_num, traverse, target_label_lbl)[source]

This function constructs loop syntax tree for goto backward case.

Parameters:
  • ( (parent) – obj: ‘ET’): Parent element of loop.

  • reconstruct_target (dict) – A dictionary that

  • statement. (will hold nested goto) –

  • nested_gotos_exist (bool) – Boolean to indicating

  • not. (whether nested goto exists or) –

  • multiple_goto_stmts (list) – A list of goto and other

  • statements.

  • number_of_gotos (int) – Number of gotos to reconstruct.

  • declared_label_flag_num (list) – List of flag numbers.

  • traverse (int) – Current traverse counter.

  • target_label_lbl (list) – A single value list that

  • <label>. (holds the label value of) –

Returns:

None.

nested_goto_handler(reconstruct_target, statements, body_elem, traverse)[source]

This function collects forward goto case related statements under the backward goto boundary. Then, it calls goto_after function to reconstruct goto.

Parameters:
  • reconstruct_target (list) – A list that holds

  • reconstruction. (statements for) –

  • ( (body_elem) – obj: ‘ET’): Statements for

  • reconstructions.

  • ( – obj: ‘ET’): Body element of

  • loop. (the) –

  • traverse (int) – Current traverse counter.

statement_recovery(statements_to_recover, parent, traverse)[source]

This function is for recovering any existing statements that follow reconstructed loop.

Parameters:
  • statements_to_recover (list) – A list of statements.

  • ( (parent) – obj: ‘ET’): A prent element.

  • traverse (int) – Current traverse counter.

is_empty(elem)[source]

This function is just a helper function for check whether the passed elements (i.e. list) is empty or not

Parameters:

( (elem) – obj:): Any structured data object (i.e. list).

Returns:

bool – True if element is empty or false if not.

indent(elem, level=0)[source]

This function indents each level of XML.

Source:

https://stackoverflow.com/questions/3095434/inserting-newlines -in-xml-file-generated-via-xml-etree-elementstree-in-python

Parameters:
  • ( (elem) – obj: ‘ET’): An XML root.

  • level (int) – A root level in integer.

Returns:

None.

buildNewASTfromXMLString(xmlString: str, original_fortran_file: str, module_log_file_path: str) Tuple[xml.etree.ElementTree.Element, List][source]

This function processes OFP generated XML and generates a rectified version by recursively calling the appropriate functions.

Parameters:
  • xmlString (str) – XML as a string

  • original_fortran_file (str) – Path to the original Fortran file

  • module_log_file_path (str) – Path to the module_log_file

Returns:

ET object – A reconstructed element object.

parse_args()[source]

This function parse the arguments passed to the script. It returns a tuple of (input ofp xml, output xml) file names.

Parameters:

None.

Returns:

None.

fileChecker(filename, mode)[source]

This function checks for the validity (file existence and mode). If either the file does not exist or the mode is not valid, throws an IO exception and terminates the program

Parameters:
  • filename (str) – A file name that reconstructed XMl

  • to. (will be written) –

  • mode (str) – Mode to open the file in.

Returns:

None.

delphi.translators.for2py.runner module

main()[source]
attempt_parse(fpath)[source]

delphi.translators.for2py.static_save module

File:

static_save.py

Purpose:

A decorator function to initialize a list of static variables to a None.

Usage:

In the python file, add the following line above the function definition which is to be decorated.

@static_vars([<variable_list]) def target_function():

where,

<variable_list> = List of static variables within the function “target_function”

static_vars(var_list)[source]

delphi.translators.for2py.strings module

File: strings.py Purpose: Code implementing string objects (corresponding to the Fortran

CHARACTER type) in the code generated by for2py.

Usage: see the document “for2py: Miscellaneous constructs”

class String(length=0, value='')[source]

Bases: object

value(obj)[source]
padding(n)[source]

padding() returns a string of blanks of length = sef._length - n.

set_(strval)[source]
adjustl()[source]

adjustl() implements the ADJUSTL() function of Fortran. This function removes leading blanks and adds blanks on the right so that the result is the same length as the input string.

adjustr()[source]

adjustr() implements the ADJUSTR() function of Fortran. This function removes trailing blanks and adds blanks on the left so that the result is the same length as the input string.

f_index() implements the string search function of Fortran's INDEX()[source]

function; we use the name f_index to emphasize that the behavior of Fortran’s INDEX() is slightly different from that of Python’s index(). f_index() returns the position within a string where substring first occurs; 0 if there is no such occurrence. If the argument direction contains “back” the string is searched backwards starting from the end.

len_trim()[source]
repeat(n)[source]
trim()[source]
get_substr(i, j)[source]

get_substr(i, j) returns the substring of the given string beginning at position i (start position = 1) and ending at position j.

set_substr(i, j, other)[source]

delphi.translators.for2py.syntax module

Various utility routines for processing Fortran syntax.

line_is_comment(line: str) bool[source]

From FORTRAN Language Reference (https://docs.oracle.com/cd/E19957-01/805-4939/z40007332024/index.html)

A line with a c, C, ‘*’, d, D, or ! in column one is a comment line, except that if the -xld option is set, then the lines starting with D or d are compiled as debug lines. The d, D, and ! are nonstandard.

If you put an exclamation mark (!) in any column of the statement field, except within character literals, then everything after the ! on that line is a comment.

A totally blank line is a comment line (we ignore this here).

Parameters:

line

Returns:

True iff line is a comment, False otherwise.

line_is_continuation(line: str, f_ext: str) bool[source]
From FORTRAN 77 Language Reference

(https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vn6l/index.html)

A statement takes one or more lines; the first line is called the initial line; the subsequent lines are called the continuation lines. You can format a source line in either of two ways: Standard fixed format, or Tab format.

In Standard Fixed Format, continuation lines are identified by a nonblank, nonzero in column 6.

Tab-Format source lines are defined as follows: A tab in any of columns 1 through 6, or an ampersand in column 1, establishes the line as a tab-format source line. If the tab is the first nonblank character, the text following the tab is scanned as if it started in column 7. Continuation lines are identified by an ampersand (&) in column 1, or a nonzero digit after the first tab.

Parameters:

line

Returns:

True iff line is a continuation line, else False. Currently this is used only for fixed-form input files, i.e., f_ext in (‘.f’, ‘.for’)

line_is_continued(line: str) bool[source]
Parameters:

line

Returns:

True iff line is continued on the next line. This is a Fortran-90 feature and indicated by a ‘&’ at the end of the line.

line_starts_subpgm(line: str) Tuple[bool, Optional[str]][source]

Indicates whether a line in the program is the first line of a subprogram definition.

Parameters:

line

Returns:

(True, f_name) if line begins a definition for subprogram f_name; (False, None) if line does not begin a subprogram definition.

line_is_pgm_unit_start(line)[source]
line_is_pgm_unit_end(line)[source]
line_is_pgm_unit_separator(line)[source]
program_unit_name(line: str) str[source]

Given a line that starts a program unit, i.e., a program, module, subprogram, or function, this function returns the name associated with that program unit.

line_ends_subpgm(line: str) bool[source]
Parameters:

line

Returns:

True if line is the last line of a subprogram definition, else False.

line_is_executable(line: str) bool[source]

line_is_executable() returns True iff the line can start an executable statement in a program.

line_is_include(line: str) str[source]

line_is_include() : if the argument is an INCLUDE statement, returns the argument to the INCLUDE statement (a file name); otherwise returns None.

line_has_implicit_array(line: str) Tuple[bool, Optional[str]][source]

This function checks for the implicit array declaration (i.e. real myArray(10)) without DIMENSION keyword.

Parameters:

line

Returns:

(True, var) if the line holds implicit array declaration; (False, None) if the line does not hold implicit array declaration

has_subroutine(lines: str) bool[source]

This function searches end of subroutine syntax from the passed string of lines to find out whether any subroutines are presented in lines or not.

Parameters:

lines

Returns:

(True) if subroutine end syntax presented in lines. (False) if subroutine end syntax does not present in lines.

has_module(lines: str) bool[source]

This function searches end of module syntax from the passed string of lines to find out whether any modules are presented in lines or not.

Parameters:

lines

Returns:

(True) if module end syntax presented in lines. (False) if module end syntax does not present in lines.

line_is_func_start(line: str) bool[source]

This function checks whether the line indicates a function entry or not.

Parameters:

line.

Returns:

(True) if the line is beginning of function definition. (False) if the line is not a beginning of function dedfinition.

variable_declaration(line: str) Tuple[bool, Optional[list]][source]

Indicates whether a line in the program is a variable declaration or not.

Parameters:

line

Returns:

(True, v_type, list) if line has one of variable declaration syntax; (False, None, None) if line does not have any variable declaration syntax .

subroutine_definition(line: str) Tuple[bool, Optional[list]][source]

Indicates whether a line in the program is the first line of a subroutine definition and extracts subroutine name and the arguments.

Parameters:

line

Returns:

(True, list) if line begins a definition for subroutine; (False, None) if line does not begin a subroutine definition.

pgm_end(line: str) Tuple[bool, str, str][source]

Indicates whether a line is the last line of a program.

Parameters:

line

Returns:

(True, pgm, name) if line is the last line of a definition either for interface or module; (False, None, None) if line is not the last line of either interface or module.

line_starts_pgm(line: str) Tuple[bool, Optional[str], Optional[str]][source]

Indicates whether a line is the first line of either interface or module.

Parameters:

line

Returns:

(True, pgm, namee) if line begins a definition either for interface or module; (False, None, None) if line is not a beginning of either interface or module.

delphi.translators.for2py.translate module

This script converts the XML version of AST of the Fortran file into a JSON representation of the AST along with other non-source code information. The output is a pickled file which contains this information in a parsable data structure.

Example

This script is executed by the autoTranslate script as one of the steps in converted a Fortran source file to Python file. For standalone execution::

python translate.py -f <ast_file> -g <pickle_file> -i <f_src_file>

where f_src_file is the Fortran source file for ast_file.

ast_file: The XML representation of the AST of the Fortran file. This is produced by the OpenFortranParser.

pickle_file: The file which will contain the pickled version of JSON AST and supporting information.

class ParseState(subroutine=None)[source]

Bases: object

This class defines the state of the XML tree parsing at any given root. For any level of the tree, it stores the subroutine under which it resides along with the subroutines arguments.

copy(subroutine=None)[source]
class XML_to_JSON_translator[source]

Bases: object

process_subroutine_or_program_module(root, state)[source]

This function should be the very first function to be called

process_call(root, state) List[Dict][source]

This function handles <call> tag and its subelement <name>.

process_argument(root, state) List[Dict][source]

This function handles <argument> tag. It simply create a new AST list and copy the values (tag and attributes) to it.

process_declaration(root, state) List[Dict][source]

This function handles <declaration> tag and its sub-elements by recursively calling the appropriate functions for the target tag.

process_type(root, state) List[Dict][source]

This function handles <type> declaration.

There may be two different cases of <type>.
  1. Simple variable type declaration

  2. Derived type declaration

process_length(root, state) List[Dict][source]

This function handles <length> tag.

process_variables(root, state) List[Dict][source]

This function handles <variables> element, which its duty is to call <variable> tag processor.

process_variable(root, state) List[Dict][source]

This function will get called from the process_variables function, and it will construct the variable AST list, then return it back to the called function.

process_constants(root, state) List[Dict][source]

This function handles <constants> element, which its duty is to call <constant> tag processor

process_constant(root, state) List[Dict][source]

This function will get called from the process_constants function, and it will construct the constant AST list, then return it back to the called function.

process_derived_types(root, state) List[Dict][source]

This function handles <derived-types> tag nested in the <type> tag. Depends on the nested sub-elements of the tag, it will recursively call other tag processors.

  1. Main type declaration

  2. Single variable declaration (with initial values)

  3. Array declaration

process_loop(root, state) List[Dict][source]

This function handles <loop type=””> tag. The type attribute indicates the current loop is either “do” or “do-while” loop.

process_index_variable(root, state) List[Dict][source]

This function handles <index-variable> tag. This tag represents index ranges of loops or arrays.

process_if(root, state) List[Dict][source]

This function handles <if> tag. Else and else if are nested under this tag.

process_operation(root, state) List[Dict][source]

This function handles <operation> tag. The nested elements should either be “operand” or “operator”.

process_literal(root, _) List[Dict][source]

This function handles <literal> tag

process_io_control(root, state) List[Dict][source]

This function checks for an asterisk in the argument of a read/write statement and stores it if found. An asterisk in the first argument specifies a input through or output to console. An asterisk in the second argument specifies a read/write without a format (implicit read/writes).

process_name(root, state) List[Dict][source]

This function handles <name> tag. The name tag will be added to the new AST for the pyTranslate.py with “ref” tag.

process_argument_types(root, _) List[Dict][source]

This function handles <argument-types> tag that only appears under the interface function names. It will extract the argument types and add to the list, then return the list

process_assignment(root, state) List[Dict][source]

This function handles <assignment> tag that nested elements of <target> and <value>.

process_function(root, state) List[Dict][source]

This function handles <function> tag.

process_dimension(root, state) List[Dict][source]

This function handles <dimension> tag. This is a tag that holds information about the array, such as the range and values.

process_range(root, state) List[Dict][source]

This function handles <range> tag.

process_keyword_argument(root, state) List[Dict][source]

This function handles <keyword-argument> tag.

process_libRtn(root, state) List[Dict][source]
process_direct_map(root, state) List[Dict][source]

Handles tags that are mapped directly from xml to IR with no additional processing other than recursive translation of any child nodes.

process_terminal(root, _) List[Dict][source]

Handles tags that terminate the computation of a program unit, namely, “return”, “stop”, and “exit”

process_format(root, state) List[Dict][source]

This function handles <format> tag.

process_format_item(root, _) List[Dict][source]

This function handles <format-item> tag.

process_use(root, _) List[Dict][source]

This function adds the tag for use statements In case of “USE .. ONLY ..” statements, the symbols to be included are stored in the “include” field of the “use” block

process_private_variable(root, _) List[Dict][source]

This function adds the tag for private symbols. Any variable/function being initialized as private is added in this tag.

process_save(root, _) List[Dict][source]

This function parses the XML tag for the Fortran save statement and adds the tag that holds the function under which SAVE has been defined along with the variables that are saved by this statement.

process_continue(root, _) List[Dict][source]

This function handles cycle (continue in Python) tag.

process_select(root, state) List[Dict][source]

This function handles select statements tag.

process_case(root, state) List[Dict][source]

This function handles the CASE statement in Fortran. This should be modeled as an if-else statement in languages like Python

process_value_range(root, state) List[Dict][source]

This function handles the range of values inside CASE statements

process_interface(root, state) List[Dict][source]

This function definition is simply a place holder for INTERFACE just in case of any possible usage in the future. For now, it does nothing when translate.py encountrs interface tag in the rectified xml.

handle_data_statements(root, state)[source]

This function handles the data statements that occurs in the declaration tag

create_1d_array_ast(root, assign_ast, state)[source]

This function creates the do-while loop ast which assigns values to a one-dimensional array according to the data statement operation

create_2d_array_ast(root, assign_ast, dimension, state)[source]

This function creates the do-while loop ast which assigns values to a two-dimensional array according to the data statement operation

check_function_call(value)[source]

This function checks whether there is a function call in the value of an assignment. If there is one, remove the function call into a separate assignment

initiate_function_replacement(function_tag)[source]
replace_function_call(tag, function_name)[source]
parseTree(root, state: delphi.translators.for2py.translate.ParseState) List[Dict][source]

Parses the XML ast tree recursively to generate a JSON AST which can be ingested by other scripts to generate Python scripts.

Parameters:
  • root – The current root of the tree.

  • state – The current state of the tree defined by an object of the ParseState class.

Returns:

ast – A JSON ast that defines the structure of the Fortran file.

loadFunction(root)[source]

Loads a list with all the functions in the Fortran File

Parameters:

root – The root of the XML ast tree.

Returns:

None

Does not return anything but populates two lists (self.functionList and self.subroutineList) that contains all the functions and subroutines in the Fortran File respectively.

analyze(trees: List[xml.etree.ElementTree.ElementTree]) Dict[source]
print_unhandled_tags()[source]
get_trees(files: List[str]) List[xml.etree.ElementTree.ElementTree][source]
xml_to_py(trees)[source]
parse_args()[source]

Parse the arguments passed to the script. Returns a tuple (fortran_file, pickle_file, args) where fortran_file is the file containing the input Fortran code, and pickle_file is the output pickle file.

gen_pickle_file(output_dictionary, pickle_filename)[source]

delphi.translators.for2py.types_ext module

This module implements several arithmetic and logical operations such that it maintains single-precision across variables and operations. It overloads double-precision floating variables into single-precision using numpy’s float32 method, forcing them to stay in single-precision type. This avoids a possible butterfly effect in numerical operations involving large floating point calculations.

Usage:

To convert a float variable into Float32 type, do the following target = Float32(float_variable) OR target = Float32(5.0) where <target> is now a Float32 object.

Now, any arithmetic/logical operation involving <float_variable> will involve single-point calculations.

Example Usage:

eps = Float32(1.0) while eps + 1.0 > 1.0: ____eps /= 2.0 eps *= 2.0

Authors:

Saumya Debray Pratik Bhandari

class Float32(val)[source]

Bases: numbers.Real

This class converts float variables into float32 type for single-precision calculation and overloads the default arithmetic and logical operations. All methods below follow a similar input/output type.

Input: Either a Float32 variable or a variable of a supported type. Output: A Float32 object of the result of the operation.

Module contents

exception For2PyError(message, status_code=None, payload=None)[source]

Bases: Exception

status_code = 400
to_dict()[source]