com.codetaco.algebrain.operator.OpAdd Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of algebrain Show documentation
Show all versions of algebrain Show documentation
Equation processing for Java
The newest version!
package com.codetaco.algebrain.operator;
import java.text.ParseException;
import com.codetaco.algebrain.EquPart;
import com.codetaco.algebrain.Operator;
import com.codetaco.algebrain.ValueStack;
/**
*
* OpAdd class.
*
*
* @author Chris DeGreef [email protected]
* @since 1.3.9
*/
public class OpAdd extends Operator
{
/**
*
* Constructor for OpAdd.
*
*/
public OpAdd()
{
super();
}
/**
*
* Constructor for OpAdd.
*
*
* @param opTok a {@link com.codetaco.algebrain.EquPart} object.
*/
public OpAdd(final EquPart opTok)
{
super(opTok);
}
/** {@inheritDoc} */
@Override
protected int precedence()
{
return 6;
}
/** {@inheritDoc} */
@Override
public void resolve(final ValueStack values) throws Exception
{
if (values.size() < 2)
throw new Exception("missing operands for " + toString());
try
{
final Object[] value = values.ensureSameTypes();
if (value[0] instanceof Long)
values.push(new Long((Long) value[1] + (Long) value[0]));
else
values.push(new Double((Double) value[1] + (Double) value[0]));
} catch (final ParseException e)
{
e.fillInStackTrace();
throw new Exception(toString() + "; " + e.getMessage(), e);
}
}
/** {@inheritDoc} */
@Override
public String toString()
{
return "op(add)";
}
}