Exemplo n.º 1
0
        public void TestBuildInvalidFormula5()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder = new AstBuilder(registry);

#if !NETCORE
            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
                    }
                });
            });
#else
            Assert.Throws <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
                    }
                });
            });
#endif
        }
Exemplo n.º 2
0
        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;

#if !NETCORE
            Assert.AreEqual(new IntegerConstant(2), sineFunction.Arguments.Single());
#else
            Assert.Equal(new IntegerConstant(2), sineFunction.Arguments.Single());
#endif
        }
Exemplo n.º 3
0
        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
                }
            });

#if !NETCORE
            Assert.AreEqual(typeof(Division), operation.GetType());
#else
            Assert.Equal(typeof(Division), operation.GetType());
#endif
            Division division = (Division)operation;
#if !NETCORE
            Assert.AreEqual(new IntegerConstant(10), division.Dividend);
            Assert.AreEqual(new IntegerConstant(2), division.Divisor);
#else
            Assert.Equal(new IntegerConstant(10), division.Dividend);
            Assert.Equal(new IntegerConstant(2), division.Divisor);
#endif
        }
Exemplo n.º 4
0
 public void TestBuildInvalidFormula2()
 {
     AstBuilder builder   = new AstBuilder();
     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 = ')', TokenType = TokenType.RightBracket, StartPosition = 4
         },
         new Token()
         {
             Value = '*', TokenType = TokenType.Operation, StartPosition = 5
         },
         new Token()
         {
             Value = 2, TokenType = TokenType.Integer, StartPosition = 6
         },
     });
 }
Exemplo n.º 5
0
        public void TestBuildInvalidFormula5()
        {
            AstBuilder builder = new AstBuilder();

            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
                    }
                });
            });
        }
Exemplo n.º 6
0
        public void TestBuildInvalidFormula1()
        {
            AstBuilder builder = new AstBuilder();

            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
                    },
                    new Token()
                    {
                        Value = ')', TokenType = TokenType.RightBracket, StartPosition = 5
                    },
                    new Token()
                    {
                        Value = '*', TokenType = TokenType.Operation, StartPosition = 6
                    },
                });
            });
        }
Exemplo n.º 7
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());
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        private void calculateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClearScreen();

                string formula = formulaTextBox.Text;

                TokenReader  reader = new TokenReader(CultureInfo.InvariantCulture);
                List <Token> tokens = reader.Read(formula);

                ShowTokens(tokens);

                AstBuilder astBuilder = new AstBuilder();
                Operation  operation  = astBuilder.Build(tokens);

                ShowAbstractSyntaxTree(operation);

                Dictionary <string, double> variables = new Dictionary <string, double>();
                foreach (Variable variable in GetVariables(operation))
                {
                    double value = AskValueOfVariable(variable);
                    variables.Add(variable.Name, value);
                }

                IExecutor executor = new Interpreter();
                double    result   = executor.Execute(operation, variables);

                resultTextBox.Text = "" + result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemplo n.º 10
0
        public void TestDivision()
        {
            AstBuilder builder   = new AstBuilder();
            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);
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        public void TestBuildInvalidFormula3()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder = new AstBuilder(registry);

            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
                    }
                });
            });
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        /// <summary>Parse the address formula.</summary>
        /// <param name="addressFormula">The address formula.</param>
        /// <returns>The result of the parsed address or <see cref="IntPtr.Zero"/>.</returns>
        public IntPtr ParseAddress(string addressFormula)
        {
            Contract.Requires(addressFormula != null);

            if (!formulaCache.TryGetValue(addressFormula, out var func))
            {
                var reader = new TokenReader();
                var tokens = reader.Read(addressFormula);

                var astBuilder = new AstBuilder();
                var operation  = astBuilder.Build(tokens);

                if (operation == null)
                {
                    return(IntPtr.Zero);
                }

                var compiler = new DynamicCompiler();
                func = compiler.CompileAddressFormula(operation);

                formulaCache.Add(addressFormula, func);
            }

            return(func(this));
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        private void calculateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClearScreen();

                string formula = formulaTextBox.Text;

                TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
                List<Token> tokens = reader.Read(formula);

                ShowTokens(tokens);

                AstBuilder astBuilder = new AstBuilder();
                Operation operation = astBuilder.Build(tokens);

                ShowAbstractSyntaxTree(operation);

                Dictionary<string, double> variables = new Dictionary<string, double>();
                foreach (Variable variable in GetVariables(operation))
                {
                    double value = AskValueOfVariable(variable);
                    variables.Add(variable.Name, value);
                }

                IExecutor executor = new Interpreter();
                double result = executor.Execute(operation, variables);

                resultTextBox.Text = "" + result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemplo n.º 17
0
        public void TestBuildInvalidFormula5()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

            AstBuilder builder = new AstBuilder(registry);

            Assert.ThrowsException <ParseException>(() =>
            {
                Operation operation = builder.Build(new List <Token>()
                {
                    new Token()
                    {
                        Value = 42.0f, TokenType = TokenType.FloatingPoint, StartPosition = 0
                    },
                    new Token()
                    {
                        Value = '+', TokenType = TokenType.Operation, StartPosition = 2
                    },
                    new Token()
                    {
                        Value = 8.0f, TokenType = TokenType.FloatingPoint, StartPosition = 3
                    },
                    new Token()
                    {
                        Value = 5.0f, TokenType = TokenType.FloatingPoint, StartPosition = 4
                    }
                });
            });
        }
Exemplo n.º 18
0
        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.0f, TokenType = TokenType.FloatingPoint
                },
                new Token()
                {
                    Value = ')', TokenType = TokenType.RightBracket
                }
            });

            Function sineFunction = (Function)operation;

            Assert.AreEqual(new FloatingPointConstant(2), sineFunction.Arguments.Single());
        }
Exemplo n.º 19
0
        public void TestDivision()
        {
            IFunctionRegistry registry = new MockFunctionRegistry();

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

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

            Division division = (Division)operation;

            Assert.AreEqual(new FloatingPointConstant(10), division.Dividend);
            Assert.AreEqual(new FloatingPointConstant(2), division.Divisor);
        }
Exemplo n.º 20
0
        public void TestSinFunction1()
        {
            AstBuilder builder   = new AstBuilder();
            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());
        }
Exemplo n.º 21
0
        public void TestBuildFormula3()
        {
            AstBuilder builder   = new AstBuilder();
            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
                }
            });

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

            Assert.AreEqual(3, ((Constant <int>)substraction.Argument2).Value);
            Assert.AreEqual(2, ((Constant <int>)multiplication.Argument1).Value);
            Assert.AreEqual(8, ((Constant <int>)multiplication.Argument2).Value);
        }
Exemplo n.º 22
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
        }
Exemplo n.º 23
0
        public static AssemblyDefinition CompileAssembly(string content, CompilationOptions options)
        {
            var tokens     = Scanner.Scan(content);
            var parseTree  = Parser.Parse(tokens);
            var syntaxTree = AstBuilder.Build(parseTree);
            var context    = new CompilationContext(syntaxTree, options);

            return(AssemblyOptimizer.Optimize(context.Assembly));
        }
Exemplo n.º 24
0
        private static AstFile BuildFile(FileContext file, CompilerContext context)
        {
            var builder = new AstBuilder(context);
            var astFile = builder.Build(file, ModuleName);

            PrintErrors(builder.Errors);
            builder.HasErrors.Should().BeFalse();
            return(astFile);
        }
Exemplo n.º 25
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
        }
Exemplo n.º 26
0
        public void Test2()
        {
            var builder = new AstBuilder();

            builder.WithUsing()
            .WithNamespaceName("System");

            var namespaceBuilder = builder.WithNamespace()
                                   .WithName("TestNamespace");

            var classBuilder = namespaceBuilder.WithClass()
                               .WithName("FooBar")
                               .WithVisibility(AccessType.Public)
                               .IsPartial(true);

            var fooProperty = classBuilder.WithProperty()
                              .WithName("Foo")
                              .WithType("int")
                              .WithVisibility(AccessType.Public)
                              .WithSetVisibility(AccessType.Public);

            var methodBuilder = classBuilder.WithMethod()
                                .WithVisibility(AccessType.Public)
                                .WithReturnType("void")
                                .WithName("Serialize");

            var bufferParameter = methodBuilder.WithParameter()
                                  .WithName("buffer")
                                  .WithType("IWriteableBuffer");

            var body = methodBuilder.WithBody()
                       .WithLine("{1}.WriteInt32({0});", fooProperty.Name, bufferParameter.Name);

            methodBuilder = classBuilder.WithMethod()
                            .WithVisibility(AccessType.Public)
                            .WithReturnType("void")
                            .WithName("Deserialize");

            bufferParameter = methodBuilder.WithParameter()
                              .WithName("buffer")
                              .WithType("IReadableBuffer");


            body = methodBuilder.WithBody()
                   .WithLine("{0} = {1}.ReadInt32();", fooProperty.Name, bufferParameter.Name);

            var ast = builder.Build();

            var renderer = new CSharpRenderer();

            var result = renderer.Render(ast);

            var location = System.Reflection.Assembly.GetExecutingAssembly().Location;

            System.IO.File.WriteAllText(location + ".result2.cs", result);
        }
Exemplo n.º 27
0
        public void ItBuildsWithAClass()
        {
            var builder = new AstBuilder();

            builder.WithClass();

            var result = builder.Build();

            Assert.AreEqual(1, result.Classes.Length);
        }
Exemplo n.º 28
0
        public void ItBuildsWithAUsing()
        {
            var builder = new AstBuilder();

            builder.WithUsing();

            var result = builder.Build();

            Assert.AreEqual(1, result.Usings.Length);
        }
Exemplo n.º 29
0
        public void ItBuildsWithANamespace()
        {
            var builder = new AstBuilder();

            builder.WithNamespace();

            var result = builder.Build();

            Assert.AreEqual(1, result.Namespaces.Length);
        }
Exemplo n.º 30
0
        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);
        }
Exemplo n.º 31
0
        private Operation BuildAbstractSyntaxTree(string formulaText)
        {
            TokenReader  tokenReader = new TokenReader();
            List <Token> tokens      = tokenReader.Read(formulaText);

            AstBuilder astBuilder = new AstBuilder(FunctionRegistry);
            Operation  operation  = astBuilder.Build(tokens);

            Operation optimized = optimizer.Optimize(operation, this.FunctionRegistry);

            return(optimized);
        }
Exemplo n.º 32
0
        public void TestBuildFormula3()
        {
            AstBuilder builder = new AstBuilder();
            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 }
            });

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

            Assert.AreEqual(3, ((Constant<int>)substraction.Argument2).Value);
            Assert.AreEqual(2, ((Constant<int>)multiplication.Argument1).Value);
            Assert.AreEqual(8, ((Constant<int>)multiplication.Argument2).Value);
        }
Exemplo n.º 33
0
        public void TestBuildFormula1()
        {
            AstBuilder builder = new AstBuilder();
            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;

            Assert.AreEqual(42, ((Constant<int>)addition.Argument1).Value);
            Assert.AreEqual(8, ((Constant<int>)addition.Argument2).Value);
            Assert.AreEqual(2, ((Constant<int>)multiplication.Argument2).Value);
        }
Exemplo n.º 34
0
        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);
        }
Exemplo n.º 35
0
 public void TestBuildInvalidFormula5()
 {
     AstBuilder builder = new AstBuilder();
     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 }
     });
 }
Exemplo n.º 36
0
 public void TestBuildInvalidFormula2()
 {
     AstBuilder builder = new AstBuilder();
     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 = ')', TokenType = TokenType.RightBracket, StartPosition = 4 },
         new Token() { Value = '*', TokenType = TokenType.Operation, StartPosition = 5 },
         new Token() { Value = 2, TokenType = TokenType.Integer, StartPosition = 6 },
     });
 }
Exemplo n.º 37
0
        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 }
                    });
                });
        }
Exemplo n.º 38
0
        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);
        }
Exemplo n.º 39
0
        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);
        }
Exemplo n.º 40
0
        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);
        }
Exemplo n.º 41
0
        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);
        }
Exemplo n.º 42
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;
            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);
        }
Exemplo n.º 43
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();
            Assert.AreEqual(new IntegerConstant(2), addition.Argument1);
            Assert.AreEqual(new IntegerConstant(3), addition.Argument2);

            Assert.AreEqual(new FloatingPointConstant(4.9), multiplication.Argument2);
        }
Exemplo n.º 44
0
        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());
        }
Exemplo n.º 45
0
        public void TestBuildInvalidFormula1()
        {
            AstBuilder builder = new AstBuilder();

            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 },
                        new Token() { Value = ')', TokenType = TokenType.RightBracket, StartPosition = 5 },
                        new Token() { Value = '*', TokenType = TokenType.Operation, StartPosition = 6 },
                    });
                });
        }
Exemplo n.º 46
0
        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);
        }
Exemplo n.º 47
0
        public void TestSinFunction2()
        {
            AstBuilder builder = new AstBuilder();
            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 }
            });

            Function sineFunction = (Function)operation;

            Addition addition = (Addition)sineFunction.Arguments.Single();
            Assert.AreEqual(new IntegerConstant(2), addition.Argument1);
            Assert.AreEqual(new IntegerConstant(3), addition.Argument2);
        }
Exemplo n.º 48
0
        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);
        }