Пример #1
0
        public void RealIntTest()
        {
            var context = new Context();
            var interpretator = new RealInterpretator(context);

            var expressionSting = "14.45+142";
            var result = interpretator.Run(expressionSting);
            Assert.AreEqual(14.45 + 142, result, 0.001);

            expressionSting = "124-48.157";
            result = interpretator.Run(expressionSting);
            Assert.AreEqual(124 - 48.157, result, 0.001);

            expressionSting = "127*0.1257";
            result = interpretator.Run(expressionSting);
            Assert.AreEqual(127 * 0.1257, result, 0.001);

            expressionSting = "1584.2251/48";
            result = interpretator.Run(expressionSting);
            Assert.AreEqual(1584.2251 / 48, result, 0.001);

            expressionSting = "(58.25/41*59.23+6-48.26)/6+458.157/45.2655*59882*(124.157+52*(124.264+263*48.65)+1.15-58)";
            result = interpretator.Run(expressionSting);
            Assert.AreEqual((58.25 / 41 * 59.23 + 6 - 48.26) / 6 + 458.157 / 45.2655 * 59882 * (124.157 + 52 * (124.264 + 263 * 48.65) + 1.15 - 58), result, 0.001);
        }
        private void E()
        {
            if (expression.StartsWith("int[]"))
            {
                expression = expression.SkipWord("int[]");

                N();

                var intArrayInterpretator = new IntArrayInterpretator(context);
                var expressionString = Regex.Match(expression, "^.+;").Value;
                var valueVariable = intArrayInterpretator.Run(expression.Remove(expressionString.Length - 1, 1));

                expression = expression.SkipWord(expressionString);

                var variableName = (string) stack.Pop();
                context.AddVariable(new VariableExp(variableName, valueVariable));

                return;
            }

            if (expression.StartsWith("real[]"))
            {
                expression = expression.SkipWord("real[]");

                N();

                var realArrayInterpretator = new RealArrayInterpretator(context);
                var expressionString = Regex.Match(expression, "^.+;").Value;
                var valueVariable = realArrayInterpretator.Run(expression.Remove(expressionString.Length - 1, 1));

                expression = expression.SkipWord(expressionString);

                var variableName = (string)stack.Pop();
                context.AddVariable(new VariableExp(variableName, valueVariable));

                return;
            }

            if (expression.StartsWith("bool[]"))
            {
                expression = expression.SkipWord("bool[]");

                N();

                var boolArrayInterpretator = new BoolArrayInterpretator(context);
                var expressionString = Regex.Match(expression, "^.+;").Value;
                var valueVariable = boolArrayInterpretator.Run(expression.Remove(expressionString.Length - 1, 1));

                expression = expression.SkipWord(expressionString);

                var variableName = (string)stack.Pop();
                context.AddVariable(new VariableExp(variableName, valueVariable));

                return;
            }

            if (expression.StartsWith("string[]"))
            {
                expression = expression.SkipWord("string[]");

                N();

                var stringArrayInterpretator = new StringArrayInterpretator(context);
                var expressionString = Regex.Match(expression, "^.+;").Value;
                var valueVariable = stringArrayInterpretator.Run(expression.Remove(expressionString.Length - 1, 1));

                expression = expression.SkipWord(expressionString);

                var variableName = (string)stack.Pop();
                context.AddVariable(new VariableExp(variableName, valueVariable));

                return;
            }

            if (expression.StartsWith("int"))
            {
                expression = expression.SkipWord("int");

                N();

                var intInterpratator = new IntInterpretator(context);
                var expressionString = Regex.Match(expression, "^.+;").Value;
                var valueVariable = intInterpratator.Run(expressionString.Remove(expressionString.Length - 1, 1));

                expression = expression.SkipWord(expressionString);

                var variableName = (string)stack.Pop();
                context.AddVariable(new VariableExp(variableName, valueVariable));

                return;
            }

            if (expression.StartsWith("real"))
            {
                expression = expression.SkipWord("real");

                N();

                var realInterpretator = new RealInterpretator(context);
                var expressionString = Regex.Match(expression, "^.+;").Value;
                var valueVariable = realInterpretator.Run(expression.Remove(expressionString.Length - 1, 1));

                expression = expression.SkipWord(expressionString);

                var variableName = (string) stack.Pop();
                context.AddVariable(new VariableExp(variableName, valueVariable));

                return;
            }

            if (expression.StartsWith("bool"))
            {
                expression = expression.SkipWord("bool");

                N();

                var boolInterpretator = new BoolInterpretator(context);
                var expressionString = Regex.Match(expression, "^.+").Value;
                var valueVariable = boolInterpretator.Run(expression.Remove(expressionString.Length - 1, 1));

                expression = expression.SkipWord(expressionString);

                var variableName = (string) stack.Pop();
                context.AddVariable(new VariableExp(variableName, valueVariable));

                return;
            }

            if (expression.StartsWith("string"))
            {
                expression = expression.SkipWord("string");

                N();

                var stringInterpretator = new StringInterpretator(context);
                var expressionString = Regex.Match(expression, "^.+").Value;
                var valueVariable = stringInterpretator.Run(expression.Remove(expressionString.Length - 1, 1));

                expression = expression.SkipWord(expressionString);

                var variableName = (string) stack.Peek();
                context.AddVariable(new VariableExp(variableName, valueVariable));
            }

            if (Regex.IsMatch(expression, PATTERN_VARIABLE_NAME))
            {
                N();

                var variableName = (string)stack.Peek();
                var variable = context.Lookup(variableName);
                string expressionNewValue = null;

                if (variable.Value is int)
                {
                    var interpretatorInt = new IntInterpretator(context);
                    expressionNewValue = Regex.Match(expression, "^.+").Value.Replace(";", string.Empty);
                    variable.Value = interpretatorInt.Run(expressionNewValue);
                }
                else if (variable.Value is double)
                {
                    var interpretatorReal = new RealInterpretator(context);
                    expressionNewValue = Regex.Match(expression, "^.+").Value.Replace(";", string.Empty);
                    variable.Value = interpretatorReal.Run(expressionNewValue);
                }
                else if (variable.Value is bool)
                {
                    var interpretatorBool = new BoolInterpretator(context);
                    expressionNewValue = Regex.Match(expression, "^.+").Value.Replace(";", string.Empty);
                    variable.Value = interpretatorBool.Run(expressionNewValue);
                }else if (variable.Value is string)
                {
                    var interpretatorString = new StringInterpretator(context);
                    expressionNewValue = Regex.Match(expression, "^.+").Value.Replace(";", string.Empty);
                    variable.Value = interpretatorString.Run(expressionNewValue);
                }

                expression = expression.SkipWord(expressionNewValue);
            }
        }
Пример #3
0
        public void RealExpTest()
        {
            var context = new Context();
            var interpretator = new RealInterpretator(context);

            var expressionSting = "14.45+0.167";
            var result = interpretator.Run(expressionSting);
            Assert.AreEqual(14.45 + 0.167, result, 0.001);

            expressionSting = "124.31-48.157";
            result = interpretator.Run(expressionSting);
            Assert.AreEqual(124.31 - 48.157, result, 0.001);

            expressionSting = "127.45*0.1257";
            result = interpretator.Run(expressionSting);
            Assert.AreEqual(127.45 * 0.1257, result, 0.001);

            expressionSting = "1584.2251/48.5987";
            result = interpretator.Run(expressionSting);
            Assert.AreEqual(1584.2251 / 48.5987, result, 0.001);

            expressionSting = "(58.25/4.21*59.23+6.1-48.26)/6.26+458.157/45.2655*5988.212*(124.157+52.26*(124.264+263.33*48.65)+1.15-58.55)";
            result = interpretator.Run(expressionSting);
            Assert.AreEqual((58.25 / 4.21 * 59.23 + 6.1 - 48.26) / 6.26 + 458.157 / 45.2655 * 5988.212 * (124.157 + 52.26 * (124.264 + 263.33 * 48.65) + 1.15 - 58.55), result, 0.001);
        }