B
    [
                 @   s|   d Z ddlmZmZ ddlm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 Python ast module for that, which is in python2.6 and later. 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 don't need
to bother with lineno and col_offset, just call fix_missing_locations()
before returning the node.
    )print_functiondivision)Basic)exec_)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   7lib/python3.7/site-packages/sympy/parsing/ast_parser.pyr   "   s    
zTransform.__init__c             C   sX   t |jtr*tttdt |gg d d S t |jtrTtttdt |gg d d S |S )NZIntegerZFloat)
isinstancenintr   r	   r
   r   float)r   noder   r   r   	visit_Num'   s    zTransform.visit_Numc             C   sv   |j | jkr|S |j | jkrD| j|j  }t|ttfs>t|rR|S n|j dkrR|S ttt	dt
 t|j gg d d S )N)TrueFalseZSymbol)idr   r   r   r   typecallabler   r	   r
   r   r   )r   r   Zname_objr   r   r   
visit_Name0   s    
zTransform.visit_Namec                sN    fdd|j j D } |j}ttdt t|t |gg d d }t|S )Nc                s   g | ]}  |qS r   )visit).0arg)r   r   r   
<listcomp>>   s    z*Transform.visit_Lambda.<locals>.<listcomp>ZLambda)argsr"   bodyr	   r
   r   r   r   )r   r   r&   r'   r   r   )r   r   visit_Lambda=   s
    zTransform.visit_LambdaN)__name__
__module____qualname__r   r   r!   r(   r   r   r   r   r       s   	r   c             C   sp   i }t d| yt|  dd}W n$ tk
rF   tdt|  Y nX 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>)
r   r   stripSyntaxErrorr   reprr   r"   compiler,   )sr   r   aer   r   r   
parse_exprD   s    
r5   N)__doc__Z
__future__r   r   Zsympy.core.basicr   Zsympy.core.compatibilityr   Zsympy.core.sympifyr   Zastr   r   r	   r
   r   r   r   r   r   r5   r   r   r   r   <module>   s   ($