a
    <b
                     @   s`   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZ G dd deZdd Zd	S )
a  
This module implements the functionality to take any Python expression as a
string and fix all numbers and other things before evaluating it,
thus

1/2

returns

Integer(1)/Integer(2)

We use the ast module for this. It is well documented at docs.python.org.

Some tips to understand how this works: use dump() to get a nice
representation of any node. Then write a string of what you want to get,
e.g. "Integer(1)", parse it, dump it and you'll see that you need to do
"Call(Name('Integer', Load()), [node], [], None, None)". You do not need
to bother with lineno and col_offset, just call fix_missing_locations()
before returning the node.
    )Basic)SympifyError)parseNodeTransformerCallNameLoadfix_missing_locationsStrTuplec                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
	Transformc                 C   s   t |  || _|| _d S )N)r   __init__
local_dictglobal_dict)selfr   r    r   h/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/parsing/ast_parser.pyr      s    
zTransform.__init__c                 C   sT   t |jtr(tttdt |gg dS t |jtrPtttdt |gg dS |S )NZIntegerfuncargskeywordsZFloat)
isinstancenintr	   r   r   r   float)r   noder   r   r   	visit_Num#   s    zTransform.visit_Numc                 C   st   |j | jv r|S |j | jv rD| j|j  }t|ttfs>t|rR|S n|j dv rR|S ttt	dt
 t|j gg dS )N)TrueFalseZSymbolr   )idr   r   r   r   typecallabler	   r   r   r   r
   )r   r   Zname_objr   r   r   
visit_Name,   s    
zTransform.visit_Namec                    sL    fdd|j j D } |j}ttdt t|t |gg d}t|S )Nc                    s   g | ]}  |qS r   )visit).0argr   r   r   
<listcomp>:       z*Transform.visit_Lambda.<locals>.<listcomp>Lambdar   )r   r#   bodyr   r   r   r   r	   )r   r   r   r*   r   r   r&   r   visit_Lambda9   s    zTransform.visit_LambdaN)__name__
__module____qualname__r   r   r"   r+   r   r   r   r   r      s   	r   c                 C   sn   i }t d| zt|  dd}W n" tyD   tdt|  Y n0 t|||}t|dd}t	|||S )z
    Converts the string "s" to a SymPy expression, in local_dict.

    It converts all numbers to Integers before feeding it to Python and
    automatically creates Symbols.
    zfrom sympy import *eval)modezCannot parse %s.z<string>)
execr   stripSyntaxErrorr   reprr   r#   compiler/   )sr   r   aer   r   r   
parse_expr@   s    
r9   N)__doc__Zsympy.core.basicr   Zsympy.core.sympifyr   astr   r   r   r   r   r	   r
   r   r   r9   r   r   r   r   <module>   s
   ($