private void LoadOperatorsInternalTest(Dictionary <String, OperatorDescriptor> operators) { Assert.AreEqual(2, operators.Count); OperatorDescriptor addOperator = operators["+"]; OperatorDescriptor substractOperator = operators["-"]; Object result = addOperator.Invoke(25.0, 34.0); Assert.AreEqual(59, result); result = substractOperator.Invoke(25.0, 34.0); Assert.AreEqual(-9, result); }
private void LoadOperators() { Dictionary <String, OperatorDescriptor> operators = OperatorsCache.Operators; OperatorDescriptor[] operatorsToSort = new OperatorDescriptor[operators.Values.Count]; operators.Values.CopyTo(operatorsToSort, 0); Array.Sort <OperatorDescriptor>(operatorsToSort, new OperatorDescriptorComparer()); foreach (OperatorDescriptor descriptor in operatorsToSort) { AddOperator(descriptor); } _operatorNode.Expand(); }
public void DescriptorTest() { Dictionary <String, OperatorDescriptor> operators = OperatorLoader.LoadOperatorsFromType(typeof(Operators)); OperatorDescriptor addOperator = operators["+"]; Assert.AreEqual("Add", addOperator.Name); Assert.AreEqual("+", addOperator.StringRepresentation); ParameterInfo[] paramsInfo = addOperator.GetInputParameters(); Assert.AreEqual(addOperator.InputParameterCount, paramsInfo.Length); Assert.AreEqual(2, addOperator.InputParameterCount); OperatorDescriptor substractOperator = operators["-"]; Assert.AreEqual("Substract", substractOperator.Name); Assert.AreEqual("-", substractOperator.StringRepresentation); }
private static Node RpnToNode(string rpn) { string[] tokens = rpn.Split(Interpreter.WhiteSpaceChar); System.Collections.Generic.Stack <Node> values = new System.Collections.Generic.Stack <Node>(); for (int tokenIndex = 0; tokenIndex < tokens.Length; ++tokenIndex) { string token = tokens[tokenIndex]; if (Interpreter.operators.ContainsKey(token)) { OperatorDescriptor opeDesc = Interpreter.operators[token]; if (opeDesc.NodeType != null) { if (opeDesc.NodeType.IsSubclassOf(typeof(ZeroNode))) { System.Reflection.ConstructorInfo constructorInfo = opeDesc.NodeType.GetConstructor(new System.Type[0]); if (constructorInfo != null) { Node node = (Node)constructorInfo.Invoke(new object[0]); values.Push(node); } } if (opeDesc.NodeType.IsSubclassOf(typeof(UnaryNode))) { System.Reflection.ConstructorInfo constructorInfo = opeDesc.NodeType.GetConstructor(new [] { typeof(Node) }); if (constructorInfo != null) { Node node = (Node)constructorInfo.Invoke(new object[] { values.Pop() }); values.Push(node); } } if (opeDesc.NodeType.IsSubclassOf(typeof(BinaryNode))) { System.Reflection.ConstructorInfo constructorInfo = opeDesc.NodeType.GetConstructor(new [] { typeof(Node), typeof(Node) }); if (constructorInfo != null) { Node right = values.Pop(); Node left = values.Pop(); Node node = (Node)constructorInfo.Invoke(new object[] { left, right }); values.Push(node); } } } } else { double value; if (double.TryParse(token, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture.NumberFormat, out value)) { Node node = new ValueNode(value); values.Push(node); } else { Node node = new VarNode(token); values.Push(node); } } } if (values.Count != 1) { throw new System.InvalidOperationException("Cannot calculate formula"); } return(values.Pop()); }