コード例 #1
0
ファイル: AstBuilderTests.cs プロジェクト: pilgrimzh/Jace
        public void TestSinFunction1()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = "sin", TokenType = TokenType.Text
                },
                new Token()
                {
                    Value = '(', TokenType = TokenType.LeftBracket
                },
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.RightBracket
                }
            });

            Function sineFunction = (Function)operation;

            Assert.AreEqual(new IntegerConstant(2), sineFunction.Arguments.Single());
        }
コード例 #2
0
ファイル: AstBuilderTests.cs プロジェクト: pilgrimzh/Jace
        public void TestBuildInvalidFormula5()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder = new AstBuilder(registry);

            AssertExtensions.ThrowsException <ParseException>(() =>
            {
                Operation operation = builder.Build(new List <Token>()
                {
                    new Token()
                    {
                        Value = 42, TokenType = TokenType.Integer, StartPosition = 0
                    },
                    new Token()
                    {
                        Value = '+', TokenType = TokenType.Operation, StartPosition = 2
                    },
                    new Token()
                    {
                        Value = 8, TokenType = TokenType.Integer, StartPosition = 3
                    },
                    new Token()
                    {
                        Value = 5, TokenType = TokenType.Integer, StartPosition = 4
                    }
                });
            });
        }
コード例 #3
0
ファイル: AstBuilderTests.cs プロジェクト: pilgrimzh/Jace
        public void TestVariable()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 10, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = "var1", TokenType = TokenType.Text
                }
            });

            Multiplication multiplication = (Multiplication)operation;

            Assert.AreEqual(new IntegerConstant(10), multiplication.Argument1);
            Assert.AreEqual(new Variable("var1"), multiplication.Argument2);
        }
コード例 #4
0
ファイル: BasicInterpreterTests.cs プロジェクト: gerfen/Jace
        public void TestBasicInterpreterWithVariables()
        {
            IFunctionRegistry functionRegistry = new MockFunctionRegistry();

            Dictionary <string, double> variables = new Dictionary <string, double>();

            variables.Add("var1", 2);
            variables.Add("age", 4);

            IExecutor interpreter = new Interpreter();
            // var1 + 2 * (3 * age)
            double result = interpreter.Execute(
                new Addition(DataType.FloatingPoint,
                             new Variable("var1"),
                             new Multiplication(
                                 DataType.FloatingPoint,
                                 new IntegerConstant(2),
                                 new Multiplication(
                                     DataType.FloatingPoint,
                                     new IntegerConstant(3),
                                     new Variable("age")))), functionRegistry, variables);

#if !NETCORE
            Assert.AreEqual(26.0, result);
#else
            Assert.Equal(26.0, result);
#endif
        }
コード例 #5
0
        public void TestSinFunction1()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry, false);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = "sin", TokenType = TokenType.Identifier
                },
                new Token()
                {
                    Value = '(', TokenType = TokenType.OpenParentheses
                },
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.CloseParentheses
                }
            });

            Function sineFunction = (Function)operation;

            Assert.AreEqual(new IntegerConstant(2), sineFunction.Arguments.Single());
        }
コード例 #6
0
ファイル: AstBuilderTests.cs プロジェクト: pilgrimzh/Jace
        public void TestDivision()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 10, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '/', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                }
            });

            Assert.AreEqual(typeof(Division), operation.GetType());

            Division division = (Division)operation;

            Assert.AreEqual(new IntegerConstant(10), division.Dividend);
            Assert.AreEqual(new IntegerConstant(2), division.Divisor);
        }
コード例 #7
0
        public void TestBuildInvalidFormula4()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder = new AstBuilder(registry);

            Assert.ThrowsException <ParseException>(() =>
            {
                Operation operation = builder.Build(new List <Token>()
                {
                    new Token()
                    {
                        Value = 5.0f, TokenType = TokenType.FloatingPoint, StartPosition = 0
                    },
                    new Token()
                    {
                        Value = 42.0f, TokenType = TokenType.FloatingPoint, StartPosition = 1
                    },
                    new Token()
                    {
                        Value = '+', TokenType = TokenType.Operation, StartPosition = 3
                    },
                    new Token()
                    {
                        Value = 8.0f, TokenType = TokenType.FloatingPoint, StartPosition = 4
                    }
                });
            });
        }
コード例 #8
0
ファイル: AstBuilderTests.cs プロジェクト: pilgrimzh/Jace
        public void TestMultiplication()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 10, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 2.0, TokenType = TokenType.FloatingPoint
                }
            });

            Multiplication multiplication = (Multiplication)operation;

            Assert.AreEqual(new IntegerConstant(10), multiplication.Argument1);
            Assert.AreEqual(new FloatingPointConstant(2.0), multiplication.Argument2);
        }
コード例 #9
0
ファイル: AstBuilderTests.cs プロジェクト: pilgrimzh/Jace
        public void TestModulo()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 2.7, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = '%', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 3, TokenType = TokenType.Integer
                }
            });

            Modulo modulo = (Modulo)operation;

            Assert.AreEqual(new FloatingPointConstant(2.7), modulo.Dividend);
            Assert.AreEqual(new IntegerConstant(3), modulo.Divisor);
        }
コード例 #10
0
ファイル: AstBuilderTests.cs プロジェクト: pilgrimzh/Jace
        public void TestExponentiation()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '^', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 3, TokenType = TokenType.Integer
                }
            });

            Exponentiation exponentiation = (Exponentiation)operation;

            Assert.AreEqual(new IntegerConstant(2), exponentiation.Base);
            Assert.AreEqual(new IntegerConstant(3), exponentiation.Exponent);
        }
コード例 #11
0
        public void TestUnaryMinus()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 5.3, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = '_', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = '(', TokenType = TokenType.LeftBracket
                },
                new Token()
                {
                    Value = 5, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 42, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.RightBracket
                },
            });

            Multiplication multiplication = (Multiplication)operation;

#if !NETCORE
            Assert.AreEqual(new FloatingPointConstant(5.3), multiplication.Argument1);
#else
            Assert.Equal(new FloatingPointConstant(5.3), multiplication.Argument1);
#endif
            UnaryMinus unaryMinus = (UnaryMinus)multiplication.Argument2;
            Addition   addition   = (Addition)unaryMinus.Argument;
#if !NETCORE
            Assert.AreEqual(new IntegerConstant(5), addition.Argument1);
            Assert.AreEqual(new IntegerConstant(42), addition.Argument2);
#else
            Assert.Equal(new IntegerConstant(5), addition.Argument1);
            Assert.Equal(new IntegerConstant(42), addition.Argument2);
#endif
        }
コード例 #12
0
        public void TestSinFunction3()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = "sin", TokenType = TokenType.Text
                },
                new Token()
                {
                    Value = '(', TokenType = TokenType.LeftBracket
                },
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 3, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.RightBracket
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 4.9, TokenType = TokenType.FloatingPoint
                }
            });

            Multiplication multiplication = (Multiplication)operation;

            Function sineFunction = (Function)multiplication.Argument1;

            Addition addition = (Addition)sineFunction.Arguments.Single();

#if !NETCORE
            Assert.AreEqual(new IntegerConstant(2), addition.Argument1);
            Assert.AreEqual(new IntegerConstant(3), addition.Argument2);
            Assert.AreEqual(new FloatingPointConstant(4.9), multiplication.Argument2);
#else
            Assert.Equal(new IntegerConstant(2), addition.Argument1);
            Assert.Equal(new IntegerConstant(3), addition.Argument2);
            Assert.Equal(new FloatingPointConstant(4.9), multiplication.Argument2);
#endif
        }
コード例 #13
0
        public void TestBasicInterpreterSubstraction()
        {
            IFunctionRegistry functionRegistry = new MockFunctionRegistry();

            IExecutor executor = new Interpreter();
            float     result   = executor.Execute(new Substraction(
                                                      new FloatingPointConstant(6),
                                                      new FloatingPointConstant(9)), functionRegistry);

            Assert.AreEqual(-3.0f, result);
        }
コード例 #14
0
        public void TestBuildInvalidFormula3()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder = new AstBuilder(registry);

#if !NETCORE
            AssertExtensions.ThrowsException <ParseException>(() =>
            {
                Operation operation = builder.Build(new List <Token>()
                {
                    new Token()
                    {
                        Value = '(', TokenType = TokenType.LeftBracket, StartPosition = 0
                    },
                    new Token()
                    {
                        Value = 42, TokenType = TokenType.Integer, StartPosition = 1
                    },
                    new Token()
                    {
                        Value = '+', TokenType = TokenType.Operation, StartPosition = 3
                    },
                    new Token()
                    {
                        Value = 8, TokenType = TokenType.Integer, StartPosition = 4
                    }
                });
            });
#else
            Assert.Throws <ParseException>(() =>
            {
                Operation operation = builder.Build(new List <Token>()
                {
                    new Token()
                    {
                        Value = '(', TokenType = TokenType.LeftBracket, StartPosition = 0
                    },
                    new Token()
                    {
                        Value = 42, TokenType = TokenType.Integer, StartPosition = 1
                    },
                    new Token()
                    {
                        Value = '+', TokenType = TokenType.Operation, StartPosition = 3
                    },
                    new Token()
                    {
                        Value = 8, TokenType = TokenType.Integer, StartPosition = 4
                    }
                });
            });
#endif
        }
コード例 #15
0
ファイル: AstBuilderTests.cs プロジェクト: pilgrimzh/Jace
        public void TestMultipleVariable()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = "var1", TokenType = TokenType.Text
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = '(', TokenType = TokenType.LeftBracket
                },
                new Token()
                {
                    Value = 3, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = "age", TokenType = TokenType.Text
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.RightBracket
                }
            });

            Addition       addition        = (Addition)operation;
            Multiplication multiplication1 = (Multiplication)addition.Argument2;
            Multiplication multiplication2 = (Multiplication)multiplication1.Argument2;

            Assert.AreEqual(new Variable("var1"), addition.Argument1);
            Assert.AreEqual(new IntegerConstant(2), multiplication1.Argument1);
            Assert.AreEqual(new IntegerConstant(3), multiplication2.Argument1);
            Assert.AreEqual(new Variable("age"), multiplication2.Argument2);
        }
コード例 #16
0
        public void TestBasicInterpreterSubstraction()
        {
            IFunctionRegistry functionRegistry = new MockFunctionRegistry();

            IExecutor executor = new Interpreter();
            double    result   = executor.Execute(new Subtraction(
                                                      DataType.Integer,
                                                      new IntegerConstant(6),
                                                      new IntegerConstant(9)), functionRegistry);

            Assert.AreEqual(-3.0, result);
        }
コード例 #17
0
        public void TestUnaryMinus()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry, false);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 5.3, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = '_', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = '(', TokenType = TokenType.OpenParentheses
                },
                new Token()
                {
                    Value = 5, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 42, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.CloseParentheses
                },
            });

            Multiplication multiplication = (Multiplication)operation;

            Assert.AreEqual(new FloatingPointConstant(5.3), multiplication.Argument1);

            UnaryMinus unaryMinus = (UnaryMinus)multiplication.Argument2;

            Addition addition = (Addition)unaryMinus.Argument;

            Assert.AreEqual(new IntegerConstant(5), addition.Argument1);
            Assert.AreEqual(new IntegerConstant(42), addition.Argument2);
        }
コード例 #18
0
        public void TestSinFunction3()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry, false);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = "sin", TokenType = TokenType.Identifier
                },
                new Token()
                {
                    Value = '(', TokenType = TokenType.OpenParentheses
                },
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 3, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.CloseParentheses
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 4.9, TokenType = TokenType.FloatingPoint
                }
            });

            Multiplication multiplication = (Multiplication)operation;

            Function sineFunction = (Function)multiplication.Argument1;

            Addition addition = (Addition)sineFunction.Arguments.Single();

            Assert.AreEqual(new IntegerConstant(2), addition.Argument1);
            Assert.AreEqual(new IntegerConstant(3), addition.Argument2);

            Assert.AreEqual(new FloatingPointConstant(4.9), multiplication.Argument2);
        }
コード例 #19
0
        public void TestBuildFormula1()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = '(', TokenType = TokenType.LeftBracket
                },
                new Token()
                {
                    Value = 42, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 8, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.RightBracket
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                }
            });

            Multiplication multiplication = (Multiplication)operation;
            Addition       addition       = (Addition)multiplication.Argument1;

#if !NETCORE
            Assert.AreEqual(42, ((Constant <int>)addition.Argument1).Value);
            Assert.AreEqual(8, ((Constant <int>)addition.Argument2).Value);
            Assert.AreEqual(2, ((Constant <int>)multiplication.Argument2).Value);
#else
            Assert.Equal(42, ((Constant <int>)addition.Argument1).Value);
            Assert.Equal(8, ((Constant <int>)addition.Argument2).Value);
            Assert.Equal(2, ((Constant <int>)multiplication.Argument2).Value);
#endif
        }
コード例 #20
0
        public void TestBasicInterpreter1()
        {
            IFunctionRegistry functionRegistry = new MockFunctionRegistry();

            IExecutor executor = new Interpreter();
            // 6 + (2 * 4)
            float result = executor.Execute(
                new Addition(
                    new FloatingPointConstant(6),
                    new Multiplication(
                        new FloatingPointConstant(2),
                        new FloatingPointConstant(4))), functionRegistry);

            Assert.AreEqual(14.0f, result);
        }
コード例 #21
0
        public void TestBasicInterpreter1()
        {
            IFunctionRegistry functionRegistry = new MockFunctionRegistry();

            IExecutor executor = new Interpreter();
            // 6 + (2 * 4)
            double result = executor.Execute(
                new Addition(
                    DataType.Integer,
                    new IntegerConstant(6),
                    new Multiplication(
                        DataType.Integer,
                        new IntegerConstant(2),
                        new IntegerConstant(4))), functionRegistry);

            Assert.AreEqual(14.0, result);
        }
コード例 #22
0
        public void TestBuildFormula1()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = '(', TokenType = TokenType.LeftBracket
                },
                new Token()
                {
                    Value = 42.0f, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 8.0f, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.RightBracket
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 2.0f, TokenType = TokenType.FloatingPoint
                }
            });

            Multiplication multiplication = (Multiplication)operation;
            Addition       addition       = (Addition)multiplication.Argument1;

            Assert.AreEqual(42, ((FloatingPointConstant)addition.Argument1).Value);
            Assert.AreEqual(8, ((FloatingPointConstant)addition.Argument2).Value);
            Assert.AreEqual(2, ((FloatingPointConstant)multiplication.Argument2).Value);
        }
コード例 #23
0
        public void TestBuildFormula1()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry, false);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = '(', TokenType = TokenType.OpenParentheses
                },
                new Token()
                {
                    Value = 42, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 8, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.CloseBracket
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                }
            });

            Multiplication multiplication = (Multiplication)operation;
            Addition       addition       = (Addition)multiplication.Argument1;

            Assert.AreEqual(42, ((Constant <int>)addition.Argument1).Value);
            Assert.AreEqual(8, ((Constant <int>)addition.Argument2).Value);
            Assert.AreEqual(2, ((Constant <int>)multiplication.Argument2).Value);
        }
コード例 #24
0
        public void TestBuildFormula3()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 8, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '-', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 3, TokenType = TokenType.Integer
                }
            });

            Subtraction    substraction   = (Subtraction)operation;
            Multiplication multiplication = (Multiplication)substraction.Argument1;

#if !NETCORE
            Assert.AreEqual(3, ((Constant <int>)substraction.Argument2).Value);
            Assert.AreEqual(2, ((Constant <int>)multiplication.Argument1).Value);
            Assert.AreEqual(8, ((Constant <int>)multiplication.Argument2).Value);
#else
            Assert.Equal(3, ((Constant <int>)substraction.Argument2).Value);
            Assert.Equal(2, ((Constant <int>)multiplication.Argument1).Value);
            Assert.Equal(8, ((Constant <int>)multiplication.Argument2).Value);
#endif
        }
コード例 #25
0
        public void TestSinFunction2()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = "sin", TokenType = TokenType.Text
                },
                new Token()
                {
                    Value = '(', TokenType = TokenType.LeftBracket
                },
                new Token()
                {
                    Value = 2.0f, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 3.0f, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.RightBracket
                }
            });

            Function sineFunction = (Function)operation;

            Addition addition = (Addition)sineFunction.Arguments.Single();

            Assert.AreEqual(new FloatingPointConstant(2), addition.Argument1);
            Assert.AreEqual(new FloatingPointConstant(3), addition.Argument2);
        }
コード例 #26
0
        public void TestBasicInterpreterWithVariables()
        {
            IFunctionRegistry functionRegistry = new MockFunctionRegistry();

            Dictionary <string, float> variables = new Dictionary <string, float>();

            variables.Add("var1", 2);
            variables.Add("age", 4);

            IExecutor interpreter = new Interpreter();
            // var1 + 2 * (3 * age)
            float result = interpreter.Execute(
                new Addition(
                    new Variable("var1"),
                    new Multiplication(
                        new FloatingPointConstant(2),
                        new Multiplication(
                            new FloatingPointConstant(3),
                            new Variable("age")))), functionRegistry, variables);

            Assert.AreEqual(26.0f, result);
        }
コード例 #27
0
        public void TestBuildInvalidFormula1()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder = new AstBuilder(registry, false);

            AssertExtensions.ThrowsException <ParseException>(() =>
            {
                Operation operation = builder.Build(new List <Token>()
                {
                    new Token()
                    {
                        Value = '(', TokenType = TokenType.OpenParentheses, StartPosition = 0
                    },
                    new Token()
                    {
                        Value = 42, TokenType = TokenType.Integer, StartPosition = 1
                    },
                    new Token()
                    {
                        Value = '+', TokenType = TokenType.Operation, StartPosition = 3
                    },
                    new Token()
                    {
                        Value = 8, TokenType = TokenType.Integer, StartPosition = 4
                    },
                    new Token()
                    {
                        Value = ')', TokenType = TokenType.CloseParentheses, StartPosition = 5
                    },
                    new Token()
                    {
                        Value = '*', TokenType = TokenType.Operation, StartPosition = 6
                    },
                });
            });
        }
コード例 #28
0
ファイル: AstBuilderTests.cs プロジェクト: pilgrimzh/Jace
        public void TestBuildFormula2()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 2, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '+', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 8, TokenType = TokenType.Integer
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 3, TokenType = TokenType.Integer
                }
            });

            Addition       addition       = (Addition)operation;
            Multiplication multiplication = (Multiplication)addition.Argument2;

            Assert.AreEqual(2, ((Constant <int>)addition.Argument1).Value);
            Assert.AreEqual(8, ((Constant <int>)multiplication.Argument1).Value);
            Assert.AreEqual(3, ((Constant <int>)multiplication.Argument2).Value);
        }
コード例 #29
0
        public void TestBuildFormula3()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder   = new AstBuilder(registry);
            Operation  operation = builder.Build(new List <Token>()
            {
                new Token()
                {
                    Value = 2.0f, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = '*', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 8.0f, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = '-', TokenType = TokenType.Operation
                },
                new Token()
                {
                    Value = 3.0f, TokenType = TokenType.FloatingPoint
                }
            });

            Substraction   substraction   = (Substraction)operation;
            Multiplication multiplication = (Multiplication)substraction.Argument1;

            Assert.AreEqual(3, ((FloatingPointConstant)substraction.Argument2).Value);
            Assert.AreEqual(2, ((FloatingPointConstant)multiplication.Argument1).Value);
            Assert.AreEqual(8, ((FloatingPointConstant)multiplication.Argument2).Value);
        }