Click here to Skip to main content
15,891,473 members
Articles / Programming Languages / C#

An extensible math expression parser with plug-ins

Rate me:
Please Sign up or sign in to vote.
4.92/5 (147 votes)
13 Mar 2008CPOL51 min read 1.4M   29K   364  
Design and code for an extensible, maintainable, robust, and easy to use math parser.
// MTFirstPlugin.cpp : Implementation of CMTFirstPlugin
#include "stdafx.h"
#include "PluginFunc.h"
#include "..\MTParserLib\MTParserDef.h"


#include "MTPluginDemo.h"
#include "MTFirstPlugin.h"



/////////////////////////////////////////////////////////////////////////////
// CMTFirstPlugin

STDMETHODIMP CMTFirstPlugin::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IMTFirstPlugin
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

void CMTFirstPlugin::initPlugin()
{
	// constants...
	addConst(_T("2pi"), 2*3.14159);

	// operators...

	// functions...
	m_funcs.push_back( new FactorialFct() );

	m_funcs.push_back(new SinFct());
	m_funcs.push_back(new ArcSinFct());
	m_funcs.push_back(new SinhFct());
	m_funcs.push_back(new CosFct());
	m_funcs.push_back(new ArcCosFct());
	m_funcs.push_back(new CoshFct());
	m_funcs.push_back(new TanFct());
	m_funcs.push_back(new ArcTanFct());
	m_funcs.push_back(new TanhFct());
	m_funcs.push_back(new SqrtFct());
	m_funcs.push_back(new MinFct());
	m_funcs.push_back(new Min3Fct());			
	m_funcs.push_back(new MinUndefFct());	
	m_funcs.push_back(new MaxFct());
	m_funcs.push_back(new Max3Fct());	
	m_funcs.push_back(new MaxUndefFct());	
	m_funcs.push_back(new AbsFct());
	m_funcs.push_back(new RandFct());
	m_funcs.push_back(new RandMinMaxFct());
	m_funcs.push_back(new CeilFct());
	m_funcs.push_back(new FloorFct());
	m_funcs.push_back(new LogFct());
	m_funcs.push_back(new Log10Fct());
	m_funcs.push_back(new AvgFct());
	m_funcs.push_back(new SumFct());

}

void CMTFirstPlugin::addConst(LPCTSTR name, double value)
{
	SConstant constant;
	constant.name = name;
	constant.value = value;

	m_consts.push_back(constant);
}

CMTFirstPlugin::~CMTFirstPlugin()
{
	int t;
	int size;

	// delete all functions
	size = m_funcs.size();
	for( t=0; t<size; t++ )
	{
		delete m_funcs[t];
	}

	// delete all operators
	size = m_ops.size();
	for( t=0; t<size; t++ )
	{
		delete m_ops[t];
	}

}

STDMETHODIMP CMTFirstPlugin::getNbConsts(INT * pNbConsts)
{
	*pNbConsts = m_consts.size();
		
	return S_OK;
}
STDMETHODIMP CMTFirstPlugin::getConst(INT id, BSTR * name, DOUBLE * pVal)
{	
	*name = SysAllocString(m_consts[id].name);
	*pVal = m_consts[id].value;		
		
	return S_OK;
}
STDMETHODIMP CMTFirstPlugin::getNbFuncs(INT * pNbFuncs)
{
	*pNbFuncs = m_funcs.size();	
		
	return S_OK;
}
STDMETHODIMP CMTFirstPlugin::getFuncSymbol(INT id, BSTR * symbol)
{	
	*symbol = SysAllocString(m_funcs[id]->getSymbol().c_str());	
		
	return S_OK;
}

STDMETHODIMP CMTFirstPlugin::getFuncHelpString(INT id, BSTR * helpString)
{
	*helpString = SysAllocString(m_funcs[id]->getHelpString().c_str());		
		
	return S_OK;
}
STDMETHODIMP CMTFirstPlugin::getFuncDescription(INT id, BSTR * description)
{
	*description = SysAllocString(m_funcs[id]->getDescription().c_str());		
	return S_OK;
}

STDMETHODIMP CMTFirstPlugin::isFuncConstant(INT id, INT * pConstant)
{
	*pConstant = m_funcs[id]->isConstant();	
	return S_OK;
}

STDMETHODIMP CMTFirstPlugin::getFuncNbArgs(INT id, INT * pNbArgs)
{
	*pNbArgs = m_funcs[id]->getNbArgs();	
	return S_OK;
}

STDMETHODIMP CMTFirstPlugin::evaluateFunc(INT id, INT nbArgs, DOUBLE * pArg, DOUBLE * pResult)
{
	*pResult = m_funcs[id]->evaluate(nbArgs, pArg);
	return S_OK;
}
STDMETHODIMP CMTFirstPlugin::getNbOps(INT * pNbOps)
{
	*pNbOps	= m_ops.size();
	return S_OK;
}
STDMETHODIMP CMTFirstPlugin::getOpPrecedence(INT id, INT * pPrecedence)
{
	*pPrecedence = m_ops[id]->getPrecedence();		
	return S_OK;
}
STDMETHODIMP CMTFirstPlugin::isOpConstant(INT id, INT * pConstant)
{
	*pConstant = m_ops[id]->isConstant();
		
	return S_OK;
}
STDMETHODIMP CMTFirstPlugin::getOpSymbol(INT id, BSTR * symbol)
{
	*symbol = SysAllocString(m_ops[id]->getSymbol().c_str());		
	return S_OK;
}

STDMETHODIMP CMTFirstPlugin::getOpHelpString(INT id, BSTR * helpString)
{
	*helpString = SysAllocString(m_ops[id]->getHelpString().c_str());			
	return S_OK;
}

STDMETHODIMP CMTFirstPlugin::getOpDescription(INT id, BSTR * description)
{
	*description = SysAllocString(m_ops[id]->getDescription().c_str());			
	return S_OK;
}

STDMETHODIMP CMTFirstPlugin::isOneArgOp(INT id, INT * pOneArg)
{
	*pOneArg = m_ops[id]->oneArgumentOp();	
	return S_OK;
}
STDMETHODIMP CMTFirstPlugin::evaluateOp(INT id, INT nbArgs, DOUBLE * pArg, DOUBLE * pResult)
{
	*pResult = m_ops[id]->evaluate(nbArgs, pArg);	
	return S_OK;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Software Engineer working at a fun and smart startup company

Comments and Discussions