コード例 #1
0
        private bool TryParseDouble(string expression, out Expression result)
        {
            result = null;
            var match = DoubleRegex.Match(expression);

            if (!match.Success)
            {
                return(false);
            }

            var value = match.Groups[0].Value;

            float @float;

            if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out @float))
            {
                result = new Expression.Constant {
                    Value = $"{value}f", TypeName = "float"
                };
                return(true);
            }

            double @double;

            if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out @double))
            {
                result = new Expression.Constant {
                    Value = expression, TypeName = "double"
                };
                return(true);
            }

            return(false);
        }
コード例 #2
0
        private bool TryParseId(string expression, out Expression result)
        {
            result = null;
            var match = IdRegex.Match(expression);

            if (!match.Success)
            {
                return(false);
            }

            var        value = match.Groups[0].Value;
            string     macroExpression;
            Expression referenceExpression;

            if (!_macroMap.TryGetValue(value, out macroExpression) || !TryParse(macroExpression, out referenceExpression))
            {
                return(false);
            }
            result = new Expression.Constant {
                Value = expression, TypeName = referenceExpression.TypeName
            };
            return(true);
        }
コード例 #3
0
        private bool TryParseInt(string expression, out Expression result)
        {
            result = null;
            var matchHex = IntHexRegex.Match(expression);

            if (matchHex.Success)
            {
                var minus = matchHex.Groups[1].Value == "-" ? -1 : 1;
                var value = matchHex.Groups[2].Value;
                var sign  = matchHex.Groups[3].Value.ToLower();
                var type  = matchHex.Groups[4].Value.ToLower();

                int @int;
                if (int.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out @int) && minus * @int != int.MinValue)
                {
                    result = minus == 1
                        ? new Expression.Constant {
                        Value = $"0x{value}", TypeName = sign + "int"
                    }
                        : new Expression.Constant {
                        Value = $"0x{value}", TypeName = sign + "long"
                    };
                    return(true);
                }

                long @long;
                if (long.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out @long))
                {
                    result = new Expression.Constant {
                        Value = $"0x{value}", TypeName = sign + "long"
                    };
                    return(true);
                }

                decimal @decimal;
                if (decimal.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out @decimal))
                {
                    result = new Expression.Constant {
                        Value = $"0x{value}", TypeName = sign + "decimal"
                    };
                    return(true);
                }
            }
            else
            {
                var matchDecimal = IntDecimalRegex.Match(expression);
                if (!matchDecimal.Success)
                {
                    return(false);
                }

                var value = matchDecimal.Groups[1].Value;
                var sign  = matchDecimal.Groups[2].Value.ToLower();
                var type  = matchHex.Groups[4].Value.ToLower();

                int @int;
                if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out @int) && @int != int.MinValue)
                {
                    result = new Expression.Constant {
                        Value = expression, TypeName = sign + "int"
                    };
                    return(true);
                }

                long @long;
                if (long.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out @long))
                {
                    result = new Expression.Constant {
                        Value = expression, TypeName = sign + "long"
                    };
                    return(true);
                }

                decimal @decimal;
                if (decimal.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out @decimal))
                {
                    result = new Expression.Constant {
                        Value = expression, TypeName = sign + "decimal"
                    };
                    return(true);
                }
            }
            return(false);
        }