uk.org.retep.math.writer.PostfixPrintVisitor Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the retep.org.uk nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package uk.org.retep.math.writer;
import java.io.StringWriter;
import java.io.Writer;
import javax.annotation.Nonnull;
import uk.org.retep.math.MathematicsException;
import uk.org.retep.math.model.DyadicOp;
import uk.org.retep.math.model.MathOp;
import uk.org.retep.math.model.MonadicOp;
import uk.org.retep.math.model.MultipleOp;
import uk.org.retep.math.model.ParenthesisOp;
/**
* A visitor which can write a tree to a PrintWriter
*
* @author peter
*/
public class PostfixPrintVisitor
extends AbstractPrintVisitor
{
public static String toString( @Nonnull final MathOp op )
throws MathematicsException
{
final StringWriter w = new StringWriter();
op.accept( new PostfixPrintVisitor( w ) );
return w.toString();
}
public PostfixPrintVisitor( @Nonnull final Writer writer )
{
super( writer );
}
@Override
protected void write( final int count, final char s )
throws MathematicsException
{
int c = count;
while( c > 0 )
{
write( s );
space();
c--;
}
}
@Override
protected void write( final int count, final String s )
throws MathematicsException
{
int c = count;
while( c > 0 )
{
write( s );
space();
c--;
}
}
@Override
protected Object visit( final DyadicOp op, final String label )
throws MathematicsException
{
op.getLeft().accept( this );
op.getRight().accept( this );
write( label );
space();
return null;
}
@Override
protected Object visit( final MonadicOp op, final char label )
throws MathematicsException
{
op.getValue().accept( this );
write( label );
space();
return null;
}
@Override
protected Object visit( final MonadicOp op, final String label )
throws MathematicsException
{
op.getValue().accept( this );
write( label );
space();
return null;
}
@Override
protected Object visit( final DyadicOp op, final char label )
throws MathematicsException
{
op.getLeft().accept( this );
op.getRight().accept( this );
write( label );
space();
return null;
}
@Override
protected Object visit( final MultipleOp op, final String label )
throws MathematicsException
{
for( MathOp o : op )
{
o.accept( this );
}
if( op.size() > 1 )
{
write( op.size() - 1, label );
}
else
{
space();
}
return null;
}
@Override
protected Object visit( final MultipleOp op, final char label )
throws MathematicsException
{
for( MathOp o : op )
{
o.accept( this );
}
if( op.size() > 1 )
{
write( op.size() - 1, label );
}
else
{
space();
}
return null;
}
@Override
protected Object function( String name, MonadicOp op )
throws MathematicsException
{
op.getValue().accept( this );
write( name );
space();
return null;
}
@Override
protected Object function( String name, DyadicOp op )
throws MathematicsException
{
op.getLeft().accept( this );
op.getRight().accept( this );
write( name );
space();
return null;
}
@Override
protected Object function( String name, MultipleOp op )
throws MathematicsException
{
for( MathOp o : op )
{
o.accept( this );
}
write( name );
space();
return null;
}
@Override
public Object subtract( MultipleOp op )
throws MathematicsException
{
// If this looks odd:
// 5 - 3 - 1 = 1
// But if we did this the normal add way and use 5 3 1 - - we'd get 3 as the anser, i.e. 5 - (3 - 1) = 5 - 2 = 3
// so we actually optimise here so we actually get 5 3 1 + - then we'd have
// 5 - (3 + 1) = 5 - 4 = 1 which is the correct result
op.getOperation( 0 ).accept( this );
for( int i = 1; i < op.size(); i++ )
{
op.getOperation( i ).accept( this );
}
if( op.size() > 2 )
{
write( op.size() - 2, '+' );
write( '-' );
}
else if( op.size() > 1 )
{
write( '-' );
}
else
{
space();
}
return null;
}
// FIXME see how the HP calculator represents ( ) in RPN mode
@Override
public Object parenthesis( final ParenthesisOp op )
throws MathematicsException
{
op.getValue().accept( this );
return null;
}
}