/*
* Copyright 2010 Jan Schmidt-Reinisch
*
* SoundComp - a sound processing library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; in version 2.1
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package de.maramuse.soundcomp.parser.math;
import de.maramuse.soundcomp.parser.Number;
import de.maramuse.soundcomp.parser.SCParser;
import de.maramuse.soundcomp.parser.SCString;
import de.maramuse.soundcomp.parser.TemplateProvider;
import de.maramuse.soundcomp.parser.SCParser.ParserVal;
import de.maramuse.soundcomp.process.ProcessElement;
/**
* this parser symbol represents an addition
*/
public class Plus extends ParserVal implements TemplateProvider, FormulaElement2 {
private static final ProcessElement template=new de.maramuse.soundcomp.math.plus();
public Plus(String s) {
super(SCParser.PLUS, s);
}
@Override
public ParserVal eliminateConst() {
if(inner.size()==1)
return inner.get(0);
if(inner.size()<1||inner.size()>2)
throw new InternalError("\"+\" requires at least one and at most two arguments");
if(inner.get(0).isConstant()&&inner.get(1).isConstant()){
if(inner.get(0).getType()==SCParser.STRING){
return new SCString(inner.get(0).getText()+inner.get(1).getText());
}
if(inner.get(0).getType()==SCParser.NUMBER&&inner.get(1).getType()==SCParser.NUMBER){
try{
ParserVal ret=new Number(inner.get(0).asDouble()+inner.get(1).asDouble());
ret.setFilename(filename);
ret.setLine(line);
return ret;
}catch(NumberFormatException ex){
throw new IllegalArgumentException("should not happen: number format error", ex);
}
}
}
return this;
}
/* (non-Javadoc)
* @see de.maramuse.soundcomp.parser.TemplateProvider#getTemplate()
*/
@Override
public ProcessElement getTemplate() {
return template;
}
/* (non-Javadoc)
* @see de.maramuse.soundcomp.parser.FormulaElement#getInput1Val()
*/
@Override
public ParserVal getInput1Val() {
return inner.get(0);
}
/* (non-Javadoc)
* @see de.maramuse.soundcomp.parser.FormulaElement2#getInput2Val()
*/
@Override
public ParserVal getInput2Val() {
return inner.get(1);
}
}