📄 eqcompiler.cs
字号:
{
return new CGreaterThan(arg1, arg2);
}
}
/// <summary>
/// CGreaterThanEq class: Implements the Greater Than or Equal To(>=) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CGreaterThanEq : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CGreaterThanEq()
{
}
public CGreaterThanEq( 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 CGreaterThanEq(arg1, arg2);
}
}
/// <summary>
/// CLessThanEq class: Implements the Less Than or Equal To(<=) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CLessThanEq : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CLessThanEq()
{
}
public CLessThanEq( 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 CLessThanEq(arg1, arg2);
}
}
/// <summary>
/// CMultiply class: Implements the Multiplication(*) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CMultiply : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CMultiply()
{
}
public CMultiply( 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 CMultiply(arg1, arg2);
}
}
/// <summary>
/// CDivide class: Implements the Division(/) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CDivide : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CDivide()
{
}
public CDivide( 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 CDivide(arg1, arg2);
}
}
/// <summary>
/// CPower class: Implements the Power(^) operation. Refer to COperator base class
/// for a description of the methods.
/// </summary>
private class CPower : COperator
{
private CValue m_arg1 = null;
private CValue m_arg2 = null;
public CPower()
{
}
public CPower( CValue arg1, CValue arg2 )
{
CheckParms("^", arg1, arg2);
m_arg1 = arg1;
m_arg2 = arg2;
}
public override double GetValue()
{
return Math.Pow(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 CPower(arg1, arg2);
}
}
#endregion
#region Helper Functions
/// <summary>
/// CheckParms( Parser.Token, CValue, CValue) - This method makes certain the arguments are non-null
/// </summary>
/// <param name="oToken">Currently processed Parser.Token object</param>
/// <param name="arg1">CValue argument 1</param>
/// <param name="arg2">CValue argument 2</param>
private void CheckParms( Parser.Token oToken, CValue arg1, CValue arg2 )
{
if( arg1==null || arg2 == null )
throw new ApplicationException( "Argument not supplied near " + oToken.ToString() + " operation.");
}
/// <summary>
/// CheckParms( Parser.Token, CValue) - This method makes certain the single argument is non-null.
/// Raises an exception if it is.
/// </summary>
/// <param name="oToken">Parser.Token object</param>
/// <param name="arg1">CValue argument</param>
private void CheckParms( Parser.Token oToken, CValue arg1 )
{
if( arg1 == null )
throw new ApplicationException("Argument not supplied near " + oToken.ToString() + " operation.");
}
/// <summary>
/// InitFunctions(): Creates all operation functions recognized by the compiler.
/// </summary>
private void InitFunctions()
{
m_aOps = new COperator[11];
m_aOps[0] = new CAdd();
m_aOps[1] = new CSubtract();
m_aOps[2] = new CMultiply();
m_aOps[3] = new CDivide();
m_aOps[4] = new CGreaterThan();
m_aOps[5] = new CGreaterThanEq();
m_aOps[6] = new CLessThan();
m_aOps[7] = new CLessThanEq();
m_aOps[8] = new CEqual();
m_aOps[9] = new CNotEqual();
m_aOps[10] = new CPower();
}
/// <summary>
/// PositionNextToken(): Manipulates the current Token position forward in the chain of tokens
/// discovered by the parser.
/// </summary>
private void PositionNextToken()
{
if( m_currentToken == null)
{
if( !m_enumTokens.MoveNext() )
throw new ApplicationException( "Invalid equation." );
m_nextToken = (Parser.Token)m_enumTokens.Current;
}
m_currentToken = m_nextToken;
if( !m_enumTokens.MoveNext() )
m_nextToken = new Parser.Token();
else
m_nextToken = (Parser.Token)m_enumTokens.Current;
}
/// <summary>
/// GetVariableByName(string) : This method returns the variable associated with the
/// provided name string.
/// </summary>
/// <param name="sVarName">string variable name</param>
/// <returns>CVariable object mapped to the passed variable name</returns>
private CVariable GetVariableByName( string sVarName )
{
if( m_slVariables == null )
m_slVariables = new SortedList();
int iIdx = m_slVariables.IndexOfKey(sVarName);
if( iIdx > -1 )
return (CVariable)m_slVariables.GetByIndex( iIdx );
CVariable oVar = new CVariable(sVarName);
m_slVariables.Add( sVarName, oVar );
return oVar;
}
#endregion
/// <summary>
/// VariableCount property: This property reports the current
/// variable count. It is valid after a 'Compile()' function is executed.
/// </summary>
public int VariableCount { get {return m_slVariables.Count;}}
/// <summary>
/// SetVariable( string, double): Sets the object mapped to the string variable name
/// to the double value passed.
/// </summary>
/// <param name="sVarName">Variable Name</param>
/// <param name="dValue">New Value for variable</param>
public void SetVariable( string sVarName, double dValue )
{
CVariable oVar = GetVariableByName( sVarName );
oVar.SetValue( dValue );
}
/// <summary>
/// GetVariableList(): returns a string array containing all the variables that
/// have been found by the compiler.
/// </summary>
/// <returns>string array of current variable names</returns>
public string[] GetVariableList()
{
if( m_slVariables.Count == 0)
return null;
string[] asVars = new string[m_slVariables.Count];
IEnumerator enu = m_slVariables.GetKeyList().GetEnumerator();
string sValue = "";
int iPos = 0;
while( enu.MoveNext() )
{
sValue = (string)enu.Current;
asVars[iPos] = sValue;
iPos++;
}
return asVars;
}
/// <summary>
/// EqCompiler() constructor: creates the compiler object with an empty function that returns '0' if evaluated.
/// </summary>
public EqCompiler( bool bIncludeStandardFunctions )
{
SetFunction( "0" );
if( bIncludeStandardFunctions )
CFunctionLibrary.AddFunctions(this);
}
/// <summary>
/// EqCompiler(string) constructor: creates the compiler object and sets the current function to the string passed
/// </summary>
/// <param name="sEquation"></param>
public EqCompiler( string sEquation, bool bIncludeStandardFunctions )
{
SetFunction( sEquation );
if( bIncludeStandardFunctions )
CFunctionLibrary.AddFunctions( this );
}
/// <summary>
/// SetFunction(string): Sets the current function to a passed string.
/// </summary>
/// <param name="sEquation">string representing the function being used</param>
public void SetFunction( string sEquation )
{
m_currentToken = null;
m_nextToken = null;
m_sEquation = sEquation;
m_Function = null;
InitFunctions();
}
/// <summary>
/// Compile(): This function kicks off the process to tokenize the function
/// and compile the resulting token set into a runnable form.
/// </summary>
public void Compile()
{
Parser oParser = new Parser(m_sEquation );
m_enumTokens = oParser.GetTokenEnumerator();
PositionNextToken();
m_Function = Relational();
}
/// <summary>
/// Calculate(): Calls into the runnable function set to evaluate the function and returns the result.
/// </summary>
/// <returns>double value evaluation of the function in its current state</returns>
public double Calculate()
{
if( m_Function == null)
Compile();
return m_Function.GetValue();
}
/// <summary>
/// AddFunction(CFunction): This member accepts a function object and
/// adds it to the compilers set of functions.
/// </summary>
/// <param name="oFunc">CFunction object that implements a functionality extension for the compiler.</param>
public void AddFunction(CFunction oFunc)
{
m_slFunctions.Add( oFunc.GetFunction(), oFunc );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -