
    sg,                     j   d dl mZ  ed      Zerd dlmZmZmZmZmZm	Z	m
Z
 d dlmZmZmZmZ d dlmZ ej$                  Zej$                  j$                  Zej(                  j*                  Zej,                  j.                  j.                  Z	  G d dej0                        Zn
 G d d      Zd	 Zd
 Zy)    )import_modulelfortran)VariableIntBaseTypeFloatBaseTypeStringReturnFunctionDefinition
Assignment)AddMulIntegerFloat)Symbolc                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)ASR2PyVisitorz
        Visitor Class for LFortran ASR

        It is a Visitor class derived from asr.ASRVisitor which visits all the
        nodes of the LFortran ASR and creates corresponding AST node for each
        ASR node

        c                     g | _         y)zInitialize the ParserN_py_astselfs    W/var/www/html/venv/lib/python3.12/site-packages/sympy/parsing/fortran/fortran_parser.py__init__zASR2PyVisitor.__init__D   s	    DL    c                     |j                   j                  D ],  }|j                   j                  |   }| j                  |       . |j                  D ]  }| j                  |        y)zx
            Function to visit all the elements of the Translation Unit
            created by LFortran ASR
            N)global_scopesymbolsvisititems)r   nodessymitems        r   visit_TranslationUnitz#ASR2PyVisitor.visit_TranslationUnitH   sa    
 &&..  ''//2

3  

 !

4 !r   c                    t        |j                  t        j                        r|j                  }|j                  }t        |t        j                        r3t        t        |j                        t        |j                              }ndt        |      t        j                  k(  r2t        |      }|D ]!  }t        t        |j                        |      }# nt        d      t        d      | j                  j                         y)a  Visitor Function for Assignment

            Visits each Assignment is the LFortran ASR and creates corresponding
            assignment for SymPy.

            Notes
            =====

            The function currently only supports variable assignment and binary
            operation assignments of varying multitudes. Any type of numberS or
            array is not supported.

            Raises
            ======

            NotImplementedError() when called for Numeric assignments or Arrays

            z!Numeric assignments not supportedzArrays not supportedN)
isinstancetargetasrr   valuer   nametypeBinOpcall_visitorNotImplementedErrorr   append)r   r    r'   r)   new_nodeexp_astexprs          r   visit_AssignmentzASR2PyVisitor.visit_AssignmentS   s    ( $++s||4

eS\\2)$$*KK" %$)JJ"	 H 5kSYY.*51G ' #- ( 5 $$ ..QRR)*@AALL)r   c                 x   |j                   }|j                  }|j                  }t        |      t        j
                  k(  rt        |j                        }n<t        |      t        j                  k(  rt        |      }|D ]  }|} nt        d      t        |      t        j
                  k(  rt        |j                        }n<t        |      t        j                  k(  rt        |      }	|	D ]  }|} nt        d      t        |t        j                        rt              }
nxt        |t        j                        rt               }
nPt        |t        j                        rt        dz        }
n&t        |t        j                        rt              }
| j                   j#                  
       y)a  Visitor Function for Binary Operations

            Visits each binary operation present in the LFortran ASR like addition,
            subtraction, multiplication, division and creates the corresponding
            operation node in SymPy's AST

            In case of more than one binary operations, the function calls the
            call_visitor() function on the child nodes of the binary operations
            recursively until all the operations have been processed.

            Notes
            =====

            The function currently only supports binary operations with Variables
            or other binary operations. Numerics are not supported as of yet.

            Raises
            ======

            NotImplementedError() when called for Numeric assignments

            zNumbers Currently not supported   N)opleftrightr+   r(   r   r   r*   r,   r-   r.   r&   r   SubDivr   r   r/   )r   r    r6   lhsrhs
left_value	l_exp_astexpright_value	r_exp_astr0   s              r   visit_BinOpzASR2PyVisitor.visit_BinOp   sS   0 B))C**CS	S\\)#CHH-
ccii'(-	$ %C!$J% **KLLS	S\\)$SXX.ccii'(-	$ &C"%K& **KLL"cgg&z;7B(zK<8B(z1[=9B(z;7LL)r   c                    t        |j                  t        j                        r t	        t        d            }t        d      }nOt        |j                  t        j                        r t        t        d            }t        d      }nt        d      |j                  dk(  sBt        |j                        j                  ||      }| j                  j                  |       yy)	a  Visitor Function for Variable Declaration

            Visits each variable declaration present in the ASR and creates a
            Symbol declaration for each variable

            Notes
            =====

            The functions currently only support declaration of integer and
            real variables. Other data types are still under development.

            Raises
            ======

            NotImplementedError() when called for unsupported data types

            integerr   realg        Data type not supportedin)r+   r)   N)r&   r+   r(   r   r   r   Realr   r   r.   intentr   r*   as_Declarationr   r/   )r   r    var_typer)   r0   s        r   visit_VariablezASR2PyVisitor.visit_Variable   s    $ $))S[[1&vi'89
DIIsxx0(8c
)*CDDKK4'#II .#! !   ##H- (r   c                 `    |,|D ]&  }| j                   j                  t        |             ( yy)zVisitor Function for code sequence

            Visits a code sequence/ block and calls the visitor function on all the
            children of the code block to create corresponding code in python

            N)r   r/   r-   )r   seqr    s      r   visit_SequencezASR2PyVisitor.visit_Sequence   s4      <DLL''T(:;< r   c                      y)zVisitor Function for Numbers in ASR

            This function is currently under development and will be updated
            with improvements in the LFortran ASR

            N )r   r    s     r   	visit_NumzASR2PyVisitor.visit_Num   s     r   c                    |j                   D cg c]  }t        |j                         }}g }|j                  }|j                  D ]  }t	        |      } 	 }|j                  j                  D ]<  }	t	        |j                  j                  |	         }
|
D ]  }|j                  |        > |D ]  }|j                  |        |j                  t        t        |j                  j                                     t        |j                  j                  t        j                        rt        t!        d            }nNt        |j                  j                  t        j"                        rt%        t!        d            }nt'        d      t)        ||||      }| j*                  j                  |       yc c}w # t
        $ r g }Y sw xY w)a  Visitor Function for function Definitions

            Visits each function definition present in the ASR and creates a
            function definition node in the Python AST with all the elements of the
            given function

            The functions declare all the variables required as SymPy symbols in
            the function before the function definition

            This function also the call_visior_function to parse the contents of
            the function body

            rD   rE   rF   )return_typer*   
parametersbodyN)argsr   r*   rV   r-   UnboundLocalErrorsymtabr   r/   r	   
return_varr&   r+   r(   r   r   r   rH   r   r.   r
   r   )r   r    arg_iterfn_argsfn_bodyfn_nameifn_astfn_body_exprr"   declr   elemret_typer0   s                  r   visit_FunctionzASR2PyVisitor.visit_Function   s    @DyyI8x.IGIGiiGYY )%a)"% {{** ,#DKK$7$7$<=# ,GNN7+,, % %t$%NN,, $//..<&vi'89DOO00#((;(8)*CDD)&.&%,&	H LL)E J % "!"s   F:F? ?GGc                     | j                   S )zReturns the AST nodesr   r   s    r   ret_astzASR2PyVisitor.ret_ast&  s    <<r   N)__name__
__module____qualname____doc__r   r$   r3   rB   rL   rO   rR   re   rg   rQ   r   r   r   r   :   s8    				!+	*Z7	*r"	.H		<		1	*f	 r   r   c                       e Zd Zd Zy)r   c                     t        d      )Nzlfortran not available)ImportError)r   rW   kwargss      r   r   zASR2PyVisitor.__init__+  s    677r   N)rh   ri   rj   r   rQ   r   r   r   r   *  s    	8r   c                 \    t               }|j                  |        |j                         }|S )a  Calls the AST Visitor on the Module

    This function is used to call the AST visitor for a program or module
    It imports all the required modules and calls the visit() function
    on the given node

    Parameters
    ==========

    fort_node : LFortran ASR object
        Node for the operation for which the NodeVisitor is called

    Returns
    =======

    res_ast : list
        list of SymPy AST Nodes

    )r   r   rg   )	fort_nodevres_asts      r   r-   r-   .  s'    ( 	AGGIiikGNr   c                 L    t        | d      }t        |      }t        |      }|S )a$  Wrapper function to convert the given Fortran source code to SymPy Expressions

    Parameters
    ==========

    src : string
        A string with the Fortran source code

    Returns
    =======

    py_src : string
        A string with the Python source code compatible with SymPy

    F)translation_unit)
src_to_ast
ast_to_asrr-   )srca_astapy_srcs       r   src_to_sympyr|   H  s(      sU3E5A!_FMr   N)sympy.externalr   r   sympy.codegen.astr   r   r   r   r	   r
   r   
sympy.corer   r   r   r   sympy.core.symbolr   r(   asr_modastrv   semanticrw   
ASTVisitorr   r-   r|   rQ   r   r   <module>r      s    ($K K K33(llG
,,

C((J""--88J'Tn  n `8 84r   