U
    c±³eâ_  ã                   @   sœ   d Z ddlmZmZ ddlmZ ddlT ddlmZ G dd„ de	ƒZ
G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deeƒZG dd„ deeƒZdS )a  
Serializes a Cython code tree to Cython code. This is primarily useful for
debugging and testing purposes.
The output is in a strict format, no whitespace or comments from the input
is preserved (and it could not be as it is not present in the code tree).
é    )Úabsolute_importÚprint_functioné   )ÚTreeVisitor)Ú*)ÚCSimpleBaseTypeNodec                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚLinesResultc                 C   s   g | _ d| _d S ©NÚ )ÚlinesÚs©Úself© r   ú0lib/python3.8/site-packages/Cython/CodeWriter.pyÚ__init__   s    zLinesResult.__init__c                 C   s   |  j |7  _ d S ©N)r   ©r   r   r   r   r   Úput   s    zLinesResult.putc                 C   s   | j  | j¡ d| _d S r	   )r   Úappendr   r   r   r   r   Únewline   s    zLinesResult.newlinec                 C   s   |   |¡ |  ¡  d S r   )r   r   r   r   r   r   Úputline   s    
zLinesResult.putlineN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r      s   r   c                       sd  e Zd ZdZdZdV‡ fdd„	Zdd„ Zdd	„ Zd
d„ ZdWdd„Z	dd„ Z
dd„ ZdXdd„Zdd„ ZdYdd„Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Z d>d?„ Z!d@dA„ Z"dBdC„ Z#dDdE„ Z$dFdG„ Z%dHdI„ Z&dJdK„ Z'dLdM„ Z(dNdO„ Z)dPdQ„ Z*dRdS„ Z+dTdU„ Z,‡  Z-S )ZÚDeclarationWriterzE
    A Cython code writer that is limited to declarations nodes.
    z    Nc                    s8   t t| ƒ ¡  |d krtƒ }|| _d| _i | _d| _d S )Nr   )Úsuperr   r   r   ÚresultÚ
numindentsÚ	tempnamesÚtempblockindex©r   r   ©Ú	__class__r   r   r   '   s    zDeclarationWriter.__init__c                 C   s   |   |¡ | jS r   ©Úvisitr   ©r   Ztreer   r   r   Úwrite0   s    
zDeclarationWriter.writec                 C   s   |  j d7  _ d S ©Nr   ©r   r   r   r   r   Úindent4   s    zDeclarationWriter.indentc                 C   s   |  j d8  _ d S r(   r)   r   r   r   r   Údedent7   s    zDeclarationWriter.dedentr
   c                 C   s   | j  | j| j | ¡ d S r   )r   r   Úindent_stringr   r   r   r   r   Ú	startline:   s    zDeclarationWriter.startlinec                 C   s   | j  |¡ d S r   )r   r   r   r   r   r   r   =   s    zDeclarationWriter.putc                 C   s   | j  | j| j | ¡ d S r   )r   r   r,   r   r   r   r   r   r   @   s    zDeclarationWriter.putlinec                 C   s   | j  |¡ d S r   )r   r   r   r   r   r   ÚendlineC   s    zDeclarationWriter.endlinec                 C   s   |   |¡ |  ¡  d S r   ©r-   r.   r   r   r   r   ÚlineF   s    
zDeclarationWriter.lineFc                 C   s”   t |ƒdkr|d d… D ]<}|  |¡ |rJ|jd k	rJ|  d¡ |  |j¡ |  d¡ q|  |d ¡ |r|d jd k	r|  d¡ |  |d j¡ d S )Nr   éÿÿÿÿú = ú, )Úlenr%   Údefaultr   )r   ÚitemsÚ
output_rhsÚitemr   r   r   Úcomma_separated_listJ   s    


z&DeclarationWriter.comma_separated_listc                 C   s   |   ¡  |  |¡ |  ¡  d S r   )r*   r%   r+   ©r   Únoder   r   r   Ú_visit_indentedW   s    
z!DeclarationWriter._visit_indentedc                 C   s   t d| ƒ‚d S ©Nz"Node not handled by serializer: %r©ÚAssertionErrorr:   r   r   r   Ú
visit_Node\   s    zDeclarationWriter.visit_Nodec                 C   s   |   |¡ d S r   ©Zvisitchildrenr:   r   r   r   Úvisit_ModuleNode_   s    z"DeclarationWriter.visit_ModuleNodec                 C   s   |   |¡ d S r   rA   r:   r   r   r   Úvisit_StatListNodeb   s    z$DeclarationWriter.visit_StatListNodec                 C   s8   |j d krd}n
d|j  }|  d| ¡ |  |j¡ d S )Nr   z"%s"zcdef extern from %s:)Zinclude_filer   r<   Úbody)r   r;   Úfiler   r   r   Úvisit_CDefExternNodee   s
    

z&DeclarationWriter.visit_CDefExternNodec                 C   s   |   d¡ |  |j¡ d S )Nr   ©r   r%   Úbaser:   r   r   r   Úvisit_CPtrDeclaratorNodem   s    
z*DeclarationWriter.visit_CPtrDeclaratorNodec                 C   s   |   d¡ |  |j¡ d S )Nú&rG   r:   r   r   r   Úvisit_CReferenceDeclaratorNodeq   s    
z0DeclarationWriter.visit_CReferenceDeclaratorNodec                 C   s:   |   |j¡ |  d¡ |jd k	r,|   |j¡ |  d¡ d S ©Nú[ú])r%   rH   r   Z	dimensionr:   r   r   r   Úvisit_CArrayDeclaratorNodeu   s
    

z,DeclarationWriter.visit_CArrayDeclaratorNodec                 C   s0   |   |j¡ |  d¡ |  |j¡ |  d¡ d S ©Nú(ú))r%   rH   r   r9   Úargsr.   r:   r   r   r   Úvisit_CFuncDeclaratorNode|   s    
z+DeclarationWriter.visit_CFuncDeclaratorNodec                 C   s   |   |j¡ d S r   ©r   Únamer:   r   r   r   Úvisit_CNameDeclaratorNodeƒ   s    z+DeclarationWriter.visit_CNameDeclaratorNodec                 C   sh   |j rN|  d|j ¡ |jdk r4|  d|j  ¡ n|jdkrN|  d|j ¡ |jd k	rd|  |j¡ d S )N)z	unsigned r
   zsigned r   zshort zlong )Zis_basic_c_typer   ZsignedZlongnessrV   r:   r   r   r   Úvisit_CSimpleBaseTypeNode†   s    


z+DeclarationWriter.visit_CSimpleBaseTypeNodec                 C   s   |   |j¡ |   |j¡ d S r   )r%   Ú	base_typeÚ
declaratorr:   r   r   r   Úvisit_CComplexBaseTypeNode‘   s    z,DeclarationWriter.visit_CComplexBaseTypeNodec                 C   s&   |   |j¡ |  d¡ |  |j¡ d S )NÚ.)r%   rY   r   rV   r:   r   r   r   Úvisit_CNestedBaseTypeNode•   s    
z+DeclarationWriter.visit_CNestedBaseTypeNodec                 C   s8   |   |j¡ |  d¡ |  |j|jj ¡ |  d¡ d S rL   )r%   Zbase_type_noder   r9   Úpositional_argsÚkeyword_argsZkey_value_pairsr:   r   r   r   Úvisit_TemplatedTypeNodeš   s    
z)DeclarationWriter.visit_TemplatedTypeNodec                 C   s<   |   d¡ |  |j¡ |  d¡ | j|jdd |  ¡  d S )Núcdef ú T)r7   )r-   r%   rY   r   r9   Zdeclaratorsr.   r:   r   r   r   Úvisit_CVarDefNode    s
    

z#DeclarationWriter.visit_CVarDefNodec                 C   s   |   |¡ |jr@|  d¡ |  |j¡ |jd k	r@|  d|j ¡ |rN|  |¡ |  d¡ |  ¡  |sp|  d¡ n|D ]}|  |¡ qt|  ¡  d S )Nrb   ú "%s"ú:Úpass)	r-   rV   r   Úcnamer.   r*   r   r%   r+   )r   r;   ÚdeclÚextrasÚ
attributesÚ	attributer   r   r   Ú_visit_container_node§   s    




z'DeclarationWriter._visit_container_nodec                 C   sP   |j rd}nd}|jdkr"|d7 }|jr0|d7 }||j7 }|  ||d |j¡ d S )Nú	ctypedef ra   Zpubliczpublic zpacked )Ztypedef_flagÚ
visibilityZpackedÚkindrl   rj   )r   r;   rh   r   r   r   Úvisit_CStructOrUnionDefNodeº   s    

z-DeclarationWriter.visit_CStructOrUnionDefNodec                 C   sJ   d}|j rdd |j ¡ }|jr4|dd |j¡ 7 }|  |d||j¡ d S )Nr
   z[%s]r3   z(%s)zcdef cppclass)Z	templatesÚjoinZbase_classesrl   rj   )r   r;   ri   r   r   r   Úvisit_CppClassNodeÆ   s    z$DeclarationWriter.visit_CppClassNodec                 C   s   |   |dd |j¡ d S )Nz	cdef enum)rl   r6   r:   r   r   r   Úvisit_CEnumDefNodeÎ   s    z$DeclarationWriter.visit_CEnumDefNodec                 C   sJ   |   |j¡ |jr"|  d|j ¡ |jr>|  d¡ |  |j¡ |  ¡  d S )Nrd   r2   )r-   rV   rg   r   Úvaluer%   r.   r:   r   r   r   Úvisit_CEnumDefItemNodeÑ   s    
z(DeclarationWriter.visit_CEnumDefItemNodec                 C   s˜   |j r
t‚|jr&|jD ]}|  |¡ q|  d¡ |  |j¡ |jr~|  d¡ |jrh|  |j¡ |  d¡ |  |j¡ |  d¡ |  	d¡ |  
|j¡ d S )Nzcdef class rQ   r\   rR   re   )Úmodule_namer?   Z
decoratorsr%   r-   r   Ú
class_nameZbase_class_nameZbase_class_moduler.   r<   rD   )r   r;   Ú	decoratorr   r   r   Úvisit_CClassDefNodeÚ   s    






z%DeclarationWriter.visit_CClassDefNodec                 C   s8   |   d¡ |  |j¡ |  d¡ |  |j¡ |  ¡  d S )Nrm   rb   )r-   r%   rY   r   rZ   r.   r:   r   r   r   Úvisit_CTypeDefNodeë   s
    

z$DeclarationWriter.visit_CTypeDefNodec                 C   s6   |   d|j ¡ |  |j¡ |  d¡ |  |j¡ d S )Nzdef %s(ú):)r-   rV   r9   rS   r.   r<   rD   r:   r   r   r   Úvisit_FuncDefNodeò   s    
z#DeclarationWriter.visit_FuncDefNodec                 C   sÎ   |   |jrdnd¡ |jr6|  d |j¡¡ |  d¡ |jdkrV|  |j¡ |  d¡ |jrf|  d¡ |jrŽ|  |j¡ |jj	d k	rŽ|  d¡ |  |j
j¡ |  d¡ |  |j
j¡ |  d¡ |  |j¡ d S )Núcpdef ra   rb   Úprivateúapi rQ   r{   )r-   ÚoverridableÚ	modifiersr   rq   rn   ÚapirY   r%   rV   rZ   rH   r9   rS   r.   r<   rD   r:   r   r   r   Úvisit_CFuncDefNodeù   s$    






z$DeclarationWriter.visit_CFuncDefNodec                 C   sh   t |jtƒr|jjd k	r8|  |j¡ |j ¡ r8|  d¡ |  |j¡ |jd k	rd|  d¡ |  |j¡ d S )Nrb   r2   )	Ú
isinstancerY   r   rV   r%   rZ   Zdeclared_namer   r5   r:   r   r   r   Úvisit_CArgDeclNode  s    
ÿ



z$DeclarationWriter.visit_CArgDeclNodec                 C   s>   |   d¡ |  |j¡ |jr2|  d¡ |  |j¡ |  ¡  d S )Nzcimport ú as )r-   r   rv   Úas_namer.   r:   r   r   r   Úvisit_CImportStatNode#  s    

z'DeclarationWriter.visit_CImportStatNodec                 C   s†   |   d¡ |  |j¡ |  d¡ d}|jD ]N\}}}}|d ksBt‚|rLd}n
|  d¡ |  |¡ |r*|  d¡ |  |¡ q*|  ¡  d S )Nzfrom z	 cimport TFr3   r†   )r-   r   rv   Zimported_namesr?   r.   )r   r;   ÚfirstÚposrV   r‡   ro   r   r   r   Úvisit_FromCImportStatNode+  s    




z+DeclarationWriter.visit_FromCImportStatNodec                 C   s   |   |j¡ d S r   rU   r:   r   r   r   Úvisit_NameNode<  s    z DeclarationWriter.visit_NameNodec                 C   s"   |   d¡ |  |j¡ |  ¡  d S )Nú@)r-   r%   rx   r.   r:   r   r   r   Úvisit_DecoratorNode?  s    
z%DeclarationWriter.visit_DecoratorNodec                 C   s   |   d¡ |  ¡  d S )Nrf   r/   r:   r   r   r   Úvisit_PassStatNodeD  s    
z$DeclarationWriter.visit_PassStatNode)N)r
   )r
   )F).r   r   r   Ú__doc__r,   r   r'   r*   r+   r-   r   r   r.   r0   r9   r<   r@   rB   rC   rF   rI   rK   rO   rT   rW   rX   r[   r]   r`   rc   rl   rp   rr   rs   ru   ry   rz   r|   rƒ   r…   rˆ   r‹   rŒ   rŽ   r   Ú__classcell__r   r   r"   r   r       sT   	


	r   c                   @   s°   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*S )+ÚStatementWriterzD
    A Cython code writer for most language statement features.
    c                 C   s6   |   ¡  |  |j¡ |  d¡ |  |j¡ |  ¡  d S ©Nr2   )r-   r%   Úlhsr   Úrhsr.   r:   r   r   r   Úvisit_SingleAssignmentNodeN  s
    
z*StatementWriter.visit_SingleAssignmentNodec                 C   s@   |   ¡  |jD ]}|  |¡ |  d¡ q|  |j¡ |  ¡  d S r“   )r-   Zlhs_listr%   r   r•   r.   )r   r;   r”   r   r   r   Úvisit_CascadedAssignmentNodeU  s    

z,StatementWriter.visit_CascadedAssignmentNodec                 C   s4   |   d¡ |  |jj¡ |js(|  d¡ |  ¡  d S )Nzprint ú,)r-   r9   Z	arg_tuplerS   Zappend_newliner   r.   r:   r   r   r   Úvisit_PrintStatNode]  s
    

z#StatementWriter.visit_PrintStatNodec                 C   s€   |   d¡ |jjr"|  |jj¡ n|  |j¡ |  d¡ |  |jj¡ |  	d¡ |  
|j¡ |jd k	r||  d¡ |  
|j¡ d S )Nzfor ú in re   úelse:)r-   ÚtargetZis_sequence_constructorr9   rS   r%   r   ÚiteratorÚsequencer.   r<   rD   Úelse_clauser0   r:   r   r   r   Úvisit_ForInStatNoded  s    




z#StatementWriter.visit_ForInStatNodec                 C   sœ   |   d¡ |  |jd j¡ |  d¡ |  |jd j¡ |jdd … D ]0}|   d¡ |  |j¡ |  d¡ |  |j¡ qF|jd k	r˜|  d¡ |  |j¡ d S )Nzif r   re   r   zelif r›   )	r-   r%   Ú
if_clausesÚ	conditionr.   r<   rD   rŸ   r0   )r   r;   Zclauser   r   r   Úvisit_IfStatNoder  s    





z StatementWriter.visit_IfStatNodec                 C   sP   |   d¡ |  |j¡ |  d¡ |  |j¡ |jd k	rL|  d¡ |  |j¡ d S )Nzwhile re   r›   )r-   r%   r¢   r.   r<   rD   rŸ   r0   r:   r   r   r   Úvisit_WhileStatNode‚  s    



z#StatementWriter.visit_WhileStatNodec                 C   s   |   d¡ d S )NÚcontinue©r0   r:   r   r   r   Úvisit_ContinueStatNode‹  s    z&StatementWriter.visit_ContinueStatNodec                 C   s   |   d¡ d S )NÚbreakr¦   r:   r   r   r   Úvisit_BreakStatNodeŽ  s    z#StatementWriter.visit_BreakStatNodec                 C   s   |   |j¡ d S r   )r9   rS   r:   r   r   r   Úvisit_SequenceNode‘  s    z"StatementWriter.visit_SequenceNodec                 C   s    |   ¡  |  |j¡ |  ¡  d S r   )r-   r%   Úexprr.   r:   r   r   r   Úvisit_ExprStatNode”  s    z"StatementWriter.visit_ExprStatNodec                 C   s<   |   ¡  |  |j¡ |  d|j ¡ |  |j¡ |  ¡  d S )Nz %s= )r-   r%   r”   r   Úoperatorr•   r.   r:   r   r   r   Úvisit_InPlaceAssignmentNode™  s
    z+StatementWriter.visit_InPlaceAssignmentNodec                 C   sX   |   ¡  |  d¡ |  |j¡ |jd k	r>|  d¡ |  |j¡ |  d¡ |  |j¡ d S )Nzwith r†   re   )r-   r   r%   Zmanagerrœ   r.   r<   rD   r:   r   r   r   Úvisit_WithStatNode   s    



z"StatementWriter.visit_WithStatNodec                 C   s0   |   d¡ |  |j¡ |   d¡ |  |j¡ d S )Nútry:zfinally:)r0   r<   rD   Zfinally_clauser:   r   r   r   Úvisit_TryFinallyStatNodeª  s    

z(StatementWriter.visit_TryFinallyStatNodec                 C   sF   |   d¡ |  |j¡ |jD ]}|  |¡ q|jd k	rB|  |j¡ d S )Nr°   )r0   r<   rD   Zexcept_clausesr%   rŸ   )r   r;   Úxr   r   r   Úvisit_TryExceptStatNode°  s    


z'StatementWriter.visit_TryExceptStatNodec                 C   sd   |   d¡ |jd k	r*|  d¡ |  |j¡ |jd k	rJ|  d¡ |  |j¡ |  d¡ |  |j¡ d S )NÚexceptrb   r3   re   )r-   Úpatternr   r%   rœ   r.   r<   rD   r:   r   r   r   Úvisit_ExceptClauseNode¸  s    





z&StatementWriter.visit_ExceptClauseNodec                 C   s6   |   d¡ |jd k	r*|  d¡ |  |j¡ |  ¡  d S )NÚreturnrb   )r-   rt   r   r%   r.   r:   r   r   r   Úvisit_ReturnStatNodeÃ  s
    


z$StatementWriter.visit_ReturnStatNodec                 C   s   |   d¡ d S )NÚraiser¦   r:   r   r   r   Úvisit_ReraiseStatNodeÊ  s    z%StatementWriter.visit_ReraiseStatNodec                 C   s   |   d|jj ¡ d S )Nz(import %s))r   rv   rt   r:   r   r   r   Úvisit_ImportNodeÍ  s    z StatementWriter.visit_ImportNodec                 C   sJ   d}|j D ] }d| j|f | j|< |d7 }q
|  jd7  _|  |j¡ dS )zÌ
        Temporaries are output like $1_1', where the first number is
        an index of the TempsBlockNode and the second number is an index
        of the temporary which that block allocates.
        r   z$%d_%dr   N)Ztempsr    r   r%   rD   )r   r;   ÚidxÚhandler   r   r   Úvisit_TempsBlockNodeÐ  s    

z$StatementWriter.visit_TempsBlockNodec                 C   s   |   | j|j ¡ d S r   )r   r   r½   r:   r   r   r   Úvisit_TempRefNodeÝ  s    z!StatementWriter.visit_TempRefNodeN)r   r   r   r   r–   r—   r™   r    r£   r¤   r§   r©   rª   r¬   r®   r¯   r±   r³   r¶   r¸   rº   r»   r¾   r¿   r   r   r   r   r’   I  s*   	
r’   c                       sÐ  e Zd ZdZdm‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdnd d!„Zd"d#„ Zd$d%„ Zd&d'„ Zdod)d*„Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d5d6d6d6d7œZd8d9d:d:d:d:d:d:d:d:d:d:d;d<d=d>d>d?d?d@d@d@d@d@dAdBœZdCdD„ ZdEdF„ ZdGdH„ Z dIdJ„ Z!dKdL„ Z"dMdN„ Z#dOdP„ Z$dQdR„ Z%dSdT„ Z&dUdV„ Z'dWdX„ Z(dYdZ„ Z)d[d\„ Z*d]d^„ Z+d_d`„ Z,dadb„ Z-dpdcdd„Z.dedf„ Z/dgdh„ Z0didj„ Z1dkdl„ Z2‡  Z3S )qÚExpressionWriterzL
    A Cython code writer that is intentionally limited to expressions.
    Nc                    s,   t t| ƒ ¡  |d krd}|| _dg| _d S )Nr
   r   )r   rÀ   r   r   Ú
precedencer!   r"   r   r   r   æ  s
    zExpressionWriter.__init__c                 C   s   |   |¡ | jS r   r$   r&   r   r   r   r'   í  s    
zExpressionWriter.writec                 C   s   |  j |7  _ d S r   )r   r   r   r   r   r   ñ  s    zExpressionWriter.putc                 C   s&   | j  |¡r"| j d t|ƒ … | _ d S r   )r   Úendswithr4   r   r   r   r   Úremoveô  s    zExpressionWriter.removec                 C   sD   t |ƒdkr@|d d… D ]}|  |¡ |  d¡ q|  |d ¡ d S )Nr   r1   r3   )r4   r%   r   )r   r6   r8   r   r   r   r9   ø  s
    
z%ExpressionWriter.comma_separated_listc                 C   s   t d| ƒ‚d S r=   r>   r:   r   r   r   r@   ÿ  s    zExpressionWriter.visit_Nodec                 C   s   |   |j¡ d S r   ©r   rt   r:   r   r   r   Úvisit_IntNode  s    zExpressionWriter.visit_IntNodec                 C   s   |   |j¡ d S r   rÄ   r:   r   r   r   Úvisit_FloatNode  s    z ExpressionWriter.visit_FloatNodec                 C   s   |   d¡ d S )NÚNone©r   r:   r   r   r   Úvisit_NoneNode  s    zExpressionWriter.visit_NoneNodec                 C   s   |   |j¡ d S r   rU   r:   r   r   r   rŒ     s    zExpressionWriter.visit_NameNodec                 C   s   |   d¡ d S )Nz...rÈ   r:   r   r   r   Úvisit_EllipsisNode  s    z#ExpressionWriter.visit_EllipsisNodec                 C   s   |   t|jƒ¡ d S r   ©r   Ústrrt   r:   r   r   r   Úvisit_BoolNode  s    zExpressionWriter.visit_BoolNodec                 C   s   |   t|jƒ¡ d S r   rË   r:   r   r   r   Úvisit_ConstNode  s    z ExpressionWriter.visit_ConstNodec                 C   s   |   |j¡ |   d¡ d S )NÚjrÄ   r:   r   r   r   Úvisit_ImagNode  s    zExpressionWriter.visit_ImagNoder
   c                 C   s8   t |jƒ}|d dkr"|dd … }|  d||f ¡ d S )Nr   Zubr   z%s%s)Úreprrt   r   )r   r;   ÚprefixZrepr_valr   r   r   Úemit_string  s    
zExpressionWriter.emit_stringc                 C   s   |   |d¡ d S )NÚb©rÓ   r:   r   r   r   Úvisit_BytesNode!  s    z ExpressionWriter.visit_BytesNodec                 C   s   |   |¡ d S r   rÕ   r:   r   r   r   Úvisit_StringNode$  s    z!ExpressionWriter.visit_StringNodec                 C   s   |   |d¡ d S )NÚurÕ   r:   r   r   r   Úvisit_UnicodeNode'  s    z"ExpressionWriter.visit_UnicodeNode©r
   r
   c                 C   s2   |\}}|  ¡ }|  |¡ |  |¡ |  |¡ d S r   )Úsubexpr_nodesr   r9   )r   r;   ÚparensÚ
open_parenÚclose_parenr6   r   r   r   Úemit_sequence*  s
    

zExpressionWriter.emit_sequencec                 C   s   |   |d¡ d S )Nú[]©rß   r:   r   r   r   Úvisit_ListNode1  s    zExpressionWriter.visit_ListNodec                 C   s   |   |d¡ d S )Nú()rá   r:   r   r   r   Úvisit_TupleNode4  s    z ExpressionWriter.visit_TupleNodec                 C   s,   t | ¡ ƒdkr|  |d¡ n
|  d¡ d S )Nr   ú{}zset())r4   rÛ   rß   r   r:   r   r   r   Úvisit_SetNode7  s    zExpressionWriter.visit_SetNodec                 C   s   |   |d¡ d S )Nrå   rá   r:   r   r   r   Úvisit_DictNode=  s    zExpressionWriter.visit_DictNodec                 C   s&   |   |j¡ |  d¡ |   |j¡ d S ©Nz: )r%   Úkeyr   rt   r:   r   r   r   Úvisit_DictItemNode@  s    
z#ExpressionWriter.visit_DictItemNodeé   é   )Únotú!ú+ú-ú~r   é   é   é   é   é   é   é	   é
   é   )ÚorÚandÚinZnot_inÚisÚis_notú<z<=ú>z>=z!=z==ú|ú^rJ   z<<z>>rï   rð   r   r   ú/z//ú%ú**c                 C   s,   | j d }||kr|  d¡ | j  |¡ d S )Nr1   rQ   )rÁ   r   r   )r   Únew_precÚold_precr   r   r   Úoperator_enterX  s    

zExpressionWriter.operator_enterc                 C   s2   | j dd … \}}||kr$|  d¡ | j  ¡  d S )NéþÿÿÿrR   )rÁ   r   Úpop)r   r  r  r   r   r   Úoperator_exit^  s    
zExpressionWriter.operator_exitc                 C   s:   d}| j | }|  |¡ |  d¡ |  |j¡ |  ¡  d S )Nrí   znot )Úunop_precedencer	  r   r%   Úoperandr  ©r   r;   ÚopZprecr   r   r   Úvisit_NotNoded  s    


zExpressionWriter.visit_NotNodec                 C   sB   |j }| j| }|  |¡ |  d|j  ¡ |  |j¡ |  ¡  d S )Nz%s)r­   r  r	  r   r%   r  r  r  r   r   r   Úvisit_UnopNodel  s    

zExpressionWriter.visit_UnopNodec                 C   sX   |j }| j |d¡}|  |¡ |  |j¡ |  d| dd¡ ¡ |  |j¡ |  	¡  d S )Nr   z %s Ú_rb   )
r­   Úbinop_precedenceÚgetr	  r%   Úoperand1r   ÚreplaceÚoperand2r  r  r   r   r   Úvisit_BinopNodet  s    
z ExpressionWriter.visit_BinopNodec                 C   s   |   |¡ d S r   ©r  r:   r   r   r   Úvisit_BoolBinopNode}  s    z$ExpressionWriter.visit_BoolBinopNodec                 C   s   |   |¡ d S r   r  r:   r   r   r   Úvisit_PrimaryCmpNode€  s    z%ExpressionWriter.visit_PrimaryCmpNodec                 C   s`   |   |j¡ |  d¡ t|jtƒrF|j ¡ r:|  |j¡ qR|  d¡ n|   |j¡ |  d¡ d S )NrM   rã   rN   )r%   rH   r   r„   ÚindexÚ	TupleNoderÛ   rß   r:   r   r   r   Úvisit_IndexNodeƒ  s    

z ExpressionWriter.visit_IndexNodec                 C   sn   |   |j¡ |  d¡ |jr(|   |j¡ |  d¡ |jrD|   |j¡ |jr`|  d¡ |   |j¡ |  d¡ d S )NrM   re   rN   )r%   rH   r   ÚstartÚstopÚslicer:   r   r   r   Úvisit_SliceIndexNode  s    


z%ExpressionWriter.visit_SliceIndexNodec                 C   sT   |j js|  |j ¡ |  d¡ |jjs2|  |j¡ |jjsP|  d¡ |  |j¡ d S )Nre   )r   Zis_noner%   r   r!  Ústepr:   r   r   r   Úvisit_SliceNodeœ  s    

z ExpressionWriter.visit_SliceNodec                 C   s<   |   |j¡ |  d¡ |   |j¡ |  d¡ |   |j¡ d S )Nú if z else )r%   Ztrue_valr   ZtestZ	false_valr:   r   r   r   Úvisit_CondExprNode¦  s
    

z#ExpressionWriter.visit_CondExprNodec                 C   s    |   |j¡ |  d|j ¡ d S )Nz.%s)r%   Úobjr   rk   r:   r   r   r   Úvisit_AttributeNode­  s    z$ExpressionWriter.visit_AttributeNodec                 C   s0   |   |j¡ |  d¡ |  |j¡ |  d¡ d S rP   )r%   Úfunctionr   r9   rS   r:   r   r   r   Úvisit_SimpleCallNode±  s    
z%ExpressionWriter.visit_SimpleCallNodec                 C   s¢   |d krd S t |tƒr0|  |j¡ |  |j¡ nnt |tƒr^| ¡ D ]}|  |¡ |  d¡ qBn@t |t	ƒrŠ|  d¡ |  |j
¡ |  d¡ n|  |¡ |  d¡ d S )Nr3   r   )r„   ZAddNodeÚemit_pos_argsr  r  r  rÛ   r%   r   ZAsTupleNodeÚarg©r   r;   r«   r   r   r   r,  ·  s    





zExpressionWriter.emit_pos_argsc                 C   s”   |d krd S t |tƒr0| ¡ D ]}|  |¡ qn`t |tƒrr| ¡ D ],}|  d|jj ¡ |  |j¡ |  d¡ qBn|  d¡ |  |¡ |  d¡ d S )Nz%s=r3   r  )	r„   ZMergedDictNoderÛ   Úemit_kwd_argsZDictNoder   ré   rt   r%   r.  r   r   r   r/  É  s    



zExpressionWriter.emit_kwd_argsc                 C   sF   |   |j¡ |  d¡ |  |j¡ |  |j¡ |  d¡ |  d¡ d S )NrQ   r3   rR   )r%   r*  r   r,  r^   r/  r_   rÃ   r:   r   r   r   Úvisit_GeneralCallNodeÙ  s    

z&ExpressionWriter.visit_GeneralCallNodec                 C   sj   |\}}|   |¡ |  |¡ |   d¡ |  |¡ |   d¡ |  |¡ |r\|   d¡ |  |¡ |   |¡ d S )Nz for rš   r&  )r   r%   )r   rD   rœ   rž   r¢   rÜ   rÝ   rÞ   r   r   r   Úemit_comprehensioná  s    







z#ExpressionWriter.emit_comprehensionc                 C   s   |   |j¡ d S r   )r%   r«   r:   r   r   r   Úvisit_ComprehensionAppendNodeð  s    z.ExpressionWriter.visit_ComprehensionAppendNodec                 C   s&   |   |j¡ |  d¡ |   |j¡ d S rè   )r%   Zkey_exprr   Z
value_exprr:   r   r   r   Ú!visit_DictComprehensionAppendNodeó  s    
z2ExpressionWriter.visit_DictComprehensionAppendNodec                 C   sp   ddddœ}||j  ¡  }|jj}|jj}|jjj}d }t|dƒrZ|jd j	}|jd j}|  
|||||¡ d S )Nrà   rå   )ÚlistÚdictÚsetr¡   r   )ÚtypeZpy_type_nameÚlooprD   rœ   r   rž   Úhasattrr¡   r¢   r1  )r   r;   ZtpmaprÜ   rD   rœ   rž   r¢   r   r   r   Úvisit_ComprehensionNodeø  s    

z(ExpressionWriter.visit_ComprehensionNodec                 C   sn   |j j}|j j}|j jj}d }t|dƒrF|jd j}|jd jjj	}nt|dƒrX|jj	}|  
||||d¡ d S )Nr¡   r   r«   rã   )r8  rD   rœ   r   rž   r9  r¡   r¢   r«   r-  r1  )r   r;   rD   rœ   rž   r¢   r   r   r   Úvisit_GeneratorExpressionNode  s    


z.ExpressionWriter.visit_GeneratorExpressionNode)N)r
   )rÚ   )rÚ   )4r   r   r   r   r   r'   r   rÃ   r9   r@   rÅ   rÆ   rÉ   rŒ   rÊ   rÍ   rÎ   rÐ   rÓ   rÖ   r×   rÙ   rß   râ   rä   ræ   rç   rê   r  r  r	  r  r  r  r  r  r  r  r#  r%  r'  r)  r+  r,  r/  r0  r1  r2  r3  r:  r;  r‘   r   r   r"   r   rÀ   á  sž   

   þ               ô	

 þ
rÀ   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	PxdWriterz\
    A Cython code writer for everything supported in pxd files.
    (currently unused)
    c                 C   s   t d |  |¡j¡ƒ |S )NÚ
)Úprintrq   r'   r   r:   r   r   r   Ú__call__  s    zPxdWriter.__call__c                 C   s~   |j r|  d¡ n
|  d¡ |jr>|  d |j¡¡ |  d¡ |jdkr^|  |j¡ |  d¡ |jrn|  d¡ |  |j¡ d S )Nr}   ra   rb   r~   r   )	r€   r-   r   r   rq   rn   r‚   r%   rZ   r:   r   r   r   rƒ     s    




zPxdWriter.visit_CFuncDefNodec                 C   s   d S r   r   r:   r   r   r   Úvisit_StatNode-  s    zPxdWriter.visit_StatNodeN)r   r   r   r   r?  rƒ   r@  r   r   r   r   r<    s   r<  c                   @   s   e Zd ZdZdS )Ú
CodeWriterz(
    A complete Cython code writer.
    N)r   r   r   r   r   r   r   r   rA  1  s   rA  N)r   Z
__future__r   r   ZCompiler.Visitorr   ZCompiler.ExprNodesZCompiler.Nodesr   Úobjectr   r   r’   rÀ   r<  rA  r   r   r   r   Ú<module>   s     +   5