o
    giP                     @   s2   d Z ddlZddlmZ ddlZG dd dZdS )a)  
The language that shall be used in a type 4 function contains expressions involving integers, real numbers, and
boolean values only. There shall be no composite data structures such as strings or arrays, no procedures, and
no variables or names. Table 42 lists the operators that can be used in this type of function. (For more
information on these operators, see Appendix B of the PostScript Language Reference, Third Edition.)
Although the semantics are those of the corresponding PostScript operators, a full PostScript interpreter is not
required.
    N)Decimalc                	   @   s:   e Zd ZdZeg fdedeje deje fddZ	dS )PostScriptEvalaE  
    The language that shall be used in a type 4 function contains expressions involving integers, real numbers, and
    boolean values only. There shall be no composite data structures such as strings or arrays, no procedures, and
    no variables or names. Table 42 lists the operators that can be used in this type of function. (For more
    information on these operators, see Appendix B of the PostScript Language Reference, Third Edition.)
    Although the semantics are those of the corresponding PostScript operators, a full PostScript interpreter is not
    required.
    sargsreturnc           	         s  g }||7 }g d}d  t k r  dv r d7  q  dks*  dkr/ d7  q  dv rad} t k rY  dv rY|  7 } d7   t k rY  dv sC|t| qt fd	d
|D rd} t k r  dv r|  7 } d7   t k r  dv s|||vrJ d| d}d}|dkrt |dksJ dt|d tsJ d|d }|t |d  |t| q|dkrt |dksJ dt|d tsJ dt|d tsJ d|d }|d }|t |d  |t |d  |||  q|dkr`t |dks%J dt|d ts1J dt|d ts=J d|d }|d }|t |d  |t |d  ||o]| q|dkrt |dkspJ dt|d ts|J d|d }|t |d  |tt	| q|d krt |dksJ d!t|d tsJ d"|d }|t |d  |tt
| q|d#krt |dksJ d$t|d tsJ d%|d }|t |d  |ttt| q|d&kr2t |dksJ d't|d tsJ d(|d }|t |d  |tt| q|d)krBt |dksBJ d*|d+krt |dksRJ d,t|d ts^J d-t|d tsjJ d.|d tdkswJ d/|d }|d }|t |d  |t |d  |||  q|d0krt |dksJ d1||d  q|d2krt |dksJ d3|d }|d }|t |d  |t |d  |||k q|d4krt |dksJ d5|d }|d }|t |d  |t |d  || || q|d6krJt |dks(J d7|d }t|ts6J d8|t |d  |tt| q|d9krU|d q|d:krt |dkseJ d;|d }t|tssJ d<|t |d  |tt| q|d=krt |dksJ d>|d }|d }t|tsJ d?t|tsJ d?|t |d  |t |d  |||k q|d@krt |dksJ dA|d }|d }t|tsJ t|tsJ |t |d  |t |d  |||k q|dBkr`t |dksJ dC|d }|d }t|ts/J t|ts7J |t |d  |t |d  |tdksTJ dD|tt||  q|dEkrt |dkspJ dF|d }|d }t|tsJ t|tsJ |t |d  |t |d  |||k q|dGkrt |dksJ dH|d }t|tsJ |t |d  |tt| q|dIkrt |dksJ dJ|d }t|tsJ |t |d  |tt|tdK q|dLkrGt |dksJ dM|d }|d }t|ts%J t|ts-J |t |d  |t |d  |||k  q|dNkrt |dksWJ dO|d }|d }t|tsgJ t|tsoJ |t |d  |t |d  |tdksJ dP|tt|t|  q|dQkrt |dksJ dR|d }|d }t|tsJ t|tsJ |t |d  |t |d  |||  q|dSkrt |dksJ dT|d }|d }|t |d  |t |d  |||k q|dUkr:t |dksJ dV|d }t|ts*J |t |d  ||  q|dWkrft |dksJJ dX|d }t|tsVJ |t |d  ||  q|dYkrt |dksvJ dZ|d }|d }t|tsJ t|tsJ |t |d  |t |d  ||p| q|d[krt |dksJ d\|d q|d]krt |dksJ d^|d }t|tsJ |t |d  |tt| q|d_kr!t |dksJ d`|d }t|ts
J |t |d  |ttt| q|dakrQt |dks1J db|d }t|ts=J |t |d  |tt| q|dckrt |dksaJ dd|d }|d }t|tsqJ t|tsyJ |t |d  |t |d  |||  q|dekr|df q|dgkrt |dksJ dh|d }|d }t|tsJ t|tsJ |t |d  |t |d  ||p|o|o|  q d7   t k sg }|D ]}t|ts J || q|S )ia?  
        This function evaluates a postscript str, using args as the (initial) stack.
        This function returns a typing.List[Decimal], or throws an assertion error
        :param s:       the (postfix) str to evaluate
        :param args:    the (initial) stack
        :return:        the resulting stack
        )(absaddandatanbitshiftceilingcopycoscvicvrdivdupeqexchexpfalsefloorgegtidivindexlelnlogltmodmulnenegnotorpoprollroundsinsqrtsubtruetruncatexorr   z 
	   {}z0123456789.- c                    s   g | ]	}|   qS  )
startswith).0xir   r3   U/home/ubuntu/.local/lib/python3.10/site-packages/borb/io/read/postfix/postfix_eval.py
<listcomp>s   s    z+PostScriptEval.evaluate.<locals>.<listcomp>abcdefghijklmnopqrstuvwxyzFz%Unknown operator %s in postscript strNr   z-Unable to apply operator abs, stack underflowz;Unable to apply operator abs, arg 1 must be of type Decimalr      z-Unable to apply operator add, stack underflowz;Unable to apply operator add, arg 1 must be of type Decimalz;Unable to apply operator add, arg 2 must be of type Decimalr	   z-Unable to apply operator and, stack underflowz8Unable to apply operator and, arg 1 must be of type boolz8Unable to apply operator and, arg 2 must be of type boolr
   z.Unable to apply operator atan, stack underflowz<Unable to apply operator atan, arg 1 must be of type Decimalr   z1Unable to apply operator ceiling, stack underflowz?Unable to apply operator ceiling, arg 1 must be of type Decimalr   z-Unable to apply operator cos, stack underflowz;Unable to apply operator cos, arg 1 must be of type Decimalr   z-Unable to apply operator cvi, stack underflowz;Unable to apply operator cvi, arg 1 must be of type Decimalr   z-Unable to apply operator cvr, stack underflowr   z-Unable to apply operator div, stack underflowz;Unable to apply operator div, arg 1 must be of type Decimalz;Unable to apply operator div, arg 2 must be of type Decimalz0Unable to apply operator div, arg1 must not be 0r   z-Unable to apply operator dup, stack underflowr   z,Unable to apply operator eq, stack underflowr   z.Unable to apply operator exch, stack underflowr   z-Unable to apply operator exp, stack underflowz-Unable to apply operator exp, unexpected typer   r   z/Unable to apply operator floor, stack underflowz/Unable to apply operator floor, unexpected typer   z,Unable to apply operator ge, stack underflowz,Unable to apply operator ge, unexpected typer   z,Unable to apply operator gt, stack underflowr   z.Unable to apply operator idiv, stack underflowz/Unable to apply operator idiv, division by zeror   z,Unable to apply operator le, stack underflowr   z,Unable to apply operator ln, stack underflowr   z-Unable to apply operator log, stack underflow
   r   z,Unable to apply operator lt, stack underflowr    z-Unable to apply operator mod, stack underflowz.Unable to apply operator mod, division by zeror!   z-Unable to apply operator mul, stack underflowr"   z,Unable to apply operator ne, stack underflowr#   z-Unable to apply operator neg, stack underflowr$   z-Unable to apply operator not, stack underflowr%   z,Unable to apply operator or, stack underflowr&   z-Unable to apply operator pop, stack underflowr(   z/Unable to apply operator round, stack underflowr)   z-Unable to apply operator sin, stack underflowr*   z.Unable to apply operator sqrt, stack underflowr+   z-Unable to apply operator sub, stack underflowr,   Tr.   z-Unable to apply operator xor, stack underflow)lenappendr   any
isinstancer&   r   boolmathr
   ceilr   degreesintr   r   r   r(   r)   r*   )	r   r   stkknown_operatorsoperandoperatorarg0arg1outr3   r7   r9   evaluate'   s  	+






































      zPostScriptEval.evaluateN)
__name__
__module____qualname____doc__staticmethodstrtypingListr   rP   r3   r3   r3   r9   r      s    ,r   )rT   rW   decimalr   rE   r   r3   r3   r3   r9   <module>   s
   