📄 eqcompiler.cs
字号:
/// <summary>
/// COperator class: A CValue derived class responsible for identifying
/// and implementing operations during the parsing and evaluation processes.
/// </summary>
private abstract class COperator : CValue
{
/// <summary>
/// IsMatch(): accepts an operation token and identifies if the implemented
/// operator class is responsible for it. This allows for multiple operators
/// to represent a given operation (i.e. != and <> both represent the "not-equal" case.
/// </summary>
/// <param name="tkn">Parser.Token containing the operator in question</param>
/// <returns>bool returning true if the object is reponsible for implementing the operator at hand.</returns>
public abstract bool IsMatch( Parser.Token tkn);
/// <summary>
/// Factory(CValue, CValue): responsible for providing an evaluation-time object that
/// holds onto the CValue objects it is responsible for operating on.
/// </summary>
/// <param name="arg1">First CValue object to operate on</param>
/// <param name="arg2">Second CValue object to operate on</param>
/// <returns>"Evaluation time" COperator object</returns>
public abstract COperator Factory( CValue arg1, CValue arg2);
/// <summary>
/// CheckParms( string, CValue, CValue): Helper function that verifies the two arguments
/// are non-null objects. If not, an exception is thrown.
/// </summary>
/// <param name="sOp">string representing the operation.</param>
/// <param name="arg1">CValue object representing the first operation</param>
/// <param name="arg2">CValue object representing the second oepration</param>
protected void CheckParms(string sOp, CValue arg1, CValue arg2)
{
if( arg1 == null || arg2 == null )
throw new ApplicationException("Missing expression on " + sOp + " operator.");
}
}
/// <summary>
/// CFunction class: A CValue derived class that provdes the base for all functions
/// implemented in the compiler. This class also allows for external clients to
/// create and register functions with the compiler - thereby extending the compilers
/// syntax and functionality.
/// </summary>
public abstract class CFunction : CValue
{
/// <summary>
/// GetFunction(): returns the function name as a string
/// </summary>
/// <returns>string</returns>
public abstract string GetFunction();
/// <summary>
/// SetValue(): Accepts an array of CValue objects that represent the parameters in
/// the function.
/// </summary>
/// <param name="alValues">ArrayList of CValue parameter objects.</param>
public abstract void SetValue( ArrayList alValues );
/// <summary>
/// CreateInstance( ArrayList ): Requests that an evaluation-time object be created
/// that performs the operation on a set of CValue objects.
/// </summary>
/// <param name="alValues">ArrayList of CValue parameter objects.</param>
/// <returns>Returns a CFunction object that references parameters for evaluation purposes.</returns>
public abstract CFunction CreateInstance( ArrayList alValues );
/// <summary>
/// CheckParms(ArrayList, int): Helper function that accepts an array list and insures an appropriate
/// number of CValue objects have been passed. If not, an ApplicationException is thrown.
/// </summary>
/// <param name="alValues">ArrayList of CValue-based objects representing parameters to the function</param>
/// <param name="iRequiredValueCount">integer: required parameter count</param>
protected void CheckParms( ArrayList alValues, int iRequiredValueCount )
{
if( alValues.Count != iRequiredValueCount )
{
string sMsg = string.Format("Invalid parameter count. Function '" + GetFunction() + "' requires {0} parameter(s).", iRequiredValueCount );
throw new ApplicationException(sMsg);
}
}
/// <summary>
/// CheckParms(ArrayList, int, int): Helper function that accepts an array list and insures an appropriate
/// number (min and/or max) of CValue objects have been passed. If not an ApplicationException is thrown.
/// </summary>
/// <param name="alValues">ArrayList of CValue object that have been passed by the compiler.</param>
/// <param name="iMinReq">int value indicating a minimum number of parameters. -1 if no minimum exists</param>
/// <param name="iMaxReq">int value indicating a maximum number of parameters. -1 if no maximum exists</param>
protected void CheckParms(ArrayList alValues, int iMinReq, int iMaxReq )
{
if( iMinReq > -1 )
{
if( iMinReq > alValues.Count )
{
string sMsg = string.Format("Invalid parameter count. Function '" + GetFunction() + "' requires a minimum of {0} parameter(s).", iMinReq );
throw new ApplicationException(sMsg);
}
}
if( iMaxReq > -1 )
{
if( iMaxReq < alValues.Count )
{
string sMsg = string.Format("Invalid parameter count. Function '" + GetFunction() + "' is limited to a maximum of {0} parameter(s).", iMaxReq );
throw new ApplicationException(sMsg);
}
}
}
}
#endregion
#region Operators
/// <summary>
/// CAdd class: Implements the Add(+) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CAdd : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CAdd()
{
}
public CAdd( CValue arg1, CValue arg2 )
{
CheckParms("+", arg1, arg2);
m_arg1 = arg1;
m_arg2 = arg2;
}
public override double GetValue()
{
return m_arg1.GetValue() + m_arg2.GetValue();
}
public override bool IsMatch( Parser.Token tkn)
{
return ( tkn.ToString() == "+" );
}
public override COperator Factory( CValue arg1, CValue arg2)
{
return new CAdd(arg1, arg2);
}
}
/// <summary>
/// CSubtract class: Implements the Subtract(-) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CSubtract : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CSubtract()
{
}
public CSubtract( CValue arg1, CValue arg2 )
{
CheckParms("-", arg1, arg2);
m_arg1 = arg1;
m_arg2 = arg2;
}
public override double GetValue()
{
return m_arg1.GetValue() - m_arg2.GetValue();
}
public override bool IsMatch( Parser.Token tkn)
{
return tkn.ToString() == "-";
}
public override COperator Factory( CValue arg1, CValue arg2)
{
return new CSubtract( arg1, arg2 );
}
}
/// <summary>
/// CLessThan class: Implements the LessThan(<) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CLessThan : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CLessThan()
{
}
public CLessThan( CValue arg1, CValue arg2 )
{
CheckParms("<", arg1, arg2);
m_arg1 = arg1;
m_arg2 = arg2;
}
public override double GetValue()
{
if( m_arg1.GetValue() < m_arg2.GetValue() )
return 1;
else
return 0;
}
public override bool IsMatch( Parser.Token tkn)
{
return ( tkn.ToString() == "<" );
}
public override COperator Factory( CValue arg1, CValue arg2)
{
return new CLessThan(arg1, arg2);
}
}
/// <summary>
/// COr class: Implements the boolean Or(||) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class COr : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public COr()
{
}
public COr( CValue arg1, CValue arg2 )
{
CheckParms("||", arg1, arg2);
m_arg1 = arg1;
m_arg2 = arg2;
}
public override double GetValue()
{
if( m_arg1.GetValue()!= 0 || m_arg2.GetValue() != 0 )
return 1;
else
return 0;
}
public override bool IsMatch( Parser.Token tkn)
{
return (tkn.ToString() == "||" );
}
public override COperator Factory( CValue arg1, CValue arg2)
{
return new COr( arg1, arg2);
}
}
/// <summary>
/// CAnd class: Implements the boolean And(&&) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CAnd : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CAnd()
{
}
public CAnd( CValue arg1, CValue arg2 )
{
CheckParms("&&", arg1, arg2);
m_arg1 = arg1;
m_arg2 = arg2;
}
public override double GetValue()
{
if( m_arg1.GetValue() != 0 && m_arg2.GetValue() != 0 )
return 1;
else
return 0;
}
public override bool IsMatch( Parser.Token tkn)
{
return (tkn.ToString() == "&&" );
}
public override COperator Factory( CValue arg1, CValue arg2)
{
return new CAnd(arg1, arg2);
}
}
/// <summary>
/// CEqual class: Implements the binary Equal(==) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CEqual : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CEqual()
{
}
public CEqual( CValue arg1, CValue arg2 )
{
CheckParms("= or ==", arg1, arg2);
m_arg1 = arg1;
m_arg2 = arg2;
}
public override double GetValue()
{
if( m_arg1.GetValue() == m_arg2.GetValue() )
return 1;
else
return 0;
}
public override bool IsMatch( Parser.Token tkn)
{
return (tkn.ToString() == "=" || tkn.ToString() == "==");
}
public override COperator Factory( CValue arg1, CValue arg2)
{
return new CEqual(arg1, arg2);
}
}
/// <summary>
/// CNotEqual class: Implements the NotEqual(<>) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CNotEqual : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CNotEqual()
{
}
public CNotEqual( CValue arg1, CValue arg2 )
{
CheckParms("<> or !=", arg1, arg2);
m_arg1 = arg1;
m_arg2 = arg2;
}
public override double GetValue()
{
if( m_arg1.GetValue() != m_arg2.GetValue() )
return 1;
else
return 0;
}
public override bool IsMatch( Parser.Token tkn)
{
return (tkn.ToString() == "!=" || tkn.ToString() == "<>" );
}
public override COperator Factory( CValue arg1, CValue arg2)
{
return new CNotEqual(arg1, arg2);
}
}
/// <summary>
/// CGreaterThan class: Implements the Greater Than(>) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CGreaterThan : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CGreaterThan()
{
}
public CGreaterThan( CValue arg1, CValue arg2 )
{
CheckParms(">", arg1, arg2);
m_arg1 = arg1;
m_arg2 = arg2;
}
public override double GetValue()
{
if( m_arg1.GetValue() > m_arg2.GetValue() )
return 1;
else
return 0;
}
public override bool IsMatch( Parser.Token tkn)
{
return (tkn.ToString() == ">");
}
public override COperator Factory( CValue arg1, CValue arg2)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -