Ejemplo n.º 1
0
        public void ToStringTest()
        {
            var lexem = new Lexem {
                Type = LexemType.Variable, Value = "Data"
            };
            var result = lexem.ToString();

            Assert.AreEqual("Variable : Data", result);
        }
Ejemplo n.º 2
0
        public static object Calculate(Lexem oper, Lexem leftValue, Lexem rightValue, ref LexemType lt)
        {
            var t = PickCorrectType(leftValue, rightValue);

            switch (t)
            {
            case TypeCode.Double:
                lt = LexemType.Number;
                switch (oper.Content.ToString())
                {
                case "*":
                    return(leftValue.ToDouble() * rightValue.ToDouble());

                case "/":
                    return(leftValue.ToDouble() / rightValue.ToDouble());

                case "+":
                    return(leftValue.ToDouble() + rightValue.ToDouble());

                case "-":
                    return(leftValue.ToDouble() - rightValue.ToDouble());

                case ">":
                    lt = LexemType.Boolean;
                    return(leftValue.ToDouble() > rightValue.ToDouble());

                case "<":
                    lt = LexemType.Boolean;
                    return(leftValue.ToDouble() < rightValue.ToDouble());

                case "!=":
                    lt = LexemType.Boolean;
                    return(leftValue.ToDouble() != rightValue.ToDouble());

                case "==":
                    lt = LexemType.Boolean;
                    return(leftValue.ToDouble() == rightValue.ToDouble());

                case ">=":
                    lt = LexemType.Boolean;
                    return(leftValue.ToDouble() >= rightValue.ToDouble());

                case "<=":
                    lt = LexemType.Boolean;
                    return(leftValue.ToDouble() <= rightValue.ToDouble());

                case "=":
                    Storage.SetVariableValue(leftValue, rightValue.ToDouble());
                    if (leftValue.Content is Variable)
                    {
                        return((leftValue.Content as Variable).Value);
                    }
                    return(Storage.GetVariable(leftValue.Content.ToString()));
                }
                break;

            case TypeCode.String:
                lt = LexemType.String;
                switch (oper.Content.ToString())
                {
                case "+":
                    return(leftValue.ToString() + rightValue.ToString());

                case "!=":
                    lt = LexemType.Boolean;
                    return(leftValue.ToString() != rightValue.ToString());

                case "==":
                    lt = LexemType.Boolean;
                    return(leftValue.ToString() == rightValue.ToString());

                case "=":
                    Storage.SetVariableValue(leftValue, rightValue.ToString());
                    if (leftValue.Content is Variable)
                    {
                        return((leftValue.Content as Variable).Value);
                    }
                    return(Storage.GetVariable(leftValue.Content.ToString()));

                default:
                    throw new SyntaxException("Неверная операция", oper);
                }

            case TypeCode.Boolean:
                lt = LexemType.Boolean;
                switch (oper.Content.ToString())
                {
                case "||":
                    return(leftValue.ToBoolean() || rightValue.ToBoolean());

                case "&&":
                    return(leftValue.ToBoolean() && rightValue.ToBoolean());

                case "==":
                    return(leftValue.ToBoolean() == rightValue.ToBoolean());

                case "!=":
                    return(leftValue.ToBoolean() != rightValue.ToBoolean());

                case "=":
                    Storage.SetVariableValue(leftValue, rightValue.ToBoolean());
                    if (leftValue.Content is Variable)
                    {
                        return((leftValue.Content as Variable).Value);
                    }
                    return(Storage.GetVariable(leftValue.Content.ToString()));

                default:
                    throw new SyntaxException("Неверная операция", oper);
                }
            }

            throw new SyntaxException("Ошибка вычисления оператора", oper);
        }
Ejemplo n.º 3
0
 protected override void OnElement(Lexem lexem)
 {
     if (lexem is LexemValue)
         _lexems.Add(lexem);
     else if (lexem is LexemVariable)
         _lexems.Add(lexem);
     else if (lexem is LexemFunction)
         _lexems.Add(lexem);
     else
         throw new FormatException($"Expression Element not recognized: {lexem.ToString()}");
 }