using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace csmic { /// /// Represents a user defined function that can be executed by the interpreter. /// public class InterpretedFunction { #region Members /// /// The name of the function. /// private string name; /// /// The number of expected arguments. /// private int numExpectedArguments; /// /// The set of instructions to be passed to the internal inputInterpreter. /// private MacroOperation script; /// /// A set of arguments used in computation of the function. /// private InterpretedFunctionArgument[] arguments; /// /// The internal macro builder used for computation. /// private MacroBuilder macroFunction; /// /// The internal input inputInterpreter that macro builder will use for computation. /// private InputInterpreter interpreter; #endregion #region Constructor /// /// Creates a new interpreted function. /// /// The name of the fuction. /// The node to be used in computation. /// A set of argument names to be used in computation. internal InterpretedFunction(string name, MacroOperation script, params string[] args) { this.name = name; this.script = script; this.macroFunction = null; this.arguments = new InterpretedFunctionArgument[args.Length]; this.interpreter = new InputInterpreter(); this.numExpectedArguments = args.Length; for (int i = 0; i < args.Length; i++) { this.arguments[i] = new InterpretedFunctionArgument(args[i], 0); } } #endregion #region Properties /// /// Gets the name of the function. /// public string Name { get { return this.name; } } /// /// Gets the number of expected arguments for the function. /// public int NumExpectedArguments { get { return this.numExpectedArguments; } } #endregion #region Public Methods /// /// Computes the value of the function. /// /// The arguments used for computation. /// The decimal value computed by the function. public string Compute(params decimal[] args) { if (args.Length != this.numExpectedArguments) { return "0"; } if (args.Length == this.arguments.Length) { for (int i = 0; i < args.Length; i++) { this.arguments[i].Value = args[i]; } } foreach (InterpretedFunctionArgument argument in this.arguments) { this.interpreter.Interpret(string.Format("{0} :: {1}", argument.Name, argument.Value)); } this.macroFunction = new MacroBuilder(this.script, this.interpreter); return this.macroFunction.FinalOutput; } #endregion /// /// Because a function's internal pattern may be different, we must manually check to see if the function /// names are the same. /// /// The object to test. /// True if the functions are the same. public override bool Equals(object obj) { InterpretedFunction fun = obj as InterpretedFunction; if(fun != null) { return fun.name == this.name; } return false; } /// Serves as a hash function for a particular type. /// A hash code for the current . public override int GetHashCode() { return this.script.GetHashCode() & this.name.GetHashCode(); } } }