Click here to Skip to main content
15,891,938 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.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>MTParserLib: MTParserItemI Class Reference</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.4 -->
<div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div>
<h1>MTParserItemI Class Reference</h1><!-- doxytag: class="MTParserItemI" --><code>#include &lt;<a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a>&gt;</code>
<p>
<p>Inheritance diagram for MTParserItemI:
<p><center><img src="classMTParserItemI.png" usemap="#MTParserItemI_map" border="0" alt=""></center>
<map name="MTParserItemI_map">
<area href="classMTFunctionI.html" alt="MTFunctionI" shape="rect" coords="0,56,198,80">
<area href="classMTOperatorI.html" alt="MTOperatorI" shape="rect" coords="416,56,614,80">
<area href="classMTVariableI.html" alt="MTVariableI" shape="rect" coords="832,56,1030,80">
<area href="classAbsFct.html" alt="AbsFct" shape="rect" coords="208,112,406,136">
<area href="classArcCosFct.html" alt="ArcCosFct" shape="rect" coords="208,168,406,192">
<area href="classArcSinFct.html" alt="ArcSinFct" shape="rect" coords="208,224,406,248">
<area href="classArcTanFct.html" alt="ArcTanFct" shape="rect" coords="208,280,406,304">
<area href="classAvgFct.html" alt="AvgFct" shape="rect" coords="208,336,406,360">
<area href="classCeilFct.html" alt="CeilFct" shape="rect" coords="208,392,406,416">
<area href="classCosFct.html" alt="CosFct" shape="rect" coords="208,448,406,472">
<area href="classCoshFct.html" alt="CoshFct" shape="rect" coords="208,504,406,528">
<area href="classFactorialFct.html" alt="FactorialFct" shape="rect" coords="208,560,406,584">
<area href="classFloorFct.html" alt="FloorFct" shape="rect" coords="208,616,406,640">
<area href="classIfFct.html" alt="IfFct" shape="rect" coords="208,672,406,696">
<area href="classIsFiniteFct.html" alt="IsFiniteFct" shape="rect" coords="208,728,406,752">
<area href="classIsNaNFct.html" alt="IsNaNFct" shape="rect" coords="208,784,406,808">
<area href="classLog10Fct.html" alt="Log10Fct" shape="rect" coords="208,840,406,864">
<area href="classLogFct.html" alt="LogFct" shape="rect" coords="208,896,406,920">
<area href="classMax3Fct.html" alt="Max3Fct" shape="rect" coords="208,952,406,976">
<area href="classMaxFct.html" alt="MaxFct" shape="rect" coords="208,1008,406,1032">
<area href="classMaxUndefFct.html" alt="MaxUndefFct" shape="rect" coords="208,1064,406,1088">
<area href="classMin3Fct.html" alt="Min3Fct" shape="rect" coords="208,1120,406,1144">
<area href="classMinFct.html" alt="MinFct" shape="rect" coords="208,1176,406,1200">
<area href="classMinUndefFct.html" alt="MinUndefFct" shape="rect" coords="208,1232,406,1256">
<area href="classMTConvFunctionI.html" alt="MTConvFunctionI" shape="rect" coords="208,1288,406,1312">
<area href="classMTFctParamTokenizer_1_1ClientFctI.html" alt="MTFctParamTokenizer::ClientFctI" shape="rect" coords="208,1344,406,1368">
<area href="classMTMacroFct.html" alt="MTMacroFct" shape="rect" coords="208,1400,406,1424">
<area href="classRandFct.html" alt="RandFct" shape="rect" coords="208,1456,406,1480">
<area href="classRandMinMaxFct.html" alt="RandMinMaxFct" shape="rect" coords="208,1512,406,1536">
<area href="classRoundFct.html" alt="RoundFct" shape="rect" coords="208,1568,406,1592">
<area href="classSinFct.html" alt="SinFct" shape="rect" coords="208,1624,406,1648">
<area href="classSinhFct.html" alt="SinhFct" shape="rect" coords="208,1680,406,1704">
<area href="classSqrtFct.html" alt="SqrtFct" shape="rect" coords="208,1736,406,1760">
<area href="classSumFct.html" alt="SumFct" shape="rect" coords="208,1792,406,1816">
<area href="classTanFct.html" alt="TanFct" shape="rect" coords="208,1848,406,1872">
<area href="classTanhFct.html" alt="TanhFct" shape="rect" coords="208,1904,406,1928">
<area href="classAddOp.html" alt="AddOp" shape="rect" coords="624,112,822,136">
<area href="classAndOp.html" alt="AndOp" shape="rect" coords="624,168,822,192">
<area href="classDivOp.html" alt="DivOp" shape="rect" coords="624,224,822,248">
<area href="classEqualOp.html" alt="EqualOp" shape="rect" coords="624,280,822,304">
<area href="classExpOp.html" alt="ExpOp" shape="rect" coords="624,336,822,360">
<area href="classGreaterEqualThanOp.html" alt="GreaterEqualThanOp" shape="rect" coords="624,392,822,416">
<area href="classGreaterThanOp.html" alt="GreaterThanOp" shape="rect" coords="624,448,822,472">
<area href="classLesserEqualThanOp.html" alt="LesserEqualThanOp" shape="rect" coords="624,504,822,528">
<area href="classLesserThanOp.html" alt="LesserThanOp" shape="rect" coords="624,560,822,584">
<area href="classMinusOp.html" alt="MinusOp" shape="rect" coords="624,616,822,640">
<area href="classModuloOp.html" alt="ModuloOp" shape="rect" coords="624,672,822,696">
<area href="classMultOp.html" alt="MultOp" shape="rect" coords="624,728,822,752">
<area href="classNotEqualOp.html" alt="NotEqualOp" shape="rect" coords="624,784,822,808">
<area href="classNotOp.html" alt="NotOp" shape="rect" coords="624,840,822,864">
<area href="classOrOp.html" alt="OrOp" shape="rect" coords="624,896,822,920">
<area href="classUnaryAddOp.html" alt="UnaryAddOp" shape="rect" coords="624,952,822,976">
<area href="classUnaryMinusOp.html" alt="UnaryMinusOp" shape="rect" coords="624,1008,822,1032">
<area href="classMTDoubleVector.html" alt="MTDoubleVector" shape="rect" coords="832,112,1030,136">
</map>
<a href="classMTParserItemI-members.html">List of all members.</a><hr><a name="_details"></a><h2>Detailed Description</h2>
Abstract parser items. 
<p>
Implement this interface to create new items (operators, functions, constants...). An item instance is owned by only one parser object at a time: it cannot be shared between parsers. When a parser needs the same item as another parser, it has to duplicate the item, thus creating a new item instance.
<p>

<p>
Definition at line <a class="el" href="MTParserPublic_8h-source.html#l00125">125</a> of file <a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a>.<table border="0" cellpadding="0" cellspacing="0">
<tr><td></td></tr>
<tr><td colspan="2"><br><h2>Public Member Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual const MTCHAR *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTParserItemI.html#a0">getID</a> ()</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Get the item unique identifier.  <a href="#a0"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual const MTCHAR *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTParserItemI.html#a1">getSymbol</a> ()=0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Get the item symbol.  <a href="#a1"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual const MTCHAR *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTParserItemI.html#a2">getHelpString</a> ()=0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Get the helpstring.  <a href="#a2"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual const MTCHAR *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTParserItemI.html#a3">getDescription</a> ()=0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Get a short description.  <a href="#a3"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual bool&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTParserItemI.html#a4">isConstant</a> ()=0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Indicate whether an item is constant.  <a href="#a4"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual <a class="el" href="MTParserPublic_8h.html#a8">MTDOUBLE</a>&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTParserItemI.html#a5">evaluate</a> (unsigned int nbArgs, const <a class="el" href="MTParserPublic_8h.html#a8">MTDOUBLE</a> *pArg)=0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Evaluate the item.  <a href="#a5"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="a6"></a><!-- doxytag: member="MTParserItemI::~MTParserItemI" ref="a6" args="()" -->
virtual&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTParserItemI.html#a6">~MTParserItemI</a> ()</td></tr>

</table>
<hr><h2>Member Function Documentation</h2>
<a class="anchor" name="a5"></a><!-- doxytag: member="MTParserItemI::evaluate" ref="a5" args="(unsigned int nbArgs, const MTDOUBLE *pArg)=0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual <a class="el" href="MTParserPublic_8h.html#a8">MTDOUBLE</a> MTParserItemI::evaluate           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top">unsigned int&nbsp;</td>
          <td class="mdname" nowrap> <em>nbArgs</em>, </td>
        </tr>
        <tr>
          <td class="md" nowrap align="right"></td>
          <td class="md"></td>
          <td class="md" nowrap>const <a class="el" href="MTParserPublic_8h.html#a8">MTDOUBLE</a> *&nbsp;</td>
          <td class="mdname" nowrap> <em>pArg</em></td>
        </tr>
        <tr>
          <td class="md"></td>
          <td class="md">)&nbsp;</td>
          <td class="md" colspan="2"><code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Evaluate the item. 
<p>
No exception should be throwned during evaluation. This is a performance issue since if exceptions were possible, then the user would have to use a try-catch clause thus diminishing performance. Instead, a NaN value may be returned to indicate that something is wrong.<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>nbArgs</em>&nbsp;</td><td>Number of arguments pointed by pArg </td></tr>
    <tr><td valign="top"></td><td valign="top"><em>pArg</em>&nbsp;</td><td>Argument buffer. Arguments are in the same order as they appear in the expression. </td></tr>
  </table>
</dl>
<dl compact><dt><b>Returns:</b></dt><dd>The evaluated result</dd></dl>

<p>
Implemented in <a class="el" href="classMTDoubleVector.html#a5">MTDoubleVector</a>, <a class="el" href="classMTMacroFct.html#a6">MTMacroFct</a>, and <a class="el" href="classMTConvFunctionI.html#a6">MTConvFunctionI</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a3"></a><!-- doxytag: member="MTParserItemI::getDescription" ref="a3" args="()=0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual const MTCHAR* MTParserItemI::getDescription           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Get a short description. 
<p>
A description of what the item is about.<p>
<dl compact><dt><b>Returns:</b></dt><dd>The description</dd></dl>

<p>
Implemented in <a class="el" href="classHexConvFunc.html#a2">HexConvFunc</a>, <a class="el" href="classBinConvFunc.html#a2">BinConvFunc</a>, <a class="el" href="classMTMacroFct.html#a3">MTMacroFct</a>, and <a class="el" href="classMTVariableI.html#a3">MTVariableI</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a2"></a><!-- doxytag: member="MTParserItemI::getHelpString" ref="a2" args="()=0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual const MTCHAR* MTParserItemI::getHelpString           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Get the helpstring. 
<p>
An helpstring gives hint to the user on how to use an item.<p>
<dl compact><dt><b>Returns:</b></dt><dd>The helpstring</dd></dl>

<p>
Implemented in <a class="el" href="classHexConvFunc.html#a1">HexConvFunc</a>, <a class="el" href="classBinConvFunc.html#a1">BinConvFunc</a>, <a class="el" href="classMTMacroFct.html#a2">MTMacroFct</a>, and <a class="el" href="classMTVariableI.html#a2">MTVariableI</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a0"></a><!-- doxytag: member="MTParserItemI::getID" ref="a0" args="()" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual const MTCHAR* MTParserItemI::getID           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [inline, virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Get the item unique identifier. 
<p>
This identifier must be unique. Overloaded items must have different identifiers. The main purpose of this identifier is for localization.<p>
The length is unlimited.<p>
<dl compact><dt><b>Returns:</b></dt><dd>The item identifier</dd></dl>

<p>
Definition at line <a class="el" href="MTParserPublic_8h-source.html#l00139">139</a> of file <a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a>.
<p>
References <a class="el" href="classMTParserItemI.html#a1">getSymbol()</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a1"></a><!-- doxytag: member="MTParserItemI::getSymbol" ref="a1" args="()=0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual const MTCHAR* MTParserItemI::getSymbol           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Get the item symbol. 
<p>
The length is unlimited.<p>
<dl compact><dt><b>Returns:</b></dt><dd>The item name</dd></dl>

<p>
Implemented in <a class="el" href="classMTDoubleVector.html#a4">MTDoubleVector</a>, <a class="el" href="classHexConvFunc.html#a0">HexConvFunc</a>, <a class="el" href="classBinConvFunc.html#a0">BinConvFunc</a>, and <a class="el" href="classMTMacroFct.html#a1">MTMacroFct</a>.
<p>
Referenced by <a class="el" href="MTParserTestCases_8cpp-source.html#l01807">MTParserTestVariable::doTests()</a>, <a class="el" href="MTParserTestCases_8cpp-source.html#l00607">MTParserTestConstDef::doTests()</a>, <a class="el" href="MTParserPublic_8h-source.html#l00139">getID()</a>, <a class="el" href="MTParserPrivate_8h-source.html#l00437">MTConvFunctionI::onCloseBracket()</a>, <a class="el" href="MTParserCompiler_8cpp-source.html#l01046">MTCompilerDefState::onOp()</a>, <a class="el" href="MTParserRegistrar_8cpp-source.html#l00690">MTParserRegistrar::redefineVar()</a>, <a class="el" href="MTParserPrivate_8h-source.html#l00481">MTConvFunctionI::throwConversionExcep()</a>, <a class="el" href="MTParserRegistrar_8cpp-source.html#l00400">MTParserRegistrar::undefineFunc()</a>, and <a class="el" href="MTParserRegistrar_8cpp-source.html#l00424">MTParserRegistrar::undefineOp()</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a4"></a><!-- doxytag: member="MTParserItemI::isConstant" ref="a4" args="()=0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual bool MTParserItemI::isConstant           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Indicate whether an item is constant. 
<p>
A constant item can be evaluated only once and its value cached, thus optimizing performance. For example, a constant function always returns the same result when evaluated with the same arguments. An example of non-constant function is the random function.<p>
This method is called after the first evaluation.<p>
<dl compact><dt><b>Returns:</b></dt><dd>True if the item is constant</dd></dl>

<p>
Implemented in <a class="el" href="classMTFunctionI.html#a4">MTFunctionI</a>, <a class="el" href="classMTOperatorI.html#a3">MTOperatorI</a>, and <a class="el" href="classMTVariableI.html#a1">MTVariableI</a>.    </td>
  </tr>
</table>
<hr>The documentation for this class was generated from the following file:<ul>
<li><a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a></ul>
<hr size="1"><address style="align: right;"><small>Generated on Sun Mar 9 17:39:37 2008 for MTParserLib by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.4 </small></address>
</body>
</html>

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