예제 #1
0
        public void BoolExpTest()
        {
            var context = new Context();
            var interpretator = new BoolInterpretator(context);

            var expressionString = "true && false";
            var result = interpretator.Run(expressionString);
            Assert.AreEqual(false, result);

            expressionString = "true && false || true";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(true, result);

            expressionString = "true && (false || true)";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(true, result);

            expressionString = "true && false || true && (!true || false)";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(false, result);

            expressionString = "true && (!false && (true || !false && true)) || false";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(true, result);
        }
예제 #2
0
        public void BooleanHardoceTest()
        {
            var context = new Context();
            var interpretator = new BoolInterpretator(context);

            var expressionString = "5 >= 4 && 5*4 != 21 && (\"Apple\" == \"Apple\")";
            var result = interpretator.Run(expressionString);
            Assert.AreEqual(5 >= 4 && 5*4 != 21 && ("Apple" == "Apple"), result);

            expressionString = "(5-3) == (12-10)";
            result = interpretator.Run(expressionString);
            Assert.AreEqual((5 - 3) == (12 - 10), result);
        }
        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);
            }
        }
예제 #4
0
        public void StringBooleanTest()
        {
            var context = new Context();
            var interpretator = new BoolInterpretator(context);

            var expressionString = "\"Rainbow\" == \"Rainbow\"";
            var result = interpretator.Run(expressionString);
            Assert.AreEqual(true, result);

            expressionString = "\"Rainbow\" != \"Rainbow\"";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(false, result);

            expressionString = "\"Rainbow\" != \"Rainbow\" || true";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(true, result);

            expressionString = "\"Rainbow\" == \"Dash\" && (\"Apple\" != \"Jack=\" || false)";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(false, result);
        }
예제 #5
0
        public void IntBooleanTest()
        {
            var context = new Context();
            var interpretator = new BoolInterpretator(context);

            var expressionString = "5 == 4";
            var result = interpretator.Run(expressionString);
            Assert.AreEqual(false, result);

            expressionString = "5.56 != 4";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(true, result);

            expressionString = "5.56 > 4";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(true, result);

            expressionString = "5.56 >= 5.56";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(true, result);

            expressionString = "5.56 <= 5.56";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(true, result);

            expressionString = "5.56 < 5.56";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(false, result);

            expressionString = "5.56 <= 5.56 && 5 >= 4 || 34.21 == 54.23";
            result = interpretator.Run(expressionString);
            Assert.AreEqual(5.56 <= 5.56 && 5 >= 4 || 34.21 == 54.23, result);
        }