
    sh,                     t   S SK Jr  \" S5      r\(       a  S SKJrJrJrJrJrJ	r	J
r
  S SKJrJrJrJr  S SKJr  \R$                  r\R$                  R$                  r\R(                  R*                  r\R,                  R.                  R.                  r  " S S\R0                  5      rO
 " S S5      rS	 rS
 rg)    )import_modulelfortran)VariableIntBaseTypeFloatBaseTypeStringReturnFunctionDefinition
Assignment)AddMulIntegerFloat)Symbolc                   N    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rSrg)ASR2PyVisitor:   z
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                     / U l         g)zInitialize the ParserN_py_astselfs    x/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/sympy/parsing/fortran/fortran_parser.py__init__ASR2PyVisitor.__init__D   s	    DL    c                     UR                   R                   H-  nUR                   R                  U   nU R                  U5        M/     UR                   H  nU R                  U5        M     g)zT
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_TranslationUnit#ASR2PyVisitor.visit_TranslationUnitH   sW    
 &&..''//2

3 / 



4  #r   c                 .   [        UR                  [        R                  5      (       a  UR                  nUR                  n[        U[        R                  5      (       a3  [        [        UR                  5      [        UR                  5      5      nOg[        U5      [        R                  :X  a4  [        U5      nU H"  n[        [        UR                  5      U5      nM$     O[        S5      e[        S5      eU R                  R                  W5        g)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_AssignmentASR2PyVisitor.visit_AssignmentS   s    ( $++s||44

eS\\22)$$*KK" %$)JJ"	 H 5kSYY.*51G '#- ( 5 $$ !( ..QRR)*@AALL)r   c                    UR                   nUR                  nUR                  n[        U5      [        R
                  :X  a  [        UR                  5      nO?[        U5      [        R                  :X  a  [        U5      nU H  nUnM     O[        S5      e[        U5      [        R
                  :X  a  [        UR                  5      nO?[        U5      [        R                  :X  a  [        U5      n	U	 H  nUnM     O[        S5      e[        U[        R                  5      (       a  [        WW5      n
O[        U[        R                  5      (       a  [        WW* 5      n
OZ[        U[        R                  5      (       a  [        WSW-  5      n
O+[        U[        R                  5      (       a  [        WW5      n
U R                   R#                  W
5        g)au  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/   r0   r1   r)   r   SubDivr   r   r2   )r   r"   r:   lhsrhs
left_value	l_exp_astexpright_value	r_exp_astr3   s              r   visit_BinOpASR2PyVisitor.visit_BinOp   sU   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                    [        UR                  [        R                  5      (       a   [	        [        S5      5      n[        S5      nOT[        UR                  [        R                  5      (       a   [        [        S5      5      n[        S5      nO[        S5      eUR                  S:X  d?  [        UR                  5      R                  UUS9nU R                  R                  U5        gg)	al  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   r1   intentr   r-   as_Declarationr   r2   )r   r"   var_typer,   r3   s        r   visit_VariableASR2PyVisitor.visit_Variable   s    $ $))S[[11&vi'89
DIIsxx00(8c
)*CDDKK4'#II .#! !   ##H- (r   c                 f    Ub.  U H'  nU R                   R                  [        U5      5        M)     gg)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   r2   r0   )r   seqr"   s      r   visit_SequenceASR2PyVisitor.visit_Sequence   s/     DLL''T(:;   r   c                     g)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_NumASR2PyVisitor.visit_Num   s     r   c                    UR                    Vs/ s H  n[        UR                  5      PM     nn/ nUR                  nUR                   H  n[	        U5      nM      WnUR                  R                   H?  n	[	        UR                  R                  U	   5      n
U
 H  nUR                  U5        M     MA     U H  nUR                  U5        M     UR                  [        [        UR                  R                  5      5      5        [        UR                  R                  [        R                  5      (       a  [        [!        S5      5      nOS[        UR                  R                  [        R"                  5      (       a  [%        [!        S5      5      nO['        S5      e[)        UUUUS9nU R*                  R                  U5        gs  snf ! [
         a    / n GNf = f)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

rI   rJ   rK   )return_typer-   
parametersbodyN)argsr   r-   r^   r0   UnboundLocalErrorsymtabr   r2   r	   
return_varr)   r.   r+   r   r   r   rM   r   r1   r
   r   )r   r"   arg_iterfn_argsfn_bodyfn_nameifn_astfn_body_exprr$   declr   elemret_typer3   s                  r   visit_FunctionASR2PyVisitor.visit_Function   s    @DyyIy8x.yG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   GG G! G!c                     U R                   $ )zReturns the AST nodesr   r   s    r   ret_astASR2PyVisitor.ret_ast&  s    <<r   r   N)__name__
__module____qualname____firstlineno____doc__r   r&   r6   rF   rQ   rU   rY   rm   rp   __static_attributes__rX   r   r   r   r   :   s8    				!+	*Z7	*r"	.H		<		1	*f	 r   r   c                       \ rS rSrS rSrg)r   i*  c                     [        S5      e)Nzlfortran not available)ImportError)r   r_   kwargss      r   r   r   +  s    677r   rX   N)rr   rs   rt   ru   r   rw   rX   r   r   r   r   *  s    	8r   c                 \    [        5       nUR                  U 5        UR                  5       nU$ )au  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    rp   )	fort_nodevres_asts      r   r0   r0   .  s'    ( 	AGGIiikGNr   c                 F    [        U SS9n[        U5      n[        U5      nU$ )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_asrr0   )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astr   semanticr   
ASTVisitorr   r0   r   rX   r   r   <module>r      s    ($K K K33(llG
,,

C((J""--88J'Tn  n `8 84r   