public IParseTreeValue Evaluate(IParseTreeValue value, string opSymbol, string requestedResultType)
        {
            var isMathOp  = MathOpsUnary.ContainsKey(opSymbol);
            var isLogicOp = LogicOpsUnary.ContainsKey(opSymbol);

            Debug.Assert(isMathOp || isLogicOp);

            var operands = PrepareOperands(new string[] { value.ValueText });

            if (operands.Count == 1)
            {
                if (isMathOp)
                {
                    var mathResult = MathOpsUnary[opSymbol](operands[0]);
                    return(_valueFactory.Create(mathResult.ToString(), requestedResultType));
                }

                //Unary Not (!) operator
                if (!value.TypeName.Equals(Tokens.Boolean) && ParseTreeValue.TryConvertValue(value, out long opValue))
                {
                    var bitwiseComplement = ~opValue;
                    return(_valueFactory.Create(Convert.ToBoolean(bitwiseComplement).ToString(), requestedResultType));
                }
                else if (value.TypeName.Equals(Tokens.Boolean))
                {
                    var logicResult = LogicOpsUnary[opSymbol](operands[0]);
                    return(_valueFactory.Create(logicResult.ToString(), requestedResultType));
                }
            }
            return(_valueFactory.Create($"{opSymbol} {value.ValueText}", requestedResultType));
        }
        public IParseTreeValue CreateDeclaredType(string expression, string declaredTypeName)
        {
            if (expression is null || declaredTypeName is null)
            {
                throw new ArgumentNullException();
            }

            if (ParseTreeValue.TryGetNonPrintingControlCharCompareToken(expression, out string comparableToken))
            {
                var charConversion = new TypeTokenPair(Tokens.String, comparableToken);
                return(ParseTreeValue.CreateValueType(charConversion));
            }

            var goalTypeTokenPair = new TypeTokenPair(declaredTypeName, null);
            var typeToken         = TypeTokenPair.ConformToType(declaredTypeName, expression);

            if (typeToken.HasValue)
            {
                if (LetCoerce.ExceedsValueTypeRange(typeToken.ValueType, typeToken.Token))
                {
                    return(ParseTreeValue.CreateOverflowExpression(expression, declaredTypeName));
                }
                return(ParseTreeValue.CreateValueType(typeToken));
            }
            return(ParseTreeValue.CreateExpression(new TypeTokenPair(declaredTypeName, expression)));
        }
 public IParseTreeValue CreateMismatchExpression(string expression, string declaredTypeName)
 {
     if (expression is null || declaredTypeName is null)
     {
         throw new ArgumentNullException();
     }
     return(ParseTreeValue.CreateMismatchExpression(expression, declaredTypeName));
 }
 public IParseTreeValue CreateValueType(string expression, string declaredTypeName)
 {
     if (expression is null || declaredTypeName is null)
     {
         throw new ArgumentNullException();
     }
     return(ParseTreeValue.CreateValueType(new TypeTokenPair(declaredTypeName, expression)));
 }
Пример #5
0
        public static IParseTreeValue CreateExpression(TypeTokenPair typeToken)
        {
            var ptValue = new ParseTreeValue()
            {
                _typeTokenPair        = typeToken,
                ParsesToConstantValue = false,
            };

            return(ptValue);
        }
Пример #6
0
 public static bool TryLetCoerce(this IParseTreeValue parseTreeValue, string destinationType, out IParseTreeValue newValue)
 {
     newValue = null;
     if (LetCoerce.TryCoerceToken((parseTreeValue.ValueType, parseTreeValue.Token), destinationType, out var valueText))
     {
         newValue = ParseTreeValue.CreateValueType(new TypeTokenPair(destinationType, valueText));
         return(true);
     }
     return(false);
 }
Пример #7
0
        public static IParseTreeValue CreateMismatchExpression(string value, string declaredValueType)
        {
            var ptValue = new ParseTreeValue()
            {
                _typeTokenPair        = new TypeTokenPair(declaredValueType, value),
                ParsesToConstantValue = false,
                IsMismatchExpression  = true
            };

            return(ptValue);
        }
Пример #8
0
        public static IParseTreeValue CreateOverflowExpression(string value, string declaredValueType)
        {
            var ptValue = new ParseTreeValue()
            {
                _typeTokenPair         = new TypeTokenPair(declaredValueType, value),
                ParsesToConstantValue  = false,
                _exceedsValueTypeRange = true
            };

            return(ptValue);
        }
Пример #9
0
        public static IParseTreeValue CreateValueType(TypeTokenPair value)
        {
            if (value.ValueType.Equals(Tokens.Date) || value.ValueType.Equals(Tokens.String))
            {
                return(new ParseTreeValue(value));
            }

            var ptValue = new ParseTreeValue()
            {
                _typeTokenPair        = value,
                ParsesToConstantValue = true,
                IsOverflowExpression  = LetCoerce.ExceedsValueTypeRange(value.ValueType, value.Token),
            };

            return(ptValue);
        }
        public IParseTreeValue CreateDeclaredType(string expression, string declaredTypeName)
        {
            if (expression is null || declaredTypeName is null)
            {
                throw new ArgumentNullException();
            }

            var goalTypeTokenPair = new TypeTokenPair(declaredTypeName, null);
            var typeToken         = TypeTokenPair.ConformToType(declaredTypeName, expression);

            if (typeToken.HasValue)
            {
                if (LetCoerce.ExceedsValueTypeRange(typeToken.ValueType, typeToken.Token))
                {
                    return(ParseTreeValue.CreateOverflowExpression(expression, declaredTypeName));
                }
                return(ParseTreeValue.CreateValueType(typeToken));
            }
            return(ParseTreeValue.CreateExpression(new TypeTokenPair(declaredTypeName, expression)));
        }
        public IParseTreeValue Create(string valueToken)
        {
            if (valueToken is null || valueToken.Equals(string.Empty))
            {
                throw new ArgumentException();
            }

            if (HasTypeHint(valueToken, out string valueType))
            {
                var vToken = valueToken.Remove(valueToken.Length - 1);
                var conformedTypeTokenPair = TypeTokenPair.ConformToType(valueType, vToken);
                if (conformedTypeTokenPair.HasValue)
                {
                    return(ParseTreeValue.CreateValueType(conformedTypeTokenPair));
                }
                return(ParseTreeValue.CreateExpression(new TypeTokenPair(valueType, vToken)));
            }

            if (TypeTokenPair.TryParse(valueToken, out TypeTokenPair result))
            {
                return(ParseTreeValue.CreateValueType(result));
            }
            return(ParseTreeValue.CreateExpression(new TypeTokenPair(null, valueToken)));
        }