Click here to Skip to main content
15,896,315 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.5M   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: MTOperatorI 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>MTOperatorI Class Reference</h1><!-- doxytag: class="MTOperatorI" --><!-- doxytag: inherits="MTParserItemI" --><code>#include &lt;<a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a>&gt;</code>
<p>
<p>Inheritance diagram for MTOperatorI:
<p><center><img src="classMTOperatorI.png" usemap="#MTOperatorI_map" border="0" alt=""></center>
<map name="MTOperatorI_map">
<area href="classMTParserItemI.html" alt="MTParserItemI" shape="rect" coords="0,0,131,24">
<area href="classAddOp.html" alt="AddOp" shape="rect" coords="141,112,272,136">
<area href="classAndOp.html" alt="AndOp" shape="rect" coords="141,168,272,192">
<area href="classDivOp.html" alt="DivOp" shape="rect" coords="141,224,272,248">
<area href="classEqualOp.html" alt="EqualOp" shape="rect" coords="141,280,272,304">
<area href="classExpOp.html" alt="ExpOp" shape="rect" coords="141,336,272,360">
<area href="classGreaterEqualThanOp.html" alt="GreaterEqualThanOp" shape="rect" coords="141,392,272,416">
<area href="classGreaterThanOp.html" alt="GreaterThanOp" shape="rect" coords="141,448,272,472">
<area href="classLesserEqualThanOp.html" alt="LesserEqualThanOp" shape="rect" coords="141,504,272,528">
<area href="classLesserThanOp.html" alt="LesserThanOp" shape="rect" coords="141,560,272,584">
<area href="classMinusOp.html" alt="MinusOp" shape="rect" coords="141,616,272,640">
<area href="classModuloOp.html" alt="ModuloOp" shape="rect" coords="141,672,272,696">
<area href="classMultOp.html" alt="MultOp" shape="rect" coords="141,728,272,752">
<area href="classNotEqualOp.html" alt="NotEqualOp" shape="rect" coords="141,784,272,808">
<area href="classNotOp.html" alt="NotOp" shape="rect" coords="141,840,272,864">
<area href="classOrOp.html" alt="OrOp" shape="rect" coords="141,896,272,920">
<area href="classUnaryAddOp.html" alt="UnaryAddOp" shape="rect" coords="141,952,272,976">
<area href="classUnaryMinusOp.html" alt="UnaryMinusOp" shape="rect" coords="141,1008,272,1032">
</map>
<a href="classMTOperatorI-members.html">List of all members.</a><hr><a name="_details"></a><h2>Detailed Description</h2>
Specialized operator evaluator. 
<p>
Implement this interface to create a new operator.
<p>

<p>
Definition at line <a class="el" href="MTParserPublic_8h-source.html#l00262">262</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 <a class="el" href="MTParserPublic_8h.html#a9">EMTOpPrecedence</a>&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTOperatorI.html#a0">getPrecedence</a> ()=0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Return the operator precedence.  <a href="#a0"></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="classMTOperatorI.html#a1">isUnaryOp</a> ()</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Query whether this is an unary operator.  <a href="#a1"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual <a class="el" href="classMTOperatorI.html">MTOperatorI</a> *&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTOperatorI.html#a2">spawn</a> ()=0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Create a new instance of itself.  <a href="#a2"></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="classMTOperatorI.html#a3">isConstant</a> ()</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Indicate whether an item is constant.  <a href="#a3"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="a4"></a><!-- doxytag: member="MTOperatorI::~MTOperatorI" ref="a4" args="()" -->
virtual&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTOperatorI.html#a4">~MTOperatorI</a> ()</td></tr>

</table>
<hr><h2>Member Function Documentation</h2>
<a class="anchor" name="a0"></a><!-- doxytag: member="MTOperatorI::getPrecedence" ref="a0" 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 <a class="el" href="MTParserPublic_8h.html#a9">EMTOpPrecedence</a> MTOperatorI::getPrecedence           </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>
Return the operator precedence. 
<p>
The operator precedence determines the evaluation order of a formula.<p>
<dl compact><dt><b>Returns:</b></dt><dd>The operator precedence</dd></dl>

<p>
Referenced by <a class="el" href="MTParserCompiler_8cpp-source.html#l01046">MTCompilerDefState::onOp()</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a3"></a><!-- doxytag: member="MTOperatorI::isConstant" ref="a3" 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 bool MTOperatorI::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> [inline, 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>
Implements <a class="el" href="classMTParserItemI.html#a4">MTParserItemI</a>.
<p>
Definition at line <a class="el" href="MTParserPublic_8h-source.html#l00297">297</a> of file <a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a1"></a><!-- doxytag: member="MTOperatorI::isUnaryOp" ref="a1" 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 bool MTOperatorI::isUnaryOp           </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>
Query whether this is an unary operator. 
<p>
An unary operator takes only a right argument. For example, the unary minus operator: -x. Most operators are binary like: x+y.<p>
<dl compact><dt><b>Returns:</b></dt><dd>Return true if this operator is a unary operator</dd></dl>

<p>
Definition at line <a class="el" href="MTParserPublic_8h-source.html#l00282">282</a> of file <a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a>.
<p>
Referenced by <a class="el" href="MTParserRegistrar_8cpp-source.html#l00424">MTParserRegistrar::undefineOp()</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a2"></a><!-- doxytag: member="MTOperatorI::spawn" 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 <a class="el" href="classMTOperatorI.html">MTOperatorI</a>* MTOperatorI::spawn           </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>
Create a new instance of itself. 
<p>
This method is used to duplicate a parser object.<p>
<dl compact><dt><b>Returns:</b></dt><dd>A new independant object representing the same operator</dd></dl>
    </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