Skip to content

Commit

Permalink
refactor/rename/reorg, moved core classes into dotMath.Core
Browse files Browse the repository at this point in the history
  • Loading branch information
bcwood committed May 13, 2014
1 parent 6a8ee7c commit 1cc5df9
Show file tree
Hide file tree
Showing 6 changed files with 672 additions and 409 deletions.
14 changes: 7 additions & 7 deletions Tests/StaticOutputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public void ConstantExpression()
[Test]
public void SignNeg()
{
EqCompiler oComp = new EqCompiler("-4.2", false);
EqCompiler oComp = new EqCompiler("-4.2");

Assert.AreEqual(-4.2, oComp.Calculate());
}

[Test]
public void DivideByZero()
{
EqCompiler oComp = new EqCompiler("4/0", false);
EqCompiler oComp = new EqCompiler("4/0");

double dValue1 = 4;
double dValue2 = 0;
Expand All @@ -58,39 +58,39 @@ public void DivideByZero()
[Test]
public void Addition()
{
EqCompiler oComp = new EqCompiler("2+2", false);
EqCompiler oComp = new EqCompiler("2+2");

Assert.AreEqual(2 + 2, oComp.Calculate());
}

[Test]
public void Multiplication()
{
EqCompiler oComp = new EqCompiler("4*4", false);
EqCompiler oComp = new EqCompiler("4*4");

Assert.AreEqual(4 * 4, oComp.Calculate());
}

[Test]
public void Division()
{
EqCompiler oComp = new EqCompiler("16/2", false);
EqCompiler oComp = new EqCompiler("16/2");

Assert.AreEqual(16 / 2, oComp.Calculate());
}

[Test]
public void Subtraction()
{
EqCompiler oComp = new EqCompiler("10-2", false);
EqCompiler oComp = new EqCompiler("10-2");

Assert.AreEqual(10 - 2, oComp.Calculate());
}

[Test]
public void Exponent()
{
EqCompiler oComp = new EqCompiler("8^2", false);
EqCompiler oComp = new EqCompiler("8^2");

Assert.AreEqual(Math.Pow(8, 2), oComp.Calculate());
}
Expand Down
170 changes: 170 additions & 0 deletions dotMath/Core/BaseClasses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
using System;
using System.Collections;

namespace dotMath.Core
{
/// <summary>
/// This base is the basic building block of all operations, variables, functions, etc.
/// Any object may call to a CValue object asking for it to resolve itself and return it's processed value.
/// </summary>
internal abstract class CValue
{
public abstract double GetValue();
}

/// <summary>
/// Derived from CValue, CVariable implements a named expression variable, holding the name and associated value.
/// This object is accessed when an expression is evaluated or when a user sets a variable value.
/// </summary>
internal class CVariable : CValue
{
private double _value;

/// <summary>
/// Creates an empty CVariable object.
/// </summary>
public CVariable()
{
}

/// <summary>
/// Creates the CVariable object with the assigned value.
/// </summary>
/// <param name="value">double containing the assigned variable value</param>
public CVariable(double value)
{
_value = value;
}

/// <summary>
/// Sets the variable's value.
/// </summary>
/// <param name="value"></param>
public void SetValue(double value)
{
_value = value;
}

/// <summary>
/// Returns the value of the variable.
/// </summary>
/// <returns>double</returns>
public override double GetValue()
{
return _value;
}
}

/// <summary>
/// A CValue-derived class that implements a static numeric value in an expression.
/// </summary>
internal class CNumber : CValue
{
private double _value;

/// <summary>
/// Takes a string representation of a static number and stores it for future retrieval.
/// </summary>
/// <param name="value">string/text representation of a number</param>
public CNumber(string value)
{
_value = Convert.ToDouble(value);
}

/// <summary>
/// Takes a double represenation of a static number and stores it for future retrieval.
/// </summary>
/// <param name="value"></param>
public CNumber(double value)
{
_value = value;
}

/// <summary>
/// Returns the static value.
/// </summary>
/// <returns>double</returns>
public override double GetValue()
{
return _value;
}
}

/// <summary>
/// Provides negative number functionality within an equation.
/// </summary>
internal class CSignNeg : CValue
{
CValue _value;

/// <summary>
/// Grabs onto the assigned CValue object and retains it for processing requested operations.
/// </summary>
/// <param name="value">Child operation this object operates upon.</param>
public CSignNeg(CValue value)
{
_value = value;
}

/// <summary>
/// Performs the negative operation on the child operation and returns the value.
/// </summary>
/// <returns>A double value evaluated and returned with the opposite sign.</returns>
public override double GetValue()
{
return _value.GetValue() * -1;
}
}

/// <summary>
/// A CValue derived class responsible for identifying and implementing operations during the parsing and evaluation processes.
/// </summary>
internal class COperator : CValue
{
private Func<double, double, double> _function;
private CValue _param1;
private CValue _param2;

public COperator(Func<double, double, double> function)
{
_function = function;
}

public void SetParameters(CValue param1, CValue param2)
{
_param1 = param1;
_param2 = param2;
}

public override double GetValue()
{
return _function(_param1.GetValue(), _param2.GetValue());
}
}

/// <summary>
/// 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 compiler's syntax and functionality.
/// </summary>
internal class CFunction : CValue
{
private Func<double, double> _function;
private ArrayList _parameters;

public CFunction(Func<double, double> function)
{
_function = function;
}

public void SetParameters(ArrayList values)
{
_parameters = values;
}

public override double GetValue()
{
return _function(((CValue) _parameters[0]).GetValue());
}
}
}
Loading

0 comments on commit 1cc5df9

Please sign in to comment.