Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
        private static bool confirmCompatibleTypes(UnitDouble left, UnitDouble right, out UnitConverter convert, out UnitTypes unittype, out Enum unit)
        {
            if (right != null && left.Converter != right.Converter && right.UnitType != UnitTypes.None && left.UnitType != UnitTypes.None)
            {
                convert  = null;
                unittype = UnitTypes.None;
                unit     = null;
                return(false);
            }

            convert  = (left.UnitType == UnitTypes.None && right != null ? right.Converter : left.Converter);
            unittype = (left.UnitType == UnitTypes.None && right != null ? right.UnitType : left.UnitType);
            unit     = (left.UnitType == UnitTypes.None && right != null ? right.Unit : left.Unit);

            return(true);
        }