/* * JMEP - Java Mathematical Expression Parser. * Copyright (C) 1999 Jo Desmet * * 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; either * version 2.1 of the License, or any later version. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * You can contact the Original submitter of this library by * email at: Jo_Desmet@yahoo.com. * */ package com.iabcinc.jmep.tokens; import com.iabcinc.jmep.XIllegalFunctionCall; import com.iabcinc.jmep.XUndefinedFunction; import com.iabcinc.jmep.hooks.Function; public class FNCToken extends Token { private Function m_oCallBack; private String m_sName; private int m_nParameters; public FNCToken(String sName, Function oCallBack, int iPosition) { super(Token.FNC, iPosition); m_oCallBack = oCallBack; m_sName = sName; m_nParameters = 0; } public FNCToken(String sName, int iPosition) { super(Token.FNC, iPosition); m_oCallBack = null; m_sName = sName; m_nParameters = 0; } public void setNumberOfParameters(int nPars) { m_nParameters = nPars; } public int getNumberOfParameters() { return m_nParameters; } private int[][] intArrays = new int[1][1]; private float[][] floatArrays = new float[1][1]; private Object[][] objectArrays = new Object[1][1]; public Object evaluate(Object[] oPars, int contextID) throws XUndefinedFunction, XIllegalFunctionCall { Object oValue = null; if (m_oCallBack != null) { try { oValue = m_oCallBack.call(oPars); } catch (RuntimeException e) { throw new XIllegalFunctionCall(getPosition(), e.getMessage(), e); } } if (oValue == null) throw new XUndefinedFunction(getPosition(), m_sName); int i = contextID; if (oValue instanceof Float){ floatArrays[i][0] = (Float)oValue; oValue = floatArrays[i]; } else if (oValue instanceof Integer){ intArrays[i][0] = (Integer)oValue; oValue = intArrays[i]; } else { objectArrays[i][0] = oValue; oValue = objectArrays[i]; } return oValue; } }