Example #1
0
 private static INode GetTree()
 {
     try
     {
         if (_tokens.Count == 0)
         {
             throw new ParseException("Not enough operands");
         }
         if (_tokens[0] is IOperator)
         {
             var oper = (Operator) _tokens[0];
             _tokens.RemoveAt(0);
             INode temp;
             switch (oper.Value)
             {
                 case "+":
                     temp = new Arithmetic.Plus<double>(GetTree(), GetTree());
                     temp.SwitchCoupleOfChildren();
                     return temp;
                 case "-":
                     temp = new Arithmetic.Minus<double>(GetTree(), GetTree());
                     temp.SwitchCoupleOfChildren();
                     return temp;
                 case "∙":
                     temp = new Arithmetic.Product<double>(GetTree(), GetTree());
                     temp.SwitchCoupleOfChildren();
                     return temp;
                 case "/":
                     temp = new Arithmetic.Divide<double>(GetTree(), GetTree());
                     temp.SwitchCoupleOfChildren();
                     return temp;
                 case "^":
                     temp = new Arithmetic.Pow<double>(GetTree(), GetTree());
                     temp.SwitchCoupleOfChildren();
                     return temp;
             }
         }
         if (_tokens[0] is IOperand)
         {
             var operand = (Operand) _tokens[0];
             _tokens.RemoveAt(0);
             if (NodeElementNames.GetVariableNodeNames().IndexOf(operand.Name) != -1)
             {
                 return VariableNode.Make<double>(NodeElementNames.GetVariableNodeNames().IndexOf(operand.Name),
                                                  operand.Name);
             }
             return Constant.Double(Double.Parse(operand.Name));
         }
     } catch(FormatException exp)
     {
         throw new ParseException("Error in parsing. Perhaps you've forgotten the multiplication sign '*'", exp);
     }
     throw new ParseException(string.Format("Unexpected symbol '{0}'", _tokens[0]));
 }
Example #2
0
        public void NodesKeepProperInfo()
        {
            var constant = Constant.Int(1);
            Assert.AreEqual(1, constant.Value);
            Assert.AreEqual(typeof (int), constant.Type);
            Assert.AreEqual(0, constant.Children.Length);

            var variable = VariableNode.Make<int>(0, "x");
            Assert.AreEqual(0, variable.Index);
            Assert.AreEqual(typeof (int), variable.Type);
            Assert.AreEqual(0, variable.Children.Length);

            var op = new Arithmetic.Plus<int>(constant, variable);
            Assert.AreEqual(op.Children.Length, 2);
            Assert.AreEqual(op.Type, typeof (int));
            Assert.AreEqual(op.Parent, null);
            Assert.AreEqual(variable.Parent, op);
            Assert.AreEqual(constant.Parent, op);
        }