protected List < EquPart > rpnize ( final List < EquPart > oldTokens ) { final List < EquPart > _rpn = new Stack <> ( ) ; final Stack < EquPart > ops = new Stack <> ( ) ; Operation leftOp ; Operation rightOp ; for ( final EquPart token : oldTokens ) if ( token instanceof Token ) _rpn . add ( token ) ; else { rightOp = ( Operation ) token ; if ( ops . empty ( ) ) { if ( rightOp . includeInRpn ( ) ) ops . push ( rightOp ) ; } else { leftOp = ( Operation ) ops . peek ( ) ; if ( leftOp . preceeds ( rightOp ) ) { _rpn . add ( ops . pop ( ) ) ; /* * while knocking one off scan back as long as the level * doesn't change. A left paren causes the level to * increase and the left paren has that new level. A * right paren causes the level to decrease and has this * new lesser level. So the right paren is one level * less than the operators within the parens. Since we * want to only scan those operators we have to adjust * for the level that they are. */ int level = rightOp . getLevel ( ) ; if ( rightOp instanceof OpRightParen ) level = level + 1 ; Operation compareOp = rightOp ; while ( ! ops . empty ( ) ) { leftOp = ( Operation ) ops . peek ( ) ; if ( leftOp . getLevel ( ) < level ) break ; if ( leftOp . preceeds ( compareOp ) ) { _rpn . add ( ops . pop ( ) ) ; compareOp = leftOp ; } else break ; break ; } } if ( rightOp . includeInRpn ( ) ) ops . push ( rightOp ) ; } } while ( ! ops . empty ( ) ) _rpn . add ( ops . pop ( ) ) ; return _rpn ; }
Create a reverse Polish notation form of the equation