Exemple #1
0
        public IMathNode CreateUniLeafNode(string expression, MathParser parser)
        {
            var parts = this.ParsePotentialUniLeaf(expression);

            if (parts.Length == 0)
            {
                return(null);
            }

            switch (parts[0])
            {
            case "log":
                if (parts.Count() != 3)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new LogUniLeafMathNode(parser.Parse(parts[2]), parser.Parse(parts[1])));

            case "sin":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new SinUniLeafMathNode(parser.Parse(parts[1])));

            case "cos":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new CosUniLeafMathNode(parser.Parse(parts[1])));

            case "asin":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new ASinUniLeafMathNode(parser.Parse(parts[1])));

            case "acos":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new ACosUniLeafMathNode(parser.Parse(parts[1])));

            case "cosh":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new CoshUniLeafMathNode(parser.Parse(parts[1])));

            case "tan":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new TanUniLeafMathNode(parser.Parse(parts[1])));

            case "atan":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new ATanUniLeafMathNode(parser.Parse(parts[1])));

            case "sinh":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new SinhUniLeafMathNode(parser.Parse(parts[1])));

            case "log2":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new LogUniLeafMathNode(new NumericMathNode(2.0M), parser.Parse(parts[1])));

            case "log10":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new LogUniLeafMathNode(new NumericMathNode(10.0M), parser.Parse(parts[1])));

            case "sqrt":
                if (parts.Count() != 2)
                {
                    throw new InvalidArgumentAmount(parts[0]);
                }

                return(new SqrtUniLeafMathNode(parser.Parse(parts[1])));
            }
            return(null);
        }
Exemple #2
0
        public IMathNode TryParse(string expression, MathParser parser)
        {
            var possiblyhex = this.isSpeicalFormattedNumber(expression);

            if (possiblyhex != null)
            {
                return(possiblyhex);
            }

            var results = unitParser.Match(expression);

            if (false == results.Success)
            {
                return(null);
            }

            var   expr = results.Groups["value"];
            var   to   = results.Groups["to"];
            var   from = results.Groups["from"];
            Group actionable;


            if (expr.Value == "\"" && from.Value.Last() == '"')
            {
                return(null);
            }

            // Continue parsing the left side of this Unit declaration
            // If this is a conversion, this will come back here until there
            // are no more units left to parse through

            // Check for " " to indicate a string literal.
            //if (expr.Value.Contains('"'))
            //{
            //    // Otherwise, we are parsing a complex expression
            //    var valu = parser.Parse(expr.Value);
            //}
            //else
            //{
            //    // Otherwise, we are parsing a complex expression
            //    var valu = parser.Parse(expr.Value);
            //}

            // Otherwise, we are parsing a complex expression
            var valu = parser.Parse(expr.Value);


            // If we are finally inside the expression of expr units
            if (from.Success)
            {
                actionable = from;
            }
            else
            {
                actionable = to;
            }

            UnitConverter converter = this.GetUnitConverter(actionable.Value);

            if (converter == null)
            {
                if (actionable.Value.ToLower() != "string" && actionable.Value.ToLower() != "str")
                {
                    throw new InvalidUnitTypeException(actionable.Value);
                }

                // Convert the int/long/hex value into a string/char.
                return(new StringMathNode(valu));
            }


            // Determines things like DistanceImperical, DistanceMetric, etc
            Enum tmpEnum;

            converter.GetUnitFromString(actionable.Value, out tmpEnum);
            var hold = tmpEnum.GetAttributeOfType <UnitTypeAttribute>().FirstOrDefault();

            if (hold != null)
            {
                //valu.UnitType = hold.UnitType;
            }

            return(new UnitUniLeafMathNode(valu, (hold == null ? UnitTypes.None : hold.UnitType), converter, tmpEnum));
        }